source: rtems/cpukit/libnetworking/libc/rcmd.c @ cb2f320

4.104.114.84.95
Last change on this file since cb2f320 was cb2f320, checked in by Joel Sherrill <joel.sherrill@…>, on 03/05/04 at 18:02:41

2004-03-05 Joel Sherrill <joel@…>

  • libblock/src/bdbuf.c, libblock/src/ramdisk.c, libcsupport/src/newlibc.c, libcsupport/src/sync.c, libmisc/cpuuse/cpuuse.c, libmisc/monitor/mon-symbols.c, libmisc/shell/cmds.c, libmisc/shell/shell.c, libnetworking/kern/kern_sysctl.c, libnetworking/lib/ftpfs.c, libnetworking/lib/tftpDriver.c, libnetworking/libc/gethostbydns.c, libnetworking/libc/gethostbyht.c, libnetworking/libc/gethostnamadr.c, libnetworking/libc/getnetbyht.c, libnetworking/libc/getnetnamadr.c, libnetworking/libc/inet_addr.c, libnetworking/libc/linkaddr.c, libnetworking/libc/map_v4v6.c, libnetworking/libc/ns_print.c, libnetworking/libc/ns_ttl.c, libnetworking/libc/nsap_addr.c, libnetworking/libc/rcmd.c, libnetworking/libc/res_debug.c, libnetworking/libc/res_mkupdate.c, libnetworking/libc/res_query.c, libnetworking/libc/res_send.c, libnetworking/libc/res_update.c, libnetworking/net/radix.c, libnetworking/rtems/mkrootfs.c, librpc/src/rpc/clnt_perror.c, librpc/src/rpc/svc.c, score/macros/rtems/score/chain.inl, score/src/objectidtoname.c: Too much was accidentally committed -- revert.
  • Property mode set to 100644
