1 /* $OpenBSD: auth-options.c,v 1.101 2023/07/14 07:44:21 dtucker 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>
34 #include "openbsd-compat/sys-queue.h"
44 #include "auth-options.h"
47 dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
57 if (nsrc >= SIZE_MAX / sizeof(*src) ||
58 (dst = calloc(nsrc, sizeof(*src))) == NULL)
60 for (i = 0; i < nsrc; i++) {
61 if ((dst[i] = strdup(src[i])) == NULL) {
62 for (j = 0; j < i; j++)
74 #define OPTIONS_CRITICAL 1
75 #define OPTIONS_EXTENSIONS 2
77 cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
78 u_int which, int crit)
80 char *command, *allowed;
82 struct sshbuf *c = NULL, *data = NULL;
83 int r, ret = -1, found;
85 if ((c = sshbuf_fromb(oblob)) == NULL) {
86 error_f("sshbuf_fromb failed");
90 while (sshbuf_len(c) > 0) {
93 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
94 (r = sshbuf_froms(c, &data)) != 0) {
95 error_r(r, "Unable to parse certificate options");
98 debug3("found certificate option \"%.100s\" len %zu",
99 name, sshbuf_len(data));
101 if ((which & OPTIONS_EXTENSIONS) != 0) {
102 if (strcmp(name, "no-touch-required") == 0) {
103 opts->no_require_user_presence = 1;
105 } else if (strcmp(name, "permit-X11-forwarding") == 0) {
106 opts->permit_x11_forwarding_flag = 1;
108 } else if (strcmp(name,
109 "permit-agent-forwarding") == 0) {
110 opts->permit_agent_forwarding_flag = 1;
112 } else if (strcmp(name,
113 "permit-port-forwarding") == 0) {
114 opts->permit_port_forwarding_flag = 1;
116 } else if (strcmp(name, "permit-pty") == 0) {
117 opts->permit_pty_flag = 1;
119 } else if (strcmp(name, "permit-user-rc") == 0) {
120 opts->permit_user_rc = 1;
124 if (!found && (which & OPTIONS_CRITICAL) != 0) {
125 if (strcmp(name, "verify-required") == 0) {
126 opts->require_verify = 1;
128 } else if (strcmp(name, "force-command") == 0) {
129 if ((r = sshbuf_get_cstring(data, &command,
131 error_r(r, "Unable to parse \"%s\" "
135 if (opts->force_command != NULL) {
136 error("Certificate has multiple "
137 "force-command options");
141 opts->force_command = command;
143 } else if (strcmp(name, "source-address") == 0) {
144 if ((r = sshbuf_get_cstring(data, &allowed,
146 error_r(r, "Unable to parse \"%s\" "
150 if (opts->required_from_host_cert != NULL) {
151 error("Certificate has multiple "
152 "source-address options");
157 if (addr_match_cidr_list(NULL, allowed) == -1) {
158 error("Certificate source-address "
162 opts->required_from_host_cert = allowed;
169 error("Certificate critical option \"%s\" "
170 "is not supported", name);
173 logit("Certificate extension \"%s\" "
174 "is not supported", name);
176 } else if (sshbuf_len(data) != 0) {
177 error("Certificate option \"%s\" corrupt "
178 "(extra data)", name);
184 /* successfully parsed all options */
197 struct sshauthopt *ret;
199 if ((ret = calloc(1, sizeof(*ret))) == NULL)
201 ret->force_tun_device = -1;
206 sshauthopt_free(struct sshauthopt *opts)
213 free(opts->cert_principals);
214 free(opts->force_command);
215 free(opts->required_from_host_cert);
216 free(opts->required_from_host_keys);
218 for (i = 0; i < opts->nenv; i++)
222 for (i = 0; i < opts->npermitopen; i++)
223 free(opts->permitopen[i]);
224 free(opts->permitopen);
226 for (i = 0; i < opts->npermitlisten; i++)
227 free(opts->permitlisten[i]);
228 free(opts->permitlisten);
230 freezero(opts, sizeof(*opts));
234 sshauthopt_new_with_keys_defaults(void)
236 struct sshauthopt *ret = NULL;
238 if ((ret = sshauthopt_new()) == NULL)
241 /* Defaults for authorized_keys flags */
242 ret->permit_port_forwarding_flag = 1;
243 ret->permit_agent_forwarding_flag = 1;
244 ret->permit_x11_forwarding_flag = 1;
245 ret->permit_pty_flag = 1;
246 ret->permit_user_rc = 1;
251 * Parse and record a permitopen/permitlisten directive.
252 * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
255 handle_permit(const char **optsp, int allow_bare_port,
256 char ***permitsp, size_t *npermitsp, const char **errstrp)
258 char *opt, *tmp, *cp, *host, **permits = *permitsp;
259 size_t npermits = *npermitsp;
260 const char *errstr = "unknown error";
262 if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
263 *errstrp = "too many permission directives";
266 if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
269 if (allow_bare_port && strchr(opt, ':') == NULL) {
271 * Allow a bare port number in permitlisten to indicate a
272 * listen_host wildcard.
274 if (asprintf(&tmp, "*:%s", opt) == -1) {
276 *errstrp = "memory allocation failed";
282 if ((tmp = strdup(opt)) == NULL) {
284 *errstrp = "memory allocation failed";
288 /* validate syntax before recording it. */
289 host = hpdelim2(&cp, NULL);
290 if (host == NULL || strlen(host) >= NI_MAXHOST) {
293 *errstrp = "invalid permission hostname";
297 * don't want to use permitopen_port to avoid
298 * dependency on channels.[ch] here.
301 (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
304 *errstrp = "invalid permission port";
307 /* XXX - add streamlocal support */
310 if ((permits = recallocarray(permits, npermits, npermits + 1,
311 sizeof(*permits))) == NULL) {
313 /* NB. don't update *permitsp if alloc fails */
314 *errstrp = "memory allocation failed";
317 permits[npermits++] = opt;
319 *npermitsp = npermits;
324 sshauthopt_parse(const char *opts, const char **errstrp)
326 char **oarray, *opt, *cp, *tmp;
328 struct sshauthopt *ret = NULL;
329 const char *errstr = "unknown error";
330 uint64_t valid_before;
335 if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
341 while (*opts && *opts != ' ' && *opts != '\t') {
343 if ((r = opt_flag("restrict", 0, &opts)) != -1) {
345 ret->permit_port_forwarding_flag = 0;
346 ret->permit_agent_forwarding_flag = 0;
347 ret->permit_x11_forwarding_flag = 0;
348 ret->permit_pty_flag = 0;
349 ret->permit_user_rc = 0;
350 } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
351 ret->cert_authority = r;
352 } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
353 ret->permit_port_forwarding_flag = r == 1;
354 } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
355 ret->permit_agent_forwarding_flag = r == 1;
356 } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
357 ret->permit_x11_forwarding_flag = r == 1;
358 } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
359 ret->no_require_user_presence = r != 1; /* NB. flip */
360 } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
361 ret->require_verify = r == 1;
362 } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
363 ret->permit_pty_flag = r == 1;
364 } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
365 ret->permit_user_rc = r == 1;
366 } else if (opt_match(&opts, "command")) {
367 if (ret->force_command != NULL) {
368 errstr = "multiple \"command\" clauses";
371 ret->force_command = opt_dequote(&opts, &errstr);
372 if (ret->force_command == NULL)
374 } else if (opt_match(&opts, "principals")) {
375 if (ret->cert_principals != NULL) {
376 errstr = "multiple \"principals\" clauses";
379 ret->cert_principals = opt_dequote(&opts, &errstr);
380 if (ret->cert_principals == NULL)
382 } else if (opt_match(&opts, "from")) {
383 if (ret->required_from_host_keys != NULL) {
384 errstr = "multiple \"from\" clauses";
387 ret->required_from_host_keys = opt_dequote(&opts,
389 if (ret->required_from_host_keys == NULL)
391 } else if (opt_match(&opts, "expiry-time")) {
392 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
394 if (parse_absolute_time(opt, &valid_before) != 0 ||
397 errstr = "invalid expires time";
401 if (ret->valid_before == 0 ||
402 valid_before < ret->valid_before)
403 ret->valid_before = valid_before;
404 } else if (opt_match(&opts, "environment")) {
405 if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
406 errstr = "too many environment strings";
409 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
411 /* env name must be alphanumeric and followed by '=' */
412 if ((tmp = strchr(opt, '=')) == NULL) {
414 errstr = "invalid environment string";
417 if ((cp = strdup(opt)) == NULL) {
421 l = (size_t)(tmp - opt);
422 cp[l] = '\0'; /* truncate at '=' */
423 if (!valid_env_name(cp)) {
426 errstr = "invalid environment string";
429 /* Check for duplicates; XXX O(n*log(n)) */
430 for (i = 0; i < ret->nenv; i++) {
431 if (strncmp(ret->env[i], cp, l) == 0 &&
432 ret->env[i][l] == '=')
436 /* First match wins */
437 if (i >= ret->nenv) {
440 if ((ret->env = recallocarray(ret->env,
441 ret->nenv, ret->nenv + 1,
442 sizeof(*ret->env))) == NULL) {
444 /* put it back for cleanup */
448 ret->env[ret->nenv++] = opt;
449 opt = NULL; /* transferred */
452 } else if (opt_match(&opts, "permitopen")) {
453 if (handle_permit(&opts, 0, &ret->permitopen,
454 &ret->npermitopen, &errstr) != 0)
456 } else if (opt_match(&opts, "permitlisten")) {
457 if (handle_permit(&opts, 1, &ret->permitlisten,
458 &ret->npermitlisten, &errstr) != 0)
460 } else if (opt_match(&opts, "tunnel")) {
461 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
463 ret->force_tun_device = a2tun(opt, NULL);
465 if (ret->force_tun_device == SSH_TUNID_ERR) {
466 errstr = "invalid tun device";
471 * Skip the comma, and move to the next option
472 * (or break out if there are no more).
474 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
475 break; /* End of options. */
476 /* Anything other than a comma is an unknown option */
478 errstr = "unknown key option";
483 errstr = "unexpected end-of-options";
494 errstr = "memory allocation failed";
496 sshauthopt_free(ret);
503 sshauthopt_from_cert(struct sshkey *k)
505 struct sshauthopt *ret;
507 if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
508 k->cert->type != SSH2_CERT_TYPE_USER)
511 if ((ret = sshauthopt_new()) == NULL)
514 /* Handle options and critical extensions separately */
515 if (cert_option_list(ret, k->cert->critical,
516 OPTIONS_CRITICAL, 1) == -1) {
517 sshauthopt_free(ret);
520 if (cert_option_list(ret, k->cert->extensions,
521 OPTIONS_EXTENSIONS, 0) == -1) {
522 sshauthopt_free(ret);
530 * Merges "additional" options to "primary" and returns the result.
531 * NB. Some options from primary have primacy.
534 sshauthopt_merge(const struct sshauthopt *primary,
535 const struct sshauthopt *additional, const char **errstrp)
537 struct sshauthopt *ret;
538 const char *errstr = "internal error";
544 if ((ret = sshauthopt_new()) == NULL)
547 /* cert_authority and cert_principals are cleared in result */
549 /* Prefer access lists from primary. */
550 /* XXX err is both set and mismatch? */
551 tmp = primary->required_from_host_cert;
553 tmp = additional->required_from_host_cert;
554 if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
556 tmp = primary->required_from_host_keys;
558 tmp = additional->required_from_host_keys;
559 if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
563 * force_tun_device, permitopen/permitlisten and environment all
564 * prefer the primary.
566 ret->force_tun_device = primary->force_tun_device;
567 if (ret->force_tun_device == -1)
568 ret->force_tun_device = additional->force_tun_device;
569 if (primary->nenv > 0) {
570 if (dup_strings(&ret->env, &ret->nenv,
571 primary->env, primary->nenv) != 0)
573 } else if (additional->nenv) {
574 if (dup_strings(&ret->env, &ret->nenv,
575 additional->env, additional->nenv) != 0)
578 if (primary->npermitopen > 0) {
579 if (dup_strings(&ret->permitopen, &ret->npermitopen,
580 primary->permitopen, primary->npermitopen) != 0)
582 } else if (additional->npermitopen > 0) {
583 if (dup_strings(&ret->permitopen, &ret->npermitopen,
584 additional->permitopen, additional->npermitopen) != 0)
588 if (primary->npermitlisten > 0) {
589 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
590 primary->permitlisten, primary->npermitlisten) != 0)
592 } else if (additional->npermitlisten > 0) {
593 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
594 additional->permitlisten, additional->npermitlisten) != 0)
598 #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
599 #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
600 /* Permissive flags are logical-AND (i.e. must be set in both) */
601 OPTFLAG_AND(permit_port_forwarding_flag);
602 OPTFLAG_AND(permit_agent_forwarding_flag);
603 OPTFLAG_AND(permit_x11_forwarding_flag);
604 OPTFLAG_AND(permit_pty_flag);
605 OPTFLAG_AND(permit_user_rc);
606 OPTFLAG_AND(no_require_user_presence);
607 /* Restrictive flags are logical-OR (i.e. must be set in either) */
608 OPTFLAG_OR(require_verify);
611 /* Earliest expiry time should win */
612 if (primary->valid_before != 0)
613 ret->valid_before = primary->valid_before;
614 if (additional->valid_before != 0 &&
615 additional->valid_before < ret->valid_before)
616 ret->valid_before = additional->valid_before;
619 * When both multiple forced-command are specified, only
620 * proceed if they are identical, otherwise fail.
622 if (primary->force_command != NULL &&
623 additional->force_command != NULL) {
624 if (strcmp(primary->force_command,
625 additional->force_command) == 0) {
627 ret->force_command = strdup(primary->force_command);
628 if (ret->force_command == NULL)
631 errstr = "forced command options do not match";
634 } else if (primary->force_command != NULL) {
635 if ((ret->force_command = strdup(
636 primary->force_command)) == NULL)
638 } else if (additional->force_command != NULL) {
639 if ((ret->force_command = strdup(
640 additional->force_command)) == NULL)
649 errstr = "memory allocation failed";
653 sshauthopt_free(ret);
661 sshauthopt_copy(const struct sshauthopt *orig)
663 struct sshauthopt *ret;
665 if ((ret = sshauthopt_new()) == NULL)
668 #define OPTSCALAR(x) ret->x = orig->x
669 OPTSCALAR(permit_port_forwarding_flag);
670 OPTSCALAR(permit_agent_forwarding_flag);
671 OPTSCALAR(permit_x11_forwarding_flag);
672 OPTSCALAR(permit_pty_flag);
673 OPTSCALAR(permit_user_rc);
674 OPTSCALAR(restricted);
675 OPTSCALAR(cert_authority);
676 OPTSCALAR(force_tun_device);
677 OPTSCALAR(valid_before);
678 OPTSCALAR(no_require_user_presence);
679 OPTSCALAR(require_verify);
681 #define OPTSTRING(x) \
683 if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
684 sshauthopt_free(ret); \
688 OPTSTRING(cert_principals);
689 OPTSTRING(force_command);
690 OPTSTRING(required_from_host_cert);
691 OPTSTRING(required_from_host_keys);
694 if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
695 dup_strings(&ret->permitopen, &ret->npermitopen,
696 orig->permitopen, orig->npermitopen) != 0 ||
697 dup_strings(&ret->permitlisten, &ret->npermitlisten,
698 orig->permitlisten, orig->npermitlisten) != 0) {
699 sshauthopt_free(ret);
706 serialise_array(struct sshbuf *m, char **a, size_t n)
710 int r = SSH_ERR_INTERNAL_ERROR;
713 return SSH_ERR_INTERNAL_ERROR;
715 if ((b = sshbuf_new()) == NULL) {
716 return SSH_ERR_ALLOC_FAIL;
718 for (i = 0; i < n; i++) {
719 if ((r = sshbuf_put_cstring(b, a[i])) != 0)
722 if ((r = sshbuf_put_u32(m, n)) != 0 ||
723 (r = sshbuf_put_stringb(m, b)) != 0)
733 deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
737 struct sshbuf *b = NULL;
739 int r = SSH_ERR_INTERNAL_ERROR;
741 if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
742 (r = sshbuf_froms(m, &b)) != 0)
745 r = SSH_ERR_INVALID_FORMAT;
749 if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
750 r = SSH_ERR_ALLOC_FAIL;
753 for (i = 0; i < n; i++) {
754 if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
765 for (i = 0; i < n; i++)
774 serialise_nullable_string(struct sshbuf *m, const char *s)
778 if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
779 (r = sshbuf_put_cstring(m, s)) != 0)
785 deserialise_nullable_string(struct sshbuf *m, char **sp)
791 if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
792 (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
798 sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
801 int r = SSH_ERR_INTERNAL_ERROR;
804 if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
805 (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
806 (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
807 (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
808 (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
809 (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
810 (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
811 (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
812 (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
815 /* Simple integer options */
816 if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
819 /* tunnel number can be negative to indicate "unset" */
820 if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
821 (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
822 0 : (u_int)opts->force_tun_device)) != 0)
825 /* String options; these may be NULL */
826 if ((r = serialise_nullable_string(m,
827 untrusted ? "yes" : opts->cert_principals)) != 0 ||
828 (r = serialise_nullable_string(m,
829 untrusted ? "true" : opts->force_command)) != 0 ||
830 (r = serialise_nullable_string(m,
831 untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
832 (r = serialise_nullable_string(m,
833 untrusted ? NULL : opts->required_from_host_keys)) != 0)
837 if ((r = serialise_array(m, opts->env,
838 untrusted ? 0 : opts->nenv)) != 0 ||
839 (r = serialise_array(m, opts->permitopen,
840 untrusted ? 0 : opts->npermitopen)) != 0 ||
841 (r = serialise_array(m, opts->permitlisten,
842 untrusted ? 0 : opts->npermitlisten)) != 0)
850 sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
852 struct sshauthopt *opts = NULL;
853 int r = SSH_ERR_INTERNAL_ERROR;
857 if ((opts = calloc(1, sizeof(*opts))) == NULL)
858 return SSH_ERR_ALLOC_FAIL;
861 #define OPT_FLAG(x) \
863 if ((r = sshbuf_get_u8(m, &f)) != 0) \
867 OPT_FLAG(permit_port_forwarding_flag);
868 OPT_FLAG(permit_agent_forwarding_flag);
869 OPT_FLAG(permit_x11_forwarding_flag);
870 OPT_FLAG(permit_pty_flag);
871 OPT_FLAG(permit_user_rc);
872 OPT_FLAG(restricted);
873 OPT_FLAG(cert_authority);
874 OPT_FLAG(no_require_user_presence);
875 OPT_FLAG(require_verify);
878 /* Simple integer options */
879 if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
882 /* tunnel number can be negative to indicate "unset" */
883 if ((r = sshbuf_get_u8(m, &f)) != 0 ||
884 (r = sshbuf_get_u32(m, &tmp)) != 0)
886 opts->force_tun_device = f ? -1 : (int)tmp;
888 /* String options may be NULL */
889 if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
890 (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
891 (r = deserialise_nullable_string(m,
892 &opts->required_from_host_cert)) != 0 ||
893 (r = deserialise_nullable_string(m,
894 &opts->required_from_host_keys)) != 0)
898 if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
899 (r = deserialise_array(m,
900 &opts->permitopen, &opts->npermitopen)) != 0 ||
901 (r = deserialise_array(m,
902 &opts->permitlisten, &opts->npermitlisten)) != 0)
910 sshauthopt_free(opts);