update from main archive 961203
[platform/upstream/glibc.git] / inet / rcmd.c
1 /*
2  * Copyright (c) 1983, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)rcmd.c      8.3 (Berkeley) 3/26/94";
36 #endif /* LIBC_SCCS and not lint */
37
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <alloca.h>
46 #include <signal.h>
47 #include <fcntl.h>
48 #include <netdb.h>
49 #include <unistd.h>
50 #include <pwd.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <ctype.h>
54 #include <string.h>
55
56
57 int     __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
58 static int __icheckhost __P((u_int32_t, char *));
59
60 int
61 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
62         char **ahost;
63         u_short rport;
64         const char *locuser, *remuser, *cmd;
65         int *fd2p;
66 {
67         struct hostent hostbuf, *hp;
68         size_t hstbuflen;
69         char *tmphstbuf;
70         struct sockaddr_in sin, from;
71         fd_set reads;
72         int32_t oldmask;
73         pid_t pid;
74         int s, lport, timo;
75         char c;
76         int herr;
77
78         pid = getpid();
79
80         hstbuflen = 1024;
81         tmphstbuf = __alloca (hstbuflen);
82         while (__gethostbyname_r (*ahost, &hostbuf, tmphstbuf, hstbuflen,
83                                   &hp, &herr) < 0)
84           if (herr != NETDB_INTERNAL || errno != ERANGE)
85             {
86               herror(*ahost);
87               return -1;
88             }
89           else
90             {
91               /* Enlarge the buffer.  */
92               hstbuflen *= 2;
93               tmphstbuf = __alloca (hstbuflen);
94             }
95
96         *ahost = hp->h_name;
97         oldmask = sigblock(sigmask(SIGURG));
98         for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
99                 s = rresvport(&lport);
100                 if (s < 0) {
101                         if (errno == EAGAIN)
102                                 (void)fprintf(stderr,
103                                     _("rcmd: socket: All ports in use\n"));
104                         else
105                                 (void)fprintf(stderr, "rcmd: socket: %m\n");
106                         sigsetmask(oldmask);
107                         return (-1);
108                 }
109                 fcntl(s, F_SETOWN, pid);
110                 sin.sin_family = hp->h_addrtype;
111                 bcopy(hp->h_addr_list[0], &sin.sin_addr,
112                       MIN (sizeof (sin.sin_addr), hp->h_length));
113                 sin.sin_port = rport;
114                 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
115                         break;
116                 (void)close(s);
117                 if (errno == EADDRINUSE) {
118                         lport--;
119                         continue;
120                 }
121                 if (errno == ECONNREFUSED && timo <= 16) {
122                         (void)sleep(timo);
123                         timo *= 2;
124                         continue;
125                 }
126                 if (hp->h_addr_list[1] != NULL) {
127                         int oerrno = errno;
128
129                         (void)fprintf(stderr, _("connect to address %s: "),
130                             inet_ntoa(sin.sin_addr));
131                         __set_errno (oerrno);
132                         perror(0);
133                         hp->h_addr_list++;
134                         bcopy(hp->h_addr_list[0], &sin.sin_addr,
135                               MIN (sizeof (sin.sin_addr), hp->h_length));
136                         (void)fprintf(stderr, _("Trying %s...\n"),
137                             inet_ntoa(sin.sin_addr));
138                         continue;
139                 }
140                 (void)fprintf(stderr, "%s: %m\n", hp->h_name);
141                 sigsetmask(oldmask);
142                 return (-1);
143         }
144         lport--;
145         if (fd2p == 0) {
146                 write(s, "", 1);
147                 lport = 0;
148         } else {
149                 char num[8];
150                 int s2 = rresvport(&lport), s3;
151                 int len = sizeof(from);
152
153                 if (s2 < 0)
154                         goto bad;
155                 listen(s2, 1);
156                 (void)snprintf(num, sizeof(num), "%d", lport);
157                 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
158                         (void)fprintf(stderr,
159                             _("rcmd: write (setting up stderr): %m\n"));
160                         (void)close(s2);
161                         goto bad;
162                 }
163                 FD_ZERO(&reads);
164                 FD_SET(s, &reads);
165                 FD_SET(s2, &reads);
166                 __set_errno (0);
167                 if (select(1 + (s > s2 ? s : s2), &reads, 0, 0, 0) < 1 ||
168                     !FD_ISSET(s2, &reads)) {
169                         if (errno != 0)
170                                 (void)fprintf(stderr,
171                                   _("rcmd: select (setting up stderr): %m\n"));
172                         else
173                                 (void)fprintf(stderr,
174                              _("select: protocol failure in circuit setup\n"));
175                         (void)close(s2);
176                         goto bad;
177                 }
178                 s3 = accept(s2, (struct sockaddr *)&from, &len);
179                 (void)close(s2);
180                 if (s3 < 0) {
181                         (void)fprintf(stderr,
182                             "rcmd: accept: %m\n");
183                         lport = 0;
184                         goto bad;
185                 }
186                 *fd2p = s3;
187                 from.sin_port = ntohs((u_short)from.sin_port);
188                 if (from.sin_family != AF_INET ||
189                     from.sin_port >= IPPORT_RESERVED ||
190                     from.sin_port < IPPORT_RESERVED / 2) {
191                         (void)fprintf(stderr,
192                             _("socket: protocol failure in circuit setup\n"));
193                         goto bad2;
194                 }
195         }
196         (void)write(s, locuser, strlen(locuser)+1);
197         (void)write(s, remuser, strlen(remuser)+1);
198         (void)write(s, cmd, strlen(cmd)+1);
199         if (read(s, &c, 1) != 1) {
200                 (void)fprintf(stderr,
201                     "rcmd: %s: %m\n", *ahost);
202                 goto bad2;
203         }
204         if (c != 0) {
205                 while (read(s, &c, 1) == 1) {
206                         (void)write(STDERR_FILENO, &c, 1);
207                         if (c == '\n')
208                                 break;
209                 }
210                 goto bad2;
211         }
212         sigsetmask(oldmask);
213         return (s);
214 bad2:
215         if (lport)
216                 (void)close(*fd2p);
217 bad:
218         (void)close(s);
219         sigsetmask(oldmask);
220         return (-1);
221 }
222
223 int
224 rresvport(alport)
225         int *alport;
226 {
227         struct sockaddr_in sin;
228         int s;
229
230         sin.sin_family = AF_INET;
231         sin.sin_addr.s_addr = INADDR_ANY;
232         s = socket(AF_INET, SOCK_STREAM, 0);
233         if (s < 0)
234                 return (-1);
235         for (;;) {
236                 sin.sin_port = htons((u_short)*alport);
237                 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
238                         return (s);
239                 if (errno != EADDRINUSE) {
240                         (void)close(s);
241                         return (-1);
242                 }
243                 (*alport)--;
244                 if (*alport == IPPORT_RESERVED/2) {
245                         (void)close(s);
246                         __set_errno (EAGAIN);           /* close */
247                         return (-1);
248                 }
249         }
250 }
251
252 int     __check_rhosts_file = 1;
253 char    *__rcmd_errstr;
254
255 int
256 ruserok(rhost, superuser, ruser, luser)
257         const char *rhost, *ruser, *luser;
258         int superuser;
259 {
260         struct hostent hostbuf, *hp;
261         size_t buflen;
262         char *buffer;
263         u_int32_t addr;
264         char **ap;
265         int herr;
266
267         buflen = 1024;
268         buffer = __alloca (buflen);
269
270         while (__gethostbyname_r (rhost, &hostbuf, buffer, buflen, &hp, &herr)
271                < 0)
272           if (herr != NETDB_INTERNAL || errno != ERANGE)
273             return -1;
274           else
275             {
276               /* Enlarge the buffer.  */
277               buflen *= 2;
278               buffer = __alloca (buflen);
279             }
280
281         for (ap = hp->h_addr_list; *ap; ++ap) {
282                 bcopy(*ap, &addr, sizeof(addr));
283                 if (iruserok(addr, superuser, ruser, luser) == 0)
284                         return (0);
285         }
286         return (-1);
287 }
288
289 /*
290  * New .rhosts strategy: We are passed an ip address. We spin through
291  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
292  * has ip addresses, we don't have to trust a nameserver.  When it
293  * contains hostnames, we spin through the list of addresses the nameserver
294  * gives us and look for a match.
295  *
296  * Returns 0 if ok, -1 if not ok.
297  */
298 int
299 iruserok(raddr, superuser, ruser, luser)
300         u_int32_t raddr;
301         int superuser;
302         const char *ruser, *luser;
303 {
304         register char *cp;
305         struct stat sbuf;
306         struct passwd pwdbuf, *pwd;
307         FILE *hostf;
308         uid_t uid;
309         int first;
310
311         first = 1;
312         hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
313 again:
314         if (hostf) {
315                 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
316                         (void)fclose(hostf);
317                         return (0);
318                 }
319                 (void)fclose(hostf);
320         }
321         if (first == 1 && (__check_rhosts_file || superuser)) {
322                 char *pbuf;
323                 size_t dirlen;
324                 size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
325                 char buffer = __alloca (buflen);
326
327                 first = 0;
328                 if (getpwnam_r (luser, &pwdbuf, buffer, buflen, &pwd) < 0)
329                         return -1;
330
331                 dirlen = strlen (pwd->pw_dir);
332                 pbuf = alloca (dirlen + sizeof "/.rhosts");
333                 memcpy (pbuf, pwd->pw_dir, dirlen);
334                 memcpy (pbuf + dirlen, "/.rhosts", sizeof "/.rhosts");
335
336                 /*
337                  * Change effective uid while opening .rhosts.  If root and
338                  * reading an NFS mounted file system, can't read files that
339                  * are protected read/write owner only.
340                  */
341                 uid = geteuid();
342                 (void)seteuid(pwd->pw_uid);
343                 hostf = fopen(pbuf, "r");
344                 (void)seteuid(uid);
345
346                 if (hostf == NULL)
347                         return (-1);
348                 /*
349                  * If not a regular file, or is owned by someone other than
350                  * user or root or if writeable by anyone but the owner, quit.
351                  */
352                 cp = NULL;
353                 if (lstat(pbuf, &sbuf) < 0)
354                         cp = _(".rhosts lstat failed");
355                 else if (!S_ISREG(sbuf.st_mode))
356                         cp = _(".rhosts not regular file");
357                 else if (fstat(fileno(hostf), &sbuf) < 0)
358                         cp = _(".rhosts fstat failed");
359                 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
360                         cp = _("bad .rhosts owner");
361                 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
362                         cp = _(".rhosts writeable by other than owner");
363                 /* If there were any problems, quit. */
364                 if (cp) {
365                         __rcmd_errstr = cp;
366                         (void)fclose(hostf);
367                         return (-1);
368                 }
369                 goto again;
370         }
371         return (-1);
372 }
373
374 /*
375  * XXX
376  * Don't make static, used by lpd(8).
377  *
378  * Returns 0 if ok, -1 if not ok.
379  */
380 int
381 __ivaliduser(hostf, raddr, luser, ruser)
382         FILE *hostf;
383         u_int32_t raddr;
384         const char *luser, *ruser;
385 {
386         register char *user, *p;
387         int ch;
388         char *buf = NULL;
389         size_t bufsize = 0;
390         ssize_t nread;
391
392         while ((nread = getline (&buf, &bufsize, hostf)) > 0) {
393                 buf[bufsize - 1] = '\0'; /* Make sure it's terminated.  */
394                 p = buf;
395                 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
396                         *p = isupper(*p) ? tolower(*p) : *p;
397                         p++;
398                 }
399                 if (*p == ' ' || *p == '\t') {
400                         *p++ = '\0';
401                         while (*p == ' ' || *p == '\t')
402                                 p++;
403                         user = p;
404                         while (*p != '\n' && *p != ' ' &&
405                             *p != '\t' && *p != '\0')
406                                 p++;
407                 } else
408                         user = p;
409                 *p = '\0';
410                 if (__icheckhost(raddr, buf) &&
411                     strcmp(ruser, *user ? user : luser) == 0) {
412                         free (buf);
413                         return (0);
414                 }
415         }
416         free (buf);
417         return (-1);
418 }
419
420 /*
421  * Returns "true" if match, 0 if no match.
422  */
423 static int
424 __icheckhost(raddr, lhost)
425         u_int32_t raddr;
426         register char *lhost;
427 {
428         register struct hostent hostbuf, *hp;
429         size_t buflen;
430         char *buffer;
431         register u_int32_t laddr;
432         register char **pp;
433         int herr;
434
435         /* Try for raw ip address first. */
436         if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
437                 return (raddr == laddr);
438
439         /* Better be a hostname. */
440         buflen = 1024;
441         buffer = __alloca (buflen);
442         while (__gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
443                < 0)
444           if (herr != NETDB_INTERNAL || errno != ERANGE)
445             return 0;
446           else
447             {
448               /* Enlarge the buffer.  */
449               buflen *= 2;
450               buflen = __alloca (buflen);
451             }
452
453         /* Spin through ip addresses. */
454         for (pp = hp->h_addr_list; *pp; ++pp)
455                 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
456                         return (1);
457
458         /* No match. */
459         return (0);
460 }