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

1.61    ! djm         1: /* $OpenBSD: auth-options.c,v 1.60 2013/11/02 22:34:01 markus Exp $ */
1.3       deraadt     2: /*
                      3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
                     11:  */
                     12:
1.36      stevesk    13: #include <sys/types.h>
1.42      djm        14: #include <sys/queue.h>
1.36      stevesk    15:
1.37      stevesk    16: #include <netdb.h>
1.36      stevesk    17: #include <pwd.h>
1.39      stevesk    18: #include <string.h>
1.40      deraadt    19: #include <stdio.h>
                     20: #include <stdarg.h>
1.1       markus     21:
                     22: #include "xmalloc.h"
                     23: #include "match.h"
1.11      markus     24: #include "log.h"
                     25: #include "canohost.h"
1.40      deraadt    26: #include "buffer.h"
1.18      markus     27: #include "channels.h"
1.12      markus     28: #include "servconf.h"
1.20      stevesk    29: #include "misc.h"
1.40      deraadt    30: #include "key.h"
1.50      djm        31: #include "auth-options.h"
1.40      deraadt    32: #include "hostfile.h"
                     33: #include "auth.h"
1.1       markus     34:
                     35: /* Flags set authorized_keys flags */
                     36: int no_port_forwarding_flag = 0;
                     37: int no_agent_forwarding_flag = 0;
                     38: int no_x11_forwarding_flag = 0;
                     39: int no_pty_flag = 0;
1.41      djm        40: int no_user_rc = 0;
1.45      djm        41: int key_is_cert_authority = 0;
1.1       markus     42:
                     43: /* "command=" option. */
                     44: char *forced_command = NULL;
                     45:
                     46: /* "environment=" options. */
                     47: struct envstring *custom_environment = NULL;
                     48:
1.32      reyk       49: /* "tunnel=" option. */
                     50: int forced_tun_device = -1;
                     51:
1.51      djm        52: /* "principals=" option. */
                     53: char *authorized_principals = NULL;
                     54:
1.12      markus     55: extern ServerOptions options;
                     56:
1.22      provos     57: void
1.5       markus     58: auth_clear_options(void)
                     59: {
                     60:        no_agent_forwarding_flag = 0;
                     61:        no_port_forwarding_flag = 0;
                     62:        no_pty_flag = 0;
                     63:        no_x11_forwarding_flag = 0;
1.41      djm        64:        no_user_rc = 0;
1.45      djm        65:        key_is_cert_authority = 0;
1.5       markus     66:        while (custom_environment) {
                     67:                struct envstring *ce = custom_environment;
                     68:                custom_environment = ce->next;
1.58      djm        69:                free(ce->s);
                     70:                free(ce);
1.5       markus     71:        }
                     72:        if (forced_command) {
1.58      djm        73:                free(forced_command);
1.5       markus     74:                forced_command = NULL;
                     75:        }
1.51      djm        76:        if (authorized_principals) {
1.58      djm        77:                free(authorized_principals);
1.51      djm        78:                authorized_principals = NULL;
                     79:        }
1.32      reyk       80:        forced_tun_device = -1;
1.15      markus     81:        channel_clear_permitted_opens();
1.5       markus     82: }
                     83:
1.10      markus     84: /*
                     85:  * return 1 if access is granted, 0 if not.
                     86:  * side effect: sets key option flags
                     87:  */
1.1       markus     88: int
1.12      markus     89: auth_parse_options(struct passwd *pw, char *opts, char *file, u_long linenum)
1.1       markus     90: {
                     91:        const char *cp;
1.15      markus     92:        int i;
1.5       markus     93:
                     94:        /* reset options */
                     95:        auth_clear_options();
1.13      markus     96:
                     97:        if (!opts)
                     98:                return 1;
1.5       markus     99:
1.12      markus    100:        while (*opts && *opts != ' ' && *opts != '\t') {
1.45      djm       101:                cp = "cert-authority";
                    102:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    103:                        key_is_cert_authority = 1;
                    104:                        opts += strlen(cp);
                    105:                        goto next_option;
                    106:                }
1.1       markus    107:                cp = "no-port-forwarding";
1.12      markus    108:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.24      markus    109:                        auth_debug_add("Port forwarding disabled.");
1.1       markus    110:                        no_port_forwarding_flag = 1;
1.12      markus    111:                        opts += strlen(cp);
1.1       markus    112:                        goto next_option;
                    113:                }
                    114:                cp = "no-agent-forwarding";
