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

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.16    ! markus     17: RCSID("$OpenBSD: auth-rhosts.c,v 1.15 2000/09/07 20:27:49 deraadt Exp $");
1.1       deraadt    18:
                     19: #include "packet.h"
                     20: #include "ssh.h"
                     21: #include "xmalloc.h"
                     22: #include "uidswap.h"
1.6       markus     23: #include "servconf.h"
1.1       deraadt    24:
1.11      markus     25: /*
                     26:  * This function processes an rhosts-style file (.rhosts, .shosts, or
                     27:  * /etc/hosts.equiv).  This returns true if authentication can be granted
                     28:  * based on the file, and returns zero otherwise.
                     29:  */
1.1       deraadt    30:
1.13      markus     31: int
1.9       markus     32: check_rhosts_file(const char *filename, const char *hostname,
                     33:                  const char *ipaddr, const char *client_user,
                     34:                  const char *server_user)
1.1       deraadt    35: {
1.9       markus     36:        FILE *f;
                     37:        char buf[1024]; /* Must not be larger than host, user, dummy below. */
1.1       deraadt    38:
1.9       markus     39:        /* Open the .rhosts file, deny if unreadable */
                     40:        f = fopen(filename, "r");
                     41:        if (!f)
                     42:                return 0;
                     43:
                     44:        while (fgets(buf, sizeof(buf), f)) {
                     45:                /* All three must be at least as big as buf to avoid overflows. */
                     46:                char hostbuf[1024], userbuf[1024], dummy[1024], *host, *user, *cp;
                     47:                int negated;
                     48:
                     49:                for (cp = buf; *cp == ' ' || *cp == '\t'; cp++)
                     50:                        ;
                     51:                if (*cp == '#' || *cp == '\n' || !*cp)
                     52:                        continue;
                     53:
1.11      markus     54:                /*
                     55:                 * NO_PLUS is supported at least on OSF/1.  We skip it (we
                     56:                 * don't ever support the plus syntax).
                     57:                 */
1.9       markus     58:                if (strncmp(cp, "NO_PLUS", 7) == 0)
                     59:                        continue;
                     60:
1.11      markus     61:                /*
                     62:                 * This should be safe because each buffer is as big as the
                     63:                 * whole string, and thus cannot be overwritten.
                     64:                 */
1.9       markus     65:                switch (sscanf(buf, "%s %s %s", hostbuf, userbuf, dummy)) {
                     66:                case 0:
                     67:                        packet_send_debug("Found empty line in %.100s.", filename);
                     68:                        continue;
                     69:                case 1:
                     70:                        /* Host name only. */
                     71:                        strlcpy(userbuf, server_user, sizeof(userbuf));
                     72:                        break;
                     73:                case 2:
                     74:                        /* Got both host and user name. */
                     75:                        break;
                     76:                case 3:
                     77:                        packet_send_debug("Found garbage in %.100s.", filename);
                     78:                        continue;
                     79:                default:
                     80:                        /* Weird... */
                     81:                        continue;
                     82:                }
                     83:
                     84:                host = hostbuf;
                     85:                user = userbuf;
                     86:                negated = 0;
                     87:
                     88:                /* Process negated host names, or positive netgroups. */
                     89:                if (host[0] == '-') {
                     90:                        negated = 1;
                     91:                        host++;
                     92:                } else if (host[0] == '+')
                     93:                        host++;
                     94:
                     95:                if (user[0] == '-') {
                     96:                        negated = 1;
                     97:                        user++;
                     98:                } else if (user[0] == '+')
                     99:                        user++;
                    100:
                    101:                /* Check for empty host/user names (particularly '+'). */
                    102:                if (!host[0] || !user[0]) {
                    103:                        /* We come here if either was '+' or '-'. */
                    104:                        packet_send_debug("Ignoring wild host/user names in %.100s.",
                    105:                                          filename);
                    106:                        continue;
                    107:                }
                    108:                /* Verify that host name matches. */
                    109:                if (host[0] == '@') {
                    110:                        if (!innetgr(host + 1, hostname, NULL, NULL) &&
                    111:                            !innetgr(host + 1, ipaddr, NULL, NULL))
                    112:                                continue;
                    113:                } else if (strcasecmp(host, hostname) && strcmp(host, ipaddr) != 0)
                    114:                        continue;       /* Different hostname. */
                    115:
                    116:                /* Verify that user name matches. */
                    117:                if (user[0] == '@') {
                    118:                        if (!innetgr(user + 1, NULL, client_user, NULL))
                    119:                                continue;
                    120:                } else if (strcmp(user, client_user) != 0)
                    121:                        continue;       /* Different username. */
                    122:
                    123:                /* Found the user and host. */
                    124:                fclose(f);
                    125:
                    126:                /* If the entry was negated, deny access. */
                    127:                if (negated) {
                    128:                        packet_send_debug("Matched negative entry in %.100s.",
                    129:                                          filename);
                    130:                        return 0;
                    131:                }
                    132:                /* Accept authentication. */
                    133:                return 1;
                    134:        }
                    135:
                    136:        /* Authentication using this file denied. */
                    137:        fclose(f);
                    138:        return 0;
1.1       deraadt   139: }
                    140:
1.11      markus    141: /*
                    142:  * Tries to authenticate the user using the .shosts or .rhosts file. Returns
                    143:  * true if authentication succeeds.  If ignore_rhosts is true, only
                    144:  * /etc/hosts.equiv will be considered (.rhosts and .shosts are ignored).
                    145:  */
