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

1.114   ! dtucker     1: /* $OpenBSD: auth2.c,v 1.113 2006/08/03 03:34:41 deraadt 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>
                     28:
                     29: #include <pwd.h>
1.112     stevesk    30: #include <string.h>
1.113     deraadt    31: #include <stdarg.h>
1.1       markus     32:
1.113     deraadt    33: #include "xmalloc.h"
1.32      markus     34: #include "ssh2.h"
1.1       markus     35: #include "packet.h"
1.32      markus     36: #include "log.h"
1.113     deraadt    37: #include "buffer.h"
1.1       markus     38: #include "servconf.h"
                     39: #include "compat.h"
1.113     deraadt    40: #include "key.h"
                     41: #include "hostfile.h"
1.1       markus     42: #include "auth.h"
                     43: #include "dispatch.h"
1.29      markus     44: #include "pathnames.h"
1.100     markus     45: #ifdef GSSAPI
                     46: #include "ssh-gss.h"
                     47: #endif
1.113     deraadt    48: #include "monitor_wrap.h"
1.100     markus     49:
1.1       markus     50: /* import */
                     51: extern ServerOptions options;
1.23      markus     52: extern u_char *session_id2;
1.99      markus     53: extern u_int session_id2_len;
1.1       markus     54:
1.93      markus     55: /* methods */
                     56:
                     57: extern Authmethod method_none;
                     58: extern Authmethod method_pubkey;
                     59: extern Authmethod method_passwd;
                     60: extern Authmethod method_kbdint;
                     61: extern Authmethod method_hostbased;
1.100     markus     62: #ifdef GSSAPI
                     63: extern Authmethod method_gssapi;
                     64: #endif
1.93      markus     65:
                     66: Authmethod *authmethods[] = {
                     67:        &method_none,
                     68:        &method_pubkey,
1.100     markus     69: #ifdef GSSAPI
                     70:        &method_gssapi,
                     71: #endif
1.93      markus     72:        &method_passwd,
                     73:        &method_kbdint,
                     74:        &method_hostbased,
                     75:        NULL
1.18      markus     76: };
                     77:
1.1       markus     78: /* protocol */
                     79:
1.80      markus     80: static void input_service_request(int, u_int32_t, void *);
                     81: static void input_userauth_request(int, u_int32_t, void *);
1.1       markus     82:
                     83: /* helper */
1.66      itojun     84: static Authmethod *authmethod_lookup(const char *);
1.67      stevesk    85: static char *authmethods_get(void);
1.88      provos     86: int user_key_allowed(struct passwd *, Key *);
1.1       markus     87:
                     88: /*
1.18      markus     89:  * loop until authctxt->success == TRUE
1.1       markus     90:  */
                     91:
1.103     markus     92: void
                     93: do_authentication2(Authctxt *authctxt)
1.1       markus     94: {
1.81      markus     95:        dispatch_init(&dispatch_protocol_error);
1.1       markus     96:        dispatch_set(SSH2_MSG_SERVICE_REQUEST, &input_service_request);
1.18      markus     97:        dispatch_run(DISPATCH_BLOCK, &authctxt->success, authctxt);
1.1       markus     98: }
                     99:
1.109     deraadt   100: /*ARGSUSED*/
1.66      itojun    101: static void
1.80      markus    102: input_service_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    103: {
1.18      markus    104:        Authctxt *authctxt = ctxt;
1.23      markus    105:        u_int len;
1.94      deraadt   106:        int acceptit = 0;
1.1       markus    107:        char *service = packet_get_string(&len);
1.79      markus    108:        packet_check_eom();
1.1       markus    109:
1.18      markus    110:        if (authctxt == NULL)
                    111:                fatal("input_service_request: no authctxt");
                    112:
1.1       markus    113:        if (strcmp(service, "ssh-userauth") == 0) {
1.18      markus    114:                if (!authctxt->success) {
1.94      deraadt   115:                        acceptit = 1;
1.1       markus    116:                        /* now we can handle user-auth requests */
                    117:                        dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &input_userauth_request);
                    118:                }
                    119:        }
                    120:        /* XXX all other service requests are denied */
                    121:
