[BACK]Return to auth-rhosts.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth-rhosts.c, Revision 1.35

1.1       deraadt     1: /*
1.10      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Rhosts authentication.  This file contains code to check whether to admit
                      6:  * the login based on rhosts authentication.  This file also processes
                      7:  * /etc/hosts.equiv.
1.13      markus      8:  *
1.15      deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
1.10      deraadt    14:  */
1.1       deraadt    15:
                     16: #include "includes.h"
1.35    ! stevesk    17: RCSID("$OpenBSD: auth-rhosts.c,v 1.34 2006/02/07 01:08:04 stevesk Exp $");
        !            18:
        !            19: #include <sys/types.h>
        !            20: #include <sys/stat.h>
1.34      stevesk    21:
                     22: #include <netgroup.h>
1.1       deraadt    23:
                     24: #include "packet.h"
                     25: #include "uidswap.h"
1.19      markus     26: #include "pathnames.h"
                     27: #include "log.h"
1.6       markus     28: #include "servconf.h"
1.19      markus     29: #include "canohost.h"
1.21      itojun     30: #include "auth.h"
1.1       deraadt    31:
1.23      markus     32: /* import */
                     33: extern ServerOptions options;
1.28      markus     34: extern int use_privsep;
1.23      markus     35:
1.11      markus     36: /*
                     37:  * This function processes an rhosts-style file (.rhosts, .shosts, or
                     38:  * /etc/hosts.equiv).  This returns true if authentication can be granted
                     39:  * based on the file, and returns zero otherwise.
                     40:  */
1.1       deraadt    41:
1.24      itojun     42: static int
1.9       markus     43: check_rhosts_file(const char *filename, const char *hostname,
                     44:                  const char *ipaddr, const char *client_user,
                     45:                  const char *server_user)
1.1       deraadt    46: {
1.9       markus     47:        FILE *f;
                     48:        char buf[1024]; /* Must not be larger than host, user, dummy below. */
1.1       deraadt    49:
1.9       markus     50:        /* Open the .rhosts file, deny if unreadable */
                     51:        f = fopen(filename, "r");
                     52:        if (!f)
                     53:                return 0;
                     54:
                     55:        while (fgets(buf, sizeof(buf), f)) {
                     56:                /* All three must be at least as big as buf to avoid overflows. */
                     57:                char hostbuf[1024], userbuf[1024], dummy[1024], *host, *user, *cp;
                     58:                int negated;
                     59:
                     60:                for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
                     61:                        ;
                     62:                if (*cp == '#' || *cp == '\n' || !*cp)
                     63:                        continue;
                     64:
1.11      markus     65:                /*
                     66:                 * NO_PLUS is supported at least on OSF/1.  We skip it (we
                     67:                 * don't ever support the plus syntax).
                     68:                 */
1.9       markus     69:                if (strncmp(cp, "NO_PLUS", 7) == 0)
                     70:                        continue;
                     71:
1.11      markus     72:                /*
                     73:                 * This should be safe because each buffer is as big as the
                     74:                 * whole string, and thus cannot be overwritten.
                     75:                 */
1.30      itojun     76:                switch (sscanf(buf, "%1023s %1023s %1023s", hostbuf, userbuf,
                     77:                    dummy)) {
1.9       markus     78:                case 0:
1.28      markus     79:                        auth_debug_add("Found empty line in %.100s.", filename);
1.9       markus     80:                        continue;
                     81:                case 1:
                     82:                        /* Host name only. */
                     83:                        strlcpy(userbuf, server_user, sizeof(userbuf));
                     84:                        break;
                     85:                case 2:
                     86:                        /* Got both host and user name. */
                     87:                        break;
                     88:                case 3:
1.28      markus     89:                        auth_debug_add("Found garbage in %.100s.", filename);
1.9       markus     90:                        continue;
                     91:                default:
                     92:                        /* Weird... */
                     93:                        continue;
                     94:                }
                     95:
                     96:                host = hostbuf;
                     97:                user = userbuf;
                     98:                negated = 0;
                     99:
                    100:                /* Process negated host names, or positive netgroups. */
                    101:                if (host[0] == '-') {
                    102:                        negated = 1;
                    103:                        host++;
                    104:                } else if (host[0] == '+')
                    105:                        host++;
                    106:
                    107:                if (user[0] == '-') {
                    108:                        negated = 1;
                    109:                        user++;
                    110:                } else if (user[0] == '+')
                    111:                        user++;
                    112:
                    113:                /* Check for empty host/user names (particularly '+'). */
                    114:                if (!host[0] || !user[0]) {
                    115:                        /* We come here if either was '+' or '-'. */
1.28      markus    116:                        auth_debug_add("Ignoring wild host/user names in %.100s.",
                    117:                            filename);
1.9       markus    118:                        continue;
                    119:                }
                    120:                /* Verify that host name matches. */
                    121:                if (host[0] == '@') {
                    122:                        if (!innetgr(host + 1, hostname, NULL, NULL) &&
                    123:                            !innetgr(host + 1, ipaddr, NULL, NULL))
                    124:                                continue;
                    125:                } else if (strcasecmp(host, hostname) && strcmp(host, ipaddr) != 0)
                    126:                        continue;       /* Different hostname. */
                    127:
                    128:                /* Verify that user name matches. */
                    129:                if (user[0] == '@') {
                    130:                        if (!innetgr(user + 1, NULL, client_user, NULL))
                    131:                                continue;
                    132:                } else if (strcmp(user, client_user) != 0)
                    133:                        continue;       /* Different username. */
                    134:
                    135:                /* Found the user and host. */
                    136:                fclose(f);
                    137:
                    138:                /* If the entry was negated, deny access. */
                    139:                if (negated) {
1.28      markus    140:                        auth_debug_add("Matched negative entry in %.100s.",
1.33      djm       141:                            filename);
1.9       markus    142:                        return 0;
                    143:                }
                    144:                /* Accept authentication. */
                    145:                return 1;
                    146:        }
                    147:
                    148:        /* Authentication using this file denied. */
                    149:        fclose(f);
                    150:        return 0;
1.1       deraadt   151: }
                    152:
