1 /* $OpenBSD: auth-options.c,v 1.92 2020/03/06 18:15:38 markus 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>
31 #include "openbsd-compat/sys-queue.h"
41 #include "auth-options.h"
44 dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
54 if ((dst = calloc(nsrc, sizeof(*src))) == NULL)
56 for (i = 0; i < nsrc; i++) {
57 if ((dst[i] = strdup(src[i])) == NULL) {
58 for (j = 0; j < i; j++)
70 #define OPTIONS_CRITICAL 1
71 #define OPTIONS_EXTENSIONS 2
73 cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
74 u_int which, int crit)
76 char *command, *allowed;
78 struct sshbuf *c = NULL, *data = NULL;
79 int r, ret = -1, found;
81 if ((c = sshbuf_fromb(oblob)) == NULL) {
82 error("%s: sshbuf_fromb failed", __func__);
86 while (sshbuf_len(c) > 0) {
89 if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
90 (r = sshbuf_froms(c, &data)) != 0) {
91 error("Unable to parse certificate options: %s",
95 debug3("found certificate option \"%.100s\" len %zu",
96 name, sshbuf_len(data));
98 if ((which & OPTIONS_EXTENSIONS) != 0) {
99 if (strcmp(name, "no-touch-required") == 0) {
100 opts->no_require_user_presence = 1;
102 } else if (strcmp(name, "permit-X11-forwarding") == 0) {
103 opts->permit_x11_forwarding_flag = 1;
105 } else if (strcmp(name,
106 "permit-agent-forwarding") == 0) {
107 opts->permit_agent_forwarding_flag = 1;
109 } else if (strcmp(name,
110 "permit-port-forwarding") == 0) {
111 opts->permit_port_forwarding_flag = 1;
113 } else if (strcmp(name, "permit-pty") == 0) {
114 opts->permit_pty_flag = 1;
116 } else if (strcmp(name, "permit-user-rc") == 0) {
117 opts->permit_user_rc = 1;
121 if (!found && (which & OPTIONS_CRITICAL) != 0) {
122 if (strcmp(name, "force-command") == 0) {
123 if ((r = sshbuf_get_cstring(data, &command,
125 error("Unable to parse \"%s\" "
126 "section: %s", name, ssh_err(r));
129 if (opts->force_command != NULL) {
130 error("Certificate has multiple "
131 "force-command options");
135 opts->force_command = command;
138 if (strcmp(name, "source-address") == 0) {
139 if ((r = sshbuf_get_cstring(data, &allowed,
141 error("Unable to parse \"%s\" "
142 "section: %s", name, ssh_err(r));
145 if (opts->required_from_host_cert != NULL) {
146 error("Certificate has multiple "
147 "source-address options");
152 if (addr_match_cidr_list(NULL, allowed) == -1) {
153 error("Certificate source-address "
157 opts->required_from_host_cert = allowed;
164 error("Certificate critical option \"%s\" "
165 "is not supported", name);
168 logit("Certificate extension \"%s\" "
169 "is not supported", name);
171 } else if (sshbuf_len(data) != 0) {
172 error("Certificate option \"%s\" corrupt "
173 "(extra data)", name);
179 /* successfully parsed all options */
192 struct sshauthopt *ret;
194 if ((ret = calloc(1, sizeof(*ret))) == NULL)
196 ret->force_tun_device = -1;
201 sshauthopt_free(struct sshauthopt *opts)
208 free(opts->cert_principals);
209 free(opts->force_command);
210 free(opts->required_from_host_cert);
211 free(opts->required_from_host_keys);
213 for (i = 0; i < opts->nenv; i++)
217 for (i = 0; i < opts->npermitopen; i++)
218 free(opts->permitopen[i]);
219 free(opts->permitopen);
221 for (i = 0; i < opts->npermitlisten; i++)
222 free(opts->permitlisten[i]);
223 free(opts->permitlisten);
225 freezero(opts, sizeof(*opts));
229 sshauthopt_new_with_keys_defaults(void)
231 struct sshauthopt *ret = NULL;
233 if ((ret = sshauthopt_new()) == NULL)
236 /* Defaults for authorized_keys flags */
237 ret->permit_port_forwarding_flag = 1;
238 ret->permit_agent_forwarding_flag = 1;
239 ret->permit_x11_forwarding_flag = 1;
240 ret->permit_pty_flag = 1;
241 ret->permit_user_rc = 1;
246 * Parse and record a permitopen/permitlisten directive.
247 * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
250 handle_permit(const char **optsp, int allow_bare_port,
251 char ***permitsp, size_t *npermitsp, const char **errstrp)
253 char *opt, *tmp, *cp, *host, **permits = *permitsp;
254 size_t npermits = *npermitsp;
255 const char *errstr = "unknown error";
257 if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
258 *errstrp = "too many permission directives";
261 if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
264 if (allow_bare_port && strchr(opt, ':') == NULL) {
266 * Allow a bare port number in permitlisten to indicate a
267 * listen_host wildcard.
269 if (asprintf(&tmp, "*:%s", opt) == -1) {
271 *errstrp = "memory allocation failed";
277 if ((tmp = strdup(opt)) == NULL) {
279 *errstrp = "memory allocation failed";
283 /* validate syntax before recording it. */
285 if (host == NULL || strlen(host) >= NI_MAXHOST) {
288 *errstrp = "invalid permission hostname";
292 * don't want to use permitopen_port to avoid
293 * dependency on channels.[ch] here.
296 (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
299 *errstrp = "invalid permission port";
302 /* XXX - add streamlocal support */
305 if ((permits = recallocarray(permits, npermits, npermits + 1,
306 sizeof(*permits))) == NULL) {
308 /* NB. don't update *permitsp if alloc fails */
309 *errstrp = "memory allocation failed";
312 permits[npermits++] = opt;
314 *npermitsp = npermits;
319 sshauthopt_parse(const char *opts, const char **errstrp)
321 char **oarray, *opt, *cp, *tmp;
323 struct sshauthopt *ret = NULL;
324 const char *errstr = "unknown error";
325 uint64_t valid_before;
329 if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
335 while (*opts && *opts != ' ' && *opts != '\t') {
337 if ((r = opt_flag("restrict", 0, &opts)) != -1) {
339 ret->permit_port_forwarding_flag = 0;
340 ret->permit_agent_forwarding_flag = 0;
341 ret->permit_x11_forwarding_flag = 0;
342 ret->permit_pty_flag = 0;
343 ret->permit_user_rc = 0;
344 } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
345 ret->cert_authority = r;
346 } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
347 ret->permit_port_forwarding_flag = r == 1;
348 } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
349 ret->permit_agent_forwarding_flag = r == 1;
350 } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
351 ret->permit_x11_forwarding_flag = r == 1;
352 } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
353 ret->no_require_user_presence = r != 1; /* NB. flip */
354 } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
355 ret->permit_pty_flag = r == 1;
356 } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
357 ret->permit_user_rc = r == 1;
358 } else if (opt_match(&opts, "command")) {
359 if (ret->force_command != NULL) {
360 errstr = "multiple \"command\" clauses";
363 ret->force_command = opt_dequote(&opts, &errstr);
364 if (ret->force_command == NULL)
366 } else if (opt_match(&opts, "principals")) {
367 if (ret->cert_principals != NULL) {
368 errstr = "multiple \"principals\" clauses";
371 ret->cert_principals = opt_dequote(&opts, &errstr);
372 if (ret->cert_principals == NULL)
374 } else if (opt_match(&opts, "from")) {
375 if (ret->required_from_host_keys != NULL) {
376 errstr = "multiple \"from\" clauses";
379 ret->required_from_host_keys = opt_dequote(&opts,
381 if (ret->required_from_host_keys == NULL)
383 } else if (opt_match(&opts, "expiry-time")) {
384 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
386 if (parse_absolute_time(opt, &valid_before) != 0 ||
389 errstr = "invalid expires time";
393 if (ret->valid_before == 0 ||
394 valid_before < ret->valid_before)
395 ret->valid_before = valid_before;
396 } else if (opt_match(&opts, "environment")) {
397 if (ret->nenv > INT_MAX) {
398 errstr = "too many environment strings";
401 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
403 /* env name must be alphanumeric and followed by '=' */
404 if ((tmp = strchr(opt, '=')) == NULL) {
406 errstr = "invalid environment string";
409 if ((cp = strdup(opt)) == NULL)
411 cp[tmp - opt] = '\0'; /* truncate at '=' */
412 if (!valid_env_name(cp)) {
415 errstr = "invalid environment string";
421 if ((ret->env = recallocarray(ret->env, ret->nenv,
422 ret->nenv + 1, sizeof(*ret->env))) == NULL) {
424 ret->env = oarray; /* put it back for cleanup */
427 ret->env[ret->nenv++] = opt;
428 } else if (opt_match(&opts, "permitopen")) {
429 if (handle_permit(&opts, 0, &ret->permitopen,
430 &ret->npermitopen, &errstr) != 0)
432 } else if (opt_match(&opts, "permitlisten")) {
433 if (handle_permit(&opts, 1, &ret->permitlisten,
434 &ret->npermitlisten, &errstr) != 0)
436 } else if (opt_match(&opts, "tunnel")) {
437 if ((opt = opt_dequote(&opts, &errstr)) == NULL)
439 ret->force_tun_device = a2tun(opt, NULL);
441 if (ret->force_tun_device == SSH_TUNID_ERR) {
442 errstr = "invalid tun device";
447 * Skip the comma, and move to the next option
448 * (or break out if there are no more).
450 if (*opts == '\0' || *opts == ' ' || *opts == '\t')
451 break; /* End of options. */
452 /* Anything other than a comma is an unknown option */
454 errstr = "unknown key option";
459 errstr = "unexpected end-of-options";
470 errstr = "memory allocation failed";
472 sshauthopt_free(ret);
479 sshauthopt_from_cert(struct sshkey *k)
481 struct sshauthopt *ret;
483 if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
484 k->cert->type != SSH2_CERT_TYPE_USER)
487 if ((ret = sshauthopt_new()) == NULL)
490 /* Handle options and critical extensions separately */
491 if (cert_option_list(ret, k->cert->critical,
492 OPTIONS_CRITICAL, 1) == -1) {
493 sshauthopt_free(ret);
496 if (cert_option_list(ret, k->cert->extensions,
497 OPTIONS_EXTENSIONS, 0) == -1) {
498 sshauthopt_free(ret);
506 * Merges "additional" options to "primary" and returns the result.
507 * NB. Some options from primary have primacy.
510 sshauthopt_merge(const struct sshauthopt *primary,
511 const struct sshauthopt *additional, const char **errstrp)
513 struct sshauthopt *ret;
514 const char *errstr = "internal error";
520 if ((ret = sshauthopt_new()) == NULL)
523 /* cert_authority and cert_principals are cleared in result */
525 /* Prefer access lists from primary. */
526 /* XXX err is both set and mismatch? */
527 tmp = primary->required_from_host_cert;
529 tmp = additional->required_from_host_cert;
530 if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
532 tmp = primary->required_from_host_keys;
534 tmp = additional->required_from_host_keys;
535 if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
539 * force_tun_device, permitopen/permitlisten and environment all
540 * prefer the primary.
542 ret->force_tun_device = primary->force_tun_device;
543 if (ret->force_tun_device == -1)
544 ret->force_tun_device = additional->force_tun_device;
545 if (primary->nenv > 0) {
546 if (dup_strings(&ret->env, &ret->nenv,
547 primary->env, primary->nenv) != 0)
549 } else if (additional->nenv) {
550 if (dup_strings(&ret->env, &ret->nenv,
551 additional->env, additional->nenv) != 0)
554 if (primary->npermitopen > 0) {
555 if (dup_strings(&ret->permitopen, &ret->npermitopen,
556 primary->permitopen, primary->npermitopen) != 0)
558 } else if (additional->npermitopen > 0) {
559 if (dup_strings(&ret->permitopen, &ret->npermitopen,
560 additional->permitopen, additional->npermitopen) != 0)
564 if (primary->npermitlisten > 0) {
565 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
566 primary->permitlisten, primary->npermitlisten) != 0)
568 } else if (additional->npermitlisten > 0) {
569 if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
570 additional->permitlisten, additional->npermitlisten) != 0)
574 #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
575 /* Permissive flags are logical-AND (i.e. must be set in both) */
576 OPTFLAG_AND(permit_port_forwarding_flag);
577 OPTFLAG_AND(permit_agent_forwarding_flag);
578 OPTFLAG_AND(permit_x11_forwarding_flag);
579 OPTFLAG_AND(permit_pty_flag);
580 OPTFLAG_AND(permit_user_rc);
581 OPTFLAG_AND(no_require_user_presence);
584 /* Earliest expiry time should win */
585 if (primary->valid_before != 0)
586 ret->valid_before = primary->valid_before;
587 if (additional->valid_before != 0 &&
588 additional->valid_before < ret->valid_before)
589 ret->valid_before = additional->valid_before;
592 * When both multiple forced-command are specified, only
593 * proceed if they are identical, otherwise fail.
595 if (primary->force_command != NULL &&
596 additional->force_command != NULL) {
597 if (strcmp(primary->force_command,
598 additional->force_command) == 0) {
600 ret->force_command = strdup(primary->force_command);
601 if (ret->force_command == NULL)
604 errstr = "forced command options do not match";
607 } else if (primary->force_command != NULL) {
608 if ((ret->force_command = strdup(
609 primary->force_command)) == NULL)
611 } else if (additional->force_command != NULL) {
612 if ((ret->force_command = strdup(
613 additional->force_command)) == NULL)
622 errstr = "memory allocation failed";
626 sshauthopt_free(ret);
634 sshauthopt_copy(const struct sshauthopt *orig)
636 struct sshauthopt *ret;
638 if ((ret = sshauthopt_new()) == NULL)
641 #define OPTSCALAR(x) ret->x = orig->x
642 OPTSCALAR(permit_port_forwarding_flag);
643 OPTSCALAR(permit_agent_forwarding_flag);
644 OPTSCALAR(permit_x11_forwarding_flag);
645 OPTSCALAR(permit_pty_flag);
646 OPTSCALAR(permit_user_rc);
647 OPTSCALAR(restricted);
648 OPTSCALAR(cert_authority);
649 OPTSCALAR(force_tun_device);
650 OPTSCALAR(valid_before);
651 OPTSCALAR(no_require_user_presence);
653 #define OPTSTRING(x) \
655 if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
656 sshauthopt_free(ret); \
660 OPTSTRING(cert_principals);
661 OPTSTRING(force_command);
662 OPTSTRING(required_from_host_cert);
663 OPTSTRING(required_from_host_keys);
666 if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
667 dup_strings(&ret->permitopen, &ret->npermitopen,
668 orig->permitopen, orig->npermitopen) != 0 ||
669 dup_strings(&ret->permitlisten, &ret->npermitlisten,
670 orig->permitlisten, orig->npermitlisten) != 0) {
671 sshauthopt_free(ret);
678 serialise_array(struct sshbuf *m, char **a, size_t n)
685 return SSH_ERR_INTERNAL_ERROR;
687 if ((b = sshbuf_new()) == NULL) {
688 return SSH_ERR_ALLOC_FAIL;
690 for (i = 0; i < n; i++) {
691 if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
696 if ((r = sshbuf_put_u32(m, n)) != 0 ||
697 (r = sshbuf_put_stringb(m, b)) != 0) {
706 deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
710 struct sshbuf *b = NULL;
712 int r = SSH_ERR_INTERNAL_ERROR;
714 if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
715 (r = sshbuf_froms(m, &b)) != 0)
718 r = SSH_ERR_INVALID_FORMAT;
722 if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
723 r = SSH_ERR_ALLOC_FAIL;
726 for (i = 0; i < n; i++) {
727 if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
738 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;
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_u8(m, opts->no_require_user_presence)) != 0)
787 /* Simple integer options */
788 if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
791 /* tunnel number can be negative to indicate "unset" */
792 if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
793 (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
794 0 : (u_int)opts->force_tun_device)) != 0)
797 /* String options; these may be NULL */
798 if ((r = serialise_nullable_string(m,
799 untrusted ? "yes" : opts->cert_principals)) != 0 ||
800 (r = serialise_nullable_string(m,
801 untrusted ? "true" : opts->force_command)) != 0 ||
802 (r = serialise_nullable_string(m,
803 untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
804 (r = serialise_nullable_string(m,
805 untrusted ? NULL : opts->required_from_host_keys)) != 0)
809 if ((r = serialise_array(m, opts->env,
810 untrusted ? 0 : opts->nenv)) != 0 ||
811 (r = serialise_array(m, opts->permitopen,
812 untrusted ? 0 : opts->npermitopen)) != 0 ||
813 (r = serialise_array(m, opts->permitlisten,
814 untrusted ? 0 : opts->npermitlisten)) != 0)
822 sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
824 struct sshauthopt *opts = NULL;
825 int r = SSH_ERR_INTERNAL_ERROR;
829 if ((opts = calloc(1, sizeof(*opts))) == NULL)
830 return SSH_ERR_ALLOC_FAIL;
833 #define OPT_FLAG(x) \
835 if ((r = sshbuf_get_u8(m, &f)) != 0) \
839 OPT_FLAG(permit_port_forwarding_flag);
840 OPT_FLAG(permit_agent_forwarding_flag);
841 OPT_FLAG(permit_x11_forwarding_flag);
842 OPT_FLAG(permit_pty_flag);
843 OPT_FLAG(permit_user_rc);
844 OPT_FLAG(restricted);
845 OPT_FLAG(cert_authority);
846 OPT_FLAG(no_require_user_presence);
849 /* Simple integer options */
850 if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
853 /* tunnel number can be negative to indicate "unset" */
854 if ((r = sshbuf_get_u8(m, &f)) != 0 ||
855 (r = sshbuf_get_u32(m, &tmp)) != 0)
857 opts->force_tun_device = f ? -1 : (int)tmp;
859 /* String options may be NULL */
860 if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
861 (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
862 (r = deserialise_nullable_string(m,
863 &opts->required_from_host_cert)) != 0 ||
864 (r = deserialise_nullable_string(m,
865 &opts->required_from_host_keys)) != 0)
869 if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
870 (r = deserialise_array(m,
871 &opts->permitopen, &opts->npermitopen)) != 0 ||
872 (r = deserialise_array(m,
873 &opts->permitlisten, &opts->npermitlisten)) != 0)
881 sshauthopt_free(opts);