10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
90Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
100Sstevel@tonic-gate */
110Sstevel@tonic-gate
12*722Smuffin #pragma ident "%Z%%M% %I% %E% SMI"
130Sstevel@tonic-gate
140Sstevel@tonic-gate #include <stdio.h>
150Sstevel@tonic-gate #include <ctype.h>
160Sstevel@tonic-gate #include <pwd.h>
170Sstevel@tonic-gate #include <sys/param.h>
180Sstevel@tonic-gate #include <sys/file.h>
190Sstevel@tonic-gate #include <sys/signal.h>
200Sstevel@tonic-gate #include <sys/socket.h>
210Sstevel@tonic-gate #include <sys/stat.h>
220Sstevel@tonic-gate
230Sstevel@tonic-gate #include <netinet/in.h>
240Sstevel@tonic-gate
250Sstevel@tonic-gate #include <netdb.h>
260Sstevel@tonic-gate #include <errno.h>
270Sstevel@tonic-gate
28*722Smuffin #include <strings.h>
29*722Smuffin
300Sstevel@tonic-gate static char *domain;
310Sstevel@tonic-gate
320Sstevel@tonic-gate int
rcmd(char ** ahost,unsigned short rport,const char * locuser,const char * remuser,const char * cmd,int * fd2p)330Sstevel@tonic-gate rcmd(
340Sstevel@tonic-gate char **ahost,
350Sstevel@tonic-gate unsigned short rport,
360Sstevel@tonic-gate const char *locuser,
370Sstevel@tonic-gate const char *remuser,
380Sstevel@tonic-gate const char *cmd,
390Sstevel@tonic-gate int *fd2p)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate int s, timo = 1, pid, oldmask, retval;
420Sstevel@tonic-gate struct sockaddr_in sin, from;
430Sstevel@tonic-gate char c;
440Sstevel@tonic-gate int lport = IPPORT_RESERVED - 1;
450Sstevel@tonic-gate struct hostent *hp;
460Sstevel@tonic-gate
470Sstevel@tonic-gate pid = getpid();
480Sstevel@tonic-gate hp = gethostbyname(*ahost);
490Sstevel@tonic-gate if (hp == 0) {
500Sstevel@tonic-gate fprintf(stderr, "%s: unknown host\n", *ahost);
510Sstevel@tonic-gate return (-1);
520Sstevel@tonic-gate }
530Sstevel@tonic-gate *ahost = hp->h_name;
540Sstevel@tonic-gate oldmask = sigblock(sigmask(SIGURG));
550Sstevel@tonic-gate for (;;) {
560Sstevel@tonic-gate s = rresvport(&lport);
570Sstevel@tonic-gate if (s < 0) {
580Sstevel@tonic-gate if (errno == EAGAIN)
590Sstevel@tonic-gate fprintf(stderr, "socket: All ports in use\n");
600Sstevel@tonic-gate else
610Sstevel@tonic-gate perror("rcmd: socket");
620Sstevel@tonic-gate sigsetmask(oldmask);
630Sstevel@tonic-gate return (-1);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate fcntl(s, F_SETOWN, pid);
660Sstevel@tonic-gate sin.sin_family = hp->h_addrtype;
670Sstevel@tonic-gate bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr, hp->h_length);
680Sstevel@tonic-gate sin.sin_port = rport;
690Sstevel@tonic-gate if (connect(s, &sin, sizeof (sin)) >= 0)
700Sstevel@tonic-gate break;
710Sstevel@tonic-gate (void) close(s);
720Sstevel@tonic-gate if (errno == EADDRINUSE) {
730Sstevel@tonic-gate lport--;
740Sstevel@tonic-gate continue;
750Sstevel@tonic-gate }
760Sstevel@tonic-gate if (errno == ECONNREFUSED && timo <= 16) {
770Sstevel@tonic-gate sleep(timo);
780Sstevel@tonic-gate timo *= 2;
790Sstevel@tonic-gate continue;
800Sstevel@tonic-gate }
810Sstevel@tonic-gate if (hp->h_addr_list[1] != NULL) {
820Sstevel@tonic-gate int oerrno = errno;
830Sstevel@tonic-gate
840Sstevel@tonic-gate fprintf(stderr,
850Sstevel@tonic-gate "connect to address %s: ", inet_ntoa(sin.sin_addr));
860Sstevel@tonic-gate errno = oerrno;
870Sstevel@tonic-gate perror(0);
880Sstevel@tonic-gate hp->h_addr_list++;
890Sstevel@tonic-gate bcopy(hp->h_addr_list[0], (caddr_t)&sin.sin_addr,
900Sstevel@tonic-gate hp->h_length);
910Sstevel@tonic-gate fprintf(stderr, "Trying %s...\n",
920Sstevel@tonic-gate inet_ntoa(sin.sin_addr));
930Sstevel@tonic-gate continue;
940Sstevel@tonic-gate }
950Sstevel@tonic-gate perror(hp->h_name);
960Sstevel@tonic-gate sigsetmask(oldmask);
970Sstevel@tonic-gate return (-1);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate lport--;
1000Sstevel@tonic-gate if (fd2p == 0) {
1010Sstevel@tonic-gate write(s, "", 1);
1020Sstevel@tonic-gate lport = 0;
1030Sstevel@tonic-gate } else {
1040Sstevel@tonic-gate char num[8];
1050Sstevel@tonic-gate int s2 = rresvport(&lport), s3;
1060Sstevel@tonic-gate int len = sizeof (from);
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (s2 < 0)
1090Sstevel@tonic-gate goto bad;
1100Sstevel@tonic-gate listen(s2, 1);
1110Sstevel@tonic-gate (void) sprintf(num, "%d", lport);
1120Sstevel@tonic-gate if (write(s, num, strlen(num)+1) != strlen(num)+1) {
1130Sstevel@tonic-gate perror("write: setting up stderr");
1140Sstevel@tonic-gate (void) close(s2);
1150Sstevel@tonic-gate goto bad;
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate s3 = accept(s2, &from, &len);
1180Sstevel@tonic-gate (void) close(s2);
1190Sstevel@tonic-gate if (s3 < 0) {
1200Sstevel@tonic-gate perror("accept");
1210Sstevel@tonic-gate lport = 0;
1220Sstevel@tonic-gate goto bad;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate *fd2p = s3;
1250Sstevel@tonic-gate from.sin_port = ntohs((u_short)from.sin_port);
1260Sstevel@tonic-gate if (from.sin_family != AF_INET ||
1270Sstevel@tonic-gate from.sin_port >= IPPORT_RESERVED) {
1280Sstevel@tonic-gate fprintf(stderr,
1290Sstevel@tonic-gate "socket: protocol failure in circuit setup.\n");
1300Sstevel@tonic-gate goto bad2;
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate (void) write(s, locuser, strlen(locuser)+1);
1340Sstevel@tonic-gate (void) write(s, remuser, strlen(remuser)+1);
1350Sstevel@tonic-gate (void) write(s, cmd, strlen(cmd)+1);
1360Sstevel@tonic-gate retval = read(s, &c, 1);
1370Sstevel@tonic-gate if (retval != 1) {
1380Sstevel@tonic-gate if (retval == 0) {
1390Sstevel@tonic-gate fprintf(stderr,
1400Sstevel@tonic-gate "Protocol error, %s closed connection\n", *ahost);
1410Sstevel@tonic-gate } else if (retval < 0) {
1420Sstevel@tonic-gate perror(*ahost);
1430Sstevel@tonic-gate } else {
1440Sstevel@tonic-gate fprintf(stderr,
1450Sstevel@tonic-gate "Protocol error, %s sent %d bytes\n", *ahost, retval);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate goto bad2;
1480Sstevel@tonic-gate }
1490Sstevel@tonic-gate if (c != 0) {
1500Sstevel@tonic-gate while (read(s, &c, 1) == 1) {
1510Sstevel@tonic-gate (void) write(2, &c, 1);
1520Sstevel@tonic-gate if (c == '\n')
1530Sstevel@tonic-gate break;
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate goto bad2;
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate sigsetmask(oldmask);
1580Sstevel@tonic-gate return (s);
1590Sstevel@tonic-gate bad2:
1600Sstevel@tonic-gate if (lport)
1610Sstevel@tonic-gate (void) close(*fd2p);
1620Sstevel@tonic-gate bad:
1630Sstevel@tonic-gate (void) close(s);
1640Sstevel@tonic-gate sigsetmask(oldmask);
1650Sstevel@tonic-gate return (-1);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate int
rresvport(int * alport)169*722Smuffin rresvport(int *alport)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate struct sockaddr_in sin;
1720Sstevel@tonic-gate int s;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate sin.sin_family = AF_INET;
1750Sstevel@tonic-gate sin.sin_addr.s_addr = INADDR_ANY;
1760Sstevel@tonic-gate s = socket(AF_INET, SOCK_STREAM, 0);
1770Sstevel@tonic-gate if (s < 0)
1780Sstevel@tonic-gate return (-1);
1790Sstevel@tonic-gate for (;;) {
1800Sstevel@tonic-gate sin.sin_port = htons((u_short)*alport);
1810Sstevel@tonic-gate if (bind(s, (caddr_t)&sin, sizeof (sin)) >= 0)
1820Sstevel@tonic-gate return (s);
1830Sstevel@tonic-gate if (errno != EADDRINUSE) {
1840Sstevel@tonic-gate (void) close(s);
1850Sstevel@tonic-gate return (-1);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate (*alport)--;
1880Sstevel@tonic-gate if (*alport == IPPORT_RESERVED/2) {
1890Sstevel@tonic-gate (void) close(s);
1900Sstevel@tonic-gate errno = EAGAIN; /* close */
1910Sstevel@tonic-gate return (-1);
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate }
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate int
ruserok(const char * rhost,int superuser,const char * ruser,const char * luser)1970Sstevel@tonic-gate ruserok(
1980Sstevel@tonic-gate const char *rhost,
1990Sstevel@tonic-gate int superuser,
2000Sstevel@tonic-gate const char *ruser,
2010Sstevel@tonic-gate const char *luser)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate FILE *hostf;
2040Sstevel@tonic-gate char fhost[MAXHOSTNAMELEN];
205*722Smuffin const char *sp;
206*722Smuffin char *p;
2070Sstevel@tonic-gate int baselen = -1;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate struct stat sbuf;
2100Sstevel@tonic-gate struct passwd *pwd;
2110Sstevel@tonic-gate char pbuf[MAXPATHLEN];
2120Sstevel@tonic-gate int euid = -1;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate sp = rhost;
2150Sstevel@tonic-gate p = fhost;
2160Sstevel@tonic-gate while (*sp) {
2170Sstevel@tonic-gate if (*sp == '.') {
2180Sstevel@tonic-gate if (baselen == -1)
2190Sstevel@tonic-gate baselen = sp - rhost;
2200Sstevel@tonic-gate *p++ = *sp++;
2210Sstevel@tonic-gate } else {
2220Sstevel@tonic-gate *p++ = isupper(*sp) ? tolower(*sp++) : *sp++;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate *p = '\0';
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* check /etc/hosts.equiv */
2280Sstevel@tonic-gate if (!superuser) {
2290Sstevel@tonic-gate if ((hostf = fopen("/etc/hosts.equiv", "r")) != NULL) {
2300Sstevel@tonic-gate if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
2310Sstevel@tonic-gate (void) fclose(hostf);
2320Sstevel@tonic-gate return(0);
2330Sstevel@tonic-gate }
2340Sstevel@tonic-gate (void) fclose(hostf);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate }
2370Sstevel@tonic-gate
2380Sstevel@tonic-gate /* check ~/.rhosts */
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate if ((pwd = getpwnam(luser)) == NULL)
2410Sstevel@tonic-gate return(-1);
2420Sstevel@tonic-gate (void)strcpy(pbuf, pwd->pw_dir);
2430Sstevel@tonic-gate (void)strcat(pbuf, "/.rhosts");
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /*
2460Sstevel@tonic-gate * Read .rhosts as the local user to avoid NFS mapping the root uid
2470Sstevel@tonic-gate * to something that can't read .rhosts.
2480Sstevel@tonic-gate */
2490Sstevel@tonic-gate euid = geteuid();
2500Sstevel@tonic-gate (void) seteuid (pwd->pw_uid);
2510Sstevel@tonic-gate if ((hostf = fopen(pbuf, "r")) == NULL) {
2520Sstevel@tonic-gate if (euid != -1)
2530Sstevel@tonic-gate (void) seteuid (euid);
2540Sstevel@tonic-gate return(-1);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate (void)fstat(fileno(hostf), &sbuf);
2570Sstevel@tonic-gate if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
2580Sstevel@tonic-gate fclose(hostf);
2590Sstevel@tonic-gate if (euid != -1)
2600Sstevel@tonic-gate (void) seteuid (euid);
2610Sstevel@tonic-gate return(-1);
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
2650Sstevel@tonic-gate (void) fclose(hostf);
2660Sstevel@tonic-gate if (euid != -1)
2670Sstevel@tonic-gate (void) seteuid (euid);
2680Sstevel@tonic-gate return(0);
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate (void) fclose(hostf);
2720Sstevel@tonic-gate if (euid != -1)
2730Sstevel@tonic-gate (void) seteuid (euid);
2740Sstevel@tonic-gate return (-1);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate
277*722Smuffin int
_validuser(FILE * hostf,char * rhost,char * luser,char * ruser,int baselen)278*722Smuffin _validuser(FILE *hostf, char *rhost, char *luser, char *ruser, int baselen)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate char *user;
2810Sstevel@tonic-gate char ahost[MAXHOSTNAMELEN];
2820Sstevel@tonic-gate int hostmatch, usermatch;
283*722Smuffin char *p;
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if (domain == NULL) {
2860Sstevel@tonic-gate (void) yp_get_default_domain(&domain);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate while (fgets(ahost, sizeof (ahost), hostf)) {
2890Sstevel@tonic-gate hostmatch = usermatch = 0; /* bugid fix 1033104 */
2900Sstevel@tonic-gate p = ahost;
2910Sstevel@tonic-gate while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
2920Sstevel@tonic-gate *p = isupper(*p) ? tolower(*p) : *p;
2930Sstevel@tonic-gate p++;
2940Sstevel@tonic-gate }
2950Sstevel@tonic-gate if (*p == ' ' || *p == '\t') {
2960Sstevel@tonic-gate *p++ = '\0';
2970Sstevel@tonic-gate while (*p == ' ' || *p == '\t')
2980Sstevel@tonic-gate p++;
2990Sstevel@tonic-gate user = p;
3000Sstevel@tonic-gate while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
3010Sstevel@tonic-gate p++;
3020Sstevel@tonic-gate } else
3030Sstevel@tonic-gate user = p;
3040Sstevel@tonic-gate *p = '\0';
3050Sstevel@tonic-gate if (ahost[0] == '+' && ahost[1] == 0)
3060Sstevel@tonic-gate hostmatch = 1;
3070Sstevel@tonic-gate else if (ahost[0] == '+' && ahost[1] == '@')
3080Sstevel@tonic-gate hostmatch = innetgr(ahost + 2, rhost,
3090Sstevel@tonic-gate NULL, domain);
3100Sstevel@tonic-gate else if (ahost[0] == '-' && ahost[1] == '@') {
3110Sstevel@tonic-gate if (innetgr(ahost + 2, rhost, NULL, domain))
3120Sstevel@tonic-gate break;
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate else if (ahost[0] == '-') {
3150Sstevel@tonic-gate if (_checkhost(rhost, ahost+1, baselen))
3160Sstevel@tonic-gate break;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate else
3190Sstevel@tonic-gate hostmatch = _checkhost(rhost, ahost, baselen);
3200Sstevel@tonic-gate if (user[0]) {
3210Sstevel@tonic-gate if (user[0] == '+' && user[1] == 0)
3220Sstevel@tonic-gate usermatch = 1;
3230Sstevel@tonic-gate else if (user[0] == '+' && user[1] == '@')
3240Sstevel@tonic-gate usermatch = innetgr(user+2, NULL,
3250Sstevel@tonic-gate ruser, domain);
3260Sstevel@tonic-gate else if (user[0] == '-' && user[1] == '@') {
3270Sstevel@tonic-gate if (hostmatch && innetgr(user+2, NULL,
3280Sstevel@tonic-gate ruser, domain))
3290Sstevel@tonic-gate break;
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate else if (user[0] == '-') {
3320Sstevel@tonic-gate if (hostmatch && !strcmp(user+1, ruser))
3330Sstevel@tonic-gate break;
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate else
3360Sstevel@tonic-gate usermatch = !strcmp(user, ruser);
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate else
3390Sstevel@tonic-gate usermatch = !strcmp(ruser, luser);
3400Sstevel@tonic-gate if (hostmatch && usermatch)
3410Sstevel@tonic-gate return (0);
3420Sstevel@tonic-gate }
3430Sstevel@tonic-gate return (-1);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate
346*722Smuffin int
_checkhost(char * rhost,char * lhost,int len)347*722Smuffin _checkhost(char *rhost, char *lhost, int len)
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate static char *ldomain;
3500Sstevel@tonic-gate static char *domainp;
3510Sstevel@tonic-gate static int nodomain;
352*722Smuffin char *cp;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate if (ldomain == NULL) {
3550Sstevel@tonic-gate ldomain = (char *)malloc(MAXHOSTNAMELEN+1);
3560Sstevel@tonic-gate if (ldomain == 0)
3570Sstevel@tonic-gate return (0);
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate
3600Sstevel@tonic-gate if (len == -1)
3610Sstevel@tonic-gate return(!strcmp(rhost, lhost));
3620Sstevel@tonic-gate if (strncmp(rhost, lhost, len))
3630Sstevel@tonic-gate return(0);
3640Sstevel@tonic-gate if (!strcmp(rhost, lhost))
3650Sstevel@tonic-gate return(1);
3660Sstevel@tonic-gate if (*(lhost + len) != '\0')
3670Sstevel@tonic-gate return(0);
3680Sstevel@tonic-gate if (nodomain)
3690Sstevel@tonic-gate return(0);
3700Sstevel@tonic-gate if (!domainp) {
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate * "domainp" points after the first dot in the host name
3730Sstevel@tonic-gate */
3740Sstevel@tonic-gate if (gethostname(ldomain, MAXHOSTNAMELEN) == -1) {
3750Sstevel@tonic-gate nodomain = 1;
3760Sstevel@tonic-gate return(0);
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate ldomain[MAXHOSTNAMELEN] = NULL;
3790Sstevel@tonic-gate if ((domainp = index(ldomain, '.')) == (char *)NULL) {
3800Sstevel@tonic-gate nodomain = 1;
3810Sstevel@tonic-gate return(0);
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate domainp++;
3840Sstevel@tonic-gate cp = domainp;
3850Sstevel@tonic-gate while (*cp) {
3860Sstevel@tonic-gate *cp = isupper(*cp) ? tolower(*cp) : *cp;
3870Sstevel@tonic-gate cp++;
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate return(!strcmp(domainp, rhost + len +1));
3910Sstevel@tonic-gate }
392