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

Annotation of src/usr.bin/ssh/auth.c, Revision 1.60

1.1       markus      1: /*
1.19      deraadt     2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.9       deraadt     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.
1.1       markus     23:  */
                     24:
                     25: #include "includes.h"
1.60    ! djm        26: RCSID("$OpenBSD: auth.c,v 1.59 2005/06/06 11:20:36 djm Exp $");
1.22      markus     27:
                     28: #include <libgen.h>
1.1       markus     29:
                     30: #include "xmalloc.h"
1.13      markus     31: #include "match.h"
1.14      markus     32: #include "groupaccess.h"
                     33: #include "log.h"
1.1       markus     34: #include "servconf.h"
1.2       markus     35: #include "auth.h"
1.13      markus     36: #include "auth-options.h"
1.14      markus     37: #include "canohost.h"
1.22      markus     38: #include "buffer.h"
                     39: #include "bufaux.h"
1.24      markus     40: #include "uidswap.h"
1.40      markus     41: #include "misc.h"
1.42      markus     42: #include "bufaux.h"
                     43: #include "packet.h"
1.2       markus     44:
1.1       markus     45: /* import */
                     46: extern ServerOptions options;
                     47:
1.42      markus     48: /* Debugging messages */
                     49: Buffer auth_debug;
                     50: int auth_debug_init;
                     51:
1.1       markus     52: /*
1.12      markus     53:  * Check if the user is allowed to log in via ssh. If user is listed
                     54:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     55:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     56:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     57:  * listed there, false will be returned.
1.1       markus     58:  * If the user's shell is not executable, false will be returned.
1.4       markus     59:  * Otherwise true is returned.
1.1       markus     60:  */
1.5       markus     61: int
1.1       markus     62: allowed_user(struct passwd * pw)
                     63: {
                     64:        struct stat st;
1.35      markus     65:        const char *hostname = NULL, *ipaddr = NULL;
1.21      markus     66:        char *shell;
1.60    ! djm        67:        u_int i;
1.1       markus     68:
                     69:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     70:        if (!pw || !pw->pw_name)
1.1       markus     71:                return 0;
                     72:
1.7       deraadt    73:        /*
                     74:         * Get the shell from the password data.  An empty shell field is
                     75:         * legal, and means /bin/sh.
                     76:         */
                     77:        shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
                     78:
1.1       markus     79:        /* deny if shell does not exists or is not executable */
1.34      stevesk    80:        if (stat(shell, &st) != 0) {
1.47      itojun     81:                logit("User %.100s not allowed because shell %.100s does not exist",
1.34      stevesk    82:                    pw->pw_name, shell);
1.1       markus     83:                return 0;
1.34      stevesk    84:        }
1.36      itojun     85:        if (S_ISREG(st.st_mode) == 0 ||
                     86:            (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP)) == 0) {
1.47      itojun     87:                logit("User %.100s not allowed because shell %.100s is not executable",
1.34      stevesk    88:                    pw->pw_name, shell);
1.1       markus     89:                return 0;
1.34      stevesk    90:        }
1.1       markus     91:
1.58      dtucker    92:        if (options.num_deny_users > 0 || options.num_allow_users > 0 ||
                     93:            options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.48      markus     94:                hostname = get_canonical_hostname(options.use_dns);
1.35      markus     95:                ipaddr = get_remote_ipaddr();
                     96:        }
                     97:
1.1       markus     98:        /* Return false if user is listed in DenyUsers */
                     99:        if (options.num_deny_users > 0) {
                    100:                for (i = 0; i < options.num_deny_users; i++)
1.39      markus    101:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.34      stevesk   102:                            options.deny_users[i])) {
1.57      dtucker   103:                                logit("User %.100s from %.100s not allowed "
                    104:                                    "because listed in DenyUsers",
                    105:                                    pw->pw_name, hostname);
1.1       markus    106:                                return 0;
1.34      stevesk   107:                        }
1.1       markus    108:        }
                    109:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                    110:        if (options.num_allow_users > 0) {
                    111:                for (i = 0; i < options.num_allow_users; i++)
1.39      markus    112:                        if (match_user(pw->pw_name, hostname, ipaddr,
1.26      markus    113:                            options.allow_users[i]))
1.1       markus    114:                                break;
                    115:                /* i < options.num_allow_users iff we break for loop */
1.34      stevesk   116:                if (i >= options.num_allow_users) {
1.57      dtucker   117:                        logit("User %.100s from %.100s not allowed because "
                    118:                            "not listed in AllowUsers", pw->pw_name, hostname);
1.1       markus    119:                        return 0;
1.34      stevesk   120:                }
1.1       markus    121:        }
                    122:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus    123:                /* Get the user's group access list (primary and supplementary) */
