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

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