File size: 13.3 KB
Line 
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 *  $Id$
34 */
35
36#if defined(LIBC_SCCS) && !defined(lint)
37static 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#include <sys/select.h>
63
64
65#define max(a, b)       ((a > b) ? a : b)
66
67#ifdef __rtems__
68int rresvport();
69#define bzero(a,s)              memset((a),0,(s))
70#define bcmp                    memcmp
71#define bcopy(s,d,i)    memcpy(d,s,i)
72int bindresvport(int, struct sockaddr_in *);
73#else /* __rtems__ */
74
75extern int innetgr __P(( const char *, const char *, const char *, const char * ));
76
77int     __ivaliduser __P((FILE *, u_long, const char *, const char *));
78static int __icheckhost __P((u_long, char *));
79
80#endif
81
82int
83rcmd(ahost, rport, locuser, remuser, cmd, fd2p)
84        char **ahost;
85        u_short rport;
86        const char *locuser, *remuser, *cmd;
87        int *fd2p;
88{
89        struct hostent *hp;
90        struct sockaddr_in sin, from;
91        fd_set reads;
92#ifndef __rtems__
93        long oldmask;
94#endif
95        pid_t pid;
96        int s, lport, timo;
97        char c;
98
99        pid = getpid();
100        hp = gethostbyname(*ahost);
101        if (hp == NULL) {
102                herror(*ahost);
103                return (-1);
104        }
105        *ahost = hp->h_name;
106#ifndef __rtems__
107        oldmask = sigblock(sigmask(SIGURG));
108#endif
109        for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
110                s = rresvport(&lport);
111                if (s < 0) {
112                        if (errno == EAGAIN)
113                                (void)fprintf(stderr,
114                                    "rcmd: socket: All ports in use\n");
115                        else
116                                (void)fprintf(stderr, "rcmd: socket: %s\n",
117                                    strerror(errno));
118#ifndef __rtems__
119                        sigsetmask(oldmask);
120#endif
121                        return (-1);
122                }
123                fcntl(s, F_SETOWN, pid);
124                bzero(&sin, sizeof sin);
125                sin.sin_len = sizeof(struct sockaddr_in);
126                sin.sin_family = hp->h_addrtype;
127                sin.sin_port = rport;
128                bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
129                if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
130                        break;
131                (void)close(s);
132                if (errno == EADDRINUSE) {
133                        lport--;
134                        continue;
135                }
136                if (errno == ECONNREFUSED && timo <= 16) {
137                        (void)sleep(timo);
138                        timo *= 2;
139                        continue;
140                }
141                if (hp->h_addr_list[1] != NULL) {
142                        int oerrno = errno;
143
144                        (void)fprintf(stderr, "connect to address %s: ",
145                            inet_ntoa(sin.sin_addr));
146                        errno = oerrno;
147                        perror(0);
148                        hp->h_addr_list++;
149                        bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
150                        (void)fprintf(stderr, "Trying %s...\n",
151                            inet_ntoa(sin.sin_addr));
152                        continue;
153                }
154                (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
155#ifndef __rtems__
156                sigsetmask(oldmask);
157#endif
158                return (-1);
159        }
160        lport--;
161        if (fd2p == 0) {
162                write(s, "", 1);
163                lport = 0;
164        } else {
165                char num[8];
166                int s2 = rresvport(&lport), s3;
167                int len = sizeof(from);
168                int nfds;
169
170                if (s2 < 0)
171                        goto bad;
172                listen(s2, 1);
173                (void)snprintf(num, sizeof(num), "%d", lport);
174                if (write(s, num, strlen(num)+1) != strlen(num)+1) {
175                        (void)fprintf(stderr,
176                            "rcmd: write (setting up stderr): %s\n",
177                            strerror(errno));
178                        (void)close(s2);
179                        goto bad;
180                }
181                nfds = max(s, s2)+1;
182                if(nfds > FD_SETSIZE) {
183                        fprintf(stderr, "rcmd: too many files\n");
184                        (void)close(s2);
185                        goto bad;
186                }
187again:
188                FD_ZERO(&reads);
189                FD_SET(s, &reads);
190                FD_SET(s2, &reads);
191                errno = 0;
192                if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
193                        if (errno != 0)
194                                (void)fprintf(stderr,
195                                    "rcmd: select (setting up stderr): %s\n",
196                                    strerror(errno));
197                        else
198                                (void)fprintf(stderr,
199                                "select: protocol failure in circuit setup\n");
200                        (void)close(s2);
201                        goto bad;
202                }
203                s3 = accept(s2, (struct sockaddr *)&from, &len);
204                /*
205                 * XXX careful for ftp bounce attacks. If discovered, shut them
206                 * down and check for the real auxiliary channel to connect.
207                 */
208                if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
209                        close(s3);
210                        goto again;
211                }
212                (void)close(s2);
213                if (s3 < 0) {
214                        (void)fprintf(stderr,
215                            "rcmd: accept: %s\n", strerror(errno));
216                        lport = 0;
217                        goto bad;
218                }
219                *fd2p = s3;
220                from.sin_port = ntohs((u_short)from.sin_port);
221                if (from.sin_family != AF_INET ||
222                    from.sin_port >= IPPORT_RESERVED ||
223                    from.sin_port < IPPORT_RESERVED / 2) {
224                        (void)fprintf(stderr,
225                            "socket: protocol failure in circuit setup.\n");
226                        goto bad2;
227                }
228        }
229        (void)write(s, locuser, strlen(locuser)+1);
230        (void)write(s, remuser, strlen(remuser)+1);
231        (void)write(s, cmd, strlen(cmd)+1);
232        if (read(s, &c, 1) != 1) {
233                (void)fprintf(stderr,
234                    "rcmd: %s: %s\n", *ahost, strerror(errno));
235                goto bad2;
236        }
237        if (c != 0) {
238                while (read(s, &c, 1) == 1) {
239                        (void)write(STDERR_FILENO, &c, 1);
240                        if (c == '\n')
241                                break;
242                }
243                goto bad2;
244        }
245#ifndef __rtems__
246        sigsetmask(oldmask);
247#endif
248        return (s);
249bad2:
250        if (lport)
251                (void)close(*fd2p);
252bad:
253        (void)close(s);
254#ifndef __rtems__
255        sigsetmask(oldmask);
256#endif
257        return (-1);
258}
259
260int
261rresvport(alport)
262        int *alport;
263{
264        struct sockaddr_in sin;
265        int s;
266
267        bzero(&sin, sizeof sin);
268        sin.sin_len = sizeof(struct sockaddr_in);
269        sin.sin_family = AF_INET;
270        sin.sin_addr.s_addr = INADDR_ANY;
271        s = socket(AF_INET, SOCK_STREAM, 0);
272        if (s < 0)
273                return (-1);
274#if 0 /* compat_exact_traditional_rresvport_semantics */
275        sin.sin_port = htons((u_short)*alport);
276        if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
277                return (s);
278        if (errno != EADDRINUSE) {
279                (void)close(s);
280                return (-1);
281        }
282#endif
283        sin.sin_port = 0;
284        if (bindresvport(s, &sin) == -1) {
285                (void)close(s);
286                return (-1);
287        }
288        *alport = (int)ntohs(sin.sin_port);
289        return (s);
290}
291
292#ifndef __rtems__
293int     __check_rhosts_file = 1;
294char    *__rcmd_errstr;
295
296int
297ruserok(rhost, superuser, ruser, luser)
298        const char *rhost, *ruser, *luser;
299        int superuser;
300{
301        struct hostent *hp;
302        u_long addr;
303        char **ap;
304
305        if ((hp = gethostbyname(rhost)) == NULL)
306                return (-1);
307        for (ap = hp->h_addr_list; *ap; ++ap) {
308                bcopy(*ap, &addr, sizeof(addr));
309                if (iruserok(addr, superuser, ruser, luser) == 0)
310                        return (0);
311        }
312        return (-1);
313}
314
315/*
316 * New .rhosts strategy: We are passed an ip address. We spin through
317 * hosts.equiv and .rhosts looking for a match. When the .rhosts only
318 * has ip addresses, we don't have to trust a nameserver.  When it
319 * contains hostnames, we spin through the list of addresses the nameserver
320 * gives us and look for a match.
321 *
322 * Returns 0 if ok, -1 if not ok.
323 */
324int
325iruserok(raddr, superuser, ruser, luser)
326        u_long raddr;
327        int superuser;
328        const char *ruser, *luser;
329{
330        register char *cp;
331        struct stat sbuf;
332        struct passwd *pwd;
333        FILE *hostf;
334        uid_t uid;
335        int first;
336        char pbuf[MAXPATHLEN];
337
338        first = 1;
339        hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r");
340again:
341        if (hostf) {
342                if (__ivaliduser(hostf, raddr, luser, ruser) == 0) {
343                        (void)fclose(hostf);
344                        return (0);
345                }
346                (void)fclose(hostf);
347        }
348        if (first == 1 && (__check_rhosts_file || superuser)) {
349                first = 0;
350                if ((pwd = getpwnam(luser)) == NULL)
351                        return (-1);
352                (void)strcpy(pbuf, pwd->pw_dir);
353                (void)strcat(pbuf, "/.rhosts");
354
355                /*
356                 * Change effective uid while opening .rhosts.  If root and
357                 * reading an NFS mounted file system, can't read files that
358                 * are protected read/write owner only.
359                 */
360                uid = geteuid();
361                (void)seteuid(pwd->pw_uid);
362                hostf = fopen(pbuf, "r");
363                (void)seteuid(uid);
364
365                if (hostf == NULL)
366                        return (-1);
367                /*
368                 * If not a regular file, or is owned by someone other than
369                 * user or root or if writeable by anyone but the owner, quit.
370                 */
371                cp = NULL;
372                if (lstat(pbuf, &sbuf) < 0)
373                        cp = ".rhosts lstat failed";
374                else if (!S_ISREG(sbuf.st_mode))
375                        cp = ".rhosts not regular file";
376                else if (fstat(fileno(hostf), &sbuf) < 0)
377                        cp = ".rhosts fstat failed";
378                else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid)
379                        cp = "bad .rhosts owner";
380                else if (sbuf.st_mode & (S_IWGRP|S_IWOTH))
381                        cp = ".rhosts writeable by other than owner";
382                /* If there were any problems, quit. */
383                if (cp) {
384                        __rcmd_errstr = cp;
385                        (void)fclose(hostf);
386                        return (-1);
387                }
388                goto again;
389        }
390        return (-1);
391}
392
393/*
394 * XXX
395 * Don't make static, used by lpd(8).
396 *
397 * Returns 0 if ok, -1 if not ok.
398 */
399int
400__ivaliduser(hostf, raddr, luser, ruser)
401        FILE *hostf;
402        u_long raddr;
403        const char *luser, *ruser;
404{
405        register char *user, *p;
406        int ch;
407        char buf[MAXHOSTNAMELEN + 128];         /* host + login */
408        char hname[MAXHOSTNAMELEN];
409        struct hostent *hp;
410        /* Presumed guilty until proven innocent. */
411        int userok = 0, hostok = 0;
412#ifdef YP
413        char *ypdomain;
414
415        if (yp_get_default_domain(&ypdomain))
416                ypdomain = NULL;
417#else
418#define ypdomain NULL
419#endif
420        /* We need to get the damn hostname back for netgroup matching. */
421        if ((hp = gethostbyaddr((char *)&raddr, sizeof(u_long),
422                                                        AF_INET)) == NULL)
423                return (-1);
424        strncpy(hname, hp->h_name, sizeof(hname));
425        hname[sizeof(hname) - 1] = '\0';
426
427        while (fgets(buf, sizeof(buf), hostf)) {
428                p = buf;
429                /* Skip lines that are too long. */
430                if (strchr(p, '\n') == NULL) {
431                        while ((ch = getc(hostf)) != '\n' && ch != EOF);
432                        continue;
433                }
434                if (*p == '\n' || *p == '#') {
435                        /* comment... */
436                        continue;
437                }
438                while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') {
439                        *p = isupper(*p) ? tolower(*p) : *p;
440                        p++;
441                }
442                if (*p == ' ' || *p == '\t') {
443                        *p++ = '\0';
444                        while (*p == ' ' || *p == '\t')
445                                p++;
446                        user = p;
447                        while (*p != '\n' && *p != ' ' &&
448                            *p != '\t' && *p != '\0')
449                                p++;
450                } else
451                        user = p;
452                *p = '\0';
453                /*
454                 * Do +/- and +@/-@ checking. This looks really nasty,
455                 * but it matches SunOS's behavior so far as I can tell.
456                 */
457                switch(buf[0]) {
458                case '+':
459                        if (!buf[1]) {     /* '+' matches all hosts */
460                                hostok = 1;
461                                break;
462                        }
463                        if (buf[1] == '@')  /* match a host by netgroup */
464                                hostok = innetgr((char *)&buf[2],
465                                        (char *)&hname, NULL, ypdomain);
466                        else            /* match a host by addr */
467                                hostok = __icheckhost(raddr,(char *)&buf[1]);
468                        break;
469                case '-':     /* reject '-' hosts and all their users */
470                        if (buf[1] == '@') {
471                                if (innetgr((char *)&buf[2],
472                                              (char *)&hname, NULL, ypdomain))
473                                        return(-1);
474                        } else {
475                                if (__icheckhost(raddr,(char *)&buf[1]))
476                                        return(-1);
477                        }
478                        break;
479                default:  /* if no '+' or '-', do a simple match */
480                        hostok = __icheckhost(raddr, buf);
481                        break;
482                }
483                switch(*user) {
484                case '+':
485                        if (!*(user+1)) {      /* '+' matches all users */
486                                userok = 1;
487                                break;
488                        }
489                        if (*(user+1) == '@')  /* match a user by netgroup */
490                                userok = innetgr(user+2, NULL, ruser, ypdomain);
491                        else       /* match a user by direct specification */
492                                userok = !(strcmp(ruser, user+1));
493                        break;
494                case '-':               /* if we matched a hostname, */
495                        if (hostok) {   /* check for user field rejections */
496                                if (!*(user+1))
497                                        return(-1);
498                                if (*(user+1) == '@') {
499                                        if (innetgr(user+2, NULL,
500                                                        ruser, ypdomain))
501                                                return(-1);
502                                } else {
503                                        if (!strcmp(ruser, user+1))
504                                                return(-1);
505                                }
506                        }
507                        break;
508                default:        /* no rejections: try to match the user */
509                        if (hostok)
510                                userok = !(strcmp(ruser,*user ? user : luser));
511                        break;
512                }
513                if (hostok && userok)
514                        return(0);
515        }
516        return (-1);
517}
518
519/*
520 * Returns "true" if match, 0 if no match.
521 */
522static int
523__icheckhost(raddr, lhost)
524        u_long raddr;
525        register char *lhost;
526{
527        register struct hostent *hp;
528        register u_long laddr;
529        register char **pp;
530
531        /* Try for raw ip address first. */
532        if (isdigit(*lhost) && (long)(laddr = inet_addr(lhost)) != -1)
533                return (raddr == laddr);
534
535        /* Better be a hostname. */
536        if ((hp = gethostbyname(lhost)) == NULL)
537                return (0);
538
539        /* Spin through ip addresses. */
540        for (pp = hp->h_addr_list; *pp; ++pp)
541                if (!bcmp(&raddr, *pp, sizeof(u_long)))
542                        return (1);
543
544        /* No match. */
545        return (0);
546}
547#endif
Note: See TracBrowser for help on using the repository browser.