update from main archive 961001
[glibc.git] / inet / rcmd.c
blob3fc8adc742913d97d6399de14d4041c2dd597f24
1 /*
2 * Copyright (c) 1983, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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.
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.
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 */
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <netdb.h>
48 #include <unistd.h>
49 #include <pwd.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <string.h>
56 int __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
57 static int __icheckhost __P((u_int32_t, char *));
59 int
60 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
61 char **ahost;
62 u_short rport;
63 const char *locuser, *remuser, *cmd;
64 int *fd2p;
66 struct hostent *hp;
67 struct sockaddr_in sin, from;
68 fd_set reads;
69 int32_t oldmask;
70 pid_t pid;
71 int s, lport, timo;
72 char c;
74 pid = getpid();
75 hp = gethostbyname(*ahost);
76 if (hp == NULL) {
77 herror(*ahost);
78 return (-1);
80 *ahost = hp->h_name;
81 oldmask = sigblock(sigmask(SIGURG));
82 for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
83 s = rresvport(&lport);
84 if (s < 0) {
85 if (errno == EAGAIN)
86 (void)fprintf(stderr,
87 _("rcmd: socket: All ports in use\n"));
88 else
89 (void)fprintf(stderr, "rcmd: socket: %s\n",
90 strerror(errno));
91 sigsetmask(oldmask);
92 return (-1);
94 fcntl(s, F_SETOWN, pid);
95 sin.sin_family = hp->h_addrtype;
96 bcopy(hp->h_addr_list[0], &sin.sin_addr,
97 MIN (sizeof (sin.sin_addr), hp->h_length));
98 sin.sin_port = rport;
99 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
100 break;
101 (void)close(s);
102 if (errno == EADDRINUSE) {
103 lport--;
104 continue;
106 if (errno == ECONNREFUSED && timo <= 16) {
107 (void)sleep(timo);
108 timo *= 2;
109 continue;
111 if (hp->h_addr_list[1] != NULL) {
112 int oerrno = errno;
114 (void)fprintf(stderr, _("connect to address %s: "),
115 inet_ntoa(sin.sin_addr));
116 __set_errno (oerrno);
117 perror(0);
118 hp->h_addr_list++;
119 bcopy(hp->h_addr_list[0], &sin.sin_addr,
120 MIN (sizeof (sin.sin_addr), hp->h_length));
121 (void)fprintf(stderr, _("Trying %s...\n"),
122 inet_ntoa(sin.sin_addr));
123 continue;
125 (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
126 sigsetmask(oldmask);
127 return (-1);
129 lport--;
130 if (fd2p == 0) {
131 write(s, "", 1);
132 lport = 0;
133 } else {
134 char num[8];
135 int s2 = rresvport(&lport), s3;
136 int len = sizeof(from);
138 if (s2 < 0)
139 goto bad;
140 listen(s2, 1);
141 (void)snprintf(num, sizeof(num), "%d", lport);
142 if (write(s, num, strlen(num)+1) != strlen(num)+1) {
143 (void)fprintf(stderr,
144 _("rcmd: write (setting up stderr): %s\n"),
145 strerror(errno));
146 (void)close(s2);
147 goto bad;
149 FD_ZERO(&reads);
150 FD_SET(s, &reads);
151 FD_SET(s2, &reads);
152 __set_errno (0);
153 if (select(1 + (s > s2 ? s : s2), &reads, 0, 0, 0) < 1 ||
154 !FD_ISSET(s2, &reads)) {
155 if (errno != 0)
156 (void)fprintf(stderr,
157 _("rcmd: select (setting up stderr): %s\n"),
158 strerror(errno));
159 else
160 (void)fprintf(stderr,
161 _("select: protocol failure in circuit setup\n"));
162 (void)close(s2);
163 goto bad;
165 s3 = accept(s2, (struct sockaddr *)&from, &len);
166 (void)close(s2);
167 if (s3 < 0) {
168 (void)fprintf(stderr,
169 "rcmd: accept: %s\n", strerror(errno));
170 lport = 0;
171 goto bad;
173 *fd2p = s3;
174 from.sin_port = ntohs((u_short)from.sin_port);
175 if (from.sin_family != AF_INET ||
176 from.sin_port >= IPPORT_RESERVED ||
177 from.sin_port < IPPORT_RESERVED / 2) {
178 (void)fprintf(stderr,
179 _("socket: protocol failure in circuit setup\n"));
180 goto bad2;
183 (void)write(s, locuser, strlen(locuser)+1);
184 (void)write(s, remuser, strlen(remuser)+1);
185 (void)write(s, cmd, strlen(cmd)+1);
186 if (read(s, &c, 1) != 1) {
187 (void)fprintf(stderr,
188 "rcmd: %s: %s\n", *ahost, strerror(errno));
189 goto bad2;
191 if (c != 0) {
192 while (read(s, &c, 1) == 1) {
193 (void)write(STDERR_FILENO, &c, 1);
194 if (c == '\n')
195 break;
197 goto bad2;
199 sigsetmask(oldmask);
200 return (s);
201 bad2:
202 if (lport)
203 (void)close(*fd2p);
204 bad:
205 (void)close(s);
206 sigsetmask(oldmask);
207 return (-1);
211 rresvport(alport)
212 int *alport;
214 struct sockaddr_in sin;
215 int s;
217 sin.sin_family = AF_INET;
218 sin.sin_addr.s_addr = INADDR_ANY;
219 s = socket(AF_INET, SOCK_STREAM, 0);
220 if (s < 0)
221 return (-1);
222 for (;;) {
223 sin.sin_port = htons((u_short)*alport);
224 if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
225 return (s);
226 if (errno != EADDRINUSE) {
227 (void)close(s);
228 return (-1);
230 (*alport)--;
231 if (*alport == IPPORT_RESERVED/2) {
232 (void)close(s);
233 __set_errno (EAGAIN); /* close */
234 return (-1);
239 int __check_rhosts_file = 1;
240 char *__rcmd_errstr;
243 ruserok(rhost, superuser, ruser, luser)
244 const char *rhost, *ruser, *luser;
245 int superuser;
247 struct hostent *hp;
248 u_int32_t addr;
249 char **ap;
251 if ((hp = gethostbyname(rhost)) == NULL)
252 return (-1);
253 for (ap = hp->h_addr_list; *ap; ++ap) {
254 bcopy(*ap, &addr, sizeof(addr));
255 if (iruserok(addr, superuser, ruser, luser) == 0)
256 return (0);
258 return (-1);
262 * New .rhosts strategy: We are passed an ip address. We spin through
263 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
264 * has ip addresses, we don't have to trust a nameserver. When it
265 * contains hostnames, we spin through the list of addresses the nameserver
266 * gives us and look for a match.
268 * Returns 0 if ok, -1 if not ok.
271 iruserok(raddr, superuser, ruser, luser)
272 u_int32_t raddr;
273 int superuser;
274 const char *ruser, *luser;
276 register char *cp;
277 struct stat sbuf;
278 struct passwd *pwd;
279 FILE *hostf;
280 uid_t uid;
281 int first;
283 first = 1;
284 hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
285 again:
286 if (hostf) {
287 if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
288 (void)fclose(hostf);
289 return (0);
291 (void)fclose(hostf);
293 if (first == 1 && (__check_rhosts_file || superuser)) {
294 char *pbuf;
295 size_t dirlen;
297 first = 0;
298 if ((pwd = getpwnam(luser)) == NULL)
299 return (-1);
301 dirlen = strlen (pwd->pw_dir);
302 pbuf = alloca (dirlen + sizeof "/.rhosts");
303 memcpy (pbuf, pwd->pw_dir, dirlen);
304 memcpy (pbuf + dirlen, "/.rhosts", sizeof "/.rhosts");
307 * Change effective uid while opening .rhosts. If root and
308 * reading an NFS mounted file system, can't read files that
309 * are protected read/write owner only.
311 uid = geteuid();
312 (void)seteuid(pwd->pw_uid);
313 hostf = fopen(pbuf, "r");
314 (void)seteuid(uid);
316 if (hostf == NULL)
317 return (-1);
319 * If not a regular file, or is owned by someone other than
320 * user or root or if writeable by anyone but the owner, quit.
322 cp = NULL;
323 if (lstat(pbuf, &sbuf) < 0)
324 cp = _(".rhosts lstat failed");
325 else if (!S_ISREG(sbuf.st_mode))
326 cp = _(".rhosts not regular file");
327 else if (fstat(fileno(hostf), &sbuf) < 0)
328 cp = _(".rhosts fstat failed");
329 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
330 cp = _("bad .rhosts owner");
331 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
332 cp = _(".rhosts writeable by other than owner");
333 /* If there were any problems, quit. */
334 if (cp) {
335 __rcmd_errstr = cp;
336 (void)fclose(hostf);
337 return (-1);
339 goto again;
341 return (-1);
345 * XXX
346 * Don't make static, used by lpd(8).
348 * Returns 0 if ok, -1 if not ok.
351 __ivaliduser(hostf, raddr, luser, ruser)
352 FILE *hostf;
353 u_int32_t raddr;
354 const char *luser, *ruser;
356 register char *user, *p;
357 int ch;
358 char *buf = NULL;
359 size_t bufsize = 0;
360 ssize_t nread;
362 while ((nread = getline (&buf, &bufsize, hostf)) > 0) {
363 buf[bufsize - 1] = '\0'; /* Make sure it's terminated. */
364 p = buf;
365 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
366 *p = isupper(*p) ? tolower(*p) : *p;
367 p++;
369 if (*p == ' ' || *p == '\t') {
370 *p++ = '\0';
371 while (*p == ' ' || *p == '\t')
372 p++;
373 user = p;
374 while (*p != '\n' && *p != ' ' &&
375 *p != '\t' && *p != '\0')
376 p++;
377 } else
378 user = p;
379 *p = '\0';
380 if (__icheckhost(raddr, buf) &&
381 strcmp(ruser, *user ? user : luser) == 0) {
382 free (buf);
383 return (0);
386 free (buf);
387 return (-1);
391 * Returns "true" if match, 0 if no match.
393 static int
394 __icheckhost(raddr, lhost)
395 u_int32_t raddr;
396 register char *lhost;
398 register struct hostent *hp;
399 register u_int32_t laddr;
400 register char **pp;
402 /* Try for raw ip address first. */
403 if (isdigit(*lhost) && (int32_t)(laddr = inet_addr(lhost)) != -1)
404 return (raddr == laddr);
406 /* Better be a hostname. */
407 if ((hp = gethostbyname(lhost)) == NULL)
408 return (0);
410 /* Spin through ip addresses. */
411 for (pp = hp->h_addr_list; *pp; ++pp)
412 if (!bcmp(&raddr, *pp, sizeof(u_int32_t)))
413 return (1);
415 /* No match. */
416 return (0);