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

Annotation of src/usr.bin/ssh/auth2.c, Revision 1.153

1.153   ! djm         1: /* $OpenBSD: auth2.c,v 1.152 2019/01/19 21:31:32 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  *
                     14:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     15:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     16:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     17:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     18:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     19:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     20:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     21:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     22:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     23:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     24:  */
1.14      deraadt    25:
1.111     stevesk    26:
                     27: #include <sys/types.h>
1.117     djm        28: #include <sys/stat.h>
                     29: #include <sys/uio.h>
1.111     stevesk    30:
1.117     djm        31: #include <fcntl.h>
1.143     djm        32: #include <limits.h>
1.111     stevesk    33: #include <pwd.h>
1.121     dtucker    34: #include <stdarg.h>
1.112     stevesk    35: #include <string.h>
1.117     djm        36: #include <unistd.h>
1.151     djm        37: #include <time.h>
1.1       markus     38:
1.117     djm        39: #include "atomicio.h"
1.113     deraadt    40: #include "xmalloc.h"
1.32      markus     41: #include "ssh2.h"
1.1       markus     42: #include "packet.h"
1.32      markus     43: #include "log.h"
1.148     markus     44: #include "sshbuf.h"
1.132     millert    45: #include "misc.h"
1.1       markus     46: #include "servconf.h"
                     47: #include "compat.h"
1.149     markus     48: #include "sshkey.h"
1.113     deraadt    49: #include "hostfile.h"
1.1       markus     50: #include "auth.h"
                     51: #include "dispatch.h"
1.29      markus     52: #include "pathnames.h"
1.100     markus     53: #ifdef GSSAPI
                     54: #include "ssh-gss.h"
                     55: #endif
1.113     deraadt    56: #include "monitor_wrap.h"
1.143     djm        57: #include "ssherr.h"
1.146     dtucker    58: #include "digest.h"
1.152     djm        59:
1.1       markus     60: /* import */
                     61: extern ServerOptions options;
1.23      markus     62: extern u_char *session_id2;
1.99      markus     63: extern u_int session_id2_len;
1.1       markus     64:
1.93      markus     65: /* methods */
                     66:
                     67: extern Authmethod method_none;
                     68: extern Authmethod method_pubkey;
                     69: extern Authmethod method_passwd;
                     70: extern Authmethod method_kbdint;
                     71: extern Authmethod method_hostbased;
1.100     markus     72: #ifdef GSSAPI
                     73: extern Authmethod method_gssapi;
                     74: #endif
1.93      markus     75:
                     76: Authmethod *authmethods[] = {
                     77:        &method_none,
                     78:        &method_pubkey,
1.100     markus     79: #ifdef GSSAPI
                     80:        &method_gssapi,
                     81: #endif
1.93      markus     82:        &method_passwd,
                     83:        &method_kbdint,
                     84:        &method_hostbased,
                     85:        NULL
1.18      markus     86: };
                     87:
1.1       markus     88: /* protocol */
                     89:
1.139     markus     90: static int input_service_request(int, u_int32_t, struct ssh *);
                     91: static int input_userauth_request(int, u_int32_t, struct ssh *);
1.1       markus     92:
                     93: /* helper */
1.125     djm        94: static Authmethod *authmethod_lookup(Authctxt *, const char *);
                     95: static char *authmethods_get(Authctxt *authctxt);
1.127     markus     96:
                     97: #define MATCH_NONE     0       /* method or submethod mismatch */
                     98: #define MATCH_METHOD   1       /* method matches (no submethod specified) */
                     99: #define MATCH_BOTH     2       /* method and submethod match */
                    100: #define MATCH_PARTIAL  3       /* method matches, submethod can't be checked */
                    101: static int list_starts_with(const char *, const char *, const char *);
1.1       markus    102:
1.117     djm       103: char *
                    104: auth2_read_banner(void)
                    105: {
                    106:        struct stat st;
                    107:        char *banner = NULL;
                    108:        size_t len, n;
                    109:        int fd;
                    110:
                    111:        if ((fd = open(options.banner, O_RDONLY)) == -1)
                    112:                return (NULL);
                    113:        if (fstat(fd, &st) == -1) {
                    114:                close(fd);
                    115:                return (NULL);
                    116:        }
1.124     djm       117:        if (st.st_size <= 0 || st.st_size > 1*1024*1024) {
1.117     djm       118:                close(fd);
                    119:                return (NULL);
                    120:        }
                    121:
                    122:        len = (size_t)st.st_size;               /* truncate */
                    123:        banner = xmalloc(len + 1);
                    124:        n = atomicio(read, fd, banner, len);
                    125:        close(fd);
                    126:
                    127:        if (n != len) {
1.128     djm       128:                free(banner);
1.117     djm       129:                return (NULL);
                    130:        }
                    131:        banner[n] = '\0';
                    132:
                    133:        return (banner);
                    134: }
                    135:
                    136: static void
