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

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