]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/net/rcmd.c
libc rcmd update for IPv6.
[FreeBSD/FreeBSD.git] / lib / libc / net / 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  * $FreeBSD$ 
34  */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)rcmd.c      8.3 (Berkeley) 3/26/94";
38 #endif /* LIBC_SCCS and not lint */
39
40 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/stat.h>
43
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
46
47 #include <signal.h>
48 #include <fcntl.h>
49 #include <netdb.h>
50 #include <unistd.h>
51 #include <pwd.h>
52 #include <errno.h>
53 #include <stdio.h>
54 #include <ctype.h>
55 #include <string.h>
56 #ifdef YP
57 #include <rpc/rpc.h>
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
60 #endif
61
62 /* wrapper for KAME-special getnameinfo() */
63 #ifndef NI_WITHSCOPEID
64 #define NI_WITHSCOPEID  0
65 #endif
66
67 extern int innetgr __P(( const char *, const char *, const char *, const char * ));
68
69 #define max(a, b)       ((a > b) ? a : b)
70
71 int     __ivaliduser __P((FILE *, u_int32_t, const char *, const char *));
72 static int __icheckhost __P((void *, char *, int, int));
73
74 char paddr[INET6_ADDRSTRLEN];
75
76 int
77 rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
78         char **ahost;
79         u_short rport;
80         const char *locuser, *remuser, *cmd;
81         int *fd2p;
82 {
83         struct addrinfo hints, *res, *ai;
84         struct sockaddr_storage from;
85         fd_set reads;
86         long oldmask;
87         pid_t pid;
88         int s, aport, lport, timo, error;
89         char c;
90         int refused;
91         char num[8];
92
93         pid = getpid();
94
95         memset(&hints, 0, sizeof(hints));
96         hints.ai_flags = AI_CANONNAME;
97         hints.ai_family = AF_UNSPEC;
98         hints.ai_socktype = SOCK_STREAM;
99         hints.ai_protocol = 0;
100         (void)snprintf(num, sizeof(num), "%d", ntohs(rport));
101         error = getaddrinfo(*ahost, num, &hints, &res);
102         if (error) {
103                 fprintf(stderr, "rcmd: getaddrinfo: %s\n",
104                         gai_strerror(error));
105                 if (error == EAI_SYSTEM)
106                         fprintf(stderr, "rcmd: getaddrinfo: %s\n",
107                                 strerror(errno));
108                 return (-1);
109         }
110         if (res->ai_canonname)
111                 *ahost = res->ai_canonname;
112         ai = res;
113         refused = 0;
114         oldmask = sigblock(sigmask(SIGURG));
115         for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
116                 s = rresvport_af(&lport, ai->ai_family);
117                 if (s < 0) {
118                         if (errno == EAGAIN)
119                                 (void)fprintf(stderr,
120                                     "rcmd: socket: All ports in use\n");
121                         else
122                                 (void)fprintf(stderr, "rcmd: socket: %s\n",
123                                     strerror(errno));
124                         sigsetmask(oldmask);
125                         freeaddrinfo(res);
126                         return (-1);
127                 }
128                 _libc_fcntl(s, F_SETOWN, pid);
129                 if (connect(s, ai->ai_addr, ai->ai_addrlen) >= 0)
130                         break;
131                 (void)_libc_close(s);
132                 if (errno == EADDRINUSE) {
133                         lport--;
134                         continue;
135                 }
136                 if (errno == ECONNREFUSED)
137                         refused = 1;
138                 if (ai->ai_next != NULL) {
139                         int oerrno = errno;
140
141                         getnameinfo(ai->ai_addr, ai->ai_addrlen,
142                                     paddr, sizeof(paddr),
143                                     NULL, 0,
144                                     NI_NUMERICHOST|NI_WITHSCOPEID);
145                         (void)fprintf(stderr, "connect to address %s: ",
146                                       paddr);
147                         errno = oerrno;
148                         perror(0);
149                         ai = ai->ai_next;
150                         getnameinfo(ai->ai_addr, ai->ai_addrlen,
151                                     paddr, sizeof(paddr),
152                                     NULL, 0,
153                                     NI_NUMERICHOST|NI_WITHSCOPEID);
154                         fprintf(stderr, "Trying %s...\n", paddr);
155                         continue;
156                 }
157                 if (refused && timo <= 16) {
158                         (void)_libc_sleep(timo);
159                         timo *= 2;
160                         ai = res;
161                         refused = 0;
162                         continue;
163                 }
164                 freeaddrinfo(res);
165                 (void)fprintf(stderr, "%s: %s\n", *ahost, strerror(errno));
166                 sigsetmask(oldmask);
167                 return (-1);
168         }
169         lport--;
170         if (fd2p == 0) {
171                 _libc_write(s, "", 1);
172                 lport = 0;
173         } else {
174                 char num[8];
175                 int s2 = rresvport_af(&lport, ai->ai_family), s3;
176                 int len = ai->ai_addrlen;
177                 int nfds;
178
179                 if (s2 < 0)
180                         goto bad;
181                 listen(s2, 1);
182                 (void)snprintf(num, sizeof(num), "%d", lport);
183                 if (_libc_write(s, num, strlen(num)+1) != strlen(num)+1) {
184                         (void)fprintf(stderr,
185                             "rcmd: write (setting up stderr): %s\n",
186                             strerror(errno));
187                         (void)_libc_close(s2);
188                         goto bad;
189                 }
190                 nfds = max(s, s2)+1;
191                 if(nfds > FD_SETSIZE) {
192                         fprintf(stderr, "rcmd: too many files\n");
193                         (void)_libc_close(s2);
194                         goto bad;
195                 }
196 again:
197                 FD_ZERO(&reads);
198                 FD_SET(s, &reads);
199                 FD_SET(s2, &reads);
200                 errno = 0;
201                 if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
202                         if (errno != 0)
203                                 (void)fprintf(stderr,
204                                     "rcmd: select (setting up stderr): %s\n",
205                                     strerror(errno));
206                         else
207                                 (void)fprintf(stderr,
208                                 "select: protocol failure in circuit setup\n");
209                         (void)_libc_close(s2);
210                         goto bad;
211                 }
212                 s3 = accept(s2, (struct sockaddr *)&from, &len);
213                 switch (from.ss_family) {
214                 case AF_INET:
215                         aport = ntohs(((struct sockaddr_in *)&from)->sin_port);
216                         break;
217 #ifdef INET6
218                 case AF_INET6:
219                         aport = ntohs(((struct sockaddr_in6 *)&from)->sin6_port);
220                         break;
221 #endif
222                 default:
223                         aport = 0;      /* error */
224                         break;
225                 }
226                 /*
227                  * XXX careful for ftp bounce attacks. If discovered, shut them
228                  * down and check for the real auxiliary channel to connect.
229                  */
230                 if (aport == 20) {
231                         _libc_close(s3);
232                         goto again;
233                 }
234                 (void)_libc_close(s2);
235                 if (s3 < 0) {
236                         (void)fprintf(stderr,
237                             "rcmd: accept: %s\n", strerror(errno));
238                         lport = 0;
239                         goto bad;
240                 }
241                 *fd2p = s3;
242                 if (aport >= IPPORT_RESERVED || aport < IPPORT_RESERVED / 2) {
243                         (void)fprintf(stderr,
244                             "socket: protocol failure in circuit setup.\n");
245                         goto bad2;
246                 }
247         }
248         (void)_libc_write(s, locuser, strlen(locuser)+1);
249         (void)_libc_write(s, remuser, strlen(remuser)+1);
250         (void)_libc_write(s, cmd, strlen(cmd)+1);
251         if (_libc_read(s, &c, 1) != 1) {
252                 (void)fprintf(stderr,
253                     "rcmd: %s: %s\n", *ahost, strerror(errno));
254                 goto bad2;
255         }
256         if (c != 0) {
257                 while (_libc_read(s, &c, 1) == 1) {
258                         (void)_libc_write(STDERR_FILENO, &c, 1);
259                         if (c == '\n')
260                                 break;
261                 }
262                 goto bad2;
263         }
264         sigsetmask(oldmask);
265         freeaddrinfo(res);
266         return (s);
267 bad2:
268         if (lport)
269                 (void)_libc_close(*fd2p);
270 bad:
271         (void)_libc_close(s);
272         sigsetmask(oldmask);
273         freeaddrinfo(res);
274         return (-1);
275 }
276
277 int
278 rresvport(port)
279         int *port;
280 {
281         return rresvport_af(port, AF_INET);
282 }
283
284 int
285 rresvport_af(alport, family)
286         int *alport, family;
287 {
288         int i, s, len, err;
289         struct sockaddr_storage ss;
290         u_short *sport;
291
292         memset(&ss, 0, sizeof(ss));
293         ss.ss_family = family;
294         switch (family) {
295         case AF_INET:
296                 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in);
297                 sport = &((struct sockaddr_in *)&ss)->sin_port;
298                 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = INADDR_ANY;
299                 break;
300 #ifdef INET6
301         case AF_INET6:
302                 ((struct sockaddr *)&ss)->sa_len = sizeof(struct sockaddr_in6);
303                 sport = &((struct sockaddr_in6 *)&ss)->sin6_port;
304                 ((struct sockaddr_in6 *)&ss)->sin6_addr = in6addr_any;
305                 break;
306 #endif
307         default:
308                 errno = EAFNOSUPPORT;
309                 return -1;
310         }
311
312         s = socket(ss.ss_family, SOCK_STREAM, 0);
313         if (s < 0)
314                 return (-1);
315 #if 0 /* compat_exact_traditional_rresvport_semantics */
316         sin.sin_port = htons((u_short)*alport);
317         if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
318                 return (s);
319         if (errno != EADDRINUSE) {
320                 (void)_libc_close(s);
321                 return (-1);
322         }
323 #endif
324         *sport = 0;
325         if (bindresvport2(s, (struct sockaddr *)&ss,
326                           ((struct sockaddr *)&ss)->sa_len) == -1) {
327                 (void)_libc_close(s);
328                 return (-1);
329         }
330         *alport = (int)ntohs(*sport);
331         return (s);
332 }
333
334 int     __check_rhosts_file = 1;
335 char    *__rcmd_errstr;
336
337 int
338 ruserok(rhost, superuser, ruser, luser)
339         const char *rhost, *ruser, *luser;
340         int superuser;
341 {
342         return ruserok_af(rhost, superuser, ruser, luser, AF_INET);
343 }
344
345 int
346 ruserok_af(rhost, superuser, ruser, luser, af)
347         const char *rhost, *ruser, *luser;
348         int superuser, af;
349 {
350         struct hostent *hp;
351         union {
352                 struct in_addr addr_in;
353                 struct in6_addr addr_in6;
354         } addr;
355         char **ap;
356         int ret, h_error;
357
358         if ((hp = getipnodebyname(rhost, af, AI_DEFAULT, &h_error)) == NULL)
359                 return (-1);
360         ret = -1;
361         for (ap = hp->h_addr_list; *ap; ++ap) {
362                 bcopy(*ap, &addr, hp->h_length);
363                 if (iruserok_af(&addr, superuser, ruser, luser, af) == 0) {
364                         ret = 0;
365                         break;
366                 }
367         }
368         freehostent(hp);
369         return (ret);
370 }
371
372 /*
373  * New .rhosts strategy: We are passed an ip address. We spin through
374  * hosts.equiv and .rhosts looking for a match. When the .rhosts only
375  * has ip addresses, we don't have to trust a nameserver.  When it
376  * contains hostnames, we spin through the list of addresses the nameserver
377  * gives us and look for a match.
378  *
379  * Returns 0 if ok, -1 if not ok.
380  */
381 int
382 iruserok(raddr, superuser, ruser, luser)
383         unsigned long raddr;
384         int superuser;
385         const char *ruser, *luser;
386 {
387         return iruserok_af(&raddr, superuser, ruser, luser, AF_INET);
388 }
389
390 int
391 iruserok_af(raddr, superuser, ruser, luser, af)
392         void *raddr;
393         int superuser;
394         const char *ruser, *luser;
395         int af;
396 {
397         register char *cp;
398         struct stat sbuf;
399         struct passwd *pwd;
400         FILE *hostf;
401         uid_t uid;
402         int first;
403         char pbuf[MAXPATHLEN];
404         int len = 0;
405
406         switch (af) {
407         case AF_INET:
408                 len = sizeof(struct in_addr);
409                 break;
410 #ifdef INET6
411         case AF_INET6:
412                 len = sizeof(struct in6_addr);
413                 break;
414 #endif
415         }
416
417         first = 1;
418         hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
419 again:
420         if (hostf) {
421                 if (__ivaliduser_af(hostf, raddr, luser, ruser, af, len)
422                     == 0) {
423                         (void)fclose(hostf);
424                         return (0);
425                 }
426                 (void)fclose(hostf);
427         }
428         if (first == 1 && (__check_rhosts_file || superuser)) {
429                 first = 0;
430                 if ((pwd = getpwnam(luser)) == NULL)
431                         return (-1);
432                 (void)strcpy(pbuf, pwd->pw_dir);
433                 (void)strcat(pbuf, "/.rhosts");
434
435                 /*
436                  * Change effective uid while opening .rhosts.  If root and
437                  * reading an NFS mounted file system, can't read files that
438                  * are protected read/write owner only.
439                  */
440                 uid = geteuid();
441                 (void)seteuid(pwd->pw_uid);
442                 hostf = fopen(pbuf, "r");
443                 (void)seteuid(uid);
444
445                 if (hostf == NULL)
446                         return (-1);
447                 /*
448                  * If not a regular file, or is owned by someone other than
449                  * user or root or if writeable by anyone but the owner, quit.
450                  */
451                 cp = NULL;
452                 if (lstat(pbuf, &sbuf) < 0)
453                         cp = ".rhosts lstat failed";
454                 else if (!S_ISREG(sbuf.st_mode))
455                         cp = ".rhosts not regular file";
456                 else if (fstat(fileno(hostf), &sbuf) < 0)
457                         cp = ".rhosts fstat failed";
458                 else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
459                         cp = "bad .rhosts owner";
460                 else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
461                         cp = ".rhosts writeable by other than owner";
462                 /* If there were any problems, quit. */
463                 if (cp) {
464                         __rcmd_errstr = cp;
465                         (void)fclose(hostf);
466                         return (-1);
467                 }
468                 goto again;
469         }
470         return (-1);
471 }
472
473 /*
474  * XXX
475  * Don't make static, used by lpd(8).
476  *
477  * Returns 0 if ok, -1 if not ok.
478  */
479 int
480 __ivaliduser(hostf, raddr, luser, ruser)
481         FILE *hostf;
482         u_int32_t raddr;
483         const char *luser, *ruser;
484 {
485         return __ivaliduser_af(hostf, &raddr, luser, ruser, AF_INET,
486                                sizeof(raddr));
487 }
488
489 int
490 __ivaliduser_af(hostf, raddr, luser, ruser, af, len)
491         FILE *hostf;
492         void *raddr;
493         const char *luser, *ruser;
494         int af, len;
495 {
496         register char *user, *p;
497         int ch;
498         char buf[MAXHOSTNAMELEN + 128];         /* host + login */
499         char hname[MAXHOSTNAMELEN];
500         struct hostent *hp;
501         /* Presumed guilty until proven innocent. */
502         int userok = 0, hostok = 0;
503         int h_error;
504 #ifdef YP
505         char *ypdomain;
506
507         if (yp_get_default_domain(&ypdomain))
508                 ypdomain = NULL;
509 #else
510 #define ypdomain NULL
511 #endif
512         /* We need to get the damn hostname back for netgroup matching. */
513         if ((hp = getipnodebyaddr((char *)raddr, len, af, &h_error)) == NULL)
514                 return (-1);
515         strncpy(hname, hp->h_name, sizeof(hname));
516         hname[sizeof(hname) - 1] = '\0';
517         freehostent(hp);
518
519         while (fgets(buf, sizeof(buf), hostf)) {
520                 p = buf;
521                 /* Skip lines that are too long. */
522                 if (strchr(p, '\n') == NULL) {
523                         while ((ch = getc(hostf)) != '\n' && ch != EOF);
524                         continue;
525                 }
526                 if (*p == '\n' || *p == '#') {
527                         /* comment... */
528                         continue;
529                 }
530                 while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
531                         *p = isupper((unsigned char)*p) ? tolower((unsigned char)*p) : *p;
532                         p++;
533                 }
534                 if (*p == ' ' || *p == '\t') {
535                         *p++ = '\0';
536                         while (*p == ' ' || *p == '\t')
537                                 p++;
538                         user = p;
539                         while (*p != '\n' && *p != ' ' &&
540                             *p != '\t' && *p != '\0')
541                                 p++;
542                 } else
543                         user = p;
544                 *p = '\0';
545                 /*
546                  * Do +/- and +@/-@ checking. This looks really nasty,
547                  * but it matches SunOS's behavior so far as I can tell.
548                  */
549                 switch(buf[0]) {
550                 case '+':
551                         if (!buf[1]) {     /* '+' matches all hosts */
552                                 hostok = 1;
553                                 break;
554                         }
555                         if (buf[1] == '@')  /* match a host by netgroup */
556                                 hostok = innetgr((char *)&buf[2],
557                                         (char *)&hname, NULL, ypdomain);
558                         else            /* match a host by addr */
559                                 hostok = __icheckhost(raddr,(char *)&buf[1],
560                                                       af, len);
561                         break;
562                 case '-':     /* reject '-' hosts and all their users */
563                         if (buf[1] == '@') {
564                                 if (innetgr((char *)&buf[2],
565                                               (char *)&hname, NULL, ypdomain))
566                                         return(-1);
567                         } else {
568                                 if (__icheckhost(raddr,(char *)&buf[1],af,len))
569                                         return(-1);
570                         }
571                         break;
572                 default:  /* if no '+' or '-', do a simple match */
573                         hostok = __icheckhost(raddr, buf, af, len);
574                         break;
575                 }
576                 switch(*user) {
577                 case '+':
578                         if (!*(user+1)) {      /* '+' matches all users */
579                                 userok = 1;
580                                 break;
581                         }
582                         if (*(user+1) == '@')  /* match a user by netgroup */
583                                 userok = innetgr(user+2, NULL, ruser, ypdomain);
584                         else       /* match a user by direct specification */
585                                 userok = !(strcmp(ruser, user+1));
586                         break;
587                 case '-':               /* if we matched a hostname, */
588                         if (hostok) {   /* check for user field rejections */
589                                 if (!*(user+1))
590                                         return(-1);
591                                 if (*(user+1) == '@') {
592                                         if (innetgr(user+2, NULL,
593                                                         ruser, ypdomain))
594                                                 return(-1);
595                                 } else {
596                                         if (!strcmp(ruser, user+1))
597                                                 return(-1);
598                                 }
599                         }
600                         break;
601                 default:        /* no rejections: try to match the user */
602                         if (hostok)
603                                 userok = !(strcmp(ruser,*user ? user : luser));
604                         break;
605                 }
606                 if (hostok && userok)
607                         return(0);
608         }
609         return (-1);
610 }
611
612 /*
613  * Returns "true" if match, 0 if no match.
614  */
615 static int
616 __icheckhost(raddr, lhost, af, len)
617         void *raddr;
618         register char *lhost;
619         int af, len;
620 {
621         register struct hostent *hp;
622         char laddr[BUFSIZ]; /* xxx */
623         register char **pp;
624         int h_error;
625         int match;
626
627         /* Try for raw ip address first. */
628         if (inet_pton(af, lhost, laddr) == 1) {
629                 if (memcmp(raddr, laddr, len) == 0)
630                         return (1);
631                 else
632                         return (0);
633         }
634
635         /* Better be a hostname. */
636         if ((hp = getipnodebyname(lhost, af, AI_DEFAULT, &h_error)) == NULL)
637                 return (0);
638
639         /* Spin through ip addresses. */
640         match = 0;
641         for (pp = hp->h_addr_list; *pp; ++pp)
642                 if (!bcmp(raddr, *pp, len)) {
643                         match = 1;
644                         break;
645                 }
646
647         freehostent(hp);
648         return (match);
649 }