1.153   ! djm       137: userauth_banner(struct ssh *ssh)
1.117     djm       138: {
                    139:        char *banner = NULL;
1.153   ! djm       140:        int r;
1.117     djm       141:
1.144     djm       142:        if (options.banner == NULL)
1.117     djm       143:                return;
                    144:
                    145:        if ((banner = PRIVSEP(auth2_read_banner())) == NULL)
                    146:                goto done;
                    147:
1.153   ! djm       148:        if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_BANNER)) != 0 ||
        !           149:            (r = sshpkt_put_cstring(ssh, banner)) != 0 ||
        !           150:            (r = sshpkt_put_cstring(ssh, "")) != 0 ||   /* language, unused */
        !           151:            (r = sshpkt_send(ssh)) != 0)
        !           152:                fatal("%s: %s", __func__, ssh_err(r));
1.117     djm       153:        debug("userauth_banner: sent");
                    154: done:
1.128     djm       155:        free(banner);
1.117     djm       156: }
                    157:
1.1       markus    158: /*
1.18      markus    159:  * loop until authctxt->success == TRUE
1.1       markus    160:  */
1.103     markus    161: void
1.153   ! djm       162: do_authentication2(struct ssh *ssh)
1.1       markus    163: {
1.153   ! djm       164:        Authctxt *authctxt = ssh->authctxt;
        !           165:
1.140     markus    166:        ssh_dispatch_init(ssh, &dispatch_protocol_error);
                    167:        ssh_dispatch_set(ssh, SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.142     markus    168:        ssh_dispatch_run_fatal(ssh, DISPATCH_BLOCK, &authctxt->success);
1.138     markus    169:        ssh->authctxt = NULL;
1.1       markus    170: }
                    171:
1.109     deraadt   172: /*ARGSUSED*/
1.135     markus    173: static int
1.139     markus    174: input_service_request(int type, u_int32_t seq, struct ssh *ssh)
1.1       markus    175: {
1.138     markus    176:        Authctxt *authctxt = ssh->authctxt;
1.153   ! djm       177:        char *service = NULL;
        !           178:        int r, acceptit = 0;
        !           179:
        !           180:        if ((r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
        !           181:            (r = sshpkt_get_end(ssh)) != 0)
        !           182:                goto out;
1.1       markus    183:
1.18      markus    184:        if (authctxt == NULL)
                    185:                fatal("input_service_request: no authctxt");
                    186:
1.1       markus    187:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    188:                if (!authctxt->success) {
1.94      deraadt   189:                        acceptit = 1;
1.1       markus    190:                        /* now we can handle user-auth requests */
1.153   ! djm       191:                        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
        !           192:                            &input_userauth_request);
1.1       markus    193:                }
                    194:        }
                    195:        /* XXX all other service requests are denied */
                    196:
1.94      deraadt   197:        if (acceptit) {
1.153   ! djm       198:                if ((r = sshpkt_start(ssh, SSH2_MSG_SERVICE_ACCEPT)) != 0 ||
        !           199:                    (r = sshpkt_put_cstring(ssh, service)) != 0 ||
        !           200:                    (r = sshpkt_send(ssh)) != 0 ||
        !           201:                    (r = ssh_packet_write_wait(ssh)) != 0)
        !           202:                        goto out;
1.1       markus    203:        } else {
                    204:                debug("bad service request %s", service);
1.153   ! djm       205:                ssh_packet_disconnect(ssh, "bad service request %s", service);
1.1       markus    206:        }
1.153   ! djm       207:        r = 0;
        !           208:  out:
1.128     djm       209:        free(service);
1.135     markus    210:        return 0;
1.1       markus    211: }
                    212:
1.146     dtucker   213: #define MIN_FAIL_DELAY_SECONDS 0.005
                    214: static double
                    215: user_specific_delay(const char *user)
                    216: {
                    217:        char b[512];
                    218:        size_t len = ssh_digest_bytes(SSH_DIGEST_SHA512);
                    219:        u_char *hash = xmalloc(len);
                    220:        double delay;
                    221:
1.147     dtucker   222:        (void)snprintf(b, sizeof b, "%llu%s",
                    223:             (unsigned long long)options.timing_secret, user);
1.146     dtucker   224:        if (ssh_digest_memory(SSH_DIGEST_SHA512, b, strlen(b), hash, len) != 0)
                    225:                fatal("%s: ssh_digest_memory", __func__);
                    226:        /* 0-4.2 ms of delay */
                    227:        delay = (double)PEEK_U32(hash) / 1000 / 1000 / 1000 / 1000;
                    228:        freezero(hash, len);
                    229:        debug3("%s: user specific delay %0.3lfms", __func__, delay/1000);
                    230:        return MIN_FAIL_DELAY_SECONDS + delay;
                    231: }
                    232:
                    233: static void
                    234: ensure_minimum_time_since(double start, double seconds)
                    235: {
                    236:        struct timespec ts;
                    237:        double elapsed = monotime_double() - start, req = seconds, remain;
                    238:
                    239:        /* if we've already passed the requested time, scale up */
                    240:        while ((remain = seconds - elapsed) < 0.0)
                    241:                seconds *= 2;
                    242:
                    243:        ts.tv_sec = remain;
                    244:        ts.tv_nsec = (remain - ts.tv_sec) * 1000000000;
                    245:        debug3("%s: elapsed %0.3lfms, delaying %0.3lfms (requested %0.3lfms)",
                    246:            __func__, elapsed*1000, remain*1000, req*1000);
                    247:        nanosleep(&ts, NULL);
                    248: }
                    249:
1.109     deraadt   250: /*ARGSUSED*/
1.135     markus    251: static int
1.139     markus    252: input_userauth_request(int type, u_int32_t seq, struct ssh *ssh)
1.1       markus    253: {
1.138     markus    254:        Authctxt *authctxt = ssh->authctxt;
1.18      markus    255:        Authmethod *m = NULL;
1.153   ! djm       256:        char *user = NULL, *service = NULL, *method = NULL, *style = NULL;
        !           257:        int r, authenticated = 0;
1.146     dtucker   258:        double tstart = monotime_double();
1.1       markus    259:
1.18      markus    260:        if (authctxt == NULL)
                    261:                fatal("input_userauth_request: no authctxt");
1.1       markus    262:
1.153   ! djm       263:        if ((r = sshpkt_get_cstring(ssh, &user, NULL)) != 0 ||
        !           264:            (r = sshpkt_get_cstring(ssh, &service, NULL)) != 0 ||
        !           265:            (r = sshpkt_get_cstring(ssh, &method, NULL)) != 0)
        !           266:                goto out;
1.1       markus    267:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    268:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    269:
1.28      markus    270:        if ((style = strchr(user, ':')) != NULL)
                    271:                *style++ = 0;
                    272:
1.36      stevesk   273:        if (authctxt->attempt++ == 0) {
1.18      markus    274:                /* setup auth context */
1.89      markus    275:                authctxt->pw = PRIVSEP(getpwnamallow(user));
                    276:                if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18      markus    277:                        authctxt->valid = 1;
1.137     djm       278:                        debug2("%s: setting up authctxt for %s",
                    279:                            __func__, user);
1.18      markus    280:                } else {
1.137     djm       281:                        /* Invalid user, fake password information */
1.102     markus    282:                        authctxt->pw = fakepw();
1.18      markus    283:                }
1.137     djm       284:                ssh_packet_set_log_preamble(ssh, "%suser %s",
                    285:                    authctxt->valid ? "authenticating " : "invalid ", user);
1.106     djm       286:                setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88      provos    287:                    use_privsep ? " [net]" : "");
1.18      markus    288:                authctxt->user = xstrdup(user);
                    289:                authctxt->service = xstrdup(service);
1.62      markus    290:                authctxt->style = style ? xstrdup(style) : NULL;
1.88      provos    291:                if (use_privsep)
                    292:                        mm_inform_authserv(service, style);
1.153   ! djm       293:                userauth_banner(ssh);
1.125     djm       294:                if (auth2_setup_methods_lists(authctxt) != 0)
1.153   ! djm       295:                        ssh_packet_disconnect(ssh,
        !           296:                            "no authentication methods enabled");