1.12      markus    115:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.24      markus    116:                        auth_debug_add("Agent forwarding disabled.");
1.1       markus    117:                        no_agent_forwarding_flag = 1;
1.12      markus    118:                        opts += strlen(cp);
1.1       markus    119:                        goto next_option;
                    120:                }
                    121:                cp = "no-X11-forwarding";
1.12      markus    122:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.24      markus    123:                        auth_debug_add("X11 forwarding disabled.");
1.1       markus    124:                        no_x11_forwarding_flag = 1;
1.12      markus    125:                        opts += strlen(cp);
1.1       markus    126:                        goto next_option;
                    127:                }
                    128:                cp = "no-pty";
1.12      markus    129:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.24      markus    130:                        auth_debug_add("Pty allocation disabled.");
1.1       markus    131:                        no_pty_flag = 1;
1.41      djm       132:                        opts += strlen(cp);
                    133:                        goto next_option;
                    134:                }
                    135:                cp = "no-user-rc";
                    136:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    137:                        auth_debug_add("User rc file execution disabled.");
                    138:                        no_user_rc = 1;
1.12      markus    139:                        opts += strlen(cp);
1.1       markus    140:                        goto next_option;
                    141:                }
                    142:                cp = "command=\"";
1.12      markus    143:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    144:                        opts += strlen(cp);
1.51      djm       145:                        if (forced_command != NULL)
1.58      djm       146:                                free(forced_command);
1.12      markus    147:                        forced_command = xmalloc(strlen(opts) + 1);
1.1       markus    148:                        i = 0;
1.12      markus    149:                        while (*opts) {
                    150:                                if (*opts == '"')
1.1       markus    151:                                        break;
1.12      markus    152:                                if (*opts == '\\' && opts[1] == '"') {
                    153:                                        opts += 2;
1.1       markus    154:                                        forced_command[i++] = '"';
                    155:                                        continue;
                    156:                                }
1.12      markus    157:                                forced_command[i++] = *opts++;
1.1       markus    158:                        }
1.12      markus    159:                        if (!*opts) {
1.1       markus    160:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    161:                                    file, linenum);
1.24      markus    162:                                auth_debug_add("%.100s, line %lu: missing end quote",
1.10      markus    163:                                    file, linenum);
1.58      djm       164:                                free(forced_command);
1.14      markus    165:                                forced_command = NULL;
                    166:                                goto bad_option;
1.1       markus    167:                        }
1.38      dtucker   168:                        forced_command[i] = '\0';
1.54      djm       169:                        auth_debug_add("Forced command.");
1.51      djm       170:                        opts++;
                    171:                        goto next_option;
                    172:                }
                    173:                cp = "principals=\"";
                    174:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    175:                        opts += strlen(cp);
                    176:                        if (authorized_principals != NULL)
1.58      djm       177:                                free(authorized_principals);
1.51      djm       178:                        authorized_principals = xmalloc(strlen(opts) + 1);
                    179:                        i = 0;
                    180:                        while (*opts) {
                    181:                                if (*opts == '"')
                    182:                                        break;
                    183:                                if (*opts == '\\' && opts[1] == '"') {
                    184:                                        opts += 2;
                    185:                                        authorized_principals[i++] = '"';
                    186:                                        continue;
                    187:                                }
                    188:                                authorized_principals[i++] = *opts++;
                    189:                        }
                    190:                        if (!*opts) {
                    191:                                debug("%.100s, line %lu: missing end quote",
                    192:                                    file, linenum);
                    193:                                auth_debug_add("%.100s, line %lu: missing end quote",
                    194:                                    file, linenum);
1.58      djm       195:                                free(authorized_principals);
1.51      djm       196:                                authorized_principals = NULL;
                    197:                                goto bad_option;
                    198:                        }
                    199:                        authorized_principals[i] = '\0';
                    200:                        auth_debug_add("principals: %.900s",
                    201:                            authorized_principals);