1.94      deraadt   122:        if (acceptit) {
1.1       markus    123:                packet_start(SSH2_MSG_SERVICE_ACCEPT);
                    124:                packet_put_cstring(service);
                    125:                packet_send();
                    126:                packet_write_wait();
                    127:        } else {
                    128:                debug("bad service request %s", service);
                    129:                packet_disconnect("bad service request %s", service);
                    130:        }
                    131:        xfree(service);
                    132: }
                    133:
1.109     deraadt   134: /*ARGSUSED*/
1.66      itojun    135: static void
1.80      markus    136: input_userauth_request(int type, u_int32_t seq, void *ctxt)
1.1       markus    137: {
1.18      markus    138:        Authctxt *authctxt = ctxt;
                    139:        Authmethod *m = NULL;
1.28      markus    140:        char *user, *service, *method, *style = NULL;
1.1       markus    141:        int authenticated = 0;
                    142:
1.18      markus    143:        if (authctxt == NULL)
                    144:                fatal("input_userauth_request: no authctxt");
1.1       markus    145:
1.18      markus    146:        user = packet_get_string(NULL);
                    147:        service = packet_get_string(NULL);
                    148:        method = packet_get_string(NULL);
1.1       markus    149:        debug("userauth-request for user %s service %s method %s", user, service, method);
1.24      markus    150:        debug("attempt %d failures %d", authctxt->attempt, authctxt->failures);
1.1       markus    151:
1.28      markus    152:        if ((style = strchr(user, ':')) != NULL)
                    153:                *style++ = 0;
                    154:
1.36      stevesk   155:        if (authctxt->attempt++ == 0) {
1.18      markus    156:                /* setup auth context */
1.89      markus    157:                authctxt->pw = PRIVSEP(getpwnamallow(user));
                    158:                if (authctxt->pw && strcmp(service, "ssh-connection")==0) {
1.18      markus    159:                        authctxt->valid = 1;
                    160:                        debug2("input_userauth_request: setting up authctxt for %s", user);
                    161:                } else {
1.107     markus    162:                        logit("input_userauth_request: invalid user %s", user);
1.102     markus    163:                        authctxt->pw = fakepw();
1.18      markus    164:                }
1.106     djm       165:                setproctitle("%s%s", authctxt->valid ? user : "unknown",
1.88      provos    166:                    use_privsep ? " [net]" : "");
1.18      markus    167:                authctxt->user = xstrdup(user);
                    168:                authctxt->service = xstrdup(service);
1.62      markus    169:                authctxt->style = style ? xstrdup(style) : NULL;
1.88      provos    170:                if (use_privsep)
                    171:                        mm_inform_authserv(service, style);
1.62      markus    172:        } else if (strcmp(user, authctxt->user) != 0 ||
                    173:            strcmp(service, authctxt->service) != 0) {
                    174:                packet_disconnect("Change of username or service not allowed: "
                    175:                    "(%s,%s) -> (%s,%s)",
                    176:                    authctxt->user, authctxt->service, user, service);
1.1       markus    177:        }
1.28      markus    178:        /* reset state */
1.75      markus    179:        auth2_challenge_stop(authctxt);
1.100     markus    180:
                    181: #ifdef GSSAPI
                    182:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
                    183:        dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
                    184: #endif
                    185:
1.28      markus    186:        authctxt->postponed = 0;
1.18      markus    187:
1.28      markus    188:        /* try to authenticate user */
1.18      markus    189:        m = authmethod_lookup(method);
                    190:        if (m != NULL) {
                    191:                debug2("input_userauth_request: try method %s", method);
                    192:                authenticated = m->userauth(authctxt);
                    193:        }
1.49      markus    194:        userauth_finish(authctxt, authenticated, method);
                    195:
                    196:        xfree(service);
                    197:        xfree(user);
                    198:        xfree(method);
                    199: }
                    200:
                    201: void
                    202: userauth_finish(Authctxt *authctxt, int authenticated, char *method)
                    203: {
1.60      markus    204:        char *methods;
                    205:
1.28      markus    206:        if (!authctxt->valid && authenticated)
                    207:                fatal("INTERNAL ERROR: authenticated invalid user %s",
                    208:                    authctxt->user);
1.18      markus    209:
                    210:        /* Special handling for root */
1.96      markus    211:        if (authenticated && authctxt->pw->pw_uid == 0 &&
1.41      markus    212:            !auth_root_allowed(method))
1.3       markus    213:                authenticated = 0;
                    214:
1.18      markus    215:        /* Log before sending the reply */
1.28      markus    216:        auth_log(authctxt, authenticated, method, " ssh2");
                    217:
1.60      markus    218:        if (authctxt->postponed)
                    219:                return;
                    220:
                    221:        /* XXX todo: check if multiple auth methods are needed */
                    222:        if (authenticated == 1) {
                    223:                /* turn off userauth */
1.81      markus    224:                dispatch_set(SSH2_MSG_USERAUTH_REQUEST, &dispatch_protocol_ignore);
1.60      markus    225:                packet_start(SSH2_MSG_USERAUTH_SUCCESS);
                    226:                packet_send();
                    227:                packet_write_wait();
                    228:                /* now we can break out */
                    229:                authctxt->success = 1;
                    230:        } else {
1.105     dtucker   231:                if (authctxt->failures++ > options.max_authtries)
1.60      markus    232:                        packet_disconnect(AUTH_FAIL_MSG, authctxt->user);
                    233:                methods = authmethods_get();
                    234:                packet_start(SSH2_MSG_USERAUTH_FAILURE);
                    235:                packet_put_cstring(methods);
                    236:                packet_put_char(0);     /* XXX partial success, unused */
                    237:                packet_send();
                    238:                packet_write_wait();
                    239:                xfree(methods);
                    240:        }
1.1       markus    241: }
                    242:
