Home | History | Annotate | Line # | Download | only in dist
auth-options.c revision 1.30
      1 /*	$NetBSD: auth-options.c,v 1.30 2025/10/11 15:45:06 christos Exp $	*/
      2 /* $OpenBSD: auth-options.c,v 1.102 2025/09/15 04:38:00 djm Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2018 Damien Miller <djm (at) mindrot.org>
      6  *
      7  * Permission to use, copy, modify, and distribute this software for any
      8  * purpose with or without fee is hereby granted, provided that the above
      9  * copyright notice and this permission notice appear in all copies.
     10  *
     11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     18  */
     19 
     20 #include "includes.h"
     21 __RCSID("$NetBSD: auth-options.c,v 1.30 2025/10/11 15:45:06 christos Exp $");
     22 #include <sys/types.h>
     23 #include <sys/queue.h>
     24 
     25 #include <stdlib.h>
     26 #include <netdb.h>
     27 #include <pwd.h>
     28 #include <string.h>
     29 #include <stdio.h>
     30 #include <stdint.h>
     31 #include <stdarg.h>
     32 #include <time.h>
     33 #include <ctype.h>
     34 #include <limits.h>
     35 
     36 #include "xmalloc.h"
     37 #include "ssherr.h"
     38 #include "log.h"
     39 #include "sshbuf.h"
     40 #include "misc.h"
     41 #include "sshkey.h"
     42 #include "match.h"
     43 #include "ssh2.h"
     44 #include "auth-options.h"
     45 
     46 static int
     47 dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
     48 {
     49 	char **dst;
     50 	size_t i, j;
     51 
     52 	*dstp = NULL;
     53 	*ndstp = 0;
     54 
     55 	if (nsrc == 0)
     56 		return 0;
     57 	if (nsrc >= SIZE_MAX / sizeof(*src) ||
     58 	    (dst = calloc(nsrc, sizeof(*src))) == NULL)
     59 		return -1;
     60 	for (i = 0; i < nsrc; i++) {
     61 		if ((dst[i] = strdup(src[i])) == NULL) {
     62 			for (j = 0; j < i; j++)
     63 				free(dst[j]);
     64 			free(dst);
     65 			return -1;
     66 		}
     67 	}
     68 	/* success */
     69 	*dstp = dst;
     70 	*ndstp = nsrc;
     71 	return 0;
     72 }
     73 
     74 #define OPTIONS_CRITICAL	1
     75 #define OPTIONS_EXTENSIONS	2
     76 static int
     77 cert_option_list(struct sshauthopt *opts, struct sshbuf *oblob,
     78     u_int which, int crit)
     79 {
     80 	char *command, *allowed;
     81 	char *name = NULL;
     82 	struct sshbuf *c = NULL, *data = NULL;
     83 	int r, ret = -1, found;
     84 
     85 	if ((c = sshbuf_fromb(oblob)) == NULL) {
     86 		error_f("sshbuf_fromb failed");
     87 		goto out;
     88 	}
     89 
     90 	while (sshbuf_len(c) > 0) {
     91 		sshbuf_free(data);
     92 		data = NULL;
     93 		if ((r = sshbuf_get_cstring(c, &name, NULL)) != 0 ||
     94 		    (r = sshbuf_froms(c, &data)) != 0) {
     95 			error_r(r, "Unable to parse certificate options");
     96 			goto out;
     97 		}
     98 		debug3("found certificate option \"%.100s\" len %zu",
     99 		    name, sshbuf_len(data));
    100 		found = 0;
    101 		if ((which & OPTIONS_EXTENSIONS) != 0) {
    102 			if (strcmp(name, "no-touch-required") == 0) {
    103 				opts->no_require_user_presence = 1;
    104 				found = 1;
    105 			} else if (strcmp(name, "permit-X11-forwarding") == 0) {
    106 				opts->permit_x11_forwarding_flag = 1;
    107 				found = 1;
    108 			} else if (strcmp(name,
    109 			    "permit-agent-forwarding") == 0) {
    110 				opts->permit_agent_forwarding_flag = 1;
    111 				found = 1;
    112 			} else if (strcmp(name,
    113 			    "permit-port-forwarding") == 0) {
    114 				opts->permit_port_forwarding_flag = 1;
    115 				found = 1;
    116 			} else if (strcmp(name, "permit-pty") == 0) {
    117 				opts->permit_pty_flag = 1;
    118 				found = 1;
    119 			} else if (strcmp(name, "permit-user-rc") == 0) {
    120 				opts->permit_user_rc = 1;
    121 				found = 1;
    122 			}
    123 		}
    124 		if (!found && (which & OPTIONS_CRITICAL) != 0) {
    125 			if (strcmp(name, "verify-required") == 0) {
    126 				opts->require_verify = 1;
    127 				found = 1;
    128 			} else if (strcmp(name, "force-command") == 0) {
    129 				if ((r = sshbuf_get_cstring(data, &command,
    130 				    NULL)) != 0) {
    131 					error_r(r, "Unable to parse \"%s\" "
    132 					    "section", name);
    133 					goto out;
    134 				}
    135 				if (opts->force_command != NULL) {
    136 					error("Certificate has multiple "
    137 					    "force-command options");
    138 					free(command);
    139 					goto out;
    140 				}
    141 				opts->force_command = command;
    142 				found = 1;
    143 			} else if (strcmp(name, "source-address") == 0) {
    144 				if ((r = sshbuf_get_cstring(data, &allowed,
    145 				    NULL)) != 0) {
    146 					error_r(r, "Unable to parse \"%s\" "
    147 					    "section", name);
    148 					goto out;
    149 				}
    150 				if (opts->required_from_host_cert != NULL) {
    151 					error("Certificate has multiple "
    152 					    "source-address options");
    153 					free(allowed);
    154 					goto out;
    155 				}
    156 				/* Check syntax */
    157 				if (addr_match_cidr_list(NULL, allowed) == -1) {
    158 					error("Certificate source-address "
    159 					    "contents invalid");
    160 					free(allowed);
    161 					goto out;
    162 				}
    163 				opts->required_from_host_cert = allowed;
    164 				found = 1;
    165 			}
    166 		}
    167 
    168 		if (!found) {
    169 			if (crit) {
    170 				error("Certificate critical option \"%s\" "
    171 				    "is not supported", name);
    172 				goto out;
    173 			} else {
    174 				logit("Certificate extension \"%s\" "
    175 				    "is not supported", name);
    176 			}
    177 		} else if (sshbuf_len(data) != 0) {
    178 			error("Certificate option \"%s\" corrupt "
    179 			    "(extra data)", name);
    180 			goto out;
    181 		}
    182 		free(name);
    183 		name = NULL;
    184 	}
    185 	/* successfully parsed all options */
    186 	ret = 0;
    187 
    188  out:
    189 	free(name);
    190 	sshbuf_free(data);
    191 	sshbuf_free(c);
    192 	return ret;
    193 }
    194 
    195 struct sshauthopt *
    196 sshauthopt_new(void)
    197 {
    198 	struct sshauthopt *ret;
    199 
    200 	if ((ret = calloc(1, sizeof(*ret))) == NULL)
    201 		return NULL;
    202 	ret->force_tun_device = -1;
    203 	return ret;
    204 }
    205 
    206 void
    207 sshauthopt_free(struct sshauthopt *opts)
    208 {
    209 	size_t i;
    210 
    211 	if (opts == NULL)
    212 		return;
    213 
    214 	free(opts->cert_principals);
    215 	free(opts->force_command);
    216 	free(opts->required_from_host_cert);
    217 	free(opts->required_from_host_keys);
    218 
    219 	for (i = 0; i < opts->nenv; i++)
    220 		free(opts->env[i]);
    221 	free(opts->env);
    222 
    223 	for (i = 0; i < opts->npermitopen; i++)
    224 		free(opts->permitopen[i]);
    225 	free(opts->permitopen);
    226 
    227 	for (i = 0; i < opts->npermitlisten; i++)
    228 		free(opts->permitlisten[i]);
    229 	free(opts->permitlisten);
    230 
    231 	freezero(opts, sizeof(*opts));
    232 }
    233 
    234 struct sshauthopt *
    235 sshauthopt_new_with_keys_defaults(void)
    236 {
    237 	struct sshauthopt *ret = NULL;
    238 
    239 	if ((ret = sshauthopt_new()) == NULL)
    240 		return NULL;
    241 
    242 	/* Defaults for authorized_keys flags */
    243 	ret->permit_port_forwarding_flag = 1;
    244 	ret->permit_agent_forwarding_flag = 1;
    245 	ret->permit_x11_forwarding_flag = 1;
    246 	ret->permit_pty_flag = 1;
    247 	ret->permit_user_rc = 1;
    248 	return ret;
    249 }
    250 
    251 /*
    252  * Parse and record a permitopen/permitlisten directive.
    253  * Return 0 on success. Return -1 on failure and sets *errstrp to error reason.
    254  */
    255 static int
    256 handle_permit(const char **optsp, int allow_bare_port,
    257     char ***permitsp, size_t *npermitsp, const char **errstrp)
    258 {
    259 	char *opt, *tmp, *cp, *host, **permits = *permitsp;
    260 	size_t npermits = *npermitsp;
    261 	const char *errstr = "unknown error";
    262 
    263 	if (npermits > SSH_AUTHOPT_PERMIT_MAX) {
    264 		*errstrp = "too many permission directives";
    265 		return -1;
    266 	}
    267 	if ((opt = opt_dequote(optsp, &errstr)) == NULL) {
    268 		return -1;
    269 	}
    270 	if (allow_bare_port && strchr(opt, ':') == NULL) {
    271 		/*
    272 		 * Allow a bare port number in permitlisten to indicate a
    273 		 * listen_host wildcard.
    274 		 */
    275 		if (asprintf(&tmp, "*:%s", opt) == -1) {
    276 			free(opt);
    277 			*errstrp = "memory allocation failed";
    278 			return -1;
    279 		}
    280 		free(opt);
    281 		opt = tmp;
    282 	}
    283 	if ((tmp = strdup(opt)) == NULL) {
    284 		free(opt);
    285 		*errstrp = "memory allocation failed";
    286 		return -1;
    287 	}
    288 	cp = tmp;
    289 	/* validate syntax before recording it. */
    290 	host = hpdelim2(&cp, NULL);
    291 	if (host == NULL || strlen(host) >= NI_MAXHOST) {
    292 		free(tmp);
    293 		free(opt);
    294 		*errstrp = "invalid permission hostname";
    295 		return -1;
    296 	}
    297 	/*
    298 	 * don't want to use permitopen_port to avoid
    299 	 * dependency on channels.[ch] here.
    300 	 */
    301 	if (cp == NULL ||
    302 	    (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) {
    303 		free(tmp);
    304 		free(opt);
    305 		*errstrp = "invalid permission port";
    306 		return -1;
    307 	}
    308 	/* XXX - add streamlocal support */
    309 	free(tmp);
    310 	/* Record it */
    311 	if ((permits = recallocarray(permits, npermits, npermits + 1,
    312 	    sizeof(*permits))) == NULL) {
    313 		free(opt);
    314 		/* NB. don't update *permitsp if alloc fails */
    315 		*errstrp = "memory allocation failed";
    316 		return -1;
    317 	}
    318 	permits[npermits++] = opt;
    319 	*permitsp = permits;
    320 	*npermitsp = npermits;
    321 	return 0;
    322 }
    323 
    324 struct sshauthopt *
    325 sshauthopt_parse(const char *opts, const char **errstrp)
    326 {
    327 	char **oarray, *opt, *cp, *tmp;
    328 	int r;
    329 	struct sshauthopt *ret = NULL;
    330 	const char *errstr = "unknown error";
    331 	uint64_t valid_before;
    332 	size_t i, l;
    333 
    334 	if (errstrp != NULL)
    335 		*errstrp = NULL;
    336 	if ((ret = sshauthopt_new_with_keys_defaults()) == NULL)
    337 		goto alloc_fail;
    338 
    339 	if (opts == NULL)
    340 		return ret;
    341 
    342 	while (*opts && *opts != ' ' && *opts != '\t') {
    343 		/* flag options */
    344 		if ((r = opt_flag("restrict", 0, &opts)) != -1) {
    345 			ret->restricted = 1;
    346 			ret->permit_port_forwarding_flag = 0;
    347 			ret->permit_agent_forwarding_flag = 0;
    348 			ret->permit_x11_forwarding_flag = 0;
    349 			ret->permit_pty_flag = 0;
    350 			ret->permit_user_rc = 0;
    351 		} else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) {
    352 			ret->cert_authority = r;
    353 		} else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) {
    354 			ret->permit_port_forwarding_flag = r == 1;
    355 		} else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) {
    356 			ret->permit_agent_forwarding_flag = r == 1;
    357 		} else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) {
    358 			ret->permit_x11_forwarding_flag = r == 1;
    359 		} else if ((r = opt_flag("touch-required", 1, &opts)) != -1) {
    360 			ret->no_require_user_presence = r != 1; /* NB. flip */
    361 		} else if ((r = opt_flag("verify-required", 1, &opts)) != -1) {
    362 			ret->require_verify = r == 1;
    363 		} else if ((r = opt_flag("pty", 1, &opts)) != -1) {
    364 			ret->permit_pty_flag = r == 1;
    365 		} else if ((r = opt_flag("user-rc", 1, &opts)) != -1) {
    366 			ret->permit_user_rc = r == 1;
    367 		} else if (opt_match(&opts, "command")) {
    368 			if (ret->force_command != NULL) {
    369 				errstr = "multiple \"command\" clauses";
    370 				goto fail;
    371 			}
    372 			ret->force_command = opt_dequote(&opts, &errstr);
    373 			if (ret->force_command == NULL)
    374 				goto fail;
    375 		} else if (opt_match(&opts, "principals")) {
    376 			if (ret->cert_principals != NULL) {
    377 				errstr = "multiple \"principals\" clauses";
    378 				goto fail;
    379 			}
    380 			ret->cert_principals = opt_dequote(&opts, &errstr);
    381 			if (ret->cert_principals == NULL)
    382 				goto fail;
    383 		} else if (opt_match(&opts, "from")) {
    384 			if (ret->required_from_host_keys != NULL) {
    385 				errstr = "multiple \"from\" clauses";
    386 				goto fail;
    387 			}
    388 			ret->required_from_host_keys = opt_dequote(&opts,
    389 			    &errstr);
    390 			if (ret->required_from_host_keys == NULL)
    391 				goto fail;
    392 		} else if (opt_match(&opts, "expiry-time")) {
    393 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
    394 				goto fail;
    395 			if (parse_absolute_time(opt, &valid_before) != 0 ||
    396 			    valid_before == 0) {
    397 				free(opt);
    398 				errstr = "invalid expires time";
    399 				goto fail;
    400 			}
    401 			free(opt);
    402 			if (ret->valid_before == 0 ||
    403 			    valid_before < ret->valid_before)
    404 				ret->valid_before = valid_before;
    405 		} else if (opt_match(&opts, "environment")) {
    406 			if (ret->nenv > SSH_AUTHOPT_ENV_MAX) {
    407 				errstr = "too many environment strings";
    408 				goto fail;
    409 			}
    410 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
    411 				goto fail;
    412 			/* env name must be alphanumeric and followed by '=' */
    413 			if ((tmp = strchr(opt, '=')) == NULL) {
    414 				free(opt);
    415 				errstr = "invalid environment string";
    416 				goto fail;
    417 			}
    418 			if ((cp = strdup(opt)) == NULL) {
    419 				free(opt);
    420 				goto alloc_fail;
    421 			}
    422 			l = (size_t)(tmp - opt);
    423 			cp[l] = '\0'; /* truncate at '=' */
    424 			if (!valid_env_name(cp)) {
    425 				free(cp);
    426 				free(opt);
    427 				errstr = "invalid environment string";
    428 				goto fail;
    429 			}
    430 			/* Check for duplicates; XXX O(n*log(n)) */
    431 			for (i = 0; i < ret->nenv; i++) {
    432 				if (strncmp(ret->env[i], cp, l) == 0 &&
    433 				    ret->env[i][l] == '=')
    434 					break;
    435 			}
    436 			free(cp);
    437 			/* First match wins */
    438 			if (i >= ret->nenv) {
    439 				/* Append it. */
    440 				oarray = ret->env;
    441 				if ((ret->env = recallocarray(ret->env,
    442 				    ret->nenv, ret->nenv + 1,
    443 				    sizeof(*ret->env))) == NULL) {
    444 					free(opt);
    445 					/* put it back for cleanup */
    446 					ret->env = oarray;
    447 					goto alloc_fail;
    448 				}
    449 				ret->env[ret->nenv++] = opt;
    450 				opt = NULL; /* transferred */
    451 			}
    452 			free(opt);
    453 		} else if (opt_match(&opts, "permitopen")) {
    454 			if (handle_permit(&opts, 0, &ret->permitopen,
    455 			    &ret->npermitopen, &errstr) != 0)
    456 				goto fail;
    457 		} else if (opt_match(&opts, "permitlisten")) {
    458 			if (handle_permit(&opts, 1, &ret->permitlisten,
    459 			    &ret->npermitlisten, &errstr) != 0)
    460 				goto fail;
    461 		} else if (opt_match(&opts, "tunnel")) {
    462 			if ((opt = opt_dequote(&opts, &errstr)) == NULL)
    463 				goto fail;
    464 			ret->force_tun_device = a2tun(opt, NULL);
    465 			free(opt);
    466 			if (ret->force_tun_device == SSH_TUNID_ERR) {
    467 				errstr = "invalid tun device";
    468 				goto fail;
    469 			}
    470 		}
    471 		/*
    472 		 * Skip the comma, and move to the next option
    473 		 * (or break out if there are no more).
    474 		 */
    475 		if (*opts == '\0' || *opts == ' ' || *opts == '\t')
    476 			break;		/* End of options. */
    477 		/* Anything other than a comma is an unknown option */
    478 		if (*opts != ',') {
    479 			errstr = "unknown key option";
    480 			goto fail;
    481 		}
    482 		opts++;
    483 		if (*opts == '\0') {
    484 			errstr = "unexpected end-of-options";
    485 			goto fail;
    486 		}
    487 	}
    488 
    489 	/* success */
    490 	if (errstrp != NULL)
    491 		*errstrp = NULL;
    492 	return ret;
    493 
    494 alloc_fail:
    495 	errstr = "memory allocation failed";
    496 fail:
    497 	sshauthopt_free(ret);
    498 	if (errstrp != NULL)
    499 		*errstrp = errstr;
    500 	return NULL;
    501 }
    502 
    503 struct sshauthopt *
    504 sshauthopt_from_cert(struct sshkey *k)
    505 {
    506 	struct sshauthopt *ret;
    507 
    508 	if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL ||
    509 	    k->cert->type != SSH2_CERT_TYPE_USER)
    510 		return NULL;
    511 
    512 	if ((ret = sshauthopt_new()) == NULL)
    513 		return NULL;
    514 
    515 	/* Handle options and critical extensions separately */
    516 	if (cert_option_list(ret, k->cert->critical,
    517 	    OPTIONS_CRITICAL, 1) == -1) {
    518 		sshauthopt_free(ret);
    519 		return NULL;
    520 	}
    521 	if (cert_option_list(ret, k->cert->extensions,
    522 	    OPTIONS_EXTENSIONS, 0) == -1) {
    523 		sshauthopt_free(ret);
    524 		return NULL;
    525 	}
    526 	/* success */
    527 	return ret;
    528 }
    529 
    530 /*
    531  * Merges "additional" options to "primary" and returns the result.
    532  * NB. Some options from primary have primacy.
    533  */
    534 struct sshauthopt *
    535 sshauthopt_merge(const struct sshauthopt *primary,
    536     const struct sshauthopt *additional, const char **errstrp)
    537 {
    538 	struct sshauthopt *ret;
    539 	const char *errstr = "internal error";
    540 	const char *tmp;
    541 
    542 	if (errstrp != NULL)
    543 		*errstrp = NULL;
    544 
    545 	if ((ret = sshauthopt_new()) == NULL)
    546 		goto alloc_fail;
    547 
    548 	/* cert_authority and cert_principals are cleared in result */
    549 
    550 	/* Prefer access lists from primary. */
    551 	/* XXX err is both set and mismatch? */
    552 	tmp = primary->required_from_host_cert;
    553 	if (tmp == NULL)
    554 		tmp = additional->required_from_host_cert;
    555 	if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL)
    556 		goto alloc_fail;
    557 	tmp = primary->required_from_host_keys;
    558 	if (tmp == NULL)
    559 		tmp = additional->required_from_host_keys;
    560 	if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL)
    561 		goto alloc_fail;
    562 
    563 	/*
    564 	 * force_tun_device, permitopen/permitlisten and environment all
    565 	 * prefer the primary.
    566 	 */
    567 	ret->force_tun_device = primary->force_tun_device;
    568 	if (ret->force_tun_device == -1)
    569 		ret->force_tun_device = additional->force_tun_device;
    570 	if (primary->nenv > 0) {
    571 		if (dup_strings(&ret->env, &ret->nenv,
    572 		    primary->env, primary->nenv) != 0)
    573 			goto alloc_fail;
    574 	} else if (additional->nenv) {
    575 		if (dup_strings(&ret->env, &ret->nenv,
    576 		    additional->env, additional->nenv) != 0)
    577 			goto alloc_fail;
    578 	}
    579 	if (primary->npermitopen > 0) {
    580 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
    581 		    primary->permitopen, primary->npermitopen) != 0)
    582 			goto alloc_fail;
    583 	} else if (additional->npermitopen > 0) {
    584 		if (dup_strings(&ret->permitopen, &ret->npermitopen,
    585 		    additional->permitopen, additional->npermitopen) != 0)
    586 			goto alloc_fail;
    587 	}
    588 
    589 	if (primary->npermitlisten > 0) {
    590 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
    591 		    primary->permitlisten, primary->npermitlisten) != 0)
    592 			goto alloc_fail;
    593 	} else if (additional->npermitlisten > 0) {
    594 		if (dup_strings(&ret->permitlisten, &ret->npermitlisten,
    595 		    additional->permitlisten, additional->npermitlisten) != 0)
    596 			goto alloc_fail;
    597 	}
    598 
    599 #define OPTFLAG_AND(x) ret->x = (primary->x == 1) && (additional->x == 1)
    600 #define OPTFLAG_OR(x) ret->x = (primary->x == 1) || (additional->x == 1)
    601 	/* Permissive flags are logical-AND (i.e. must be set in both) */
    602 	OPTFLAG_AND(permit_port_forwarding_flag);
    603 	OPTFLAG_AND(permit_agent_forwarding_flag);
    604 	OPTFLAG_AND(permit_x11_forwarding_flag);
    605 	OPTFLAG_AND(permit_pty_flag);
    606 	OPTFLAG_AND(permit_user_rc);
    607 	OPTFLAG_AND(no_require_user_presence);
    608 	/* Restrictive flags are logical-OR (i.e. must be set in either) */
    609 	OPTFLAG_OR(require_verify);
    610 #undef OPTFLAG_AND
    611 
    612 	/* Earliest expiry time should win */
    613 	if (primary->valid_before != 0)
    614 		ret->valid_before = primary->valid_before;
    615 	if (additional->valid_before != 0 &&
    616 	    additional->valid_before < ret->valid_before)
    617 		ret->valid_before = additional->valid_before;
    618 
    619 	/*
    620 	 * When both multiple forced-command are specified, only
    621 	 * proceed if they are identical, otherwise fail.
    622 	 */
    623 	if (primary->force_command != NULL &&
    624 	    additional->force_command != NULL) {
    625 		if (strcmp(primary->force_command,
    626 		    additional->force_command) == 0) {
    627 			/* ok */
    628 			ret->force_command = strdup(primary->force_command);
    629 			if (ret->force_command == NULL)
    630 				goto alloc_fail;
    631 		} else {
    632 			errstr = "forced command options do not match";
    633 			goto fail;
    634 		}
    635 	} else if (primary->force_command != NULL) {
    636 		if ((ret->force_command = strdup(
    637 		    primary->force_command)) == NULL)
    638 			goto alloc_fail;
    639 	} else if (additional->force_command != NULL) {
    640 		if ((ret->force_command = strdup(
    641 		    additional->force_command)) == NULL)
    642 			goto alloc_fail;
    643 	}
    644 	/* success */
    645 	if (errstrp != NULL)
    646 		*errstrp = NULL;
    647 	return ret;
    648 
    649  alloc_fail:
    650 	errstr = "memory allocation failed";
    651  fail:
    652 	if (errstrp != NULL)
    653 		*errstrp = errstr;
    654 	sshauthopt_free(ret);
    655 	return NULL;
    656 }
    657 
    658 /*
    659  * Copy options
    660  */
    661 struct sshauthopt *
    662 sshauthopt_copy(const struct sshauthopt *orig)
    663 {
    664 	struct sshauthopt *ret;
    665 
    666 	if ((ret = sshauthopt_new()) == NULL)
    667 		return NULL;
    668 
    669 #define OPTSCALAR(x) ret->x = orig->x
    670 	OPTSCALAR(permit_port_forwarding_flag);
    671 	OPTSCALAR(permit_agent_forwarding_flag);
    672 	OPTSCALAR(permit_x11_forwarding_flag);
    673 	OPTSCALAR(permit_pty_flag);
    674 	OPTSCALAR(permit_user_rc);
    675 	OPTSCALAR(restricted);
    676 	OPTSCALAR(cert_authority);
    677 	OPTSCALAR(force_tun_device);
    678 	OPTSCALAR(valid_before);
    679 	OPTSCALAR(no_require_user_presence);
    680 	OPTSCALAR(require_verify);
    681 #undef OPTSCALAR
    682 #define OPTSTRING(x) \
    683 	do { \
    684 		if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \
    685 			sshauthopt_free(ret); \
    686 			return NULL; \
    687 		} \
    688 	} while (0)
    689 	OPTSTRING(cert_principals);
    690 	OPTSTRING(force_command);
    691 	OPTSTRING(required_from_host_cert);
    692 	OPTSTRING(required_from_host_keys);
    693 #undef OPTSTRING
    694 
    695 	if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 ||
    696 	    dup_strings(&ret->permitopen, &ret->npermitopen,
    697 	    orig->permitopen, orig->npermitopen) != 0 ||
    698 	    dup_strings(&ret->permitlisten, &ret->npermitlisten,
    699 	    orig->permitlisten, orig->npermitlisten) != 0) {
    700 		sshauthopt_free(ret);
    701 		return NULL;
    702 	}
    703 	return ret;
    704 }
    705 
    706 static int
    707 serialise_array(struct sshbuf *m, char **a, size_t n)
    708 {
    709 	struct sshbuf *b;
    710 	size_t i;
    711 	int r = SSH_ERR_INTERNAL_ERROR;
    712 
    713 	if (n > INT_MAX)
    714 		return SSH_ERR_INTERNAL_ERROR;
    715 
    716 	if ((b = sshbuf_new()) == NULL) {
    717 		return SSH_ERR_ALLOC_FAIL;
    718 	}
    719 	for (i = 0; i < n; i++) {
    720 		if ((r = sshbuf_put_cstring(b, a[i])) != 0)
    721 			goto out;
    722 	}
    723 	if ((r = sshbuf_put_u32(m, n)) != 0 ||
    724 	    (r = sshbuf_put_stringb(m, b)) != 0)
    725 		goto out;
    726 	/* success */
    727 	r = 0;
    728  out:
    729 	sshbuf_free(b);
    730 	return r;
    731 }
    732 
    733 static int
    734 deserialise_array(struct sshbuf *m, char ***ap, size_t *np)
    735 {
    736 	char **a = NULL;
    737 	size_t i, n = 0;
    738 	struct sshbuf *b = NULL;
    739 	u_int tmp;
    740 	int r = SSH_ERR_INTERNAL_ERROR;
    741 
    742 	if ((r = sshbuf_get_u32(m, &tmp)) != 0 ||
    743 	    (r = sshbuf_froms(m, &b)) != 0)
    744 		goto out;
    745 	if (tmp > INT_MAX) {
    746 		r = SSH_ERR_INVALID_FORMAT;
    747 		goto out;
    748 	}
    749 	n = tmp;
    750 	if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) {
    751 		r = SSH_ERR_ALLOC_FAIL;
    752 		goto out;
    753 	}
    754 	for (i = 0; i < n; i++) {
    755 		if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0)
    756 			goto out;
    757 	}
    758 	/* success */
    759 	r = 0;
    760 	*ap = a;
    761 	a = NULL;
    762 	*np = n;
    763 	n = 0;
    764  out:
    765 	if (a != NULL) {
    766 		for (i = 0; i < n; i++)
    767 			free(a[i]);
    768 		free(a);
    769 	}
    770 	sshbuf_free(b);
    771 	return r;
    772 }
    773 
    774 static int
    775 serialise_nullable_string(struct sshbuf *m, const char *s)
    776 {
    777 	int r;
    778 
    779 	if ((r = sshbuf_put_u8(m, s == NULL)) != 0 ||
    780 	    (r = sshbuf_put_cstring(m, s)) != 0)
    781 		return r;
    782 	return 0;
    783 }
    784 
    785 static int
    786 deserialise_nullable_string(struct sshbuf *m, char **sp)
    787 {
    788 	int r;
    789 	u_char flag;
    790 
    791 	*sp = NULL;
    792 	if ((r = sshbuf_get_u8(m, &flag)) != 0 ||
    793 	    (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0)
    794 		return r;
    795 	return 0;
    796 }
    797 
    798 int
    799 sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m,
    800     int untrusted)
    801 {
    802 	int r = SSH_ERR_INTERNAL_ERROR;
    803 
    804 	/* Flag options */
    805 	if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 ||
    806 	    (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 ||
    807 	    (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 ||
    808 	    (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 ||
    809 	    (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 ||
    810 	    (r = sshbuf_put_u8(m, opts->restricted)) != 0 ||
    811 	    (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 ||
    812 	    (r = sshbuf_put_u8(m, opts->no_require_user_presence)) != 0 ||
    813 	    (r = sshbuf_put_u8(m, opts->require_verify)) != 0)
    814 		return r;
    815 
    816 	/* Simple integer options */
    817 	if ((r = sshbuf_put_u64(m, opts->valid_before)) != 0)
    818 		return r;
    819 
    820 	/* tunnel number can be negative to indicate "unset" */
    821 	if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 ||
    822 	    (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ?
    823 	    0 : (u_int)opts->force_tun_device)) != 0)
    824 		return r;
    825 
    826 	/* String options; these may be NULL */
    827 	if ((r = serialise_nullable_string(m,
    828 	    untrusted ? "yes" : opts->cert_principals)) != 0 ||
    829 	    (r = serialise_nullable_string(m,
    830 	    untrusted ? "true" : opts->force_command)) != 0 ||
    831 	    (r = serialise_nullable_string(m,
    832 	    untrusted ? NULL : opts->required_from_host_cert)) != 0 ||
    833 	    (r = serialise_nullable_string(m,
    834 	    untrusted ? NULL : opts->required_from_host_keys)) != 0)
    835 		return r;
    836 
    837 	/* Array options */
    838 	if ((r = serialise_array(m, opts->env,
    839 	    untrusted ? 0 : opts->nenv)) != 0 ||
    840 	    (r = serialise_array(m, opts->permitopen,
    841 	    untrusted ? 0 : opts->npermitopen)) != 0 ||
    842 	    (r = serialise_array(m, opts->permitlisten,
    843 	    untrusted ? 0 : opts->npermitlisten)) != 0)
    844 		return r;
    845 
    846 	/* success */
    847 	return 0;
    848 }
    849 
    850 int
    851 sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp)
    852 {
    853 	struct sshauthopt *opts = NULL;
    854 	int r = SSH_ERR_INTERNAL_ERROR;
    855 	u_char f;
    856 	u_int tmp;
    857 
    858 	if ((opts = calloc(1, sizeof(*opts))) == NULL)
    859 		return SSH_ERR_ALLOC_FAIL;
    860 
    861 	/* Flag options */
    862 #define OPT_FLAG(x) \
    863 	do { \
    864 		if ((r = sshbuf_get_u8(m, &f)) != 0) \
    865 			goto out; \
    866 		opts->x = f; \
    867 	} while (0)
    868 	OPT_FLAG(permit_port_forwarding_flag);
    869 	OPT_FLAG(permit_agent_forwarding_flag);
    870 	OPT_FLAG(permit_x11_forwarding_flag);
    871 	OPT_FLAG(permit_pty_flag);
    872 	OPT_FLAG(permit_user_rc);
    873 	OPT_FLAG(restricted);
    874 	OPT_FLAG(cert_authority);
    875 	OPT_FLAG(no_require_user_presence);
    876 	OPT_FLAG(require_verify);
    877 #undef OPT_FLAG
    878 
    879 	/* Simple integer options */
    880 	if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0)
    881 		goto out;
    882 
    883 	/* tunnel number can be negative to indicate "unset" */
    884 	if ((r = sshbuf_get_u8(m, &f)) != 0 ||
    885 	    (r = sshbuf_get_u32(m, &tmp)) != 0)
    886 		goto out;
    887 	opts->force_tun_device = f ? -1 : (int)tmp;
    888 
    889 	/* String options may be NULL */
    890 	if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 ||
    891 	    (r = deserialise_nullable_string(m, &opts->force_command)) != 0 ||
    892 	    (r = deserialise_nullable_string(m,
    893 	    &opts->required_from_host_cert)) != 0 ||
    894 	    (r = deserialise_nullable_string(m,
    895 	    &opts->required_from_host_keys)) != 0)
    896 		goto out;
    897 
    898 	/* Array options */
    899 	if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 ||
    900 	    (r = deserialise_array(m,
    901 	    &opts->permitopen, &opts->npermitopen)) != 0 ||
    902 	    (r = deserialise_array(m,
    903 	    &opts->permitlisten, &opts->npermitlisten)) != 0)
    904 		goto out;
    905 
    906 	/* success */
    907 	r = 0;
    908 	*optsp = opts;
    909 	opts = NULL;
    910  out:
    911 	sshauthopt_free(opts);
    912 	return r;
    913 }
    914