Annotation of src/usr.bin/ssh/auth-options.c, Revision 1.90
1.90 ! djm 1: /* $OpenBSD: auth-options.c,v 1.89 2019/09/13 04:36:43 dtucker 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) {
79: error("%s: sshbuf_fromb failed", __func__);
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) {
88: error("Unable to parse certificate options: %s",
89: ssh_err(r));
90: goto out;
91: }
92: debug3("found certificate option \"%.100s\" len %zu",
93: name, sshbuf_len(data));
94: found = 0;
95: if ((which & OPTIONS_EXTENSIONS) != 0) {
1.90 ! djm 96: if (strcmp(name, "no-touch-required") == 0) {
! 97: opts->no_require_user_presence = 1;
! 98: found = 1;
! 99: } else if (strcmp(name, "permit-X11-forwarding") == 0) {
1.75 djm 100: opts->permit_x11_forwarding_flag = 1;
101: found = 1;
102: } else if (strcmp(name,
103: "permit-agent-forwarding") == 0) {
104: opts->permit_agent_forwarding_flag = 1;
105: found = 1;
106: } else if (strcmp(name,
107: "permit-port-forwarding") == 0) {
108: opts->permit_port_forwarding_flag = 1;
109: found = 1;
110: } else if (strcmp(name, "permit-pty") == 0) {
111: opts->permit_pty_flag = 1;
112: found = 1;
113: } else if (strcmp(name, "permit-user-rc") == 0) {
114: opts->permit_user_rc = 1;
115: found = 1;
116: }
117: }
118: if (!found && (which & OPTIONS_CRITICAL) != 0) {
119: if (strcmp(name, "force-command") == 0) {
120: if ((r = sshbuf_get_cstring(data, &command,
121: NULL)) != 0) {
122: error("Unable to parse \"%s\" "
123: "section: %s", name, ssh_err(r));
124: goto out;
125: }
126: if (opts->force_command != NULL) {
127: error("Certificate has multiple "
128: "force-command options");
129: free(command);
130: goto out;
131: }
132: opts->force_command = command;
133: found = 1;
134: }
135: if (strcmp(name, "source-address") == 0) {
136: if ((r = sshbuf_get_cstring(data, &allowed,
137: NULL)) != 0) {
138: error("Unable to parse \"%s\" "
139: "section: %s", name, ssh_err(r));
140: goto out;
141: }
142: if (opts->required_from_host_cert != NULL) {
143: error("Certificate has multiple "
144: "source-address options");
145: free(allowed);
146: goto out;
147: }
148: /* Check syntax */
149: if (addr_match_cidr_list(NULL, allowed) == -1) {
150: error("Certificate source-address "
151: "contents invalid");
152: goto out;
153: }
154: opts->required_from_host_cert = allowed;
155: found = 1;
156: }
157: }
158:
159: if (!found) {
160: if (crit) {
161: error("Certificate critical option \"%s\" "
162: "is not supported", name);
163: goto out;
164: } else {
165: logit("Certificate extension \"%s\" "
166: "is not supported", name);
167: }
168: } else if (sshbuf_len(data) != 0) {
169: error("Certificate option \"%s\" corrupt "
170: "(extra data)", name);
171: goto out;
172: }
173: free(name);
174: name = NULL;
175: }
176: /* successfully parsed all options */
177: ret = 0;
178:
179: out:
180: free(name);
181: sshbuf_free(data);
182: sshbuf_free(c);
183: return ret;
184: }
185:
186: struct sshauthopt *
187: sshauthopt_new(void)
188: {
189: struct sshauthopt *ret;
190:
191: if ((ret = calloc(1, sizeof(*ret))) == NULL)
192: return NULL;
193: ret->force_tun_device = -1;
194: return ret;
195: }
196:
197: void
198: sshauthopt_free(struct sshauthopt *opts)
199: {
200: size_t i;
201:
202: if (opts == NULL)
203: return;
204:
205: free(opts->cert_principals);
206: free(opts->force_command);
207: free(opts->required_from_host_cert);
208: free(opts->required_from_host_keys);
209:
210: for (i = 0; i < opts->nenv; i++)
211: free(opts->env[i]);
212: free(opts->env);
213:
214: for (i = 0; i < opts->npermitopen; i++)
215: free(opts->permitopen[i]);
216: free(opts->permitopen);
217:
1.80 djm 218: for (i = 0; i < opts->npermitlisten; i++)
219: free(opts->permitlisten[i]);
220: free(opts->permitlisten);
221:
1.75 djm 222: explicit_bzero(opts, sizeof(*opts));
223: free(opts);
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.75 djm 324:
325: if (errstrp != NULL)
326: *errstrp = NULL;
327: if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
328: goto alloc_fail;
329:
330: if (opts == NULL)
331: return ret;
332:
333: while (*opts && *opts != ' ' && *opts != '\t') {
334: /* flag options */
335: if ((r = opt_flag("restrict", 0, &opts)) != -1) {
336: ret->restricted = 1;
337: ret->permit_port_forwarding_flag = 0;
338: ret->permit_agent_forwarding_flag = 0;
339: ret->permit_x11_forwarding_flag = 0;
340: ret->permit_pty_flag = 0;
341: ret->permit_user_rc = 0;
342: } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
343: ret->cert_authority = r;
344: } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
345: ret->permit_port_forwarding_flag = r == 1;
346: } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
347: ret->permit_agent_forwarding_flag = r == 1;
348: } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
349: ret->permit_x11_forwarding_flag = r == 1;
1.90 ! djm 350: } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
! 351: ret->no_require_user_presence = r != 1; /* NB. flip */
1.75 djm 352: } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
353: ret->permit_pty_flag = r == 1;
354: } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
355: ret->permit_user_rc = r == 1;
356: } else if (opt_match(&opts, "command")) {
357: if (ret->force_command != NULL) {
358: errstr = "multiple \"command\" clauses";
359: goto fail;
360: }
361: ret->force_command = opt_dequote(&opts, &errstr);
362: if (ret->force_command == NULL)
363: goto fail;
364: } else if (opt_match(&opts, "principals")) {
365: if (ret->cert_principals != NULL) {
366: errstr = "multiple \"principals\" clauses";
367: goto fail;
368: }
369: ret->cert_principals = opt_dequote(&opts, &errstr);
370: if (ret->cert_principals == NULL)
371: goto fail;
372: } else if (opt_match(&opts, "from")) {
373: if (ret->required_from_host_keys != NULL) {
374: errstr = "multiple \"from\" clauses";
375: goto fail;
376: }
377: ret->required_from_host_keys = opt_dequote(&opts,
378: &errstr);
379: if (ret->required_from_host_keys == NULL)
380: goto fail;
1.78 djm 381: } else if (opt_match(&opts, "expiry-time")) {
1.77 djm 382: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
383: goto fail;
384: if (parse_absolute_time(opt, &valid_before) != 0 ||
385: valid_before == 0) {
386: free(opt);
387: errstr = "invalid expires time";
388: goto fail;
389: }
390: free(opt);
391: if (ret->valid_before == 0 ||
392: valid_before < ret->valid_before)
393: ret->valid_before = valid_before;
1.75 djm 394: } else if (opt_match(&opts, "environment")) {
395: if (ret->nenv > INT_MAX) {
396: errstr = "too many environment strings";
397: goto fail;
398: }
399: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
400: goto fail;
401: /* env name must be alphanumeric and followed by '=' */
402: if ((tmp = strchr(opt, '=')) == NULL) {
403: free(opt);
404: errstr = "invalid environment string";
405: goto fail;
406: }
1.84 djm 407: if ((cp = strdup(opt)) == NULL)
408: goto alloc_fail;
409: cp[tmp - opt] = '\0'; /* truncate at '=' */
410: if (!valid_env_name(cp)) {
411: free(cp);
412: free(opt);
413: errstr = "invalid environment string";
414: goto fail;
1.75 djm 415: }
1.84 djm 416: free(cp);
1.75 djm 417: /* Append it. */
418: oarray = ret->env;
419: if ((ret->env = recallocarray(ret->env, ret->nenv,
420: ret->nenv + 1, sizeof(*ret->env))) == NULL) {
421: free(opt);
422: ret->env = oarray; /* put it back for cleanup */
423: goto alloc_fail;
424: }
425: ret->env[ret->nenv++] = opt;
426: } else if (opt_match(&opts, "permitopen")) {
1.83 djm 427: if (handle_permit(&opts, 0, &ret->permitopen,
1.80 djm 428: &ret->npermitopen, &errstr) != 0)
1.75 djm 429: goto fail;
1.80 djm 430: } else if (opt_match(&opts, "permitlisten")) {
1.83 djm 431: if (handle_permit(&opts, 1, &ret->permitlisten,
1.80 djm 432: &ret->npermitlisten, &errstr) != 0)
1.75 djm 433: goto fail;
434: } else if (opt_match(&opts, "tunnel")) {
435: if ((opt = opt_dequote(&opts, &errstr)) == NULL)
436: goto fail;
437: ret->force_tun_device = a2tun(opt, NULL);
438: free(opt);
439: if (ret->force_tun_device == SSH_TUNID_ERR) {
440: errstr = "invalid tun device";
441: goto fail;
442: }
443: }
444: /*
445: * Skip the comma, and move to the next option
446: * (or break out if there are no more).
447: */
448: if (*opts == '\0' || *opts == ' ' || *opts == '\t')
449: break; /* End of options. */
450: /* Anything other than a comma is an unknown option */
451: if (*opts != ',') {
452: errstr = "unknown key option";
453: goto fail;
454: }
455: opts++;
456: if (*opts == '\0') {
457: errstr = "unexpected end-of-options";
458: goto fail;
459: }
460: }
461:
462: /* success */
463: if (errstrp != NULL)
464: *errstrp = NULL;
465: return ret;
466:
467: alloc_fail:
468: errstr = "memory allocation failed";
469: fail:
470: sshauthopt_free(ret);
471: if (errstrp != NULL)
472: *errstrp = errstr;
473: return NULL;
474: }
475:
476: struct sshauthopt *
477: sshauthopt_from_cert(struct sshkey *k)
478: {
479: struct sshauthopt *ret;
480:
481: if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
482: k->cert->type != SSH2_CERT_TYPE_USER)
483: return NULL;
484:
485: if ((ret = sshauthopt_new()) == NULL)
486: return NULL;
487:
488: /* Handle options and critical extensions separately */
489: if (cert_option_list(ret, k->cert->critical,
490: OPTIONS_CRITICAL, 1) == -1) {
491: sshauthopt_free(ret);
492: return NULL;
493: }
494: if (cert_option_list(ret, k->cert->extensions,
495: OPTIONS_EXTENSIONS, 0) == -1) {
496: sshauthopt_free(ret);
497: return NULL;
498: }
499: /* success */
500: return ret;
501: }
502:
503: /*
504: * Merges "additional" options to "primary" and returns the result.
505: * NB. Some options from primary have primacy.
506: */
507: struct sshauthopt *
508: sshauthopt_merge(const struct sshauthopt *primary,
509: const struct sshauthopt *additional, const char **errstrp)
510: {
511: struct sshauthopt *ret;
512: const char *errstr = "internal error";
513: const char *tmp;
514:
515: if (errstrp != NULL)
516: *errstrp = NULL;
517:
518: if ((ret = sshauthopt_new()) == NULL)
519: goto alloc_fail;
520:
521: /* cert_authority and cert_principals are cleared in result */
522:
523: /* Prefer access lists from primary. */
524: /* XXX err is both set and mismatch? */
525: tmp = primary->required_from_host_cert;
526: if (tmp == NULL)
527: tmp = additional->required_from_host_cert;
528: if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
529: goto alloc_fail;
530: tmp = primary->required_from_host_keys;
531: if (tmp == NULL)
532: tmp = additional->required_from_host_keys;
533: if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
534: goto alloc_fail;
535:
1.80 djm 536: /*
537: * force_tun_device, permitopen/permitlisten and environment all
538: * prefer the primary.
539: */
1.75 djm 540: ret->force_tun_device = primary->force_tun_device;
541: if (ret->force_tun_device == -1)
542: ret->force_tun_device = additional->force_tun_device;
543: if (primary->nenv > 0) {
544: if (dup_strings(&ret->env, &ret->nenv,
545: primary->env, primary->nenv) != 0)
546: goto alloc_fail;
547: } else if (additional->nenv) {
548: if (dup_strings(&ret->env, &ret->nenv,
549: additional->env, additional->nenv) != 0)
550: goto alloc_fail;
551: }
552: if (primary->npermitopen > 0) {
553: if (dup_strings(&ret->permitopen, &ret->npermitopen,
554: primary->permitopen, primary->npermitopen) != 0)
555: goto alloc_fail;
556: } else if (additional->npermitopen > 0) {
557: if (dup_strings(&ret->permitopen, &ret->npermitopen,
558: additional->permitopen, additional->npermitopen) != 0)
559: goto alloc_fail;
560: }
561:
1.80 djm 562: if (primary->npermitlisten > 0) {
563: if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
564: primary->permitlisten, primary->npermitlisten) != 0)
565: goto alloc_fail;
566: } else if (additional->npermitlisten > 0) {
567: if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
568: additional->permitlisten, additional->npermitlisten) != 0)
569: goto alloc_fail;
570: }
571:
1.90 ! djm 572: #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
! 573: /* Permissive flags are logical-AND (i.e. must be set in both) */
! 574: OPTFLAG_AND(permit_port_forwarding_flag);
! 575: OPTFLAG_AND(permit_agent_forwarding_flag);
! 576: OPTFLAG_AND(permit_x11_forwarding_flag);
! 577: OPTFLAG_AND(permit_pty_flag);
! 578: OPTFLAG_AND(permit_user_rc);
! 579: OPTFLAG_AND(no_require_user_presence);
! 580: #undef OPTFLAG_AND
1.75 djm 581:
1.77 djm 582: /* Earliest expiry time should win */
583: if (primary->valid_before != 0)
584: ret->valid_before = primary->valid_before;
585: if (additional->valid_before != 0 &&
586: additional->valid_before < ret->valid_before)
587: ret->valid_before = additional->valid_before;
588:
1.75 djm 589: /*
590: * When both multiple forced-command are specified, only
591: * proceed if they are identical, otherwise fail.
592: */
593: if (primary->force_command != NULL &&
594: additional->force_command != NULL) {
595: if (strcmp(primary->force_command,
596: additional->force_command) == 0) {
597: /* ok */
598: ret->force_command = strdup(primary->force_command);
599: if (ret->force_command == NULL)
600: goto alloc_fail;
601: } else {
602: errstr = "forced command options do not match";
603: goto fail;
604: }
605: } else if (primary->force_command != NULL) {
606: if ((ret->force_command = strdup(
607: primary->force_command)) == NULL)
608: goto alloc_fail;
609: } else if (additional->force_command != NULL) {
610: if ((ret->force_command = strdup(
611: additional->force_command)) == NULL)
612: goto alloc_fail;
613: }
614: /* success */
615: if (errstrp != NULL)
616: *errstrp = NULL;
617: return ret;
618:
619: alloc_fail:
620: errstr = "memory allocation failed";
621: fail:
622: if (errstrp != NULL)
623: *errstrp = errstr;
624: sshauthopt_free(ret);
625: return NULL;
626: }
627:
628: /*
629: * Copy options
630: */
631: struct sshauthopt *
632: sshauthopt_copy(const struct sshauthopt *orig)
633: {
634: struct sshauthopt *ret;
635:
636: if ((ret = sshauthopt_new()) == NULL)
637: return NULL;
638:
639: #define OPTSCALAR(x) ret->x = orig->x
640: OPTSCALAR(permit_port_forwarding_flag);
641: OPTSCALAR(permit_agent_forwarding_flag);
642: OPTSCALAR(permit_x11_forwarding_flag);
643: OPTSCALAR(permit_pty_flag);
644: OPTSCALAR(permit_user_rc);
645: OPTSCALAR(restricted);
646: OPTSCALAR(cert_authority);
647: OPTSCALAR(force_tun_device);
1.77 djm 648: OPTSCALAR(valid_before);
1.90 ! djm 649: OPTSCALAR(no_require_user_presence);
1.75 djm 650: #undef OPTSCALAR
651: #define OPTSTRING(x) \
652: do { \
653: if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
654: sshauthopt_free(ret); \
655: return NULL; \
656: } \
657: } while (0)
658: OPTSTRING(cert_principals);
659: OPTSTRING(force_command);
660: OPTSTRING(required_from_host_cert);
661: OPTSTRING(required_from_host_keys);
662: #undef OPTSTRING
663:
664: if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
665: dup_strings(&ret->permitopen, &ret->npermitopen,
1.80 djm 666: orig->permitopen, orig->npermitopen) != 0 ||
667: dup_strings(&ret->permitlisten, &ret->npermitlisten,
668: orig->permitlisten, orig->npermitlisten) != 0) {
1.75 djm 669: sshauthopt_free(ret);
670: return NULL;
671: }
672: return ret;
673: }
674:
675: static int
676: serialise_array(struct sshbuf *m, char **a, size_t n)
677: {
678: struct sshbuf *b;
679: size_t i;
680: int r;
681:
682: if (n > INT_MAX)
683: return SSH_ERR_INTERNAL_ERROR;
684:
685: if ((b = sshbuf_new()) == NULL) {
686: return SSH_ERR_ALLOC_FAIL;
687: }
688: for (i = 0; i < n; i++) {
689: if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
690: sshbuf_free(b);
691: return r;
692: }
693: }
694: if ((r = sshbuf_put_u32(m, n)) != 0 ||
695: (r = sshbuf_put_stringb(m, b)) != 0) {
696: sshbuf_free(b);
697: return r;
698: }
699: /* success */
700: return 0;
701: }
702:
703: static int
704: deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
705: {
706: char **a = NULL;
707: size_t i, n = 0;
708: struct sshbuf *b = NULL;
709: u_int tmp;
710: int r = SSH_ERR_INTERNAL_ERROR;
711:
712: if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
713: (r = sshbuf_froms(m, &b)) != 0)
714: goto out;
715: if (tmp > INT_MAX) {
716: r = SSH_ERR_INVALID_FORMAT;
717: goto out;
718: }
719: n = tmp;
720: if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
721: r = SSH_ERR_ALLOC_FAIL;
722: goto out;
723: }
724: for (i = 0; i < n; i++) {
725: if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
726: goto out;
727: }
728: /* success */
729: r = 0;
730: *ap = a;
731: a = NULL;
732: *np = n;
733: n = 0;
734: out:
735: for (i = 0; i < n; i++)
736: free(a[i]);
737: free(a);
738: sshbuf_free(b);
739: return r;
740: }
741:
742: static int
743: serialise_nullable_string(struct sshbuf *m, const char *s)
744: {
745: int r;
746:
747: if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
748: (r = sshbuf_put_cstring(m, s)) != 0)
749: return r;
750: return 0;
751: }
752:
753: static int
754: deserialise_nullable_string(struct sshbuf *m, char **sp)
755: {
756: int r;
757: u_char flag;
758:
759: *sp = NULL;
760: if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
761: (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
762: return r;
763: return 0;
764: }
765:
766: int
767: sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
768: int untrusted)
769: {
770: int r = SSH_ERR_INTERNAL_ERROR;
771:
1.90 ! djm 772: /* Flag options */
1.75 djm 773: if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
774: (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
775: (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
776: (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
777: (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
778: (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
1.77 djm 779: (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
1.90 ! djm 780: (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0)
! 781: return r;
! 782:
! 783: /* Simple integer options */
! 784: if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
1.75 djm 785: return r;
786:
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)
791: return r;
792:
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)
802: return r;
803:
804: /* Array options */
805: if ((r = serialise_array(m, opts->env,
806: untrusted ? 0 : opts->nenv)) != 0 ||
807: (r = serialise_array(m, opts->permitopen,
1.82 djm 808: untrusted ? 0 : opts->npermitopen)) != 0 ||
1.80 djm 809: (r = serialise_array(m, opts->permitlisten,
810: untrusted ? 0 : opts->npermitlisten)) != 0)
1.75 djm 811: return r;
812:
813: /* success */
814: return 0;
815: }
816:
817: int
818: sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
819: {
820: struct sshauthopt *opts = NULL;
821: int r = SSH_ERR_INTERNAL_ERROR;
822: u_char f;
823: u_int tmp;
824:
825: if ((opts = calloc(1, sizeof(*opts))) == NULL)
826: return SSH_ERR_ALLOC_FAIL;
827:
1.90 ! djm 828: /* Flag options */
1.75 djm 829: #define OPT_FLAG(x) \
830: do { \
831: if ((r = sshbuf_get_u8(m, &f)) != 0) \
832: goto out; \
833: opts->x = f; \
834: } while (0)
835: OPT_FLAG(permit_port_forwarding_flag);
836: OPT_FLAG(permit_agent_forwarding_flag);
837: OPT_FLAG(permit_x11_forwarding_flag);
838: OPT_FLAG(permit_pty_flag);
839: OPT_FLAG(permit_user_rc);
840: OPT_FLAG(restricted);
841: OPT_FLAG(cert_authority);
1.90 ! djm 842: OPT_FLAG(no_require_user_presence);
1.75 djm 843: #undef OPT_FLAG
1.77 djm 844:
1.90 ! djm 845: /* Simple integer options */
1.77 djm 846: if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
847: goto out;
1.75 djm 848:
849: /* tunnel number can be negative to indicate "unset" */
850: if ((r = sshbuf_get_u8(m, &f)) != 0 ||
851: (r = sshbuf_get_u32(m, &tmp)) != 0)
852: goto out;
853: opts->force_tun_device = f ? -1 : (int)tmp;
854:
855: /* String options may be NULL */
856: if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
857: (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
858: (r = deserialise_nullable_string(m,
859: &opts->required_from_host_cert)) != 0 ||
860: (r = deserialise_nullable_string(m,
861: &opts->required_from_host_keys)) != 0)
862: goto out;
863:
864: /* Array options */
865: if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
866: (r = deserialise_array(m,
1.80 djm 867: &opts->permitopen, &opts->npermitopen)) != 0 ||
868: (r = deserialise_array(m,
869: &opts->permitlisten, &opts->npermitlisten)) != 0)
1.75 djm 870: goto out;
871:
872: /* success */
873: r = 0;
874: *optsp = opts;
875: opts = NULL;
876: out:
877: sshauthopt_free(opts);
878: return r;
879: }