Annotation of src/usr.bin/ssh/auth-options.c, Revision 1.97
1.97 ! djm 1: /* $OpenBSD: auth-options.c,v 1.96 2021/07/23 03:57:20 djm Exp $ */
1.75 djm 2: /*
3: * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
4: *
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.
8: *
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.
16: */
1.3 deraadt 17:
1.36 stevesk 18: #include <sys/types.h>
1.42 djm 19: #include <sys/queue.h>
1.36 stevesk 20:
1.88 djm 21: #include <stdlib.h>
1.37 stevesk 22: #include <netdb.h>
1.36 stevesk 23: #include <pwd.h>
1.39 stevesk 24: #include <string.h>
1.40 deraadt 25: #include <stdio.h>
26: #include <stdarg.h>
1.75 djm 27: #include <ctype.h>
28: #include <limits.h>
1.1 markus 29:
30: #include "xmalloc.h"
1.65 markus 31: #include "ssherr.h"
1.11 markus 32: #include "log.h"
1.65 markus 33: #include "sshbuf.h"
1.64 millert 34: #include "misc.h"
1.65 markus 35: #include "sshkey.h"
1.76 djm 36: #include "match.h"
37: #include "ssh2.h"
1.50 djm 38: #include "auth-options.h"
1.75 djm 39:
40: static int
41: dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
42: {
43: char **dst;
44: size_t i, j;
45:
46: *dstp = NULL;
47: *ndstp = 0;
48: if (nsrc == 0)
49: return 0;
50:
51: if ((dst = calloc(nsrc, sizeof(*src))) == NULL)
52: return -1;
53: for (i = 0; i < nsrc; i++) {
54: if ((dst[i] = strdup(src[i])) == NULL) {
55: for (j = 0; j < i; j++)
56: free(dst[j]);
57: free(dst);
58: return -1;
59: }
60: }
61: /* success */
62: *dstp = dst;
63: *ndstp = nsrc;
64: return 0;
65: }
66:
67: #define OPTIONS_CRITICAL 1
68: #define OPTIONS_EXTENSIONS 2
69: static int
70: cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
71: u_int which, int crit)
72: {
73: char *command, *allowed;
74: char *name = NULL;
75: struct sshbuf *c = NULL, *data = NULL;
76: int r, ret = -1, found;
77:
78: if ((c = sshbuf_fromb(oblob)) == NULL) {
1.94 djm 79: error_f("sshbuf_fromb failed");
1.75 djm 80: goto out;
81: }
82:
83: while (sshbuf_len(c) > 0) {
84: sshbuf_free(data);
85: data = NULL;
86: if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
87: (r = sshbuf_froms(c, &data)) != 0) {
1.94 djm 88: error_r(r, "Unable to parse certificate options");
1.75 djm 89: goto out;
90: }
91: debug3("found certificate option \"%.100s\" len %zu",
92: name, sshbuf_len(data));
93: found = 0;
94: if ((which & OPTIONS_EXTENSIONS) != 0) {
1.90 djm 95: if (strcmp(name, "no-touch-required") == 0) {
96: opts->no_require_user_presence = 1;
97: found = 1;
98: } else if (strcmp(name, "permit-X11-forwarding") == 0) {
1.75 djm 99: opts->permit_x11_forwarding_flag = 1;
100: found = 1;
101: } else if (strcmp(name,
102: "permit-agent-forwarding") == 0) {
103: opts->permit_agent_forwarding_flag = 1;
104: found = 1;
105: } else if (strcmp(name,
106: "permit-port-forwarding") == 0) {
107: opts->permit_port_forwarding_flag = 1;
108: found = 1;
109: } else if (strcmp(name, "permit-pty") == 0) {
110: opts->permit_pty_flag = 1;
111: found = 1;
112: } else if (strcmp(name, "permit-user-rc") == 0) {
113: opts->permit_user_rc = 1;
114: found = 1;
115: }
116: }
117: if (!found && (which & OPTIONS_CRITICAL) != 0) {
1.93 djm 118: if (strcmp(name, "verify-required") == 0) {
119: opts->require_verify = 1;
120: found = 1;
121: } else if (strcmp(name, "force-command") == 0) {
1.75 djm 122: if ((r = sshbuf_get_cstring(data, &command,
123: NULL)) != 0) {
1.94 djm 124: error_r(r, "Unable to parse \"%s\" "
125: "section", name);
1.75 djm 126: goto out;
127: }
128: if (opts->force_command != NULL) {
129: error("Certificate has multiple "
130: "force-command options");
131: free(command);
132: goto out;
133: }
134: opts->force_command = command;
135: found = 1;
1.93 djm 136: } else if (strcmp(name, "source-address") == 0) {
1.75 djm 137: if ((r = sshbuf_get_cstring(data, &allowed,
138: NULL)) != 0) {
1.94 djm 139: error_r(r, "Unable to parse \"%s\" "
140: "section", name);
1.75 djm 141: goto out;
142: }
143: if (opts->required_from_host_cert != NULL) {
144: error("Certificate has multiple "
145: "source-address options");
146: free(allowed);
147: goto out;
148: }
149: /* Check syntax */
150: if (addr_match_cidr_list(NULL, allowed) == -1) {
151: error("Certificate source-address "
152: "contents invalid");
153: goto out;
154: }
155: opts->required_from_host_cert = allowed;
156: found = 1;
157: }
158: }
159:
160: if (!found) {
161: if (crit) {
162: error("Certificate critical option \"%s\" "
163: "is not supported", name);
164: goto out;
165: } else {
166: logit("Certificate extension \"%s\" "
167: "is not supported", name);
168: }
169: } else if (sshbuf_len(data) != 0) {
170: error("Certificate option \"%s\" corrupt "
171: "(extra data)", name);
172: goto out;
173: }
174: free(name);
175: name = NULL;
176: }
177: /* successfully parsed all options */
178: ret = 0;
179:
180: out:
181: free(name);
182: sshbuf_free(data);
183: sshbuf_free(c);
184: return ret;
185: }
186:
187: struct sshauthopt *
188: sshauthopt_new(void)
189: {
190: struct sshauthopt *ret;
191:
192: if ((ret = calloc(1, sizeof(*ret))) == NULL)
193: return NULL;
194: ret->force_tun_device = -1;
195: return ret;
196: }
197:
198: void
199: sshauthopt_free(struct sshauthopt *opts)
200: {
201: size_t i;
202:
203: if (opts == NULL)
204: return;
205:
206: free(opts->cert_principals);
207: free(opts->force_command);
208: free(opts->required_from_host_cert);
209: free(opts->required_from_host_keys);
210:
211: for (i = 0; i < opts->nenv; i++)
212: free(opts->env[i]);
213: free(opts->env);
214:
215: for (i = 0; i < opts->npermitopen; i++)
216: free(opts->permitopen[i]);
217: free(opts->permitopen);
218:
1.80 djm 219: for (i = 0; i < opts->npermitlisten; i++)
220: free(opts->permitlisten[i]);
221: free(opts->permitlisten);
222:
1.91 jsg 223: freezero(opts, sizeof(*opts));
1.75 djm 224: }
225:
226: struct sshauthopt *
227: sshauthopt_new_with_keys_defaults(void)
228: {
229: struct sshauthopt *ret = NULL;
230:
231: if ((ret = sshauthopt_new()) == NULL)
232: return NULL;
233:
234: /* Defaults for authorized_keys flags */
235: ret->permit_port_forwarding_flag = 1;
236: ret->permit_agent_forwarding_flag = 1;
237: ret->permit_x11_forwarding_flag = 1;
238: ret->permit_pty_flag = 1;
239: ret->permit_user_rc = 1;
240: return ret;
241: }
242:
1.80 djm 243: /*
244: * Parse and record a permitopen/permitlisten directive.
245: * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
246: */
247: static int
1.83 djm 248: handle_permit(const char **optsp, int allow_bare_port,
249: char ***permitsp, size_t *npermitsp, const char **errstrp)
1.80 djm 250: {
251: char *opt, *tmp, *cp, *host, **permits = *permitsp;
252: size_t npermits = *npermitsp;
253: const char *errstr = "unknown error";
254:
1.86 djm 255: if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
1.80 djm 256: *errstrp = "too many permission directives";
257: return -1;
258: }
1.82 djm 259: if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
1.80 djm 260: return -1;
261: }
1.83 djm 262: if (allow_bare_port && strchr(opt, ':') == NULL) {
263: /*
264: * Allow a bare port number in permitlisten to indicate a
265: * listen_host wildcard.
266: */
1.85 deraadt 267: if (asprintf(&tmp, "*:%s", opt) == -1) {
1.89 dtucker 268: free(opt);
1.83 djm 269: *errstrp = "memory allocation failed";
270: return -1;
271: }
272: free(opt);
273: opt = tmp;
274: }
1.80 djm 275: if ((tmp = strdup(opt)) == NULL) {
276: free(opt);
277: *errstrp = "memory allocation failed";
278: return -1;
279: }
280: cp = tmp;
281: /* validate syntax before recording it. */
282: host = hpdelim(&cp);
283: if (host == NULL || strlen(host) >= NI_MAXHOST) {
284: free(tmp);
285: free(opt);
286: *errstrp = "invalid permission hostname";
287: return -1;
288: }
289: /*
290: * don't want to use permitopen_port to avoid
291: * dependency on channels.[ch] here.
292: */
293: if (cp == NULL ||
294: (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
295: free(tmp);
296: free(opt);
297: *errstrp = "invalid permission port";
298: return -1;
299: }
300: /* XXX - add streamlocal support */
301: free(tmp);
302: /* Record it */
303: if ((permits = recallocarray(permits, npermits, npermits + 1,
304: sizeof(*permits))) == NULL) {
305: free(opt);
306: /* NB. don't update *permitsp if alloc fails */
307: *errstrp = "memory allocation failed";
308: return -1;
309: }
310: permits[npermits++] = opt;
311: *permitsp = permits;
312: *npermitsp = npermits;
313: return 0;
314: }
315:
1.75 djm 316: struct sshauthopt *
317: sshauthopt_parse(const char *opts, const char **errstrp)
318: {
1.80 djm 319: char **oarray, *opt, *cp, *tmp;
1.75 djm 320: int r;
321: struct sshauthopt *ret = NULL;
322: const char *errstr = "unknown error";
1.77 djm 323: uint64_t valid_before;
1.96 djm 324: size_t i, l;
1.75 djm 325:
326: if (errstrp != NULL)
327: *errstrp = NULL;
328: if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
329: goto alloc_fail;
330:
331: if (opts == NULL)
332: return ret;
333:
334: while (*opts && *opts != ' ' && *opts != '\t') {
335: /* flag options */
336: if ((r = opt_flag("restrict", 0, &opts)) != -1) {
337: ret->restricted = 1;
338: ret->permit_port_forwarding_flag = 0;
339: ret->permit_agent_forwarding_flag = 0;
340: ret->permit_x11_forwarding_flag = 0;
341: ret->permit_pty_flag = 0;
342: ret->permit_user_rc = 0;
343: } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
344: ret->cert_authority = r;
345: } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
346: ret->permit_port_forwarding_flag = r == 1;
347: } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
348: ret->permit_agent_forwarding_flag = r == 1;
349: } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
350: ret->permit_x11_forwarding_flag = r == 1;
1.90 djm 351: } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
352: ret->no_require_user_presence = r != 1; /* NB. flip */
1.93 djm 353: } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
354: ret->require_verify = r == 1;
1.75 djm 355: } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
356: ret->permit_pty_flag = r == 1;
357: } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
358: ret->permit_user_rc = r == 1;
359: } else if (opt_match(&opts, "command")) {
360: if (ret->force_command != NULL) {
361: errstr = "multiple \"command\" clauses";
362: goto fail;
363: }
364: ret->force_command = opt_dequote(&opts, &errstr);
365: if (ret->force_command == NULL)
366: goto fail;
367: } else if (opt_match(&opts, "principals")) {
368: if (ret->cert_principals != NULL) {
369: errstr = "multiple \"principals\" clauses";
370: goto fail;
371: }
372: ret->cert_principals = opt_dequote(&opts, &errstr);
373: if (ret->cert_principals == NULL)
374: goto fail;
375: } else if (opt_match(&opts, "from")) {
376: if (ret->required_from_host_keys != NULL) {
377: errstr = "multiple \"from\" clauses";
378: goto fail;
379: }
380: ret->required_from_host_keys = opt_dequote(&opts,
381: &errstr);
382: if (ret->required_from_host_keys == NULL)
383: goto fail;
1.78 djm 384: } else if (opt_match(&opts, "expiry-time")) {
1.77 djm 385: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
386: goto fail;
387: if (parse_absolute_time(opt, &valid_before) != 0 ||
388: valid_before == 0) {
389: free(opt);
390: errstr = "invalid expires time";
391: goto fail;
392: }
393: free(opt);
394: if (ret->valid_before == 0 ||
395: valid_before < ret->valid_before)
396: ret->valid_before = valid_before;
1.75 djm 397: } else if (opt_match(&opts, "environment")) {
1.96 djm 398: if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
1.75 djm 399: errstr = "too many environment strings";
400: goto fail;
401: }
402: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
403: goto fail;
404: /* env name must be alphanumeric and followed by '=' */
405: if ((tmp = strchr(opt, '=')) == NULL) {
406: free(opt);
407: errstr = "invalid environment string";
408: goto fail;
409: }
1.97 ! djm 410: if ((cp = strdup(opt)) == NULL) {
! 411: free(opt);
1.84 djm 412: goto alloc_fail;
1.97 ! djm 413: }
1.96 djm 414: l = (size_t)(tmp - opt);
415: cp[l] = '\0'; /* truncate at '=' */
1.84 djm 416: if (!valid_env_name(cp)) {
417: free(cp);
418: free(opt);
419: errstr = "invalid environment string";
420: goto fail;
1.75 djm 421: }
1.96 djm 422: /* Check for duplicates; XXX O(n*log(n)) */
423: for (i = 0; i < ret->nenv; i++) {
424: if (strncmp(ret->env[i], cp, l) == 0 &&
425: ret->env[i][l] == '=')
426: break;
427: }
1.84 djm 428: free(cp);
1.96 djm 429: /* First match wins */
430: if (i >= ret->nenv) {
431: /* Append it. */
432: oarray = ret->env;
433: if ((ret->env = recallocarray(ret->env,
434: ret->nenv, ret->nenv + 1,
435: sizeof(*ret->env))) == NULL) {
436: free(opt);
437: /* put it back for cleanup */
438: ret->env = oarray;
439: goto alloc_fail;
440: }
441: ret->env[ret->nenv++] = opt;
1.97 ! djm 442: opt = NULL; /* transferred */
1.75 djm 443: }
1.97 ! djm 444: free(opt);
1.75 djm 445: } else if (opt_match(&opts, "permitopen")) {
1.83 djm 446: if (handle_permit(&opts, 0, &ret->permitopen,
1.80 djm 447: &ret->npermitopen, &errstr) != 0)
1.75 djm 448: goto fail;
1.80 djm 449: } else if (opt_match(&opts, "permitlisten")) {
1.83 djm 450: if (handle_permit(&opts, 1, &ret->permitlisten,
1.80 djm 451: &ret->npermitlisten, &errstr) != 0)
1.75 djm 452: goto fail;
453: } else if (opt_match(&opts, "tunnel")) {
454: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
455: goto fail;
456: ret->force_tun_device = a2tun(opt, NULL);
457: free(opt);
458: if (ret->force_tun_device == SSH_TUNID_ERR) {
459: errstr = "invalid tun device";
460: goto fail;
461: }
462: }
463: /*
464: * Skip the comma, and move to the next option
465: * (or break out if there are no more).
466: */
467: if (*opts == '\0' || *opts == ' ' || *opts == '\t')
468: break; /* End of options. */
469: /* Anything other than a comma is an unknown option */
470: if (*opts != ',') {
471: errstr = "unknown key option";
472: goto fail;
473: }
474: opts++;
475: if (*opts == '\0') {
476: errstr = "unexpected end-of-options";
477: goto fail;
478: }
479: }
480:
481: /* success */
482: if (errstrp != NULL)
483: *errstrp = NULL;
484: return ret;
485:
486: alloc_fail:
487: errstr = "memory allocation failed";
488: fail:
489: sshauthopt_free(ret);
490: if (errstrp != NULL)
491: *errstrp = errstr;
492: return NULL;
493: }
494:
495: struct sshauthopt *
496: sshauthopt_from_cert(struct sshkey *k)
497: {
498: struct sshauthopt *ret;
499:
500: if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
501: k->cert->type != SSH2_CERT_TYPE_USER)
502: return NULL;
503:
504: if ((ret = sshauthopt_new()) == NULL)
505: return NULL;
506:
507: /* Handle options and critical extensions separately */
508: if (cert_option_list(ret, k->cert->critical,
509: OPTIONS_CRITICAL, 1) == -1) {
510: sshauthopt_free(ret);
511: return NULL;
512: }
513: if (cert_option_list(ret, k->cert->extensions,
514: OPTIONS_EXTENSIONS, 0) == -1) {
515: sshauthopt_free(ret);
516: return NULL;
517: }
518: /* success */
519: return ret;
520: }
521:
522: /*
523: * Merges "additional" options to "primary" and returns the result.
524: * NB. Some options from primary have primacy.
525: */
526: struct sshauthopt *
527: sshauthopt_merge(const struct sshauthopt *primary,
528: const struct sshauthopt *additional, const char **errstrp)
529: {
530: struct sshauthopt *ret;
531: const char *errstr = "internal error";
532: const char *tmp;
533:
534: if (errstrp != NULL)
535: *errstrp = NULL;
536:
537: if ((ret = sshauthopt_new()) == NULL)
538: goto alloc_fail;
539:
540: /* cert_authority and cert_principals are cleared in result */
541:
542: /* Prefer access lists from primary. */
543: /* XXX err is both set and mismatch? */
544: tmp = primary->required_from_host_cert;
545: if (tmp == NULL)
546: tmp = additional->required_from_host_cert;
547: if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
548: goto alloc_fail;
549: tmp = primary->required_from_host_keys;
550: if (tmp == NULL)
551: tmp = additional->required_from_host_keys;
552: if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
553: goto alloc_fail;
554:
1.80 djm 555: /*
556: * force_tun_device, permitopen/permitlisten and environment all
557: * prefer the primary.
558: */
1.75 djm 559: ret->force_tun_device = primary->force_tun_device;
560: if (ret->force_tun_device == -1)
561: ret->force_tun_device = additional->force_tun_device;
562: if (primary->nenv > 0) {
563: if (dup_strings(&ret->env, &ret->nenv,
564: primary->env, primary->nenv) != 0)
565: goto alloc_fail;
566: } else if (additional->nenv) {
567: if (dup_strings(&ret->env, &ret->nenv,
568: additional->env, additional->nenv) != 0)
569: goto alloc_fail;
570: }
571: if (primary->npermitopen > 0) {
572: if (dup_strings(&ret->permitopen, &ret->npermitopen,
573: primary->permitopen, primary->npermitopen) != 0)
574: goto alloc_fail;
575: } else if (additional->npermitopen > 0) {
576: if (dup_strings(&ret->permitopen, &ret->npermitopen,
577: additional->permitopen, additional->npermitopen) != 0)
578: goto alloc_fail;
579: }
580:
1.80 djm 581: if (primary->npermitlisten > 0) {
582: if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
583: primary->permitlisten, primary->npermitlisten) != 0)
584: goto alloc_fail;
585: } else if (additional->npermitlisten > 0) {
586: if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
587: additional->permitlisten, additional->npermitlisten) != 0)
588: goto alloc_fail;
589: }
590:
1.90 djm 591: #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
1.93 djm 592: #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
1.90 djm 593: /* Permissive flags are logical-AND (i.e. must be set in both) */
594: OPTFLAG_AND(permit_port_forwarding_flag);
595: OPTFLAG_AND(permit_agent_forwarding_flag);
596: OPTFLAG_AND(permit_x11_forwarding_flag);
597: OPTFLAG_AND(permit_pty_flag);
598: OPTFLAG_AND(permit_user_rc);
599: OPTFLAG_AND(no_require_user_presence);
1.93 djm 600: /* Restrictive flags are logical-OR (i.e. must be set in either) */
601: OPTFLAG_OR(require_verify);
1.90 djm 602: #undef OPTFLAG_AND
1.75 djm 603:
1.77 djm 604: /* Earliest expiry time should win */
605: if (primary->valid_before != 0)
606: ret->valid_before = primary->valid_before;
607: if (additional->valid_before != 0 &&
608: additional->valid_before < ret->valid_before)
609: ret->valid_before = additional->valid_before;
610:
1.75 djm 611: /*
612: * When both multiple forced-command are specified, only
613: * proceed if they are identical, otherwise fail.
614: */
615: if (primary->force_command != NULL &&
616: additional->force_command != NULL) {
617: if (strcmp(primary->force_command,
618: additional->force_command) == 0) {
619: /* ok */
620: ret->force_command = strdup(primary->force_command);
621: if (ret->force_command == NULL)
622: goto alloc_fail;
623: } else {
624: errstr = "forced command options do not match";
625: goto fail;
626: }
627: } else if (primary->force_command != NULL) {
628: if ((ret->force_command = strdup(
629: primary->force_command)) == NULL)
630: goto alloc_fail;
631: } else if (additional->force_command != NULL) {
632: if ((ret->force_command = strdup(
633: additional->force_command)) == NULL)
634: goto alloc_fail;
635: }
636: /* success */
637: if (errstrp != NULL)
638: *errstrp = NULL;
639: return ret;
640:
641: alloc_fail:
642: errstr = "memory allocation failed";
643: fail:
644: if (errstrp != NULL)
645: *errstrp = errstr;
646: sshauthopt_free(ret);
647: return NULL;
648: }
649:
650: /*
651: * Copy options
652: */
653: struct sshauthopt *
654: sshauthopt_copy(const struct sshauthopt *orig)
655: {
656: struct sshauthopt *ret;
657:
658: if ((ret = sshauthopt_new()) == NULL)
659: return NULL;
660:
661: #define OPTSCALAR(x) ret->x = orig->x
662: OPTSCALAR(permit_port_forwarding_flag);
663: OPTSCALAR(permit_agent_forwarding_flag);
664: OPTSCALAR(permit_x11_forwarding_flag);
665: OPTSCALAR(permit_pty_flag);
666: OPTSCALAR(permit_user_rc);
667: OPTSCALAR(restricted);
668: OPTSCALAR(cert_authority);
669: OPTSCALAR(force_tun_device);
1.77 djm 670: OPTSCALAR(valid_before);
1.90 djm 671: OPTSCALAR(no_require_user_presence);
1.93 djm 672: OPTSCALAR(require_verify);
1.75 djm 673: #undef OPTSCALAR
674: #define OPTSTRING(x) \
675: do { \
676: if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
677: sshauthopt_free(ret); \
678: return NULL; \
679: } \
680: } while (0)
681: OPTSTRING(cert_principals);
682: OPTSTRING(force_command);
683: OPTSTRING(required_from_host_cert);
684: OPTSTRING(required_from_host_keys);
685: #undef OPTSTRING
686:
687: if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
688: dup_strings(&ret->permitopen, &ret->npermitopen,
1.80 djm 689: orig->permitopen, orig->npermitopen) != 0 ||
690: dup_strings(&ret->permitlisten, &ret->npermitlisten,
691: orig->permitlisten, orig->npermitlisten) != 0) {
1.75 djm 692: sshauthopt_free(ret);
693: return NULL;
694: }
695: return ret;
696: }
697:
698: static int
699: serialise_array(struct sshbuf *m, char **a, size_t n)
700: {
701: struct sshbuf *b;
702: size_t i;
703: int r;
704:
705: if (n > INT_MAX)
706: return SSH_ERR_INTERNAL_ERROR;
707:
708: if ((b = sshbuf_new()) == NULL) {
709: return SSH_ERR_ALLOC_FAIL;
710: }
711: for (i = 0; i < n; i++) {
712: if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
713: sshbuf_free(b);
714: return r;
715: }
716: }
717: if ((r = sshbuf_put_u32(m, n)) != 0 ||
718: (r = sshbuf_put_stringb(m, b)) != 0) {
719: sshbuf_free(b);
720: return r;
721: }
722: /* success */
723: return 0;
724: }
725:
726: static int
727: deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
728: {
729: char **a = NULL;
730: size_t i, n = 0;
731: struct sshbuf *b = NULL;
732: u_int tmp;
733: int r = SSH_ERR_INTERNAL_ERROR;
734:
735: if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
736: (r = sshbuf_froms(m, &b)) != 0)
737: goto out;
738: if (tmp > INT_MAX) {
739: r = SSH_ERR_INVALID_FORMAT;
740: goto out;
741: }
742: n = tmp;
743: if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
744: r = SSH_ERR_ALLOC_FAIL;
745: goto out;
746: }
747: for (i = 0; i < n; i++) {
748: if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
749: goto out;
750: }
751: /* success */
752: r = 0;
753: *ap = a;
754: a = NULL;
755: *np = n;
756: n = 0;
757: out:
1.92 markus 758: if (a != NULL) {
759: for (i = 0; i < n; i++)
760: free(a[i]);
761: free(a);
762: }
1.75 djm 763: sshbuf_free(b);
764: return r;
765: }
766:
767: static int
768: serialise_nullable_string(struct sshbuf *m, const char *s)
769: {
770: int r;
771:
772: if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
773: (r = sshbuf_put_cstring(m, s)) != 0)
774: return r;
775: return 0;
776: }
777:
778: static int
779: deserialise_nullable_string(struct sshbuf *m, char **sp)
780: {
781: int r;
782: u_char flag;
783:
784: *sp = NULL;
785: if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
786: (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
787: return r;
788: return 0;
789: }
790:
791: int
792: sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
793: int untrusted)
794: {
795: int r = SSH_ERR_INTERNAL_ERROR;
796:
1.90 djm 797: /* Flag options */
1.75 djm 798: if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
799: (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
800: (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
801: (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
802: (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
803: (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
1.77 djm 804: (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
1.93 djm 805: (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
806: (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
1.90 djm 807: return r;
808:
809: /* Simple integer options */
810: if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
1.75 djm 811: return r;
812:
813: /* tunnel number can be negative to indicate "unset" */
814: if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
815: (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
816: 0 : (u_int)opts->force_tun_device)) != 0)
817: return r;
818:
819: /* String options; these may be NULL */
820: if ((r = serialise_nullable_string(m,
821: untrusted ? "yes" : opts->cert_principals)) != 0 ||
822: (r = serialise_nullable_string(m,
823: untrusted ? "true" : opts->force_command)) != 0 ||
824: (r = serialise_nullable_string(m,
825: untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
826: (r = serialise_nullable_string(m,
1.95 djm 827: untrusted ? NULL : opts->required_from_host_keys)) != 0)
1.75 djm 828: return r;
829:
830: /* Array options */
831: if ((r = serialise_array(m, opts->env,
832: untrusted ? 0 : opts->nenv)) != 0 ||
833: (r = serialise_array(m, opts->permitopen,
1.82 djm 834: untrusted ? 0 : opts->npermitopen)) != 0 ||
1.80 djm 835: (r = serialise_array(m, opts->permitlisten,
836: untrusted ? 0 : opts->npermitlisten)) != 0)
1.75 djm 837: return r;
838:
839: /* success */
840: return 0;
841: }
842:
843: int
844: sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
845: {
846: struct sshauthopt *opts = NULL;
847: int r = SSH_ERR_INTERNAL_ERROR;
848: u_char f;
849: u_int tmp;
850:
851: if ((opts = calloc(1, sizeof(*opts))) == NULL)
852: return SSH_ERR_ALLOC_FAIL;
853:
1.90 djm 854: /* Flag options */
1.75 djm 855: #define OPT_FLAG(x) \
856: do { \
857: if ((r = sshbuf_get_u8(m, &f)) != 0) \
858: goto out; \
859: opts->x = f; \
860: } while (0)
861: OPT_FLAG(permit_port_forwarding_flag);
862: OPT_FLAG(permit_agent_forwarding_flag);
863: OPT_FLAG(permit_x11_forwarding_flag);
864: OPT_FLAG(permit_pty_flag);
865: OPT_FLAG(permit_user_rc);
866: OPT_FLAG(restricted);
867: OPT_FLAG(cert_authority);
1.90 djm 868: OPT_FLAG(no_require_user_presence);
1.93 djm 869: OPT_FLAG(require_verify);
1.75 djm 870: #undef OPT_FLAG
1.77 djm 871:
1.90 djm 872: /* Simple integer options */
1.77 djm 873: if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
874: goto out;
1.75 djm 875:
876: /* tunnel number can be negative to indicate "unset" */
877: if ((r = sshbuf_get_u8(m, &f)) != 0 ||
878: (r = sshbuf_get_u32(m, &tmp)) != 0)
879: goto out;
880: opts->force_tun_device = f ? -1 : (int)tmp;
881:
882: /* String options may be NULL */
883: if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
884: (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
885: (r = deserialise_nullable_string(m,
886: &opts->required_from_host_cert)) != 0 ||
887: (r = deserialise_nullable_string(m,
888: &opts->required_from_host_keys)) != 0)
889: goto out;
890:
891: /* Array options */
892: if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
893: (r = deserialise_array(m,
1.80 djm 894: &opts->permitopen, &opts->npermitopen)) != 0 ||
895: (r = deserialise_array(m,
896: &opts->permitlisten, &opts->npermitlisten)) != 0)
1.75 djm 897: goto out;
898:
899: /* success */
900: r = 0;
901: *optsp = opts;
902: opts = NULL;
903: out:
904: sshauthopt_free(opts);
905: return r;
906: }