1.12      markus    202:                        opts++;
1.1       markus    203:                        goto next_option;
                    204:                }
                    205:                cp = "environment=\"";
1.26      markus    206:                if (options.permit_user_env &&
                    207:                    strncasecmp(opts, cp, strlen(cp)) == 0) {
1.1       markus    208:                        char *s;
                    209:                        struct envstring *new_envstring;
1.15      markus    210:
1.12      markus    211:                        opts += strlen(cp);
                    212:                        s = xmalloc(strlen(opts) + 1);
1.1       markus    213:                        i = 0;
1.12      markus    214:                        while (*opts) {
                    215:                                if (*opts == '"')
1.1       markus    216:                                        break;
1.12      markus    217:                                if (*opts == '\\' && opts[1] == '"') {
                    218:                                        opts += 2;
1.1       markus    219:                                        s[i++] = '"';
                    220:                                        continue;
                    221:                                }
1.12      markus    222:                                s[i++] = *opts++;
1.1       markus    223:                        }
1.12      markus    224:                        if (!*opts) {
1.1       markus    225:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    226:                                    file, linenum);
1.24      markus    227:                                auth_debug_add("%.100s, line %lu: missing end quote",
1.10      markus    228:                                    file, linenum);
1.58      djm       229:                                free(s);
1.14      markus    230:                                goto bad_option;
1.1       markus    231:                        }
1.38      dtucker   232:                        s[i] = '\0';
1.24      markus    233:                        auth_debug_add("Adding to environment: %.900s", s);
1.1       markus    234:                        debug("Adding to environment: %.900s", s);
1.12      markus    235:                        opts++;
1.61    ! djm       236:                        new_envstring = xcalloc(1, sizeof(struct envstring));
1.1       markus    237:                        new_envstring->s = s;
                    238:                        new_envstring->next = custom_environment;
                    239:                        custom_environment = new_envstring;
                    240:                        goto next_option;
                    241:                }
                    242:                cp = "from=\"";
1.12      markus    243:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    244:                        const char *remote_ip = get_remote_ipaddr();
                    245:                        const char *remote_host = get_canonical_hostname(
1.28      markus    246:                            options.use_dns);
1.12      markus    247:                        char *patterns = xmalloc(strlen(opts) + 1);
1.15      markus    248:
1.12      markus    249:                        opts += strlen(cp);
1.1       markus    250:                        i = 0;
1.12      markus    251:                        while (*opts) {
                    252:                                if (*opts == '"')
1.1       markus    253:                                        break;
1.12      markus    254:                                if (*opts == '\\' && opts[1] == '"') {
                    255:                                        opts += 2;
1.1       markus    256:                                        patterns[i++] = '"';
                    257:                                        continue;
                    258:                                }
1.12      markus    259:                                patterns[i++] = *opts++;
1.1       markus    260:                        }
1.12      markus    261:                        if (!*opts) {
1.1       markus    262:                                debug("%.100s, line %lu: missing end quote",
1.10      markus    263:                                    file, linenum);
1.24      markus    264:                                auth_debug_add("%.100s, line %lu: missing end quote",
1.10      markus    265:                                    file, linenum);
1.58      djm       266:                                free(patterns);
1.14      markus    267:                                goto bad_option;
1.1       markus    268:                        }
1.38      dtucker   269:                        patterns[i] = '\0';
1.12      markus    270:                        opts++;
1.43      djm       271:                        switch (match_host_and_ip(remote_host, remote_ip,
                    272:                            patterns)) {
                    273:                        case 1:
1.58      djm       274:                                free(patterns);
1.43      djm       275:                                /* Host name matches. */
                    276:                                goto next_option;
                    277:                        case -1:
                    278:                                debug("%.100s, line %lu: invalid criteria",
                    279:                                    file, linenum);
                    280:                                auth_debug_add("%.100s, line %lu: "
                    281:                                    "invalid criteria", file, linenum);
                    282:                                /* FALLTHROUGH */
                    283:                        case 0:
1.58      djm       284:                                free(patterns);
1.27      itojun    285:                                logit("Authentication tried for %.100s with "
1.12      markus    286:                                    "correct key but not from a permitted "
                    287:                                    "host (host=%.200s, ip=%.200s).",
                    288:                                    pw->pw_name, remote_host, remote_ip);
1.24      markus    289:                                auth_debug_add("Your host '%.200s' is not "
1.12      markus    290:                                    "permitted to use this key for login.",
                    291:                                    remote_host);
1.43      djm       292:                                break;
1.1       markus    293:                        }
1.43      djm       294:                        /* deny access */
                    295:                        return 0;
