1 /* $OpenBSD: auth-options.c,v 1.54 2010/12/24 21:41:48 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 /* Throttle log messages. */
62 int logged_from_hostip = 0;
63 int logged_cert_hostip = 0;
65 extern ServerOptions options;
68 auth_start_parse_options(void)
70 logged_from_hostip = 0;
71 logged_cert_hostip = 0;
75 auth_clear_options(void)
77 no_agent_forwarding_flag = 0;
78 no_port_forwarding_flag = 0;
80 no_x11_forwarding_flag = 0;
82 key_is_cert_authority = 0;
83 while (custom_environment) {
84 struct envstring *ce = custom_environment;
85 custom_environment = ce->next;
90 xfree(forced_command);
91 forced_command = NULL;
93 if (authorized_principals) {
94 xfree(authorized_principals);
95 authorized_principals = NULL;
97 forced_tun_device = -1;
98 channel_clear_permitted_opens();
102 * return 1 if access is granted, 0 if not.
103 * side effect: sets key option flags
106 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
112 auth_clear_options();
117 while (*opts && *opts != ' ' && *opts != '\t') {
118 cp = "cert-authority";
119 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
120 key_is_cert_authority = 1;
124 cp = "no-port-forwarding";
125 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
126 auth_debug_add("Port forwarding disabled.");
127 no_port_forwarding_flag = 1;
131 cp = "no-agent-forwarding";
132 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
133 auth_debug_add("Agent forwarding disabled.");
134 no_agent_forwarding_flag = 1;
138 cp = "no-X11-forwarding";
139 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
140 auth_debug_add("X11 forwarding disabled.");
141 no_x11_forwarding_flag = 1;
146 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
147 auth_debug_add("Pty allocation disabled.");
153 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
154 auth_debug_add("User rc file execution disabled.");
160 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
162 if (forced_command != NULL)
163 xfree(forced_command);
164 forced_command = xmalloc(strlen(opts) + 1);
169 if (*opts == '\\' && opts[1] == '"') {
171 forced_command[i++] = '"';
174 forced_command[i++] = *opts++;
177 debug("%.100s, line %lu: missing end quote",
179 auth_debug_add("%.100s, line %lu: missing end quote",
181 xfree(forced_command);
182 forced_command = NULL;
185 forced_command[i] = '\0';
186 auth_debug_add("Forced command.");
190 cp = "principals=\"";
191 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
193 if (authorized_principals != NULL)
194 xfree(authorized_principals);
195 authorized_principals = xmalloc(strlen(opts) + 1);
200 if (*opts == '\\' && opts[1] == '"') {
202 authorized_principals[i++] = '"';
205 authorized_principals[i++] = *opts++;
208 debug("%.100s, line %lu: missing end quote",
210 auth_debug_add("%.100s, line %lu: missing end quote",
212 xfree(authorized_principals);
213 authorized_principals = NULL;
216 authorized_principals[i] = '\0';
217 auth_debug_add("principals: %.900s",
218 authorized_principals);
222 cp = "environment=\"";
223 if (options.permit_user_env &&
224 strncasecmp(opts, cp, strlen(cp)) == 0) {
226 struct envstring *new_envstring;
229 s = xmalloc(strlen(opts) + 1);
234 if (*opts == '\\' && opts[1] == '"') {
242 debug("%.100s, line %lu: missing end quote",
244 auth_debug_add("%.100s, line %lu: missing end quote",
250 auth_debug_add("Adding to environment: %.900s", s);
251 debug("Adding to environment: %.900s", s);
253 new_envstring = xmalloc(sizeof(struct envstring));
254 new_envstring->s = s;
255 new_envstring->next = custom_environment;
256 custom_environment = new_envstring;
260 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
261 const char *remote_ip = get_remote_ipaddr();
262 const char *remote_host = get_canonical_hostname(
264 char *patterns = xmalloc(strlen(opts) + 1);
271 if (*opts == '\\' && opts[1] == '"') {
276 patterns[i++] = *opts++;
279 debug("%.100s, line %lu: missing end quote",
281 auth_debug_add("%.100s, line %lu: missing end quote",
288 switch (match_host_and_ip(remote_host, remote_ip,
292 /* Host name matches. */
295 debug("%.100s, line %lu: invalid criteria",
297 auth_debug_add("%.100s, line %lu: "
298 "invalid criteria", file, linenum);
302 if (!logged_from_hostip) {
303 logit("Authentication tried for %.100s with "
304 "correct key but not from a permitted "
305 "host (host=%.200s, ip=%.200s).",
306 pw->pw_name, remote_host, remote_ip);
307 logged_from_hostip = 1;
309 auth_debug_add("Your host '%.200s' is not "
310 "permitted to use this key for login.",
317 cp = "permitopen=\"";
318 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
321 char *patterns = xmalloc(strlen(opts) + 1);
328 if (*opts == '\\' && opts[1] == '"') {
333 patterns[i++] = *opts++;
336 debug("%.100s, line %lu: missing end quote",
338 auth_debug_add("%.100s, line %lu: missing "
339 "end quote", file, linenum);
347 if (host == NULL || strlen(host) >= NI_MAXHOST) {
348 debug("%.100s, line %lu: Bad permitopen "
349 "specification <%.100s>", file, linenum,
351 auth_debug_add("%.100s, line %lu: "
352 "Bad permitopen specification", file,
357 host = cleanhostname(host);
358 if (p == NULL || (port = a2port(p)) <= 0) {
359 debug("%.100s, line %lu: Bad permitopen port "
360 "<%.100s>", file, linenum, p ? p : "");
361 auth_debug_add("%.100s, line %lu: "
362 "Bad permitopen port", file, linenum);
366 if (options.allow_tcp_forwarding)
367 channel_add_permitted_opens(host, port);
372 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
375 tun = xmalloc(strlen(opts) + 1);
383 debug("%.100s, line %lu: missing end quote",
385 auth_debug_add("%.100s, line %lu: missing end quote",
388 forced_tun_device = -1;
392 forced_tun_device = a2tun(tun, NULL);
394 if (forced_tun_device == SSH_TUNID_ERR) {
395 debug("%.100s, line %lu: invalid tun device",
397 auth_debug_add("%.100s, line %lu: invalid tun device",
399 forced_tun_device = -1;
402 auth_debug_add("Forced tun device: %d", forced_tun_device);
408 * Skip the comma, and move to the next option
409 * (or break out if there are no more).
412 fatal("Bugs in auth-options.c option processing.");
413 if (*opts == ' ' || *opts == '\t')
414 break; /* End of options. */
418 /* Process the next option. */
425 logit("Bad options in %.100s file, line %lu: %.50s",
426 file, linenum, opts);
427 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
428 file, linenum, opts);
434 #define OPTIONS_CRITICAL 1
435 #define OPTIONS_EXTENSIONS 2
437 parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
438 u_int which, int crit,
439 int *cert_no_port_forwarding_flag,
440 int *cert_no_agent_forwarding_flag,
441 int *cert_no_x11_forwarding_flag,
442 int *cert_no_pty_flag,
443 int *cert_no_user_rc,
444 char **cert_forced_command,
445 int *cert_source_address_done)
447 char *command, *allowed;
448 const char *remote_ip;
449 u_char *name = NULL, *data_blob = NULL;
450 u_int nlen, dlen, clen;
456 /* Make copy to avoid altering original */
458 buffer_append(&c, optblob, optblob_len);
460 while (buffer_len(&c) > 0) {
461 if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
462 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
463 error("Certificate options corrupt");
466 buffer_append(&data, data_blob, dlen);
467 debug3("found certificate option \"%.100s\" len %u",
469 if (strlen(name) != nlen) {
470 error("Certificate constraint name contains \\0");
474 if ((which & OPTIONS_EXTENSIONS) != 0) {
475 if (strcmp(name, "permit-X11-forwarding") == 0) {
476 *cert_no_x11_forwarding_flag = 0;
478 } else if (strcmp(name,
479 "permit-agent-forwarding") == 0) {
480 *cert_no_agent_forwarding_flag = 0;
482 } else if (strcmp(name,
483 "permit-port-forwarding") == 0) {
484 *cert_no_port_forwarding_flag = 0;
486 } else if (strcmp(name, "permit-pty") == 0) {
487 *cert_no_pty_flag = 0;
489 } else if (strcmp(name, "permit-user-rc") == 0) {
490 *cert_no_user_rc = 0;
494 if (!found && (which & OPTIONS_CRITICAL) != 0) {
495 if (strcmp(name, "force-command") == 0) {
496 if ((command = buffer_get_cstring_ret(&data,
498 error("Certificate constraint \"%s\" "
502 if (strlen(command) != clen) {
503 error("force-command constraint "
507 if (*cert_forced_command != NULL) {
508 error("Certificate has multiple "
509 "force-command options");
513 *cert_forced_command = command;
516 if (strcmp(name, "source-address") == 0) {
517 if ((allowed = buffer_get_cstring_ret(&data,
519 error("Certificate constraint "
520 "\"%s\" corrupt", name);
523 if (strlen(allowed) != clen) {
524 error("source-address constraint "
528 if ((*cert_source_address_done)++) {
529 error("Certificate has multiple "
530 "source-address options");
534 remote_ip = get_remote_ipaddr();
535 switch (addr_match_cidr_list(remote_ip,
543 if (!logged_cert_hostip) {
544 logit("Authentication tried for %.100s "
545 "with valid certificate but not "
546 "from a permitted host "
547 "(ip=%.200s).", pw->pw_name,
549 logged_cert_hostip = 1;
551 auth_debug_add("Your address '%.200s' "
552 "is not permitted to use this "
553 "certificate for login.",
558 error("Certificate source-address "
569 error("Certificate critical option \"%s\" "
570 "is not supported", name);
573 logit("Certificate extension \"%s\" "
574 "is not supported", name);
576 } else if (buffer_len(&data) != 0) {
577 error("Certificate option \"%s\" corrupt "
578 "(extra data)", name);
584 name = data_blob = NULL;
586 /* successfully parsed all options */
591 cert_forced_command != NULL &&
592 *cert_forced_command != NULL) {
593 xfree(*cert_forced_command);
594 *cert_forced_command = NULL;
598 if (data_blob != NULL)
606 * Set options from critical certificate options. These supersede user key
607 * options so this must be called after auth_parse_options().
610 auth_cert_options(Key *k, struct passwd *pw)
612 int cert_no_port_forwarding_flag = 1;
613 int cert_no_agent_forwarding_flag = 1;
614 int cert_no_x11_forwarding_flag = 1;
615 int cert_no_pty_flag = 1;
616 int cert_no_user_rc = 1;
617 char *cert_forced_command = NULL;
618 int cert_source_address_done = 0;
620 if (key_cert_is_legacy(k)) {
621 /* All options are in the one field for v00 certs */
622 if (parse_option_list(buffer_ptr(&k->cert->critical),
623 buffer_len(&k->cert->critical), pw,
624 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
625 &cert_no_port_forwarding_flag,
626 &cert_no_agent_forwarding_flag,
627 &cert_no_x11_forwarding_flag,
630 &cert_forced_command,
631 &cert_source_address_done) == -1)
634 /* Separate options and extensions for v01 certs */
635 if (parse_option_list(buffer_ptr(&k->cert->critical),
636 buffer_len(&k->cert->critical), pw,
637 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
638 &cert_forced_command,
639 &cert_source_address_done) == -1)
641 if (parse_option_list(buffer_ptr(&k->cert->extensions),
642 buffer_len(&k->cert->extensions), pw,
643 OPTIONS_EXTENSIONS, 1,
644 &cert_no_port_forwarding_flag,
645 &cert_no_agent_forwarding_flag,
646 &cert_no_x11_forwarding_flag,
653 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
654 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
655 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
656 no_pty_flag |= cert_no_pty_flag;
657 no_user_rc |= cert_no_user_rc;
658 /* CA-specified forced command supersedes key option */
659 if (cert_forced_command != NULL) {
660 if (forced_command != NULL)
661 xfree(forced_command);
662 forced_command = cert_forced_command;