1.34      stevesk   124:                if (ga_init(pw->pw_name, pw->pw_gid) == 0) {
1.57      dtucker   125:                        logit("User %.100s from %.100s not allowed because "
                    126:                            "not in any group", pw->pw_name, hostname);
1.1       markus    127:                        return 0;
1.34      stevesk   128:                }
1.1       markus    129:
1.12      markus    130:                /* Return false if one of user's groups is listed in DenyGroups */
                    131:                if (options.num_deny_groups > 0)
                    132:                        if (ga_match(options.deny_groups,
                    133:                            options.num_deny_groups)) {
                    134:                                ga_free();
1.57      dtucker   135:                                logit("User %.100s from %.100s not allowed "
                    136:                                    "because a group is listed in DenyGroups",
                    137:                                    pw->pw_name, hostname);
1.1       markus    138:                                return 0;
1.12      markus    139:                        }
1.1       markus    140:                /*
1.12      markus    141:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    142:                 * isn't listed there
                    143:                 */
1.12      markus    144:                if (options.num_allow_groups > 0)
                    145:                        if (!ga_match(options.allow_groups,
                    146:                            options.num_allow_groups)) {
                    147:                                ga_free();
1.57      dtucker   148:                                logit("User %.100s from %.100s not allowed "
                    149:                                    "because none of user's groups are listed "
                    150:                                    "in AllowGroups", pw->pw_name, hostname);
1.1       markus    151:                                return 0;
1.12      markus    152:                        }
                    153:                ga_free();
1.1       markus    154:        }
                    155:        /* We found no reason not to let this user try to log on... */
                    156:        return 1;
1.13      markus    157: }
                    158:
                    159: void
                    160: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
                    161: {
                    162:        void (*authlog) (const char *fmt,...) = verbose;
                    163:        char *authmsg;
                    164:
                    165:        /* Raise logging level */
                    166:        if (authenticated == 1 ||
                    167:            !authctxt->valid ||
1.54      dtucker   168:            authctxt->failures >= options.max_authtries / 2 ||
1.13      markus    169:            strcmp(method, "password") == 0)
1.47      itojun    170:                authlog = logit;
1.13      markus    171:
                    172:        if (authctxt->postponed)
                    173:                authmsg = "Postponed";
                    174:        else
                    175:                authmsg = authenticated ? "Accepted" : "Failed";
                    176:
                    177:        authlog("%s %s for %s%.100s from %.200s port %d%s",
                    178:            authmsg,
                    179:            method,
1.56      markus    180:            authctxt->valid ? "" : "invalid user ",
1.29      markus    181:            authctxt->user,
1.13      markus    182:            get_remote_ipaddr(),
                    183:            get_remote_port(),
                    184:            info);
                    185: }
                    186:
                    187: /*
1.17      markus    188:  * Check whether root logins are disallowed.
1.13      markus    189:  */
                    190: int