1.11      markus    153: /*
                    154:  * Tries to authenticate the user using the .shosts or .rhosts file. Returns
                    155:  * true if authentication succeeds.  If ignore_rhosts is true, only
                    156:  * /etc/hosts.equiv will be considered (.rhosts and .shosts are ignored).
                    157:  */
1.1       deraadt   158:
1.13      markus    159: int
1.9       markus    160: auth_rhosts(struct passwd *pw, const char *client_user)
1.1       deraadt   161: {
1.23      markus    162:        const char *hostname, *ipaddr;
                    163:
1.31      markus    164:        hostname = get_canonical_hostname(options.use_dns);
1.23      markus    165:        ipaddr = get_remote_ipaddr();
1.28      markus    166:        return auth_rhosts2(pw, client_user, hostname, ipaddr);
1.23      markus    167: }
                    168:
1.28      markus    169: static int
                    170: auth_rhosts2_raw(struct passwd *pw, const char *client_user, const char *hostname,
1.23      markus    171:     const char *ipaddr)
                    172: {
1.9       markus    173:        char buf[1024];
                    174:        struct stat st;
                    175:        static const char *rhosts_files[] = {".shosts", ".rhosts", NULL};
1.17      markus    176:        u_int rhosts_file_index;
1.9       markus    177:
1.23      markus    178:        debug2("auth_rhosts2: clientuser %s hostname %s ipaddr %s",
                    179:            client_user, hostname, ipaddr);
                    180:
1.9       markus    181:        /* Switch to the user's uid. */
1.22      markus    182:        temporarily_use_uid(pw);
1.11      markus    183:        /*
                    184:         * Quick check: if the user has no .shosts or .rhosts files, return
                    185:         * failure immediately without doing costly lookups from name
                    186:         * servers.
                    187:         */
1.9       markus    188:        for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
1.25      deraadt   189:            rhosts_file_index++) {
1.9       markus    190:                /* Check users .rhosts or .shosts. */
                    191:                snprintf(buf, sizeof buf, "%.500s/%.100s",
                    192:                         pw->pw_dir, rhosts_files[rhosts_file_index]);
                    193:                if (stat(buf, &st) >= 0)
                    194:                        break;
                    195:        }
                    196:        /* Switch back to privileged uid. */
                    197:        restore_uid();
                    198:
                    199:        /* Deny if The user has no .shosts or .rhosts file and there are no system-wide files. */
                    200:        if (!rhosts_files[rhosts_file_index] &&
1.18      markus    201:            stat(_PATH_RHOSTS_EQUIV, &st) < 0 &&
                    202:            stat(_PATH_SSH_HOSTS_EQUIV, &st) < 0)
