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