1.17      markus    191: auth_root_allowed(char *method)
1.13      markus    192: {
1.17      markus    193:        switch (options.permit_root_login) {
                    194:        case PERMIT_YES:
1.13      markus    195:                return 1;
1.17      markus    196:                break;
                    197:        case PERMIT_NO_PASSWD:
                    198:                if (strcmp(method, "password") != 0)
                    199:                        return 1;
                    200:                break;
                    201:        case PERMIT_FORCED_ONLY:
                    202:                if (forced_command) {
1.47      itojun    203:                        logit("Root login accepted for forced command.");
1.17      markus    204:                        return 1;
                    205:                }
                    206:                break;
1.13      markus    207:        }
1.47      itojun    208:        logit("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
1.22      markus    209:        return 0;
                    210: }
                    211:
                    212:
                    213: /*
                    214:  * Given a template and a passwd structure, build a filename
                    215:  * by substituting % tokenised options. Currently, %% becomes '%',
                    216:  * %h becomes the home directory and %u the username.
                    217:  *
                    218:  * This returns a buffer allocated by xmalloc.
                    219:  */
1.59      djm       220: static char *
                    221: expand_authorized_keys(const char *filename, struct passwd *pw)
1.22      markus    222: {
1.59      djm       223:        char *file, *ret;
1.22      markus    224:
1.59      djm       225:        file = percent_expand(filename, "h", pw->pw_dir,
                    226:            "u", pw->pw_name, (char *)NULL);
1.22      markus    227:
                    228:        /*
                    229:         * Ensure that filename starts anchored. If not, be backward
                    230:         * compatible and prepend the '%h/'
                    231:         */
1.59      djm       232:        if (*file == '/')
                    233:                return (file);
                    234:
                    235:        ret = xmalloc(MAXPATHLEN);
                    236:        if (strlcpy(ret, pw->pw_dir, MAXPATHLEN) >= MAXPATHLEN ||
                    237:            strlcat(ret, "/", MAXPATHLEN) >= MAXPATHLEN ||
                    238:            strlcat(ret, file, MAXPATHLEN) >= MAXPATHLEN)
                    239:                fatal("expand_authorized_keys: path too long");
1.22      markus    240:
1.59      djm       241:        xfree(file);
                    242:        return (ret);
1.22      markus    243: }
                    244:
                    245: char *
                    246: authorized_keys_file(struct passwd *pw)
                    247: {
1.59      djm       248:        return expand_authorized_keys(options.authorized_keys_file, pw);
1.22      markus    249: }
                    250:
                    251: char *
                    252: authorized_keys_file2(struct passwd *pw)
                    253: {
1.59      djm       254:        return expand_authorized_keys(options.authorized_keys_file2, pw);
1.22      markus    255: }
1.24      markus    256:
                    257: /* return ok if key exists in sysfile or userfile */
                    258: HostStatus
                    259: check_key_in_hostfiles(struct passwd *pw, Key *key, const char *host,
                    260:     const char *sysfile, const char *userfile)
                    261: {
                    262:        Key *found;
                    263:        char *user_hostfile;
                    264:        struct stat st;
1.30      stevesk   265:        HostStatus host_status;
1.24      markus    266:
                    267:        /* Check if we know the host and its host key. */
                    268:        found = key_new(key->type);
                    269:        host_status = check_host_in_hostfile(sysfile, host, key, found, NULL);
                    270:
                    271:        if (host_status != HOST_OK && userfile != NULL) {
                    272:                user_hostfile = tilde_expand_filename(userfile, pw->pw_uid);
                    273:                if (options.strict_modes &&
                    274:                    (stat(user_hostfile, &st) == 0) &&
                    275:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.31      deraadt   276:                    (st.st_mode & 022) != 0)) {
1.47      itojun    277:                        logit("Authentication refused for %.100s: "
1.24      markus    278:                            "bad owner or modes for %.200s",
                    279:                            pw->pw_name, user_hostfile);
                    280:                } else {
                    281:                        temporarily_use_uid(pw);
                    282:                        host_status = check_host_in_hostfile(user_hostfile,
                    283:                            host, key, found, NULL);
                    284:                        restore_uid();
                    285:                }
                    286:                xfree(user_hostfile);
                    287:        }
                    288:        key_free(found);
                    289:
                    290:        debug2("check_key_in_hostfiles: key %s for %s", host_status == HOST_OK ?
                    291:            "ok" : "not found", host);
                    292:        return host_status;
                    293: }
                    294:
1.22      markus    295:
                    296: /*
                    297:  * Check a given file for security. This is defined as all components
1.44      stevesk   298:  * of the path to the file must be owned by either the owner of
1.23      markus    299:  * of the file or root and no directories must be group or world writable.
1.22      markus    300:  *
                    301:  * XXX Should any specific check be done for sym links ?
                    302:  *
                    303:  * Takes an open file descriptor, the file name, a uid and and
                    304:  * error buffer plus max size as arguments.
                    305:  *
                    306:  * Returns 0 on success and -1 on failure
                    307:  */
                    308: int
1.25      provos    309: secure_filename(FILE *f, const char *file, struct passwd *pw,
                    310:     char *err, size_t errlen)
