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

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