1 /* $OpenBSD: auth-options.c,v 1.62 2013/12/19 00:27:57 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"
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);
329 if (host == NULL || strlen(host) >= NI_MAXHOST) {
330 debug("%.100s, line %lu: Bad permitopen "
331 "specification <%.100s>", file, linenum,
333 auth_debug_add("%.100s, line %lu: "
334 "Bad permitopen specification", file,
339 host = cleanhostname(host);
340 if (p == NULL || (port = permitopen_port(p)) < 0) {
341 debug("%.100s, line %lu: Bad permitopen port "
342 "<%.100s>", file, linenum, p ? p : "");
343 auth_debug_add("%.100s, line %lu: "
344 "Bad permitopen port", file, linenum);
348 if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
349 channel_add_permitted_opens(host, port);
354 if (strncasecmp(opts, cp, strlen(cp)) == 0) {
357 tun = xmalloc(strlen(opts) + 1);
365 debug("%.100s, line %lu: missing end quote",
367 auth_debug_add("%.100s, line %lu: missing end quote",
370 forced_tun_device = -1;
374 forced_tun_device = a2tun(tun, NULL);
376 if (forced_tun_device == SSH_TUNID_ERR) {
377 debug("%.100s, line %lu: invalid tun device",
379 auth_debug_add("%.100s, line %lu: invalid tun device",
381 forced_tun_device = -1;
384 auth_debug_add("Forced tun device: %d", forced_tun_device);
390 * Skip the comma, and move to the next option
391 * (or break out if there are no more).
394 fatal("Bugs in auth-options.c option processing.");
395 if (*opts == ' ' || *opts == '\t')
396 break; /* End of options. */
400 /* Process the next option. */
407 logit("Bad options in %.100s file, line %lu: %.50s",
408 file, linenum, opts);
409 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
410 file, linenum, opts);
416 #define OPTIONS_CRITICAL 1
417 #define OPTIONS_EXTENSIONS 2
419 parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
420 u_int which, int crit,
421 int *cert_no_port_forwarding_flag,
422 int *cert_no_agent_forwarding_flag,
423 int *cert_no_x11_forwarding_flag,
424 int *cert_no_pty_flag,
425 int *cert_no_user_rc,
426 char **cert_forced_command,
427 int *cert_source_address_done)
429 char *command, *allowed;
430 const char *remote_ip;
432 u_char *data_blob = NULL;
433 u_int nlen, dlen, clen;
435 int ret = -1, result, found;
439 /* Make copy to avoid altering original */
441 buffer_append(&c, optblob, optblob_len);
443 while (buffer_len(&c) > 0) {
444 if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
445 (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
446 error("Certificate options corrupt");
449 buffer_append(&data, data_blob, dlen);
450 debug3("found certificate option \"%.100s\" len %u",
453 if ((which & OPTIONS_EXTENSIONS) != 0) {
454 if (strcmp(name, "permit-X11-forwarding") == 0) {
455 *cert_no_x11_forwarding_flag = 0;
457 } else if (strcmp(name,
458 "permit-agent-forwarding") == 0) {
459 *cert_no_agent_forwarding_flag = 0;
461 } else if (strcmp(name,
462 "permit-port-forwarding") == 0) {
463 *cert_no_port_forwarding_flag = 0;
465 } else if (strcmp(name, "permit-pty") == 0) {
466 *cert_no_pty_flag = 0;
468 } else if (strcmp(name, "permit-user-rc") == 0) {
469 *cert_no_user_rc = 0;
473 if (!found && (which & OPTIONS_CRITICAL) != 0) {
474 if (strcmp(name, "force-command") == 0) {
475 if ((command = buffer_get_cstring_ret(&data,
477 error("Certificate constraint \"%s\" "
481 if (*cert_forced_command != NULL) {
482 error("Certificate has multiple "
483 "force-command options");
487 *cert_forced_command = command;
490 if (strcmp(name, "source-address") == 0) {
491 if ((allowed = buffer_get_cstring_ret(&data,
493 error("Certificate constraint "
494 "\"%s\" corrupt", name);
497 if ((*cert_source_address_done)++) {
498 error("Certificate has multiple "
499 "source-address options");
503 remote_ip = get_remote_ipaddr();
504 result = addr_match_cidr_list(remote_ip,
513 logit("Authentication tried for %.100s "
514 "with valid certificate but not "
515 "from a permitted host "
516 "(ip=%.200s).", pw->pw_name,
518 auth_debug_add("Your address '%.200s' "
519 "is not permitted to use this "
520 "certificate for login.",
525 error("Certificate source-address "
535 error("Certificate critical option \"%s\" "
536 "is not supported", name);
539 logit("Certificate extension \"%s\" "
540 "is not supported", name);
542 } else if (buffer_len(&data) != 0) {
543 error("Certificate option \"%s\" corrupt "
544 "(extra data)", name);
553 /* successfully parsed all options */
558 cert_forced_command != NULL &&
559 *cert_forced_command != NULL) {
560 free(*cert_forced_command);
561 *cert_forced_command = NULL;
565 if (data_blob != NULL)
573 * Set options from critical certificate options. These supersede user key
574 * options so this must be called after auth_parse_options().
577 auth_cert_options(Key *k, struct passwd *pw)
579 int cert_no_port_forwarding_flag = 1;
580 int cert_no_agent_forwarding_flag = 1;
581 int cert_no_x11_forwarding_flag = 1;
582 int cert_no_pty_flag = 1;
583 int cert_no_user_rc = 1;
584 char *cert_forced_command = NULL;
585 int cert_source_address_done = 0;
587 if (key_cert_is_legacy(k)) {
588 /* All options are in the one field for v00 certs */
589 if (parse_option_list(buffer_ptr(&k->cert->critical),
590 buffer_len(&k->cert->critical), pw,
591 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
592 &cert_no_port_forwarding_flag,
593 &cert_no_agent_forwarding_flag,
594 &cert_no_x11_forwarding_flag,
597 &cert_forced_command,
598 &cert_source_address_done) == -1)
601 /* Separate options and extensions for v01 certs */
602 if (parse_option_list(buffer_ptr(&k->cert->critical),
603 buffer_len(&k->cert->critical), pw,
604 OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
605 &cert_forced_command,
606 &cert_source_address_done) == -1)
608 if (parse_option_list(buffer_ptr(&k->cert->extensions),
609 buffer_len(&k->cert->extensions), pw,
610 OPTIONS_EXTENSIONS, 1,
611 &cert_no_port_forwarding_flag,
612 &cert_no_agent_forwarding_flag,
613 &cert_no_x11_forwarding_flag,
620 no_port_forwarding_flag |= cert_no_port_forwarding_flag;
621 no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
622 no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
623 no_pty_flag |= cert_no_pty_flag;
624 no_user_rc |= cert_no_user_rc;
625 /* CA-specified forced command supersedes key option */
626 if (cert_forced_command != NULL) {
627 if (forced_command != NULL)
628 free(forced_command);
629 forced_command = cert_forced_command;