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