1.15      markus    296:                }
                    297:                cp = "permitopen=\"";
                    298:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
1.29      djm       299:                        char *host, *p;
1.44      djm       300:                        int port;
1.15      markus    301:                        char *patterns = xmalloc(strlen(opts) + 1);
                    302:
                    303:                        opts += strlen(cp);
                    304:                        i = 0;
                    305:                        while (*opts) {
                    306:                                if (*opts == '"')
                    307:                                        break;
                    308:                                if (*opts == '\\' && opts[1] == '"') {
                    309:                                        opts += 2;
                    310:                                        patterns[i++] = '"';
                    311:                                        continue;
                    312:                                }
                    313:                                patterns[i++] = *opts++;
                    314:                        }
                    315:                        if (!*opts) {
                    316:                                debug("%.100s, line %lu: missing end quote",
                    317:                                    file, linenum);
1.29      djm       318:                                auth_debug_add("%.100s, line %lu: missing "
                    319:                                    "end quote", file, linenum);
1.58      djm       320:                                free(patterns);
1.15      markus    321:                                goto bad_option;
                    322:                        }
1.38      dtucker   323:                        patterns[i] = '\0';
1.15      markus    324:                        opts++;
1.29      djm       325:                        p = patterns;
                    326:                        host = hpdelim(&p);
                    327:                        if (host == NULL || strlen(host) >= NI_MAXHOST) {
                    328:                                debug("%.100s, line %lu: Bad permitopen "
1.31      deraadt   329:                                    "specification <%.100s>", file, linenum,
1.29      djm       330:                                    patterns);
1.24      markus    331:                                auth_debug_add("%.100s, line %lu: "
1.29      djm       332:                                    "Bad permitopen specification", file,
                    333:                                    linenum);
1.58      djm       334:                                free(patterns);
1.15      markus    335:                                goto bad_option;
                    336:                        }
1.30      deraadt   337:                        host = cleanhostname(host);
1.55      dtucker   338:                        if (p == NULL || (port = permitopen_port(p)) < 0) {
1.29      djm       339:                                debug("%.100s, line %lu: Bad permitopen port "
                    340:                                    "<%.100s>", file, linenum, p ? p : "");
1.24      markus    341:                                auth_debug_add("%.100s, line %lu: "
1.20      stevesk   342:                                    "Bad permitopen port", file, linenum);
1.58      djm       343:                                free(patterns);
1.15      markus    344:                                goto bad_option;
                    345:                        }
1.57      djm       346:                        if ((options.allow_tcp_forwarding & FORWARD_LOCAL) != 0)
1.20      stevesk   347:                                channel_add_permitted_opens(host, port);
1.58      djm       348:                        free(patterns);
1.32      reyk      349:                        goto next_option;
                    350:                }
                    351:                cp = "tunnel=\"";
                    352:                if (strncasecmp(opts, cp, strlen(cp)) == 0) {
                    353:                        char *tun = NULL;
                    354:                        opts += strlen(cp);
                    355:                        tun = xmalloc(strlen(opts) + 1);
                    356:                        i = 0;
                    357:                        while (*opts) {
                    358:                                if (*opts == '"')
                    359:                                        break;
                    360:                                tun[i++] = *opts++;
                    361:                        }
                    362:                        if (!*opts) {
                    363:                                debug("%.100s, line %lu: missing end quote",
                    364:                                    file, linenum);
                    365:                                auth_debug_add("%.100s, line %lu: missing end quote",
                    366:                                    file, linenum);
1.58      djm       367:                                free(tun);
1.32      reyk      368:                                forced_tun_device = -1;
                    369:                                goto bad_option;
                    370:                        }
1.38      dtucker   371:                        tun[i] = '\0';
1.32      reyk      372:                        forced_tun_device = a2tun(tun, NULL);
1.58      djm       373:                        free(tun);
1.33      reyk      374:                        if (forced_tun_device == SSH_TUNID_ERR) {
1.32      reyk      375:                                debug("%.100s, line %lu: invalid tun device",
                    376:                                    file, linenum);
                    377:                                auth_debug_add("%.100s, line %lu: invalid tun device",
                    378:                                    file, linenum);
                    379:                                forced_tun_device = -1;
                    380:                                goto bad_option;
                    381:                        }
                    382:                        auth_debug_add("Forced tun device: %d", forced_tun_device);
                    383:                        opts++;
1.1       markus    384:                        goto next_option;
                    385:                }
                    386: next_option:
                    387:                /*
                    388:                 * Skip the comma, and move to the next option
                    389:                 * (or break out if there are no more).
                    390:                 */
