1*ba1276acSMatthew Dillon /* $OpenBSD: auth-options.c,v 1.101 2023/07/14 07:44:21 dtucker 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>
27*ba1276acSMatthew Dillon #ifdef HAVE_STDINT_H
28*ba1276acSMatthew Dillon # include <stdint.h>
29*ba1276acSMatthew Dillon #endif
3018de8d7fSPeter Avalos #include <stdarg.h>
31664f4763Szrj #include <ctype.h>
32664f4763Szrj #include <limits.h>
3318de8d7fSPeter Avalos
3418de8d7fSPeter Avalos #include "openbsd-compat/sys-queue.h"
35e9778795SPeter Avalos
3618de8d7fSPeter Avalos #include "xmalloc.h"
37e9778795SPeter Avalos #include "ssherr.h"
3818de8d7fSPeter Avalos #include "log.h"
39e9778795SPeter Avalos #include "sshbuf.h"
4036e94dc5SPeter Avalos #include "misc.h"
41e9778795SPeter Avalos #include "sshkey.h"
42664f4763Szrj #include "match.h"
43664f4763Szrj #include "ssh2.h"
44856ea928SPeter Avalos #include "auth-options.h"
4518de8d7fSPeter Avalos
46664f4763Szrj static int
dup_strings(char *** dstp,size_t * ndstp,char ** src,size_t nsrc)47664f4763Szrj dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
48664f4763Szrj {
49664f4763Szrj char **dst;
50664f4763Szrj size_t i, j;
5118de8d7fSPeter Avalos
52664f4763Szrj *dstp = NULL;
53664f4763Szrj *ndstp = 0;
54*ba1276acSMatthew Dillon
55664f4763Szrj if (nsrc == 0)
56664f4763Szrj return 0;
57*ba1276acSMatthew Dillon if (nsrc >= SIZE_MAX / sizeof(*src) ||
58*ba1276acSMatthew Dillon (dst = calloc(nsrc, sizeof(*src))) == NULL)
59664f4763Szrj return -1;
60664f4763Szrj for (i = 0; i < nsrc; i++) {
61664f4763Szrj if ((dst[i] = strdup(src[i])) == NULL) {
62664f4763Szrj for (j = 0; j < i; j++)
63664f4763Szrj free(dst[j]);
64664f4763Szrj free(dst);
65664f4763Szrj return -1;
66664f4763Szrj }
67664f4763Szrj }
68664f4763Szrj /* success */
69664f4763Szrj *dstp = dst;
70664f4763Szrj *ndstp = nsrc;
7118de8d7fSPeter Avalos return 0;
7218de8d7fSPeter Avalos }
73856ea928SPeter Avalos
74856ea928SPeter Avalos #define OPTIONS_CRITICAL 1
75856ea928SPeter Avalos #define OPTIONS_EXTENSIONS 2
76856ea928SPeter Avalos static int
cert_option_list(struct sshauthopt * opts,struct sshbuf * oblob,u_int which,int crit)77664f4763Szrj cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
78664f4763Szrj u_int which, int crit)
79856ea928SPeter Avalos {
80856ea928SPeter Avalos char *command, *allowed;
8136e94dc5SPeter Avalos char *name = NULL;
82e9778795SPeter Avalos struct sshbuf *c = NULL, *data = NULL;
83664f4763Szrj int r, ret = -1, found;
84856ea928SPeter Avalos
85e9778795SPeter Avalos if ((c = sshbuf_fromb(oblob)) == NULL) {
8650a69bb5SSascha Wildner error_f("sshbuf_fromb failed");
87856ea928SPeter Avalos goto out;
88856ea928SPeter Avalos }
89e9778795SPeter Avalos
90e9778795SPeter Avalos while (sshbuf_len(c) > 0) {
91e9778795SPeter Avalos sshbuf_free(data);
92e9778795SPeter Avalos data = NULL;
93e9778795SPeter Avalos if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
94e9778795SPeter Avalos (r = sshbuf_froms(c, &data)) != 0) {
9550a69bb5SSascha Wildner error_r(r, "Unable to parse certificate options");
96e9778795SPeter Avalos goto out;
97e9778795SPeter Avalos }
98e9778795SPeter Avalos debug3("found certificate option \"%.100s\" len %zu",
99e9778795SPeter Avalos name, sshbuf_len(data));
100856ea928SPeter Avalos found = 0;
101856ea928SPeter Avalos if ((which & OPTIONS_EXTENSIONS) != 0) {
1020cbfa66cSDaniel Fojt if (strcmp(name, "no-touch-required") == 0) {
1030cbfa66cSDaniel Fojt opts->no_require_user_presence = 1;
1040cbfa66cSDaniel Fojt found = 1;
1050cbfa66cSDaniel Fojt } else if (strcmp(name, "permit-X11-forwarding") == 0) {
106664f4763Szrj opts->permit_x11_forwarding_flag = 1;
107856ea928SPeter Avalos found = 1;
108856ea928SPeter Avalos } else if (strcmp(name,
109856ea928SPeter Avalos "permit-agent-forwarding") == 0) {
110664f4763Szrj opts->permit_agent_forwarding_flag = 1;
111856ea928SPeter Avalos found = 1;
112856ea928SPeter Avalos } else if (strcmp(name,
113856ea928SPeter Avalos "permit-port-forwarding") == 0) {
114664f4763Szrj opts->permit_port_forwarding_flag = 1;
115856ea928SPeter Avalos found = 1;
116856ea928SPeter Avalos } else if (strcmp(name, "permit-pty") == 0) {
117664f4763Szrj opts->permit_pty_flag = 1;
118856ea928SPeter Avalos found = 1;
119856ea928SPeter Avalos } else if (strcmp(name, "permit-user-rc") == 0) {
120664f4763Szrj opts->permit_user_rc = 1;
121856ea928SPeter Avalos found = 1;
122856ea928SPeter Avalos }
123856ea928SPeter Avalos }
124856ea928SPeter Avalos if (!found && (which & OPTIONS_CRITICAL) != 0) {
12550a69bb5SSascha Wildner if (strcmp(name, "verify-required") == 0) {
12650a69bb5SSascha Wildner opts->require_verify = 1;
12750a69bb5SSascha Wildner found = 1;
12850a69bb5SSascha Wildner } else if (strcmp(name, "force-command") == 0) {
129e9778795SPeter Avalos if ((r = sshbuf_get_cstring(data, &command,
130e9778795SPeter Avalos NULL)) != 0) {
13150a69bb5SSascha Wildner error_r(r, "Unable to parse \"%s\" "
13250a69bb5SSascha Wildner "section", name);
133856ea928SPeter Avalos goto out;
134856ea928SPeter Avalos }
135664f4763Szrj if (opts->force_command != NULL) {
136856ea928SPeter Avalos error("Certificate has multiple "
137856ea928SPeter Avalos "force-command options");
13836e94dc5SPeter Avalos free(command);
139856ea928SPeter Avalos goto out;
140856ea928SPeter Avalos }
141664f4763Szrj opts->force_command = command;
142856ea928SPeter Avalos found = 1;
14350a69bb5SSascha Wildner } else if (strcmp(name, "source-address") == 0) {
144e9778795SPeter Avalos if ((r = sshbuf_get_cstring(data, &allowed,
145e9778795SPeter Avalos NULL)) != 0) {
14650a69bb5SSascha Wildner error_r(r, "Unable to parse \"%s\" "
14750a69bb5SSascha Wildner "section", name);
148856ea928SPeter Avalos goto out;
149856ea928SPeter Avalos }
150664f4763Szrj if (opts->required_from_host_cert != NULL) {
151856ea928SPeter Avalos error("Certificate has multiple "
152856ea928SPeter Avalos "source-address options");
15336e94dc5SPeter Avalos free(allowed);
154856ea928SPeter Avalos goto out;
155856ea928SPeter Avalos }
156664f4763Szrj /* Check syntax */
157664f4763Szrj if (addr_match_cidr_list(NULL, allowed) == -1) {
158856ea928SPeter Avalos error("Certificate source-address "
159856ea928SPeter Avalos "contents invalid");
160856ea928SPeter Avalos goto out;
161856ea928SPeter Avalos }
162664f4763Szrj opts->required_from_host_cert = allowed;
163856ea928SPeter Avalos found = 1;
164856ea928SPeter Avalos }
165856ea928SPeter Avalos }
166856ea928SPeter Avalos
167856ea928SPeter Avalos if (!found) {
168856ea928SPeter Avalos if (crit) {
169856ea928SPeter Avalos error("Certificate critical option \"%s\" "
170856ea928SPeter Avalos "is not supported", name);
171856ea928SPeter Avalos goto out;
172856ea928SPeter Avalos } else {
173856ea928SPeter Avalos logit("Certificate extension \"%s\" "
174856ea928SPeter Avalos "is not supported", name);
175856ea928SPeter Avalos }
176e9778795SPeter Avalos } else if (sshbuf_len(data) != 0) {
177856ea928SPeter Avalos error("Certificate option \"%s\" corrupt "
178856ea928SPeter Avalos "(extra data)", name);
179856ea928SPeter Avalos goto out;
180856ea928SPeter Avalos }
18136e94dc5SPeter Avalos free(name);
18236e94dc5SPeter Avalos name = NULL;
183856ea928SPeter Avalos }
184856ea928SPeter Avalos /* successfully parsed all options */
185856ea928SPeter Avalos ret = 0;
186856ea928SPeter Avalos
187856ea928SPeter Avalos out:
18836e94dc5SPeter Avalos free(name);
189e9778795SPeter Avalos sshbuf_free(data);
190e9778795SPeter Avalos sshbuf_free(c);
191856ea928SPeter Avalos return ret;
192856ea928SPeter Avalos }
193856ea928SPeter Avalos
194664f4763Szrj struct sshauthopt *
sshauthopt_new(void)195664f4763Szrj sshauthopt_new(void)
196856ea928SPeter Avalos {
197664f4763Szrj struct sshauthopt *ret;
198856ea928SPeter Avalos
199664f4763Szrj if ((ret = calloc(1, sizeof(*ret))) == NULL)
200664f4763Szrj return NULL;
201664f4763Szrj ret->force_tun_device = -1;
202664f4763Szrj return ret;
203664f4763Szrj }
204ce74bacaSMatthew Dillon
205664f4763Szrj void
sshauthopt_free(struct sshauthopt * opts)206664f4763Szrj sshauthopt_free(struct sshauthopt *opts)
207664f4763Szrj {
208664f4763Szrj size_t i;
209856ea928SPeter Avalos
210664f4763Szrj if (opts == NULL)
211664f4763Szrj return;
212664f4763Szrj
213664f4763Szrj free(opts->cert_principals);
214664f4763Szrj free(opts->force_command);
215664f4763Szrj free(opts->required_from_host_cert);
216664f4763Szrj free(opts->required_from_host_keys);
217664f4763Szrj
218664f4763Szrj for (i = 0; i < opts->nenv; i++)
219664f4763Szrj free(opts->env[i]);
220664f4763Szrj free(opts->env);
221664f4763Szrj
222664f4763Szrj for (i = 0; i < opts->npermitopen; i++)
223664f4763Szrj free(opts->permitopen[i]);
224664f4763Szrj free(opts->permitopen);
225664f4763Szrj
226664f4763Szrj for (i = 0; i < opts->npermitlisten; i++)
227664f4763Szrj free(opts->permitlisten[i]);
228664f4763Szrj free(opts->permitlisten);
229664f4763Szrj
2300cbfa66cSDaniel Fojt freezero(opts, sizeof(*opts));
231664f4763Szrj }
232664f4763Szrj
233664f4763Szrj struct sshauthopt *
sshauthopt_new_with_keys_defaults(void)234664f4763Szrj sshauthopt_new_with_keys_defaults(void)
235664f4763Szrj {
236664f4763Szrj struct sshauthopt *ret = NULL;
237664f4763Szrj
238664f4763Szrj if ((ret = sshauthopt_new()) == NULL)
239664f4763Szrj return NULL;
240664f4763Szrj
241664f4763Szrj /* Defaults for authorized_keys flags */
242664f4763Szrj ret->permit_port_forwarding_flag = 1;
243664f4763Szrj ret->permit_agent_forwarding_flag = 1;
244664f4763Szrj ret->permit_x11_forwarding_flag = 1;
245664f4763Szrj ret->permit_pty_flag = 1;
246664f4763Szrj ret->permit_user_rc = 1;
247664f4763Szrj return ret;
248664f4763Szrj }
249664f4763Szrj
250ce74bacaSMatthew Dillon /*
251664f4763Szrj * Parse and record a permitopen/permitlisten directive.
252664f4763Szrj * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
253ce74bacaSMatthew Dillon */
254664f4763Szrj static int
handle_permit(const char ** optsp,int allow_bare_port,char *** permitsp,size_t * npermitsp,const char ** errstrp)255664f4763Szrj handle_permit(const char **optsp, int allow_bare_port,
256664f4763Szrj char ***permitsp, size_t *npermitsp, const char **errstrp)
257664f4763Szrj {
258664f4763Szrj char *opt, *tmp, *cp, *host, **permits = *permitsp;
259664f4763Szrj size_t npermits = *npermitsp;
260664f4763Szrj const char *errstr = "unknown error";
261664f4763Szrj
2620cbfa66cSDaniel Fojt if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
263664f4763Szrj *errstrp = "too many permission directives";
264ce74bacaSMatthew Dillon return -1;
265856ea928SPeter Avalos }
266664f4763Szrj if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
267664f4763Szrj return -1;
268664f4763Szrj }
269664f4763Szrj if (allow_bare_port && strchr(opt, ':') == NULL) {
270664f4763Szrj /*
271664f4763Szrj * Allow a bare port number in permitlisten to indicate a
272664f4763Szrj * listen_host wildcard.
273664f4763Szrj */
2740cbfa66cSDaniel Fojt if (asprintf(&tmp, "*:%s", opt) == -1) {
2750cbfa66cSDaniel Fojt free(opt);
276664f4763Szrj *errstrp = "memory allocation failed";
277664f4763Szrj return -1;
278664f4763Szrj }
279664f4763Szrj free(opt);
280664f4763Szrj opt = tmp;
281664f4763Szrj }
282664f4763Szrj if ((tmp = strdup(opt)) == NULL) {
283664f4763Szrj free(opt);
284664f4763Szrj *errstrp = "memory allocation failed";
285664f4763Szrj return -1;
286664f4763Szrj }
287664f4763Szrj cp = tmp;
288664f4763Szrj /* validate syntax before recording it. */
289ee116499SAntonio Huete Jimenez host = hpdelim2(&cp, NULL);
290664f4763Szrj if (host == NULL || strlen(host) >= NI_MAXHOST) {
291664f4763Szrj free(tmp);
292664f4763Szrj free(opt);
293664f4763Szrj *errstrp = "invalid permission hostname";
294664f4763Szrj return -1;
295664f4763Szrj }
296664f4763Szrj /*
297664f4763Szrj * don't want to use permitopen_port to avoid
298664f4763Szrj * dependency on channels.[ch] here.
299664f4763Szrj */
300664f4763Szrj if (cp == NULL ||
301664f4763Szrj (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
302664f4763Szrj free(tmp);
303664f4763Szrj free(opt);
304664f4763Szrj *errstrp = "invalid permission port";
305664f4763Szrj return -1;
306664f4763Szrj }
307664f4763Szrj /* XXX - add streamlocal support */
308664f4763Szrj free(tmp);
309664f4763Szrj /* Record it */
310664f4763Szrj if ((permits = recallocarray(permits, npermits, npermits + 1,
311664f4763Szrj sizeof(*permits))) == NULL) {
312664f4763Szrj free(opt);
313664f4763Szrj /* NB. don't update *permitsp if alloc fails */
314664f4763Szrj *errstrp = "memory allocation failed";
315664f4763Szrj return -1;
316664f4763Szrj }
317664f4763Szrj permits[npermits++] = opt;
318664f4763Szrj *permitsp = permits;
319664f4763Szrj *npermitsp = npermits;
320856ea928SPeter Avalos return 0;
321856ea928SPeter Avalos }
322856ea928SPeter Avalos
323664f4763Szrj struct sshauthopt *
sshauthopt_parse(const char * opts,const char ** errstrp)324664f4763Szrj sshauthopt_parse(const char *opts, const char **errstrp)
325664f4763Szrj {
326664f4763Szrj char **oarray, *opt, *cp, *tmp;
327664f4763Szrj int r;
328664f4763Szrj struct sshauthopt *ret = NULL;
329664f4763Szrj const char *errstr = "unknown error";
330664f4763Szrj uint64_t valid_before;
33150a69bb5SSascha Wildner size_t i, l;
332664f4763Szrj
333664f4763Szrj if (errstrp != NULL)
334664f4763Szrj *errstrp = NULL;
335664f4763Szrj if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
336664f4763Szrj goto alloc_fail;
337664f4763Szrj
338664f4763Szrj if (opts == NULL)
339664f4763Szrj return ret;
340664f4763Szrj
341664f4763Szrj while (*opts && *opts != ' ' && *opts != '\t') {
342664f4763Szrj /* flag options */
343664f4763Szrj if ((r = opt_flag("restrict", 0, &opts)) != -1) {
344664f4763Szrj ret->restricted = 1;
345664f4763Szrj ret->permit_port_forwarding_flag = 0;
346664f4763Szrj ret->permit_agent_forwarding_flag = 0;
347664f4763Szrj ret->permit_x11_forwarding_flag = 0;
348664f4763Szrj ret->permit_pty_flag = 0;
349664f4763Szrj ret->permit_user_rc = 0;
350664f4763Szrj } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
351664f4763Szrj ret->cert_authority = r;
352664f4763Szrj } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
353664f4763Szrj ret->permit_port_forwarding_flag = r == 1;
354664f4763Szrj } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
355664f4763Szrj ret->permit_agent_forwarding_flag = r == 1;
356664f4763Szrj } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
357664f4763Szrj ret->permit_x11_forwarding_flag = r == 1;
3580cbfa66cSDaniel Fojt } else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
3590cbfa66cSDaniel Fojt ret->no_require_user_presence = r != 1; /* NB. flip */
36050a69bb5SSascha Wildner } else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
36150a69bb5SSascha Wildner ret->require_verify = r == 1;
362664f4763Szrj } else if ((r = opt_flag("pty", 1, &opts)) != -1) {
363664f4763Szrj ret->permit_pty_flag = r == 1;
364664f4763Szrj } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
365664f4763Szrj ret->permit_user_rc = r == 1;
366664f4763Szrj } else if (opt_match(&opts, "command")) {
367664f4763Szrj if (ret->force_command != NULL) {
368664f4763Szrj errstr = "multiple \"command\" clauses";
369664f4763Szrj goto fail;
370664f4763Szrj }
371664f4763Szrj ret->force_command = opt_dequote(&opts, &errstr);
372664f4763Szrj if (ret->force_command == NULL)
373664f4763Szrj goto fail;
374664f4763Szrj } else if (opt_match(&opts, "principals")) {
375664f4763Szrj if (ret->cert_principals != NULL) {
376664f4763Szrj errstr = "multiple \"principals\" clauses";
377664f4763Szrj goto fail;
378664f4763Szrj }
379664f4763Szrj ret->cert_principals = opt_dequote(&opts, &errstr);
380664f4763Szrj if (ret->cert_principals == NULL)
381664f4763Szrj goto fail;
382664f4763Szrj } else if (opt_match(&opts, "from")) {
383664f4763Szrj if (ret->required_from_host_keys != NULL) {
384664f4763Szrj errstr = "multiple \"from\" clauses";
385664f4763Szrj goto fail;
386664f4763Szrj }
387664f4763Szrj ret->required_from_host_keys = opt_dequote(&opts,
388664f4763Szrj &errstr);
389664f4763Szrj if (ret->required_from_host_keys == NULL)
390664f4763Szrj goto fail;
391664f4763Szrj } else if (opt_match(&opts, "expiry-time")) {
392664f4763Szrj if ((opt = opt_dequote(&opts, &errstr)) == NULL)
393664f4763Szrj goto fail;
394664f4763Szrj if (parse_absolute_time(opt, &valid_before) != 0 ||
395664f4763Szrj valid_before == 0) {
396664f4763Szrj free(opt);
397664f4763Szrj errstr = "invalid expires time";
398664f4763Szrj goto fail;
399664f4763Szrj }
400664f4763Szrj free(opt);
401664f4763Szrj if (ret->valid_before == 0 ||
402664f4763Szrj valid_before < ret->valid_before)
403664f4763Szrj ret->valid_before = valid_before;
404664f4763Szrj } else if (opt_match(&opts, "environment")) {
40550a69bb5SSascha Wildner if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
406664f4763Szrj errstr = "too many environment strings";
407664f4763Szrj goto fail;
408664f4763Szrj }
409664f4763Szrj if ((opt = opt_dequote(&opts, &errstr)) == NULL)
410664f4763Szrj goto fail;
411664f4763Szrj /* env name must be alphanumeric and followed by '=' */
412664f4763Szrj if ((tmp = strchr(opt, '=')) == NULL) {
413664f4763Szrj free(opt);
414664f4763Szrj errstr = "invalid environment string";
415664f4763Szrj goto fail;
416664f4763Szrj }
41750a69bb5SSascha Wildner if ((cp = strdup(opt)) == NULL) {
41850a69bb5SSascha Wildner free(opt);
419664f4763Szrj goto alloc_fail;
42050a69bb5SSascha Wildner }
42150a69bb5SSascha Wildner l = (size_t)(tmp - opt);
42250a69bb5SSascha Wildner cp[l] = '\0'; /* truncate at '=' */
423664f4763Szrj if (!valid_env_name(cp)) {
424664f4763Szrj free(cp);
425664f4763Szrj free(opt);
426664f4763Szrj errstr = "invalid environment string";
427664f4763Szrj goto fail;
428664f4763Szrj }
42950a69bb5SSascha Wildner /* Check for duplicates; XXX O(n*log(n)) */
43050a69bb5SSascha Wildner for (i = 0; i < ret->nenv; i++) {
43150a69bb5SSascha Wildner if (strncmp(ret->env[i], cp, l) == 0 &&
43250a69bb5SSascha Wildner ret->env[i][l] == '=')
43350a69bb5SSascha Wildner break;
43450a69bb5SSascha Wildner }
435664f4763Szrj free(cp);
43650a69bb5SSascha Wildner /* First match wins */
43750a69bb5SSascha Wildner if (i >= ret->nenv) {
438664f4763Szrj /* Append it. */
439664f4763Szrj oarray = ret->env;
44050a69bb5SSascha Wildner if ((ret->env = recallocarray(ret->env,
44150a69bb5SSascha Wildner ret->nenv, ret->nenv + 1,
44250a69bb5SSascha Wildner sizeof(*ret->env))) == NULL) {
443664f4763Szrj free(opt);
44450a69bb5SSascha Wildner /* put it back for cleanup */
44550a69bb5SSascha Wildner ret->env = oarray;
446664f4763Szrj goto alloc_fail;
447664f4763Szrj }
448664f4763Szrj ret->env[ret->nenv++] = opt;
44950a69bb5SSascha Wildner opt = NULL; /* transferred */
45050a69bb5SSascha Wildner }
45150a69bb5SSascha Wildner free(opt);
452664f4763Szrj } else if (opt_match(&opts, "permitopen")) {
453664f4763Szrj if (handle_permit(&opts, 0, &ret->permitopen,
454664f4763Szrj &ret->npermitopen, &errstr) != 0)
455664f4763Szrj goto fail;
456664f4763Szrj } else if (opt_match(&opts, "permitlisten")) {
457664f4763Szrj if (handle_permit(&opts, 1, &ret->permitlisten,
458664f4763Szrj &ret->npermitlisten, &errstr) != 0)
459664f4763Szrj goto fail;
460664f4763Szrj } else if (opt_match(&opts, "tunnel")) {
461664f4763Szrj if ((opt = opt_dequote(&opts, &errstr)) == NULL)
462664f4763Szrj goto fail;
463664f4763Szrj ret->force_tun_device = a2tun(opt, NULL);
464664f4763Szrj free(opt);
465664f4763Szrj if (ret->force_tun_device == SSH_TUNID_ERR) {
466664f4763Szrj errstr = "invalid tun device";
467664f4763Szrj goto fail;
468664f4763Szrj }
469664f4763Szrj }
470664f4763Szrj /*
471664f4763Szrj * Skip the comma, and move to the next option
472664f4763Szrj * (or break out if there are no more).
473664f4763Szrj */
474664f4763Szrj if (*opts == '\0' || *opts == ' ' || *opts == '\t')
475664f4763Szrj break; /* End of options. */
476664f4763Szrj /* Anything other than a comma is an unknown option */
477664f4763Szrj if (*opts != ',') {
478664f4763Szrj errstr = "unknown key option";
479664f4763Szrj goto fail;
480664f4763Szrj }
481664f4763Szrj opts++;
482664f4763Szrj if (*opts == '\0') {
483664f4763Szrj errstr = "unexpected end-of-options";
484664f4763Szrj goto fail;
485664f4763Szrj }
486664f4763Szrj }
487664f4763Szrj
488664f4763Szrj /* success */
489664f4763Szrj if (errstrp != NULL)
490664f4763Szrj *errstrp = NULL;
491664f4763Szrj return ret;
492664f4763Szrj
493664f4763Szrj alloc_fail:
494664f4763Szrj errstr = "memory allocation failed";
495664f4763Szrj fail:
496664f4763Szrj sshauthopt_free(ret);
497664f4763Szrj if (errstrp != NULL)
498664f4763Szrj *errstrp = errstr;
499664f4763Szrj return NULL;
500664f4763Szrj }
501664f4763Szrj
502664f4763Szrj struct sshauthopt *
sshauthopt_from_cert(struct sshkey * k)503664f4763Szrj sshauthopt_from_cert(struct sshkey *k)
504664f4763Szrj {
505664f4763Szrj struct sshauthopt *ret;
506664f4763Szrj
507664f4763Szrj if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
508664f4763Szrj k->cert->type != SSH2_CERT_TYPE_USER)
509664f4763Szrj return NULL;
510664f4763Szrj
511664f4763Szrj if ((ret = sshauthopt_new()) == NULL)
512664f4763Szrj return NULL;
513664f4763Szrj
514664f4763Szrj /* Handle options and critical extensions separately */
515664f4763Szrj if (cert_option_list(ret, k->cert->critical,
516664f4763Szrj OPTIONS_CRITICAL, 1) == -1) {
517664f4763Szrj sshauthopt_free(ret);
518664f4763Szrj return NULL;
519664f4763Szrj }
520664f4763Szrj if (cert_option_list(ret, k->cert->extensions,
521664f4763Szrj OPTIONS_EXTENSIONS, 0) == -1) {
522664f4763Szrj sshauthopt_free(ret);
523664f4763Szrj return NULL;
524664f4763Szrj }
525664f4763Szrj /* success */
526664f4763Szrj return ret;
527664f4763Szrj }
528664f4763Szrj
529664f4763Szrj /*
530664f4763Szrj * Merges "additional" options to "primary" and returns the result.
531664f4763Szrj * NB. Some options from primary have primacy.
532664f4763Szrj */
533664f4763Szrj struct sshauthopt *
sshauthopt_merge(const struct sshauthopt * primary,const struct sshauthopt * additional,const char ** errstrp)534664f4763Szrj sshauthopt_merge(const struct sshauthopt *primary,
535664f4763Szrj const struct sshauthopt *additional, const char **errstrp)
536664f4763Szrj {
537664f4763Szrj struct sshauthopt *ret;
538664f4763Szrj const char *errstr = "internal error";
539664f4763Szrj const char *tmp;
540664f4763Szrj
541664f4763Szrj if (errstrp != NULL)
542664f4763Szrj *errstrp = NULL;
543664f4763Szrj
544664f4763Szrj if ((ret = sshauthopt_new()) == NULL)
545664f4763Szrj goto alloc_fail;
546664f4763Szrj
547664f4763Szrj /* cert_authority and cert_principals are cleared in result */
548664f4763Szrj
549664f4763Szrj /* Prefer access lists from primary. */
550664f4763Szrj /* XXX err is both set and mismatch? */
551664f4763Szrj tmp = primary->required_from_host_cert;
552664f4763Szrj if (tmp == NULL)
553664f4763Szrj tmp = additional->required_from_host_cert;
554664f4763Szrj if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
555664f4763Szrj goto alloc_fail;
556664f4763Szrj tmp = primary->required_from_host_keys;
557664f4763Szrj if (tmp == NULL)
558664f4763Szrj tmp = additional->required_from_host_keys;
559664f4763Szrj if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
560664f4763Szrj goto alloc_fail;
561664f4763Szrj
562664f4763Szrj /*
563664f4763Szrj * force_tun_device, permitopen/permitlisten and environment all
564664f4763Szrj * prefer the primary.
565664f4763Szrj */
566664f4763Szrj ret->force_tun_device = primary->force_tun_device;
567664f4763Szrj if (ret->force_tun_device == -1)
568664f4763Szrj ret->force_tun_device = additional->force_tun_device;
569664f4763Szrj if (primary->nenv > 0) {
570664f4763Szrj if (dup_strings(&ret->env, &ret->nenv,
571664f4763Szrj primary->env, primary->nenv) != 0)
572664f4763Szrj goto alloc_fail;
573664f4763Szrj } else if (additional->nenv) {
574664f4763Szrj if (dup_strings(&ret->env, &ret->nenv,
575664f4763Szrj additional->env, additional->nenv) != 0)
576664f4763Szrj goto alloc_fail;
577664f4763Szrj }
578664f4763Szrj if (primary->npermitopen > 0) {
579664f4763Szrj if (dup_strings(&ret->permitopen, &ret->npermitopen,
580664f4763Szrj primary->permitopen, primary->npermitopen) != 0)
581664f4763Szrj goto alloc_fail;
582664f4763Szrj } else if (additional->npermitopen > 0) {
583664f4763Szrj if (dup_strings(&ret->permitopen, &ret->npermitopen,
584664f4763Szrj additional->permitopen, additional->npermitopen) != 0)
585664f4763Szrj goto alloc_fail;
586664f4763Szrj }
587664f4763Szrj
588664f4763Szrj if (primary->npermitlisten > 0) {
589664f4763Szrj if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
590664f4763Szrj primary->permitlisten, primary->npermitlisten) != 0)
591664f4763Szrj goto alloc_fail;
592664f4763Szrj } else if (additional->npermitlisten > 0) {
593664f4763Szrj if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
594664f4763Szrj additional->permitlisten, additional->npermitlisten) != 0)
595664f4763Szrj goto alloc_fail;
596664f4763Szrj }
597664f4763Szrj
5980cbfa66cSDaniel Fojt #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
59950a69bb5SSascha Wildner #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
6000cbfa66cSDaniel Fojt /* Permissive flags are logical-AND (i.e. must be set in both) */
6010cbfa66cSDaniel Fojt OPTFLAG_AND(permit_port_forwarding_flag);
6020cbfa66cSDaniel Fojt OPTFLAG_AND(permit_agent_forwarding_flag);
6030cbfa66cSDaniel Fojt OPTFLAG_AND(permit_x11_forwarding_flag);
6040cbfa66cSDaniel Fojt OPTFLAG_AND(permit_pty_flag);
6050cbfa66cSDaniel Fojt OPTFLAG_AND(permit_user_rc);
6060cbfa66cSDaniel Fojt OPTFLAG_AND(no_require_user_presence);
60750a69bb5SSascha Wildner /* Restrictive flags are logical-OR (i.e. must be set in either) */
60850a69bb5SSascha Wildner OPTFLAG_OR(require_verify);
6090cbfa66cSDaniel Fojt #undef OPTFLAG_AND
610664f4763Szrj
611664f4763Szrj /* Earliest expiry time should win */
612664f4763Szrj if (primary->valid_before != 0)
613664f4763Szrj ret->valid_before = primary->valid_before;
614664f4763Szrj if (additional->valid_before != 0 &&
615664f4763Szrj additional->valid_before < ret->valid_before)
616664f4763Szrj ret->valid_before = additional->valid_before;
617664f4763Szrj
618664f4763Szrj /*
619664f4763Szrj * When both multiple forced-command are specified, only
620664f4763Szrj * proceed if they are identical, otherwise fail.
621664f4763Szrj */
622664f4763Szrj if (primary->force_command != NULL &&
623664f4763Szrj additional->force_command != NULL) {
624664f4763Szrj if (strcmp(primary->force_command,
625664f4763Szrj additional->force_command) == 0) {
626664f4763Szrj /* ok */
627664f4763Szrj ret->force_command = strdup(primary->force_command);
628664f4763Szrj if (ret->force_command == NULL)
629664f4763Szrj goto alloc_fail;
630664f4763Szrj } else {
631664f4763Szrj errstr = "forced command options do not match";
632664f4763Szrj goto fail;
633664f4763Szrj }
634664f4763Szrj } else if (primary->force_command != NULL) {
635664f4763Szrj if ((ret->force_command = strdup(
636664f4763Szrj primary->force_command)) == NULL)
637664f4763Szrj goto alloc_fail;
638664f4763Szrj } else if (additional->force_command != NULL) {
639664f4763Szrj if ((ret->force_command = strdup(
640664f4763Szrj additional->force_command)) == NULL)
641664f4763Szrj goto alloc_fail;
642664f4763Szrj }
643664f4763Szrj /* success */
644664f4763Szrj if (errstrp != NULL)
645664f4763Szrj *errstrp = NULL;
646664f4763Szrj return ret;
647664f4763Szrj
648664f4763Szrj alloc_fail:
649664f4763Szrj errstr = "memory allocation failed";
650664f4763Szrj fail:
651664f4763Szrj if (errstrp != NULL)
652664f4763Szrj *errstrp = errstr;
653664f4763Szrj sshauthopt_free(ret);
654664f4763Szrj return NULL;
655664f4763Szrj }
656664f4763Szrj
657664f4763Szrj /*
658664f4763Szrj * Copy options
659664f4763Szrj */
660664f4763Szrj struct sshauthopt *
sshauthopt_copy(const struct sshauthopt * orig)661664f4763Szrj sshauthopt_copy(const struct sshauthopt *orig)
662664f4763Szrj {
663664f4763Szrj struct sshauthopt *ret;
664664f4763Szrj
665664f4763Szrj if ((ret = sshauthopt_new()) == NULL)
666664f4763Szrj return NULL;
667664f4763Szrj
668664f4763Szrj #define OPTSCALAR(x) ret->x = orig->x
669664f4763Szrj OPTSCALAR(permit_port_forwarding_flag);
670664f4763Szrj OPTSCALAR(permit_agent_forwarding_flag);
671664f4763Szrj OPTSCALAR(permit_x11_forwarding_flag);
672664f4763Szrj OPTSCALAR(permit_pty_flag);
673664f4763Szrj OPTSCALAR(permit_user_rc);
674664f4763Szrj OPTSCALAR(restricted);
675664f4763Szrj OPTSCALAR(cert_authority);
676664f4763Szrj OPTSCALAR(force_tun_device);
677664f4763Szrj OPTSCALAR(valid_before);
6780cbfa66cSDaniel Fojt OPTSCALAR(no_require_user_presence);
67950a69bb5SSascha Wildner OPTSCALAR(require_verify);
680664f4763Szrj #undef OPTSCALAR
681664f4763Szrj #define OPTSTRING(x) \
682664f4763Szrj do { \
683664f4763Szrj if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
684664f4763Szrj sshauthopt_free(ret); \
685664f4763Szrj return NULL; \
686664f4763Szrj } \
687664f4763Szrj } while (0)
688664f4763Szrj OPTSTRING(cert_principals);
689664f4763Szrj OPTSTRING(force_command);
690664f4763Szrj OPTSTRING(required_from_host_cert);
691664f4763Szrj OPTSTRING(required_from_host_keys);
692664f4763Szrj #undef OPTSTRING
693664f4763Szrj
694664f4763Szrj if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
695664f4763Szrj dup_strings(&ret->permitopen, &ret->npermitopen,
696664f4763Szrj orig->permitopen, orig->npermitopen) != 0 ||
697664f4763Szrj dup_strings(&ret->permitlisten, &ret->npermitlisten,
698664f4763Szrj orig->permitlisten, orig->npermitlisten) != 0) {
699664f4763Szrj sshauthopt_free(ret);
700664f4763Szrj return NULL;
701664f4763Szrj }
702664f4763Szrj return ret;
703664f4763Szrj }
704664f4763Szrj
705664f4763Szrj static int
serialise_array(struct sshbuf * m,char ** a,size_t n)706664f4763Szrj serialise_array(struct sshbuf *m, char **a, size_t n)
707664f4763Szrj {
708664f4763Szrj struct sshbuf *b;
709664f4763Szrj size_t i;
710*ba1276acSMatthew Dillon int r = SSH_ERR_INTERNAL_ERROR;
711664f4763Szrj
712664f4763Szrj if (n > INT_MAX)
713664f4763Szrj return SSH_ERR_INTERNAL_ERROR;
714664f4763Szrj
715664f4763Szrj if ((b = sshbuf_new()) == NULL) {
716664f4763Szrj return SSH_ERR_ALLOC_FAIL;
717664f4763Szrj }
718664f4763Szrj for (i = 0; i < n; i++) {
719*ba1276acSMatthew Dillon if ((r = sshbuf_put_cstring(b, a[i])) != 0)
720*ba1276acSMatthew Dillon goto out;
721664f4763Szrj }
722664f4763Szrj if ((r = sshbuf_put_u32(m, n)) != 0 ||
723*ba1276acSMatthew Dillon (r = sshbuf_put_stringb(m, b)) != 0)
724*ba1276acSMatthew Dillon goto out;
725*ba1276acSMatthew Dillon /* success */
726*ba1276acSMatthew Dillon r = 0;
727*ba1276acSMatthew Dillon out:
728664f4763Szrj sshbuf_free(b);
729664f4763Szrj return r;
730664f4763Szrj }
731664f4763Szrj
732664f4763Szrj static int
deserialise_array(struct sshbuf * m,char *** ap,size_t * np)733664f4763Szrj deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
734664f4763Szrj {
735664f4763Szrj char **a = NULL;
736664f4763Szrj size_t i, n = 0;
737664f4763Szrj struct sshbuf *b = NULL;
738664f4763Szrj u_int tmp;
739664f4763Szrj int r = SSH_ERR_INTERNAL_ERROR;
740664f4763Szrj
741664f4763Szrj if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
742664f4763Szrj (r = sshbuf_froms(m, &b)) != 0)
743664f4763Szrj goto out;
744664f4763Szrj if (tmp > INT_MAX) {
745664f4763Szrj r = SSH_ERR_INVALID_FORMAT;
746664f4763Szrj goto out;
747664f4763Szrj }
748664f4763Szrj n = tmp;
749664f4763Szrj if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
750664f4763Szrj r = SSH_ERR_ALLOC_FAIL;
751664f4763Szrj goto out;
752664f4763Szrj }
753664f4763Szrj for (i = 0; i < n; i++) {
754664f4763Szrj if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
755664f4763Szrj goto out;
756664f4763Szrj }
757664f4763Szrj /* success */
758664f4763Szrj r = 0;
759664f4763Szrj *ap = a;
760664f4763Szrj a = NULL;
761664f4763Szrj *np = n;
762664f4763Szrj n = 0;
763664f4763Szrj out:
7640cbfa66cSDaniel Fojt if (a != NULL) {
765664f4763Szrj for (i = 0; i < n; i++)
766664f4763Szrj free(a[i]);
767664f4763Szrj free(a);
7680cbfa66cSDaniel Fojt }
769664f4763Szrj sshbuf_free(b);
770664f4763Szrj return r;
771664f4763Szrj }
772664f4763Szrj
773664f4763Szrj static int
serialise_nullable_string(struct sshbuf * m,const char * s)774664f4763Szrj serialise_nullable_string(struct sshbuf *m, const char *s)
775664f4763Szrj {
776664f4763Szrj int r;
777664f4763Szrj
778664f4763Szrj if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
779664f4763Szrj (r = sshbuf_put_cstring(m, s)) != 0)
780664f4763Szrj return r;
781664f4763Szrj return 0;
782664f4763Szrj }
783664f4763Szrj
784664f4763Szrj static int
deserialise_nullable_string(struct sshbuf * m,char ** sp)785664f4763Szrj deserialise_nullable_string(struct sshbuf *m, char **sp)
786664f4763Szrj {
787664f4763Szrj int r;
788664f4763Szrj u_char flag;
789664f4763Szrj
790664f4763Szrj *sp = NULL;
791664f4763Szrj if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
792664f4763Szrj (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
793664f4763Szrj return r;
794664f4763Szrj return 0;
795664f4763Szrj }
796664f4763Szrj
797664f4763Szrj int
sshauthopt_serialise(const struct sshauthopt * opts,struct sshbuf * m,int untrusted)798664f4763Szrj sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
799664f4763Szrj int untrusted)
800664f4763Szrj {
801664f4763Szrj int r = SSH_ERR_INTERNAL_ERROR;
802664f4763Szrj
8030cbfa66cSDaniel Fojt /* Flag options */
804664f4763Szrj if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
805664f4763Szrj (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
806664f4763Szrj (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
807664f4763Szrj (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
808664f4763Szrj (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
809664f4763Szrj (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
810664f4763Szrj (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
81150a69bb5SSascha Wildner (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
81250a69bb5SSascha Wildner (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
8130cbfa66cSDaniel Fojt return r;
8140cbfa66cSDaniel Fojt
8150cbfa66cSDaniel Fojt /* Simple integer options */
8160cbfa66cSDaniel Fojt if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
817664f4763Szrj return r;
818664f4763Szrj
819664f4763Szrj /* tunnel number can be negative to indicate "unset" */
820664f4763Szrj if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
821664f4763Szrj (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
822664f4763Szrj 0 : (u_int)opts->force_tun_device)) != 0)
823664f4763Szrj return r;
824664f4763Szrj
825664f4763Szrj /* String options; these may be NULL */
826664f4763Szrj if ((r = serialise_nullable_string(m,
827664f4763Szrj untrusted ? "yes" : opts->cert_principals)) != 0 ||
828664f4763Szrj (r = serialise_nullable_string(m,
829664f4763Szrj untrusted ? "true" : opts->force_command)) != 0 ||
830664f4763Szrj (r = serialise_nullable_string(m,
831664f4763Szrj untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
832664f4763Szrj (r = serialise_nullable_string(m,
833664f4763Szrj untrusted ? NULL : opts->required_from_host_keys)) != 0)
834664f4763Szrj return r;
835664f4763Szrj
836664f4763Szrj /* Array options */
837664f4763Szrj if ((r = serialise_array(m, opts->env,
838664f4763Szrj untrusted ? 0 : opts->nenv)) != 0 ||
839664f4763Szrj (r = serialise_array(m, opts->permitopen,
840664f4763Szrj untrusted ? 0 : opts->npermitopen)) != 0 ||
841664f4763Szrj (r = serialise_array(m, opts->permitlisten,
842664f4763Szrj untrusted ? 0 : opts->npermitlisten)) != 0)
843664f4763Szrj return r;
844664f4763Szrj
845664f4763Szrj /* success */
846664f4763Szrj return 0;
847664f4763Szrj }
848664f4763Szrj
849664f4763Szrj int
sshauthopt_deserialise(struct sshbuf * m,struct sshauthopt ** optsp)850664f4763Szrj sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
851664f4763Szrj {
852664f4763Szrj struct sshauthopt *opts = NULL;
853664f4763Szrj int r = SSH_ERR_INTERNAL_ERROR;
854664f4763Szrj u_char f;
855664f4763Szrj u_int tmp;
856664f4763Szrj
857664f4763Szrj if ((opts = calloc(1, sizeof(*opts))) == NULL)
858664f4763Szrj return SSH_ERR_ALLOC_FAIL;
859664f4763Szrj
8600cbfa66cSDaniel Fojt /* Flag options */
861664f4763Szrj #define OPT_FLAG(x) \
862664f4763Szrj do { \
863664f4763Szrj if ((r = sshbuf_get_u8(m, &f)) != 0) \
864664f4763Szrj goto out; \
865664f4763Szrj opts->x = f; \
866664f4763Szrj } while (0)
867664f4763Szrj OPT_FLAG(permit_port_forwarding_flag);
868664f4763Szrj OPT_FLAG(permit_agent_forwarding_flag);
869664f4763Szrj OPT_FLAG(permit_x11_forwarding_flag);
870664f4763Szrj OPT_FLAG(permit_pty_flag);
871664f4763Szrj OPT_FLAG(permit_user_rc);
872664f4763Szrj OPT_FLAG(restricted);
873664f4763Szrj OPT_FLAG(cert_authority);
8740cbfa66cSDaniel Fojt OPT_FLAG(no_require_user_presence);
87550a69bb5SSascha Wildner OPT_FLAG(require_verify);
876664f4763Szrj #undef OPT_FLAG
877664f4763Szrj
8780cbfa66cSDaniel Fojt /* Simple integer options */
879664f4763Szrj if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
880664f4763Szrj goto out;
881664f4763Szrj
882664f4763Szrj /* tunnel number can be negative to indicate "unset" */
883664f4763Szrj if ((r = sshbuf_get_u8(m, &f)) != 0 ||
884664f4763Szrj (r = sshbuf_get_u32(m, &tmp)) != 0)
885664f4763Szrj goto out;
886664f4763Szrj opts->force_tun_device = f ? -1 : (int)tmp;
887664f4763Szrj
888664f4763Szrj /* String options may be NULL */
889664f4763Szrj if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
890664f4763Szrj (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
891664f4763Szrj (r = deserialise_nullable_string(m,
892664f4763Szrj &opts->required_from_host_cert)) != 0 ||
893664f4763Szrj (r = deserialise_nullable_string(m,
894664f4763Szrj &opts->required_from_host_keys)) != 0)
895664f4763Szrj goto out;
896664f4763Szrj
897664f4763Szrj /* Array options */
898664f4763Szrj if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
899664f4763Szrj (r = deserialise_array(m,
900664f4763Szrj &opts->permitopen, &opts->npermitopen)) != 0 ||
901664f4763Szrj (r = deserialise_array(m,
902664f4763Szrj &opts->permitlisten, &opts->npermitlisten)) != 0)
903664f4763Szrj goto out;
904664f4763Szrj
905664f4763Szrj /* success */
906664f4763Szrj r = 0;
907664f4763Szrj *optsp = opts;
908664f4763Szrj opts = NULL;
909664f4763Szrj out:
910664f4763Szrj sshauthopt_free(opts);
911664f4763Szrj return r;
912664f4763Szrj }
913