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