1.62      markus    297:        } else if (strcmp(user, authctxt->user) != 0 ||
                    298:            strcmp(service, authctxt->service) != 0) {
1.153   ! djm       299:                ssh_packet_disconnect(ssh, "Change of username or service "
        !           300:                    "not allowed: (%s,%s) -> (%s,%s)",
1.62      markus    301:                    authctxt->user, authctxt->service, user, service);
1.1       markus    302:        }
1.28      markus    303:        /* reset state */
1.140     markus    304:        auth2_challenge_stop(ssh);
1.100     markus    305:
                    306: #ifdef GSSAPI
1.120     djm       307:        /* XXX move to auth2_gssapi_stop() */
1.140     markus    308:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
                    309:        ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
1.100     markus    310: #endif
                    311:
1.143     djm       312:        auth2_authctxt_reset_info(authctxt);
1.28      markus    313:        authctxt->postponed = 0;
1.123     djm       314:        authctxt->server_caused_failure = 0;
1.18      markus    315:
1.28      markus    316:        /* try to authenticate user */
1.125     djm       317:        m = authmethod_lookup(authctxt, method);
1.117     djm       318:        if (m != NULL && authctxt->failures < options.max_authtries) {
1.18      markus    319:                debug2("input_userauth_request: try method %s", method);
1.140     markus    320:                authenticated = m->userauth(ssh);
1.18      markus    321:        }
1.146     dtucker   322:        if (!authctxt->authenticated)
                    323:                ensure_minimum_time_since(tstart,
                    324:                    user_specific_delay(authctxt->user));
1.140     markus    325:        userauth_finish(ssh, authenticated, method, NULL);
1.153   ! djm       326:        r = 0;
        !           327:  out:
1.128     djm       328:        free(service);
                    329:        free(user);
                    330:        free(method);
1.153   ! djm       331:        return r;
1.49      markus    332: }
                    333:
                    334: void
1.140     markus    335: userauth_finish(struct ssh *ssh, int authenticated, const char *method,
1.126     djm       336:     const char *submethod)
1.49      markus    337: {
1.140     markus    338:        Authctxt *authctxt = ssh->authctxt;
1.60      markus    339:        char *methods;
1.153   ! djm       340:        int r, partial = 0;
1.60      markus    341:
1.28      markus    342:        if (!authctxt->valid && authenticated)
                    343:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    344:                    authctxt->user);
1.126     djm       345:        if (authenticated && authctxt->postponed)
                    346:                fatal("INTERNAL ERROR: authenticated and postponed");
1.18      markus    347:
                    348:        /* Special handling for root */
1.96      markus    349:        if (authenticated && authctxt->pw->pw_uid == 0 &&
1.145     djm       350:            !auth_root_allowed(ssh, method))
1.3       markus    351:                authenticated = 0;
                    352:
1.125     djm       353:        if (authenticated && options.num_auth_methods != 0) {
1.127     markus    354:                if (!auth2_update_methods_lists(authctxt, method, submethod)) {
1.125     djm       355:                        authenticated = 0;
                    356:                        partial = 1;
                    357:                }
                    358:        }
1.126     djm       359:
                    360:        /* Log before sending the reply */
1.129     djm       361:        auth_log(authctxt, authenticated, partial, method, submethod);
1.126     djm       362:
1.143     djm       363:        /* Update information exposed to session */
                    364:        if (authenticated || partial)
                    365:                auth2_update_session_info(authctxt, method, submethod);
                    366:
1.126     djm       367:        if (authctxt->postponed)
                    368:                return;
1.125     djm       369:
1.60      markus    370:        if (authenticated == 1) {
                    371:                /* turn off userauth */
1.153   ! djm       372:                ssh_dispatch_set(ssh, SSH2_MSG_USERAUTH_REQUEST,
        !           373:                    &dispatch_protocol_ignore);
        !           374:                if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_SUCCESS)) != 0 ||
        !           375:                    (r = sshpkt_send(ssh)) != 0 ||
        !           376:                    (r = ssh_packet_write_wait(ssh)) != 0)
        !           377:                        fatal("%s: %s", __func__, ssh_err(r));
1.60      markus    378:                /* now we can break out */
                    379:                authctxt->success = 1;
