1 /* $OpenBSD: auth-options.c,v 1.78 2018/03/14 05:35:40 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 explicit_bzero(opts, sizeof(*opts));
291 sshauthopt_new_with_keys_defaults(void)
293 struct sshauthopt *ret = NULL;
295 if ((ret = sshauthopt_new()) == NULL)
298 /* Defaults for authorized_keys flags */
299 ret->permit_port_forwarding_flag = 1;
300 ret->permit_agent_forwarding_flag = 1;
301 ret->permit_x11_forwarding_flag = 1;
302 ret->permit_pty_flag = 1;
303 ret->permit_user_rc = 1;
308 sshauthopt_parse(const char *opts, const char **errstrp)
310 char **oarray, *opt, *cp, *tmp, *host;
312 struct sshauthopt *ret = NULL;
313 const char *errstr = "unknown error";
314 uint64_t valid_before;
318 if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
324 while (*opts && *opts != ' ' && *opts != '\t') {
326 if ((r = opt_flag("restrict", 0, &opts)) != -1) {
328 ret->permit_port_forwarding_flag = 0;
329 ret->permit_agent_forwarding_flag = 0;
330 ret->permit_x11_forwarding_flag = 0;
331 ret->permit_pty_flag = 0;
332 ret->permit_user_rc = 0;
333 } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
334 ret->cert_authority = r;
335 } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
336 ret->permit_port_forwarding_flag = r == 1;
337 } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
338 ret->permit_agent_forwarding_flag = r == 1;
339 } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
340 ret->permit_x11_forwarding_flag = r == 1;
341 } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
342 ret->permit_pty_flag = r == 1;
343 } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
344 ret->permit_user_rc = r == 1;
345 } else if (opt_match(&opts, "command")) {
346 if (ret->force_command != NULL) {
347 errstr = "multiple \"command\" clauses";
350 ret->force_command = opt_dequote(&opts, &errstr);
351 if (ret->force_command == NULL)
353 } else if (opt_match(&opts, "principals")) {
354 if (ret->cert_principals != NULL) {
355 errstr = "multiple \"principals\" clauses";
358 ret->cert_principals = opt_dequote(&opts, &errstr);
359 if (ret->cert_principals == NULL)
361 } else if (opt_match(&opts, "from")) {
362 if (ret->required_from_host_keys != NULL) {
363 errstr = "multiple \"from\" clauses";
366 ret->required_from_host_keys = opt_dequote(&opts,
368 if (ret->required_from_host_keys == NULL)
370 } else if (opt_match(&opts, "expiry-time")) {
371 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
373 if (parse_absolute_time(opt, &valid_before) != 0 ||
376 errstr = "invalid expires time";
380 if (ret->valid_before == 0 ||
381 valid_before < ret->valid_before)
382 ret->valid_before = valid_before;
383 } else if (opt_match(&opts, "environment")) {
384 if (ret->nenv > INT_MAX) {
385 errstr = "too many environment strings";
388 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
390 /* env name must be alphanumeric and followed by '=' */
391 if ((tmp = strchr(opt, '=')) == NULL) {
393 errstr = "invalid environment string";
396 for (cp = opt; cp < tmp; cp++) {
397 if (!isalnum((u_char)*cp)) {
399 errstr = "invalid environment string";
405 if ((ret->env = recallocarray(ret->env, ret->nenv,
406 ret->nenv + 1, sizeof(*ret->env))) == NULL) {
408 ret->env = oarray; /* put it back for cleanup */
411 ret->env[ret->nenv++] = opt;
412 } else if (opt_match(&opts, "permitopen")) {
413 if (ret->npermitopen > INT_MAX) {
414 errstr = "too many permitopens";
417 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
419 if ((tmp = strdup(opt)) == NULL) {
424 /* validate syntax of permitopen before recording it. */
426 if (host == NULL || strlen(host) >= NI_MAXHOST) {
429 errstr = "invalid permitopen hostname";
433 * don't want to use permitopen_port to avoid
434 * dependency on channels.[ch] here.
437 (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
440 errstr = "invalid permitopen port";
443 /* XXX - add streamlocal support */
446 oarray = ret->permitopen;
447 if ((ret->permitopen = recallocarray(ret->permitopen,
448 ret->npermitopen, ret->npermitopen + 1,
449 sizeof(*ret->permitopen))) == NULL) {
451 ret->permitopen = oarray;
454 ret->permitopen[ret->npermitopen++] = opt;
455 } else if (opt_match(&opts, "tunnel")) {
456 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
458 ret->force_tun_device = a2tun(opt, NULL);
460 if (ret->force_tun_device == SSH_TUNID_ERR) {
461 errstr = "invalid tun device";
466 * Skip the comma, and move to the next option
467 * (or break out if there are no more).
469 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
470 break; /* End of options. */
471 /* Anything other than a comma is an unknown option */
473 errstr = "unknown key option";
478 errstr = "unexpected end-of-options";
489 errstr = "memory allocation failed";
491 sshauthopt_free(ret);
498 sshauthopt_from_cert(struct sshkey *k)
500 struct sshauthopt *ret;
502 if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
503 k->cert->type != SSH2_CERT_TYPE_USER)
506 if ((ret = sshauthopt_new()) == NULL)
509 /* Handle options and critical extensions separately */
510 if (cert_option_list(ret, k->cert->critical,
511 OPTIONS_CRITICAL, 1) == -1) {
512 sshauthopt_free(ret);
515 if (cert_option_list(ret, k->cert->extensions,
516 OPTIONS_EXTENSIONS, 0) == -1) {
517 sshauthopt_free(ret);
525 * Merges "additional" options to "primary" and returns the result.
526 * NB. Some options from primary have primacy.
529 sshauthopt_merge(const struct sshauthopt *primary,
530 const struct sshauthopt *additional, const char **errstrp)
532 struct sshauthopt *ret;
533 const char *errstr = "internal error";
539 if ((ret = sshauthopt_new()) == NULL)
542 /* cert_authority and cert_principals are cleared in result */
544 /* Prefer access lists from primary. */
545 /* XXX err is both set and mismatch? */
546 tmp = primary->required_from_host_cert;
548 tmp = additional->required_from_host_cert;
549 if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
551 tmp = primary->required_from_host_keys;
553 tmp = additional->required_from_host_keys;
554 if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
557 /* force_tun_device, permitopen and environment prefer the primary. */
558 ret->force_tun_device = primary->force_tun_device;
559 if (ret->force_tun_device == -1)
560 ret->force_tun_device = additional->force_tun_device;
561 if (primary->nenv > 0) {
562 if (dup_strings(&ret->env, &ret->nenv,
563 primary->env, primary->nenv) != 0)
565 } else if (additional->nenv) {
566 if (dup_strings(&ret->env, &ret->nenv,
567 additional->env, additional->nenv) != 0)
570 if (primary->npermitopen > 0) {
571 if (dup_strings(&ret->permitopen, &ret->npermitopen,
572 primary->permitopen, primary->npermitopen) != 0)
574 } else if (additional->npermitopen > 0) {
575 if (dup_strings(&ret->permitopen, &ret->npermitopen,
576 additional->permitopen, additional->npermitopen) != 0)
580 /* Flags are logical-AND (i.e. must be set in both for permission) */
581 #define OPTFLAG(x) ret->x = (primary->x == 1) && (additional->x == 1)
582 OPTFLAG(permit_port_forwarding_flag);
583 OPTFLAG(permit_agent_forwarding_flag);
584 OPTFLAG(permit_x11_forwarding_flag);
585 OPTFLAG(permit_pty_flag);
586 OPTFLAG(permit_user_rc);
589 /* Earliest expiry time should win */
590 if (primary->valid_before != 0)
591 ret->valid_before = primary->valid_before;
592 if (additional->valid_before != 0 &&
593 additional->valid_before < ret->valid_before)
594 ret->valid_before = additional->valid_before;
597 * When both multiple forced-command are specified, only
598 * proceed if they are identical, otherwise fail.
600 if (primary->force_command != NULL &&
601 additional->force_command != NULL) {
602 if (strcmp(primary->force_command,
603 additional->force_command) == 0) {
605 ret->force_command = strdup(primary->force_command);
606 if (ret->force_command == NULL)
609 errstr = "forced command options do not match";
612 } else if (primary->force_command != NULL) {
613 if ((ret->force_command = strdup(
614 primary->force_command)) == NULL)
616 } else if (additional->force_command != NULL) {
617 if ((ret->force_command = strdup(
618 additional->force_command)) == NULL)
627 errstr = "memory allocation failed";
631 sshauthopt_free(ret);
639 sshauthopt_copy(const struct sshauthopt *orig)
641 struct sshauthopt *ret;
643 if ((ret = sshauthopt_new()) == NULL)
646 #define OPTSCALAR(x) ret->x = orig->x
647 OPTSCALAR(permit_port_forwarding_flag);
648 OPTSCALAR(permit_agent_forwarding_flag);
649 OPTSCALAR(permit_x11_forwarding_flag);
650 OPTSCALAR(permit_pty_flag);
651 OPTSCALAR(permit_user_rc);
652 OPTSCALAR(restricted);
653 OPTSCALAR(cert_authority);
654 OPTSCALAR(force_tun_device);
655 OPTSCALAR(valid_before);
657 #define OPTSTRING(x) \
659 if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
660 sshauthopt_free(ret); \
664 OPTSTRING(cert_principals);
665 OPTSTRING(force_command);
666 OPTSTRING(required_from_host_cert);
667 OPTSTRING(required_from_host_keys);
670 if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
671 dup_strings(&ret->permitopen, &ret->npermitopen,
672 orig->permitopen, orig->npermitopen) != 0) {
673 sshauthopt_free(ret);
680 serialise_array(struct sshbuf *m, char **a, size_t n)
687 return SSH_ERR_INTERNAL_ERROR;
689 if ((b = sshbuf_new()) == NULL) {
690 return SSH_ERR_ALLOC_FAIL;
692 for (i = 0; i < n; i++) {
693 if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
698 if ((r = sshbuf_put_u32(m, n)) != 0 ||
699 (r = sshbuf_put_stringb(m, b)) != 0) {
708 deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
712 struct sshbuf *b = NULL;
714 int r = SSH_ERR_INTERNAL_ERROR;
716 if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
717 (r = sshbuf_froms(m, &b)) != 0)
720 r = SSH_ERR_INVALID_FORMAT;
724 if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
725 r = SSH_ERR_ALLOC_FAIL;
728 for (i = 0; i < n; i++) {
729 if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
739 for (i = 0; i < n; i++)
747 serialise_nullable_string(struct sshbuf *m, const char *s)
751 if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
752 (r = sshbuf_put_cstring(m, s)) != 0)
758 deserialise_nullable_string(struct sshbuf *m, char **sp)
764 if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
765 (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
771 sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
774 int r = SSH_ERR_INTERNAL_ERROR;
776 /* Flag and simple integer options */
777 if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
778 (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
779 (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
780 (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
781 (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
782 (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
783 (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
784 (r = sshbuf_put_u64(m, opts->valid_before)) != 0)
787 /* tunnel number can be negative to indicate "unset" */
788 if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
789 (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
790 0 : (u_int)opts->force_tun_device)) != 0)
793 /* String options; these may be NULL */
794 if ((r = serialise_nullable_string(m,
795 untrusted ? "yes" : opts->cert_principals)) != 0 ||
796 (r = serialise_nullable_string(m,
797 untrusted ? "true" : opts->force_command)) != 0 ||
798 (r = serialise_nullable_string(m,
799 untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
800 (r = serialise_nullable_string(m,
801 untrusted ? NULL : opts->required_from_host_keys)) != 0)
805 if ((r = serialise_array(m, opts->env,
806 untrusted ? 0 : opts->nenv)) != 0 ||
807 (r = serialise_array(m, opts->permitopen,
808 untrusted ? 0 : opts->npermitopen)) != 0)
816 sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
818 struct sshauthopt *opts = NULL;
819 int r = SSH_ERR_INTERNAL_ERROR;
823 if ((opts = calloc(1, sizeof(*opts))) == NULL)
824 return SSH_ERR_ALLOC_FAIL;
826 #define OPT_FLAG(x) \
828 if ((r = sshbuf_get_u8(m, &f)) != 0) \
832 OPT_FLAG(permit_port_forwarding_flag);
833 OPT_FLAG(permit_agent_forwarding_flag);
834 OPT_FLAG(permit_x11_forwarding_flag);
835 OPT_FLAG(permit_pty_flag);
836 OPT_FLAG(permit_user_rc);
837 OPT_FLAG(restricted);
838 OPT_FLAG(cert_authority);
841 if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
844 /* tunnel number can be negative to indicate "unset" */
845 if ((r = sshbuf_get_u8(m, &f)) != 0 ||
846 (r = sshbuf_get_u32(m, &tmp)) != 0)
848 opts->force_tun_device = f ? -1 : (int)tmp;
850 /* String options may be NULL */
851 if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
852 (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
853 (r = deserialise_nullable_string(m,
854 &opts->required_from_host_cert)) != 0 ||
855 (r = deserialise_nullable_string(m,
856 &opts->required_from_host_keys)) != 0)
860 if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
861 (r = deserialise_array(m,
862 &opts->permitopen, &opts->npermitopen)) != 0)
870 sshauthopt_free(opts);