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