1.137     djm       380:                ssh_packet_set_log_preamble(ssh, "user %s", authctxt->user);
1.60      markus    381:        } else {
1.119     djm       382:                /* Allow initial try of "none" auth without failure penalty */
1.133     djm       383:                if (!partial && !authctxt->server_caused_failure &&
1.123     djm       384:                    (authctxt->attempt > 1 || strcmp(method, "none") != 0))
1.119     djm       385:                        authctxt->failures++;
                    386:                if (authctxt->failures >= options.max_authtries)
1.131     djm       387:                        auth_maxtries_exceeded(authctxt);
1.125     djm       388:                methods = authmethods_get(authctxt);
                    389:                debug3("%s: failure partial=%d next methods=\"%s\"", __func__,
                    390:                    partial, methods);
1.153   ! djm       391:                if ((r = sshpkt_start(ssh, SSH2_MSG_USERAUTH_FAILURE)) != 0 ||
        !           392:                    (r = sshpkt_put_cstring(ssh, methods)) != 0 ||
        !           393:                    (r = sshpkt_put_u8(ssh, partial)) != 0 ||
        !           394:                    (r = sshpkt_send(ssh)) != 0 ||
        !           395:                    (r = ssh_packet_write_wait(ssh)) != 0)
        !           396:                        fatal("%s: %s", __func__, ssh_err(r));
1.128     djm       397:                free(methods);
1.60      markus    398:        }
1.1       markus    399: }
1.18      markus    400:
1.125     djm       401: /*
                    402:  * Checks whether method is allowed by at least one AuthenticationMethods
                    403:  * methods list. Returns 1 if allowed, or no methods lists configured.
                    404:  * 0 otherwise.
                    405:  */
1.127     markus    406: int
                    407: auth2_method_allowed(Authctxt *authctxt, const char *method,
                    408:     const char *submethod)
1.125     djm       409: {
                    410:        u_int i;
                    411:
                    412:        /*
                    413:         * NB. authctxt->num_auth_methods might be zero as a result of
                    414:         * auth2_setup_methods_lists(), so check the configuration.
                    415:         */
                    416:        if (options.num_auth_methods == 0)
                    417:                return 1;
                    418:        for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127     markus    419:                if (list_starts_with(authctxt->auth_methods[i], method,
                    420:                    submethod) != MATCH_NONE)
1.125     djm       421:                        return 1;
                    422:        }
                    423:        return 0;
                    424: }
                    425:
1.67      stevesk   426: static char *
1.125     djm       427: authmethods_get(Authctxt *authctxt)
1.1       markus    428: {
1.148     markus    429:        struct sshbuf *b;
1.18      markus    430:        char *list;
1.148     markus    431:        int i, r;
1.1       markus    432:
1.148     markus    433:        if ((b = sshbuf_new()) == NULL)
                    434:                fatal("%s: sshbuf_new failed", __func__);
1.93      markus    435:        for (i = 0; authmethods[i] != NULL; i++) {
                    436:                if (strcmp(authmethods[i]->name, "none") == 0)
1.18      markus    437:                        continue;
1.125     djm       438:                if (authmethods[i]->enabled == NULL ||
                    439:                    *(authmethods[i]->enabled) == 0)
                    440:                        continue;
1.127     markus    441:                if (!auth2_method_allowed(authctxt, authmethods[i]->name,
                    442:                    NULL))
1.125     djm       443:                        continue;
1.148     markus    444:                if ((r = sshbuf_putf(b, "%s%s", sshbuf_len(b) ? "," : "",
                    445:                    authmethods[i]->name)) != 0)
                    446:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    447:        }
1.148     markus    448:        if ((list = sshbuf_dup_string(b)) == NULL)
1.136     djm       449:                fatal("%s: sshbuf_dup_string failed", __func__);
1.148     markus    450:        sshbuf_free(b);
1.18      markus    451:        return list;
                    452: }
                    453:
