1 /* $OpenBSD: auth-options.c,v 1.84 2018/10/03 06:38:35 djm Exp $ */
3 * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
30 #include "openbsd-compat/sys-queue.h"
40 #include "auth-options.h"
43 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
44 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
45 * if negated option matches.
46 * If the option or negated option matches, then *optsp is updated to
47 * point to the first character after the option.
50 opt_flag(const char *opt, int allow_negate, const char **optsp)
52 size_t opt_len = strlen(opt);
53 const char *opts = *optsp;
56 if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
60 if (strncasecmp(opts, opt, opt_len) == 0) {
61 *optsp = opts + opt_len;
62 return negate ? 0 : 1;
68 opt_dequote(const char **sp, const char **errstrp)
76 *errstrp = "missing start quote";
80 if ((ret = malloc(strlen((s)) + 1)) == NULL) {
81 *errstrp = "memory allocation failed";
84 for (i = 0; *s != '\0' && *s != '"';) {
85 if (s[0] == '\\' && s[1] == '"')
90 *errstrp = "missing end quote";
101 opt_match(const char **opts, const char *term)
103 if (strncasecmp((*opts), term, strlen(term)) == 0 &&
104 (*opts)[strlen(term)] == '=') {
105 *opts += strlen(term) + 1;
112 dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
122 if ((dst = calloc(nsrc, sizeof(*src))) == NULL)
124 for (i = 0; i < nsrc; i++) {
125 if ((dst[i] = strdup(src[i])) == NULL) {
126 for (j = 0; j < i; j++)
138 #define OPTIONS_CRITICAL 1
139 #define OPTIONS_EXTENSIONS 2
141 cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
142 u_int which, int crit)
144 char *command, *allowed;
146 struct sshbuf *c = NULL, *data = NULL;
147 int r, ret = -1, found;
149 if ((c = sshbuf_fromb(oblob)) == NULL) {
150 error("%s: sshbuf_fromb failed", __func__);
154 while (sshbuf_len(c) > 0) {
157 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
158 (r = sshbuf_froms(c, &data)) != 0) {
159 error("Unable to parse certificate options: %s",
163 debug3("found certificate option \"%.100s\" len %zu",
164 name, sshbuf_len(data));
166 if ((which & OPTIONS_EXTENSIONS) != 0) {
167 if (strcmp(name, "permit-X11-forwarding") == 0) {
168 opts->permit_x11_forwarding_flag = 1;
170 } else if (strcmp(name,
171 "permit-agent-forwarding") == 0) {
172 opts->permit_agent_forwarding_flag = 1;
174 } else if (strcmp(name,
175 "permit-port-forwarding") == 0) {
176 opts->permit_port_forwarding_flag = 1;
178 } else if (strcmp(name, "permit-pty") == 0) {
179 opts->permit_pty_flag = 1;
181 } else if (strcmp(name, "permit-user-rc") == 0) {
182 opts->permit_user_rc = 1;
186 if (!found && (which & OPTIONS_CRITICAL) != 0) {
187 if (strcmp(name, "force-command") == 0) {
188 if ((r = sshbuf_get_cstring(data, &command,
190 error("Unable to parse \"%s\" "
191 "section: %s", name, ssh_err(r));
194 if (opts->force_command != NULL) {
195 error("Certificate has multiple "
196 "force-command options");
200 opts->force_command = command;
203 if (strcmp(name, "source-address") == 0) {
204 if ((r = sshbuf_get_cstring(data, &allowed,
206 error("Unable to parse \"%s\" "
207 "section: %s", name, ssh_err(r));
210 if (opts->required_from_host_cert != NULL) {
211 error("Certificate has multiple "
212 "source-address options");
217 if (addr_match_cidr_list(NULL, allowed) == -1) {
218 error("Certificate source-address "
222 opts->required_from_host_cert = allowed;
229 error("Certificate critical option \"%s\" "
230 "is not supported", name);
233 logit("Certificate extension \"%s\" "
234 "is not supported", name);
236 } else if (sshbuf_len(data) != 0) {
237 error("Certificate option \"%s\" corrupt "
238 "(extra data)", name);
244 /* successfully parsed all options */
257 struct sshauthopt *ret;
259 if ((ret = calloc(1, sizeof(*ret))) == NULL)
261 ret->force_tun_device = -1;
266 sshauthopt_free(struct sshauthopt *opts)
273 free(opts->cert_principals);
274 free(opts->force_command);
275 free(opts->required_from_host_cert);
276 free(opts->required_from_host_keys);
278 for (i = 0; i < opts->nenv; i++)
282 for (i = 0; i < opts->npermitopen; i++)
283 free(opts->permitopen[i]);
284 free(opts->permitopen);
286 for (i = 0; i < opts->npermitlisten; i++)
287 free(opts->permitlisten[i]);
288 free(opts->permitlisten);
290 explicit_bzero(opts, sizeof(*opts));
295 sshauthopt_new_with_keys_defaults(void)
297 struct sshauthopt *ret = NULL;
299 if ((ret = sshauthopt_new()) == NULL)
302 /* Defaults for authorized_keys flags */
303 ret->permit_port_forwarding_flag = 1;
304 ret->permit_agent_forwarding_flag = 1;
305 ret->permit_x11_forwarding_flag = 1;
306 ret->permit_pty_flag = 1;
307 ret->permit_user_rc = 1;
312 * Parse and record a permitopen/permitlisten directive.
313 * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
316 handle_permit(const char **optsp, int allow_bare_port,
317 char ***permitsp, size_t *npermitsp, const char **errstrp)
319 char *opt, *tmp, *cp, *host, **permits = *permitsp;
320 size_t npermits = *npermitsp;
321 const char *errstr = "unknown error";
323 if (npermits > INT_MAX) {
324 *errstrp = "too many permission directives";
327 if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
330 if (allow_bare_port && strchr(opt, ':') == NULL) {
332 * Allow a bare port number in permitlisten to indicate a
333 * listen_host wildcard.
335 if (asprintf(&tmp, "*:%s", opt) < 0) {
336 *errstrp = "memory allocation failed";
342 if ((tmp = strdup(opt)) == NULL) {
344 *errstrp = "memory allocation failed";
348 /* validate syntax before recording it. */
350 if (host == NULL || strlen(host) >= NI_MAXHOST) {
353 *errstrp = "invalid permission hostname";
357 * don't want to use permitopen_port to avoid
358 * dependency on channels.[ch] here.
361 (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
364 *errstrp = "invalid permission port";
367 /* XXX - add streamlocal support */
370 if ((permits = recallocarray(permits, npermits, npermits + 1,
371 sizeof(*permits))) == NULL) {
373 /* NB. don't update *permitsp if alloc fails */
374 *errstrp = "memory allocation failed";
377 permits[npermits++] = opt;
379 *npermitsp = npermits;
384 sshauthopt_parse(const char *opts, const char **errstrp)
386 char **oarray, *opt, *cp, *tmp;
388 struct sshauthopt *ret = NULL;
389 const char *errstr = "unknown error";
390 uint64_t valid_before;
394 if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
400 while (*opts && *opts != ' ' && *opts != '\t') {
402 if ((r = opt_flag("restrict", 0, &opts)) != -1) {
404 ret->permit_port_forwarding_flag = 0;
405 ret->permit_agent_forwarding_flag = 0;
406 ret->permit_x11_forwarding_flag = 0;
407 ret->permit_pty_flag = 0;
408 ret->permit_user_rc = 0;
409 } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
410 ret->cert_authority = r;
411 } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
412 ret->permit_port_forwarding_flag = r == 1;
413 } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
414 ret->permit_agent_forwarding_flag = r == 1;
415 } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
416 ret->permit_x11_forwarding_flag = r == 1;
417 } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
418 ret->permit_pty_flag = r == 1;
419 } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
420 ret->permit_user_rc = r == 1;
421 } else if (opt_match(&opts, "command")) {
422 if (ret->force_command != NULL) {
423 errstr = "multiple \"command\" clauses";
426 ret->force_command = opt_dequote(&opts, &errstr);
427 if (ret->force_command == NULL)
429 } else if (opt_match(&opts, "principals")) {
430 if (ret->cert_principals != NULL) {
431 errstr = "multiple \"principals\" clauses";
434 ret->cert_principals = opt_dequote(&opts, &errstr);
435 if (ret->cert_principals == NULL)
437 } else if (opt_match(&opts, "from")) {
438 if (ret->required_from_host_keys != NULL) {
439 errstr = "multiple \"from\" clauses";
442 ret->required_from_host_keys = opt_dequote(&opts,
444 if (ret->required_from_host_keys == NULL)
446 } else if (opt_match(&opts, "expiry-time")) {
447 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
449 if (parse_absolute_time(opt, &valid_before) != 0 ||
452 errstr = "invalid expires time";
456 if (ret->valid_before == 0 ||
457 valid_before < ret->valid_before)
458 ret->valid_before = valid_before;
459 } else if (opt_match(&opts, "environment")) {
460 if (ret->nenv > INT_MAX) {
461 errstr = "too many environment strings";
464 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
466 /* env name must be alphanumeric and followed by '=' */
467 if ((tmp = strchr(opt, '=')) == NULL) {
469 errstr = "invalid environment string";
472 if ((cp = strdup(opt)) == NULL)
474 cp[tmp - opt] = '\0'; /* truncate at '=' */
475 if (!valid_env_name(cp)) {
478 errstr = "invalid environment string";
484 if ((ret->env = recallocarray(ret->env, ret->nenv,
485 ret->nenv + 1, sizeof(*ret->env))) == NULL) {
487 ret->env = oarray; /* put it back for cleanup */
490 ret->env[ret->nenv++] = opt;
491 } else if (opt_match(&opts, "permitopen")) {
492 if (handle_permit(&opts, 0, &ret->permitopen,
493 &ret->npermitopen, &errstr) != 0)
495 } else if (opt_match(&opts, "permitlisten")) {
496 if (handle_permit(&opts, 1, &ret->permitlisten,
497 &ret->npermitlisten, &errstr) != 0)
499 } else if (opt_match(&opts, "tunnel")) {
500 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
502 ret->force_tun_device = a2tun(opt, NULL);
504 if (ret->force_tun_device == SSH_TUNID_ERR) {
505 errstr = "invalid tun device";
510 * Skip the comma, and move to the next option
511 * (or break out if there are no more).
513 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
514 break; /* End of options. */
515 /* Anything other than a comma is an unknown option */
517 errstr = "unknown key option";
522 errstr = "unexpected end-of-options";
533 errstr = "memory allocation failed";
535 sshauthopt_free(ret);
542 sshauthopt_from_cert(struct sshkey *k)
544 struct sshauthopt *ret;
546 if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
547 k->cert->type != SSH2_CERT_TYPE_USER)
550 if ((ret = sshauthopt_new()) == NULL)
553 /* Handle options and critical extensions separately */
554 if (cert_option_list(ret, k->cert->critical,
555 OPTIONS_CRITICAL, 1) == -1) {
556 sshauthopt_free(ret);
559 if (cert_option_list(ret, k->cert->extensions,
560 OPTIONS_EXTENSIONS, 0) == -1) {
561 sshauthopt_free(ret);
569 * Merges "additional" options to "primary" and returns the result.
570 * NB. Some options from primary have primacy.
573 sshauthopt_merge(const struct sshauthopt *primary,
574 const struct sshauthopt *additional, const char **errstrp)
576 struct sshauthopt *ret;
577 const char *errstr = "internal error";
583 if ((ret = sshauthopt_new()) == NULL)
586 /* cert_authority and cert_principals are cleared in result */
588 /* Prefer access lists from primary. */
589 /* XXX err is both set and mismatch? */
590 tmp = primary->required_from_host_cert;
592 tmp = additional->required_from_host_cert;
593 if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
595 tmp = primary->required_from_host_keys;
597 tmp = additional->required_from_host_keys;
598 if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
602 * force_tun_device, permitopen/permitlisten and environment all
603 * prefer the primary.
605 ret->force_tun_device = primary->force_tun_device;
606 if (ret->force_tun_device == -1)
607 ret->force_tun_device = additional->force_tun_device;
608 if (primary->nenv > 0) {
609 if (dup_strings(&ret->env, &ret->nenv,
610 primary->env, primary->nenv) != 0)
612 } else if (additional->nenv) {
613 if (dup_strings(&ret->env, &ret->nenv,
614 additional->env, additional->nenv) != 0)
617 if (primary->npermitopen > 0) {
618 if (dup_strings(&ret->permitopen, &ret->npermitopen,
619 primary->permitopen, primary->npermitopen) != 0)
621 } else if (additional->npermitopen > 0) {
622 if (dup_strings(&ret->permitopen, &ret->npermitopen,
623 additional->permitopen, additional->npermitopen) != 0)
627 if (primary->npermitlisten > 0) {
628 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
629 primary->permitlisten, primary->npermitlisten) != 0)
631 } else if (additional->npermitlisten > 0) {
632 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
633 additional->permitlisten, additional->npermitlisten) != 0)
637 /* Flags are logical-AND (i.e. must be set in both for permission) */
638 #define OPTFLAG(x) ret->x = (primary->x == 1) && (additional->x == 1)
639 OPTFLAG(permit_port_forwarding_flag);
640 OPTFLAG(permit_agent_forwarding_flag);
641 OPTFLAG(permit_x11_forwarding_flag);
642 OPTFLAG(permit_pty_flag);
643 OPTFLAG(permit_user_rc);
646 /* Earliest expiry time should win */
647 if (primary->valid_before != 0)
648 ret->valid_before = primary->valid_before;
649 if (additional->valid_before != 0 &&
650 additional->valid_before < ret->valid_before)
651 ret->valid_before = additional->valid_before;
654 * When both multiple forced-command are specified, only
655 * proceed if they are identical, otherwise fail.
657 if (primary->force_command != NULL &&
658 additional->force_command != NULL) {
659 if (strcmp(primary->force_command,
660 additional->force_command) == 0) {
662 ret->force_command = strdup(primary->force_command);
663 if (ret->force_command == NULL)
666 errstr = "forced command options do not match";
669 } else if (primary->force_command != NULL) {
670 if ((ret->force_command = strdup(
671 primary->force_command)) == NULL)
673 } else if (additional->force_command != NULL) {
674 if ((ret->force_command = strdup(
675 additional->force_command)) == NULL)
684 errstr = "memory allocation failed";
688 sshauthopt_free(ret);
696 sshauthopt_copy(const struct sshauthopt *orig)
698 struct sshauthopt *ret;
700 if ((ret = sshauthopt_new()) == NULL)
703 #define OPTSCALAR(x) ret->x = orig->x
704 OPTSCALAR(permit_port_forwarding_flag);
705 OPTSCALAR(permit_agent_forwarding_flag);
706 OPTSCALAR(permit_x11_forwarding_flag);
707 OPTSCALAR(permit_pty_flag);
708 OPTSCALAR(permit_user_rc);
709 OPTSCALAR(restricted);
710 OPTSCALAR(cert_authority);
711 OPTSCALAR(force_tun_device);
712 OPTSCALAR(valid_before);
714 #define OPTSTRING(x) \
716 if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
717 sshauthopt_free(ret); \
721 OPTSTRING(cert_principals);
722 OPTSTRING(force_command);
723 OPTSTRING(required_from_host_cert);
724 OPTSTRING(required_from_host_keys);
727 if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
728 dup_strings(&ret->permitopen, &ret->npermitopen,
729 orig->permitopen, orig->npermitopen) != 0 ||
730 dup_strings(&ret->permitlisten, &ret->npermitlisten,
731 orig->permitlisten, orig->npermitlisten) != 0) {
732 sshauthopt_free(ret);
739 serialise_array(struct sshbuf *m, char **a, size_t n)
746 return SSH_ERR_INTERNAL_ERROR;
748 if ((b = sshbuf_new()) == NULL) {
749 return SSH_ERR_ALLOC_FAIL;
751 for (i = 0; i < n; i++) {
752 if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
757 if ((r = sshbuf_put_u32(m, n)) != 0 ||
758 (r = sshbuf_put_stringb(m, b)) != 0) {
767 deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
771 struct sshbuf *b = NULL;
773 int r = SSH_ERR_INTERNAL_ERROR;
775 if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
776 (r = sshbuf_froms(m, &b)) != 0)
779 r = SSH_ERR_INVALID_FORMAT;
783 if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
784 r = SSH_ERR_ALLOC_FAIL;
787 for (i = 0; i < n; i++) {
788 if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
798 for (i = 0; i < n; i++)
806 serialise_nullable_string(struct sshbuf *m, const char *s)
810 if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
811 (r = sshbuf_put_cstring(m, s)) != 0)
817 deserialise_nullable_string(struct sshbuf *m, char **sp)
823 if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
824 (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
830 sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
833 int r = SSH_ERR_INTERNAL_ERROR;
835 /* Flag and simple integer options */
836 if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
837 (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
838 (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
839 (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
840 (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
841 (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
842 (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
843 (r = sshbuf_put_u64(m, opts->valid_before)) != 0)
846 /* tunnel number can be negative to indicate "unset" */
847 if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
848 (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
849 0 : (u_int)opts->force_tun_device)) != 0)
852 /* String options; these may be NULL */
853 if ((r = serialise_nullable_string(m,
854 untrusted ? "yes" : opts->cert_principals)) != 0 ||
855 (r = serialise_nullable_string(m,
856 untrusted ? "true" : opts->force_command)) != 0 ||
857 (r = serialise_nullable_string(m,
858 untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
859 (r = serialise_nullable_string(m,
860 untrusted ? NULL : opts->required_from_host_keys)) != 0)
864 if ((r = serialise_array(m, opts->env,
865 untrusted ? 0 : opts->nenv)) != 0 ||
866 (r = serialise_array(m, opts->permitopen,
867 untrusted ? 0 : opts->npermitopen)) != 0 ||
868 (r = serialise_array(m, opts->permitlisten,
869 untrusted ? 0 : opts->npermitlisten)) != 0)
877 sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
879 struct sshauthopt *opts = NULL;
880 int r = SSH_ERR_INTERNAL_ERROR;
884 if ((opts = calloc(1, sizeof(*opts))) == NULL)
885 return SSH_ERR_ALLOC_FAIL;
887 #define OPT_FLAG(x) \
889 if ((r = sshbuf_get_u8(m, &f)) != 0) \
893 OPT_FLAG(permit_port_forwarding_flag);
894 OPT_FLAG(permit_agent_forwarding_flag);
895 OPT_FLAG(permit_x11_forwarding_flag);
896 OPT_FLAG(permit_pty_flag);
897 OPT_FLAG(permit_user_rc);
898 OPT_FLAG(restricted);
899 OPT_FLAG(cert_authority);
902 if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
905 /* tunnel number can be negative to indicate "unset" */
906 if ((r = sshbuf_get_u8(m, &f)) != 0 ||
907 (r = sshbuf_get_u32(m, &tmp)) != 0)
909 opts->force_tun_device = f ? -1 : (int)tmp;
911 /* String options may be NULL */
912 if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
913 (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
914 (r = deserialise_nullable_string(m,
915 &opts->required_from_host_cert)) != 0 ||
916 (r = deserialise_nullable_string(m,
917 &opts->required_from_host_keys)) != 0)
921 if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
922 (r = deserialise_array(m,
923 &opts->permitopen, &opts->npermitopen)) != 0 ||
924 (r = deserialise_array(m,
925 &opts->permitlisten, &opts->npermitlisten)) != 0)
933 sshauthopt_free(opts);