[BACK]Return to auth-options.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth-options.c, Revision 1.88

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