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