1.66      itojun    454: static Authmethod *
1.125     djm       455: authmethod_lookup(Authctxt *authctxt, const char *name)
1.18      markus    456: {
1.93      markus    457:        int i;
                    458:
1.18      markus    459:        if (name != NULL)
1.93      markus    460:                for (i = 0; authmethods[i] != NULL; i++)
                    461:                        if (authmethods[i]->enabled != NULL &&
                    462:                            *(authmethods[i]->enabled) != 0 &&
1.125     djm       463:                            strcmp(name, authmethods[i]->name) == 0 &&
1.127     markus    464:                            auth2_method_allowed(authctxt,
                    465:                            authmethods[i]->name, NULL))
1.93      markus    466:                                return authmethods[i];
                    467:        debug2("Unrecognized authentication method name: %s",
                    468:            name ? name : "NULL");
1.18      markus    469:        return NULL;
1.1       markus    470: }
1.125     djm       471:
                    472: /*
                    473:  * Check a comma-separated list of methods for validity. Is need_enable is
                    474:  * non-zero, then also require that the methods are enabled.
                    475:  * Returns 0 on success or -1 if the methods list is invalid.
                    476:  */
                    477: int
                    478: auth2_methods_valid(const char *_methods, int need_enable)
                    479: {
1.127     markus    480:        char *methods, *omethods, *method, *p;
1.125     djm       481:        u_int i, found;
                    482:        int ret = -1;
                    483:
                    484:        if (*_methods == '\0') {
                    485:                error("empty authentication method list");
                    486:                return -1;
                    487:        }
                    488:        omethods = methods = xstrdup(_methods);
                    489:        while ((method = strsep(&methods, ",")) != NULL) {
                    490:                for (found = i = 0; !found && authmethods[i] != NULL; i++) {
1.127     markus    491:                        if ((p = strchr(method, ':')) != NULL)
                    492:                                *p = '\0';
1.125     djm       493:                        if (strcmp(method, authmethods[i]->name) != 0)
                    494:                                continue;
                    495:                        if (need_enable) {
                    496:                                if (authmethods[i]->enabled == NULL ||
                    497:                                    *(authmethods[i]->enabled) == 0) {
                    498:                                        error("Disabled method \"%s\" in "
                    499:                                            "AuthenticationMethods list \"%s\"",
                    500:                                            method, _methods);
                    501:                                        goto out;
                    502:                                }
                    503:                        }
                    504:                        found = 1;
                    505:                        break;
                    506:                }
                    507:                if (!found) {
                    508:                        error("Unknown authentication method \"%s\" in list",
                    509:                            method);
                    510:                        goto out;
                    511:                }
                    512:        }
                    513:        ret = 0;
                    514:  out:
                    515:        free(omethods);
                    516:        return ret;
                    517: }
                    518:
                    519: /*
                    520:  * Prune the AuthenticationMethods supplied in the configuration, removing
                    521:  * any methods lists that include disabled methods. Note that this might
                    522:  * leave authctxt->num_auth_methods == 0, even when multiple required auth
                    523:  * has been requested. For this reason, all tests for whether multiple is
                    524:  * enabled should consult options.num_auth_methods directly.
                    525:  */
                    526: int
                    527: auth2_setup_methods_lists(Authctxt *authctxt)
                    528: {
                    529:        u_int i;
                    530:
                    531:        if (options.num_auth_methods == 0)
                    532:                return 0;
                    533:        debug3("%s: checking methods", __func__);
                    534:        authctxt->auth_methods = xcalloc(options.num_auth_methods,
                    535:            sizeof(*authctxt->auth_methods));
                    536:        authctxt->num_auth_methods = 0;
                    537:        for (i = 0; i < options.num_auth_methods; i++) {
                    538:                if (auth2_methods_valid(options.auth_methods[i], 1) != 0) {
                    539:                        logit("Authentication methods list \"%s\" contains "
                    540:                            "disabled method, skipping",
                    541:                            options.auth_methods[i]);
                    542:                        continue;
                    543:                }
                    544:                debug("authentication methods list %d: %s",
                    545:                    authctxt->num_auth_methods, options.auth_methods[i]);
                    546:                authctxt->auth_methods[authctxt->num_auth_methods++] =
                    547:                    xstrdup(options.auth_methods[i]);
                    548:        }
                    549:        if (authctxt->num_auth_methods == 0) {
                    550:                error("No AuthenticationMethods left after eliminating "
                    551:                    "disabled methods");
                    552:                return -1;
                    553:        }
                    554:        return 0;
                    555: }
                    556:
                    557: static int
1.127     markus    558: list_starts_with(const char *methods, const char *method,
                    559:     const char *submethod)
