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