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

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