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

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