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