1.22      markus    311: {
1.25      provos    312:        uid_t uid = pw->pw_uid;
1.28      markus    313:        char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22      markus    314:        char *cp;
1.46      markus    315:        int comparehome = 0;
1.22      markus    316:        struct stat st;
                    317:
                    318:        if (realpath(file, buf) == NULL) {
                    319:                snprintf(err, errlen, "realpath %s failed: %s", file,
                    320:                    strerror(errno));
                    321:                return -1;
                    322:        }
1.46      markus    323:        if (realpath(pw->pw_dir, homedir) != NULL)
                    324:                comparehome = 1;
1.22      markus    325:
                    326:        /* check the open file to avoid races */
                    327:        if (fstat(fileno(f), &st) < 0 ||
                    328:            (st.st_uid != 0 && st.st_uid != uid) ||
                    329:            (st.st_mode & 022) != 0) {
                    330:                snprintf(err, errlen, "bad ownership or modes for file %s",
                    331:                    buf);
                    332:                return -1;
                    333:        }
                    334:
                    335:        /* for each component of the canonical path, walking upwards */
                    336:        for (;;) {
                    337:                if ((cp = dirname(buf)) == NULL) {
                    338:                        snprintf(err, errlen, "dirname() failed");
                    339:                        return -1;
                    340:                }
                    341:                strlcpy(buf, cp, sizeof(buf));
1.25      provos    342:
1.22      markus    343:                debug3("secure_filename: checking '%s'", buf);
                    344:                if (stat(buf, &st) < 0 ||
                    345:                    (st.st_uid != 0 && st.st_uid != uid) ||
                    346:                    (st.st_mode & 022) != 0) {
1.31      deraadt   347:                        snprintf(err, errlen,
1.22      markus    348:                            "bad ownership or modes for directory %s", buf);
                    349:                        return -1;
                    350:                }
                    351:
1.27      markus    352:                /* If are passed the homedir then we can stop */
1.46      markus    353:                if (comparehome && strcmp(homedir, buf) == 0) {
1.27      markus    354:                        debug3("secure_filename: terminating check at '%s'",
                    355:                            buf);
                    356:                        break;
                    357:                }
1.22      markus    358:                /*
                    359:                 * dirname should always complete with a "/" path,
                    360:                 * but we can be paranoid and check for "." too
                    361:                 */
                    362:                if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
                    363:                        break;
                    364:        }
1.17      markus    365:        return 0;
1.37      provos    366: }
                    367:
                    368: struct passwd *
                    369: getpwnamallow(const char *user)
                    370: {
1.38      provos    371: #ifdef HAVE_LOGIN_CAP
                    372:        extern login_cap_t *lc;
                    373: #ifdef BSD_AUTH
                    374:        auth_session_t *as;
                    375: #endif
                    376: #endif
1.37      provos    377:        struct passwd *pw;
                    378:
                    379:        pw = getpwnam(user);
1.45      stevesk   380:        if (pw == NULL) {
1.55      markus    381:                logit("Invalid user %.100s from %.100s",
1.45      stevesk   382:                    user, get_remote_ipaddr());
                    383:                return (NULL);
                    384:        }
                    385:        if (!allowed_user(pw))
1.38      provos    386:                return (NULL);
                    387: #ifdef HAVE_LOGIN_CAP
                    388:        if ((lc = login_getclass(pw->pw_class)) == NULL) {
                    389:                debug("unable to get login class: %s", user);
                    390:                return (NULL);
                    391:        }
                    392: #ifdef BSD_AUTH
                    393:        if ((as = auth_open()) == NULL || auth_setpwd(as, pw) != 0 ||
1.43      millert   394:            auth_approval(as, lc, pw->pw_name, "ssh") <= 0) {
1.38      provos    395:                debug("Approval failure for %s", user);
1.37      provos    396:                pw = NULL;
1.38      provos    397:        }
                    398:        if (as != NULL)
                    399:                auth_close(as);
                    400: #endif
                    401: #endif
1.41      markus    402:        if (pw != NULL)
                    403:                return (pwcopy(pw));
                    404:        return (NULL);
1.42      markus    405: }
                    406:
                    407: void
                    408: auth_debug_add(const char *fmt,...)
                    409: {
                    410:        char buf[1024];
                    411:        va_list args;
                    412:
                    413:        if (!auth_debug_init)
                    414:                return;
                    415:
                    416:        va_start(args, fmt);
                    417:        vsnprintf(buf, sizeof(buf), fmt, args);
                    418:        va_end(args);
                    419:        buffer_put_cstring(&auth_debug, buf);
                    420: }
                    421:
                    422: void
                    423: auth_debug_send(void)
                    424: {
                    425:        char *msg;
                    426:
                    427:        if (!auth_debug_init)
                    428:                return;
                    429:        while (buffer_len(&auth_debug)) {
                    430:                msg = buffer_get_string(&auth_debug, NULL);
                    431:                packet_send_debug("%s", msg);
                    432:                xfree(msg);
                    433:        }
                    434: }
                    435:
                    436: void
                    437: auth_debug_reset(void)
                    438: {
                    439:        if (auth_debug_init)
                    440:                buffer_clear(&auth_debug);
                    441:        else {
                    442:                buffer_init(&auth_debug);
                    443:                auth_debug_init = 1;
                    444:        }
1.49      markus    445: }
                    446:
                    447: struct passwd *
                    448: fakepw(void)
                    449: {
                    450:        static struct passwd fake;
                    451:
                    452:        memset(&fake, 0, sizeof(fake));
                    453:        fake.pw_name = "NOUSER";
                    454:        fake.pw_passwd =
1.51      djm       455:            "$2a$06$r3.juUaHZDlIbQaO2dS9FuYxL1W9M81R1Tc92PoSNmzvpEqLkLGrK";
1.49      markus    456:        fake.pw_gecos = "NOUSER";
1.53      deraadt   457:        fake.pw_uid = (uid_t)-1;
                    458:        fake.pw_gid = (gid_t)-1;
1.49      markus    459:        fake.pw_class = "";
                    460:        fake.pw_dir = "/nonexist";
                    461:        fake.pw_shell = "/nonexist";
                    462:
                    463:        return (&fake);
1.1       markus    464: }