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