xref: /dflybsd-src/crypto/openssh/auth-options.c (revision 50a69bb51183a7916e776f2c9f5fa64c999f1a2f)
1*50a69bb5SSascha Wildner /* $OpenBSD: auth-options.c,v 1.97 2021/07/24 01:55:19 djm Exp $ */
218de8d7fSPeter Avalos /*
3664f4763Szrj  * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
4664f4763Szrj  *
5664f4763Szrj  * Permission to use, copy, modify, and distribute this software for any
6664f4763Szrj  * purpose with or without fee is hereby granted, provided that the above
7664f4763Szrj  * copyright notice and this permission notice appear in all copies.
8664f4763Szrj  *
9664f4763Szrj  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10664f4763Szrj  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11664f4763Szrj  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12664f4763Szrj  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13664f4763Szrj  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14664f4763Szrj  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15664f4763Szrj  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1618de8d7fSPeter Avalos  */
1718de8d7fSPeter Avalos 
1818de8d7fSPeter Avalos #include "includes.h"
1918de8d7fSPeter Avalos 
2018de8d7fSPeter Avalos #include <sys/types.h>
2118de8d7fSPeter Avalos 
220cbfa66cSDaniel Fojt #include <stdlib.h>
2318de8d7fSPeter Avalos #include <netdb.h>
2418de8d7fSPeter Avalos #include <pwd.h>
2518de8d7fSPeter Avalos #include <string.h>
2618de8d7fSPeter Avalos #include <stdio.h>
2718de8d7fSPeter Avalos #include <stdarg.h>
28664f4763Szrj #include <ctype.h>
29664f4763Szrj #include <limits.h>
3018de8d7fSPeter Avalos 
3118de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h"
32e9778795SPeter Avalos 
3318de8d7fSPeter Avalos #include "xmalloc.h"
34e9778795SPeter Avalos #include "ssherr.h"
3518de8d7fSPeter Avalos #include "log.h"
36e9778795SPeter Avalos #include "sshbuf.h"
3736e94dc5SPeter Avalos #include "misc.h"
38e9778795SPeter Avalos #include "sshkey.h"
39664f4763Szrj #include "match.h"
40664f4763Szrj #include "ssh2.h"
41856ea928SPeter Avalos #include "auth-options.h"
4218de8d7fSPeter Avalos 
43664f4763Szrj static int
44664f4763Szrj dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
45664f4763Szrj {
46664f4763Szrj 	char **dst;
47664f4763Szrj 	size_t i, j;
4818de8d7fSPeter Avalos 
49664f4763Szrj 	*dstp = NULL;
50664f4763Szrj 	*ndstp = 0;
51664f4763Szrj 	if (nsrc == 0)
52664f4763Szrj 		return 0;
5318de8d7fSPeter Avalos 
54664f4763Szrj 	if ((dst = calloc(nsrc, sizeof(*src))) == NULL)
55664f4763Szrj 		return -1;
56664f4763Szrj 	for (i = 0; i < nsrc; i++) {
57664f4763Szrj 		if ((dst[i] = strdup(src[i])) == NULL) {
58664f4763Szrj 			for (j = 0; j < i; j++)
59664f4763Szrj 				free(dst[j]);
60664f4763Szrj 			free(dst);
61664f4763Szrj 			return -1;
62664f4763Szrj 		}
63664f4763Szrj 	}
64664f4763Szrj 	/* success */
65664f4763Szrj 	*dstp = dst;
66664f4763Szrj 	*ndstp = nsrc;
6718de8d7fSPeter Avalos 	return 0;
6818de8d7fSPeter Avalos }
69856ea928SPeter Avalos 
70856ea928SPeter Avalos #define OPTIONS_CRITICAL	1
71856ea928SPeter Avalos #define OPTIONS_EXTENSIONS	2
72856ea928SPeter Avalos static int
73664f4763Szrj cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
74664f4763Szrj     u_int which, int crit)
75856ea928SPeter Avalos {
76856ea928SPeter Avalos 	char *command, *allowed;
7736e94dc5SPeter Avalos 	char *name = NULL;
78e9778795SPeter Avalos 	struct sshbuf *c = NULL, *data = NULL;
79664f4763Szrj 	int r, ret = -1, found;
80856ea928SPeter Avalos 
81e9778795SPeter Avalos 	if ((c = sshbuf_fromb(oblob)) == NULL) {
82*50a69bb5SSascha Wildner 		error_f("sshbuf_fromb failed");
83856ea928SPeter Avalos 		goto out;
84856ea928SPeter Avalos 	}
85e9778795SPeter Avalos 
86e9778795SPeter Avalos 	while (sshbuf_len(c) > 0) {
87e9778795SPeter Avalos 		sshbuf_free(data);
88e9778795SPeter Avalos 		data = NULL;
89e9778795SPeter Avalos 		if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
90e9778795SPeter Avalos 		    (r = sshbuf_froms(c, &data)) != 0) {
91*50a69bb5SSascha Wildner 			error_r(r, "Unable to parse certificate options");
92e9778795SPeter Avalos 			goto out;
93e9778795SPeter Avalos 		}
94e9778795SPeter Avalos 		debug3("found certificate option \"%.100s\" len %zu",
95e9778795SPeter Avalos 		    name, sshbuf_len(data));
96856ea928SPeter Avalos 		found = 0;
97856ea928SPeter Avalos 		if ((which & OPTIONS_EXTENSIONS) != 0) {
980cbfa66cSDaniel Fojt 			if (strcmp(name, "no-touch-required") == 0) {
990cbfa66cSDaniel Fojt 				opts->no_require_user_presence = 1;
1000cbfa66cSDaniel Fojt 				found = 1;
1010cbfa66cSDaniel Fojt 			} else if (strcmp(name, "permit-X11-forwarding") == 0) {
102664f4763Szrj 				opts->permit_x11_forwarding_flag = 1;
103856ea928SPeter Avalos 				found = 1;
104856ea928SPeter Avalos 			} else if (strcmp(name,
105856ea928SPeter Avalos 			    "permit-agent-forwarding") == 0) {
106664f4763Szrj 				opts->permit_agent_forwarding_flag = 1;
107856ea928SPeter Avalos 				found = 1;
108856ea928SPeter Avalos 			} else if (strcmp(name,
109856ea928SPeter Avalos 			    "permit-port-forwarding") == 0) {
110664f4763Szrj 				opts->permit_port_forwarding_flag = 1;
111856ea928SPeter Avalos 				found = 1;
112856ea928SPeter Avalos 			} else if (strcmp(name, "permit-pty") == 0) {
113664f4763Szrj 				opts->permit_pty_flag = 1;
114856ea928SPeter Avalos 				found = 1;
115856ea928SPeter Avalos 			} else if (strcmp(name, "permit-user-rc") == 0) {
116664f4763Szrj 				opts->permit_user_rc = 1;
117856ea928SPeter Avalos 				found = 1;
118856ea928SPeter Avalos 			}
119856ea928SPeter Avalos 		}
120856ea928SPeter Avalos 		if (!found && (which & OPTIONS_CRITICAL) != 0) {
121*50a69bb5SSascha Wildner 			if (strcmp(name, "verify-required") == 0) {
122*50a69bb5SSascha Wildner 				opts->require_verify = 1;
123*50a69bb5SSascha Wildner 				found = 1;
124*50a69bb5SSascha Wildner 			} else if (strcmp(name, "force-command") == 0) {
125e9778795SPeter Avalos 				if ((r = sshbuf_get_cstring(data, &command,
126e9778795SPeter Avalos 				    NULL)) != 0) {
127*50a69bb5SSascha Wildner 					error_r(r, "Unable to parse \"%s\" "
128*50a69bb5SSascha Wildner 					    "section", name);
129856ea928SPeter Avalos 					goto out;
130856ea928SPeter Avalos 				}
131664f4763Szrj 				if (opts->force_command != NULL) {
132856ea928SPeter Avalos 					error("Certificate has multiple "
133856ea928SPeter Avalos 					    "force-command options");
13436e94dc5SPeter Avalos 					free(command);
135856ea928SPeter Avalos 					goto out;
136856ea928SPeter Avalos 				}
137664f4763Szrj 				opts->force_command = command;
138856ea928SPeter Avalos 				found = 1;
139*50a69bb5SSascha Wildner 			} else if (strcmp(name, "source-address") == 0) {
140e9778795SPeter Avalos 				if ((r = sshbuf_get_cstring(data, &allowed,
141e9778795SPeter Avalos 				    NULL)) != 0) {
142*50a69bb5SSascha Wildner 					error_r(r, "Unable to parse \"%s\" "
143*50a69bb5SSascha Wildner 					    "section", name);
144856ea928SPeter Avalos 					goto out;
145856ea928SPeter Avalos 				}
146664f4763Szrj 				if (opts->required_from_host_cert != NULL) {
147856ea928SPeter Avalos 					error("Certificate has multiple "
148856ea928SPeter Avalos 					    "source-address options");
14936e94dc5SPeter Avalos 					free(allowed);
150856ea928SPeter Avalos 					goto out;
151856ea928SPeter Avalos 				}
152664f4763Szrj 				/* Check syntax */
153664f4763Szrj 				if (addr_match_cidr_list(NULL, allowed) == -1) {
154856ea928SPeter Avalos 					error("Certificate source-address "
155856ea928SPeter Avalos 					    "contents invalid");
156856ea928SPeter Avalos 					goto out;
157856ea928SPeter Avalos 				}
158664f4763Szrj 				opts->required_from_host_cert = allowed;
159856ea928SPeter Avalos 				found = 1;
160856ea928SPeter Avalos 			}
161856ea928SPeter Avalos 		}
162856ea928SPeter Avalos 
163856ea928SPeter Avalos 		if (!found) {
164856ea928SPeter Avalos 			if (crit) {
165856ea928SPeter Avalos 				error("Certificate critical option \"%s\" "
166856ea928SPeter Avalos 				    "is not supported", name);
167856ea928SPeter Avalos 				goto out;
168856ea928SPeter Avalos 			} else {
169856ea928SPeter Avalos 				logit("Certificate extension \"%s\" "
170856ea928SPeter Avalos 				    "is not supported", name);
171856ea928SPeter Avalos 			}
172e9778795SPeter Avalos 		} else if (sshbuf_len(data) != 0) {
173856ea928SPeter Avalos 			error("Certificate option \"%s\" corrupt "
174856ea928SPeter Avalos 			    "(extra data)", name);
175856ea928SPeter Avalos 			goto out;
176856ea928SPeter Avalos 		}
17736e94dc5SPeter Avalos 		free(name);
17836e94dc5SPeter Avalos 		name = NULL;
179856ea928SPeter Avalos 	}
180856ea928SPeter Avalos 	/* successfully parsed all options */
181856ea928SPeter Avalos 	ret = 0;
182856ea928SPeter Avalos 
183856ea928SPeter Avalos  out:
18436e94dc5SPeter Avalos 	free(name);
185e9778795SPeter Avalos 	sshbuf_free(data);
186e9778795SPeter Avalos 	sshbuf_free(c);
187856ea928SPeter Avalos 	return ret;
188856ea928SPeter Avalos }
189856ea928SPeter Avalos 
190664f4763Szrj struct sshauthopt *
191664f4763Szrj sshauthopt_new(void)
192856ea928SPeter Avalos {
193664f4763Szrj 	struct sshauthopt *ret;
194856ea928SPeter Avalos 
195664f4763Szrj 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
196664f4763Szrj 		return NULL;
197664f4763Szrj 	ret->force_tun_device = -1;
198664f4763Szrj 	return ret;
199664f4763Szrj }
200ce74bacaSMatthew Dillon 
201664f4763Szrj void
202664f4763Szrj sshauthopt_free(struct sshauthopt *opts)
203664f4763Szrj {
204664f4763Szrj 	size_t i;
205856ea928SPeter Avalos 
206664f4763Szrj 	if (opts == NULL)
207664f4763Szrj 		return;
208664f4763Szrj 
209664f4763Szrj 	free(opts->cert_principals);
210664f4763Szrj 	free(opts->force_command);
211664f4763Szrj 	free(opts->required_from_host_cert);
212664f4763Szrj 	free(opts->required_from_host_keys);
213664f4763Szrj 
214664f4763Szrj 	for (i = 0; i < opts->nenv; i++)
215664f4763Szrj 		free(opts->env[i]);
216664f4763Szrj 	free(opts->env);
217664f4763Szrj 
218664f4763Szrj 	for (i = 0; i < opts->npermitopen; i++)
219664f4763Szrj 		free(opts->permitopen[i]);
220664f4763Szrj 	free(opts->permitopen);
221664f4763Szrj 
222664f4763Szrj 	for (i = 0; i < opts->npermitlisten; i++)
223664f4763Szrj 		free(opts->permitlisten[i]);
224664f4763Szrj 	free(opts->permitlisten);
225664f4763Szrj 
2260cbfa66cSDaniel Fojt 	freezero(opts, sizeof(*opts));
227664f4763Szrj }
228664f4763Szrj 
229664f4763Szrj struct sshauthopt *
230664f4763Szrj sshauthopt_new_with_keys_defaults(void)
231664f4763Szrj {
232664f4763Szrj 	struct sshauthopt *ret = NULL;
233664f4763Szrj 
234664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
235664f4763Szrj 		return NULL;
236664f4763Szrj 
237664f4763Szrj 	/* Defaults for authorized_keys flags */
238664f4763Szrj 	ret->permit_port_forwarding_flag = 1;
239664f4763Szrj 	ret->permit_agent_forwarding_flag = 1;
240664f4763Szrj 	ret->permit_x11_forwarding_flag = 1;
241664f4763Szrj 	ret->permit_pty_flag = 1;
242664f4763Szrj 	ret->permit_user_rc = 1;
243664f4763Szrj 	return ret;
244664f4763Szrj }
245664f4763Szrj 
246ce74bacaSMatthew Dillon /*
247664f4763Szrj  * Parse and record a permitopen/permitlisten directive.
248664f4763Szrj  * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
249ce74bacaSMatthew Dillon  */
250664f4763Szrj static int
251664f4763Szrj handle_permit(const char **optsp, int allow_bare_port,
252664f4763Szrj     char ***permitsp, size_t *npermitsp, const char **errstrp)
253664f4763Szrj {
254664f4763Szrj 	char *opt, *tmp, *cp, *host, **permits = *permitsp;
255664f4763Szrj 	size_t npermits = *npermitsp;
256664f4763Szrj 	const char *errstr = "unknown error";
257664f4763Szrj 
2580cbfa66cSDaniel Fojt 	if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
259664f4763Szrj 		*errstrp = "too many permission directives";
260ce74bacaSMatthew Dillon 		return -1;
261856ea928SPeter Avalos 	}
262664f4763Szrj 	if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
263664f4763Szrj 		return -1;
264664f4763Szrj 	}
265664f4763Szrj 	if (allow_bare_port && strchr(opt, ':') == NULL) {
266664f4763Szrj 		/*
267664f4763Szrj 		 * Allow a bare port number in permitlisten to indicate a
268664f4763Szrj 		 * listen_host wildcard.
269664f4763Szrj 		 */
2700cbfa66cSDaniel Fojt 		if (asprintf(&tmp, "*:%s", opt) == -1) {
2710cbfa66cSDaniel Fojt 			free(opt);
272664f4763Szrj 			*errstrp = "memory allocation failed";
273664f4763Szrj 			return -1;
274664f4763Szrj 		}
275664f4763Szrj 		free(opt);
276664f4763Szrj 		opt = tmp;
277664f4763Szrj 	}
278664f4763Szrj 	if ((tmp = strdup(opt)) == NULL) {
279664f4763Szrj 		free(opt);
280664f4763Szrj 		*errstrp = "memory allocation failed";
281664f4763Szrj 		return -1;
282664f4763Szrj 	}
283664f4763Szrj 	cp = tmp;
284664f4763Szrj 	/* validate syntax before recording it. */
285664f4763Szrj 	host = hpdelim(&cp);
286664f4763Szrj 	if (host == NULL || strlen(host) >= NI_MAXHOST) {
287664f4763Szrj 		free(tmp);
288664f4763Szrj 		free(opt);
289664f4763Szrj 		*errstrp = "invalid permission hostname";
290664f4763Szrj 		return -1;
291664f4763Szrj 	}
292664f4763Szrj 	/*
293664f4763Szrj 	 * don't want to use permitopen_port to avoid
294664f4763Szrj 	 * dependency on channels.[ch] here.
295664f4763Szrj 	 */
296664f4763Szrj 	if (cp == NULL ||
297664f4763Szrj 	    (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
298664f4763Szrj 		free(tmp);
299664f4763Szrj 		free(opt);
300664f4763Szrj 		*errstrp = "invalid permission port";
301664f4763Szrj 		return -1;
302664f4763Szrj 	}
303664f4763Szrj 	/* XXX - add streamlocal support */
304664f4763Szrj 	free(tmp);
305664f4763Szrj 	/* Record it */
306664f4763Szrj 	if ((permits = recallocarray(permits, npermits, npermits + 1,
307664f4763Szrj 	    sizeof(*permits))) == NULL) {
308664f4763Szrj 		free(opt);
309664f4763Szrj 		/* NB. don't update *permitsp if alloc fails */
310664f4763Szrj 		*errstrp = "memory allocation failed";
311664f4763Szrj 		return -1;
312664f4763Szrj 	}
313664f4763Szrj 	permits[npermits++] = opt;
314664f4763Szrj 	*permitsp = permits;
315664f4763Szrj 	*npermitsp = npermits;
316856ea928SPeter Avalos 	return 0;
317856ea928SPeter Avalos }
318856ea928SPeter Avalos 
319664f4763Szrj struct sshauthopt *
320664f4763Szrj sshauthopt_parse(const char *opts, const char **errstrp)
321664f4763Szrj {
322664f4763Szrj 	char **oarray, *opt, *cp, *tmp;
323664f4763Szrj 	int r;
324664f4763Szrj 	struct sshauthopt *ret = NULL;
325664f4763Szrj 	const char *errstr = "unknown error";
326664f4763Szrj 	uint64_t valid_before;
327*50a69bb5SSascha Wildner 	size_t i, l;
328664f4763Szrj 
329664f4763Szrj 	if (errstrp != NULL)
330664f4763Szrj 		*errstrp = NULL;
331664f4763Szrj 	if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
332664f4763Szrj 		goto alloc_fail;
333664f4763Szrj 
334664f4763Szrj 	if (opts == NULL)
335664f4763Szrj 		return ret;
336664f4763Szrj 
337664f4763Szrj 	while (*opts && *opts != ' ' && *opts != '\t') {
338664f4763Szrj 		/* flag options */
339664f4763Szrj 		if ((r = opt_flag("restrict", 0, &opts)) != -1) {
340664f4763Szrj 			ret->restricted = 1;
341664f4763Szrj 			ret->permit_port_forwarding_flag = 0;
342664f4763Szrj 			ret->permit_agent_forwarding_flag = 0;
343664f4763Szrj 			ret->permit_x11_forwarding_flag = 0;
344664f4763Szrj 			ret->permit_pty_flag = 0;
345664f4763Szrj 			ret->permit_user_rc = 0;
346664f4763Szrj 		} else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
347664f4763Szrj 			ret->cert_authority = r;
348664f4763Szrj 		} else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
349664f4763Szrj 			ret->permit_port_forwarding_flag = r == 1;
350664f4763Szrj 		} else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
351664f4763Szrj 			ret->permit_agent_forwarding_flag = r == 1;
352664f4763Szrj 		} else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
353664f4763Szrj 			ret->permit_x11_forwarding_flag = r == 1;
3540cbfa66cSDaniel Fojt 		} else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
3550cbfa66cSDaniel Fojt 			ret->no_require_user_presence = r != 1; /* NB. flip */
356*50a69bb5SSascha Wildner 		} else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
357*50a69bb5SSascha Wildner 			ret->require_verify = r == 1;
358664f4763Szrj 		} else if ((r = opt_flag("pty", 1, &opts)) != -1) {
359664f4763Szrj 			ret->permit_pty_flag = r == 1;
360664f4763Szrj 		} else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
361664f4763Szrj 			ret->permit_user_rc = r == 1;
362664f4763Szrj 		} else if (opt_match(&opts, "command")) {
363664f4763Szrj 			if (ret->force_command != NULL) {
364664f4763Szrj 				errstr = "multiple \"command\" clauses";
365664f4763Szrj 				goto fail;
366664f4763Szrj 			}
367664f4763Szrj 			ret->force_command = opt_dequote(&opts, &errstr);
368664f4763Szrj 			if (ret->force_command == NULL)
369664f4763Szrj 				goto fail;
370664f4763Szrj 		} else if (opt_match(&opts, "principals")) {
371664f4763Szrj 			if (ret->cert_principals != NULL) {
372664f4763Szrj 				errstr = "multiple \"principals\" clauses";
373664f4763Szrj 				goto fail;
374664f4763Szrj 			}
375664f4763Szrj 			ret->cert_principals = opt_dequote(&opts, &errstr);
376664f4763Szrj 			if (ret->cert_principals == NULL)
377664f4763Szrj 				goto fail;
378664f4763Szrj 		} else if (opt_match(&opts, "from")) {
379664f4763Szrj 			if (ret->required_from_host_keys != NULL) {
380664f4763Szrj 				errstr = "multiple \"from\" clauses";
381664f4763Szrj 				goto fail;
382664f4763Szrj 			}
383664f4763Szrj 			ret->required_from_host_keys = opt_dequote(&opts,
384664f4763Szrj 			    &errstr);
385664f4763Szrj 			if (ret->required_from_host_keys == NULL)
386664f4763Szrj 				goto fail;
387664f4763Szrj 		} else if (opt_match(&opts, "expiry-time")) {
388664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
389664f4763Szrj 				goto fail;
390664f4763Szrj 			if (parse_absolute_time(opt, &valid_before) != 0 ||
391664f4763Szrj 			    valid_before == 0) {
392664f4763Szrj 				free(opt);
393664f4763Szrj 				errstr = "invalid expires time";
394664f4763Szrj 				goto fail;
395664f4763Szrj 			}
396664f4763Szrj 			free(opt);
397664f4763Szrj 			if (ret->valid_before == 0 ||
398664f4763Szrj 			    valid_before < ret->valid_before)
399664f4763Szrj 				ret->valid_before = valid_before;
400664f4763Szrj 		} else if (opt_match(&opts, "environment")) {
401*50a69bb5SSascha Wildner 			if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
402664f4763Szrj 				errstr = "too many environment strings";
403664f4763Szrj 				goto fail;
404664f4763Szrj 			}
405664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
406664f4763Szrj 				goto fail;
407664f4763Szrj 			/* env name must be alphanumeric and followed by '=' */
408664f4763Szrj 			if ((tmp = strchr(opt, '=')) == NULL) {
409664f4763Szrj 				free(opt);
410664f4763Szrj 				errstr = "invalid environment string";
411664f4763Szrj 				goto fail;
412664f4763Szrj 			}
413*50a69bb5SSascha Wildner 			if ((cp = strdup(opt)) == NULL) {
414*50a69bb5SSascha Wildner 				free(opt);
415664f4763Szrj 				goto alloc_fail;
416*50a69bb5SSascha Wildner 			}
417*50a69bb5SSascha Wildner 			l = (size_t)(tmp - opt);
418*50a69bb5SSascha Wildner 			cp[l] = '\0'; /* truncate at '=' */
419664f4763Szrj 			if (!valid_env_name(cp)) {
420664f4763Szrj 				free(cp);
421664f4763Szrj 				free(opt);
422664f4763Szrj 				errstr = "invalid environment string";
423664f4763Szrj 				goto fail;
424664f4763Szrj 			}
425*50a69bb5SSascha Wildner 			/* Check for duplicates; XXX O(n*log(n)) */
426*50a69bb5SSascha Wildner 			for (i = 0; i < ret->nenv; i++) {
427*50a69bb5SSascha Wildner 				if (strncmp(ret->env[i], cp, l) == 0 &&
428*50a69bb5SSascha Wildner 				    ret->env[i][l] == '=')
429*50a69bb5SSascha Wildner 					break;
430*50a69bb5SSascha Wildner 			}
431664f4763Szrj 			free(cp);
432*50a69bb5SSascha Wildner 			/* First match wins */
433*50a69bb5SSascha Wildner 			if (i >= ret->nenv) {
434664f4763Szrj 				/* Append it. */
435664f4763Szrj 				oarray = ret->env;
436*50a69bb5SSascha Wildner 				if ((ret->env = recallocarray(ret->env,
437*50a69bb5SSascha Wildner 				    ret->nenv, ret->nenv + 1,
438*50a69bb5SSascha Wildner 				    sizeof(*ret->env))) == NULL) {
439664f4763Szrj 					free(opt);
440*50a69bb5SSascha Wildner 					/* put it back for cleanup */
441*50a69bb5SSascha Wildner 					ret->env = oarray;
442664f4763Szrj 					goto alloc_fail;
443664f4763Szrj 				}
444664f4763Szrj 				ret->env[ret->nenv++] = opt;
445*50a69bb5SSascha Wildner 				opt = NULL; /* transferred */
446*50a69bb5SSascha Wildner 			}
447*50a69bb5SSascha Wildner 			free(opt);
448664f4763Szrj 		} else if (opt_match(&opts, "permitopen")) {
449664f4763Szrj 			if (handle_permit(&opts, 0, &ret->permitopen,
450664f4763Szrj 			    &ret->npermitopen, &errstr) != 0)
451664f4763Szrj 				goto fail;
452664f4763Szrj 		} else if (opt_match(&opts, "permitlisten")) {
453664f4763Szrj 			if (handle_permit(&opts, 1, &ret->permitlisten,
454664f4763Szrj 			    &ret->npermitlisten, &errstr) != 0)
455664f4763Szrj 				goto fail;
456664f4763Szrj 		} else if (opt_match(&opts, "tunnel")) {
457664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
458664f4763Szrj 				goto fail;
459664f4763Szrj 			ret->force_tun_device = a2tun(opt, NULL);
460664f4763Szrj 			free(opt);
461664f4763Szrj 			if (ret->force_tun_device == SSH_TUNID_ERR) {
462664f4763Szrj 				errstr = "invalid tun device";
463664f4763Szrj 				goto fail;
464664f4763Szrj 			}
465664f4763Szrj 		}
466664f4763Szrj 		/*
467664f4763Szrj 		 * Skip the comma, and move to the next option
468664f4763Szrj 		 * (or break out if there are no more).
469664f4763Szrj 		 */
470664f4763Szrj 		if (*opts == '\0' || *opts == ' ' || *opts == '\t')
471664f4763Szrj 			break;		/* End of options. */
472664f4763Szrj 		/* Anything other than a comma is an unknown option */
473664f4763Szrj 		if (*opts != ',') {
474664f4763Szrj 			errstr = "unknown key option";
475664f4763Szrj 			goto fail;
476664f4763Szrj 		}
477664f4763Szrj 		opts++;
478664f4763Szrj 		if (*opts == '\0') {
479664f4763Szrj 			errstr = "unexpected end-of-options";
480664f4763Szrj 			goto fail;
481664f4763Szrj 		}
482664f4763Szrj 	}
483664f4763Szrj 
484664f4763Szrj 	/* success */
485664f4763Szrj 	if (errstrp != NULL)
486664f4763Szrj 		*errstrp = NULL;
487664f4763Szrj 	return ret;
488664f4763Szrj 
489664f4763Szrj alloc_fail:
490664f4763Szrj 	errstr = "memory allocation failed";
491664f4763Szrj fail:
492664f4763Szrj 	sshauthopt_free(ret);
493664f4763Szrj 	if (errstrp != NULL)
494664f4763Szrj 		*errstrp = errstr;
495664f4763Szrj 	return NULL;
496664f4763Szrj }
497664f4763Szrj 
498664f4763Szrj struct sshauthopt *
499664f4763Szrj sshauthopt_from_cert(struct sshkey *k)
500664f4763Szrj {
501664f4763Szrj 	struct sshauthopt *ret;
502664f4763Szrj 
503664f4763Szrj 	if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
504664f4763Szrj 	    k->cert->type != SSH2_CERT_TYPE_USER)
505664f4763Szrj 		return NULL;
506664f4763Szrj 
507664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
508664f4763Szrj 		return NULL;
509664f4763Szrj 
510664f4763Szrj 	/* Handle options and critical extensions separately */
511664f4763Szrj 	if (cert_option_list(ret, k->cert->critical,
512664f4763Szrj 	    OPTIONS_CRITICAL, 1) == -1) {
513664f4763Szrj 		sshauthopt_free(ret);
514664f4763Szrj 		return NULL;
515664f4763Szrj 	}
516664f4763Szrj 	if (cert_option_list(ret, k->cert->extensions,
517664f4763Szrj 	    OPTIONS_EXTENSIONS, 0) == -1) {
518664f4763Szrj 		sshauthopt_free(ret);
519664f4763Szrj 		return NULL;
520664f4763Szrj 	}
521664f4763Szrj 	/* success */
522664f4763Szrj 	return ret;
523664f4763Szrj }
524664f4763Szrj 
525664f4763Szrj /*
526664f4763Szrj  * Merges "additional" options to "primary" and returns the result.
527664f4763Szrj  * NB. Some options from primary have primacy.
528664f4763Szrj  */
529664f4763Szrj struct sshauthopt *
530664f4763Szrj sshauthopt_merge(const struct sshauthopt *primary,
531664f4763Szrj     const struct sshauthopt *additional, const char **errstrp)
532664f4763Szrj {
533664f4763Szrj 	struct sshauthopt *ret;
534664f4763Szrj 	const char *errstr = "internal error";
535664f4763Szrj 	const char *tmp;
536664f4763Szrj 
537664f4763Szrj 	if (errstrp != NULL)
538664f4763Szrj 		*errstrp = NULL;
539664f4763Szrj 
540664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
541664f4763Szrj 		goto alloc_fail;
542664f4763Szrj 
543664f4763Szrj 	/* cert_authority and cert_principals are cleared in result */
544664f4763Szrj 
545664f4763Szrj 	/* Prefer access lists from primary. */
546664f4763Szrj 	/* XXX err is both set and mismatch? */
547664f4763Szrj 	tmp = primary->required_from_host_cert;
548664f4763Szrj 	if (tmp == NULL)
549664f4763Szrj 		tmp = additional->required_from_host_cert;
550664f4763Szrj 	if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
551664f4763Szrj 		goto alloc_fail;
552664f4763Szrj 	tmp = primary->required_from_host_keys;
553664f4763Szrj 	if (tmp == NULL)
554664f4763Szrj 		tmp = additional->required_from_host_keys;
555664f4763Szrj 	if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
556664f4763Szrj 		goto alloc_fail;
557664f4763Szrj 
558664f4763Szrj 	/*
559664f4763Szrj 	 * force_tun_device, permitopen/permitlisten and environment all
560664f4763Szrj 	 * prefer the primary.
561664f4763Szrj 	 */
562664f4763Szrj 	ret->force_tun_device = primary->force_tun_device;
563664f4763Szrj 	if (ret->force_tun_device == -1)
564664f4763Szrj 		ret->force_tun_device = additional->force_tun_device;
565664f4763Szrj 	if (primary->nenv > 0) {
566664f4763Szrj 		if (dup_strings(&ret->env, &ret->nenv,
567664f4763Szrj 		    primary->env, primary->nenv) != 0)
568664f4763Szrj 			goto alloc_fail;
569664f4763Szrj 	} else if (additional->nenv) {
570664f4763Szrj 		if (dup_strings(&ret->env, &ret->nenv,
571664f4763Szrj 		    additional->env, additional->nenv) != 0)
572664f4763Szrj 			goto alloc_fail;
573664f4763Szrj 	}
574664f4763Szrj 	if (primary->npermitopen > 0) {
575664f4763Szrj 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
576664f4763Szrj 		    primary->permitopen, primary->npermitopen) != 0)
577664f4763Szrj 			goto alloc_fail;
578664f4763Szrj 	} else if (additional->npermitopen > 0) {
579664f4763Szrj 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
580664f4763Szrj 		    additional->permitopen, additional->npermitopen) != 0)
581664f4763Szrj 			goto alloc_fail;
582664f4763Szrj 	}
583664f4763Szrj 
584664f4763Szrj 	if (primary->npermitlisten > 0) {
585664f4763Szrj 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
586664f4763Szrj 		    primary->permitlisten, primary->npermitlisten) != 0)
587664f4763Szrj 			goto alloc_fail;
588664f4763Szrj 	} else if (additional->npermitlisten > 0) {
589664f4763Szrj 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
590664f4763Szrj 		    additional->permitlisten, additional->npermitlisten) != 0)
591664f4763Szrj 			goto alloc_fail;
592664f4763Szrj 	}
593664f4763Szrj 
5940cbfa66cSDaniel Fojt #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
595*50a69bb5SSascha Wildner #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
5960cbfa66cSDaniel Fojt 	/* Permissive flags are logical-AND (i.e. must be set in both) */
5970cbfa66cSDaniel Fojt 	OPTFLAG_AND(permit_port_forwarding_flag);
5980cbfa66cSDaniel Fojt 	OPTFLAG_AND(permit_agent_forwarding_flag);
5990cbfa66cSDaniel Fojt 	OPTFLAG_AND(permit_x11_forwarding_flag);
6000cbfa66cSDaniel Fojt 	OPTFLAG_AND(permit_pty_flag);
6010cbfa66cSDaniel Fojt 	OPTFLAG_AND(permit_user_rc);
6020cbfa66cSDaniel Fojt 	OPTFLAG_AND(no_require_user_presence);
603*50a69bb5SSascha Wildner 	/* Restrictive flags are logical-OR (i.e. must be set in either) */
604*50a69bb5SSascha Wildner 	OPTFLAG_OR(require_verify);
6050cbfa66cSDaniel Fojt #undef OPTFLAG_AND
606664f4763Szrj 
607664f4763Szrj 	/* Earliest expiry time should win */
608664f4763Szrj 	if (primary->valid_before != 0)
609664f4763Szrj 		ret->valid_before = primary->valid_before;
610664f4763Szrj 	if (additional->valid_before != 0 &&
611664f4763Szrj 	    additional->valid_before < ret->valid_before)
612664f4763Szrj 		ret->valid_before = additional->valid_before;
613664f4763Szrj 
614664f4763Szrj 	/*
615664f4763Szrj 	 * When both multiple forced-command are specified, only
616664f4763Szrj 	 * proceed if they are identical, otherwise fail.
617664f4763Szrj 	 */
618664f4763Szrj 	if (primary->force_command != NULL &&
619664f4763Szrj 	    additional->force_command != NULL) {
620664f4763Szrj 		if (strcmp(primary->force_command,
621664f4763Szrj 		    additional->force_command) == 0) {
622664f4763Szrj 			/* ok */
623664f4763Szrj 			ret->force_command = strdup(primary->force_command);
624664f4763Szrj 			if (ret->force_command == NULL)
625664f4763Szrj 				goto alloc_fail;
626664f4763Szrj 		} else {
627664f4763Szrj 			errstr = "forced command options do not match";
628664f4763Szrj 			goto fail;
629664f4763Szrj 		}
630664f4763Szrj 	} else if (primary->force_command != NULL) {
631664f4763Szrj 		if ((ret->force_command = strdup(
632664f4763Szrj 		    primary->force_command)) == NULL)
633664f4763Szrj 			goto alloc_fail;
634664f4763Szrj 	} else if (additional->force_command != NULL) {
635664f4763Szrj 		if ((ret->force_command = strdup(
636664f4763Szrj 		    additional->force_command)) == NULL)
637664f4763Szrj 			goto alloc_fail;
638664f4763Szrj 	}
639664f4763Szrj 	/* success */
640664f4763Szrj 	if (errstrp != NULL)
641664f4763Szrj 		*errstrp = NULL;
642664f4763Szrj 	return ret;
643664f4763Szrj 
644664f4763Szrj  alloc_fail:
645664f4763Szrj 	errstr = "memory allocation failed";
646664f4763Szrj  fail:
647664f4763Szrj 	if (errstrp != NULL)
648664f4763Szrj 		*errstrp = errstr;
649664f4763Szrj 	sshauthopt_free(ret);
650664f4763Szrj 	return NULL;
651664f4763Szrj }
652664f4763Szrj 
653664f4763Szrj /*
654664f4763Szrj  * Copy options
655664f4763Szrj  */
656664f4763Szrj struct sshauthopt *
657664f4763Szrj sshauthopt_copy(const struct sshauthopt *orig)
658664f4763Szrj {
659664f4763Szrj 	struct sshauthopt *ret;
660664f4763Szrj 
661664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
662664f4763Szrj 		return NULL;
663664f4763Szrj 
664664f4763Szrj #define OPTSCALAR(x) ret->x = orig->x
665664f4763Szrj 	OPTSCALAR(permit_port_forwarding_flag);
666664f4763Szrj 	OPTSCALAR(permit_agent_forwarding_flag);
667664f4763Szrj 	OPTSCALAR(permit_x11_forwarding_flag);
668664f4763Szrj 	OPTSCALAR(permit_pty_flag);
669664f4763Szrj 	OPTSCALAR(permit_user_rc);
670664f4763Szrj 	OPTSCALAR(restricted);
671664f4763Szrj 	OPTSCALAR(cert_authority);
672664f4763Szrj 	OPTSCALAR(force_tun_device);
673664f4763Szrj 	OPTSCALAR(valid_before);
6740cbfa66cSDaniel Fojt 	OPTSCALAR(no_require_user_presence);
675*50a69bb5SSascha Wildner 	OPTSCALAR(require_verify);
676664f4763Szrj #undef OPTSCALAR
677664f4763Szrj #define OPTSTRING(x) \
678664f4763Szrj 	do { \
679664f4763Szrj 		if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
680664f4763Szrj 			sshauthopt_free(ret); \
681664f4763Szrj 			return NULL; \
682664f4763Szrj 		} \
683664f4763Szrj 	} while (0)
684664f4763Szrj 	OPTSTRING(cert_principals);
685664f4763Szrj 	OPTSTRING(force_command);
686664f4763Szrj 	OPTSTRING(required_from_host_cert);
687664f4763Szrj 	OPTSTRING(required_from_host_keys);
688664f4763Szrj #undef OPTSTRING
689664f4763Szrj 
690664f4763Szrj 	if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
691664f4763Szrj 	    dup_strings(&ret->permitopen, &ret->npermitopen,
692664f4763Szrj 	    orig->permitopen, orig->npermitopen) != 0 ||
693664f4763Szrj 	    dup_strings(&ret->permitlisten, &ret->npermitlisten,
694664f4763Szrj 	    orig->permitlisten, orig->npermitlisten) != 0) {
695664f4763Szrj 		sshauthopt_free(ret);
696664f4763Szrj 		return NULL;
697664f4763Szrj 	}
698664f4763Szrj 	return ret;
699664f4763Szrj }
700664f4763Szrj 
701664f4763Szrj static int
702664f4763Szrj serialise_array(struct sshbuf *m, char **a, size_t n)
703664f4763Szrj {
704664f4763Szrj 	struct sshbuf *b;
705664f4763Szrj 	size_t i;
706664f4763Szrj 	int r;
707664f4763Szrj 
708664f4763Szrj 	if (n > INT_MAX)
709664f4763Szrj 		return SSH_ERR_INTERNAL_ERROR;
710664f4763Szrj 
711664f4763Szrj 	if ((b = sshbuf_new()) == NULL) {
712664f4763Szrj 		return SSH_ERR_ALLOC_FAIL;
713664f4763Szrj 	}
714664f4763Szrj 	for (i = 0; i < n; i++) {
715664f4763Szrj 		if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
716664f4763Szrj 			sshbuf_free(b);
717664f4763Szrj 			return r;
718664f4763Szrj 		}
719664f4763Szrj 	}
720664f4763Szrj 	if ((r = sshbuf_put_u32(m, n)) != 0 ||
721664f4763Szrj 	    (r = sshbuf_put_stringb(m, b)) != 0) {
722664f4763Szrj 		sshbuf_free(b);
723664f4763Szrj 		return r;
724664f4763Szrj 	}
725664f4763Szrj 	/* success */
726664f4763Szrj 	return 0;
727664f4763Szrj }
728664f4763Szrj 
729664f4763Szrj static int
730664f4763Szrj deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
731664f4763Szrj {
732664f4763Szrj 	char **a = NULL;
733664f4763Szrj 	size_t i, n = 0;
734664f4763Szrj 	struct sshbuf *b = NULL;
735664f4763Szrj 	u_int tmp;
736664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
737664f4763Szrj 
738664f4763Szrj 	if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
739664f4763Szrj 	    (r = sshbuf_froms(m, &b)) != 0)
740664f4763Szrj 		goto out;
741664f4763Szrj 	if (tmp > INT_MAX) {
742664f4763Szrj 		r = SSH_ERR_INVALID_FORMAT;
743664f4763Szrj 		goto out;
744664f4763Szrj 	}
745664f4763Szrj 	n = tmp;
746664f4763Szrj 	if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
747664f4763Szrj 		r = SSH_ERR_ALLOC_FAIL;
748664f4763Szrj 		goto out;
749664f4763Szrj 	}
750664f4763Szrj 	for (i = 0; i < n; i++) {
751664f4763Szrj 		if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
752664f4763Szrj 			goto out;
753664f4763Szrj 	}
754664f4763Szrj 	/* success */
755664f4763Szrj 	r = 0;
756664f4763Szrj 	*ap = a;
757664f4763Szrj 	a = NULL;
758664f4763Szrj 	*np = n;
759664f4763Szrj 	n = 0;
760664f4763Szrj  out:
7610cbfa66cSDaniel Fojt 	if (a != NULL) {
762664f4763Szrj 		for (i = 0; i < n; i++)
763664f4763Szrj 			free(a[i]);
764664f4763Szrj 		free(a);
7650cbfa66cSDaniel Fojt 	}
766664f4763Szrj 	sshbuf_free(b);
767664f4763Szrj 	return r;
768664f4763Szrj }
769664f4763Szrj 
770664f4763Szrj static int
771664f4763Szrj serialise_nullable_string(struct sshbuf *m, const char *s)
772664f4763Szrj {
773664f4763Szrj 	int r;
774664f4763Szrj 
775664f4763Szrj 	if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
776664f4763Szrj 	    (r = sshbuf_put_cstring(m, s)) != 0)
777664f4763Szrj 		return r;
778664f4763Szrj 	return 0;
779664f4763Szrj }
780664f4763Szrj 
781664f4763Szrj static int
782664f4763Szrj deserialise_nullable_string(struct sshbuf *m, char **sp)
783664f4763Szrj {
784664f4763Szrj 	int r;
785664f4763Szrj 	u_char flag;
786664f4763Szrj 
787664f4763Szrj 	*sp = NULL;
788664f4763Szrj 	if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
789664f4763Szrj 	    (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
790664f4763Szrj 		return r;
791664f4763Szrj 	return 0;
792664f4763Szrj }
793664f4763Szrj 
794664f4763Szrj int
795664f4763Szrj sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
796664f4763Szrj     int untrusted)
797664f4763Szrj {
798664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
799664f4763Szrj 
8000cbfa66cSDaniel Fojt 	/* Flag options */
801664f4763Szrj 	if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
802664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
803664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
804664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
805664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
806664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
807664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
808*50a69bb5SSascha Wildner 	    (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
809*50a69bb5SSascha Wildner 	    (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
8100cbfa66cSDaniel Fojt 		return r;
8110cbfa66cSDaniel Fojt 
8120cbfa66cSDaniel Fojt 	/* Simple integer options */
8130cbfa66cSDaniel Fojt 	if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
814664f4763Szrj 		return r;
815664f4763Szrj 
816664f4763Szrj 	/* tunnel number can be negative to indicate "unset" */
817664f4763Szrj 	if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
818664f4763Szrj 	    (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
819664f4763Szrj 	    0 : (u_int)opts->force_tun_device)) != 0)
820664f4763Szrj 		return r;
821664f4763Szrj 
822664f4763Szrj 	/* String options; these may be NULL */
823664f4763Szrj 	if ((r = serialise_nullable_string(m,
824664f4763Szrj 	    untrusted ? "yes" : opts->cert_principals)) != 0 ||
825664f4763Szrj 	    (r = serialise_nullable_string(m,
826664f4763Szrj 	    untrusted ? "true" : opts->force_command)) != 0 ||
827664f4763Szrj 	    (r = serialise_nullable_string(m,
828664f4763Szrj 	    untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
829664f4763Szrj 	    (r = serialise_nullable_string(m,
830664f4763Szrj 	    untrusted ? NULL : opts->required_from_host_keys)) != 0)
831664f4763Szrj 		return r;
832664f4763Szrj 
833664f4763Szrj 	/* Array options */
834664f4763Szrj 	if ((r = serialise_array(m, opts->env,
835664f4763Szrj 	    untrusted ? 0 : opts->nenv)) != 0 ||
836664f4763Szrj 	    (r = serialise_array(m, opts->permitopen,
837664f4763Szrj 	    untrusted ? 0 : opts->npermitopen)) != 0 ||
838664f4763Szrj 	    (r = serialise_array(m, opts->permitlisten,
839664f4763Szrj 	    untrusted ? 0 : opts->npermitlisten)) != 0)
840664f4763Szrj 		return r;
841664f4763Szrj 
842664f4763Szrj 	/* success */
843664f4763Szrj 	return 0;
844664f4763Szrj }
845664f4763Szrj 
846664f4763Szrj int
847664f4763Szrj sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
848664f4763Szrj {
849664f4763Szrj 	struct sshauthopt *opts = NULL;
850664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
851664f4763Szrj 	u_char f;
852664f4763Szrj 	u_int tmp;
853664f4763Szrj 
854664f4763Szrj 	if ((opts = calloc(1, sizeof(*opts))) == NULL)
855664f4763Szrj 		return SSH_ERR_ALLOC_FAIL;
856664f4763Szrj 
8570cbfa66cSDaniel Fojt 	/* Flag options */
858664f4763Szrj #define OPT_FLAG(x) \
859664f4763Szrj 	do { \
860664f4763Szrj 		if ((r = sshbuf_get_u8(m, &f)) != 0) \
861664f4763Szrj 			goto out; \
862664f4763Szrj 		opts->x = f; \
863664f4763Szrj 	} while (0)
864664f4763Szrj 	OPT_FLAG(permit_port_forwarding_flag);
865664f4763Szrj 	OPT_FLAG(permit_agent_forwarding_flag);
866664f4763Szrj 	OPT_FLAG(permit_x11_forwarding_flag);
867664f4763Szrj 	OPT_FLAG(permit_pty_flag);
868664f4763Szrj 	OPT_FLAG(permit_user_rc);
869664f4763Szrj 	OPT_FLAG(restricted);
870664f4763Szrj 	OPT_FLAG(cert_authority);
8710cbfa66cSDaniel Fojt 	OPT_FLAG(no_require_user_presence);
872*50a69bb5SSascha Wildner 	OPT_FLAG(require_verify);
873664f4763Szrj #undef OPT_FLAG
874664f4763Szrj 
8750cbfa66cSDaniel Fojt 	/* Simple integer options */
876664f4763Szrj 	if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
877664f4763Szrj 		goto out;
878664f4763Szrj 
879664f4763Szrj 	/* tunnel number can be negative to indicate "unset" */
880664f4763Szrj 	if ((r = sshbuf_get_u8(m, &f)) != 0 ||
881664f4763Szrj 	    (r = sshbuf_get_u32(m, &tmp)) != 0)
882664f4763Szrj 		goto out;
883664f4763Szrj 	opts->force_tun_device = f ? -1 : (int)tmp;
884664f4763Szrj 
885664f4763Szrj 	/* String options may be NULL */
886664f4763Szrj 	if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
887664f4763Szrj 	    (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
888664f4763Szrj 	    (r = deserialise_nullable_string(m,
889664f4763Szrj 	    &opts->required_from_host_cert)) != 0 ||
890664f4763Szrj 	    (r = deserialise_nullable_string(m,
891664f4763Szrj 	    &opts->required_from_host_keys)) != 0)
892664f4763Szrj 		goto out;
893664f4763Szrj 
894664f4763Szrj 	/* Array options */
895664f4763Szrj 	if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
896664f4763Szrj 	    (r = deserialise_array(m,
897664f4763Szrj 	    &opts->permitopen, &opts->npermitopen)) != 0 ||
898664f4763Szrj 	    (r = deserialise_array(m,
899664f4763Szrj 	    &opts->permitlisten, &opts->npermitlisten)) != 0)
900664f4763Szrj 		goto out;
901664f4763Szrj 
902664f4763Szrj 	/* success */
903664f4763Szrj 	r = 0;
904664f4763Szrj 	*optsp = opts;
905664f4763Szrj 	opts = NULL;
906664f4763Szrj  out:
907664f4763Szrj 	sshauthopt_free(opts);
908664f4763Szrj 	return r;
909664f4763Szrj }
910