1.12      markus    391:                if (!*opts)
1.1       markus    392:                        fatal("Bugs in auth-options.c option processing.");
1.12      markus    393:                if (*opts == ' ' || *opts == '\t')
1.1       markus    394:                        break;          /* End of options. */
1.12      markus    395:                if (*opts != ',')
1.1       markus    396:                        goto bad_option;
1.12      markus    397:                opts++;
1.1       markus    398:                /* Process the next option. */
                    399:        }
1.22      provos    400:
1.1       markus    401:        /* grant access */
                    402:        return 1;
                    403:
                    404: bad_option:
1.27      itojun    405:        logit("Bad options in %.100s file, line %lu: %.50s",
1.12      markus    406:            file, linenum, opts);
1.24      markus    407:        auth_debug_add("Bad options in %.100s file, line %lu: %.50s",
1.12      markus    408:            file, linenum, opts);
1.22      provos    409:
1.1       markus    410:        /* deny access */
                    411:        return 0;
                    412: }
1.45      djm       413:
1.52      djm       414: #define OPTIONS_CRITICAL       1
                    415: #define OPTIONS_EXTENSIONS     2
                    416: static int
                    417: parse_option_list(u_char *optblob, size_t optblob_len, struct passwd *pw,
                    418:     u_int which, int crit,
                    419:     int *cert_no_port_forwarding_flag,
                    420:     int *cert_no_agent_forwarding_flag,
                    421:     int *cert_no_x11_forwarding_flag,
                    422:     int *cert_no_pty_flag,
                    423:     int *cert_no_user_rc,
                    424:     char **cert_forced_command,
                    425:     int *cert_source_address_done)
