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

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