rcmd.c revision 92986
11573Srgrimes/* 21573Srgrimes * Copyright (c) 1983, 1993, 1994 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 3. All advertising materials mentioning features or use of this software 141573Srgrimes * must display the following acknowledgement: 151573Srgrimes * This product includes software developed by the University of 161573Srgrimes * California, Berkeley and its contributors. 171573Srgrimes * 4. Neither the name of the University nor the names of its contributors 181573Srgrimes * may be used to endorse or promote products derived from this software 191573Srgrimes * without specific prior written permission. 201573Srgrimes * 211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311573Srgrimes * SUCH DAMAGE. 321573Srgrimes */ 331573Srgrimes 341573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 351573Srgrimesstatic char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; 361573Srgrimes#endif /* LIBC_SCCS and not lint */ 3792986Sobrien#include <sys/cdefs.h> 3892986Sobrien__FBSDID("$FreeBSD: head/lib/libc/net/rcmd.c 92986 2002-03-22 21:53:29Z obrien $"); 391573Srgrimes 4071579Sdeischen#include "namespace.h" 411573Srgrimes#include <sys/param.h> 421573Srgrimes#include <sys/socket.h> 431573Srgrimes#include <sys/stat.h> 441573Srgrimes 451573Srgrimes#include <netinet/in.h> 461573Srgrimes#include <arpa/inet.h> 471573Srgrimes 481573Srgrimes#include <signal.h> 491573Srgrimes#include <fcntl.h> 501573Srgrimes#include <netdb.h> 5185342Simp#include <stdlib.h> 521573Srgrimes#include <unistd.h> 531573Srgrimes#include <pwd.h> 541573Srgrimes#include <errno.h> 551573Srgrimes#include <stdio.h> 561573Srgrimes#include <ctype.h> 571573Srgrimes#include <string.h> 589978Swpaul#ifdef YP 599978Swpaul#include <rpc/rpc.h> 609978Swpaul#include <rpcsvc/yp_prot.h> 619978Swpaul#include <rpcsvc/ypclnt.h> 629978Swpaul#endif 6363682Sume#include <arpa/nameser.h> 6471579Sdeischen#include "un-namespace.h" 651573Srgrimes 6655918Sshin/* wrapper for KAME-special getnameinfo() */ 6755918Sshin#ifndef NI_WITHSCOPEID 6855918Sshin#define NI_WITHSCOPEID 0 6955918Sshin#endif 7055918Sshin 7192905Sobrienextern int innetgr( const char *, const char *, const char *, const char * ); 7217141Sjkh 732592Scsgr#define max(a, b) ((a > b) ? a : b) 742592Scsgr 7592941Sobrienint __ivaliduser(FILE *, u_int32_t, const char *, const char *); 7692941Sobrienint __ivaliduser_af(FILE *,const void *, const char *, const char *, int, int); 7792941Sobrienint __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, const char *, 7892941Sobrien const char *); 7992941Sobrienstatic int __icheckhost(const struct sockaddr *, socklen_t, const char *); 801573Srgrimes 8169575Sumechar paddr[NI_MAXHOST]; 8255918Sshin 831573Srgrimesint 841573Srgrimesrcmd(ahost, rport, locuser, remuser, cmd, fd2p) 851573Srgrimes char **ahost; 861573Srgrimes u_short rport; 871573Srgrimes const char *locuser, *remuser, *cmd; 881573Srgrimes int *fd2p; 891573Srgrimes{ 9056590Sshin return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); 9156590Sshin} 9256590Sshin 9356590Sshinint 9456590Sshinrcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, af) 9556590Sshin char **ahost; 9656590Sshin u_short rport; 9756590Sshin const char *locuser, *remuser, *cmd; 9856590Sshin int *fd2p; 9956590Sshin int af; 10056590Sshin{ 10155918Sshin struct addrinfo hints, *res, *ai; 10255918Sshin struct sockaddr_storage from; 1031573Srgrimes fd_set reads; 10471579Sdeischen sigset_t oldmask, newmask; 1051573Srgrimes pid_t pid; 10655918Sshin int s, aport, lport, timo, error; 10785342Simp char c, *p; 10869575Sume int refused, nres; 10963682Sume char num[8]; 11063682Sume static char canonnamebuf[MAXDNAME]; /* is it proper here? */ 1111573Srgrimes 11285342Simp /* call rcmdsh() with specified remote shell if appropriate. */ 11385342Simp if (!issetugid() && (p = getenv("RSH"))) { 11485342Simp struct servent *sp = getservbyname("shell", "tcp"); 11585342Simp 11685342Simp if (sp && sp->s_port == rport) 11785342Simp return (rcmdsh(ahost, rport, locuser, remuser, 11885342Simp cmd, p)); 11985342Simp } 12085342Simp 12185342Simp /* use rsh(1) if non-root and remote port is shell. */ 12285342Simp if (geteuid()) { 12385342Simp struct servent *sp = getservbyname("shell", "tcp"); 12485342Simp 12585342Simp if (sp && sp->s_port == rport) 12685342Simp return (rcmdsh(ahost, rport, locuser, remuser, 12785342Simp cmd, NULL)); 12885342Simp } 12985342Simp 1301573Srgrimes pid = getpid(); 13155918Sshin 13255918Sshin memset(&hints, 0, sizeof(hints)); 13355918Sshin hints.ai_flags = AI_CANONNAME; 13456590Sshin hints.ai_family = af; 13555918Sshin hints.ai_socktype = SOCK_STREAM; 13655918Sshin hints.ai_protocol = 0; 13755918Sshin (void)snprintf(num, sizeof(num), "%d", ntohs(rport)); 13855918Sshin error = getaddrinfo(*ahost, num, &hints, &res); 13955918Sshin if (error) { 14055918Sshin fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14155918Sshin gai_strerror(error)); 14255918Sshin if (error == EAI_SYSTEM) 14355918Sshin fprintf(stderr, "rcmd: getaddrinfo: %s\n", 14455918Sshin strerror(errno)); 1451573Srgrimes return (-1); 1461573Srgrimes } 14763682Sume 14863682Sume if (res->ai_canonname 14963682Sume && strlen(res->ai_canonname) + 1 < sizeof(canonnamebuf)) { 15063682Sume strncpy(canonnamebuf, res->ai_canonname, sizeof(canonnamebuf)); 15163682Sume *ahost = canonnamebuf; 15263682Sume } 15369575Sume nres = 0; 15469575Sume for (ai = res; ai; ai = ai->ai_next) 15569575Sume nres++; 15655918Sshin ai = res; 15769575Sume refused = 0; 15871579Sdeischen sigemptyset(&newmask); 15971579Sdeischen sigaddset(&newmask, SIGURG); 16071579Sdeischen _sigprocmask(SIG_BLOCK, (const sigset_t *)&newmask, &oldmask); 1611573Srgrimes for (timo = 1, lport = IPPORT_RESERVED - 1;;) { 16255918Sshin s = rresvport_af(&lport, ai->ai_family); 1631573Srgrimes if (s < 0) { 16457123Sshin if (errno != EAGAIN && ai->ai_next) { 16557123Sshin ai = ai->ai_next; 16657123Sshin continue; 16757123Sshin } 1681573Srgrimes if (errno == EAGAIN) 1691573Srgrimes (void)fprintf(stderr, 1701573Srgrimes "rcmd: socket: All ports in use\n"); 1711573Srgrimes else 1721573Srgrimes (void)fprintf(stderr, "rcmd: socket: %s\n", 1731573Srgrimes strerror(errno)); 17457123Sshin freeaddrinfo(res); 17571579Sdeischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 17671579Sdeischen NULL); 1771573Srgrimes return (-1); 1781573Srgrimes } 17956698Sjasone _fcntl(s, F_SETOWN, pid); 18071579Sdeischen if (_connect(s, ai->ai_addr, ai->ai_addrlen) >= 0) 1811573Srgrimes break; 18256698Sjasone (void)_close(s); 1831573Srgrimes if (errno == EADDRINUSE) { 1841573Srgrimes lport--; 1851573Srgrimes continue; 1861573Srgrimes } 18769575Sume if (errno == ECONNREFUSED) 18869575Sume refused = 1; 18969575Sume if (ai->ai_next == NULL && (!refused || timo > 16)) { 19069575Sume (void)fprintf(stderr, "%s: %s\n", 19169575Sume *ahost, strerror(errno)); 19269575Sume freeaddrinfo(res); 19371579Sdeischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, 19471579Sdeischen NULL); 19569575Sume return (-1); 19664493Sume } 19769575Sume if (nres > 1) { 1981573Srgrimes int oerrno = errno; 1991573Srgrimes 20055918Sshin getnameinfo(ai->ai_addr, ai->ai_addrlen, 20155918Sshin paddr, sizeof(paddr), 20255918Sshin NULL, 0, 20355918Sshin NI_NUMERICHOST|NI_WITHSCOPEID); 2041573Srgrimes (void)fprintf(stderr, "connect to address %s: ", 20555918Sshin paddr); 2061573Srgrimes errno = oerrno; 2071573Srgrimes perror(0); 20869575Sume } 20969575Sume if ((ai = ai->ai_next) == NULL) { 21069575Sume /* refused && timo <= 16 */ 21169575Sume struct timespec time_to_sleep, time_remaining; 21269575Sume 21369575Sume time_to_sleep.tv_sec = timo; 21469575Sume time_to_sleep.tv_nsec = 0; 21569575Sume (void)_nanosleep(&time_to_sleep, &time_remaining); 21669575Sume timo *= 2; 21769575Sume ai = res; 21869575Sume refused = 0; 21969575Sume } 22069575Sume if (nres > 1) { 22155918Sshin getnameinfo(ai->ai_addr, ai->ai_addrlen, 22255918Sshin paddr, sizeof(paddr), 22355918Sshin NULL, 0, 22455918Sshin NI_NUMERICHOST|NI_WITHSCOPEID); 22555918Sshin fprintf(stderr, "Trying %s...\n", paddr); 2261573Srgrimes } 2271573Srgrimes } 2281573Srgrimes lport--; 2291573Srgrimes if (fd2p == 0) { 23056698Sjasone _write(s, "", 1); 2311573Srgrimes lport = 0; 2321573Srgrimes } else { 2331573Srgrimes char num[8]; 23455918Sshin int s2 = rresvport_af(&lport, ai->ai_family), s3; 23555918Sshin int len = ai->ai_addrlen; 2362592Scsgr int nfds; 2371573Srgrimes 2381573Srgrimes if (s2 < 0) 2391573Srgrimes goto bad; 24071579Sdeischen _listen(s2, 1); 2411573Srgrimes (void)snprintf(num, sizeof(num), "%d", lport); 24256698Sjasone if (_write(s, num, strlen(num)+1) != strlen(num)+1) { 2431573Srgrimes (void)fprintf(stderr, 2441573Srgrimes "rcmd: write (setting up stderr): %s\n", 2451573Srgrimes strerror(errno)); 24656698Sjasone (void)_close(s2); 2471573Srgrimes goto bad; 2481573Srgrimes } 2492592Scsgr nfds = max(s, s2)+1; 2502592Scsgr if(nfds > FD_SETSIZE) { 2512592Scsgr fprintf(stderr, "rcmd: too many files\n"); 25256698Sjasone (void)_close(s2); 2532592Scsgr goto bad; 2542592Scsgr } 25517543Speteragain: 2561573Srgrimes FD_ZERO(&reads); 2571573Srgrimes FD_SET(s, &reads); 2581573Srgrimes FD_SET(s2, &reads); 2591573Srgrimes errno = 0; 26071579Sdeischen if (_select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){ 2611573Srgrimes if (errno != 0) 2621573Srgrimes (void)fprintf(stderr, 2631573Srgrimes "rcmd: select (setting up stderr): %s\n", 2641573Srgrimes strerror(errno)); 2651573Srgrimes else 2661573Srgrimes (void)fprintf(stderr, 2671573Srgrimes "select: protocol failure in circuit setup\n"); 26856698Sjasone (void)_close(s2); 2691573Srgrimes goto bad; 2701573Srgrimes } 27171579Sdeischen s3 = _accept(s2, (struct sockaddr *)&from, &len); 27255918Sshin switch (from.ss_family) { 27355918Sshin case AF_INET: 27455918Sshin aport = ntohs(((struct sockaddr_in *)&from)->sin_port); 27555918Sshin break; 27655918Sshin#ifdef INET6 27755918Sshin case AF_INET6: 27855918Sshin aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port); 27955918Sshin break; 28055918Sshin#endif 28155918Sshin default: 28255918Sshin aport = 0; /* error */ 28355918Sshin break; 28455918Sshin } 28517543Speter /* 28617543Speter * XXX careful for ftp bounce attacks. If discovered, shut them 28717543Speter * down and check for the real auxiliary channel to connect. 28817543Speter */ 28955918Sshin if (aport == 20) { 29056698Sjasone _close(s3); 29117543Speter goto again; 29217543Speter } 29356698Sjasone (void)_close(s2); 2941573Srgrimes if (s3 < 0) { 2951573Srgrimes (void)fprintf(stderr, 2961573Srgrimes "rcmd: accept: %s\n", strerror(errno)); 2971573Srgrimes lport = 0; 2981573Srgrimes goto bad; 2991573Srgrimes } 3001573Srgrimes *fd2p = s3; 30155918Sshin if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) { 3021573Srgrimes (void)fprintf(stderr, 3031573Srgrimes "socket: protocol failure in circuit setup.\n"); 3041573Srgrimes goto bad2; 3051573Srgrimes } 3061573Srgrimes } 30756698Sjasone (void)_write(s, locuser, strlen(locuser)+1); 30856698Sjasone (void)_write(s, remuser, strlen(remuser)+1); 30956698Sjasone (void)_write(s, cmd, strlen(cmd)+1); 31056698Sjasone if (_read(s, &c, 1) != 1) { 3111573Srgrimes (void)fprintf(stderr, 3121573Srgrimes "rcmd: %s: %s\n", *ahost, strerror(errno)); 3131573Srgrimes goto bad2; 3141573Srgrimes } 3151573Srgrimes if (c != 0) { 31656698Sjasone while (_read(s, &c, 1) == 1) { 31756698Sjasone (void)_write(STDERR_FILENO, &c, 1); 3181573Srgrimes if (c == '\n') 3191573Srgrimes break; 3201573Srgrimes } 3211573Srgrimes goto bad2; 3221573Srgrimes } 32371579Sdeischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 32455918Sshin freeaddrinfo(res); 3251573Srgrimes return (s); 3261573Srgrimesbad2: 3271573Srgrimes if (lport) 32856698Sjasone (void)_close(*fd2p); 3291573Srgrimesbad: 33056698Sjasone (void)_close(s); 33171579Sdeischen _sigprocmask(SIG_SETMASK, (const sigset_t *)&oldmask, NULL); 33255918Sshin freeaddrinfo(res); 3331573Srgrimes return (-1); 3341573Srgrimes} 3351573Srgrimes 3361573Srgrimesint 33755918Sshinrresvport(port) 33855918Sshin int *port; 3391573Srgrimes{ 34055918Sshin return rresvport_af(port, AF_INET); 34155918Sshin} 3421573Srgrimes 34355918Sshinint 34455918Sshinrresvport_af(alport, family) 34555918Sshin int *alport, family; 34655918Sshin{ 34755918Sshin int i, s, len, err; 34855918Sshin struct sockaddr_storage ss; 34955918Sshin u_short *sport; 35055918Sshin 35155918Sshin memset(&ss, 0, sizeof(ss)); 35255918Sshin ss.ss_family = family; 35355918Sshin switch (family) { 35455918Sshin case AF_INET: 35555918Sshin ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in); 35655918Sshin sport = &((struct sockaddr_in *)&ss)->sin_port; 35755918Sshin ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY; 35855918Sshin break; 35955918Sshin#ifdef INET6 36055918Sshin case AF_INET6: 36155918Sshin ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6); 36255918Sshin sport = &((struct sockaddr_in6 *)&ss)->sin6_port; 36355918Sshin ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any; 36455918Sshin break; 36555918Sshin#endif 36655918Sshin default: 36755918Sshin errno = EAFNOSUPPORT; 36855918Sshin return -1; 36955918Sshin } 37055918Sshin 37171579Sdeischen s = _socket(ss.ss_family, SOCK_STREAM, 0); 3721573Srgrimes if (s < 0) 3731573Srgrimes return (-1); 37417543Speter#if 0 /* compat_exact_traditional_rresvport_semantics */ 37517543Speter sin.sin_port = htons((u_short)*alport); 37671579Sdeischen if (_bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0) 37717543Speter return (s); 37817543Speter if (errno != EADDRINUSE) { 37956698Sjasone (void)_close(s); 38017543Speter return (-1); 38116034Speter } 38217543Speter#endif 38355918Sshin *sport = 0; 38456636Sshin if (bindresvport_sa(s, (struct sockaddr *)&ss) == -1) { 38556698Sjasone (void)_close(s); 38617543Speter return (-1); 3871573Srgrimes } 38855918Sshin *alport = (int)ntohs(*sport); 38917543Speter return (s); 3901573Srgrimes} 3911573Srgrimes 3921573Srgrimesint __check_rhosts_file = 1; 3931573Srgrimeschar *__rcmd_errstr; 3941573Srgrimes 3951573Srgrimesint 3961573Srgrimesruserok(rhost, superuser, ruser, luser) 3971573Srgrimes const char *rhost, *ruser, *luser; 3981573Srgrimes int superuser; 3991573Srgrimes{ 40056939Sshin struct addrinfo hints, *res, *r; 40156939Sshin int error; 40255918Sshin 40356939Sshin memset(&hints, 0, sizeof(hints)); 40456939Sshin hints.ai_family = PF_UNSPEC; 40556939Sshin hints.ai_socktype = SOCK_DGRAM; /*dummy*/ 40656939Sshin error = getaddrinfo(rhost, "0", &hints, &res); 40756939Sshin if (error) 40856939Sshin return (-1); 4091573Srgrimes 41056939Sshin for (r = res; r; r = r->ai_next) { 41156939Sshin if (iruserok_sa(r->ai_addr, r->ai_addrlen, superuser, ruser, 41256939Sshin luser) == 0) { 41356939Sshin freeaddrinfo(res); 41456939Sshin return (0); 41555918Sshin } 4161573Srgrimes } 41756939Sshin freeaddrinfo(res); 41856939Sshin return (-1); 4191573Srgrimes} 4201573Srgrimes 4211573Srgrimes/* 4221573Srgrimes * New .rhosts strategy: We are passed an ip address. We spin through 4231573Srgrimes * hosts.equiv and .rhosts looking for a match. When the .rhosts only 4241573Srgrimes * has ip addresses, we don't have to trust a nameserver. When it 4251573Srgrimes * contains hostnames, we spin through the list of addresses the nameserver 4261573Srgrimes * gives us and look for a match. 4271573Srgrimes * 4281573Srgrimes * Returns 0 if ok, -1 if not ok. 4291573Srgrimes */ 4301573Srgrimesint 4311573Srgrimesiruserok(raddr, superuser, ruser, luser) 43239979Sdfr unsigned long raddr; 4331573Srgrimes int superuser; 4341573Srgrimes const char *ruser, *luser; 4351573Srgrimes{ 43669575Sume struct sockaddr_in sin; 43769575Sume 43869575Sume memset(&sin, 0, sizeof(sin)); 43969575Sume sin.sin_family = AF_INET; 44069575Sume sin.sin_len = sizeof(struct sockaddr_in); 44169575Sume memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 44269575Sume return iruserok_sa((struct sockaddr *)&sin, sin.sin_len, superuser, 44369575Sume ruser, luser); 44455918Sshin} 44555918Sshin 44669575Sume/* 44769575Sume * AF independent extension of iruserok. 44869575Sume * 44969575Sume * Returns 0 if ok, -1 if not ok. 45069575Sume */ 45169575Sumeint 45269575Sumeiruserok_sa(ra, rlen, superuser, ruser, luser) 45369575Sume const void *ra; 45469575Sume int rlen; 45555918Sshin int superuser; 45655918Sshin const char *ruser, *luser; 45755918Sshin{ 45892889Sobrien char *cp; 4591573Srgrimes struct stat sbuf; 4601573Srgrimes struct passwd *pwd; 4611573Srgrimes FILE *hostf; 4621573Srgrimes uid_t uid; 4631573Srgrimes int first; 4641573Srgrimes char pbuf[MAXPATHLEN]; 46569575Sume const struct sockaddr *raddr; 46669575Sume struct sockaddr_storage ss; 4671573Srgrimes 46869575Sume /* avoid alignment issue */ 46969575Sume if (rlen > sizeof(ss)) 47069575Sume return(-1); 47169575Sume memcpy(&ss, ra, rlen); 47269575Sume raddr = (struct sockaddr *)&ss; 47355918Sshin 4741573Srgrimes first = 1; 4751573Srgrimes hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); 4761573Srgrimesagain: 4771573Srgrimes if (hostf) { 47869575Sume if (__ivaliduser_sa(hostf, raddr, rlen, luser, ruser) == 0) { 4791573Srgrimes (void)fclose(hostf); 4801573Srgrimes return (0); 4811573Srgrimes } 4821573Srgrimes (void)fclose(hostf); 4831573Srgrimes } 4841573Srgrimes if (first == 1 && (__check_rhosts_file || superuser)) { 4851573Srgrimes first = 0; 4861573Srgrimes if ((pwd = getpwnam(luser)) == NULL) 4871573Srgrimes return (-1); 4881573Srgrimes (void)strcpy(pbuf, pwd->pw_dir); 4891573Srgrimes (void)strcat(pbuf, "/.rhosts"); 4901573Srgrimes 4911573Srgrimes /* 4921573Srgrimes * Change effective uid while opening .rhosts. If root and 4931573Srgrimes * reading an NFS mounted file system, can't read files that 4941573Srgrimes * are protected read/write owner only. 4951573Srgrimes */ 4961573Srgrimes uid = geteuid(); 4971573Srgrimes (void)seteuid(pwd->pw_uid); 4981573Srgrimes hostf = fopen(pbuf, "r"); 4991573Srgrimes (void)seteuid(uid); 5001573Srgrimes 5011573Srgrimes if (hostf == NULL) 5021573Srgrimes return (-1); 5031573Srgrimes /* 5041573Srgrimes * If not a regular file, or is owned by someone other than 5051573Srgrimes * user or root or if writeable by anyone but the owner, quit. 5061573Srgrimes */ 5071573Srgrimes cp = NULL; 5081573Srgrimes if (lstat(pbuf, &sbuf) < 0) 5091573Srgrimes cp = ".rhosts lstat failed"; 5101573Srgrimes else if (!S_ISREG(sbuf.st_mode)) 5111573Srgrimes cp = ".rhosts not regular file"; 51271579Sdeischen else if (_fstat(fileno(hostf), &sbuf) < 0) 5131573Srgrimes cp = ".rhosts fstat failed"; 5141573Srgrimes else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) 5151573Srgrimes cp = "bad .rhosts owner"; 5161573Srgrimes else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) 5171573Srgrimes cp = ".rhosts writeable by other than owner"; 5181573Srgrimes /* If there were any problems, quit. */ 5191573Srgrimes if (cp) { 5201573Srgrimes __rcmd_errstr = cp; 5211573Srgrimes (void)fclose(hostf); 5221573Srgrimes return (-1); 5231573Srgrimes } 5241573Srgrimes goto again; 5251573Srgrimes } 5261573Srgrimes return (-1); 5271573Srgrimes} 5281573Srgrimes 5291573Srgrimes/* 53069575Sume * XXX 53169575Sume * Don't make static, used by lpd(8). 53256939Sshin * 53356939Sshin * Returns 0 if ok, -1 if not ok. 53456939Sshin */ 53556939Sshinint 53669575Sume__ivaliduser(hostf, raddr, luser, ruser) 53769575Sume FILE *hostf; 53869575Sume u_int32_t raddr; 53969575Sume const char *luser, *ruser; 54056939Sshin{ 54169575Sume struct sockaddr_in sin; 54256939Sshin 54369575Sume memset(&sin, 0, sizeof(sin)); 54469575Sume sin.sin_family = AF_INET; 54569575Sume sin.sin_len = sizeof(struct sockaddr_in); 54669575Sume memcpy(&sin.sin_addr, &raddr, sizeof(sin.sin_addr)); 54769575Sume return __ivaliduser_sa(hostf, (struct sockaddr *)&sin, sin.sin_len, 54869575Sume luser, ruser); 54969575Sume} 55069575Sume 55169575Sume/* 55269575Sume * Returns 0 if ok, -1 if not ok. 55369575Sume * 55469575Sume * XXX obsolete API. 55569575Sume */ 55669575Sumeint 55769575Sume__ivaliduser_af(hostf, raddr, luser, ruser, af, len) 55869575Sume FILE *hostf; 55969575Sume const void *raddr; 56069575Sume const char *luser, *ruser; 56169575Sume int af, len; 56269575Sume{ 56369575Sume struct sockaddr *sa = NULL; 56469575Sume struct sockaddr_in *sin = NULL; 56569575Sume#ifdef INET6 56669575Sume struct sockaddr_in6 *sin6 = NULL; 56769575Sume#endif 56869575Sume struct sockaddr_storage ss; 56969575Sume 57069575Sume memset(&ss, 0, sizeof(ss)); 57169575Sume switch (af) { 57256939Sshin case AF_INET: 57369575Sume if (len != sizeof(sin->sin_addr)) 57469575Sume return -1; 57569575Sume sin = (struct sockaddr_in *)&ss; 57669575Sume sin->sin_family = AF_INET; 57769575Sume sin->sin_len = sizeof(struct sockaddr_in); 57869575Sume memcpy(&sin->sin_addr, raddr, sizeof(sin->sin_addr)); 57956939Sshin break; 58056939Sshin#ifdef INET6 58156939Sshin case AF_INET6: 58269575Sume if (len != sizeof(sin6->sin6_addr)) 58369575Sume return -1; 58469575Sume /* you will lose scope info */ 58569575Sume sin6 = (struct sockaddr_in6 *)&ss; 58669575Sume sin6->sin6_family = AF_INET6; 58769575Sume sin6->sin6_len = sizeof(struct sockaddr_in6); 58869575Sume memcpy(&sin6->sin6_addr, raddr, sizeof(sin6->sin6_addr)); 58956939Sshin break; 59056939Sshin#endif 59169575Sume default: 59269575Sume return -1; 59356939Sshin } 59456939Sshin 59569575Sume sa = (struct sockaddr *)&ss; 59669575Sume return __ivaliduser_sa(hostf, sa, sa->sa_len, luser, ruser); 59756939Sshin} 59856939Sshin 5991573Srgrimesint 60069575Sume__ivaliduser_sa(hostf, raddr, salen, luser, ruser) 6011573Srgrimes FILE *hostf; 60269575Sume const struct sockaddr *raddr; 60369575Sume socklen_t salen; 6041573Srgrimes const char *luser, *ruser; 6051573Srgrimes{ 60692889Sobrien char *user, *p; 6071573Srgrimes int ch; 6081573Srgrimes char buf[MAXHOSTNAMELEN + 128]; /* host + login */ 60910059Swpaul char hname[MAXHOSTNAMELEN]; 6107183Swpaul /* Presumed guilty until proven innocent. */ 6117183Swpaul int userok = 0, hostok = 0; 61255918Sshin int h_error; 6139978Swpaul#ifdef YP 6149978Swpaul char *ypdomain; 6151573Srgrimes 6169978Swpaul if (yp_get_default_domain(&ypdomain)) 6179978Swpaul ypdomain = NULL; 6189978Swpaul#else 6199978Swpaul#define ypdomain NULL 6209978Swpaul#endif 6217183Swpaul /* We need to get the damn hostname back for netgroup matching. */ 62269575Sume if (getnameinfo(raddr, salen, hname, sizeof(hname), NULL, 0, 62369575Sume NI_NAMEREQD) != 0) 6247183Swpaul return (-1); 6257183Swpaul 6261573Srgrimes while (fgets(buf, sizeof(buf), hostf)) { 6271573Srgrimes p = buf; 6281573Srgrimes /* Skip lines that are too long. */ 62958154Sbsd if (strchr(p, '\n') == NULL) { 6301573Srgrimes while ((ch = getc(hostf)) != '\n' && ch != EOF); 6311573Srgrimes continue; 6321573Srgrimes } 6339552Speter if (*p == '\n' || *p == '#') { 6349552Speter /* comment... */ 6359552Speter continue; 6369552Speter } 6371573Srgrimes while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { 63852859Sache *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p; 6391573Srgrimes p++; 6401573Srgrimes } 6411573Srgrimes if (*p == ' ' || *p == '\t') { 6421573Srgrimes *p++ = '\0'; 6431573Srgrimes while (*p == ' ' || *p == '\t') 6441573Srgrimes p++; 6451573Srgrimes user = p; 6461573Srgrimes while (*p != '\n' && *p != ' ' && 6471573Srgrimes *p != '\t' && *p != '\0') 6481573Srgrimes p++; 6491573Srgrimes } else 6501573Srgrimes user = p; 6511573Srgrimes *p = '\0'; 6527183Swpaul /* 6537183Swpaul * Do +/- and +@/-@ checking. This looks really nasty, 6547183Swpaul * but it matches SunOS's behavior so far as I can tell. 6557183Swpaul */ 6567183Swpaul switch(buf[0]) { 6577183Swpaul case '+': 6587183Swpaul if (!buf[1]) { /* '+' matches all hosts */ 6597183Swpaul hostok = 1; 6607183Swpaul break; 6617183Swpaul } 6627183Swpaul if (buf[1] == '@') /* match a host by netgroup */ 66310059Swpaul hostok = innetgr((char *)&buf[2], 66410059Swpaul (char *)&hname, NULL, ypdomain); 6657183Swpaul else /* match a host by addr */ 66669575Sume hostok = __icheckhost(raddr, salen, 66769575Sume (char *)&buf[1]); 6687183Swpaul break; 6697183Swpaul case '-': /* reject '-' hosts and all their users */ 6707183Swpaul if (buf[1] == '@') { 6717183Swpaul if (innetgr((char *)&buf[2], 67210059Swpaul (char *)&hname, NULL, ypdomain)) 6737183Swpaul return(-1); 6747183Swpaul } else { 67569575Sume if (__icheckhost(raddr, salen, 67669575Sume (char *)&buf[1])) 6777183Swpaul return(-1); 6787183Swpaul } 6797183Swpaul break; 6807183Swpaul default: /* if no '+' or '-', do a simple match */ 68169575Sume hostok = __icheckhost(raddr, salen, buf); 6827183Swpaul break; 6831573Srgrimes } 6847183Swpaul switch(*user) { 6857183Swpaul case '+': 6867183Swpaul if (!*(user+1)) { /* '+' matches all users */ 6877183Swpaul userok = 1; 6887183Swpaul break; 6897183Swpaul } 6907183Swpaul if (*(user+1) == '@') /* match a user by netgroup */ 6919978Swpaul userok = innetgr(user+2, NULL, ruser, ypdomain); 6927183Swpaul else /* match a user by direct specification */ 6937183Swpaul userok = !(strcmp(ruser, user+1)); 6947183Swpaul break; 6957183Swpaul case '-': /* if we matched a hostname, */ 6967183Swpaul if (hostok) { /* check for user field rejections */ 6977183Swpaul if (!*(user+1)) 6987183Swpaul return(-1); 6997183Swpaul if (*(user+1) == '@') { 7007183Swpaul if (innetgr(user+2, NULL, 7019978Swpaul ruser, ypdomain)) 7027183Swpaul return(-1); 7037183Swpaul } else { 7047183Swpaul if (!strcmp(ruser, user+1)) 7057183Swpaul return(-1); 7067183Swpaul } 7077183Swpaul } 7087183Swpaul break; 7097183Swpaul default: /* no rejections: try to match the user */ 7107183Swpaul if (hostok) 7117183Swpaul userok = !(strcmp(ruser,*user ? user : luser)); 7127183Swpaul break; 7137183Swpaul } 7147183Swpaul if (hostok && userok) 7157183Swpaul return(0); 7161573Srgrimes } 7171573Srgrimes return (-1); 7181573Srgrimes} 7191573Srgrimes 7201573Srgrimes/* 7211573Srgrimes * Returns "true" if match, 0 if no match. 72269575Sume * 72369575Sume * NI_WITHSCOPEID is useful for comparing sin6_scope_id portion 72469575Sume * if af == AF_INET6. 7251573Srgrimes */ 7261573Srgrimesstatic int 72769575Sume__icheckhost(raddr, salen, lhost) 72869575Sume const struct sockaddr *raddr; 72969575Sume socklen_t salen; 73069575Sume const char *lhost; 7311573Srgrimes{ 73269575Sume struct sockaddr_in sin; 73369575Sume struct sockaddr_in6 *sin6; 73469575Sume struct addrinfo hints, *res, *r; 73569575Sume int error; 73669575Sume char h1[NI_MAXHOST], h2[NI_MAXHOST]; 7371573Srgrimes 73869575Sume if (raddr->sa_family == AF_INET6) { 73969575Sume sin6 = (struct sockaddr_in6 *)raddr; 74069575Sume if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 74169575Sume memset(&sin, 0, sizeof(sin)); 74269575Sume sin.sin_family = AF_INET; 74369575Sume sin.sin_len = sizeof(struct sockaddr_in); 74469575Sume memcpy(&sin.sin_addr, &sin6->sin6_addr.s6_addr[12], 74569575Sume sizeof(sin.sin_addr)); 74669575Sume raddr = (struct sockaddr *)&sin; 74769575Sume salen = sin.sin_len; 74869575Sume } 74955918Sshin } 7501573Srgrimes 75169575Sume h1[0] = '\0'; 75269575Sume if (getnameinfo(raddr, salen, h1, sizeof(h1), NULL, 0, 75369575Sume NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 7541573Srgrimes return (0); 7551573Srgrimes 75669575Sume /* Resolve laddr into sockaddr */ 75769575Sume memset(&hints, 0, sizeof(hints)); 75869575Sume hints.ai_family = raddr->sa_family; 75969575Sume hints.ai_socktype = SOCK_DGRAM; /*XXX dummy*/ 76069575Sume res = NULL; 76169575Sume error = getaddrinfo(lhost, "0", &hints, &res); 76269575Sume if (error) 76369575Sume return (0); 76469575Sume 76569575Sume for (r = res; r ; r = r->ai_next) { 76669575Sume h2[0] = '\0'; 76769575Sume if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, sizeof(h2), 76869575Sume NULL, 0, NI_NUMERICHOST | NI_WITHSCOPEID) != 0) 76969575Sume continue; 77069575Sume if (strcmp(h1, h2) == 0) { 77169575Sume freeaddrinfo(res); 77269575Sume return (1); 77355918Sshin } 77469575Sume } 7751573Srgrimes 77669575Sume /* No match. */ 77769575Sume freeaddrinfo(res); 77869575Sume return (0); 7791573Srgrimes} 780