1.18      markus    243: #define        DELIM   ","
                    244:
1.67      stevesk   245: static char *
1.18      markus    246: authmethods_get(void)
1.1       markus    247: {
1.82      markus    248:        Buffer b;
1.18      markus    249:        char *list;
1.93      markus    250:        int i;
1.1       markus    251:
1.82      markus    252:        buffer_init(&b);
1.93      markus    253:        for (i = 0; authmethods[i] != NULL; i++) {
                    254:                if (strcmp(authmethods[i]->name, "none") == 0)
1.18      markus    255:                        continue;
1.93      markus    256:                if (authmethods[i]->enabled != NULL &&
                    257:                    *(authmethods[i]->enabled) != 0) {
1.82      markus    258:                        if (buffer_len(&b) > 0)
                    259:                                buffer_append(&b, ",", 1);
1.93      markus    260:                        buffer_append(&b, authmethods[i]->name,
                    261:                            strlen(authmethods[i]->name));
1.1       markus    262:                }
                    263:        }
1.82      markus    264:        buffer_append(&b, "\0", 1);
                    265:        list = xstrdup(buffer_ptr(&b));
                    266:        buffer_free(&b);
1.18      markus    267:        return list;
                    268: }
                    269:
1.66      itojun    270: static Authmethod *
1.18      markus    271: authmethod_lookup(const char *name)
                    272: {
1.93      markus    273:        int i;
                    274:
1.18      markus    275:        if (name != NULL)
1.93      markus    276:                for (i = 0; authmethods[i] != NULL; i++)
                    277:                        if (authmethods[i]->enabled != NULL &&
                    278:                            *(authmethods[i]->enabled) != 0 &&
                    279:                            strcmp(name, authmethods[i]->name) == 0)
                    280:                                return authmethods[i];
                    281:        debug2("Unrecognized authentication method name: %s",
                    282:            name ? name : "NULL");
1.18      markus    283:        return NULL;
1.1       markus    284: }