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

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