1.1       deraadt   146:
1.13      markus    147: int
1.9       markus    148: auth_rhosts(struct passwd *pw, const char *client_user)
1.1       deraadt   149: {
1.9       markus    150:        extern ServerOptions options;
                    151:        char buf[1024];
                    152:        const char *hostname, *ipaddr;
                    153:        struct stat st;
                    154:        static const char *rhosts_files[] = {".shosts", ".rhosts", NULL};
                    155:        unsigned int rhosts_file_index;
                    156:
1.16    ! markus    157:        /* no user given */
        !           158:        if (pw == NULL)
        !           159:                return 0;
1.9       markus    160:        /* Switch to the user's uid. */
                    161:        temporarily_use_uid(pw->pw_uid);
1.11      markus    162:        /*
                    163:         * Quick check: if the user has no .shosts or .rhosts files, return
                    164:         * failure immediately without doing costly lookups from name
                    165:         * servers.
                    166:         */
1.9       markus    167:        for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
                    168:             rhosts_file_index++) {
                    169:                /* Check users .rhosts or .shosts. */
                    170:                snprintf(buf, sizeof buf, "%.500s/%.100s",
                    171:                         pw->pw_dir, rhosts_files[rhosts_file_index]);
                    172:                if (stat(buf, &st) >= 0)
                    173:                        break;
                    174:        }
                    175:        /* Switch back to privileged uid. */
                    176:        restore_uid();
                    177:
                    178:        /* Deny if The user has no .shosts or .rhosts file and there are no system-wide files. */
                    179:        if (!rhosts_files[rhosts_file_index] &&
                    180:            stat("/etc/hosts.equiv", &st) < 0 &&
                    181:            stat(SSH_HOSTS_EQUIV, &st) < 0)
                    182:                return 0;
                    183:
                    184:        hostname = get_canonical_hostname();
                    185:        ipaddr = get_remote_ipaddr();
                    186:
                    187:        /* If not logging in as superuser, try /etc/hosts.equiv and shosts.equiv. */
                    188:        if (pw->pw_uid != 0) {
                    189:                if (check_rhosts_file("/etc/hosts.equiv", hostname, ipaddr, client_user,
                    190:                                      pw->pw_name)) {
                    191:                        packet_send_debug("Accepted for %.100s [%.100s] by /etc/hosts.equiv.",
                    192:                                          hostname, ipaddr);
                    193:                        return 1;
                    194:                }
                    195:                if (check_rhosts_file(SSH_HOSTS_EQUIV, hostname, ipaddr, client_user,
                    196:                                      pw->pw_name)) {
                    197:                        packet_send_debug("Accepted for %.100s [%.100s] by %.100s.",
                    198:                                      hostname, ipaddr, SSH_HOSTS_EQUIV);
                    199:                        return 1;
                    200:                }
                    201:        }
1.11      markus    202:        /*
                    203:         * Check that the home directory is owned by root or the user, and is
                    204:         * not group or world writable.
                    205:         */
1.9       markus    206:        if (stat(pw->pw_dir, &st) < 0) {
                    207:                log("Rhosts authentication refused for %.100s: no home directory %.200s",
                    208:                    pw->pw_name, pw->pw_dir);
1.12      markus    209:                packet_send_debug("Rhosts authentication refused for %.100s: no home directory %.200s",
1.9       markus    210:                                  pw->pw_name, pw->pw_dir);
                    211:                return 0;
                    212:        }
                    213:        if (options.strict_modes &&
                    214:            ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    215:             (st.st_mode & 022) != 0)) {
                    216:                log("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
                    217:                    pw->pw_name);
                    218:                packet_send_debug("Rhosts authentication refused for %.100s: bad ownership or modes for home directory.",
                    219:                                  pw->pw_name);
                    220:                return 0;
                    221:        }
                    222:        /* Temporarily use the user's uid. */
                    223:        temporarily_use_uid(pw->pw_uid);
                    224:
                    225:        /* Check all .rhosts files (currently .shosts and .rhosts). */
                    226:        for (rhosts_file_index = 0; rhosts_files[rhosts_file_index];
                    227:             rhosts_file_index++) {
                    228:                /* Check users .rhosts or .shosts. */
                    229:                snprintf(buf, sizeof buf, "%.500s/%.100s",
                    230:                         pw->pw_dir, rhosts_files[rhosts_file_index]);
                    231:                if (stat(buf, &st) < 0)
                    232:                        continue;
                    233:
1.11      markus    234:                /*
                    235:                 * Make sure that the file is either owned by the user or by
                    236:                 * root, and make sure it is not writable by anyone but the
                    237:                 * owner.  This is to help avoid novices accidentally
                    238:                 * allowing access to their account by anyone.
                    239:                 */
1.9       markus    240:                if (options.strict_modes &&
                    241:                    ((st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    242:                     (st.st_mode & 022) != 0)) {
                    243:                        log("Rhosts authentication refused for %.100s: bad modes for %.200s",
                    244:                            pw->pw_name, buf);
                    245:                        packet_send_debug("Bad file modes for %.200s", buf);
                    246:                        continue;
                    247:                }
                    248:                /* Check if we have been configured to ignore .rhosts and .shosts files. */
                    249:                if (options.ignore_rhosts) {
                    250:                        packet_send_debug("Server has been configured to ignore %.100s.",
                    251:                                          rhosts_files[rhosts_file_index]);
                    252:                        continue;
                    253:                }
                    254:                /* Check if authentication is permitted by the file. */
                    255:                if (check_rhosts_file(buf, hostname, ipaddr, client_user, pw->pw_name)) {
                    256:                        packet_send_debug("Accepted by %.100s.",
                    257:                                          rhosts_files[rhosts_file_index]);
                    258:                        /* Restore the privileged uid. */
                    259:                        restore_uid();
                    260:                        return 1;
                    261:                }
                    262:        }
                    263:
                    264:        /* Restore the privileged uid. */
                    265:        restore_uid();
                    266:        return 0;
1.1       deraadt   267: }