1 /* $OpenBSD: auth-options.c,v 1.70 2015/12/10 17:08:40 mmcc 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
;
79 forced_command
= NULL
;
80 free(authorized_principals
);
81 authorized_principals
= NULL
;
82 forced_tun_device
= -1;
83 channel_clear_permitted_opens();
87 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
88 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
89 * if negated option matches.
90 * If the option or negated option matches, then *optsp is updated to
91 * point to the first character after the option and, if 'msg' is not NULL
92 * then a message based on it added via auth_debug_add().
95 match_flag(const char *opt
, int allow_negate
, char **optsp
, const char *msg
)
97 size_t opt_len
= strlen(opt
);
101 if (allow_negate
&& strncasecmp(opts
, "no-", 3) == 0) {
105 if (strncasecmp(opts
, opt
, opt_len
) == 0) {
106 *optsp
= opts
+ opt_len
;
108 auth_debug_add("%s %s.", msg
,
109 negate
? "disabled" : "enabled");
111 return negate
? 0 : 1;
117 * return 1 if access is granted, 0 if not.
118 * side effect: sets key option flags
121 auth_parse_options(struct passwd
*pw
, char *opts
, char *file
, u_long linenum
)
127 auth_clear_options();
132 while (*opts
&& *opts
!= ' ' && *opts
!= '\t') {
133 if ((r
= match_flag("cert-authority", 0, &opts
, NULL
)) != -1) {
134 key_is_cert_authority
= r
;
137 if ((r
= match_flag("restrict", 0, &opts
, NULL
)) != -1) {
138 auth_debug_add("Key is restricted.");
139 no_port_forwarding_flag
= 1;
140 no_agent_forwarding_flag
= 1;
141 no_x11_forwarding_flag
= 1;
146 if ((r
= match_flag("port-forwarding", 1, &opts
,
147 "Port forwarding")) != -1) {
148 no_port_forwarding_flag
= r
!= 1;
151 if ((r
= match_flag("agent-forwarding", 1, &opts
,
152 "Agent forwarding")) != -1) {
153 no_agent_forwarding_flag
= r
!= 1;
156 if ((r
= match_flag("x11-forwarding", 1, &opts
,
157 "X11 forwarding")) != -1) {
158 no_x11_forwarding_flag
= r
!= 1;
161 if ((r
= match_flag("pty", 1, &opts
,
162 "PTY allocation")) != -1) {
163 no_pty_flag
= r
!= 1;
166 if ((r
= match_flag("user-rc", 1, &opts
,
167 "User rc execution")) != -1) {
172 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
174 free(forced_command
);
175 forced_command
= xmalloc(strlen(opts
) + 1);
180 if (*opts
== '\\' && opts
[1] == '"') {
182 forced_command
[i
++] = '"';
185 forced_command
[i
++] = *opts
++;
188 debug("%.100s, line %lu: missing end quote",
190 auth_debug_add("%.100s, line %lu: missing end quote",
192 free(forced_command
);
193 forced_command
= NULL
;
196 forced_command
[i
] = '\0';
197 auth_debug_add("Forced command.");
201 cp
= "principals=\"";
202 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
204 free(authorized_principals
);
205 authorized_principals
= xmalloc(strlen(opts
) + 1);
210 if (*opts
== '\\' && opts
[1] == '"') {
212 authorized_principals
[i
++] = '"';
215 authorized_principals
[i
++] = *opts
++;
218 debug("%.100s, line %lu: missing end quote",
220 auth_debug_add("%.100s, line %lu: missing end quote",
222 free(authorized_principals
);
223 authorized_principals
= NULL
;
226 authorized_principals
[i
] = '\0';
227 auth_debug_add("principals: %.900s",
228 authorized_principals
);
232 cp
= "environment=\"";
233 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
235 struct envstring
*new_envstring
;
238 s
= xmalloc(strlen(opts
) + 1);
243 if (*opts
== '\\' && opts
[1] == '"') {
251 debug("%.100s, line %lu: missing end quote",
253 auth_debug_add("%.100s, line %lu: missing end quote",
260 if (options
.permit_user_env
) {
261 auth_debug_add("Adding to environment: "
263 debug("Adding to environment: %.900s", s
);
264 new_envstring
= xcalloc(1,
265 sizeof(*new_envstring
));
266 new_envstring
->s
= s
;
267 new_envstring
->next
= custom_environment
;
268 custom_environment
= new_envstring
;
275 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
276 const char *remote_ip
= get_remote_ipaddr();
277 const char *remote_host
= get_canonical_hostname(
279 char *patterns
= xmalloc(strlen(opts
) + 1);
286 if (*opts
== '\\' && opts
[1] == '"') {
291 patterns
[i
++] = *opts
++;
294 debug("%.100s, line %lu: missing end quote",
296 auth_debug_add("%.100s, line %lu: missing end quote",
303 switch (match_host_and_ip(remote_host
, remote_ip
,
307 /* Host name matches. */
310 debug("%.100s, line %lu: invalid criteria",
312 auth_debug_add("%.100s, line %lu: "
313 "invalid criteria", file
, linenum
);
317 logit("Authentication tried for %.100s with "
318 "correct key but not from a permitted "
319 "host (host=%.200s, ip=%.200s).",
320 pw
->pw_name
, remote_host
, remote_ip
);
321 auth_debug_add("Your host '%.200s' is not "
322 "permitted to use this key for login.",
329 cp
= "permitopen=\"";
330 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
333 char *patterns
= xmalloc(strlen(opts
) + 1);
340 if (*opts
== '\\' && opts
[1] == '"') {
345 patterns
[i
++] = *opts
++;
348 debug("%.100s, line %lu: missing end quote",
350 auth_debug_add("%.100s, line %lu: missing "
351 "end quote", file
, linenum
);
358 /* XXX - add streamlocal support */
360 if (host
== NULL
|| strlen(host
) >= NI_MAXHOST
) {
361 debug("%.100s, line %lu: Bad permitopen "
362 "specification <%.100s>", file
, linenum
,
364 auth_debug_add("%.100s, line %lu: "
365 "Bad permitopen specification", file
,
370 host
= cleanhostname(host
);
371 if (p
== NULL
|| (port
= permitopen_port(p
)) < 0) {
372 debug("%.100s, line %lu: Bad permitopen port "
373 "<%.100s>", file
, linenum
, p
? p
: "");
374 auth_debug_add("%.100s, line %lu: "
375 "Bad permitopen port", file
, linenum
);
379 if ((options
.allow_tcp_forwarding
& FORWARD_LOCAL
) != 0)
380 channel_add_permitted_opens(host
, port
);
385 if (strncasecmp(opts
, cp
, strlen(cp
)) == 0) {
388 tun
= xmalloc(strlen(opts
) + 1);
396 debug("%.100s, line %lu: missing end quote",
398 auth_debug_add("%.100s, line %lu: missing end quote",
401 forced_tun_device
= -1;
405 forced_tun_device
= a2tun(tun
, NULL
);
407 if (forced_tun_device
== SSH_TUNID_ERR
) {
408 debug("%.100s, line %lu: invalid tun device",
410 auth_debug_add("%.100s, line %lu: invalid tun device",
412 forced_tun_device
= -1;
415 auth_debug_add("Forced tun device: %d", forced_tun_device
);
421 * Skip the comma, and move to the next option
422 * (or break out if there are no more).
425 fatal("Bugs in auth-options.c option processing.");
426 if (*opts
== ' ' || *opts
== '\t')
427 break; /* End of options. */
431 /* Process the next option. */
438 logit("Bad options in %.100s file, line %lu: %.50s",
439 file
, linenum
, opts
);
440 auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
441 file
, linenum
, opts
);
447 #define OPTIONS_CRITICAL 1
448 #define OPTIONS_EXTENSIONS 2
450 parse_option_list(struct sshbuf
*oblob
, struct passwd
*pw
,
451 u_int which
, int crit
,
452 int *cert_no_port_forwarding_flag
,
453 int *cert_no_agent_forwarding_flag
,
454 int *cert_no_x11_forwarding_flag
,
455 int *cert_no_pty_flag
,
456 int *cert_no_user_rc
,
457 char **cert_forced_command
,
458 int *cert_source_address_done
)
460 char *command
, *allowed
;
461 const char *remote_ip
;
463 struct sshbuf
*c
= NULL
, *data
= NULL
;
464 int r
, ret
= -1, result
, found
;
466 if ((c
= sshbuf_fromb(oblob
)) == NULL
) {
467 error("%s: sshbuf_fromb failed", __func__
);
471 while (sshbuf_len(c
) > 0) {
474 if ((r
= sshbuf_get_cstring(c
, &name
, NULL
)) != 0 ||
475 (r
= sshbuf_froms(c
, &data
)) != 0) {
476 error("Unable to parse certificate options: %s",
480 debug3("found certificate option \"%.100s\" len %zu",
481 name
, sshbuf_len(data
));
483 if ((which
& OPTIONS_EXTENSIONS
) != 0) {
484 if (strcmp(name
, "permit-X11-forwarding") == 0) {
485 *cert_no_x11_forwarding_flag
= 0;
487 } else if (strcmp(name
,
488 "permit-agent-forwarding") == 0) {
489 *cert_no_agent_forwarding_flag
= 0;
491 } else if (strcmp(name
,
492 "permit-port-forwarding") == 0) {
493 *cert_no_port_forwarding_flag
= 0;
495 } else if (strcmp(name
, "permit-pty") == 0) {
496 *cert_no_pty_flag
= 0;
498 } else if (strcmp(name
, "permit-user-rc") == 0) {
499 *cert_no_user_rc
= 0;
503 if (!found
&& (which
& OPTIONS_CRITICAL
) != 0) {
504 if (strcmp(name
, "force-command") == 0) {
505 if ((r
= sshbuf_get_cstring(data
, &command
,
507 error("Unable to parse \"%s\" "
508 "section: %s", name
, ssh_err(r
));
511 if (*cert_forced_command
!= NULL
) {
512 error("Certificate has multiple "
513 "force-command options");
517 *cert_forced_command
= command
;
520 if (strcmp(name
, "source-address") == 0) {
521 if ((r
= sshbuf_get_cstring(data
, &allowed
,
523 error("Unable to parse \"%s\" "
524 "section: %s", name
, ssh_err(r
));
527 if ((*cert_source_address_done
)++) {
528 error("Certificate has multiple "
529 "source-address options");
533 remote_ip
= get_remote_ipaddr();
534 result
= addr_match_cidr_list(remote_ip
,
543 logit("Authentication tried for %.100s "
544 "with valid certificate but not "
545 "from a permitted host "
546 "(ip=%.200s).", pw
->pw_name
,
548 auth_debug_add("Your address '%.200s' "
549 "is not permitted to use this "
550 "certificate for login.",
555 error("Certificate source-address "
565 error("Certificate critical option \"%s\" "
566 "is not supported", name
);
569 logit("Certificate extension \"%s\" "
570 "is not supported", name
);
572 } else if (sshbuf_len(data
) != 0) {
573 error("Certificate option \"%s\" corrupt "
574 "(extra data)", name
);
580 /* successfully parsed all options */
585 cert_forced_command
!= NULL
&&
586 *cert_forced_command
!= NULL
) {
587 free(*cert_forced_command
);
588 *cert_forced_command
= NULL
;
597 * Set options from critical certificate options. These supersede user key
598 * options so this must be called after auth_parse_options().
601 auth_cert_options(struct sshkey
*k
, struct passwd
*pw
)
603 int cert_no_port_forwarding_flag
= 1;
604 int cert_no_agent_forwarding_flag
= 1;
605 int cert_no_x11_forwarding_flag
= 1;
606 int cert_no_pty_flag
= 1;
607 int cert_no_user_rc
= 1;
608 char *cert_forced_command
= NULL
;
609 int cert_source_address_done
= 0;
611 /* Separate options and extensions for v01 certs */
612 if (parse_option_list(k
->cert
->critical
, pw
,
613 OPTIONS_CRITICAL
, 1, NULL
, NULL
, NULL
, NULL
, NULL
,
614 &cert_forced_command
,
615 &cert_source_address_done
) == -1)
617 if (parse_option_list(k
->cert
->extensions
, pw
,
618 OPTIONS_EXTENSIONS
, 0,
619 &cert_no_port_forwarding_flag
,
620 &cert_no_agent_forwarding_flag
,
621 &cert_no_x11_forwarding_flag
,
627 no_port_forwarding_flag
|= cert_no_port_forwarding_flag
;
628 no_agent_forwarding_flag
|= cert_no_agent_forwarding_flag
;
629 no_x11_forwarding_flag
|= cert_no_x11_forwarding_flag
;
630 no_pty_flag
|= cert_no_pty_flag
;
631 no_user_rc
|= cert_no_user_rc
;
632 /* CA-specified forced command supersedes key option */
633 if (cert_forced_command
!= NULL
) {
634 free(forced_command
);
635 forced_command
= cert_forced_command
;