1 /* $OpenBSD: auth-options.c,v 1.57 2012/12/02 20:46:11 djm Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
15 #include <sys/types.h>
23 #include "openbsd-compat/sys-queue.h"
33 #include "auth-options.h"
39 #include "monitor_wrap.h"
41 /* Flags set authorized_keys flags */
42 int no_port_forwarding_flag = 0;
43 int no_agent_forwarding_flag = 0;
44 int no_x11_forwarding_flag = 0;
47 int key_is_cert_authority = 0;
49 /* "command=" option. */
50 char *forced_command = NULL;
52 /* "environment=" options. */
53 struct envstring *custom_environment = NULL;
55 /* "tunnel=" option. */
56 int forced_tun_device = -1;
58 /* "principals=" option. */
59 char *authorized_principals = NULL;
61 extern ServerOptions options;
64 auth_clear_options(void)
66 no_agent_forwarding_flag = 0;
67 no_port_forwarding_flag = 0;
69 no_x11_forwarding_flag = 0;
71 key_is_cert_authority = 0;
72 while (custom_environment) {
73 struct envstring *ce = custom_environment;
74 custom_environment = ce->next;
79 xfree(forced_command);
80 forced_command = NULL;
82 if (authorized_principals) {
83 xfree(authorized_principals);
84 authorized_principals = NULL;
86 forced_tun_device = -1;
87 channel_clear_permitted_opens();
91 * return 1 if access is granted, 0 if not.
92 * side effect: sets key option flags
95 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
101 auth_clear_options();
106 while (*opts && *opts != ' ' && *opts != '\t') {
107 cp = "cert-authority";
108 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
109 key_is_cert_authority = 1;
113 cp = "no-port-forwarding";
114 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
115 auth_debug_add("Port forwarding disabled.");
116 no_port_forwarding_flag = 1;
120 cp = "no-agent-forwarding";
121 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
122 auth_debug_add("Agent forwarding disabled.");
123 no_agent_forwarding_flag = 1;
127 cp = "no-X11-forwarding";
128 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
129 auth_debug_add("X11 forwarding disabled.");
130 no_x11_forwarding_flag = 1;
135 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
136 auth_debug_add("Pty allocation disabled.");
142 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
143 auth_debug_add("User rc file execution disabled.");
149 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
151 if (forced_command != NULL)
152 xfree(forced_command);
153 forced_command = xmalloc(strlen(opts) + 1);
158 if (*opts == '\\' && opts[1] == '"') {
160 forced_command[i++] = '"';
163 forced_command[i++] = *opts++;
166 debug("%.100s, line %lu: missing end quote",
168 auth_debug_add("%.100s, line %lu: missing end quote",
170 xfree(forced_command);
171 forced_command = NULL;
174 forced_command[i] = '\0';
175 auth_debug_add("Forced command.");
179 cp = "principals=\"";
180 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
182 if (authorized_principals != NULL)
183 xfree(authorized_principals);
184 authorized_principals = xmalloc(strlen(opts) + 1);
189 if (*opts == '\\' && opts[1] == '"') {
191 authorized_principals[i++] = '"';
194 authorized_principals[i++] = *opts++;
197 debug("%.100s, line %lu: missing end quote",
199 auth_debug_add("%.100s, line %lu: missing end quote",
201 xfree(authorized_principals);
202 authorized_principals = NULL;
205 authorized_principals[i] = '\0';
206 auth_debug_add("principals: %.900s",
207 authorized_principals);
211 cp = "environment=\"";
212 if (options.permit_user_env &&
213 strncasecmp(opts, cp, strlen(cp)) == 0) {
215 struct envstring *new_envstring;
218 s = xmalloc(strlen(opts) + 1);
223 if (*opts == '\\' && opts[1] == '"') {
231 debug("%.100s, line %lu: missing end quote",
233 auth_debug_add("%.100s, line %lu: missing end quote",
239 auth_debug_add("Adding to environment: %.900s", s);
240 debug("Adding to environment: %.900s", s);
242 new_envstring = xmalloc(sizeof(struct envstring));
243 new_envstring->s = s;
244 new_envstring->next = custom_environment;
245 custom_environment = new_envstring;
249 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
250 const char *remote_ip = get_remote_ipaddr();
251 const char *remote_host = get_canonical_hostname(
253 char *patterns = xmalloc(strlen(opts) + 1);
260 if (*opts == '\\' && opts[1] == '"') {
265 patterns[i++] = *opts++;
268 debug("%.100s, line %lu: missing end quote",
270 auth_debug_add("%.100s, line %lu: missing end quote",
277 switch (match_host_and_ip(remote_host, remote_ip,
281 /* Host name matches. */
284 debug("%.100s, line %lu: invalid criteria",
286 auth_debug_add("%.100s, line %lu: "
287 "invalid criteria", file, linenum);
291 logit("Authentication tried for %.100s with "
292 "correct key but not from a permitted "
293 "host (host=%.200s, ip=%.200s).",
294 pw->pw_name, remote_host, remote_ip);
295 auth_debug_add("Your host '%.200s' is not "
296 "permitted to use this key for login.",
303 cp = "permitopen=\"";
304 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
307 char *patterns = xmalloc(strlen(opts) + 1);
314 if (*opts == '\\' && opts[1] == '"') {
319 patterns[i++] = *opts++;
322 debug("%.100s, line %lu: missing end quote",
324 auth_debug_add("%.100s, line %lu: missing "
325 "end quote", file, linenum);
333 if (host == NULL || strlen(host) >= NI_MAXHOST) {
334 debug("%.100s, line %lu: Bad permitopen "
335 "specification <%.100s>", file, linenum,
337 auth_debug_add("%.100s, line %lu: "
338 "Bad permitopen specification", file,
343 host = cleanhostname(host);
344 if (p == NULL || (port = permitopen_port(p)) < 0) {
345 debug("%.100s, line %lu: Bad permitopen port "
346 "<%.100s>", file, linenum, p ? p : "");
347 auth_debug_add("%.100s, line %lu: "
348 "Bad permitopen port", file, linenum);
352 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
353 channel_add_permitted_opens(host, port);
358 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
361 tun = xmalloc(strlen(opts) + 1);
369 debug("%.100s, line %lu: missing end quote",
371 auth_debug_add("%.100s, line %lu: missing end quote",
374 forced_tun_device = -1;
378 forced_tun_device = a2tun(tun, NULL);
380 if (forced_tun_device == SSH_TUNID_ERR) {
381 debug("%.100s, line %lu: invalid tun device",
383 auth_debug_add("%.100s, line %lu: invalid tun device",
385 forced_tun_device = -1;
388 auth_debug_add("Forced tun device: %d", forced_tun_device);
394 * Skip the comma, and move to the next option
395 * (or break out if there are no more).
398 fatal("Bugs in auth-options.c option processing.");
399 if (*opts == ' ' || *opts == '\t')
400 break; /* End of options. */
404 /* Process the next option. */
411 logit("Bad options in %.100s file, line %lu: %.50s",
412 file, linenum, opts);
413 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
414 file, linenum, opts);
420 #define OPTIONS_CRITICAL 1
421 #define OPTIONS_EXTENSIONS 2
423 parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
424 u_int which, int crit,
425 int *cert_no_port_forwarding_flag,
426 int *cert_no_agent_forwarding_flag,
427 int *cert_no_x11_forwarding_flag,
428 int *cert_no_pty_flag,
429 int *cert_no_user_rc,
430 char **cert_forced_command,
431 int *cert_source_address_done)
433 char *command, *allowed;
434 const char *remote_ip;
435 u_char *name = NULL, *data_blob = NULL;
436 u_int nlen, dlen, clen;
442 /* Make copy to avoid altering original */
444 buffer_append(&c, optblob, optblob_len);
446 while (buffer_len(&c) > 0) {
447 if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
448 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
449 error("Certificate options corrupt");
452 buffer_append(&data, data_blob, dlen);
453 debug3("found certificate option \"%.100s\" len %u",
456 if ((which & OPTIONS_EXTENSIONS) != 0) {
457 if (strcmp(name, "permit-X11-forwarding") == 0) {
458 *cert_no_x11_forwarding_flag = 0;
460 } else if (strcmp(name,
461 "permit-agent-forwarding") == 0) {
462 *cert_no_agent_forwarding_flag = 0;
464 } else if (strcmp(name,
465 "permit-port-forwarding") == 0) {
466 *cert_no_port_forwarding_flag = 0;
468 } else if (strcmp(name, "permit-pty") == 0) {
469 *cert_no_pty_flag = 0;
471 } else if (strcmp(name, "permit-user-rc") == 0) {
472 *cert_no_user_rc = 0;
476 if (!found && (which & OPTIONS_CRITICAL) != 0) {
477 if (strcmp(name, "force-command") == 0) {
478 if ((command = buffer_get_cstring_ret(&data,
480 error("Certificate constraint \"%s\" "
484 if (*cert_forced_command != NULL) {
485 error("Certificate has multiple "
486 "force-command options");
490 *cert_forced_command = command;
493 if (strcmp(name, "source-address") == 0) {
494 if ((allowed = buffer_get_cstring_ret(&data,
496 error("Certificate constraint "
497 "\"%s\" corrupt", name);
500 if ((*cert_source_address_done)++) {
501 error("Certificate has multiple "
502 "source-address options");
506 remote_ip = get_remote_ipaddr();
507 switch (addr_match_cidr_list(remote_ip,
515 logit("Authentication tried for %.100s "
516 "with valid certificate but not "
517 "from a permitted host "
518 "(ip=%.200s).", pw->pw_name,
520 auth_debug_add("Your address '%.200s' "
521 "is not permitted to use this "
522 "certificate for login.",
527 error("Certificate source-address "
538 error("Certificate critical option \"%s\" "
539 "is not supported", name);
542 logit("Certificate extension \"%s\" "
543 "is not supported", name);
545 } else if (buffer_len(&data) != 0) {
546 error("Certificate option \"%s\" corrupt "
547 "(extra data)", name);
553 name = data_blob = NULL;
555 /* successfully parsed all options */
560 cert_forced_command != NULL &&
561 *cert_forced_command != NULL) {
562 xfree(*cert_forced_command);
563 *cert_forced_command = NULL;
567 if (data_blob != NULL)
575 * Set options from critical certificate options. These supersede user key
576 * options so this must be called after auth_parse_options().
579 auth_cert_options(Key *k, struct passwd *pw)
581 int cert_no_port_forwarding_flag = 1;
582 int cert_no_agent_forwarding_flag = 1;
583 int cert_no_x11_forwarding_flag = 1;
584 int cert_no_pty_flag = 1;
585 int cert_no_user_rc = 1;
586 char *cert_forced_command = NULL;
587 int cert_source_address_done = 0;
589 if (key_cert_is_legacy(k)) {
590 /* All options are in the one field for v00 certs */
591 if (parse_option_list(buffer_ptr(&k->cert->critical),
592 buffer_len(&k->cert->critical), pw,
593 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
594 &cert_no_port_forwarding_flag,
595 &cert_no_agent_forwarding_flag,
596 &cert_no_x11_forwarding_flag,
599 &cert_forced_command,
600 &cert_source_address_done) == -1)
603 /* Separate options and extensions for v01 certs */
604 if (parse_option_list(buffer_ptr(&k->cert->critical),
605 buffer_len(&k->cert->critical), pw,
606 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
607 &cert_forced_command,
608 &cert_source_address_done) == -1)
610 if (parse_option_list(buffer_ptr(&k->cert->extensions),
611 buffer_len(&k->cert->extensions), pw,
612 OPTIONS_EXTENSIONS, 1,
613 &cert_no_port_forwarding_flag,
614 &cert_no_agent_forwarding_flag,
615 &cert_no_x11_forwarding_flag,
622 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
623 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
624 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
625 no_pty_flag |= cert_no_pty_flag;
626 no_user_rc |= cert_no_user_rc;
627 /* CA-specified forced command supersedes key option */
628 if (cert_forced_command != NULL) {
629 if (forced_command != NULL)
630 xfree(forced_command);
631 forced_command = cert_forced_command;