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

1.1       markus      1: /*
1.2       markus      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.17    ! markus     26: RCSID("$OpenBSD: auth.c,v 1.16 2001/02/04 15:32:22 stevesk Exp $");
1.1       markus     27:
                     28: #include "xmalloc.h"
1.13      markus     29: #include "match.h"
1.14      markus     30: #include "groupaccess.h"
                     31: #include "log.h"
1.1       markus     32: #include "servconf.h"
1.2       markus     33: #include "auth.h"
1.13      markus     34: #include "auth-options.h"
1.14      markus     35: #include "canohost.h"
1.2       markus     36:
1.1       markus     37: /* import */
                     38: extern ServerOptions options;
                     39:
                     40: /*
1.12      markus     41:  * Check if the user is allowed to log in via ssh. If user is listed
                     42:  * in DenyUsers or one of user's groups is listed in DenyGroups, false
                     43:  * will be returned. If AllowUsers isn't empty and user isn't listed
                     44:  * there, or if AllowGroups isn't empty and one of user's groups isn't
                     45:  * listed there, false will be returned.
1.1       markus     46:  * If the user's shell is not executable, false will be returned.
1.4       markus     47:  * Otherwise true is returned.
1.1       markus     48:  */
1.5       markus     49: int
1.1       markus     50: allowed_user(struct passwd * pw)
                     51: {
                     52:        struct stat st;
1.7       deraadt    53:        char *shell;
1.1       markus     54:        int i;
                     55:
                     56:        /* Shouldn't be called if pw is NULL, but better safe than sorry... */
1.12      markus     57:        if (!pw || !pw->pw_name)
1.1       markus     58:                return 0;
                     59:
1.7       deraadt    60:        /*
                     61:         * Get the shell from the password data.  An empty shell field is
                     62:         * legal, and means /bin/sh.
                     63:         */
                     64:        shell = (pw->pw_shell[0] == '\0') ? _PATH_BSHELL : pw->pw_shell;
                     65:
1.1       markus     66:        /* deny if shell does not exists or is not executable */
1.7       deraadt    67:        if (stat(shell, &st) != 0)
1.1       markus     68:                return 0;
                     69:        if (!((st.st_mode & S_IFREG) && (st.st_mode & (S_IXOTH|S_IXUSR|S_IXGRP))))
                     70:                return 0;
                     71:
                     72:        /* Return false if user is listed in DenyUsers */
                     73:        if (options.num_deny_users > 0) {
                     74:                for (i = 0; i < options.num_deny_users; i++)
                     75:                        if (match_pattern(pw->pw_name, options.deny_users[i]))
                     76:                                return 0;
                     77:        }
                     78:        /* Return false if AllowUsers isn't empty and user isn't listed there */
                     79:        if (options.num_allow_users > 0) {
                     80:                for (i = 0; i < options.num_allow_users; i++)
                     81:                        if (match_pattern(pw->pw_name, options.allow_users[i]))
                     82:                                break;
                     83:                /* i < options.num_allow_users iff we break for loop */
                     84:                if (i >= options.num_allow_users)
                     85:                        return 0;
                     86:        }
                     87:        if (options.num_deny_groups > 0 || options.num_allow_groups > 0) {
1.12      markus     88:                /* Get the user's group access list (primary and supplementary) */
                     89:                if (ga_init(pw->pw_name, pw->pw_gid) == 0)
1.1       markus     90:                        return 0;
                     91:
1.12      markus     92:                /* Return false if one of user's groups is listed in DenyGroups */
                     93:                if (options.num_deny_groups > 0)
                     94:                        if (ga_match(options.deny_groups,
                     95:                            options.num_deny_groups)) {
                     96:                                ga_free();
1.1       markus     97:                                return 0;
1.12      markus     98:                        }
1.1       markus     99:                /*
1.12      markus    100:                 * Return false if AllowGroups isn't empty and one of user's groups
1.1       markus    101:                 * isn't listed there
                    102:                 */
1.12      markus    103:                if (options.num_allow_groups > 0)
                    104:                        if (!ga_match(options.allow_groups,
                    105:                            options.num_allow_groups)) {
                    106:                                ga_free();
1.1       markus    107:                                return 0;
1.12      markus    108:                        }
                    109:                ga_free();
1.1       markus    110:        }
                    111:        /* We found no reason not to let this user try to log on... */
                    112:        return 1;
1.13      markus    113: }
                    114:
                    115: Authctxt *
                    116: authctxt_new(void)
                    117: {
1.16      stevesk   118:        Authctxt *authctxt = xmalloc(sizeof(*authctxt));
                    119:        memset(authctxt, 0, sizeof(*authctxt));
                    120:        return authctxt;
1.13      markus    121: }
                    122:
                    123: struct passwd *
                    124: pwcopy(struct passwd *pw)
                    125: {
                    126:        struct passwd *copy = xmalloc(sizeof(*copy));
                    127:        memset(copy, 0, sizeof(*copy));
                    128:        copy->pw_name = xstrdup(pw->pw_name);
                    129:        copy->pw_passwd = xstrdup(pw->pw_passwd);
                    130:        copy->pw_uid = pw->pw_uid;
                    131:        copy->pw_gid = pw->pw_gid;
                    132:        copy->pw_class = xstrdup(pw->pw_class);
                    133:        copy->pw_dir = xstrdup(pw->pw_dir);
                    134:        copy->pw_shell = xstrdup(pw->pw_shell);
                    135:        return copy;
                    136: }
                    137:
                    138: void
                    139: auth_log(Authctxt *authctxt, int authenticated, char *method, char *info)
                    140: {
                    141:        void (*authlog) (const char *fmt,...) = verbose;
                    142:        char *authmsg;
                    143:
                    144:        /* Raise logging level */
                    145:        if (authenticated == 1 ||
                    146:            !authctxt->valid ||
                    147:            authctxt->failures >= AUTH_FAIL_LOG ||
                    148:            strcmp(method, "password") == 0)
                    149:                authlog = log;
                    150:
                    151:        if (authctxt->postponed)
                    152:                authmsg = "Postponed";
                    153:        else
                    154:                authmsg = authenticated ? "Accepted" : "Failed";
                    155:
                    156:        authlog("%s %s for %s%.100s from %.200s port %d%s",
                    157:            authmsg,
                    158:            method,
                    159:            authctxt->valid ? "" : "illegal user ",
                    160:            authctxt->valid && authctxt->pw->pw_uid == 0 ? "ROOT" : authctxt->user,
                    161:            get_remote_ipaddr(),
                    162:            get_remote_port(),
                    163:            info);
                    164: }
                    165:
                    166: /*
1.17    ! markus    167:  * Check whether root logins are disallowed.
1.13      markus    168:  */
                    169: int
1.17    ! markus    170: auth_root_allowed(char *method)
1.13      markus    171: {
1.17    ! markus    172:        switch (options.permit_root_login) {
        !           173:        case PERMIT_YES:
1.13      markus    174:                return 1;
1.17    ! markus    175:                break;
        !           176:        case PERMIT_NO_PASSWD:
        !           177:                if (strcmp(method, "password") != 0)
        !           178:                        return 1;
        !           179:                break;
        !           180:        case PERMIT_FORCED_ONLY:
        !           181:                if (forced_command) {
        !           182:                        log("Root login accepted for forced command.");
        !           183:                        return 1;
        !           184:                }
        !           185:                break;
1.13      markus    186:        }
1.17    ! markus    187:        log("ROOT LOGIN REFUSED FROM %.200s", get_remote_ipaddr());
        !           188:        return 0;
1.1       markus    189: }