xref: /netbsd-src/crypto/external/bsd/openssh/dist/auth-options.c (revision a629fefc36f2e87b36355a611e948fafe62680b4)
1*a629fefcSchristos /*	$NetBSD: auth-options.c,v 1.29 2023/10/25 20:19:57 christos Exp $	*/
2*a629fefcSchristos /* $OpenBSD: auth-options.c,v 1.101 2023/07/14 07:44:21 dtucker Exp $ */
3*a629fefcSchristos 
4ca32bd8dSchristos /*
5ffae97bbSchristos  * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
6ffae97bbSchristos  *
7ffae97bbSchristos  * Permission to use, copy, modify, and distribute this software for any
8ffae97bbSchristos  * purpose with or without fee is hereby granted, provided that the above
9ffae97bbSchristos  * copyright notice and this permission notice appear in all copies.
10ffae97bbSchristos  *
11ffae97bbSchristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12ffae97bbSchristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13ffae97bbSchristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14ffae97bbSchristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15ffae97bbSchristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16ffae97bbSchristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17ffae97bbSchristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18ca32bd8dSchristos  */
19ca32bd8dSchristos 
20313c6c94Schristos #include "includes.h"
21*a629fefcSchristos __RCSID("$NetBSD: auth-options.c,v 1.29 2023/10/25 20:19:57 christos Exp $");
22ca32bd8dSchristos #include <sys/types.h>
23ca32bd8dSchristos #include <sys/queue.h>
24ca32bd8dSchristos 
25cd4ada6aSchristos #include <stdlib.h>
26ca32bd8dSchristos #include <netdb.h>
27ca32bd8dSchristos #include <pwd.h>
28ca32bd8dSchristos #include <string.h>
29ca32bd8dSchristos #include <stdio.h>
30*a629fefcSchristos #include <stdint.h>
31ca32bd8dSchristos #include <stdarg.h>
32313c6c94Schristos #include <time.h>
33ffae97bbSchristos #include <ctype.h>
34ffae97bbSchristos #include <limits.h>
35ca32bd8dSchristos 
36ca32bd8dSchristos #include "xmalloc.h"
37e4d43b82Schristos #include "ssherr.h"
38ca32bd8dSchristos #include "log.h"
39e4d43b82Schristos #include "sshbuf.h"
408a4530f9Schristos #include "misc.h"
41e4d43b82Schristos #include "sshkey.h"
42ffae97bbSchristos #include "match.h"
43ffae97bbSchristos #include "ssh2.h"
4434b27b53Sadam #include "auth-options.h"
45ca32bd8dSchristos 
46ffae97bbSchristos static int
dup_strings(char *** dstp,size_t * ndstp,char ** src,size_t nsrc)47ffae97bbSchristos dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
48ffae97bbSchristos {
49ffae97bbSchristos 	char **dst;
50ffae97bbSchristos 	size_t i, j;
51ca32bd8dSchristos 
52ffae97bbSchristos 	*dstp = NULL;
53ffae97bbSchristos 	*ndstp = 0;
54*a629fefcSchristos 
55ffae97bbSchristos 	if (nsrc == 0)
56ffae97bbSchristos 		return 0;
57*a629fefcSchristos 	if (nsrc >= SIZE_MAX / sizeof(*src) ||
58*a629fefcSchristos 	    (dst = calloc(nsrc, sizeof(*src))) == NULL)
59ffae97bbSchristos 		return -1;
60ffae97bbSchristos 	for (i = 0; i < nsrc; i++) {
61ffae97bbSchristos 		if ((dst[i] = strdup(src[i])) == NULL) {
62ffae97bbSchristos 			for (j = 0; j < i; j++)
63ffae97bbSchristos 				free(dst[j]);
64ffae97bbSchristos 			free(dst);
65ffae97bbSchristos 			return -1;
66ffae97bbSchristos 		}
67ffae97bbSchristos 	}
68ffae97bbSchristos 	/* success */
69ffae97bbSchristos 	*dstp = dst;
70ffae97bbSchristos 	*ndstp = nsrc;
71ca32bd8dSchristos 	return 0;
72ca32bd8dSchristos }
7334b27b53Sadam 
7434b27b53Sadam #define OPTIONS_CRITICAL	1
7534b27b53Sadam #define OPTIONS_EXTENSIONS	2
7634b27b53Sadam static int
cert_option_list(struct sshauthopt * opts,struct sshbuf * oblob,u_int which,int crit)77ffae97bbSchristos cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
78ffae97bbSchristos     u_int which, int crit)
7934b27b53Sadam {
8034b27b53Sadam 	char *command, *allowed;
8100a838c4Schristos 	char *name = NULL;
82e4d43b82Schristos 	struct sshbuf *c = NULL, *data = NULL;
83ffae97bbSchristos 	int r, ret = -1, found;
8434b27b53Sadam 
85e4d43b82Schristos 	if ((c = sshbuf_fromb(oblob)) == NULL) {
8617418e98Schristos 		error_f("sshbuf_fromb failed");
8734b27b53Sadam 		goto out;
8834b27b53Sadam 	}
89e4d43b82Schristos 
90e4d43b82Schristos 	while (sshbuf_len(c) > 0) {
91e4d43b82Schristos 		sshbuf_free(data);
92e4d43b82Schristos 		data = NULL;
93e4d43b82Schristos 		if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
94e4d43b82Schristos 		    (r = sshbuf_froms(c, &data)) != 0) {
9517418e98Schristos 			error_r(r, "Unable to parse certificate options");
96e4d43b82Schristos 			goto out;
97e4d43b82Schristos 		}
98e4d43b82Schristos 		debug3("found certificate option \"%.100s\" len %zu",
99e4d43b82Schristos 		    name, sshbuf_len(data));
10034b27b53Sadam 		found = 0;
10134b27b53Sadam 		if ((which & OPTIONS_EXTENSIONS) != 0) {
102ed75d7a8Schristos 			if (strcmp(name, "no-touch-required") == 0) {
103ed75d7a8Schristos 				opts->no_require_user_presence = 1;
104ed75d7a8Schristos 				found = 1;
105ed75d7a8Schristos 			} else if (strcmp(name, "permit-X11-forwarding") == 0) {
106ffae97bbSchristos 				opts->permit_x11_forwarding_flag = 1;
10734b27b53Sadam 				found = 1;
10834b27b53Sadam 			} else if (strcmp(name,
10934b27b53Sadam 			    "permit-agent-forwarding") == 0) {
110ffae97bbSchristos 				opts->permit_agent_forwarding_flag = 1;
11134b27b53Sadam 				found = 1;
11234b27b53Sadam 			} else if (strcmp(name,
11334b27b53Sadam 			    "permit-port-forwarding") == 0) {
114ffae97bbSchristos 				opts->permit_port_forwarding_flag = 1;
11534b27b53Sadam 				found = 1;
11634b27b53Sadam 			} else if (strcmp(name, "permit-pty") == 0) {
117ffae97bbSchristos 				opts->permit_pty_flag = 1;
11834b27b53Sadam 				found = 1;
11934b27b53Sadam 			} else if (strcmp(name, "permit-user-rc") == 0) {
120ffae97bbSchristos 				opts->permit_user_rc = 1;
12134b27b53Sadam 				found = 1;
12234b27b53Sadam 			}
12334b27b53Sadam 		}
12434b27b53Sadam 		if (!found && (which & OPTIONS_CRITICAL) != 0) {
1252d3b0f52Schristos 			if (strcmp(name, "verify-required") == 0) {
1262d3b0f52Schristos 				opts->require_verify = 1;
1272d3b0f52Schristos 				found = 1;
1282d3b0f52Schristos 			} else if (strcmp(name, "force-command") == 0) {
129e4d43b82Schristos 				if ((r = sshbuf_get_cstring(data, &command,
130e4d43b82Schristos 				    NULL)) != 0) {
13117418e98Schristos 					error_r(r, "Unable to parse \"%s\" "
13217418e98Schristos 					    "section", name);
13334b27b53Sadam 					goto out;
13434b27b53Sadam 				}
135ffae97bbSchristos 				if (opts->force_command != NULL) {
13634b27b53Sadam 					error("Certificate has multiple "
13734b27b53Sadam 					    "force-command options");
13800a838c4Schristos 					free(command);
13934b27b53Sadam 					goto out;
14034b27b53Sadam 				}
141ffae97bbSchristos 				opts->force_command = command;
14234b27b53Sadam 				found = 1;
1432d3b0f52Schristos 			} else if (strcmp(name, "source-address") == 0) {
144e4d43b82Schristos 				if ((r = sshbuf_get_cstring(data, &allowed,
145e4d43b82Schristos 				    NULL)) != 0) {
14617418e98Schristos 					error_r(r, "Unable to parse \"%s\" "
14717418e98Schristos 					    "section", name);
14834b27b53Sadam 					goto out;
14934b27b53Sadam 				}
150ffae97bbSchristos 				if (opts->required_from_host_cert != NULL) {
15134b27b53Sadam 					error("Certificate has multiple "
15234b27b53Sadam 					    "source-address options");
15300a838c4Schristos 					free(allowed);
15434b27b53Sadam 					goto out;
15534b27b53Sadam 				}
156ffae97bbSchristos 				/* Check syntax */
157ffae97bbSchristos 				if (addr_match_cidr_list(NULL, allowed) == -1) {
15834b27b53Sadam 					error("Certificate source-address "
15934b27b53Sadam 					    "contents invalid");
16034b27b53Sadam 					goto out;
16134b27b53Sadam 				}
162ffae97bbSchristos 				opts->required_from_host_cert = allowed;
16334b27b53Sadam 				found = 1;
16434b27b53Sadam 			}
16534b27b53Sadam 		}
16634b27b53Sadam 
16734b27b53Sadam 		if (!found) {
16834b27b53Sadam 			if (crit) {
16934b27b53Sadam 				error("Certificate critical option \"%s\" "
17034b27b53Sadam 				    "is not supported", name);
17134b27b53Sadam 				goto out;
17234b27b53Sadam 			} else {
17334b27b53Sadam 				logit("Certificate extension \"%s\" "
17434b27b53Sadam 				    "is not supported", name);
17534b27b53Sadam 			}
176e4d43b82Schristos 		} else if (sshbuf_len(data) != 0) {
17734b27b53Sadam 			error("Certificate option \"%s\" corrupt "
17834b27b53Sadam 			    "(extra data)", name);
17934b27b53Sadam 			goto out;
18034b27b53Sadam 		}
18100a838c4Schristos 		free(name);
18200a838c4Schristos 		name = NULL;
18334b27b53Sadam 	}
18434b27b53Sadam 	/* successfully parsed all options */
18534b27b53Sadam 	ret = 0;
18634b27b53Sadam 
18734b27b53Sadam  out:
18800a838c4Schristos 	free(name);
189e4d43b82Schristos 	sshbuf_free(data);
190e4d43b82Schristos 	sshbuf_free(c);
19134b27b53Sadam 	return ret;
19234b27b53Sadam }
19334b27b53Sadam 
194ffae97bbSchristos struct sshauthopt *
sshauthopt_new(void)195ffae97bbSchristos sshauthopt_new(void)
19634b27b53Sadam {
197ffae97bbSchristos 	struct sshauthopt *ret;
19834b27b53Sadam 
199ffae97bbSchristos 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
200ffae97bbSchristos 		return NULL;
201ffae97bbSchristos 	ret->force_tun_device = -1;
202ffae97bbSchristos 	return ret;
20334b27b53Sadam }
204ffae97bbSchristos 
205ffae97bbSchristos void
sshauthopt_free(struct sshauthopt * opts)206ffae97bbSchristos sshauthopt_free(struct sshauthopt *opts)
207ffae97bbSchristos {
208ffae97bbSchristos 	size_t i;
209ffae97bbSchristos 
210ffae97bbSchristos 	if (opts == NULL)
211ffae97bbSchristos 		return;
212ffae97bbSchristos 
213ffae97bbSchristos 	free(opts->cert_principals);
214ffae97bbSchristos 	free(opts->force_command);
215ffae97bbSchristos 	free(opts->required_from_host_cert);
216ffae97bbSchristos 	free(opts->required_from_host_keys);
217ffae97bbSchristos 
218ffae97bbSchristos 	for (i = 0; i < opts->nenv; i++)
219ffae97bbSchristos 		free(opts->env[i]);
220ffae97bbSchristos 	free(opts->env);
221ffae97bbSchristos 
222ffae97bbSchristos 	for (i = 0; i < opts->npermitopen; i++)
223ffae97bbSchristos 		free(opts->permitopen[i]);
224ffae97bbSchristos 	free(opts->permitopen);
225ffae97bbSchristos 
22655a4608bSchristos 	for (i = 0; i < opts->npermitlisten; i++)
22755a4608bSchristos 		free(opts->permitlisten[i]);
22855a4608bSchristos 	free(opts->permitlisten);
22955a4608bSchristos 
2308db691beSchristos 	freezero(opts, sizeof(*opts));
231ffae97bbSchristos }
232ffae97bbSchristos 
233ffae97bbSchristos struct sshauthopt *
sshauthopt_new_with_keys_defaults(void)234ffae97bbSchristos sshauthopt_new_with_keys_defaults(void)
235ffae97bbSchristos {
236ffae97bbSchristos 	struct sshauthopt *ret = NULL;
237ffae97bbSchristos 
238ffae97bbSchristos 	if ((ret = sshauthopt_new()) == NULL)
239ffae97bbSchristos 		return NULL;
240ffae97bbSchristos 
241ffae97bbSchristos 	/* Defaults for authorized_keys flags */
242ffae97bbSchristos 	ret->permit_port_forwarding_flag = 1;
243ffae97bbSchristos 	ret->permit_agent_forwarding_flag = 1;
244ffae97bbSchristos 	ret->permit_x11_forwarding_flag = 1;
245ffae97bbSchristos 	ret->permit_pty_flag = 1;
246ffae97bbSchristos 	ret->permit_user_rc = 1;
247ffae97bbSchristos 	return ret;
248ffae97bbSchristos }
249ffae97bbSchristos 
25055a4608bSchristos /*
25155a4608bSchristos  * Parse and record a permitopen/permitlisten directive.
25255a4608bSchristos  * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
25355a4608bSchristos  */
25455a4608bSchristos static int
handle_permit(const char ** optsp,int allow_bare_port,char *** permitsp,size_t * npermitsp,const char ** errstrp)25555a4608bSchristos handle_permit(const char **optsp, int allow_bare_port,
25655a4608bSchristos     char ***permitsp, size_t *npermitsp, const char **errstrp)
25755a4608bSchristos {
25855a4608bSchristos 	char *opt, *tmp, *cp, *host, **permits = *permitsp;
25955a4608bSchristos 	size_t npermits = *npermitsp;
26055a4608bSchristos 	const char *errstr = "unknown error";
26155a4608bSchristos 
262cd4ada6aSchristos 	if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
26355a4608bSchristos 		*errstrp = "too many permission directives";
26455a4608bSchristos 		return -1;
26555a4608bSchristos 	}
26655a4608bSchristos 	if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
26755a4608bSchristos 		return -1;
26855a4608bSchristos 	}
26955a4608bSchristos 	if (allow_bare_port && strchr(opt, ':') == NULL) {
27055a4608bSchristos 		/*
27155a4608bSchristos 		 * Allow a bare port number in permitlisten to indicate a
27255a4608bSchristos 		 * listen_host wildcard.
27355a4608bSchristos 		 */
274cd4ada6aSchristos 		if (asprintf(&tmp, "*:%s", opt) == -1) {
275cd4ada6aSchristos 			free(opt);
27655a4608bSchristos 			*errstrp = "memory allocation failed";
27755a4608bSchristos 			return -1;
27855a4608bSchristos 		}
27955a4608bSchristos 		free(opt);
28055a4608bSchristos 		opt = tmp;
28155a4608bSchristos 	}
28255a4608bSchristos 	if ((tmp = strdup(opt)) == NULL) {
28355a4608bSchristos 		free(opt);
28455a4608bSchristos 		*errstrp = "memory allocation failed";
28555a4608bSchristos 		return -1;
28655a4608bSchristos 	}
28755a4608bSchristos 	cp = tmp;
28855a4608bSchristos 	/* validate syntax before recording it. */
289a03ec00cSchristos 	host = hpdelim2(&cp, NULL);
29055a4608bSchristos 	if (host == NULL || strlen(host) >= NI_MAXHOST) {
29155a4608bSchristos 		free(tmp);
29255a4608bSchristos 		free(opt);
29355a4608bSchristos 		*errstrp = "invalid permission hostname";
29455a4608bSchristos 		return -1;
29555a4608bSchristos 	}
29655a4608bSchristos 	/*
29755a4608bSchristos 	 * don't want to use permitopen_port to avoid
29855a4608bSchristos 	 * dependency on channels.[ch] here.
29955a4608bSchristos 	 */
30055a4608bSchristos 	if (cp == NULL ||
30155a4608bSchristos 	    (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
30255a4608bSchristos 		free(tmp);
30355a4608bSchristos 		free(opt);
30455a4608bSchristos 		*errstrp = "invalid permission port";
30555a4608bSchristos 		return -1;
30655a4608bSchristos 	}
30755a4608bSchristos 	/* XXX - add streamlocal support */
30855a4608bSchristos 	free(tmp);
30955a4608bSchristos 	/* Record it */
31055a4608bSchristos 	if ((permits = recallocarray(permits, npermits, npermits + 1,
31155a4608bSchristos 	    sizeof(*permits))) == NULL) {
31255a4608bSchristos 		free(opt);
31355a4608bSchristos 		/* NB. don't update *permitsp if alloc fails */
31455a4608bSchristos 		*errstrp = "memory allocation failed";
31555a4608bSchristos 		return -1;
31655a4608bSchristos 	}
31755a4608bSchristos 	permits[npermits++] = opt;
31855a4608bSchristos 	*permitsp = permits;
31955a4608bSchristos 	*npermitsp = npermits;
32055a4608bSchristos 	return 0;
32155a4608bSchristos }
32255a4608bSchristos 
323ffae97bbSchristos struct sshauthopt *
sshauthopt_parse(const char * opts,const char ** errstrp)324ffae97bbSchristos sshauthopt_parse(const char *opts, const char **errstrp)
325ffae97bbSchristos {
32655a4608bSchristos 	char **oarray, *opt, *cp, *tmp;
327ffae97bbSchristos 	int r;
328ffae97bbSchristos 	struct sshauthopt *ret = NULL;
329ffae97bbSchristos 	const char *errstr = "unknown error";
330ffae97bbSchristos 	uint64_t valid_before;
331b592f463Schristos 	size_t i, l;
332ffae97bbSchristos 
333ffae97bbSchristos 	if (errstrp != NULL)
334ffae97bbSchristos 		*errstrp = NULL;
335ffae97bbSchristos 	if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
336ffae97bbSchristos 		goto alloc_fail;
337ffae97bbSchristos 
338ffae97bbSchristos 	if (opts == NULL)
339ffae97bbSchristos 		return ret;
340ffae97bbSchristos 
341ffae97bbSchristos 	while (*opts && *opts != ' ' && *opts != '\t') {
342ffae97bbSchristos 		/* flag options */
343ffae97bbSchristos 		if ((r = opt_flag("restrict", 0, &opts)) != -1) {
344ffae97bbSchristos 			ret->restricted = 1;
345ffae97bbSchristos 			ret->permit_port_forwarding_flag = 0;
346ffae97bbSchristos 			ret->permit_agent_forwarding_flag = 0;
347ffae97bbSchristos 			ret->permit_x11_forwarding_flag = 0;
348ffae97bbSchristos 			ret->permit_pty_flag = 0;
349ffae97bbSchristos 			ret->permit_user_rc = 0;
350ffae97bbSchristos 		} else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
351ffae97bbSchristos 			ret->cert_authority = r;
352ffae97bbSchristos 		} else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
353ffae97bbSchristos 			ret->permit_port_forwarding_flag = r == 1;
354ffae97bbSchristos 		} else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
355ffae97bbSchristos 			ret->permit_agent_forwarding_flag = r == 1;
356ffae97bbSchristos 		} else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
357ffae97bbSchristos 			ret->permit_x11_forwarding_flag = r == 1;
358ed75d7a8Schristos 		} else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
359ed75d7a8Schristos 			ret->no_require_user_presence = r != 1; /* NB. flip */
3602d3b0f52Schristos 		} else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
3612d3b0f52Schristos 			ret->require_verify = r == 1;
362ffae97bbSchristos 		} else if ((r = opt_flag("pty", 1, &opts)) != -1) {
363ffae97bbSchristos 			ret->permit_pty_flag = r == 1;
364ffae97bbSchristos 		} else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
365ffae97bbSchristos 			ret->permit_user_rc = r == 1;
366ffae97bbSchristos 		} else if (opt_match(&opts, "command")) {
367ffae97bbSchristos 			if (ret->force_command != NULL) {
368ffae97bbSchristos 				errstr = "multiple \"command\" clauses";
369ffae97bbSchristos 				goto fail;
370ffae97bbSchristos 			}
371ffae97bbSchristos 			ret->force_command = opt_dequote(&opts, &errstr);
372ffae97bbSchristos 			if (ret->force_command == NULL)
373ffae97bbSchristos 				goto fail;
374ffae97bbSchristos 		} else if (opt_match(&opts, "principals")) {
375ffae97bbSchristos 			if (ret->cert_principals != NULL) {
376ffae97bbSchristos 				errstr = "multiple \"principals\" clauses";
377ffae97bbSchristos 				goto fail;
378ffae97bbSchristos 			}
379ffae97bbSchristos 			ret->cert_principals = opt_dequote(&opts, &errstr);
380ffae97bbSchristos 			if (ret->cert_principals == NULL)
381ffae97bbSchristos 				goto fail;
382ffae97bbSchristos 		} else if (opt_match(&opts, "from")) {
383ffae97bbSchristos 			if (ret->required_from_host_keys != NULL) {
384ffae97bbSchristos 				errstr = "multiple \"from\" clauses";
385ffae97bbSchristos 				goto fail;
386ffae97bbSchristos 			}
387ffae97bbSchristos 			ret->required_from_host_keys = opt_dequote(&opts,
388ffae97bbSchristos 			    &errstr);
389ffae97bbSchristos 			if (ret->required_from_host_keys == NULL)
390ffae97bbSchristos 				goto fail;
391ffae97bbSchristos 		} else if (opt_match(&opts, "expiry-time")) {
392ffae97bbSchristos 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
393ffae97bbSchristos 				goto fail;
394ffae97bbSchristos 			if (parse_absolute_time(opt, &valid_before) != 0 ||
395ffae97bbSchristos 			    valid_before == 0) {
396ffae97bbSchristos 				free(opt);
397ffae97bbSchristos 				errstr = "invalid expires time";
398ffae97bbSchristos 				goto fail;
399ffae97bbSchristos 			}
400ffae97bbSchristos 			free(opt);
401ffae97bbSchristos 			if (ret->valid_before == 0 ||
402ffae97bbSchristos 			    valid_before < ret->valid_before)
403ffae97bbSchristos 				ret->valid_before = valid_before;
404ffae97bbSchristos 		} else if (opt_match(&opts, "environment")) {
405b592f463Schristos 			if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
406ffae97bbSchristos 				errstr = "too many environment strings";
407ffae97bbSchristos 				goto fail;
408ffae97bbSchristos 			}
409ffae97bbSchristos 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
410ffae97bbSchristos 				goto fail;
411ffae97bbSchristos 			/* env name must be alphanumeric and followed by '=' */
412ffae97bbSchristos 			if ((tmp = strchr(opt, '=')) == NULL) {
413ffae97bbSchristos 				free(opt);
414ffae97bbSchristos 				errstr = "invalid environment string";
415ffae97bbSchristos 				goto fail;
416ffae97bbSchristos 			}
417b592f463Schristos 			if ((cp = strdup(opt)) == NULL) {
418b592f463Schristos 				free(opt);
419aa36fcacSchristos 				goto alloc_fail;
420b592f463Schristos 			}
421b592f463Schristos 			l = (size_t)(tmp - opt);
422b592f463Schristos 			cp[l] = '\0'; /* truncate at '=' */
423aa36fcacSchristos 			if (!valid_env_name(cp)) {
424aa36fcacSchristos 				free(cp);
425ffae97bbSchristos 				free(opt);
426ffae97bbSchristos 				errstr = "invalid environment string";
427ffae97bbSchristos 				goto fail;
428ffae97bbSchristos 			}
429b592f463Schristos 			/* Check for duplicates; XXX O(n*log(n)) */
430b592f463Schristos 			for (i = 0; i < ret->nenv; i++) {
431b592f463Schristos 				if (strncmp(ret->env[i], cp, l) == 0 &&
432b592f463Schristos 				    ret->env[i][l] == '=')
433b592f463Schristos 					break;
434b592f463Schristos 			}
435aa36fcacSchristos 			free(cp);
436b592f463Schristos 			/* First match wins */
437b592f463Schristos 			if (i >= ret->nenv) {
438ffae97bbSchristos 				/* Append it. */
439ffae97bbSchristos 				oarray = ret->env;
440b592f463Schristos 				if ((ret->env = recallocarray(ret->env,
441b592f463Schristos 				    ret->nenv, ret->nenv + 1,
442b592f463Schristos 				    sizeof(*ret->env))) == NULL) {
443ffae97bbSchristos 					free(opt);
444b592f463Schristos 					/* put it back for cleanup */
445b592f463Schristos 					ret->env = oarray;
446ffae97bbSchristos 					goto alloc_fail;
447ffae97bbSchristos 				}
448ffae97bbSchristos 				ret->env[ret->nenv++] = opt;
449b592f463Schristos 				opt = NULL; /* transferred */
450b592f463Schristos 			}
451b592f463Schristos 			free(opt);
452ffae97bbSchristos 		} else if (opt_match(&opts, "permitopen")) {
45355a4608bSchristos 			if (handle_permit(&opts, 0, &ret->permitopen,
45455a4608bSchristos 			    &ret->npermitopen, &errstr) != 0)
455ffae97bbSchristos 				goto fail;
45655a4608bSchristos 		} else if (opt_match(&opts, "permitlisten")) {
45755a4608bSchristos 			if (handle_permit(&opts, 1, &ret->permitlisten,
45855a4608bSchristos 			    &ret->npermitlisten, &errstr) != 0)
459ffae97bbSchristos 				goto fail;
460ffae97bbSchristos 		} else if (opt_match(&opts, "tunnel")) {
461ffae97bbSchristos 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
462ffae97bbSchristos 				goto fail;
463ffae97bbSchristos 			ret->force_tun_device = a2tun(opt, NULL);
464ffae97bbSchristos 			free(opt);
465ffae97bbSchristos 			if (ret->force_tun_device == SSH_TUNID_ERR) {
466ffae97bbSchristos 				errstr = "invalid tun device";
467ffae97bbSchristos 				goto fail;
468ffae97bbSchristos 			}
469ffae97bbSchristos 		}
470ffae97bbSchristos 		/*
471ffae97bbSchristos 		 * Skip the comma, and move to the next option
472ffae97bbSchristos 		 * (or break out if there are no more).
473ffae97bbSchristos 		 */
474ffae97bbSchristos 		if (*opts == '\0' || *opts == ' ' || *opts == '\t')
475ffae97bbSchristos 			break;		/* End of options. */
476ffae97bbSchristos 		/* Anything other than a comma is an unknown option */
477ffae97bbSchristos 		if (*opts != ',') {
478ffae97bbSchristos 			errstr = "unknown key option";
479ffae97bbSchristos 			goto fail;
480ffae97bbSchristos 		}
481ffae97bbSchristos 		opts++;
482ffae97bbSchristos 		if (*opts == '\0') {
483ffae97bbSchristos 			errstr = "unexpected end-of-options";
484ffae97bbSchristos 			goto fail;
485ffae97bbSchristos 		}
486ffae97bbSchristos 	}
487ffae97bbSchristos 
488ee85abc4Schristos 	/* success */
489ffae97bbSchristos 	if (errstrp != NULL)
490ffae97bbSchristos 		*errstrp = NULL;
491ffae97bbSchristos 	return ret;
492ffae97bbSchristos 
493ffae97bbSchristos alloc_fail:
494ffae97bbSchristos 	errstr = "memory allocation failed";
495ffae97bbSchristos fail:
496ffae97bbSchristos 	sshauthopt_free(ret);
497ffae97bbSchristos 	if (errstrp != NULL)
498ffae97bbSchristos 		*errstrp = errstr;
499ffae97bbSchristos 	return NULL;
500ffae97bbSchristos }
501ffae97bbSchristos 
502ffae97bbSchristos struct sshauthopt *
sshauthopt_from_cert(struct sshkey * k)503ffae97bbSchristos sshauthopt_from_cert(struct sshkey *k)
504ffae97bbSchristos {
505ffae97bbSchristos 	struct sshauthopt *ret;
506ffae97bbSchristos 
507ffae97bbSchristos 	if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
508ffae97bbSchristos 	    k->cert->type != SSH2_CERT_TYPE_USER)
509ffae97bbSchristos 		return NULL;
510ffae97bbSchristos 
511ffae97bbSchristos 	if ((ret = sshauthopt_new()) == NULL)
512ffae97bbSchristos 		return NULL;
513ffae97bbSchristos 
514ffae97bbSchristos 	/* Handle options and critical extensions separately */
515ffae97bbSchristos 	if (cert_option_list(ret, k->cert->critical,
516ffae97bbSchristos 	    OPTIONS_CRITICAL, 1) == -1) {
517ffae97bbSchristos 		sshauthopt_free(ret);
518ffae97bbSchristos 		return NULL;
519ffae97bbSchristos 	}
520ffae97bbSchristos 	if (cert_option_list(ret, k->cert->extensions,
521ffae97bbSchristos 	    OPTIONS_EXTENSIONS, 0) == -1) {
522ffae97bbSchristos 		sshauthopt_free(ret);
523ffae97bbSchristos 		return NULL;
524ffae97bbSchristos 	}
525ffae97bbSchristos 	/* success */
526ffae97bbSchristos 	return ret;
527ffae97bbSchristos }
528ffae97bbSchristos 
529ffae97bbSchristos /*
530ffae97bbSchristos  * Merges "additional" options to "primary" and returns the result.
531ffae97bbSchristos  * NB. Some options from primary have primacy.
532ffae97bbSchristos  */
533ffae97bbSchristos struct sshauthopt *
sshauthopt_merge(const struct sshauthopt * primary,const struct sshauthopt * additional,const char ** errstrp)534ffae97bbSchristos sshauthopt_merge(const struct sshauthopt *primary,
535ffae97bbSchristos     const struct sshauthopt *additional, const char **errstrp)
536ffae97bbSchristos {
537ffae97bbSchristos 	struct sshauthopt *ret;
538ffae97bbSchristos 	const char *errstr = "internal error";
539ffae97bbSchristos 	const char *tmp;
540ffae97bbSchristos 
541ffae97bbSchristos 	if (errstrp != NULL)
542ffae97bbSchristos 		*errstrp = NULL;
543ffae97bbSchristos 
544ffae97bbSchristos 	if ((ret = sshauthopt_new()) == NULL)
545ffae97bbSchristos 		goto alloc_fail;
546ffae97bbSchristos 
547ffae97bbSchristos 	/* cert_authority and cert_principals are cleared in result */
548ffae97bbSchristos 
549ffae97bbSchristos 	/* Prefer access lists from primary. */
550ffae97bbSchristos 	/* XXX err is both set and mismatch? */
551ffae97bbSchristos 	tmp = primary->required_from_host_cert;
552ffae97bbSchristos 	if (tmp == NULL)
553ffae97bbSchristos 		tmp = additional->required_from_host_cert;
554ffae97bbSchristos 	if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
555ffae97bbSchristos 		goto alloc_fail;
556ffae97bbSchristos 	tmp = primary->required_from_host_keys;
557ffae97bbSchristos 	if (tmp == NULL)
558ffae97bbSchristos 		tmp = additional->required_from_host_keys;
559ffae97bbSchristos 	if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
560ffae97bbSchristos 		goto alloc_fail;
561ffae97bbSchristos 
56255a4608bSchristos 	/*
56355a4608bSchristos 	 * force_tun_device, permitopen/permitlisten and environment all
56455a4608bSchristos 	 * prefer the primary.
56555a4608bSchristos 	 */
566ffae97bbSchristos 	ret->force_tun_device = primary->force_tun_device;
567ffae97bbSchristos 	if (ret->force_tun_device == -1)
568ffae97bbSchristos 		ret->force_tun_device = additional->force_tun_device;
569ffae97bbSchristos 	if (primary->nenv > 0) {
570ffae97bbSchristos 		if (dup_strings(&ret->env, &ret->nenv,
571ffae97bbSchristos 		    primary->env, primary->nenv) != 0)
572ffae97bbSchristos 			goto alloc_fail;
573ffae97bbSchristos 	} else if (additional->nenv) {
574ffae97bbSchristos 		if (dup_strings(&ret->env, &ret->nenv,
575ffae97bbSchristos 		    additional->env, additional->nenv) != 0)
576ffae97bbSchristos 			goto alloc_fail;
577ffae97bbSchristos 	}
578ffae97bbSchristos 	if (primary->npermitopen > 0) {
579ffae97bbSchristos 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
580ffae97bbSchristos 		    primary->permitopen, primary->npermitopen) != 0)
581ffae97bbSchristos 			goto alloc_fail;
582ffae97bbSchristos 	} else if (additional->npermitopen > 0) {
583ffae97bbSchristos 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
584ffae97bbSchristos 		    additional->permitopen, additional->npermitopen) != 0)
585ffae97bbSchristos 			goto alloc_fail;
586ffae97bbSchristos 	}
587ffae97bbSchristos 
58855a4608bSchristos 	if (primary->npermitlisten > 0) {
58955a4608bSchristos 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
59055a4608bSchristos 		    primary->permitlisten, primary->npermitlisten) != 0)
59155a4608bSchristos 			goto alloc_fail;
59255a4608bSchristos 	} else if (additional->npermitlisten > 0) {
59355a4608bSchristos 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
59455a4608bSchristos 		    additional->permitlisten, additional->npermitlisten) != 0)
59555a4608bSchristos 			goto alloc_fail;
59655a4608bSchristos 	}
59755a4608bSchristos 
598ed75d7a8Schristos #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
5992d3b0f52Schristos #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
600ed75d7a8Schristos 	/* Permissive flags are logical-AND (i.e. must be set in both) */
601ed75d7a8Schristos 	OPTFLAG_AND(permit_port_forwarding_flag);
602ed75d7a8Schristos 	OPTFLAG_AND(permit_agent_forwarding_flag);
603ed75d7a8Schristos 	OPTFLAG_AND(permit_x11_forwarding_flag);
604ed75d7a8Schristos 	OPTFLAG_AND(permit_pty_flag);
605ed75d7a8Schristos 	OPTFLAG_AND(permit_user_rc);
606ed75d7a8Schristos 	OPTFLAG_AND(no_require_user_presence);
6072d3b0f52Schristos 	/* Restrictive flags are logical-OR (i.e. must be set in either) */
6082d3b0f52Schristos 	OPTFLAG_OR(require_verify);
609ed75d7a8Schristos #undef OPTFLAG_AND
610ffae97bbSchristos 
611ffae97bbSchristos 	/* Earliest expiry time should win */
612ffae97bbSchristos 	if (primary->valid_before != 0)
613ffae97bbSchristos 		ret->valid_before = primary->valid_before;
614ffae97bbSchristos 	if (additional->valid_before != 0 &&
615ffae97bbSchristos 	    additional->valid_before < ret->valid_before)
616ffae97bbSchristos 		ret->valid_before = additional->valid_before;
617ffae97bbSchristos 
618ffae97bbSchristos 	/*
619ffae97bbSchristos 	 * When both multiple forced-command are specified, only
620ffae97bbSchristos 	 * proceed if they are identical, otherwise fail.
621ffae97bbSchristos 	 */
622ffae97bbSchristos 	if (primary->force_command != NULL &&
623ffae97bbSchristos 	    additional->force_command != NULL) {
624ffae97bbSchristos 		if (strcmp(primary->force_command,
625ffae97bbSchristos 		    additional->force_command) == 0) {
626ffae97bbSchristos 			/* ok */
627ffae97bbSchristos 			ret->force_command = strdup(primary->force_command);
628ffae97bbSchristos 			if (ret->force_command == NULL)
629ffae97bbSchristos 				goto alloc_fail;
630ffae97bbSchristos 		} else {
631ffae97bbSchristos 			errstr = "forced command options do not match";
632ffae97bbSchristos 			goto fail;
633ffae97bbSchristos 		}
634ffae97bbSchristos 	} else if (primary->force_command != NULL) {
635ffae97bbSchristos 		if ((ret->force_command = strdup(
636ffae97bbSchristos 		    primary->force_command)) == NULL)
637ffae97bbSchristos 			goto alloc_fail;
638ffae97bbSchristos 	} else if (additional->force_command != NULL) {
639ffae97bbSchristos 		if ((ret->force_command = strdup(
640ffae97bbSchristos 		    additional->force_command)) == NULL)
641ffae97bbSchristos 			goto alloc_fail;
642ffae97bbSchristos 	}
643ffae97bbSchristos 	/* success */
644ffae97bbSchristos 	if (errstrp != NULL)
645ffae97bbSchristos 		*errstrp = NULL;
646ffae97bbSchristos 	return ret;
647ffae97bbSchristos 
648ffae97bbSchristos  alloc_fail:
649ffae97bbSchristos 	errstr = "memory allocation failed";
650ffae97bbSchristos  fail:
651ffae97bbSchristos 	if (errstrp != NULL)
652ffae97bbSchristos 		*errstrp = errstr;
653ffae97bbSchristos 	sshauthopt_free(ret);
654ffae97bbSchristos 	return NULL;
655ffae97bbSchristos }
656ffae97bbSchristos 
657ffae97bbSchristos /*
658ffae97bbSchristos  * Copy options
659ffae97bbSchristos  */
660ffae97bbSchristos struct sshauthopt *
sshauthopt_copy(const struct sshauthopt * orig)661ffae97bbSchristos sshauthopt_copy(const struct sshauthopt *orig)
662ffae97bbSchristos {
663ffae97bbSchristos 	struct sshauthopt *ret;
664ffae97bbSchristos 
665ffae97bbSchristos 	if ((ret = sshauthopt_new()) == NULL)
666ffae97bbSchristos 		return NULL;
667ffae97bbSchristos 
668ffae97bbSchristos #define OPTSCALAR(x) ret->x = orig->x
669ffae97bbSchristos 	OPTSCALAR(permit_port_forwarding_flag);
670ffae97bbSchristos 	OPTSCALAR(permit_agent_forwarding_flag);
671ffae97bbSchristos 	OPTSCALAR(permit_x11_forwarding_flag);
672ffae97bbSchristos 	OPTSCALAR(permit_pty_flag);
673ffae97bbSchristos 	OPTSCALAR(permit_user_rc);
674ffae97bbSchristos 	OPTSCALAR(restricted);
675ffae97bbSchristos 	OPTSCALAR(cert_authority);
676ffae97bbSchristos 	OPTSCALAR(force_tun_device);
677ffae97bbSchristos 	OPTSCALAR(valid_before);
678ed75d7a8Schristos 	OPTSCALAR(no_require_user_presence);
6792d3b0f52Schristos 	OPTSCALAR(require_verify);
680ffae97bbSchristos #undef OPTSCALAR
681ffae97bbSchristos #define OPTSTRING(x) \
682ffae97bbSchristos 	do { \
683ffae97bbSchristos 		if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
684ffae97bbSchristos 			sshauthopt_free(ret); \
685ffae97bbSchristos 			return NULL; \
686ffae97bbSchristos 		} \
687ffae97bbSchristos 	} while (0)
688ffae97bbSchristos 	OPTSTRING(cert_principals);
689ffae97bbSchristos 	OPTSTRING(force_command);
690ffae97bbSchristos 	OPTSTRING(required_from_host_cert);
691ffae97bbSchristos 	OPTSTRING(required_from_host_keys);
692ffae97bbSchristos #undef OPTSTRING
693ffae97bbSchristos 
694ffae97bbSchristos 	if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
695ffae97bbSchristos 	    dup_strings(&ret->permitopen, &ret->npermitopen,
69655a4608bSchristos 	    orig->permitopen, orig->npermitopen) != 0 ||
69755a4608bSchristos 	    dup_strings(&ret->permitlisten, &ret->npermitlisten,
69855a4608bSchristos 	    orig->permitlisten, orig->npermitlisten) != 0) {
699ffae97bbSchristos 		sshauthopt_free(ret);
700ffae97bbSchristos 		return NULL;
701ffae97bbSchristos 	}
702ffae97bbSchristos 	return ret;
703ffae97bbSchristos }
704ffae97bbSchristos 
705ffae97bbSchristos static int
serialise_array(struct sshbuf * m,char ** a,size_t n)706ffae97bbSchristos serialise_array(struct sshbuf *m, char **a, size_t n)
707ffae97bbSchristos {
708ffae97bbSchristos 	struct sshbuf *b;
709ffae97bbSchristos 	size_t i;
710*a629fefcSchristos 	int r = SSH_ERR_INTERNAL_ERROR;
711ffae97bbSchristos 
712ffae97bbSchristos 	if (n > INT_MAX)
713ffae97bbSchristos 		return SSH_ERR_INTERNAL_ERROR;
714ffae97bbSchristos 
715ffae97bbSchristos 	if ((b = sshbuf_new()) == NULL) {
716ffae97bbSchristos 		return SSH_ERR_ALLOC_FAIL;
717ffae97bbSchristos 	}
718ffae97bbSchristos 	for (i = 0; i < n; i++) {
719*a629fefcSchristos 		if ((r = sshbuf_put_cstring(b, a[i])) != 0)
720*a629fefcSchristos 			goto out;
721ffae97bbSchristos 	}
722ffae97bbSchristos 	if ((r = sshbuf_put_u32(m, n)) != 0 ||
723*a629fefcSchristos 	    (r = sshbuf_put_stringb(m, b)) != 0)
724*a629fefcSchristos 		goto out;
725*a629fefcSchristos 	/* success */
726*a629fefcSchristos 	r = 0;
727*a629fefcSchristos  out:
728ffae97bbSchristos 	sshbuf_free(b);
729ffae97bbSchristos 	return r;
730ffae97bbSchristos }
73134b27b53Sadam 
732ffae97bbSchristos static int
deserialise_array(struct sshbuf * m,char *** ap,size_t * np)733ffae97bbSchristos deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
734ffae97bbSchristos {
735ffae97bbSchristos 	char **a = NULL;
736ffae97bbSchristos 	size_t i, n = 0;
737ffae97bbSchristos 	struct sshbuf *b = NULL;
738ffae97bbSchristos 	u_int tmp;
739ffae97bbSchristos 	int r = SSH_ERR_INTERNAL_ERROR;
740ffae97bbSchristos 
741ffae97bbSchristos 	if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
742ffae97bbSchristos 	    (r = sshbuf_froms(m, &b)) != 0)
743ffae97bbSchristos 		goto out;
744ffae97bbSchristos 	if (tmp > INT_MAX) {
745ffae97bbSchristos 		r = SSH_ERR_INVALID_FORMAT;
746ffae97bbSchristos 		goto out;
747ffae97bbSchristos 	}
748ffae97bbSchristos 	n = tmp;
749ffae97bbSchristos 	if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
750ffae97bbSchristos 		r = SSH_ERR_ALLOC_FAIL;
751ffae97bbSchristos 		goto out;
752ffae97bbSchristos 	}
753ffae97bbSchristos 	for (i = 0; i < n; i++) {
754ffae97bbSchristos 		if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
755ffae97bbSchristos 			goto out;
756ffae97bbSchristos 	}
757ffae97bbSchristos 	/* success */
758ffae97bbSchristos 	r = 0;
759ffae97bbSchristos 	*ap = a;
760ffae97bbSchristos 	a = NULL;
761ffae97bbSchristos 	*np = n;
762ffae97bbSchristos 	n = 0;
763ffae97bbSchristos  out:
7648db691beSchristos 	if (a != NULL) {
765ffae97bbSchristos 		for (i = 0; i < n; i++)
766ffae97bbSchristos 			free(a[i]);
767ffae97bbSchristos 		free(a);
7688db691beSchristos 	}
769ffae97bbSchristos 	sshbuf_free(b);
770ffae97bbSchristos 	return r;
771ffae97bbSchristos }
772ffae97bbSchristos 
773ffae97bbSchristos static int
serialise_nullable_string(struct sshbuf * m,const char * s)774ffae97bbSchristos serialise_nullable_string(struct sshbuf *m, const char *s)
775ffae97bbSchristos {
776ffae97bbSchristos 	int r;
777ffae97bbSchristos 
778ffae97bbSchristos 	if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
779ffae97bbSchristos 	    (r = sshbuf_put_cstring(m, s)) != 0)
780ffae97bbSchristos 		return r;
781ffae97bbSchristos 	return 0;
782ffae97bbSchristos }
783ffae97bbSchristos 
784ffae97bbSchristos static int
deserialise_nullable_string(struct sshbuf * m,char ** sp)785ffae97bbSchristos deserialise_nullable_string(struct sshbuf *m, char **sp)
786ffae97bbSchristos {
787ffae97bbSchristos 	int r;
788ffae97bbSchristos 	u_char flag;
789ffae97bbSchristos 
790ffae97bbSchristos 	*sp = NULL;
791ffae97bbSchristos 	if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
792ffae97bbSchristos 	    (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
793ffae97bbSchristos 		return r;
794ffae97bbSchristos 	return 0;
795ffae97bbSchristos }
796ffae97bbSchristos 
797ffae97bbSchristos int
sshauthopt_serialise(const struct sshauthopt * opts,struct sshbuf * m,int untrusted)798ffae97bbSchristos sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
799ffae97bbSchristos     int untrusted)
800ffae97bbSchristos {
801ffae97bbSchristos 	int r = SSH_ERR_INTERNAL_ERROR;
802ffae97bbSchristos 
803ed75d7a8Schristos 	/* Flag options */
804ffae97bbSchristos 	if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
805ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
806ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
807ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
808ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
809ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
810ffae97bbSchristos 	    (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
8112d3b0f52Schristos 	    (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
8122d3b0f52Schristos 	    (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
813ed75d7a8Schristos 		return r;
814ed75d7a8Schristos 
815ed75d7a8Schristos 	/* Simple integer options */
816ed75d7a8Schristos 	if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
817ffae97bbSchristos 		return r;
818ffae97bbSchristos 
819ffae97bbSchristos 	/* tunnel number can be negative to indicate "unset" */
820ffae97bbSchristos 	if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
821ffae97bbSchristos 	    (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
822ffae97bbSchristos 	    0 : (u_int)opts->force_tun_device)) != 0)
823ffae97bbSchristos 		return r;
824ffae97bbSchristos 
825ffae97bbSchristos 	/* String options; these may be NULL */
826ffae97bbSchristos 	if ((r = serialise_nullable_string(m,
827ffae97bbSchristos 	    untrusted ? "yes" : opts->cert_principals)) != 0 ||
828ffae97bbSchristos 	    (r = serialise_nullable_string(m,
829ffae97bbSchristos 	    untrusted ? "true" : opts->force_command)) != 0 ||
830ffae97bbSchristos 	    (r = serialise_nullable_string(m,
831ffae97bbSchristos 	    untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
832ffae97bbSchristos 	    (r = serialise_nullable_string(m,
833ffae97bbSchristos 	    untrusted ? NULL : opts->required_from_host_keys)) != 0)
834ffae97bbSchristos 		return r;
835ffae97bbSchristos 
836ffae97bbSchristos 	/* Array options */
837ffae97bbSchristos 	if ((r = serialise_array(m, opts->env,
838ffae97bbSchristos 	    untrusted ? 0 : opts->nenv)) != 0 ||
839ffae97bbSchristos 	    (r = serialise_array(m, opts->permitopen,
84055a4608bSchristos 	    untrusted ? 0 : opts->npermitopen)) != 0 ||
84155a4608bSchristos 	    (r = serialise_array(m, opts->permitlisten,
84255a4608bSchristos 	    untrusted ? 0 : opts->npermitlisten)) != 0)
843ffae97bbSchristos 		return r;
844ffae97bbSchristos 
845ffae97bbSchristos 	/* success */
846ffae97bbSchristos 	return 0;
847ffae97bbSchristos }
848ffae97bbSchristos 
849ffae97bbSchristos int
sshauthopt_deserialise(struct sshbuf * m,struct sshauthopt ** optsp)850ffae97bbSchristos sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
851ffae97bbSchristos {
852ffae97bbSchristos 	struct sshauthopt *opts = NULL;
853ffae97bbSchristos 	int r = SSH_ERR_INTERNAL_ERROR;
854ffae97bbSchristos 	u_char f;
855ffae97bbSchristos 	u_int tmp;
856ffae97bbSchristos 
857ffae97bbSchristos 	if ((opts = calloc(1, sizeof(*opts))) == NULL)
858ffae97bbSchristos 		return SSH_ERR_ALLOC_FAIL;
859ffae97bbSchristos 
860ed75d7a8Schristos 	/* Flag options */
861ffae97bbSchristos #define OPT_FLAG(x) \
862ffae97bbSchristos 	do { \
863ffae97bbSchristos 		if ((r = sshbuf_get_u8(m, &f)) != 0) \
864ffae97bbSchristos 			goto out; \
865ffae97bbSchristos 		opts->x = f; \
866ffae97bbSchristos 	} while (0)
867ffae97bbSchristos 	OPT_FLAG(permit_port_forwarding_flag);
868ffae97bbSchristos 	OPT_FLAG(permit_agent_forwarding_flag);
869ffae97bbSchristos 	OPT_FLAG(permit_x11_forwarding_flag);
870ffae97bbSchristos 	OPT_FLAG(permit_pty_flag);
871ffae97bbSchristos 	OPT_FLAG(permit_user_rc);
872ffae97bbSchristos 	OPT_FLAG(restricted);
873ffae97bbSchristos 	OPT_FLAG(cert_authority);
874ed75d7a8Schristos 	OPT_FLAG(no_require_user_presence);
8752d3b0f52Schristos 	OPT_FLAG(require_verify);
876ffae97bbSchristos #undef OPT_FLAG
877ffae97bbSchristos 
878ed75d7a8Schristos 	/* Simple integer options */
879ffae97bbSchristos 	if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
880ffae97bbSchristos 		goto out;
881ffae97bbSchristos 
882ffae97bbSchristos 	/* tunnel number can be negative to indicate "unset" */
883ffae97bbSchristos 	if ((r = sshbuf_get_u8(m, &f)) != 0 ||
884ffae97bbSchristos 	    (r = sshbuf_get_u32(m, &tmp)) != 0)
885ffae97bbSchristos 		goto out;
886ffae97bbSchristos 	opts->force_tun_device = f ? -1 : (int)tmp;
887ffae97bbSchristos 
888ffae97bbSchristos 	/* String options may be NULL */
889ffae97bbSchristos 	if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
890ffae97bbSchristos 	    (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
891ffae97bbSchristos 	    (r = deserialise_nullable_string(m,
892ffae97bbSchristos 	    &opts->required_from_host_cert)) != 0 ||
893ffae97bbSchristos 	    (r = deserialise_nullable_string(m,
894ffae97bbSchristos 	    &opts->required_from_host_keys)) != 0)
895ffae97bbSchristos 		goto out;
896ffae97bbSchristos 
897ffae97bbSchristos 	/* Array options */
898ffae97bbSchristos 	if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
899ffae97bbSchristos 	    (r = deserialise_array(m,
90055a4608bSchristos 	    &opts->permitopen, &opts->npermitopen)) != 0 ||
90155a4608bSchristos 	    (r = deserialise_array(m,
90255a4608bSchristos 	    &opts->permitlisten, &opts->npermitlisten)) != 0)
903ffae97bbSchristos 		goto out;
904ffae97bbSchristos 
905ffae97bbSchristos 	/* success */
906ffae97bbSchristos 	r = 0;
907ffae97bbSchristos 	*optsp = opts;
908ffae97bbSchristos 	opts = NULL;
909ffae97bbSchristos  out:
910ffae97bbSchristos 	sshauthopt_free(opts);
911ffae97bbSchristos 	return r;
912ffae97bbSchristos }
913