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

4.104.114.84.95
Last change on this file since abef0f6 was abef0f6, checked in by Joel Sherrill <joel.sherrill@…>, on 10/28/02 at 13:56:01

2002-10-28 Joel Sherrill <joel@…>

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