1.125     djm       560: {
                    561:        size_t l = strlen(method);
1.127     markus    562:        int match;
                    563:        const char *p;
1.125     djm       564:
                    565:        if (strncmp(methods, method, l) != 0)
1.127     markus    566:                return MATCH_NONE;
                    567:        p = methods + l;
                    568:        match = MATCH_METHOD;
                    569:        if (*p == ':') {
                    570:                if (!submethod)
                    571:                        return MATCH_PARTIAL;
                    572:                l = strlen(submethod);
                    573:                p += 1;
                    574:                if (strncmp(submethod, p, l))
                    575:                        return MATCH_NONE;
                    576:                p += l;
                    577:                match = MATCH_BOTH;
                    578:        }
                    579:        if (*p != ',' && *p != '\0')
                    580:                return MATCH_NONE;
                    581:        return match;
1.125     djm       582: }
                    583:
                    584: /*
                    585:  * Remove method from the start of a comma-separated list of methods.
                    586:  * Returns 0 if the list of methods did not start with that method or 1
                    587:  * if it did.
                    588:  */
                    589: static int
1.127     markus    590: remove_method(char **methods, const char *method, const char *submethod)
1.125     djm       591: {
1.127     markus    592:        char *omethods = *methods, *p;
1.125     djm       593:        size_t l = strlen(method);
1.127     markus    594:        int match;
1.125     djm       595:
1.127     markus    596:        match = list_starts_with(omethods, method, submethod);
                    597:        if (match != MATCH_METHOD && match != MATCH_BOTH)
1.125     djm       598:                return 0;
1.127     markus    599:        p = omethods + l;
                    600:        if (submethod && match == MATCH_BOTH)
                    601:                p += 1 + strlen(submethod); /* include colon */
                    602:        if (*p == ',')
                    603:                p++;
                    604:        *methods = xstrdup(p);
1.125     djm       605:        free(omethods);
                    606:        return 1;
                    607: }
                    608:
                    609: /*
                    610:  * Called after successful authentication. Will remove the successful method
                    611:  * from the start of each list in which it occurs. If it was the last method
                    612:  * in any list, then authentication is deemed successful.
                    613:  * Returns 1 if the method completed any authentication list or 0 otherwise.
                    614:  */
                    615: int
1.127     markus    616: auth2_update_methods_lists(Authctxt *authctxt, const char *method,
                    617:     const char *submethod)
1.125     djm       618: {
                    619:        u_int i, found = 0;
                    620:
                    621:        debug3("%s: updating methods list after \"%s\"", __func__, method);
                    622:        for (i = 0; i < authctxt->num_auth_methods; i++) {
1.127     markus    623:                if (!remove_method(&(authctxt->auth_methods[i]), method,
                    624:                    submethod))
1.125     djm       625:                        continue;
                    626:                found = 1;
                    627:                if (*authctxt->auth_methods[i] == '\0') {
                    628:                        debug2("authentication methods list %d complete", i);
                    629:                        return 1;
                    630:                }
                    631:                debug3("authentication methods list %d remaining: \"%s\"",
                    632:                    i, authctxt->auth_methods[i]);
                    633:        }
                    634:        /* This should not happen, but would be bad if it did */
                    635:        if (!found)
                    636:                fatal("%s: method not in AuthenticationMethods", __func__);
                    637:        return 0;
                    638: }
                    639:
