1 /* $OpenBSD: auth-options.c,v 1.68 2015/07/03 03:43:18 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"
25 #include "key.h" /* XXX for typedef */
26 #include "buffer.h" /* XXX for typedef */
37 #include "auth-options.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;
80 forced_command = NULL;
82 if (authorized_principals) {
83 free(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 free(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 free(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 free(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 free(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 (strncasecmp(opts, cp, strlen(cp)) == 0) {
214 struct envstring *new_envstring;
217 s = xmalloc(strlen(opts) + 1);
222 if (*opts == '\\' && opts[1] == '"') {
230 debug("%.100s, line %lu: missing end quote",
232 auth_debug_add("%.100s, line %lu: missing end quote",
239 if (options.permit_user_env) {
240 auth_debug_add("Adding to environment: "
242 debug("Adding to environment: %.900s", s);
243 new_envstring = xcalloc(1,
244 sizeof(*new_envstring));
245 new_envstring->s = s;
246 new_envstring->next = custom_environment;
247 custom_environment = new_envstring;
254 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
255 const char *remote_ip = get_remote_ipaddr();
256 const char *remote_host = get_canonical_hostname(
258 char *patterns = xmalloc(strlen(opts) + 1);
265 if (*opts == '\\' && opts[1] == '"') {
270 patterns[i++] = *opts++;
273 debug("%.100s, line %lu: missing end quote",
275 auth_debug_add("%.100s, line %lu: missing end quote",
282 switch (match_host_and_ip(remote_host, remote_ip,
286 /* Host name matches. */
289 debug("%.100s, line %lu: invalid criteria",
291 auth_debug_add("%.100s, line %lu: "
292 "invalid criteria", file, linenum);
296 logit("Authentication tried for %.100s with "
297 "correct key but not from a permitted "
298 "host (host=%.200s, ip=%.200s).",
299 pw->pw_name, remote_host, remote_ip);
300 auth_debug_add("Your host '%.200s' is not "
301 "permitted to use this key for login.",
308 cp = "permitopen=\"";
309 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
312 char *patterns = xmalloc(strlen(opts) + 1);
319 if (*opts == '\\' && opts[1] == '"') {
324 patterns[i++] = *opts++;
327 debug("%.100s, line %lu: missing end quote",
329 auth_debug_add("%.100s, line %lu: missing "
330 "end quote", file, linenum);
337 /* XXX - add streamlocal support */
339 if (host == NULL || strlen(host) >= NI_MAXHOST) {
340 debug("%.100s, line %lu: Bad permitopen "
341 "specification <%.100s>", file, linenum,
343 auth_debug_add("%.100s, line %lu: "
344 "Bad permitopen specification", file,
349 host = cleanhostname(host);
350 if (p == NULL || (port = permitopen_port(p)) < 0) {
351 debug("%.100s, line %lu: Bad permitopen port "
352 "<%.100s>", file, linenum, p ? p : "");
353 auth_debug_add("%.100s, line %lu: "
354 "Bad permitopen port", file, linenum);
358 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
359 channel_add_permitted_opens(host, port);
364 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
367 tun = xmalloc(strlen(opts) + 1);
375 debug("%.100s, line %lu: missing end quote",
377 auth_debug_add("%.100s, line %lu: missing end quote",
380 forced_tun_device = -1;
384 forced_tun_device = a2tun(tun, NULL);
386 if (forced_tun_device == SSH_TUNID_ERR) {
387 debug("%.100s, line %lu: invalid tun device",
389 auth_debug_add("%.100s, line %lu: invalid tun device",
391 forced_tun_device = -1;
394 auth_debug_add("Forced tun device: %d", forced_tun_device);
400 * Skip the comma, and move to the next option
401 * (or break out if there are no more).
404 fatal("Bugs in auth-options.c option processing.");
405 if (*opts == ' ' || *opts == '\t')
406 break; /* End of options. */
410 /* Process the next option. */
417 logit("Bad options in %.100s file, line %lu: %.50s",
418 file, linenum, opts);
419 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
420 file, linenum, opts);
426 #define OPTIONS_CRITICAL 1
427 #define OPTIONS_EXTENSIONS 2
429 parse_option_list(struct sshbuf *oblob, struct passwd *pw,
430 u_int which, int crit,
431 int *cert_no_port_forwarding_flag,
432 int *cert_no_agent_forwarding_flag,
433 int *cert_no_x11_forwarding_flag,
434 int *cert_no_pty_flag,
435 int *cert_no_user_rc,
436 char **cert_forced_command,
437 int *cert_source_address_done)
439 char *command, *allowed;
440 const char *remote_ip;
442 struct sshbuf *c = NULL, *data = NULL;
443 int r, ret = -1, result, found;
445 if ((c = sshbuf_fromb(oblob)) == NULL) {
446 error("%s: sshbuf_fromb failed", __func__);
450 while (sshbuf_len(c) > 0) {
453 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
454 (r = sshbuf_froms(c, &data)) != 0) {
455 error("Unable to parse certificate options: %s",
459 debug3("found certificate option \"%.100s\" len %zu",
460 name, sshbuf_len(data));
462 if ((which & OPTIONS_EXTENSIONS) != 0) {
463 if (strcmp(name, "permit-X11-forwarding") == 0) {
464 *cert_no_x11_forwarding_flag = 0;
466 } else if (strcmp(name,
467 "permit-agent-forwarding") == 0) {
468 *cert_no_agent_forwarding_flag = 0;
470 } else if (strcmp(name,
471 "permit-port-forwarding") == 0) {
472 *cert_no_port_forwarding_flag = 0;
474 } else if (strcmp(name, "permit-pty") == 0) {
475 *cert_no_pty_flag = 0;
477 } else if (strcmp(name, "permit-user-rc") == 0) {
478 *cert_no_user_rc = 0;
482 if (!found && (which & OPTIONS_CRITICAL) != 0) {
483 if (strcmp(name, "force-command") == 0) {
484 if ((r = sshbuf_get_cstring(data, &command,
486 error("Unable to parse \"%s\" "
487 "section: %s", name, ssh_err(r));
490 if (*cert_forced_command != NULL) {
491 error("Certificate has multiple "
492 "force-command options");
496 *cert_forced_command = command;
499 if (strcmp(name, "source-address") == 0) {
500 if ((r = sshbuf_get_cstring(data, &allowed,
502 error("Unable to parse \"%s\" "
503 "section: %s", name, ssh_err(r));
506 if ((*cert_source_address_done)++) {
507 error("Certificate has multiple "
508 "source-address options");
512 remote_ip = get_remote_ipaddr();
513 result = addr_match_cidr_list(remote_ip,
522 logit("Authentication tried for %.100s "
523 "with valid certificate but not "
524 "from a permitted host "
525 "(ip=%.200s).", pw->pw_name,
527 auth_debug_add("Your address '%.200s' "
528 "is not permitted to use this "
529 "certificate for login.",
534 error("Certificate source-address "
544 error("Certificate critical option \"%s\" "
545 "is not supported", name);
548 logit("Certificate extension \"%s\" "
549 "is not supported", name);
551 } else if (sshbuf_len(data) != 0) {
552 error("Certificate option \"%s\" corrupt "
553 "(extra data)", name);
559 /* successfully parsed all options */
564 cert_forced_command != NULL &&
565 *cert_forced_command != NULL) {
566 free(*cert_forced_command);
567 *cert_forced_command = NULL;
577 * Set options from critical certificate options. These supersede user key
578 * options so this must be called after auth_parse_options().
581 auth_cert_options(struct sshkey *k, struct passwd *pw)
583 int cert_no_port_forwarding_flag = 1;
584 int cert_no_agent_forwarding_flag = 1;
585 int cert_no_x11_forwarding_flag = 1;
586 int cert_no_pty_flag = 1;
587 int cert_no_user_rc = 1;
588 char *cert_forced_command = NULL;
589 int cert_source_address_done = 0;
591 /* Separate options and extensions for v01 certs */
592 if (parse_option_list(k->cert->critical, pw,
593 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
594 &cert_forced_command,
595 &cert_source_address_done) == -1)
597 if (parse_option_list(k->cert->extensions, pw,
598 OPTIONS_EXTENSIONS, 0,
599 &cert_no_port_forwarding_flag,
600 &cert_no_agent_forwarding_flag,
601 &cert_no_x11_forwarding_flag,
607 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
608 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
609 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
610 no_pty_flag |= cert_no_pty_flag;
611 no_user_rc |= cert_no_user_rc;
612 /* CA-specified forced command supersedes key option */
613 if (cert_forced_command != NULL) {
614 if (forced_command != NULL)
615 free(forced_command);
616 forced_command = cert_forced_command;