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