1.143     djm       640: /* Reset method-specific information */
                    641: void auth2_authctxt_reset_info(Authctxt *authctxt)
                    642: {
                    643:        sshkey_free(authctxt->auth_method_key);
                    644:        free(authctxt->auth_method_info);
                    645:        authctxt->auth_method_key = NULL;
                    646:        authctxt->auth_method_info = NULL;
                    647: }
                    648:
                    649: /* Record auth method-specific information for logs */
                    650: void
                    651: auth2_record_info(Authctxt *authctxt, const char *fmt, ...)
                    652: {
                    653:        va_list ap;
                    654:         int i;
                    655:
                    656:        free(authctxt->auth_method_info);
                    657:        authctxt->auth_method_info = NULL;
                    658:
                    659:        va_start(ap, fmt);
                    660:        i = vasprintf(&authctxt->auth_method_info, fmt, ap);
                    661:        va_end(ap);
                    662:
                    663:        if (i < 0 || authctxt->auth_method_info == NULL)
                    664:                fatal("%s: vasprintf failed", __func__);
                    665: }
                    666:
                    667: /*
                    668:  * Records a public key used in authentication. This is used for logging
                    669:  * and to ensure that the same key is not subsequently accepted again for
                    670:  * multiple authentication.
                    671:  */
                    672: void
                    673: auth2_record_key(Authctxt *authctxt, int authenticated,
                    674:     const struct sshkey *key)
                    675: {
                    676:        struct sshkey **tmp, *dup;
                    677:        int r;
                    678:
1.150     djm       679:        if ((r = sshkey_from_private(key, &dup)) != 0)
1.143     djm       680:                fatal("%s: copy key: %s", __func__, ssh_err(r));
                    681:        sshkey_free(authctxt->auth_method_key);
                    682:        authctxt->auth_method_key = dup;
                    683:
                    684:        if (!authenticated)
                    685:                return;
                    686:
                    687:        /* If authenticated, make sure we don't accept this key again */
1.150     djm       688:        if ((r = sshkey_from_private(key, &dup)) != 0)
1.143     djm       689:                fatal("%s: copy key: %s", __func__, ssh_err(r));
                    690:        if (authctxt->nprev_keys >= INT_MAX ||
                    691:            (tmp = recallocarray(authctxt->prev_keys, authctxt->nprev_keys,
                    692:            authctxt->nprev_keys + 1, sizeof(*authctxt->prev_keys))) == NULL)
                    693:                fatal("%s: reallocarray failed", __func__);
                    694:        authctxt->prev_keys = tmp;
                    695:        authctxt->prev_keys[authctxt->nprev_keys] = dup;
                    696:        authctxt->nprev_keys++;
                    697:
                    698: }
                    699:
                    700: /* Checks whether a key has already been previously used for authentication */
                    701: int
                    702: auth2_key_already_used(Authctxt *authctxt, const struct sshkey *key)
                    703: {
                    704:        u_int i;
                    705:        char *fp;
                    706:
                    707:        for (i = 0; i < authctxt->nprev_keys; i++) {
                    708:                if (sshkey_equal_public(key, authctxt->prev_keys[i])) {
                    709:                        fp = sshkey_fingerprint(authctxt->prev_keys[i],
                    710:                            options.fingerprint_hash, SSH_FP_DEFAULT);
                    711:                        debug3("%s: key already used: %s %s", __func__,
                    712:                            sshkey_type(authctxt->prev_keys[i]),
                    713:                            fp == NULL ? "UNKNOWN" : fp);
                    714:                        free(fp);
                    715:                        return 1;
                    716:                }
                    717:        }
                    718:        return 0;
                    719: }
                    720:
                    721: /*
                    722:  * Updates authctxt->session_info with details of authentication. Should be
                    723:  * whenever an authentication method succeeds.
                    724:  */
                    725: void
                    726: auth2_update_session_info(Authctxt *authctxt, const char *method,
                    727:     const char *submethod)
                    728: {
                    729:        int r;
                    730:
                    731:        if (authctxt->session_info == NULL) {
                    732:                if ((authctxt->session_info = sshbuf_new()) == NULL)
                    733:                        fatal("%s: sshbuf_new", __func__);
                    734:        }
                    735:
                    736:        /* Append method[/submethod] */
                    737:        if ((r = sshbuf_putf(authctxt->session_info, "%s%s%s",
                    738:            method, submethod == NULL ? "" : "/",
                    739:            submethod == NULL ? "" : submethod)) != 0)
                    740:                fatal("%s: append method: %s", __func__, ssh_err(r));
                    741:
                    742:        /* Append key if present */
                    743:        if (authctxt->auth_method_key != NULL) {
                    744:                if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
                    745:                    (r = sshkey_format_text(authctxt->auth_method_key,
                    746:                    authctxt->session_info)) != 0)
                    747:                        fatal("%s: append key: %s", __func__, ssh_err(r));
                    748:        }
                    749:
                    750:        if (authctxt->auth_method_info != NULL) {
                    751:                /* Ensure no ambiguity here */
                    752:                if (strchr(authctxt->auth_method_info, '\n') != NULL)
                    753:                        fatal("%s: auth_method_info contains \\n", __func__);
                    754:                if ((r = sshbuf_put_u8(authctxt->session_info, ' ')) != 0 ||
                    755:                    (r = sshbuf_putf(authctxt->session_info, "%s",
                    756:                    authctxt->auth_method_info)) != 0) {
                    757:                        fatal("%s: append method info: %s",
                    758:                            __func__, ssh_err(r));
                    759:                }
                    760:        }
                    761:        if ((r = sshbuf_put_u8(authctxt->session_info, '\n')) != 0)
                    762:                fatal("%s: append: %s", __func__, ssh_err(r));
                    763: }
1.117     djm       764: