| dtucker@openbsd.org | b36ee3f | 2019-09-13 04:36:43 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth-options.c,v 1.89 2019/09/13 04:36:43 dtucker 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 | |
| djm@openbsd.org | be02d7c | 2019-09-06 04:53:27 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 23 | #include <netdb.h> |
| Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 24 | #include <pwd.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) { |
| dtucker@openbsd.org | b36ee3f | 2019-09-13 04:36:43 +0000 | [diff] [blame] | 268 | free(opt); |
| djm@openbsd.org | 87ddd67 | 2018-06-19 02:59:41 +0000 | [diff] [blame] | 269 | *errstrp = "memory allocation failed"; |
| 270 | return -1; |
| 271 | } |
| 272 | free(opt); |
| 273 | opt = tmp; |
| 274 | } |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 275 | if ((tmp = strdup(opt)) == NULL) { |
| 276 | free(opt); |
| 277 | *errstrp = "memory allocation failed"; |
| 278 | return -1; |
| 279 | } |
| 280 | cp = tmp; |
| 281 | /* validate syntax before recording it. */ |
| 282 | host = hpdelim(&cp); |
| 283 | if (host == NULL || strlen(host) >= NI_MAXHOST) { |
| 284 | free(tmp); |
| 285 | free(opt); |
| 286 | *errstrp = "invalid permission hostname"; |
| 287 | return -1; |
| 288 | } |
| 289 | /* |
| 290 | * don't want to use permitopen_port to avoid |
| 291 | * dependency on channels.[ch] here. |
| 292 | */ |
| 293 | if (cp == NULL || |
| 294 | (strcmp(cp, "*") != 0 && a2port(cp) <= 0)) { |
| 295 | free(tmp); |
| 296 | free(opt); |
| 297 | *errstrp = "invalid permission port"; |
| 298 | return -1; |
| 299 | } |
| 300 | /* XXX - add streamlocal support */ |
| 301 | free(tmp); |
| 302 | /* Record it */ |
| 303 | if ((permits = recallocarray(permits, npermits, npermits + 1, |
| 304 | sizeof(*permits))) == NULL) { |
| 305 | free(opt); |
| 306 | /* NB. don't update *permitsp if alloc fails */ |
| 307 | *errstrp = "memory allocation failed"; |
| 308 | return -1; |
| 309 | } |
| 310 | permits[npermits++] = opt; |
| 311 | *permitsp = permits; |
| 312 | *npermitsp = npermits; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 316 | struct sshauthopt * |
| 317 | sshauthopt_parse(const char *opts, const char **errstrp) |
| 318 | { |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 319 | char **oarray, *opt, *cp, *tmp; |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 320 | int r; |
| 321 | struct sshauthopt *ret = NULL; |
| 322 | const char *errstr = "unknown error"; |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 323 | uint64_t valid_before; |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 324 | |
| 325 | if (errstrp != NULL) |
| 326 | *errstrp = NULL; |
| 327 | if ((ret = sshauthopt_new_with_keys_defaults()) == NULL) |
| 328 | goto alloc_fail; |
| 329 | |
| 330 | if (opts == NULL) |
| 331 | return ret; |
| 332 | |
| 333 | while (*opts && *opts != ' ' && *opts != '\t') { |
| 334 | /* flag options */ |
| 335 | if ((r = opt_flag("restrict", 0, &opts)) != -1) { |
| 336 | ret->restricted = 1; |
| 337 | ret->permit_port_forwarding_flag = 0; |
| 338 | ret->permit_agent_forwarding_flag = 0; |
| 339 | ret->permit_x11_forwarding_flag = 0; |
| 340 | ret->permit_pty_flag = 0; |
| 341 | ret->permit_user_rc = 0; |
| 342 | } else if ((r = opt_flag("cert-authority", 0, &opts)) != -1) { |
| 343 | ret->cert_authority = r; |
| 344 | } else if ((r = opt_flag("port-forwarding", 1, &opts)) != -1) { |
| 345 | ret->permit_port_forwarding_flag = r == 1; |
| 346 | } else if ((r = opt_flag("agent-forwarding", 1, &opts)) != -1) { |
| 347 | ret->permit_agent_forwarding_flag = r == 1; |
| 348 | } else if ((r = opt_flag("x11-forwarding", 1, &opts)) != -1) { |
| 349 | ret->permit_x11_forwarding_flag = r == 1; |
| 350 | } else if ((r = opt_flag("pty", 1, &opts)) != -1) { |
| 351 | ret->permit_pty_flag = r == 1; |
| 352 | } else if ((r = opt_flag("user-rc", 1, &opts)) != -1) { |
| 353 | ret->permit_user_rc = r == 1; |
| 354 | } else if (opt_match(&opts, "command")) { |
| 355 | if (ret->force_command != NULL) { |
| 356 | errstr = "multiple \"command\" clauses"; |
| 357 | goto fail; |
| 358 | } |
| 359 | ret->force_command = opt_dequote(&opts, &errstr); |
| 360 | if (ret->force_command == NULL) |
| 361 | goto fail; |
| 362 | } else if (opt_match(&opts, "principals")) { |
| 363 | if (ret->cert_principals != NULL) { |
| 364 | errstr = "multiple \"principals\" clauses"; |
| 365 | goto fail; |
| 366 | } |
| 367 | ret->cert_principals = opt_dequote(&opts, &errstr); |
| 368 | if (ret->cert_principals == NULL) |
| 369 | goto fail; |
| 370 | } else if (opt_match(&opts, "from")) { |
| 371 | if (ret->required_from_host_keys != NULL) { |
| 372 | errstr = "multiple \"from\" clauses"; |
| 373 | goto fail; |
| 374 | } |
| 375 | ret->required_from_host_keys = opt_dequote(&opts, |
| 376 | &errstr); |
| 377 | if (ret->required_from_host_keys == NULL) |
| 378 | goto fail; |
| djm@openbsd.org | abc0fa3 | 2018-03-14 05:35:40 +0000 | [diff] [blame] | 379 | } else if (opt_match(&opts, "expiry-time")) { |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 380 | if ((opt = opt_dequote(&opts, &errstr)) == NULL) |
| 381 | goto fail; |
| 382 | if (parse_absolute_time(opt, &valid_before) != 0 || |
| 383 | valid_before == 0) { |
| 384 | free(opt); |
| 385 | errstr = "invalid expires time"; |
| 386 | goto fail; |
| 387 | } |
| 388 | free(opt); |
| 389 | if (ret->valid_before == 0 || |
| 390 | valid_before < ret->valid_before) |
| 391 | ret->valid_before = valid_before; |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 392 | } else if (opt_match(&opts, "environment")) { |
| 393 | if (ret->nenv > INT_MAX) { |
| 394 | errstr = "too many environment strings"; |
| 395 | goto fail; |
| 396 | } |
| 397 | if ((opt = opt_dequote(&opts, &errstr)) == NULL) |
| 398 | goto fail; |
| 399 | /* env name must be alphanumeric and followed by '=' */ |
| 400 | if ((tmp = strchr(opt, '=')) == NULL) { |
| 401 | free(opt); |
| 402 | errstr = "invalid environment string"; |
| 403 | goto fail; |
| 404 | } |
| djm@openbsd.org | 5eff5b8 | 2018-10-03 06:38:35 +0000 | [diff] [blame] | 405 | if ((cp = strdup(opt)) == NULL) |
| 406 | goto alloc_fail; |
| 407 | cp[tmp - opt] = '\0'; /* truncate at '=' */ |
| 408 | if (!valid_env_name(cp)) { |
| 409 | free(cp); |
| 410 | free(opt); |
| 411 | errstr = "invalid environment string"; |
| 412 | goto fail; |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 413 | } |
| djm@openbsd.org | 5eff5b8 | 2018-10-03 06:38:35 +0000 | [diff] [blame] | 414 | free(cp); |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 415 | /* Append it. */ |
| 416 | oarray = ret->env; |
| 417 | if ((ret->env = recallocarray(ret->env, ret->nenv, |
| 418 | ret->nenv + 1, sizeof(*ret->env))) == NULL) { |
| 419 | free(opt); |
| 420 | ret->env = oarray; /* put it back for cleanup */ |
| 421 | goto alloc_fail; |
| 422 | } |
| 423 | ret->env[ret->nenv++] = opt; |
| 424 | } else if (opt_match(&opts, "permitopen")) { |
| djm@openbsd.org | 87ddd67 | 2018-06-19 02:59:41 +0000 | [diff] [blame] | 425 | if (handle_permit(&opts, 0, &ret->permitopen, |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 426 | &ret->npermitopen, &errstr) != 0) |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 427 | goto fail; |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 428 | } else if (opt_match(&opts, "permitlisten")) { |
| djm@openbsd.org | 87ddd67 | 2018-06-19 02:59:41 +0000 | [diff] [blame] | 429 | if (handle_permit(&opts, 1, &ret->permitlisten, |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 430 | &ret->npermitlisten, &errstr) != 0) |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 431 | goto fail; |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 432 | } else if (opt_match(&opts, "tunnel")) { |
| 433 | if ((opt = opt_dequote(&opts, &errstr)) == NULL) |
| 434 | goto fail; |
| 435 | ret->force_tun_device = a2tun(opt, NULL); |
| 436 | free(opt); |
| 437 | if (ret->force_tun_device == SSH_TUNID_ERR) { |
| 438 | errstr = "invalid tun device"; |
| 439 | goto fail; |
| 440 | } |
| 441 | } |
| 442 | /* |
| 443 | * Skip the comma, and move to the next option |
| 444 | * (or break out if there are no more). |
| 445 | */ |
| 446 | if (*opts == '\0' || *opts == ' ' || *opts == '\t') |
| 447 | break; /* End of options. */ |
| 448 | /* Anything other than a comma is an unknown option */ |
| 449 | if (*opts != ',') { |
| 450 | errstr = "unknown key option"; |
| 451 | goto fail; |
| 452 | } |
| 453 | opts++; |
| 454 | if (*opts == '\0') { |
| 455 | errstr = "unexpected end-of-options"; |
| 456 | goto fail; |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /* success */ |
| 461 | if (errstrp != NULL) |
| 462 | *errstrp = NULL; |
| 463 | return ret; |
| 464 | |
| 465 | alloc_fail: |
| 466 | errstr = "memory allocation failed"; |
| 467 | fail: |
| 468 | sshauthopt_free(ret); |
| 469 | if (errstrp != NULL) |
| 470 | *errstrp = errstr; |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | struct sshauthopt * |
| 475 | sshauthopt_from_cert(struct sshkey *k) |
| 476 | { |
| 477 | struct sshauthopt *ret; |
| 478 | |
| 479 | if (k == NULL || !sshkey_type_is_cert(k->type) || k->cert == NULL || |
| 480 | k->cert->type != SSH2_CERT_TYPE_USER) |
| 481 | return NULL; |
| 482 | |
| 483 | if ((ret = sshauthopt_new()) == NULL) |
| 484 | return NULL; |
| 485 | |
| 486 | /* Handle options and critical extensions separately */ |
| 487 | if (cert_option_list(ret, k->cert->critical, |
| 488 | OPTIONS_CRITICAL, 1) == -1) { |
| 489 | sshauthopt_free(ret); |
| 490 | return NULL; |
| 491 | } |
| 492 | if (cert_option_list(ret, k->cert->extensions, |
| 493 | OPTIONS_EXTENSIONS, 0) == -1) { |
| 494 | sshauthopt_free(ret); |
| 495 | return NULL; |
| 496 | } |
| 497 | /* success */ |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Merges "additional" options to "primary" and returns the result. |
| 503 | * NB. Some options from primary have primacy. |
| 504 | */ |
| 505 | struct sshauthopt * |
| 506 | sshauthopt_merge(const struct sshauthopt *primary, |
| 507 | const struct sshauthopt *additional, const char **errstrp) |
| 508 | { |
| 509 | struct sshauthopt *ret; |
| 510 | const char *errstr = "internal error"; |
| 511 | const char *tmp; |
| 512 | |
| 513 | if (errstrp != NULL) |
| 514 | *errstrp = NULL; |
| 515 | |
| 516 | if ((ret = sshauthopt_new()) == NULL) |
| 517 | goto alloc_fail; |
| 518 | |
| 519 | /* cert_authority and cert_principals are cleared in result */ |
| 520 | |
| 521 | /* Prefer access lists from primary. */ |
| 522 | /* XXX err is both set and mismatch? */ |
| 523 | tmp = primary->required_from_host_cert; |
| 524 | if (tmp == NULL) |
| 525 | tmp = additional->required_from_host_cert; |
| 526 | if (tmp != NULL && (ret->required_from_host_cert = strdup(tmp)) == NULL) |
| 527 | goto alloc_fail; |
| 528 | tmp = primary->required_from_host_keys; |
| 529 | if (tmp == NULL) |
| 530 | tmp = additional->required_from_host_keys; |
| 531 | if (tmp != NULL && (ret->required_from_host_keys = strdup(tmp)) == NULL) |
| 532 | goto alloc_fail; |
| 533 | |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 534 | /* |
| 535 | * force_tun_device, permitopen/permitlisten and environment all |
| 536 | * prefer the primary. |
| 537 | */ |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 538 | ret->force_tun_device = primary->force_tun_device; |
| 539 | if (ret->force_tun_device == -1) |
| 540 | ret->force_tun_device = additional->force_tun_device; |
| 541 | if (primary->nenv > 0) { |
| 542 | if (dup_strings(&ret->env, &ret->nenv, |
| 543 | primary->env, primary->nenv) != 0) |
| 544 | goto alloc_fail; |
| 545 | } else if (additional->nenv) { |
| 546 | if (dup_strings(&ret->env, &ret->nenv, |
| 547 | additional->env, additional->nenv) != 0) |
| 548 | goto alloc_fail; |
| 549 | } |
| 550 | if (primary->npermitopen > 0) { |
| 551 | if (dup_strings(&ret->permitopen, &ret->npermitopen, |
| 552 | primary->permitopen, primary->npermitopen) != 0) |
| 553 | goto alloc_fail; |
| 554 | } else if (additional->npermitopen > 0) { |
| 555 | if (dup_strings(&ret->permitopen, &ret->npermitopen, |
| 556 | additional->permitopen, additional->npermitopen) != 0) |
| 557 | goto alloc_fail; |
| 558 | } |
| 559 | |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 560 | if (primary->npermitlisten > 0) { |
| 561 | if (dup_strings(&ret->permitlisten, &ret->npermitlisten, |
| 562 | primary->permitlisten, primary->npermitlisten) != 0) |
| 563 | goto alloc_fail; |
| 564 | } else if (additional->npermitlisten > 0) { |
| 565 | if (dup_strings(&ret->permitlisten, &ret->npermitlisten, |
| 566 | additional->permitlisten, additional->npermitlisten) != 0) |
| 567 | goto alloc_fail; |
| 568 | } |
| 569 | |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 570 | /* Flags are logical-AND (i.e. must be set in both for permission) */ |
| 571 | #define OPTFLAG(x) ret->x = (primary->x == 1) && (additional->x == 1) |
| 572 | OPTFLAG(permit_port_forwarding_flag); |
| 573 | OPTFLAG(permit_agent_forwarding_flag); |
| 574 | OPTFLAG(permit_x11_forwarding_flag); |
| 575 | OPTFLAG(permit_pty_flag); |
| 576 | OPTFLAG(permit_user_rc); |
| 577 | #undef OPTFLAG |
| 578 | |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 579 | /* Earliest expiry time should win */ |
| 580 | if (primary->valid_before != 0) |
| 581 | ret->valid_before = primary->valid_before; |
| 582 | if (additional->valid_before != 0 && |
| 583 | additional->valid_before < ret->valid_before) |
| 584 | ret->valid_before = additional->valid_before; |
| 585 | |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 586 | /* |
| 587 | * When both multiple forced-command are specified, only |
| 588 | * proceed if they are identical, otherwise fail. |
| 589 | */ |
| 590 | if (primary->force_command != NULL && |
| 591 | additional->force_command != NULL) { |
| 592 | if (strcmp(primary->force_command, |
| 593 | additional->force_command) == 0) { |
| 594 | /* ok */ |
| 595 | ret->force_command = strdup(primary->force_command); |
| 596 | if (ret->force_command == NULL) |
| 597 | goto alloc_fail; |
| 598 | } else { |
| 599 | errstr = "forced command options do not match"; |
| 600 | goto fail; |
| 601 | } |
| 602 | } else if (primary->force_command != NULL) { |
| 603 | if ((ret->force_command = strdup( |
| 604 | primary->force_command)) == NULL) |
| 605 | goto alloc_fail; |
| 606 | } else if (additional->force_command != NULL) { |
| 607 | if ((ret->force_command = strdup( |
| 608 | additional->force_command)) == NULL) |
| 609 | goto alloc_fail; |
| 610 | } |
| 611 | /* success */ |
| 612 | if (errstrp != NULL) |
| 613 | *errstrp = NULL; |
| 614 | return ret; |
| 615 | |
| 616 | alloc_fail: |
| 617 | errstr = "memory allocation failed"; |
| 618 | fail: |
| 619 | if (errstrp != NULL) |
| 620 | *errstrp = errstr; |
| 621 | sshauthopt_free(ret); |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * Copy options |
| 627 | */ |
| 628 | struct sshauthopt * |
| 629 | sshauthopt_copy(const struct sshauthopt *orig) |
| 630 | { |
| 631 | struct sshauthopt *ret; |
| 632 | |
| 633 | if ((ret = sshauthopt_new()) == NULL) |
| 634 | return NULL; |
| 635 | |
| 636 | #define OPTSCALAR(x) ret->x = orig->x |
| 637 | OPTSCALAR(permit_port_forwarding_flag); |
| 638 | OPTSCALAR(permit_agent_forwarding_flag); |
| 639 | OPTSCALAR(permit_x11_forwarding_flag); |
| 640 | OPTSCALAR(permit_pty_flag); |
| 641 | OPTSCALAR(permit_user_rc); |
| 642 | OPTSCALAR(restricted); |
| 643 | OPTSCALAR(cert_authority); |
| 644 | OPTSCALAR(force_tun_device); |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 645 | OPTSCALAR(valid_before); |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 646 | #undef OPTSCALAR |
| 647 | #define OPTSTRING(x) \ |
| 648 | do { \ |
| 649 | if (orig->x != NULL && (ret->x = strdup(orig->x)) == NULL) { \ |
| 650 | sshauthopt_free(ret); \ |
| 651 | return NULL; \ |
| 652 | } \ |
| 653 | } while (0) |
| 654 | OPTSTRING(cert_principals); |
| 655 | OPTSTRING(force_command); |
| 656 | OPTSTRING(required_from_host_cert); |
| 657 | OPTSTRING(required_from_host_keys); |
| 658 | #undef OPTSTRING |
| 659 | |
| 660 | if (dup_strings(&ret->env, &ret->nenv, orig->env, orig->nenv) != 0 || |
| 661 | dup_strings(&ret->permitopen, &ret->npermitopen, |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 662 | orig->permitopen, orig->npermitopen) != 0 || |
| 663 | dup_strings(&ret->permitlisten, &ret->npermitlisten, |
| 664 | orig->permitlisten, orig->npermitlisten) != 0) { |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 665 | sshauthopt_free(ret); |
| 666 | return NULL; |
| 667 | } |
| 668 | return ret; |
| 669 | } |
| 670 | |
| 671 | static int |
| 672 | serialise_array(struct sshbuf *m, char **a, size_t n) |
| 673 | { |
| 674 | struct sshbuf *b; |
| 675 | size_t i; |
| 676 | int r; |
| 677 | |
| 678 | if (n > INT_MAX) |
| 679 | return SSH_ERR_INTERNAL_ERROR; |
| 680 | |
| 681 | if ((b = sshbuf_new()) == NULL) { |
| 682 | return SSH_ERR_ALLOC_FAIL; |
| 683 | } |
| 684 | for (i = 0; i < n; i++) { |
| 685 | if ((r = sshbuf_put_cstring(b, a[i])) != 0) { |
| 686 | sshbuf_free(b); |
| 687 | return r; |
| 688 | } |
| 689 | } |
| 690 | if ((r = sshbuf_put_u32(m, n)) != 0 || |
| 691 | (r = sshbuf_put_stringb(m, b)) != 0) { |
| 692 | sshbuf_free(b); |
| 693 | return r; |
| 694 | } |
| 695 | /* success */ |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | static int |
| 700 | deserialise_array(struct sshbuf *m, char ***ap, size_t *np) |
| 701 | { |
| 702 | char **a = NULL; |
| 703 | size_t i, n = 0; |
| 704 | struct sshbuf *b = NULL; |
| 705 | u_int tmp; |
| 706 | int r = SSH_ERR_INTERNAL_ERROR; |
| 707 | |
| 708 | if ((r = sshbuf_get_u32(m, &tmp)) != 0 || |
| 709 | (r = sshbuf_froms(m, &b)) != 0) |
| 710 | goto out; |
| 711 | if (tmp > INT_MAX) { |
| 712 | r = SSH_ERR_INVALID_FORMAT; |
| 713 | goto out; |
| 714 | } |
| 715 | n = tmp; |
| 716 | if (n > 0 && (a = calloc(n, sizeof(*a))) == NULL) { |
| 717 | r = SSH_ERR_ALLOC_FAIL; |
| 718 | goto out; |
| 719 | } |
| 720 | for (i = 0; i < n; i++) { |
| 721 | if ((r = sshbuf_get_cstring(b, &a[i], NULL)) != 0) |
| 722 | goto out; |
| 723 | } |
| 724 | /* success */ |
| 725 | r = 0; |
| 726 | *ap = a; |
| 727 | a = NULL; |
| 728 | *np = n; |
| 729 | n = 0; |
| 730 | out: |
| 731 | for (i = 0; i < n; i++) |
| 732 | free(a[i]); |
| 733 | free(a); |
| 734 | sshbuf_free(b); |
| 735 | return r; |
| 736 | } |
| 737 | |
| 738 | static int |
| 739 | serialise_nullable_string(struct sshbuf *m, const char *s) |
| 740 | { |
| 741 | int r; |
| 742 | |
| 743 | if ((r = sshbuf_put_u8(m, s == NULL)) != 0 || |
| 744 | (r = sshbuf_put_cstring(m, s)) != 0) |
| 745 | return r; |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static int |
| 750 | deserialise_nullable_string(struct sshbuf *m, char **sp) |
| 751 | { |
| 752 | int r; |
| 753 | u_char flag; |
| 754 | |
| 755 | *sp = NULL; |
| 756 | if ((r = sshbuf_get_u8(m, &flag)) != 0 || |
| 757 | (r = sshbuf_get_cstring(m, flag ? NULL : sp, NULL)) != 0) |
| 758 | return r; |
| 759 | return 0; |
| 760 | } |
| 761 | |
| 762 | int |
| 763 | sshauthopt_serialise(const struct sshauthopt *opts, struct sshbuf *m, |
| 764 | int untrusted) |
| 765 | { |
| 766 | int r = SSH_ERR_INTERNAL_ERROR; |
| 767 | |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 768 | /* Flag and simple integer options */ |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 769 | if ((r = sshbuf_put_u8(m, opts->permit_port_forwarding_flag)) != 0 || |
| 770 | (r = sshbuf_put_u8(m, opts->permit_agent_forwarding_flag)) != 0 || |
| 771 | (r = sshbuf_put_u8(m, opts->permit_x11_forwarding_flag)) != 0 || |
| 772 | (r = sshbuf_put_u8(m, opts->permit_pty_flag)) != 0 || |
| 773 | (r = sshbuf_put_u8(m, opts->permit_user_rc)) != 0 || |
| 774 | (r = sshbuf_put_u8(m, opts->restricted)) != 0 || |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 775 | (r = sshbuf_put_u8(m, opts->cert_authority)) != 0 || |
| 776 | (r = sshbuf_put_u64(m, opts->valid_before)) != 0) |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 777 | return r; |
| 778 | |
| 779 | /* tunnel number can be negative to indicate "unset" */ |
| 780 | if ((r = sshbuf_put_u8(m, opts->force_tun_device == -1)) != 0 || |
| 781 | (r = sshbuf_put_u32(m, (opts->force_tun_device < 0) ? |
| 782 | 0 : (u_int)opts->force_tun_device)) != 0) |
| 783 | return r; |
| 784 | |
| 785 | /* String options; these may be NULL */ |
| 786 | if ((r = serialise_nullable_string(m, |
| 787 | untrusted ? "yes" : opts->cert_principals)) != 0 || |
| 788 | (r = serialise_nullable_string(m, |
| 789 | untrusted ? "true" : opts->force_command)) != 0 || |
| 790 | (r = serialise_nullable_string(m, |
| 791 | untrusted ? NULL : opts->required_from_host_cert)) != 0 || |
| 792 | (r = serialise_nullable_string(m, |
| 793 | untrusted ? NULL : opts->required_from_host_keys)) != 0) |
| 794 | return r; |
| 795 | |
| 796 | /* Array options */ |
| 797 | if ((r = serialise_array(m, opts->env, |
| 798 | untrusted ? 0 : opts->nenv)) != 0 || |
| 799 | (r = serialise_array(m, opts->permitopen, |
| djm@openbsd.org | 6d41815 | 2018-06-07 09:26:42 +0000 | [diff] [blame] | 800 | untrusted ? 0 : opts->npermitopen)) != 0 || |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 801 | (r = serialise_array(m, opts->permitlisten, |
| 802 | untrusted ? 0 : opts->npermitlisten)) != 0) |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 803 | return r; |
| 804 | |
| 805 | /* success */ |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | int |
| 810 | sshauthopt_deserialise(struct sshbuf *m, struct sshauthopt **optsp) |
| 811 | { |
| 812 | struct sshauthopt *opts = NULL; |
| 813 | int r = SSH_ERR_INTERNAL_ERROR; |
| 814 | u_char f; |
| 815 | u_int tmp; |
| 816 | |
| 817 | if ((opts = calloc(1, sizeof(*opts))) == NULL) |
| 818 | return SSH_ERR_ALLOC_FAIL; |
| 819 | |
| 820 | #define OPT_FLAG(x) \ |
| 821 | do { \ |
| 822 | if ((r = sshbuf_get_u8(m, &f)) != 0) \ |
| 823 | goto out; \ |
| 824 | opts->x = f; \ |
| 825 | } while (0) |
| 826 | OPT_FLAG(permit_port_forwarding_flag); |
| 827 | OPT_FLAG(permit_agent_forwarding_flag); |
| 828 | OPT_FLAG(permit_x11_forwarding_flag); |
| 829 | OPT_FLAG(permit_pty_flag); |
| 830 | OPT_FLAG(permit_user_rc); |
| 831 | OPT_FLAG(restricted); |
| 832 | OPT_FLAG(cert_authority); |
| 833 | #undef OPT_FLAG |
| 834 | |
| djm@openbsd.org | bf0fbf2 | 2018-03-12 00:52:01 +0000 | [diff] [blame] | 835 | if ((r = sshbuf_get_u64(m, &opts->valid_before)) != 0) |
| 836 | goto out; |
| 837 | |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 838 | /* tunnel number can be negative to indicate "unset" */ |
| 839 | if ((r = sshbuf_get_u8(m, &f)) != 0 || |
| 840 | (r = sshbuf_get_u32(m, &tmp)) != 0) |
| 841 | goto out; |
| 842 | opts->force_tun_device = f ? -1 : (int)tmp; |
| 843 | |
| 844 | /* String options may be NULL */ |
| 845 | if ((r = deserialise_nullable_string(m, &opts->cert_principals)) != 0 || |
| 846 | (r = deserialise_nullable_string(m, &opts->force_command)) != 0 || |
| 847 | (r = deserialise_nullable_string(m, |
| 848 | &opts->required_from_host_cert)) != 0 || |
| 849 | (r = deserialise_nullable_string(m, |
| 850 | &opts->required_from_host_keys)) != 0) |
| 851 | goto out; |
| 852 | |
| 853 | /* Array options */ |
| 854 | if ((r = deserialise_array(m, &opts->env, &opts->nenv)) != 0 || |
| 855 | (r = deserialise_array(m, |
| djm@openbsd.org | 93c06ab | 2018-06-06 18:23:32 +0000 | [diff] [blame] | 856 | &opts->permitopen, &opts->npermitopen)) != 0 || |
| 857 | (r = deserialise_array(m, |
| 858 | &opts->permitlisten, &opts->npermitlisten)) != 0) |
| djm@openbsd.org | 90c4bec | 2018-03-03 03:06:02 +0000 | [diff] [blame] | 859 | goto out; |
| 860 | |
| 861 | /* success */ |
| 862 | r = 0; |
| 863 | *optsp = opts; |
| 864 | opts = NULL; |
| 865 | out: |
| 866 | sshauthopt_free(opts); |
| 867 | return r; |
| 868 | } |