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

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.29    ! markus     26: RCSID("$OpenBSD: auth.c,v 1.28 2001/10/03 10:01:20 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.26      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.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.26      markus     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.26      markus     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.26      markus     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 ",
1.29    ! markus    159:            authctxt->user,
1.13      markus    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.22      markus    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: }
1.24      markus    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:
1.22      markus    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
1.23      markus    300:  * of the file or root and no directories must be group or world writable.
1.22      markus    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
1.25      provos    310: secure_filename(FILE *f, const char *file, struct passwd *pw,
                    311:     char *err, size_t errlen)
1.22      markus    312: {
1.25      provos    313:        uid_t uid = pw->pw_uid;
1.28      markus    314:        char buf[MAXPATHLEN], homedir[MAXPATHLEN];
1.22      markus    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:        }
1.28      markus    323:        if (realpath(pw->pw_dir, homedir) == NULL) {
                    324:                snprintf(err, errlen, "realpath %s failed: %s", pw->pw_dir,
                    325:                    strerror(errno));
                    326:                return -1;
                    327:        }
1.22      markus    328:
                    329:        /* check the open file to avoid races */
                    330:        if (fstat(fileno(f), &st) < 0 ||
                    331:            (st.st_uid != 0 && st.st_uid != uid) ||
                    332:            (st.st_mode & 022) != 0) {
                    333:                snprintf(err, errlen, "bad ownership or modes for file %s",
                    334:                    buf);
                    335:                return -1;
                    336:        }
                    337:
                    338:        /* for each component of the canonical path, walking upwards */
                    339:        for (;;) {
                    340:                if ((cp = dirname(buf)) == NULL) {
                    341:                        snprintf(err, errlen, "dirname() failed");
                    342:                        return -1;
                    343:                }
                    344:                strlcpy(buf, cp, sizeof(buf));
1.25      provos    345:
1.22      markus    346:                debug3("secure_filename: checking '%s'", buf);
                    347:                if (stat(buf, &st) < 0 ||
                    348:                    (st.st_uid != 0 && st.st_uid != uid) ||
                    349:                    (st.st_mode & 022) != 0) {
                    350:                        snprintf(err, errlen,
                    351:                            "bad ownership or modes for directory %s", buf);
                    352:                        return -1;
                    353:                }
                    354:
1.27      markus    355:                /* If are passed the homedir then we can stop */
1.28      markus    356:                if (strcmp(homedir, buf) == 0) {
1.27      markus    357:                        debug3("secure_filename: terminating check at '%s'",
                    358:                            buf);
                    359:                        break;
                    360:                }
1.22      markus    361:                /*
                    362:                 * dirname should always complete with a "/" path,
                    363:                 * but we can be paranoid and check for "." too
                    364:                 */
                    365:                if ((strcmp("/", buf) == 0) || (strcmp(".", buf) == 0))
                    366:                        break;
                    367:        }
1.17      markus    368:        return 0;
1.1       markus    369: }