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

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