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

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