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