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

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
1.14      deraadt    24:
1.1       markus     25: #include "includes.h"
1.92    ! markus     26: RCSID("$OpenBSD: auth2.c,v 1.91 2002/05/13 02:37:39 itojun Exp $");
1.1       markus     27:
1.32      markus     28: #include "ssh2.h"
1.1       markus     29: #include "xmalloc.h"
                     30: #include "packet.h"
1.32      markus     31: #include "log.h"
1.1       markus     32: #include "servconf.h"
                     33: #include "compat.h"
                     34: #include "auth.h"
                     35: #include "dispatch.h"
1.29      markus     36: #include "pathnames.h"
1.88      provos     37: #include "monitor_wrap.h"
1.1       markus     38:
                     39: /* import */
                     40: extern ServerOptions options;
1.23      markus     41: extern u_char *session_id2;
1.1       markus     42: extern int session_id2_len;
                     43:
1.88      provos     44: Authctxt *x_authctxt = NULL;
1.18      markus     45: static int one = 1;
                     46:
                     47: typedef struct Authmethod Authmethod;
                     48: struct Authmethod {
                     49:        char    *name;
                     50:        int     (*userauth)(Authctxt *authctxt);
                     51:        int     *enabled;
                     52: };
                     53:
1.1       markus     54: /* protocol */
                     55:
1.80      markus     56: static void input_service_request(int, u_int32_t, void *);
                     57: static void input_userauth_request(int, u_int32_t, void *);
1.1       markus     58:
                     59: /* helper */
1.66      itojun     60: static Authmethod *authmethod_lookup(const char *);
1.67      stevesk    61: static char *authmethods_get(void);
1.88      provos     62: int user_key_allowed(struct passwd *, Key *);
                     63: int hostbased_key_allowed(struct passwd *, const char *, char *, Key *);
1.1       markus     64:
1.18      markus     65: /* auth */
                     66:
                     67: Authmethod authmethods[] = {
                     68:        {"none",
                     69:                userauth_none,
                     70:                &one},
                     71:        {"publickey",
                     72:                userauth_pubkey,
1.21      markus     73:                &options.pubkey_authentication},
1.40      markus     74:        {"password",
                     75:                userauth_passwd,
                     76:                &options.password_authentication},
1.18      markus     77:        {"keyboard-interactive",
                     78:                userauth_kbdint,
                     79:                &options.kbd_interactive_authentication},
1.52      markus     80:        {"hostbased",
                     81:                userauth_hostbased,
                     82:                &options.hostbased_authentication},
1.18      markus     83:        {NULL, NULL, NULL}
1.1       markus     84: };
                     85:
                     86: /*
1.18      markus     87:  * loop until authctxt->success == TRUE
1.1       markus     88:  */
                     89:
1.87      provos     90: Authctxt *
1.74      itojun     91: do_authentication2(void)
1.1       markus     92: {
1.28      markus     93:        Authctxt *authctxt = authctxt_new();
                     94:
1.18      markus     95:        x_authctxt = authctxt;          /*XXX*/
                     96:
1.71      markus     97:        /* challenge-response is implemented via keyboard interactive */
1.57      markus     98:        if (options.challenge_response_authentication)
1.34      markus     99:                options.kbd_interactive_authentication = 1;
                    100:
1.81      markus    101:        dispatch_init(&dispatch_protocol_error);
1.1       markus    102:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus    103:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.87      provos    104:
                    105:        return (authctxt);
1.1       markus    106: }
                    107:
1.66      itojun    108: static void
1.80      markus    109: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    110: {
1.18      markus    111:        Authctxt *authctxt = ctxt;
1.23      markus    112:        u_int len;
1.1       markus    113:        int accept = 0;
                    114:        char *service = packet_get_string(&len);
1.79      markus    115:        packet_check_eom();
1.1       markus    116:
1.18      markus    117:        if (authctxt == NULL)
                    118:                fatal("input_service_request: no authctxt");
                    119:
1.1       markus    120:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    121:                if (!authctxt->success) {
1.1       markus    122:                        accept = 1;
                    123:                        /* now we can handle user-auth requests */
                    124:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    125:                }
                    126:        }
                    127:        /* XXX all other service requests are denied */
                    128:
                    129:        if (accept) {
                    130:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    131:                packet_put_cstring(service);
                    132:                packet_send();
                    133:                packet_write_wait();
                    134:        } else {
                    135:                debug("bad service request %s", service);
                    136:                packet_disconnect("bad service request %s", service);
                    137:        }
                    138:        xfree(service);
                    139: }
                    140:
