xref: /dflybsd-src/crypto/openssh/auth-options.c (revision 664f47636b7e6e9e2c54a4799ca4884a9c628df5)
1*664f4763Szrj /* $OpenBSD: auth-options.c,v 1.84 2018/10/03 06:38:35 djm Exp $ */
218de8d7fSPeter Avalos /*
3*664f4763Szrj  * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
4*664f4763Szrj  *
5*664f4763Szrj  * Permission to use, copy, modify, and distribute this software for any
6*664f4763Szrj  * purpose with or without fee is hereby granted, provided that the above
7*664f4763Szrj  * copyright notice and this permission notice appear in all copies.
8*664f4763Szrj  *
9*664f4763Szrj  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10*664f4763Szrj  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*664f4763Szrj  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12*664f4763Szrj  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*664f4763Szrj  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*664f4763Szrj  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15*664f4763Szrj  * 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 
2218de8d7fSPeter Avalos #include <netdb.h>
2318de8d7fSPeter Avalos #include <pwd.h>
2418de8d7fSPeter Avalos #include <string.h>
2518de8d7fSPeter Avalos #include <stdio.h>
2618de8d7fSPeter Avalos #include <stdarg.h>
27*664f4763Szrj #include <ctype.h>
28*664f4763Szrj #include <limits.h>
2918de8d7fSPeter Avalos 
3018de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h"
31e9778795SPeter Avalos 
3218de8d7fSPeter Avalos #include "xmalloc.h"
33e9778795SPeter Avalos #include "ssherr.h"
3418de8d7fSPeter Avalos #include "log.h"
35e9778795SPeter Avalos #include "sshbuf.h"
3636e94dc5SPeter Avalos #include "misc.h"
37e9778795SPeter Avalos #include "sshkey.h"
38*664f4763Szrj #include "match.h"
39*664f4763Szrj #include "ssh2.h"
40856ea928SPeter Avalos #include "auth-options.h"
4118de8d7fSPeter Avalos 
4218de8d7fSPeter Avalos /*
43e9778795SPeter Avalos  * Match flag 'opt' in *optsp, and if allow_negate is set then also match
44e9778795SPeter Avalos  * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
45e9778795SPeter Avalos  * if negated option matches.
46e9778795SPeter Avalos  * If the option or negated option matches, then *optsp is updated to
47*664f4763Szrj  * point to the first character after the option.
48e9778795SPeter Avalos  */
49e9778795SPeter Avalos static int
50*664f4763Szrj opt_flag(const char *opt, int allow_negate, const char **optsp)
51e9778795SPeter Avalos {
52e9778795SPeter Avalos 	size_t opt_len = strlen(opt);
53*664f4763Szrj 	const char *opts = *optsp;
54e9778795SPeter Avalos 	int negate = 0;
55e9778795SPeter Avalos 
56e9778795SPeter Avalos 	if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
57e9778795SPeter Avalos 		opts += 3;
58e9778795SPeter Avalos 		negate = 1;
59e9778795SPeter Avalos 	}
60e9778795SPeter Avalos 	if (strncasecmp(opts, opt, opt_len) == 0) {
61e9778795SPeter Avalos 		*optsp = opts + opt_len;
62e9778795SPeter Avalos 		return negate ? 0 : 1;
63e9778795SPeter Avalos 	}
64e9778795SPeter Avalos 	return -1;
65e9778795SPeter Avalos }
66e9778795SPeter Avalos 
67*664f4763Szrj static char *
68*664f4763Szrj opt_dequote(const char **sp, const char **errstrp)
6918de8d7fSPeter Avalos {
70*664f4763Szrj 	const char *s = *sp;
71*664f4763Szrj 	char *ret;
72*664f4763Szrj 	size_t i;
7318de8d7fSPeter Avalos 
74*664f4763Szrj 	*errstrp = NULL;
75*664f4763Szrj 	if (*s != '"') {
76*664f4763Szrj 		*errstrp = "missing start quote";
77*664f4763Szrj 		return NULL;
78*664f4763Szrj 	}
79*664f4763Szrj 	s++;
80*664f4763Szrj 	if ((ret = malloc(strlen((s)) + 1)) == NULL) {
81*664f4763Szrj 		*errstrp = "memory allocation failed";
82*664f4763Szrj 		return NULL;
83*664f4763Szrj 	}
84*664f4763Szrj 	for (i = 0; *s != '\0' && *s != '"';) {
85*664f4763Szrj 		if (s[0] == '\\' && s[1] == '"')
86*664f4763Szrj 			s++;
87*664f4763Szrj 		ret[i++] = *s++;
88*664f4763Szrj 	}
89*664f4763Szrj 	if (*s == '\0') {
90*664f4763Szrj 		*errstrp = "missing end quote";
91*664f4763Szrj 		free(ret);
92*664f4763Szrj 		return NULL;
93*664f4763Szrj 	}
94*664f4763Szrj 	ret[i] = '\0';
95*664f4763Szrj 	s++;
96*664f4763Szrj 	*sp = s;
97*664f4763Szrj 	return ret;
98*664f4763Szrj }
9918de8d7fSPeter Avalos 
100*664f4763Szrj static int
101*664f4763Szrj opt_match(const char **opts, const char *term)
102*664f4763Szrj {
103*664f4763Szrj 	if (strncasecmp((*opts), term, strlen(term)) == 0 &&
104*664f4763Szrj 	    (*opts)[strlen(term)] == '=') {
105*664f4763Szrj 		*opts += strlen(term) + 1;
10618de8d7fSPeter Avalos 		return 1;
107856ea928SPeter Avalos 	}
10818de8d7fSPeter Avalos 	return 0;
10918de8d7fSPeter Avalos }
11018de8d7fSPeter Avalos 
111*664f4763Szrj static int
112*664f4763Szrj dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
113*664f4763Szrj {
114*664f4763Szrj 	char **dst;
115*664f4763Szrj 	size_t i, j;
11618de8d7fSPeter Avalos 
117*664f4763Szrj 	*dstp = NULL;
118*664f4763Szrj 	*ndstp = 0;
119*664f4763Szrj 	if (nsrc == 0)
120*664f4763Szrj 		return 0;
12118de8d7fSPeter Avalos 
122*664f4763Szrj 	if ((dst = calloc(nsrc, sizeof(*src))) == NULL)
123*664f4763Szrj 		return -1;
124*664f4763Szrj 	for (i = 0; i < nsrc; i++) {
125*664f4763Szrj 		if ((dst[i] = strdup(src[i])) == NULL) {
126*664f4763Szrj 			for (j = 0; j < i; j++)
127*664f4763Szrj 				free(dst[j]);
128*664f4763Szrj 			free(dst);
129*664f4763Szrj 			return -1;
130*664f4763Szrj 		}
131*664f4763Szrj 	}
132*664f4763Szrj 	/* success */
133*664f4763Szrj 	*dstp = dst;
134*664f4763Szrj 	*ndstp = nsrc;
13518de8d7fSPeter Avalos 	return 0;
13618de8d7fSPeter Avalos }
137856ea928SPeter Avalos 
138856ea928SPeter Avalos #define OPTIONS_CRITICAL	1
139856ea928SPeter Avalos #define OPTIONS_EXTENSIONS	2
140856ea928SPeter Avalos static int
141*664f4763Szrj cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
142*664f4763Szrj     u_int which, int crit)
143856ea928SPeter Avalos {
144856ea928SPeter Avalos 	char *command, *allowed;
14536e94dc5SPeter Avalos 	char *name = NULL;
146e9778795SPeter Avalos 	struct sshbuf *c = NULL, *data = NULL;
147*664f4763Szrj 	int r, ret = -1, found;
148856ea928SPeter Avalos 
149e9778795SPeter Avalos 	if ((c = sshbuf_fromb(oblob)) == NULL) {
150e9778795SPeter Avalos 		error("%s: sshbuf_fromb failed", __func__);
151856ea928SPeter Avalos 		goto out;
152856ea928SPeter Avalos 	}
153e9778795SPeter Avalos 
154e9778795SPeter Avalos 	while (sshbuf_len(c) > 0) {
155e9778795SPeter Avalos 		sshbuf_free(data);
156e9778795SPeter Avalos 		data = NULL;
157e9778795SPeter Avalos 		if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
158e9778795SPeter Avalos 		    (r = sshbuf_froms(c, &data)) != 0) {
159e9778795SPeter Avalos 			error("Unable to parse certificate options: %s",
160e9778795SPeter Avalos 			    ssh_err(r));
161e9778795SPeter Avalos 			goto out;
162e9778795SPeter Avalos 		}
163e9778795SPeter Avalos 		debug3("found certificate option \"%.100s\" len %zu",
164e9778795SPeter Avalos 		    name, sshbuf_len(data));
165856ea928SPeter Avalos 		found = 0;
166856ea928SPeter Avalos 		if ((which & OPTIONS_EXTENSIONS) != 0) {
167856ea928SPeter Avalos 			if (strcmp(name, "permit-X11-forwarding") == 0) {
168*664f4763Szrj 				opts->permit_x11_forwarding_flag = 1;
169856ea928SPeter Avalos 				found = 1;
170856ea928SPeter Avalos 			} else if (strcmp(name,
171856ea928SPeter Avalos 			    "permit-agent-forwarding") == 0) {
172*664f4763Szrj 				opts->permit_agent_forwarding_flag = 1;
173856ea928SPeter Avalos 				found = 1;
174856ea928SPeter Avalos 			} else if (strcmp(name,
175856ea928SPeter Avalos 			    "permit-port-forwarding") == 0) {
176*664f4763Szrj 				opts->permit_port_forwarding_flag = 1;
177856ea928SPeter Avalos 				found = 1;
178856ea928SPeter Avalos 			} else if (strcmp(name, "permit-pty") == 0) {
179*664f4763Szrj 				opts->permit_pty_flag = 1;
180856ea928SPeter Avalos 				found = 1;
181856ea928SPeter Avalos 			} else if (strcmp(name, "permit-user-rc") == 0) {
182*664f4763Szrj 				opts->permit_user_rc = 1;
183856ea928SPeter Avalos 				found = 1;
184856ea928SPeter Avalos 			}
185856ea928SPeter Avalos 		}
186856ea928SPeter Avalos 		if (!found && (which & OPTIONS_CRITICAL) != 0) {
187856ea928SPeter Avalos 			if (strcmp(name, "force-command") == 0) {
188e9778795SPeter Avalos 				if ((r = sshbuf_get_cstring(data, &command,
189e9778795SPeter Avalos 				    NULL)) != 0) {
190e9778795SPeter Avalos 					error("Unable to parse \"%s\" "
191e9778795SPeter Avalos 					    "section: %s", name, ssh_err(r));
192856ea928SPeter Avalos 					goto out;
193856ea928SPeter Avalos 				}
194*664f4763Szrj 				if (opts->force_command != NULL) {
195856ea928SPeter Avalos 					error("Certificate has multiple "
196856ea928SPeter Avalos 					    "force-command options");
19736e94dc5SPeter Avalos 					free(command);
198856ea928SPeter Avalos 					goto out;
199856ea928SPeter Avalos 				}
200*664f4763Szrj 				opts->force_command = command;
201856ea928SPeter Avalos 				found = 1;
202856ea928SPeter Avalos 			}
203856ea928SPeter Avalos 			if (strcmp(name, "source-address") == 0) {
204e9778795SPeter Avalos 				if ((r = sshbuf_get_cstring(data, &allowed,
205e9778795SPeter Avalos 				    NULL)) != 0) {
206e9778795SPeter Avalos 					error("Unable to parse \"%s\" "
207e9778795SPeter Avalos 					    "section: %s", name, ssh_err(r));
208856ea928SPeter Avalos 					goto out;
209856ea928SPeter Avalos 				}
210*664f4763Szrj 				if (opts->required_from_host_cert != NULL) {
211856ea928SPeter Avalos 					error("Certificate has multiple "
212856ea928SPeter Avalos 					    "source-address options");
21336e94dc5SPeter Avalos 					free(allowed);
214856ea928SPeter Avalos 					goto out;
215856ea928SPeter Avalos 				}
216*664f4763Szrj 				/* Check syntax */
217*664f4763Szrj 				if (addr_match_cidr_list(NULL, allowed) == -1) {
218856ea928SPeter Avalos 					error("Certificate source-address "
219856ea928SPeter Avalos 					    "contents invalid");
220856ea928SPeter Avalos 					goto out;
221856ea928SPeter Avalos 				}
222*664f4763Szrj 				opts->required_from_host_cert = allowed;
223856ea928SPeter Avalos 				found = 1;
224856ea928SPeter Avalos 			}
225856ea928SPeter Avalos 		}
226856ea928SPeter Avalos 
227856ea928SPeter Avalos 		if (!found) {
228856ea928SPeter Avalos 			if (crit) {
229856ea928SPeter Avalos 				error("Certificate critical option \"%s\" "
230856ea928SPeter Avalos 				    "is not supported", name);
231856ea928SPeter Avalos 				goto out;
232856ea928SPeter Avalos 			} else {
233856ea928SPeter Avalos 				logit("Certificate extension \"%s\" "
234856ea928SPeter Avalos 				    "is not supported", name);
235856ea928SPeter Avalos 			}
236e9778795SPeter Avalos 		} else if (sshbuf_len(data) != 0) {
237856ea928SPeter Avalos 			error("Certificate option \"%s\" corrupt "
238856ea928SPeter Avalos 			    "(extra data)", name);
239856ea928SPeter Avalos 			goto out;
240856ea928SPeter Avalos 		}
24136e94dc5SPeter Avalos 		free(name);
24236e94dc5SPeter Avalos 		name = NULL;
243856ea928SPeter Avalos 	}
244856ea928SPeter Avalos 	/* successfully parsed all options */
245856ea928SPeter Avalos 	ret = 0;
246856ea928SPeter Avalos 
247856ea928SPeter Avalos  out:
24836e94dc5SPeter Avalos 	free(name);
249e9778795SPeter Avalos 	sshbuf_free(data);
250e9778795SPeter Avalos 	sshbuf_free(c);
251856ea928SPeter Avalos 	return ret;
252856ea928SPeter Avalos }
253856ea928SPeter Avalos 
254*664f4763Szrj struct sshauthopt *
255*664f4763Szrj sshauthopt_new(void)
256856ea928SPeter Avalos {
257*664f4763Szrj 	struct sshauthopt *ret;
258856ea928SPeter Avalos 
259*664f4763Szrj 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
260*664f4763Szrj 		return NULL;
261*664f4763Szrj 	ret->force_tun_device = -1;
262*664f4763Szrj 	return ret;
263*664f4763Szrj }
264ce74bacaSMatthew Dillon 
265*664f4763Szrj void
266*664f4763Szrj sshauthopt_free(struct sshauthopt *opts)
267*664f4763Szrj {
268*664f4763Szrj 	size_t i;
269856ea928SPeter Avalos 
270*664f4763Szrj 	if (opts == NULL)
271*664f4763Szrj 		return;
272*664f4763Szrj 
273*664f4763Szrj 	free(opts->cert_principals);
274*664f4763Szrj 	free(opts->force_command);
275*664f4763Szrj 	free(opts->required_from_host_cert);
276*664f4763Szrj 	free(opts->required_from_host_keys);
277*664f4763Szrj 
278*664f4763Szrj 	for (i = 0; i < opts->nenv; i++)
279*664f4763Szrj 		free(opts->env[i]);
280*664f4763Szrj 	free(opts->env);
281*664f4763Szrj 
282*664f4763Szrj 	for (i = 0; i < opts->npermitopen; i++)
283*664f4763Szrj 		free(opts->permitopen[i]);
284*664f4763Szrj 	free(opts->permitopen);
285*664f4763Szrj 
286*664f4763Szrj 	for (i = 0; i < opts->npermitlisten; i++)
287*664f4763Szrj 		free(opts->permitlisten[i]);
288*664f4763Szrj 	free(opts->permitlisten);
289*664f4763Szrj 
290*664f4763Szrj 	explicit_bzero(opts, sizeof(*opts));
291*664f4763Szrj 	free(opts);
292*664f4763Szrj }
293*664f4763Szrj 
294*664f4763Szrj struct sshauthopt *
295*664f4763Szrj sshauthopt_new_with_keys_defaults(void)
296*664f4763Szrj {
297*664f4763Szrj 	struct sshauthopt *ret = NULL;
298*664f4763Szrj 
299*664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
300*664f4763Szrj 		return NULL;
301*664f4763Szrj 
302*664f4763Szrj 	/* Defaults for authorized_keys flags */
303*664f4763Szrj 	ret->permit_port_forwarding_flag = 1;
304*664f4763Szrj 	ret->permit_agent_forwarding_flag = 1;
305*664f4763Szrj 	ret->permit_x11_forwarding_flag = 1;
306*664f4763Szrj 	ret->permit_pty_flag = 1;
307*664f4763Szrj 	ret->permit_user_rc = 1;
308*664f4763Szrj 	return ret;
309*664f4763Szrj }
310*664f4763Szrj 
311ce74bacaSMatthew Dillon /*
312*664f4763Szrj  * Parse and record a permitopen/permitlisten directive.
313*664f4763Szrj  * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
314ce74bacaSMatthew Dillon  */
315*664f4763Szrj static int
316*664f4763Szrj handle_permit(const char **optsp, int allow_bare_port,
317*664f4763Szrj     char ***permitsp, size_t *npermitsp, const char **errstrp)
318*664f4763Szrj {
319*664f4763Szrj 	char *opt, *tmp, *cp, *host, **permits = *permitsp;
320*664f4763Szrj 	size_t npermits = *npermitsp;
321*664f4763Szrj 	const char *errstr = "unknown error";
322*664f4763Szrj 
323*664f4763Szrj 	if (npermits > INT_MAX) {
324*664f4763Szrj 		*errstrp = "too many permission directives";
325ce74bacaSMatthew Dillon 		return -1;
326856ea928SPeter Avalos 	}
327*664f4763Szrj 	if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
328*664f4763Szrj 		return -1;
329*664f4763Szrj 	}
330*664f4763Szrj 	if (allow_bare_port && strchr(opt, ':') == NULL) {
331*664f4763Szrj 		/*
332*664f4763Szrj 		 * Allow a bare port number in permitlisten to indicate a
333*664f4763Szrj 		 * listen_host wildcard.
334*664f4763Szrj 		 */
335*664f4763Szrj 		if (asprintf(&tmp, "*:%s", opt) < 0) {
336*664f4763Szrj 			*errstrp = "memory allocation failed";
337*664f4763Szrj 			return -1;
338*664f4763Szrj 		}
339*664f4763Szrj 		free(opt);
340*664f4763Szrj 		opt = tmp;
341*664f4763Szrj 	}
342*664f4763Szrj 	if ((tmp = strdup(opt)) == NULL) {
343*664f4763Szrj 		free(opt);
344*664f4763Szrj 		*errstrp = "memory allocation failed";
345*664f4763Szrj 		return -1;
346*664f4763Szrj 	}
347*664f4763Szrj 	cp = tmp;
348*664f4763Szrj 	/* validate syntax before recording it. */
349*664f4763Szrj 	host = hpdelim(&cp);
350*664f4763Szrj 	if (host == NULL || strlen(host) >= NI_MAXHOST) {
351*664f4763Szrj 		free(tmp);
352*664f4763Szrj 		free(opt);
353*664f4763Szrj 		*errstrp = "invalid permission hostname";
354*664f4763Szrj 		return -1;
355*664f4763Szrj 	}
356*664f4763Szrj 	/*
357*664f4763Szrj 	 * don't want to use permitopen_port to avoid
358*664f4763Szrj 	 * dependency on channels.[ch] here.
359*664f4763Szrj 	 */
360*664f4763Szrj 	if (cp == NULL ||
361*664f4763Szrj 	    (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
362*664f4763Szrj 		free(tmp);
363*664f4763Szrj 		free(opt);
364*664f4763Szrj 		*errstrp = "invalid permission port";
365*664f4763Szrj 		return -1;
366*664f4763Szrj 	}
367*664f4763Szrj 	/* XXX - add streamlocal support */
368*664f4763Szrj 	free(tmp);
369*664f4763Szrj 	/* Record it */
370*664f4763Szrj 	if ((permits = recallocarray(permits, npermits, npermits + 1,
371*664f4763Szrj 	    sizeof(*permits))) == NULL) {
372*664f4763Szrj 		free(opt);
373*664f4763Szrj 		/* NB. don't update *permitsp if alloc fails */
374*664f4763Szrj 		*errstrp = "memory allocation failed";
375*664f4763Szrj 		return -1;
376*664f4763Szrj 	}
377*664f4763Szrj 	permits[npermits++] = opt;
378*664f4763Szrj 	*permitsp = permits;
379*664f4763Szrj 	*npermitsp = npermits;
380856ea928SPeter Avalos 	return 0;
381856ea928SPeter Avalos }
382856ea928SPeter Avalos 
383*664f4763Szrj struct sshauthopt *
384*664f4763Szrj sshauthopt_parse(const char *opts, const char **errstrp)
385*664f4763Szrj {
386*664f4763Szrj 	char **oarray, *opt, *cp, *tmp;
387*664f4763Szrj 	int r;
388*664f4763Szrj 	struct sshauthopt *ret = NULL;
389*664f4763Szrj 	const char *errstr = "unknown error";
390*664f4763Szrj 	uint64_t valid_before;
391*664f4763Szrj 
392*664f4763Szrj 	if (errstrp != NULL)
393*664f4763Szrj 		*errstrp = NULL;
394*664f4763Szrj 	if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
395*664f4763Szrj 		goto alloc_fail;
396*664f4763Szrj 
397*664f4763Szrj 	if (opts == NULL)
398*664f4763Szrj 		return ret;
399*664f4763Szrj 
400*664f4763Szrj 	while (*opts && *opts != ' ' && *opts != '\t') {
401*664f4763Szrj 		/* flag options */
402*664f4763Szrj 		if ((r = opt_flag("restrict", 0, &opts)) != -1) {
403*664f4763Szrj 			ret->restricted = 1;
404*664f4763Szrj 			ret->permit_port_forwarding_flag = 0;
405*664f4763Szrj 			ret->permit_agent_forwarding_flag = 0;
406*664f4763Szrj 			ret->permit_x11_forwarding_flag = 0;
407*664f4763Szrj 			ret->permit_pty_flag = 0;
408*664f4763Szrj 			ret->permit_user_rc = 0;
409*664f4763Szrj 		} else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
410*664f4763Szrj 			ret->cert_authority = r;
411*664f4763Szrj 		} else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
412*664f4763Szrj 			ret->permit_port_forwarding_flag = r == 1;
413*664f4763Szrj 		} else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
414*664f4763Szrj 			ret->permit_agent_forwarding_flag = r == 1;
415*664f4763Szrj 		} else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
416*664f4763Szrj 			ret->permit_x11_forwarding_flag = r == 1;
417*664f4763Szrj 		} else if ((r = opt_flag("pty", 1, &opts)) != -1) {
418*664f4763Szrj 			ret->permit_pty_flag = r == 1;
419*664f4763Szrj 		} else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
420*664f4763Szrj 			ret->permit_user_rc = r == 1;
421*664f4763Szrj 		} else if (opt_match(&opts, "command")) {
422*664f4763Szrj 			if (ret->force_command != NULL) {
423*664f4763Szrj 				errstr = "multiple \"command\" clauses";
424*664f4763Szrj 				goto fail;
425*664f4763Szrj 			}
426*664f4763Szrj 			ret->force_command = opt_dequote(&opts, &errstr);
427*664f4763Szrj 			if (ret->force_command == NULL)
428*664f4763Szrj 				goto fail;
429*664f4763Szrj 		} else if (opt_match(&opts, "principals")) {
430*664f4763Szrj 			if (ret->cert_principals != NULL) {
431*664f4763Szrj 				errstr = "multiple \"principals\" clauses";
432*664f4763Szrj 				goto fail;
433*664f4763Szrj 			}
434*664f4763Szrj 			ret->cert_principals = opt_dequote(&opts, &errstr);
435*664f4763Szrj 			if (ret->cert_principals == NULL)
436*664f4763Szrj 				goto fail;
437*664f4763Szrj 		} else if (opt_match(&opts, "from")) {
438*664f4763Szrj 			if (ret->required_from_host_keys != NULL) {
439*664f4763Szrj 				errstr = "multiple \"from\" clauses";
440*664f4763Szrj 				goto fail;
441*664f4763Szrj 			}
442*664f4763Szrj 			ret->required_from_host_keys = opt_dequote(&opts,
443*664f4763Szrj 			    &errstr);
444*664f4763Szrj 			if (ret->required_from_host_keys == NULL)
445*664f4763Szrj 				goto fail;
446*664f4763Szrj 		} else if (opt_match(&opts, "expiry-time")) {
447*664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
448*664f4763Szrj 				goto fail;
449*664f4763Szrj 			if (parse_absolute_time(opt, &valid_before) != 0 ||
450*664f4763Szrj 			    valid_before == 0) {
451*664f4763Szrj 				free(opt);
452*664f4763Szrj 				errstr = "invalid expires time";
453*664f4763Szrj 				goto fail;
454*664f4763Szrj 			}
455*664f4763Szrj 			free(opt);
456*664f4763Szrj 			if (ret->valid_before == 0 ||
457*664f4763Szrj 			    valid_before < ret->valid_before)
458*664f4763Szrj 				ret->valid_before = valid_before;
459*664f4763Szrj 		} else if (opt_match(&opts, "environment")) {
460*664f4763Szrj 			if (ret->nenv > INT_MAX) {
461*664f4763Szrj 				errstr = "too many environment strings";
462*664f4763Szrj 				goto fail;
463*664f4763Szrj 			}
464*664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
465*664f4763Szrj 				goto fail;
466*664f4763Szrj 			/* env name must be alphanumeric and followed by '=' */
467*664f4763Szrj 			if ((tmp = strchr(opt, '=')) == NULL) {
468*664f4763Szrj 				free(opt);
469*664f4763Szrj 				errstr = "invalid environment string";
470*664f4763Szrj 				goto fail;
471*664f4763Szrj 			}
472*664f4763Szrj 			if ((cp = strdup(opt)) == NULL)
473*664f4763Szrj 				goto alloc_fail;
474*664f4763Szrj 			cp[tmp - opt] = '\0'; /* truncate at '=' */
475*664f4763Szrj 			if (!valid_env_name(cp)) {
476*664f4763Szrj 				free(cp);
477*664f4763Szrj 				free(opt);
478*664f4763Szrj 				errstr = "invalid environment string";
479*664f4763Szrj 				goto fail;
480*664f4763Szrj 			}
481*664f4763Szrj 			free(cp);
482*664f4763Szrj 			/* Append it. */
483*664f4763Szrj 			oarray = ret->env;
484*664f4763Szrj 			if ((ret->env = recallocarray(ret->env, ret->nenv,
485*664f4763Szrj 			    ret->nenv + 1, sizeof(*ret->env))) == NULL) {
486*664f4763Szrj 				free(opt);
487*664f4763Szrj 				ret->env = oarray; /* put it back for cleanup */
488*664f4763Szrj 				goto alloc_fail;
489*664f4763Szrj 			}
490*664f4763Szrj 			ret->env[ret->nenv++] = opt;
491*664f4763Szrj 		} else if (opt_match(&opts, "permitopen")) {
492*664f4763Szrj 			if (handle_permit(&opts, 0, &ret->permitopen,
493*664f4763Szrj 			    &ret->npermitopen, &errstr) != 0)
494*664f4763Szrj 				goto fail;
495*664f4763Szrj 		} else if (opt_match(&opts, "permitlisten")) {
496*664f4763Szrj 			if (handle_permit(&opts, 1, &ret->permitlisten,
497*664f4763Szrj 			    &ret->npermitlisten, &errstr) != 0)
498*664f4763Szrj 				goto fail;
499*664f4763Szrj 		} else if (opt_match(&opts, "tunnel")) {
500*664f4763Szrj 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
501*664f4763Szrj 				goto fail;
502*664f4763Szrj 			ret->force_tun_device = a2tun(opt, NULL);
503*664f4763Szrj 			free(opt);
504*664f4763Szrj 			if (ret->force_tun_device == SSH_TUNID_ERR) {
505*664f4763Szrj 				errstr = "invalid tun device";
506*664f4763Szrj 				goto fail;
507*664f4763Szrj 			}
508*664f4763Szrj 		}
509*664f4763Szrj 		/*
510*664f4763Szrj 		 * Skip the comma, and move to the next option
511*664f4763Szrj 		 * (or break out if there are no more).
512*664f4763Szrj 		 */
513*664f4763Szrj 		if (*opts == '\0' || *opts == ' ' || *opts == '\t')
514*664f4763Szrj 			break;		/* End of options. */
515*664f4763Szrj 		/* Anything other than a comma is an unknown option */
516*664f4763Szrj 		if (*opts != ',') {
517*664f4763Szrj 			errstr = "unknown key option";
518*664f4763Szrj 			goto fail;
519*664f4763Szrj 		}
520*664f4763Szrj 		opts++;
521*664f4763Szrj 		if (*opts == '\0') {
522*664f4763Szrj 			errstr = "unexpected end-of-options";
523*664f4763Szrj 			goto fail;
524*664f4763Szrj 		}
525*664f4763Szrj 	}
526*664f4763Szrj 
527*664f4763Szrj 	/* success */
528*664f4763Szrj 	if (errstrp != NULL)
529*664f4763Szrj 		*errstrp = NULL;
530*664f4763Szrj 	return ret;
531*664f4763Szrj 
532*664f4763Szrj alloc_fail:
533*664f4763Szrj 	errstr = "memory allocation failed";
534*664f4763Szrj fail:
535*664f4763Szrj 	sshauthopt_free(ret);
536*664f4763Szrj 	if (errstrp != NULL)
537*664f4763Szrj 		*errstrp = errstr;
538*664f4763Szrj 	return NULL;
539*664f4763Szrj }
540*664f4763Szrj 
541*664f4763Szrj struct sshauthopt *
542*664f4763Szrj sshauthopt_from_cert(struct sshkey *k)
543*664f4763Szrj {
544*664f4763Szrj 	struct sshauthopt *ret;
545*664f4763Szrj 
546*664f4763Szrj 	if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
547*664f4763Szrj 	    k->cert->type != SSH2_CERT_TYPE_USER)
548*664f4763Szrj 		return NULL;
549*664f4763Szrj 
550*664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
551*664f4763Szrj 		return NULL;
552*664f4763Szrj 
553*664f4763Szrj 	/* Handle options and critical extensions separately */
554*664f4763Szrj 	if (cert_option_list(ret, k->cert->critical,
555*664f4763Szrj 	    OPTIONS_CRITICAL, 1) == -1) {
556*664f4763Szrj 		sshauthopt_free(ret);
557*664f4763Szrj 		return NULL;
558*664f4763Szrj 	}
559*664f4763Szrj 	if (cert_option_list(ret, k->cert->extensions,
560*664f4763Szrj 	    OPTIONS_EXTENSIONS, 0) == -1) {
561*664f4763Szrj 		sshauthopt_free(ret);
562*664f4763Szrj 		return NULL;
563*664f4763Szrj 	}
564*664f4763Szrj 	/* success */
565*664f4763Szrj 	return ret;
566*664f4763Szrj }
567*664f4763Szrj 
568*664f4763Szrj /*
569*664f4763Szrj  * Merges "additional" options to "primary" and returns the result.
570*664f4763Szrj  * NB. Some options from primary have primacy.
571*664f4763Szrj  */
572*664f4763Szrj struct sshauthopt *
573*664f4763Szrj sshauthopt_merge(const struct sshauthopt *primary,
574*664f4763Szrj     const struct sshauthopt *additional, const char **errstrp)
575*664f4763Szrj {
576*664f4763Szrj 	struct sshauthopt *ret;
577*664f4763Szrj 	const char *errstr = "internal error";
578*664f4763Szrj 	const char *tmp;
579*664f4763Szrj 
580*664f4763Szrj 	if (errstrp != NULL)
581*664f4763Szrj 		*errstrp = NULL;
582*664f4763Szrj 
583*664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
584*664f4763Szrj 		goto alloc_fail;
585*664f4763Szrj 
586*664f4763Szrj 	/* cert_authority and cert_principals are cleared in result */
587*664f4763Szrj 
588*664f4763Szrj 	/* Prefer access lists from primary. */
589*664f4763Szrj 	/* XXX err is both set and mismatch? */
590*664f4763Szrj 	tmp = primary->required_from_host_cert;
591*664f4763Szrj 	if (tmp == NULL)
592*664f4763Szrj 		tmp = additional->required_from_host_cert;
593*664f4763Szrj 	if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
594*664f4763Szrj 		goto alloc_fail;
595*664f4763Szrj 	tmp = primary->required_from_host_keys;
596*664f4763Szrj 	if (tmp == NULL)
597*664f4763Szrj 		tmp = additional->required_from_host_keys;
598*664f4763Szrj 	if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
599*664f4763Szrj 		goto alloc_fail;
600*664f4763Szrj 
601*664f4763Szrj 	/*
602*664f4763Szrj 	 * force_tun_device, permitopen/permitlisten and environment all
603*664f4763Szrj 	 * prefer the primary.
604*664f4763Szrj 	 */
605*664f4763Szrj 	ret->force_tun_device = primary->force_tun_device;
606*664f4763Szrj 	if (ret->force_tun_device == -1)
607*664f4763Szrj 		ret->force_tun_device = additional->force_tun_device;
608*664f4763Szrj 	if (primary->nenv > 0) {
609*664f4763Szrj 		if (dup_strings(&ret->env, &ret->nenv,
610*664f4763Szrj 		    primary->env, primary->nenv) != 0)
611*664f4763Szrj 			goto alloc_fail;
612*664f4763Szrj 	} else if (additional->nenv) {
613*664f4763Szrj 		if (dup_strings(&ret->env, &ret->nenv,
614*664f4763Szrj 		    additional->env, additional->nenv) != 0)
615*664f4763Szrj 			goto alloc_fail;
616*664f4763Szrj 	}
617*664f4763Szrj 	if (primary->npermitopen > 0) {
618*664f4763Szrj 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
619*664f4763Szrj 		    primary->permitopen, primary->npermitopen) != 0)
620*664f4763Szrj 			goto alloc_fail;
621*664f4763Szrj 	} else if (additional->npermitopen > 0) {
622*664f4763Szrj 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
623*664f4763Szrj 		    additional->permitopen, additional->npermitopen) != 0)
624*664f4763Szrj 			goto alloc_fail;
625*664f4763Szrj 	}
626*664f4763Szrj 
627*664f4763Szrj 	if (primary->npermitlisten > 0) {
628*664f4763Szrj 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
629*664f4763Szrj 		    primary->permitlisten, primary->npermitlisten) != 0)
630*664f4763Szrj 			goto alloc_fail;
631*664f4763Szrj 	} else if (additional->npermitlisten > 0) {
632*664f4763Szrj 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
633*664f4763Szrj 		    additional->permitlisten, additional->npermitlisten) != 0)
634*664f4763Szrj 			goto alloc_fail;
635*664f4763Szrj 	}
636*664f4763Szrj 
637*664f4763Szrj 	/* Flags are logical-AND (i.e. must be set in both for permission) */
638*664f4763Szrj #define OPTFLAG(x) ret->x = (primary->x == 1) && (additional->x == 1)
639*664f4763Szrj 	OPTFLAG(permit_port_forwarding_flag);
640*664f4763Szrj 	OPTFLAG(permit_agent_forwarding_flag);
641*664f4763Szrj 	OPTFLAG(permit_x11_forwarding_flag);
642*664f4763Szrj 	OPTFLAG(permit_pty_flag);
643*664f4763Szrj 	OPTFLAG(permit_user_rc);
644*664f4763Szrj #undef OPTFLAG
645*664f4763Szrj 
646*664f4763Szrj 	/* Earliest expiry time should win */
647*664f4763Szrj 	if (primary->valid_before != 0)
648*664f4763Szrj 		ret->valid_before = primary->valid_before;
649*664f4763Szrj 	if (additional->valid_before != 0 &&
650*664f4763Szrj 	    additional->valid_before < ret->valid_before)
651*664f4763Szrj 		ret->valid_before = additional->valid_before;
652*664f4763Szrj 
653*664f4763Szrj 	/*
654*664f4763Szrj 	 * When both multiple forced-command are specified, only
655*664f4763Szrj 	 * proceed if they are identical, otherwise fail.
656*664f4763Szrj 	 */
657*664f4763Szrj 	if (primary->force_command != NULL &&
658*664f4763Szrj 	    additional->force_command != NULL) {
659*664f4763Szrj 		if (strcmp(primary->force_command,
660*664f4763Szrj 		    additional->force_command) == 0) {
661*664f4763Szrj 			/* ok */
662*664f4763Szrj 			ret->force_command = strdup(primary->force_command);
663*664f4763Szrj 			if (ret->force_command == NULL)
664*664f4763Szrj 				goto alloc_fail;
665*664f4763Szrj 		} else {
666*664f4763Szrj 			errstr = "forced command options do not match";
667*664f4763Szrj 			goto fail;
668*664f4763Szrj 		}
669*664f4763Szrj 	} else if (primary->force_command != NULL) {
670*664f4763Szrj 		if ((ret->force_command = strdup(
671*664f4763Szrj 		    primary->force_command)) == NULL)
672*664f4763Szrj 			goto alloc_fail;
673*664f4763Szrj 	} else if (additional->force_command != NULL) {
674*664f4763Szrj 		if ((ret->force_command = strdup(
675*664f4763Szrj 		    additional->force_command)) == NULL)
676*664f4763Szrj 			goto alloc_fail;
677*664f4763Szrj 	}
678*664f4763Szrj 	/* success */
679*664f4763Szrj 	if (errstrp != NULL)
680*664f4763Szrj 		*errstrp = NULL;
681*664f4763Szrj 	return ret;
682*664f4763Szrj 
683*664f4763Szrj  alloc_fail:
684*664f4763Szrj 	errstr = "memory allocation failed";
685*664f4763Szrj  fail:
686*664f4763Szrj 	if (errstrp != NULL)
687*664f4763Szrj 		*errstrp = errstr;
688*664f4763Szrj 	sshauthopt_free(ret);
689*664f4763Szrj 	return NULL;
690*664f4763Szrj }
691*664f4763Szrj 
692*664f4763Szrj /*
693*664f4763Szrj  * Copy options
694*664f4763Szrj  */
695*664f4763Szrj struct sshauthopt *
696*664f4763Szrj sshauthopt_copy(const struct sshauthopt *orig)
697*664f4763Szrj {
698*664f4763Szrj 	struct sshauthopt *ret;
699*664f4763Szrj 
700*664f4763Szrj 	if ((ret = sshauthopt_new()) == NULL)
701*664f4763Szrj 		return NULL;
702*664f4763Szrj 
703*664f4763Szrj #define OPTSCALAR(x) ret->x = orig->x
704*664f4763Szrj 	OPTSCALAR(permit_port_forwarding_flag);
705*664f4763Szrj 	OPTSCALAR(permit_agent_forwarding_flag);
706*664f4763Szrj 	OPTSCALAR(permit_x11_forwarding_flag);
707*664f4763Szrj 	OPTSCALAR(permit_pty_flag);
708*664f4763Szrj 	OPTSCALAR(permit_user_rc);
709*664f4763Szrj 	OPTSCALAR(restricted);
710*664f4763Szrj 	OPTSCALAR(cert_authority);
711*664f4763Szrj 	OPTSCALAR(force_tun_device);
712*664f4763Szrj 	OPTSCALAR(valid_before);
713*664f4763Szrj #undef OPTSCALAR
714*664f4763Szrj #define OPTSTRING(x) \
715*664f4763Szrj 	do { \
716*664f4763Szrj 		if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
717*664f4763Szrj 			sshauthopt_free(ret); \
718*664f4763Szrj 			return NULL; \
719*664f4763Szrj 		} \
720*664f4763Szrj 	} while (0)
721*664f4763Szrj 	OPTSTRING(cert_principals);
722*664f4763Szrj 	OPTSTRING(force_command);
723*664f4763Szrj 	OPTSTRING(required_from_host_cert);
724*664f4763Szrj 	OPTSTRING(required_from_host_keys);
725*664f4763Szrj #undef OPTSTRING
726*664f4763Szrj 
727*664f4763Szrj 	if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
728*664f4763Szrj 	    dup_strings(&ret->permitopen, &ret->npermitopen,
729*664f4763Szrj 	    orig->permitopen, orig->npermitopen) != 0 ||
730*664f4763Szrj 	    dup_strings(&ret->permitlisten, &ret->npermitlisten,
731*664f4763Szrj 	    orig->permitlisten, orig->npermitlisten) != 0) {
732*664f4763Szrj 		sshauthopt_free(ret);
733*664f4763Szrj 		return NULL;
734*664f4763Szrj 	}
735*664f4763Szrj 	return ret;
736*664f4763Szrj }
737*664f4763Szrj 
738*664f4763Szrj static int
739*664f4763Szrj serialise_array(struct sshbuf *m, char **a, size_t n)
740*664f4763Szrj {
741*664f4763Szrj 	struct sshbuf *b;
742*664f4763Szrj 	size_t i;
743*664f4763Szrj 	int r;
744*664f4763Szrj 
745*664f4763Szrj 	if (n > INT_MAX)
746*664f4763Szrj 		return SSH_ERR_INTERNAL_ERROR;
747*664f4763Szrj 
748*664f4763Szrj 	if ((b = sshbuf_new()) == NULL) {
749*664f4763Szrj 		return SSH_ERR_ALLOC_FAIL;
750*664f4763Szrj 	}
751*664f4763Szrj 	for (i = 0; i < n; i++) {
752*664f4763Szrj 		if ((r = sshbuf_put_cstring(b, a[i])) != 0) {
753*664f4763Szrj 			sshbuf_free(b);
754*664f4763Szrj 			return r;
755*664f4763Szrj 		}
756*664f4763Szrj 	}
757*664f4763Szrj 	if ((r = sshbuf_put_u32(m, n)) != 0 ||
758*664f4763Szrj 	    (r = sshbuf_put_stringb(m, b)) != 0) {
759*664f4763Szrj 		sshbuf_free(b);
760*664f4763Szrj 		return r;
761*664f4763Szrj 	}
762*664f4763Szrj 	/* success */
763*664f4763Szrj 	return 0;
764*664f4763Szrj }
765*664f4763Szrj 
766*664f4763Szrj static int
767*664f4763Szrj deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
768*664f4763Szrj {
769*664f4763Szrj 	char **a = NULL;
770*664f4763Szrj 	size_t i, n = 0;
771*664f4763Szrj 	struct sshbuf *b = NULL;
772*664f4763Szrj 	u_int tmp;
773*664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
774*664f4763Szrj 
775*664f4763Szrj 	if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
776*664f4763Szrj 	    (r = sshbuf_froms(m, &b)) != 0)
777*664f4763Szrj 		goto out;
778*664f4763Szrj 	if (tmp > INT_MAX) {
779*664f4763Szrj 		r = SSH_ERR_INVALID_FORMAT;
780*664f4763Szrj 		goto out;
781*664f4763Szrj 	}
782*664f4763Szrj 	n = tmp;
783*664f4763Szrj 	if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
784*664f4763Szrj 		r = SSH_ERR_ALLOC_FAIL;
785*664f4763Szrj 		goto out;
786*664f4763Szrj 	}
787*664f4763Szrj 	for (i = 0; i < n; i++) {
788*664f4763Szrj 		if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
789*664f4763Szrj 			goto out;
790*664f4763Szrj 	}
791*664f4763Szrj 	/* success */
792*664f4763Szrj 	r = 0;
793*664f4763Szrj 	*ap = a;
794*664f4763Szrj 	a = NULL;
795*664f4763Szrj 	*np = n;
796*664f4763Szrj 	n = 0;
797*664f4763Szrj  out:
798*664f4763Szrj 	for (i = 0; i < n; i++)
799*664f4763Szrj 		free(a[i]);
800*664f4763Szrj 	free(a);
801*664f4763Szrj 	sshbuf_free(b);
802*664f4763Szrj 	return r;
803*664f4763Szrj }
804*664f4763Szrj 
805*664f4763Szrj static int
806*664f4763Szrj serialise_nullable_string(struct sshbuf *m, const char *s)
807*664f4763Szrj {
808*664f4763Szrj 	int r;
809*664f4763Szrj 
810*664f4763Szrj 	if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
811*664f4763Szrj 	    (r = sshbuf_put_cstring(m, s)) != 0)
812*664f4763Szrj 		return r;
813*664f4763Szrj 	return 0;
814*664f4763Szrj }
815*664f4763Szrj 
816*664f4763Szrj static int
817*664f4763Szrj deserialise_nullable_string(struct sshbuf *m, char **sp)
818*664f4763Szrj {
819*664f4763Szrj 	int r;
820*664f4763Szrj 	u_char flag;
821*664f4763Szrj 
822*664f4763Szrj 	*sp = NULL;
823*664f4763Szrj 	if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
824*664f4763Szrj 	    (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
825*664f4763Szrj 		return r;
826*664f4763Szrj 	return 0;
827*664f4763Szrj }
828*664f4763Szrj 
829*664f4763Szrj int
830*664f4763Szrj sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
831*664f4763Szrj     int untrusted)
832*664f4763Szrj {
833*664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
834*664f4763Szrj 
835*664f4763Szrj 	/* Flag and simple integer options */
836*664f4763Szrj 	if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
837*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
838*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
839*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
840*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
841*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
842*664f4763Szrj 	    (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
843*664f4763Szrj 	    (r = sshbuf_put_u64(m, opts->valid_before)) != 0)
844*664f4763Szrj 		return r;
845*664f4763Szrj 
846*664f4763Szrj 	/* tunnel number can be negative to indicate "unset" */
847*664f4763Szrj 	if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
848*664f4763Szrj 	    (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
849*664f4763Szrj 	    0 : (u_int)opts->force_tun_device)) != 0)
850*664f4763Szrj 		return r;
851*664f4763Szrj 
852*664f4763Szrj 	/* String options; these may be NULL */
853*664f4763Szrj 	if ((r = serialise_nullable_string(m,
854*664f4763Szrj 	    untrusted ? "yes" : opts->cert_principals)) != 0 ||
855*664f4763Szrj 	    (r = serialise_nullable_string(m,
856*664f4763Szrj 	    untrusted ? "true" : opts->force_command)) != 0 ||
857*664f4763Szrj 	    (r = serialise_nullable_string(m,
858*664f4763Szrj 	    untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
859*664f4763Szrj 	    (r = serialise_nullable_string(m,
860*664f4763Szrj 	     untrusted ? NULL : opts->required_from_host_keys)) != 0)
861*664f4763Szrj 		return r;
862*664f4763Szrj 
863*664f4763Szrj 	/* Array options */
864*664f4763Szrj 	if ((r = serialise_array(m, opts->env,
865*664f4763Szrj 	    untrusted ? 0 : opts->nenv)) != 0 ||
866*664f4763Szrj 	    (r = serialise_array(m, opts->permitopen,
867*664f4763Szrj 	    untrusted ? 0 : opts->npermitopen)) != 0 ||
868*664f4763Szrj 	    (r = serialise_array(m, opts->permitlisten,
869*664f4763Szrj 	    untrusted ? 0 : opts->npermitlisten)) != 0)
870*664f4763Szrj 		return r;
871*664f4763Szrj 
872*664f4763Szrj 	/* success */
873*664f4763Szrj 	return 0;
874*664f4763Szrj }
875*664f4763Szrj 
876*664f4763Szrj int
877*664f4763Szrj sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
878*664f4763Szrj {
879*664f4763Szrj 	struct sshauthopt *opts = NULL;
880*664f4763Szrj 	int r = SSH_ERR_INTERNAL_ERROR;
881*664f4763Szrj 	u_char f;
882*664f4763Szrj 	u_int tmp;
883*664f4763Szrj 
884*664f4763Szrj 	if ((opts = calloc(1, sizeof(*opts))) == NULL)
885*664f4763Szrj 		return SSH_ERR_ALLOC_FAIL;
886*664f4763Szrj 
887*664f4763Szrj #define OPT_FLAG(x) \
888*664f4763Szrj 	do { \
889*664f4763Szrj 		if ((r = sshbuf_get_u8(m, &f)) != 0) \
890*664f4763Szrj 			goto out; \
891*664f4763Szrj 		opts->x = f; \
892*664f4763Szrj 	} while (0)
893*664f4763Szrj 	OPT_FLAG(permit_port_forwarding_flag);
894*664f4763Szrj 	OPT_FLAG(permit_agent_forwarding_flag);
895*664f4763Szrj 	OPT_FLAG(permit_x11_forwarding_flag);
896*664f4763Szrj 	OPT_FLAG(permit_pty_flag);
897*664f4763Szrj 	OPT_FLAG(permit_user_rc);
898*664f4763Szrj 	OPT_FLAG(restricted);
899*664f4763Szrj 	OPT_FLAG(cert_authority);
900*664f4763Szrj #undef OPT_FLAG
901*664f4763Szrj 
902*664f4763Szrj 	if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
903*664f4763Szrj 		goto out;
904*664f4763Szrj 
905*664f4763Szrj 	/* tunnel number can be negative to indicate "unset" */
906*664f4763Szrj 	if ((r = sshbuf_get_u8(m, &f)) != 0 ||
907*664f4763Szrj 	    (r = sshbuf_get_u32(m, &tmp)) != 0)
908*664f4763Szrj 		goto out;
909*664f4763Szrj 	opts->force_tun_device = f ? -1 : (int)tmp;
910*664f4763Szrj 
911*664f4763Szrj 	/* String options may be NULL */
912*664f4763Szrj 	if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
913*664f4763Szrj 	    (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
914*664f4763Szrj 	    (r = deserialise_nullable_string(m,
915*664f4763Szrj 	    &opts->required_from_host_cert)) != 0 ||
916*664f4763Szrj 	    (r = deserialise_nullable_string(m,
917*664f4763Szrj 	    &opts->required_from_host_keys)) != 0)
918*664f4763Szrj 		goto out;
919*664f4763Szrj 
920*664f4763Szrj 	/* Array options */
921*664f4763Szrj 	if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
922*664f4763Szrj 	    (r = deserialise_array(m,
923*664f4763Szrj 	    &opts->permitopen, &opts->npermitopen)) != 0 ||
924*664f4763Szrj 	    (r = deserialise_array(m,
925*664f4763Szrj 	    &opts->permitlisten, &opts->npermitlisten)) != 0)
926*664f4763Szrj 		goto out;
927*664f4763Szrj 
928*664f4763Szrj 	/* success */
929*664f4763Szrj 	r = 0;
930*664f4763Szrj 	*optsp = opts;
931*664f4763Szrj 	opts = NULL;
932*664f4763Szrj  out:
933*664f4763Szrj 	sshauthopt_free(opts);
934*664f4763Szrj 	return r;
935*664f4763Szrj }
936