update from main archive 961201
[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: %s\n",
106                                     strerror(errno));
107                         sigsetmask(oldmask);
108                         return (-1);
109                 }
110                 fcntl(s, F_SETOWN, pid);
111                 sin.sin_family = hp->h_addrtype;
112                 bcopy(hp->h_addr_list[0], &sin.sin_addr,
113                       MIN (sizeof (sin.sin_addr), hp->h_length));
114                 sin.sin_port = rport;
115                 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
116                         break;
117                 (void)close(s);
118                 if (errno == EADDRINUSE) {
119                         lport--;
120                         continue;
121                 }
122                 if (errno == ECONNREFUSED && timo <= 16) {
123                         (void)sleep(timo);
124                         timo *= 2;
125                         continue;
126                 }
127                 if (hp->h_addr_list[1] != NULL) {
128                         int oerrno = errno;
129
130                         (void)fprintf(stderr, _("connect to address %s: "),
131                             inet_ntoa(sin.sin_addr));
132                         __set_errno (oerrno);
133                         perror(0);
134                         hp->h_addr_list++;
135                         bcopy(hp->h_addr_list[0], &sin.sin_addr,
136                               MIN (sizeof (sin.sin_addr), hp->h_length));
137                         (void)fprintf(stderr, _("Trying %s...\n"),
138                             inet_ntoa(sin.sin_addr));
139                         continue;
140                 }
141                 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
142                 sigsetmask(oldmask);
143                 return (-1);
144         }
145         lport--;
146         if (fd2p == 0) {
147                 write(s, "", 1);
148                 lport = 0;
149         } else {
150                 char num[8];
151                 int s2 = rresvport(&lport), s3;
152                 int len = sizeof(from);
153
154                 if (s2 < 0)
155                         goto bad;
156                 listen(s2, 1);
157                 (void)snprintf(num, sizeof(num), "%d", lport);
158                 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
159                         (void)fprintf(stderr,
160                             _("rcmd: write (setting up stderr): %s\n"),
161                             strerror(errno));
162                         (void)close(s2);
163                         goto bad;
164                 }
165                 FD_ZERO(&reads);
166                 FD_SET(s, &reads);
167                 FD_SET(s2, &reads);
168                 __set_errno (0);
169                 if (select(1 + (s > s2 ? s : s2), &reads, 0, 0, 0) < 1 ||
170                     !FD_ISSET(s2, &reads)) {
171                         if (errno != 0)
172                                 (void)fprintf(stderr,
173                                    _("rcmd: select (setting up stderr): %s\n"),
174                                    strerror(errno));
175                         else
176                                 (void)fprintf(stderr,
177                              _("select: protocol failure in circuit setup\n"));
178                         (void)close(s2);
179                         goto bad;
180                 }
181                 s3 = accept(s2, (struct sockaddr *)&from, &len);
182                 (void)close(s2);
183                 if (s3 < 0) {
184                         (void)fprintf(stderr,
185                             "rcmd: accept: %s\n", strerror(errno));
186                         lport = 0;
187                         goto bad;
188                 }
189                 *fd2p = s3;
190                 from.sin_port = ntohs((u_short)from.sin_port);
191                 if (from.sin_family != AF_INET ||
192                     from.sin_port >= IPPORT_RESERVED ||
193                     from.sin_port < IPPORT_RESERVED / 2) {
194                         (void)fprintf(stderr,
195                             _("socket: protocol failure in circuit setup\n"));
196                         goto bad2;
197                 }
198         }
199         (void)write(s, locuser, strlen(locuser)+1);
200         (void)write(s, remuser, strlen(remuser)+1);
201         (void)write(s, cmd, strlen(cmd)+1);
202         if (read(s, &c, 1) != 1) {
203                 (void)fprintf(stderr,
204                     "rcmd: %s: %s\n", *ahost, strerror(errno));
205                 goto bad2;
206         }
207         if (c != 0) {
208                 while (read(s, &c, 1) == 1) {
209                         (void)write(STDERR_FILENO, &c, 1);
210                         if (c == '\n')
211                                 break;
212                 }
213                 goto bad2;
214         }
215         sigsetmask(oldmask);
216         return (s);
217 bad2:
218         if (lport)
219                 (void)close(*fd2p);
220 bad:
221         (void)close(s);
222         sigsetmask(oldmask);
223         return (-1);
224 }
225
226 int
227 rresvport(alport)
228         int *alport;
229 {
230         struct sockaddr_in sin;
231         int s;
232
233         sin.sin_family = AF_INET;
234         sin.sin_addr.s_addr = INADDR_ANY;
235         s = socket(AF_INET, SOCK_STREAM, 0);
236         if (s < 0)
237                 return (-1);
238         for (;;) {
239                 sin.sin_port = htons((u_short)*alport);
240                 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
241                         return (s);
242                 if (errno != EADDRINUSE) {
243                         (void)close(s);
244                         return (-1);
245                 }
246                 (*alport)--;
247                 if (*alport == IPPORT_RESERVED/2) {
248                         (void)close(s);
249                         __set_errno (EAGAIN);           /* close */
250                         return (-1);
251                 }
252         }
253 }
254
255 int     __check_rhosts_file = 1;
256 char    *__rcmd_errstr;
257
258 int
259 ruserok(rhost, superuser, ruser, luser)
260         const char *rhost, *ruser, *luser;
261         int superuser;
262 {
263         struct hostent hostbuf, *hp;
264         size_t buflen;
265         char *buffer;
266         u_int32_t addr;
267         char **ap;
268         int herr;
269
270         buflen = 1024;
271         buffer = __alloca (buflen);
272
273         while (__gethostbyname_r (rhost, &hostbuf, buffer, buflen, &hp, &herr)
274                < 0)
275           if (herr != NETDB_INTERNAL || errno != ERANGE)
276             return -1;
277           else
278             {
279               /* Enlarge the buffer.  */
280               buflen *= 2;
281               buffer = __alloca (buflen);
282             }
283
284         for (ap = hp->h_addr_list; *ap; ++ap) {
285                 bcopy(*ap, &addr, sizeof(addr));
286                 if (iruserok(addr, superuser, ruser, luser) == 0)
287                         return (0);
288         }
289         return (-1);
290 }
291
292 /*
293  * New .rhosts strategy: We are passed an ip address. We spin through
294  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
295  * has ip addresses, we don't have to trust a nameserver.  When it
296  * contains hostnames, we spin through the list of addresses the nameserver
297  * gives us and look for a match.
298  *
299  * Returns 0 if ok, -1 if not ok.
300  */
301 int
302 iruserok(raddr, superuser, ruser, luser)
303         u_int32_t raddr;
304         int superuser;
305         const char *ruser, *luser;
306 {
307         register char *cp;
308         struct stat sbuf;
309         struct passwd pwdbuf, *pwd;
310         FILE *hostf;
311         uid_t uid;
312         int first;
313
314         first = 1;
315         hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
316 again:
317         if (hostf) {
318                 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
319                         (void)fclose(hostf);
320                         return (0);
321                 }
322                 (void)fclose(hostf);
323         }
324         if (first == 1 && (__check_rhosts_file || superuser)) {
325                 char *pbuf;
326                 size_t dirlen;
327                 size_t buflen = __sysconf (_SC_GETPW_R_SIZE_MAX);
328                 char buffer = __alloca (buflen);
329
330                 first = 0;
331                 if (getpwnam_r (luser, &pwdbuf, buffer, buflen, &pwd) < 0)
332                         return -1;
333
334                 dirlen = strlen (pwd->pw_dir);
335                 pbuf = alloca (dirlen + sizeof "/.rhosts");
336                 memcpy (pbuf, pwd->pw_dir, dirlen);
337                 memcpy (pbuf + dirlen, "/.rhosts", sizeof "/.rhosts");
338
339                 /*
340                  * Change effective uid while opening .rhosts.  If root and
341                  * reading an NFS mounted file system, can't read files that
342                  * are protected read/write owner only.
343                  */
344                 uid = geteuid();
345                 (void)seteuid(pwd->pw_uid);
346                 hostf = fopen(pbuf, "r");
347                 (void)seteuid(uid);
348
349                 if (hostf == NULL)
350                         return (-1);
351                 /*
352                  * If not a regular file, or is owned by someone other than
353                  * user or root or if writeable by anyone but the owner, quit.
354                  */
355                 cp = NULL;
356                 if (lstat(pbuf, &sbuf) < 0)
357                         cp = _(".rhosts lstat failed");
358                 else if (!S_ISREG(sbuf.st_mode))
359                         cp = _(".rhosts not regular file");
360                 else if (fstat(fileno(hostf), &sbuf) < 0)
361                         cp = _(".rhosts fstat failed");
362                 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
363                         cp = _("bad .rhosts owner");
364                 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
365                         cp = _(".rhosts writeable by other than owner");
366                 /* If there were any problems, quit. */
367                 if (cp) {
368                         __rcmd_errstr = cp;
369                         (void)fclose(hostf);
370                         return (-1);
371                 }
372                 goto again;
373         }
374         return (-1);
375 }
376
377 /*
378  * XXX
379  * Don't make static, used by lpd(8).
380  *
381  * Returns 0 if ok, -1 if not ok.
382  */
383 int
384 __ivaliduser(hostf, raddr, luser, ruser)
385         FILE *hostf;
386         u_int32_t raddr;
387         const char *luser, *ruser;
388 {
389         register char *user, *p;
390         int ch;
391         char *buf = NULL;
392         size_t bufsize = 0;
393         ssize_t nread;
394
395         while ((nread = getline (&buf, &bufsize, hostf)) > 0) {
396                 buf[bufsize - 1] = '\0'; /* Make sure it's terminated.  */
397                 p = buf;
398                 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
399                         *p = isupper(*p) ? tolower(*p) : *p;
400                         p++;
401                 }
402                 if (*p == ' ' || *p == '\t') {
403                         *p++ = '\0';
404                         while (*p == ' ' || *p == '\t')
405                                 p++;
406                         user = p;
407                         while (*p != '\n' && *p != ' ' &&
408                             *p != '\t' && *p != '\0')
409                                 p++;
410                 } else
411                         user = p;
412                 *p = '\0';
413                 if (__icheckhost(raddr, buf) &&
414                     strcmp(ruser, *user ? user : luser) == 0) {
415                         free (buf);
416                         return (0);
417                 }
418         }
419         free (buf);
420         return (-1);
421 }
422
423 /*
424  * Returns "true" if match, 0 if no match.
425  */
426 static int
427 __icheckhost(raddr, lhost)
428         u_int32_t raddr;
429         register char *lhost;
430 {
431         register struct hostent hostbuf, *hp;
432         size_t buflen;
433         char *buffer;
434         register u_int32_t laddr;
435         register char **pp;
436         int herr;
437
438         /* Try for raw ip address first. */
439         if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
440                 return (raddr == laddr);
441
442         /* Better be a hostname. */
443         buflen = 1024;
444         buffer = __alloca (buflen);
445         while (__gethostbyname_r (lhost, &hostbuf, buffer, buflen, &hp, &herr)
446                < 0)
447           if (herr != NETDB_INTERNAL || errno != ERANGE)
448             return 0;
449           else
450             {
451               /* Enlarge the buffer.  */
452               buflen *= 2;
453               buflen = __alloca (buflen);
454             }
455
456         /* Spin through ip addresses. */
457         for (pp = hp->h_addr_list; *pp; ++pp)
458                 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
459                         return (1);
460
461         /* No match. */
462         return (0);
463 }