1.66      itojun    141: static void
1.80      markus    142: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    143: {
1.18      markus    144:        Authctxt *authctxt = ctxt;
                    145:        Authmethod *m = NULL;
1.28      markus    146:        char *user, *service, *method, *style = NULL;
1.1       markus    147:        int authenticated = 0;
                    148:
1.18      markus    149:        if (authctxt == NULL)
                    150:                fatal("input_userauth_request: no authctxt");
1.1       markus    151:
1.18      markus    152:        user = packet_get_string(NULL);
                    153:        service = packet_get_string(NULL);
                    154:        method = packet_get_string(NULL);
1.1       markus    155:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    156:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    157:
1.28      markus    158:        if ((style = strchr(user, ':')) != NULL)
                    159:                *style++ = 0;
                    160:
1.36      stevesk   161:        if (authctxt->attempt++ == 0) {
1.18      markus    162:                /* setup auth context */
1.89      markus    163:                authctxt->pw = PRIVSEP(getpwnamallow(user));
                    164:                if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18      markus    165:                        authctxt->valid = 1;
                    166:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    167:                } else {
                    168:                        log("input_userauth_request: illegal user %s", user);
                    169:                }
1.89      markus    170:                setproctitle("%s%s", authctxt->pw ? user : "unknown",
1.88      provos    171:                    use_privsep ? " [net]" : "");
1.18      markus    172:                authctxt->user = xstrdup(user);
                    173:                authctxt->service = xstrdup(service);
1.62      markus    174:                authctxt->style = style ? xstrdup(style) : NULL;
1.88      provos    175:                if (use_privsep)
                    176:                        mm_inform_authserv(service, style);
1.62      markus    177:        } else if (strcmp(user, authctxt->user) != 0 ||
                    178:            strcmp(service, authctxt->service) != 0) {
                    179:                packet_disconnect("Change of username or service not allowed: "
                    180:                    "(%s,%s) -> (%s,%s)",
                    181:                    authctxt->user, authctxt->service, user, service);
1.1       markus    182:        }
1.28      markus    183:        /* reset state */
1.75      markus    184:        auth2_challenge_stop(authctxt);
1.28      markus    185:        authctxt->postponed = 0;
1.18      markus    186:
1.28      markus    187:        /* try to authenticate user */
1.18      markus    188:        m = authmethod_lookup(method);
                    189:        if (m != NULL) {
                    190:                debug2("input_userauth_request: try method %s", method);
                    191:                authenticated = m->userauth(authctxt);
                    192:        }
1.49      markus    193:        userauth_finish(authctxt, authenticated, method);
                    194:
                    195:        xfree(service);
                    196:        xfree(user);
                    197:        xfree(method);
                    198: }
                    199:
                    200: void
                    201: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                    202: {
1.60      markus    203:        char *methods;
                    204:
1.28      markus    205:        if (!authctxt->valid && authenticated)
                    206:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    207:                    authctxt->user);
1.18      markus    208:
                    209:        /* Special handling for root */
1.41      markus    210:        if (authenticated && authctxt->pw->pw_uid == 0 &&
                    211:            !auth_root_allowed(method))
1.3       markus    212:                authenticated = 0;
                    213:
1.18      markus    214:        /* Log before sending the reply */
1.28      markus    215:        auth_log(authctxt, authenticated, method, " ssh2");
                    216:
1.60      markus    217:        if (authctxt->postponed)
                    218:                return;
                    219:
                    220:        /* XXX todo: check if multiple auth methods are needed */
                    221:        if (authenticated == 1) {
                    222:                /* turn off userauth */
1.81      markus    223:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60      markus    224:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    225:                packet_send();
                    226:                packet_write_wait();
                    227:                /* now we can break out */
                    228:                authctxt->success = 1;
                    229:        } else {
                    230:                if (authctxt->failures++ > AUTH_FAIL_MAX)
                    231:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
                    232:                methods = authmethods_get();
                    233:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
                    234:                packet_put_cstring(methods);
                    235:                packet_put_char(0);     /* XXX partial success, unused */
                    236:                packet_send();
                    237:                packet_write_wait();
                    238:                xfree(methods);
                    239:        }
1.18      markus    240: }
                    241:
                    242: /* get current user */
1.1       markus    243:
                    244: struct passwd*
                    245: auth_get_user(void)
                    246: {
1.18      markus    247:        return (x_authctxt != NULL && x_authctxt->valid) ? x_authctxt->pw : NULL;
1.1       markus    248: }
                    249:
1.18      markus    250: #define        DELIM   ","
                    251:
1.67      stevesk   252: static char *
1.18      markus    253: authmethods_get(void)
1.1       markus    254: {
1.18      markus    255:        Authmethod *method = NULL;
1.82      markus    256:        Buffer b;
1.18      markus    257:        char *list;
1.1       markus    258:
1.82      markus    259:        buffer_init(&b);
1.18      markus    260:        for (method = authmethods; method->name != NULL; method++) {
                    261:                if (strcmp(method->name, "none") == 0)
                    262:                        continue;
                    263:                if (method->enabled != NULL && *(method->enabled) != 0) {
1.82      markus    264:                        if (buffer_len(&b) > 0)
                    265:                                buffer_append(&b, ",", 1);
                    266:                        buffer_append(&b, method->name, strlen(method->name));
1.1       markus    267:                }
                    268:        }
1.82      markus    269:        buffer_append(&b, "\0", 1);
                    270:        list = xstrdup(buffer_ptr(&b));
                    271:        buffer_free(&b);
1.18      markus    272:        return list;
                    273: }
                    274:
1.66      itojun    275: static Authmethod *
1.18      markus    276: authmethod_lookup(const char *name)
                    277: {
                    278:        Authmethod *method = NULL;
                    279:        if (name != NULL)
                    280:                for (method = authmethods; method->name != NULL; method++)
                    281:                        if (method->enabled != NULL &&
                    282:                            *(method->enabled) != 0 &&
                    283:                            strcmp(name, method->name) == 0)
                    284:                                return method;
                    285:        debug2("Unrecognized authentication method name: %s", name ? name : "NULL");
                    286:        return NULL;
1.1       markus    287: }