1 /* $OpenBSD: auth-options.c,v 1.64 2014/07/15 15:54:14 millert 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"
37 /* Flags set authorized_keys flags */
38 int no_port_forwarding_flag = 0;
39 int no_agent_forwarding_flag = 0;
40 int no_x11_forwarding_flag = 0;
43 int key_is_cert_authority = 0;
45 /* "command=" option. */
46 char *forced_command = NULL;
48 /* "environment=" options. */
49 struct envstring *custom_environment = NULL;
51 /* "tunnel=" option. */
52 int forced_tun_device = -1;
54 /* "principals=" option. */
55 char *authorized_principals = NULL;
57 extern ServerOptions options;
60 auth_clear_options(void)
62 no_agent_forwarding_flag = 0;
63 no_port_forwarding_flag = 0;
65 no_x11_forwarding_flag = 0;
67 key_is_cert_authority = 0;
68 while (custom_environment) {
69 struct envstring *ce = custom_environment;
70 custom_environment = ce->next;
76 forced_command = NULL;
78 if (authorized_principals) {
79 free(authorized_principals);
80 authorized_principals = NULL;
82 forced_tun_device = -1;
83 channel_clear_permitted_opens();
87 * return 1 if access is granted, 0 if not.
88 * side effect: sets key option flags
91 auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
102 while (*opts && *opts != ' ' && *opts != '\t') {
103 cp = "cert-authority";
104 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
105 key_is_cert_authority = 1;
109 cp = "no-port-forwarding";
110 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
111 auth_debug_add("Port forwarding disabled.");
112 no_port_forwarding_flag = 1;
116 cp = "no-agent-forwarding";
117 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
118 auth_debug_add("Agent forwarding disabled.");
119 no_agent_forwarding_flag = 1;
123 cp = "no-X11-forwarding";
124 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
125 auth_debug_add("X11 forwarding disabled.");
126 no_x11_forwarding_flag = 1;
131 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
132 auth_debug_add("Pty allocation disabled.");
138 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
139 auth_debug_add("User rc file execution disabled.");
145 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
147 if (forced_command != NULL)
148 free(forced_command);
149 forced_command = xmalloc(strlen(opts) + 1);
154 if (*opts == '\\' && opts[1] == '"') {
156 forced_command[i++] = '"';
159 forced_command[i++] = *opts++;
162 debug("%.100s, line %lu: missing end quote",
164 auth_debug_add("%.100s, line %lu: missing end quote",
166 free(forced_command);
167 forced_command = NULL;
170 forced_command[i] = '\0';
171 auth_debug_add("Forced command.");
175 cp = "principals=\"";
176 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
178 if (authorized_principals != NULL)
179 free(authorized_principals);
180 authorized_principals = xmalloc(strlen(opts) + 1);
185 if (*opts == '\\' && opts[1] == '"') {
187 authorized_principals[i++] = '"';
190 authorized_principals[i++] = *opts++;
193 debug("%.100s, line %lu: missing end quote",
195 auth_debug_add("%.100s, line %lu: missing end quote",
197 free(authorized_principals);
198 authorized_principals = NULL;
201 authorized_principals[i] = '\0';
202 auth_debug_add("principals: %.900s",
203 authorized_principals);
207 cp = "environment=\"";
208 if (options.permit_user_env &&
209 strncasecmp(opts, cp, strlen(cp)) == 0) {
211 struct envstring *new_envstring;
214 s = xmalloc(strlen(opts) + 1);
219 if (*opts == '\\' && opts[1] == '"') {
227 debug("%.100s, line %lu: missing end quote",
229 auth_debug_add("%.100s, line %lu: missing end quote",
235 auth_debug_add("Adding to environment: %.900s", s);
236 debug("Adding to environment: %.900s", s);
238 new_envstring = xcalloc(1, sizeof(struct envstring));
239 new_envstring->s = s;
240 new_envstring->next = custom_environment;
241 custom_environment = new_envstring;
245 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
246 const char *remote_ip = get_remote_ipaddr();
247 const char *remote_host = get_canonical_hostname(
249 char *patterns = xmalloc(strlen(opts) + 1);
256 if (*opts == '\\' && opts[1] == '"') {
261 patterns[i++] = *opts++;
264 debug("%.100s, line %lu: missing end quote",
266 auth_debug_add("%.100s, line %lu: missing end quote",
273 switch (match_host_and_ip(remote_host, remote_ip,
277 /* Host name matches. */
280 debug("%.100s, line %lu: invalid criteria",
282 auth_debug_add("%.100s, line %lu: "
283 "invalid criteria", file, linenum);
287 logit("Authentication tried for %.100s with "
288 "correct key but not from a permitted "
289 "host (host=%.200s, ip=%.200s).",
290 pw->pw_name, remote_host, remote_ip);
291 auth_debug_add("Your host '%.200s' is not "
292 "permitted to use this key for login.",
299 cp = "permitopen=\"";
300 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
303 char *patterns = xmalloc(strlen(opts) + 1);
310 if (*opts == '\\' && opts[1] == '"') {
315 patterns[i++] = *opts++;
318 debug("%.100s, line %lu: missing end quote",
320 auth_debug_add("%.100s, line %lu: missing "
321 "end quote", file, linenum);
328 /* XXX - add streamlocal support */
330 if (host == NULL || strlen(host) >= NI_MAXHOST) {
331 debug("%.100s, line %lu: Bad permitopen "
332 "specification <%.100s>", file, linenum,
334 auth_debug_add("%.100s, line %lu: "
335 "Bad permitopen specification", file,
340 host = cleanhostname(host);
341 if (p == NULL || (port = permitopen_port(p)) < 0) {
342 debug("%.100s, line %lu: Bad permitopen port "
343 "<%.100s>", file, linenum, p ? p : "");
344 auth_debug_add("%.100s, line %lu: "
345 "Bad permitopen port", file, linenum);
349 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
350 channel_add_permitted_opens(host, port);
355 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
358 tun = xmalloc(strlen(opts) + 1);
366 debug("%.100s, line %lu: missing end quote",
368 auth_debug_add("%.100s, line %lu: missing end quote",
371 forced_tun_device = -1;
375 forced_tun_device = a2tun(tun, NULL);
377 if (forced_tun_device == SSH_TUNID_ERR) {
378 debug("%.100s, line %lu: invalid tun device",
380 auth_debug_add("%.100s, line %lu: invalid tun device",
382 forced_tun_device = -1;
385 auth_debug_add("Forced tun device: %d", forced_tun_device);
391 * Skip the comma, and move to the next option
392 * (or break out if there are no more).
395 fatal("Bugs in auth-options.c option processing.");
396 if (*opts == ' ' || *opts == '\t')
397 break; /* End of options. */
401 /* Process the next option. */
408 logit("Bad options in %.100s file, line %lu: %.50s",
409 file, linenum, opts);
410 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
411 file, linenum, opts);
417 #define OPTIONS_CRITICAL 1
418 #define OPTIONS_EXTENSIONS 2
420 parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
421 u_int which, int crit,
422 int *cert_no_port_forwarding_flag,
423 int *cert_no_agent_forwarding_flag,
424 int *cert_no_x11_forwarding_flag,
425 int *cert_no_pty_flag,
426 int *cert_no_user_rc,
427 char **cert_forced_command,
428 int *cert_source_address_done)
430 char *command, *allowed;
431 const char *remote_ip;
433 u_char *data_blob = NULL;
434 u_int nlen, dlen, clen;
436 int ret = -1, result, found;
440 /* Make copy to avoid altering original */
442 buffer_append(&c, optblob, optblob_len);
444 while (buffer_len(&c) > 0) {
445 if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
446 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
447 error("Certificate options corrupt");
450 buffer_append(&data, data_blob, dlen);
451 debug3("found certificate option \"%.100s\" len %u",
454 if ((which & OPTIONS_EXTENSIONS) != 0) {
455 if (strcmp(name, "permit-X11-forwarding") == 0) {
456 *cert_no_x11_forwarding_flag = 0;
458 } else if (strcmp(name,
459 "permit-agent-forwarding") == 0) {
460 *cert_no_agent_forwarding_flag = 0;
462 } else if (strcmp(name,
463 "permit-port-forwarding") == 0) {
464 *cert_no_port_forwarding_flag = 0;
466 } else if (strcmp(name, "permit-pty") == 0) {
467 *cert_no_pty_flag = 0;
469 } else if (strcmp(name, "permit-user-rc") == 0) {
470 *cert_no_user_rc = 0;
474 if (!found && (which & OPTIONS_CRITICAL) != 0) {
475 if (strcmp(name, "force-command") == 0) {
476 if ((command = buffer_get_cstring_ret(&data,
478 error("Certificate constraint \"%s\" "
482 if (*cert_forced_command != NULL) {
483 error("Certificate has multiple "
484 "force-command options");
488 *cert_forced_command = command;
491 if (strcmp(name, "source-address") == 0) {
492 if ((allowed = buffer_get_cstring_ret(&data,
494 error("Certificate constraint "
495 "\"%s\" corrupt", name);
498 if ((*cert_source_address_done)++) {
499 error("Certificate has multiple "
500 "source-address options");
504 remote_ip = get_remote_ipaddr();
505 result = addr_match_cidr_list(remote_ip,
514 logit("Authentication tried for %.100s "
515 "with valid certificate but not "
516 "from a permitted host "
517 "(ip=%.200s).", pw->pw_name,
519 auth_debug_add("Your address '%.200s' "
520 "is not permitted to use this "
521 "certificate for login.",
526 error("Certificate source-address "
536 error("Certificate critical option \"%s\" "
537 "is not supported", name);
540 logit("Certificate extension \"%s\" "
541 "is not supported", name);
543 } else if (buffer_len(&data) != 0) {
544 error("Certificate option \"%s\" corrupt "
545 "(extra data)", name);
554 /* successfully parsed all options */
559 cert_forced_command != NULL &&
560 *cert_forced_command != NULL) {
561 free(*cert_forced_command);
562 *cert_forced_command = NULL;
566 if (data_blob != NULL)
574 * Set options from critical certificate options. These supersede user key
575 * options so this must be called after auth_parse_options().
578 auth_cert_options(Key *k, struct passwd *pw)
580 int cert_no_port_forwarding_flag = 1;
581 int cert_no_agent_forwarding_flag = 1;
582 int cert_no_x11_forwarding_flag = 1;
583 int cert_no_pty_flag = 1;
584 int cert_no_user_rc = 1;
585 char *cert_forced_command = NULL;
586 int cert_source_address_done = 0;
588 if (key_cert_is_legacy(k)) {
589 /* All options are in the one field for v00 certs */
590 if (parse_option_list(buffer_ptr(k->cert->critical),
591 buffer_len(k->cert->critical), pw,
592 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
593 &cert_no_port_forwarding_flag,
594 &cert_no_agent_forwarding_flag,
595 &cert_no_x11_forwarding_flag,
598 &cert_forced_command,
599 &cert_source_address_done) == -1)
602 /* Separate options and extensions for v01 certs */
603 if (parse_option_list(buffer_ptr(k->cert->critical),
604 buffer_len(k->cert->critical), pw,
605 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
606 &cert_forced_command,
607 &cert_source_address_done) == -1)
609 if (parse_option_list(buffer_ptr(k->cert->extensions),
610 buffer_len(k->cert->extensions), pw,
611 OPTIONS_EXTENSIONS, 1,
612 &cert_no_port_forwarding_flag,
613 &cert_no_agent_forwarding_flag,
614 &cert_no_x11_forwarding_flag,
621 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
622 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
623 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
624 no_pty_flag |= cert_no_pty_flag;
625 no_user_rc |= cert_no_user_rc;
626 /* CA-specified forced command supersedes key option */
627 if (cert_forced_command != NULL) {
628 if (forced_command != NULL)
629 free(forced_command);
630 forced_command = cert_forced_command;