1.45      djm       426: {
1.52      djm       427:        char *command, *allowed;
                    428:        const char *remote_ip;
1.59      djm       429:        char *name = NULL;
                    430:        u_char *data_blob = NULL;
1.46      djm       431:        u_int nlen, dlen, clen;
1.45      djm       432:        Buffer c, data;
1.52      djm       433:        int ret = -1, found;
1.45      djm       434:
                    435:        buffer_init(&data);
                    436:
                    437:        /* Make copy to avoid altering original */
                    438:        buffer_init(&c);
1.52      djm       439:        buffer_append(&c, optblob, optblob_len);
1.45      djm       440:
                    441:        while (buffer_len(&c) > 0) {
1.53      djm       442:                if ((name = buffer_get_cstring_ret(&c, &nlen)) == NULL ||
1.46      djm       443:                    (data_blob = buffer_get_string_ret(&c, &dlen)) == NULL) {
1.50      djm       444:                        error("Certificate options corrupt");
1.45      djm       445:                        goto out;
                    446:                }
1.46      djm       447:                buffer_append(&data, data_blob, dlen);
1.52      djm       448:                debug3("found certificate option \"%.100s\" len %u",
1.46      djm       449:                    name, dlen);
1.52      djm       450:                found = 0;
                    451:                if ((which & OPTIONS_EXTENSIONS) != 0) {
                    452:                        if (strcmp(name, "permit-X11-forwarding") == 0) {
                    453:                                *cert_no_x11_forwarding_flag = 0;
                    454:                                found = 1;
                    455:                        } else if (strcmp(name,
                    456:                            "permit-agent-forwarding") == 0) {
                    457:                                *cert_no_agent_forwarding_flag = 0;
                    458:                                found = 1;
                    459:                        } else if (strcmp(name,
                    460:                            "permit-port-forwarding") == 0) {
                    461:                                *cert_no_port_forwarding_flag = 0;
                    462:                                found = 1;
                    463:                        } else if (strcmp(name, "permit-pty") == 0) {
                    464:                                *cert_no_pty_flag = 0;
                    465:                                found = 1;
                    466:                        } else if (strcmp(name, "permit-user-rc") == 0) {
                    467:                                *cert_no_user_rc = 0;
                    468:                                found = 1;
                    469:                        }
                    470:                }
                    471:                if (!found && (which & OPTIONS_CRITICAL) != 0) {
                    472:                        if (strcmp(name, "force-command") == 0) {
1.53      djm       473:                                if ((command = buffer_get_cstring_ret(&data,
1.52      djm       474:                                    &clen)) == NULL) {
                    475:                                        error("Certificate constraint \"%s\" "
                    476:                                            "corrupt", name);
                    477:                                        goto out;
                    478:                                }
                    479:                                if (*cert_forced_command != NULL) {
                    480:                                        error("Certificate has multiple "
                    481:                                            "force-command options");
1.58      djm       482:                                        free(command);
1.52      djm       483:                                        goto out;
                    484:                                }
                    485:                                *cert_forced_command = command;
                    486:                                found = 1;
1.45      djm       487:                        }
1.52      djm       488:                        if (strcmp(name, "source-address") == 0) {
1.53      djm       489:                                if ((allowed = buffer_get_cstring_ret(&data,
1.52      djm       490:                                    &clen)) == NULL) {
                    491:                                        error("Certificate constraint "
                    492:                                            "\"%s\" corrupt", name);
                    493:                                        goto out;
                    494:                                }
                    495:                                if ((*cert_source_address_done)++) {
                    496:                                        error("Certificate has multiple "
                    497:                                            "source-address options");
1.58      djm       498:                                        free(allowed);
1.52      djm       499:                                        goto out;
                    500:                                }
                    501:                                remote_ip = get_remote_ipaddr();
                    502:                                switch (addr_match_cidr_list(remote_ip,
                    503:                                    allowed)) {
                    504:                                case 1:
                    505:                                        /* accepted */
1.58      djm       506:                                        free(allowed);
1.52      djm       507:                                        break;
                    508:                                case 0:
                    509:                                        /* no match */
                    510:                                        logit("Authentication tried for %.100s "
                    511:                                            "with valid certificate but not "
                    512:                                            "from a permitted host "
                    513:                                            "(ip=%.200s).", pw->pw_name,
                    514:                                            remote_ip);
                    515:                                        auth_debug_add("Your address '%.200s' "
                    516:                                            "is not permitted to use this "
                    517:                                            "certificate for login.",
                    518:                                            remote_ip);
1.58      djm       519:                                        free(allowed);
1.52      djm       520:                                        goto out;
                    521:                                case -1:
                    522:                                        error("Certificate source-address "
                    523:                                            "contents invalid");
1.58      djm       524:                                        free(allowed);
1.52      djm       525:                                        goto out;
                    526:                                }
                    527:                                found = 1;
1.46      djm       528:                        }
1.52      djm       529:                }
                    530:
                    531:                if (!found) {
                    532:                        if (crit) {
                    533:                                error("Certificate critical option \"%s\" "
                    534:                                    "is not supported", name);
1.45      djm       535:                                goto out;
1.52      djm       536:                        } else {
                    537:                                logit("Certificate extension \"%s\" "
                    538:                                    "is not supported", name);
1.45      djm       539:                        }
1.52      djm       540:                } else if (buffer_len(&data) != 0) {
                    541:                        error("Certificate option \"%s\" corrupt "
1.45      djm       542:                            "(extra data)", name);
                    543:                        goto out;
                    544:                }
                    545:                buffer_clear(&data);
1.58      djm       546:                free(name);
                    547:                free(data_blob);
1.59      djm       548:                name = NULL;
                    549:                data_blob = NULL;
1.45      djm       550:        }
1.50      djm       551:        /* successfully parsed all options */
1.45      djm       552:        ret = 0;
                    553:
1.52      djm       554:  out:
                    555:        if (ret != 0 &&
                    556:            cert_forced_command != NULL &&
                    557:            *cert_forced_command != NULL) {
1.58      djm       558:                free(*cert_forced_command);
1.52      djm       559:                *cert_forced_command = NULL;
                    560:        }
                    561:        if (name != NULL)
1.58      djm       562:                free(name);
1.52      djm       563:        if (data_blob != NULL)
1.58      djm       564:                free(data_blob);
1.52      djm       565:        buffer_free(&data);
                    566:        buffer_free(&c);
                    567:        return ret;
                    568: }
                    569:
                    570: /*
                    571:  * Set options from critical certificate options. These supersede user key
                    572:  * options so this must be called after auth_parse_options().
                    573:  */
                    574: int
                    575: auth_cert_options(Key *k, struct passwd *pw)
                    576: {
                    577:        int cert_no_port_forwarding_flag = 1;
                    578:        int cert_no_agent_forwarding_flag = 1;
                    579:        int cert_no_x11_forwarding_flag = 1;
                    580:        int cert_no_pty_flag = 1;
                    581:        int cert_no_user_rc = 1;
                    582:        char *cert_forced_command = NULL;
                    583:        int cert_source_address_done = 0;
                    584:
                    585:        if (key_cert_is_legacy(k)) {
                    586:                /* All options are in the one field for v00 certs */
                    587:                if (parse_option_list(buffer_ptr(&k->cert->critical),
                    588:                    buffer_len(&k->cert->critical), pw,
                    589:                    OPTIONS_CRITICAL|OPTIONS_EXTENSIONS, 1,
                    590:                    &cert_no_port_forwarding_flag,
                    591:                    &cert_no_agent_forwarding_flag,
                    592:                    &cert_no_x11_forwarding_flag,
                    593:                    &cert_no_pty_flag,
                    594:                    &cert_no_user_rc,
                    595:                    &cert_forced_command,
                    596:                    &cert_source_address_done) == -1)
                    597:                        return -1;
                    598:        } else {
                    599:                /* Separate options and extensions for v01 certs */
                    600:                if (parse_option_list(buffer_ptr(&k->cert->critical),
                    601:                    buffer_len(&k->cert->critical), pw,
                    602:                    OPTIONS_CRITICAL, 1, NULL, NULL, NULL, NULL, NULL,
                    603:                    &cert_forced_command,
                    604:                    &cert_source_address_done) == -1)
                    605:                        return -1;
                    606:                if (parse_option_list(buffer_ptr(&k->cert->extensions),
                    607:                    buffer_len(&k->cert->extensions), pw,
                    608:                    OPTIONS_EXTENSIONS, 1,
                    609:                    &cert_no_port_forwarding_flag,
                    610:                    &cert_no_agent_forwarding_flag,
                    611:                    &cert_no_x11_forwarding_flag,
                    612:                    &cert_no_pty_flag,
                    613:                    &cert_no_user_rc,
                    614:                    NULL, NULL) == -1)
                    615:                        return -1;
                    616:        }
                    617:
1.45      djm       618:        no_port_forwarding_flag |= cert_no_port_forwarding_flag;
                    619:        no_agent_forwarding_flag |= cert_no_agent_forwarding_flag;
                    620:        no_x11_forwarding_flag |= cert_no_x11_forwarding_flag;
                    621:        no_pty_flag |= cert_no_pty_flag;
                    622:        no_user_rc |= cert_no_user_rc;
                    623:        /* CA-specified forced command supersedes key option */
                    624:        if (cert_forced_command != NULL) {
                    625:                if (forced_command != NULL)
1.58      djm       626:                        free(forced_command);
1.45      djm       627:                forced_command = cert_forced_command;
                    628:        }
1.52      djm       629:        return 0;
1.45      djm       630: }
                    631: