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