2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18 #include <afsconfig.h>
19 #include <afs/param.h>
23 #include <sys/types.h>
24 #define MAXHOSTNAMELEN 64 /* use BSD's, not sys/param's */
25 #define MAXPATHLEN 1024 /* from BSD */
26 #include <sys/ioctl.h> /* for SIOCSPGRP */
31 #include <sys/param.h>
37 #include <unistd.h> /* select() prototype */
38 #include <sys/types.h> /* fd_set on older platforms */
39 #include <sys/time.h> /* struct timeval, select() prototype */
41 # include <sys/select.h> /* fd_set on newer platforms */
43 #include <sys/signal.h>
44 #include <sys/socket.h>
46 #include <netinet/in.h>
49 #if defined(AFS_HPUX_ENV)
50 /* HPUX uses a different call to set[res][gu]ids: */
51 #define seteuid(newEuid) setresuid(-1, (newEuid), -1)
52 #define setegid(newEgid) setresgid(-1, (newEgid), -1)
53 #endif /* defined(AFS_HPUX_ENV) */
55 #include <sys/syslog.h>
56 # define DPRINTF(args) afs_dprintf args
62 static char prefix[] = "rcmd: ";
65 vsprintf(buf, argv[0], &argv[1]);
66 syslog(LOG_DEBUG, "%s %s\n", prefix, buf);
69 # define DPRINTF(args)
74 #ifdef AFS_HPUX102_ENV
76 rmcd(ahost, rport, locuser, remuser, cmd, fd2p)
79 const char *locuser, *remuser, *cmd;
83 rcmd(ahost, rport, locuser, remuser, cmd, fd2p, retry)
86 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
90 #if defined(AFS_LINUX_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
91 const char *locuser, *remuser, *cmd;
93 char *locuser, *remuser, *cmd;
101 int someSignals[100];
102 struct servent *sp, *getservbyport();
103 struct sockaddr_in sin, from;
105 int lport = IPPORT_RESERVED - 1;
109 memset(someSignals, 0, sizeof(someSignals));
110 someSignals[0] = 1 << (SIGURG - 1);
111 sigBlock = *((sigset_t *) someSignals);
114 hp = gethostbyname(*ahost); /* CAUTION: this uses global static */
115 /* storage. ANY OTHER CALLS to gethostbyname (including from within
116 * syslog, eg) will trash the contents of hp. BE CAREFUL! */
122 /* was: syslog(LOG_ERR, "rcmd: host=%s, rport=%d, luser=%s,ruser=%s,cmd=%s,fd2p=%s\n", *ahost,rport,locuser,remuser,cmd,fd2p)
123 * but that trashes hp... */
124 sigprocmask(SIG_BLOCK, &sigBlock, &oldset);
126 s = rresvport(&lport);
129 fprintf(stderr, "socket: All ports in use\n");
131 perror("rcmd: socket");
132 sigprocmask(SIG_SETMASK, &oldset, (sigset_t *) 0);
137 fcntl(s, F_SETOWN, pid);
139 /* since AIX has no F_SETOWN, we just do the ioctl */
140 (void)ioctl(s, SIOCSPGRP, &pid);
143 #if !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV)
144 fcntl(s, F_SETOWN, pid);
145 #endif /* !defined(AIX) */
147 sin.sin_family = hp->h_addrtype;
148 memcpy((caddr_t) & sin.sin_addr, hp->h_addr, hp->h_length);
149 sin.sin_port = rport;
150 /* attempt to remote authenticate first... */
151 sp = getservbyport((int)rport, "tcp");
155 switch (ta_rauth(s, sp->s_name, sin.sin_addr)) {
158 fprintf(stderr, "No remote authentication\n");
161 s = rresvport(&lport);
164 fprintf(stderr, "socket: All ports in use\n");
166 perror("rcmd: socket");
167 sigprocmask(SIG_SETMASK, &oldset, (sigset_t *) 0);
170 #if !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV)
171 fcntl(s, F_SETOWN, pid);
172 #endif /* !defined(AIX) */
178 fprintf(stderr, "Login incorrect.");
182 fprintf(stderr, "internal failure, ta_rauth\n");
186 fprintf(stderr, "Cannot connect to remote machine\n");
192 break; /* from for loop */
195 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
198 if (errno == EADDRINUSE) {
202 if (errno == ECONNREFUSED && timo <= 16) {
212 if (hp->h_addr_list[1] != NULL) {
215 fprintf(stderr, "connect to address %s: ",
216 inet_ntoa(sin.sin_addr));
220 memcpy((caddr_t) & sin.sin_addr, hp->h_addr_list[0],
222 fprintf(stderr, "Trying %s...\n", inet_ntoa(sin.sin_addr));
226 sigprocmask(SIG_SETMASK, &oldset, (sigset_t *) 0);
235 int s2 = rresvport(&lport), s3;
236 int len = sizeof(from);
242 (void)sprintf(num, "%d", lport);
243 if (write(s, num, strlen(num) + 1) != strlen(num) + 1) {
244 perror("write: setting up stderr");
256 if (select(maxfd + 1, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)) {
258 perror("select: setting up stderr");
261 "select: protocol failure in circuit setup.\n");
265 s3 = accept(s2, (struct sockaddr *)&from, &len);
273 from.sin_port = ntohs((u_short) from.sin_port);
274 if (from.sin_family != AF_INET || from.sin_port >= IPPORT_RESERVED
275 || from.sin_port < IPPORT_RESERVED / 2) {
276 fprintf(stderr, "socket: protocol failure in circuit setup.\n");
280 (void)write(s, locuser, strlen(locuser) + 1);
281 (void)write(s, remuser, strlen(remuser) + 1);
282 (void)write(s, cmd, strlen(cmd) + 1);
284 if (read(s, &c, 1) < 0) {
289 while (read(s, &c, 1) == 1) {
290 (void)write(2, &c, 1);
296 sigprocmask(SIG_SETMASK, &oldset, (sigset_t *) 0);
303 sigprocmask(SIG_SETMASK, &oldset, (sigset_t *) 0);
307 #ifndef AFS_AIX32_ENV
311 struct sockaddr_in sin;
314 sin.sin_family = AF_INET;
315 sin.sin_addr.s_addr = INADDR_ANY;
316 s = socket(AF_INET, SOCK_STREAM, 0);
320 sin.sin_port = htons((u_short) * alport);
321 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
323 if (errno != EADDRINUSE) {
328 if (*alport == IPPORT_RESERVED / 2) {
330 errno = EAGAIN; /* close */
337 int _check_rhosts_file = 1;
339 #if defined(AFS_HPUX102_ENV) || defined(AFS_LINUX_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
340 ruserok(rhost, superuser, ruser, luser)
343 const char *ruser, *luser;
345 ruserok(rhost, superuser, ruser, luser)
352 char fhost[MAXHOSTNAMELEN];
357 int group_list_size = -1;
358 gid_t groups[NGROUPS_MAX];
364 baselen = sp - rhost;
367 *p++ = isupper(*sp) ? tolower(*sp++) : *sp++;
371 hostf = superuser ? (FILE *) 0 : fopen("/etc/hosts.equiv", "r");
374 if (!_validuser(hostf, fhost, luser, ruser, baselen)) {
380 if (first == 1 && (_check_rhosts_file || superuser)) {
382 struct passwd *pwd, *getpwnam();
383 char pbuf[MAXPATHLEN];
388 group_list_size = getgroups(NGROUPS_MAX, groups);
389 if ((pwd = getpwnam(luser)) == NULL)
391 if (setegid(pwd->pw_gid) >= 0)
392 (void)initgroups(luser, pwd->pw_gid);
393 (void)seteuid(pwd->pw_uid);
394 (void)strcpy(pbuf, pwd->pw_dir);
395 (void)strcat(pbuf, "/.rhosts");
396 if ((hostf = fopen(pbuf, "r")) == NULL)
399 * if owned by someone other than user or root or if
400 * writable by anyone but the owner, quit
402 if (fstat(fileno(hostf), &sbuf) || sbuf.st_uid
403 && sbuf.st_uid != pwd->pw_uid || sbuf.st_mode & 022) {
413 if (group_list_size >= 0)
414 (void)setgroups(group_list_size, groups);
419 /* don't make static, used by lpd(8) */
420 _validuser(hostf, rhost, luser, ruser, baselen)
421 char *rhost, *luser, *ruser;
426 char ahost[MAXHOSTNAMELEN * 4];
429 #include <arpa/nameser.h>
430 int hostmatch, usermatch;
431 char domain[MAXDNAME], *dp;
434 if (getdomainname(domain, sizeof(domain)) == 0)
437 while (fgets(ahost, sizeof(ahost), hostf)) {
439 hostmatch = usermatch = 0;
441 if (*p == '#' || *p == '\n') /* ignore comments and blanks */
443 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
445 if (*p == ' ' || *p == '\t') {
447 while (*p == ' ' || *p == '\t')
450 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
457 * +@<name> - group <name> allowed
458 * -@<name> - group <name> disallowed
459 * -<name> - host <name> disallowed
461 if (ahost[0] == '+' && ahost[1] == 0)
463 else if (ahost[0] == '+' && ahost[1] == '@')
464 hostmatch = innetgr(ahost + 2, rhost, NULL, dp);
465 else if (ahost[0] == '-' && ahost[1] == '@') {
466 if (innetgr(ahost + 2, rhost, NULL, dp))
468 } else if (ahost[0] == '-') {
469 if (_checkhost(rhost, ahost + 1, baselen))
472 hostmatch = _checkhost(rhost, ahost, baselen);
474 if (user[0] == '+' && user[1] == 0)
476 else if (user[0] == '+' && user[1] == '@')
477 usermatch = innetgr(user + 2, NULL, ruser, dp);
478 else if (user[0] == '-' && user[1] == '@') {
479 if (hostmatch && innetgr(user + 2, NULL, ruser, dp))
481 } else if (user[0] == '-') {
482 if (hostmatch && !strcmp(user + 1, ruser))
485 usermatch = !strcmp(user, ruser);
487 usermatch = !strcmp(ruser, luser);
488 if (hostmatch && usermatch)
492 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
493 *p = isupper(*p) ? tolower(*p) : *p;
496 if (*p == ' ' || *p == '\t') {
498 while (*p == ' ' || *p == '\t')
501 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0')
506 if (_checkhost(rhost, ahost, baselen)
507 && !strcmp(ruser, *user ? user : luser)) {
516 _checkhost(rhost, lhost, len)
520 static char ldomain[MAXHOSTNAMELEN + 1];
521 static char *domainp = NULL;
522 static int nodomain = 0;
530 * check for ip address and do a lookup to convert to hostname
532 if (isinet_addr(lhost) && (addr = inet_addr(lhost)) != -1
533 && (hp = gethostbyaddr(&addr, sizeof(addr), AF_INET)))
539 /* see if hostname from file has a domain name */
540 for (cp = lhost; *cp; ++cp) {
547 return (!strcmp(rhost, lhost));
549 if (strncmp(rhost, lhost, len))
551 if (!strcmp(rhost, lhost))
554 if (*(lhost + len) != '\0' && *(rhost + len) != '\0')
556 if (*(lhost + len) != '\0')
562 if (gethostname(ldomain, sizeof(ldomain)) == -1) {
566 ldomain[MAXHOSTNAMELEN] = '\0';
567 if ((domainp = strchr(ldomain, '.')) == NULL) {
571 for (cp = ++domainp; *cp; ++cp)
575 return (!strcmp(domainp, rhost + len + 1));