1.9       markus    203:                return 0;
                    204:
                    205:        /* If not logging in as superuser, try /etc/hosts.equiv and shosts.equiv. */
                    206:        if (pw->pw_uid != 0) {
1.25      deraadt   207:                if (check_rhosts_file(_PATH_RHOSTS_EQUIV, hostname, ipaddr,
                    208:                    client_user, pw->pw_name)) {
1.28      markus    209:                        auth_debug_add("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
1.25      deraadt   210:                            hostname, ipaddr);
1.9       markus    211:                        return 1;
                    212:                }
1.25      deraadt   213:                if (check_rhosts_file(_PATH_SSH_HOSTS_EQUIV, hostname, ipaddr,
                    214:                    client_user, pw->pw_name)) {
1.28      markus    215:                        auth_debug_add("Accepted for %.100s [%.100s] by %.100s.",
1.25      deraadt   216:                            hostname, ipaddr, _PATH_SSH_HOSTS_EQUIV);
1.9       markus    217:                        return 1;
                    218:                }
                    219:        }
1.11      markus    220:        /*
                    221:         * Check that the home directory is owned by root or the user, and is
                    222:         * not group or world writable.
                    223:         */
1.9       markus    224:        if (stat(pw->pw_dir, &st) < 0) {
1.29      itojun    225:                logit("Rhosts authentication refused for %.100s: "
1.28      markus    226:                    "no home directory %.200s", pw->pw_name, pw->pw_dir);
                    227:                auth_debug_add("Rhosts authentication refused for %.100s: "
                    228:                    "no home directory %.200s", pw->pw_name, pw->pw_dir);
1.9       markus    229:                return 0;
                    230:        }
                    231:        if (options.strict_modes &&
                    232:            ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.25      deraadt   233:            (st.st_mode & 022) != 0)) {
1.29      itojun    234:                logit("Rhosts authentication refused for %.100s: "
1.28      markus    235:                    "bad ownership or modes for home directory.", pw->pw_name);
                    236:                auth_debug_add("Rhosts authentication refused for %.100s: "
                    237:                    "bad ownership or modes for home directory.", pw->pw_name);
1.9       markus    238:                return 0;
                    239:        }
                    240:        /* Temporarily use the user's uid. */
1.22      markus    241:        temporarily_use_uid(pw);
1.9       markus    242:
                    243:        /* Check all .rhosts files (currently .shosts and .rhosts). */
                    244:        for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
1.25      deraadt   245:            rhosts_file_index++) {
1.9       markus    246:                /* Check users .rhosts or .shosts. */
                    247:                snprintf(buf, sizeof buf, "%.500s/%.100s",
                    248:                         pw->pw_dir, rhosts_files[rhosts_file_index]);
                    249:                if (stat(buf, &st) < 0)
                    250:                        continue;
                    251:
1.11      markus    252:                /*
                    253:                 * Make sure that the file is either owned by the user or by
                    254:                 * root, and make sure it is not writable by anyone but the
                    255:                 * owner.  This is to help avoid novices accidentally
                    256:                 * allowing access to their account by anyone.
                    257:                 */
1.9       markus    258:                if (options.strict_modes &&
                    259:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
1.25      deraadt   260:                    (st.st_mode & 022) != 0)) {
1.29      itojun    261:                        logit("Rhosts authentication refused for %.100s: bad modes for %.200s",
1.9       markus    262:                            pw->pw_name, buf);
1.28      markus    263:                        auth_debug_add("Bad file modes for %.200s", buf);
1.9       markus    264:                        continue;
                    265:                }
                    266:                /* Check if we have been configured to ignore .rhosts and .shosts files. */
                    267:                if (options.ignore_rhosts) {
1.28      markus    268:                        auth_debug_add("Server has been configured to ignore %.100s.",
                    269:                            rhosts_files[rhosts_file_index]);
1.9       markus    270:                        continue;
                    271:                }
                    272:                /* Check if authentication is permitted by the file. */
                    273:                if (check_rhosts_file(buf, hostname, ipaddr, client_user, pw->pw_name)) {
1.28      markus    274:                        auth_debug_add("Accepted by %.100s.",
                    275:                            rhosts_files[rhosts_file_index]);
1.9       markus    276:                        /* Restore the privileged uid. */
                    277:                        restore_uid();
1.28      markus    278:                        auth_debug_add("Accepted host %s ip %s client_user %s server_user %s",
                    279:                                hostname, ipaddr, client_user, pw->pw_name);
1.9       markus    280:                        return 1;
                    281:                }
                    282:        }
                    283:
                    284:        /* Restore the privileged uid. */
                    285:        restore_uid();
                    286:        return 0;
1.28      markus    287: }
                    288:
                    289: int
                    290: auth_rhosts2(struct passwd *pw, const char *client_user, const char *hostname,
                    291:     const char *ipaddr)
                    292: {
                    293:        int ret;
                    294:
                    295:        auth_debug_reset();
                    296:        ret = auth_rhosts2_raw(pw, client_user, hostname, ipaddr);
                    297:        if (!use_privsep)
                    298:                auth_debug_send();
                    299:        return ret;
1.1       deraadt   300: }