[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.96

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