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

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