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

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