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

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