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