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

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