source: rtems/cpukit/libnetworking/libc/rcmd.c @ 9b4422a2

4.115
Last change on this file since 9b4422a2 was 9b4422a2, checked in by Joel Sherrill <joel.sherrill@…>, on 05/03/12 at 15:09:24

Remove All CVS Id Strings Possible Using a Script

Script does what is expected and tries to do it as
smartly as possible.

+ remove occurrences of two blank comment lines

next to each other after Id string line removed.

+ remove entire comment blocks which only exited to

contain CVS Ids

+ If the processing left a blank line at the top of

a file, it was removed.

  • Property mode set to 100644
File size: 13.2 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
34#if HAVE_CONFIG_H
35#include "config.h"
36#endif
37
38#if HAVE_DECL_RCMD
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#include <rpc/rpc.h>
57#include <sys/select.h>
58#ifdef YP
59#include <rpcsvc/yp_prot.h>
60#include <rpcsvc/ypclnt.h>
61#endif
62
63#ifndef __rtems__
64extern int innetgr( const char *, const char *, const char *, const char * );
65#endif
66
67#define max(a, b)       ((a > b) ? a : b)
68
69#ifdef __rtems__
70int rresvport(int *alport);
71#define bzero(a,s)              memset((a),0,(s))
72#define bcmp                    memcmp
73#define bcopy(s,d,i)    memcpy(d,s,i)
74#else /* __rtems__ */
75
76int __ivaliduser(FILE *, u_int32_t, const char *, const char *);
77static int __icheckhost(const struct sockaddr *, socklen_t, const char *);
78
79#endif
80
81int
82rcmd(
83        char **ahost,
84        int rport,
85        const char *locuser,
86        const char *remuser,
87        const char *cmd,
88        int *fd2p )
89{
90        struct hostent *hp;
91        struct sockaddr_in sin, from;
92        fd_set reads;
93#ifndef __rtems__
94        long oldmask;
95#endif
96        pid_t pid;
97        int s, lport, timo;
98        char c;
99
100        pid = getpid();
101        hp = gethostbyname(*ahost);
102        if (hp == NULL) {
103                herror(*ahost);
104                return (-1);
105        }
106        *ahost = hp->h_name;
107#ifndef __rtems__
108        oldmask = sigblock(sigmask(SIGURG));
109#endif
110        for (timo = 1, lport = IPPORT_RESERVED - 1;;) {
111                s = rresvport(&lport);
112                if (s < 0) {
113                        if (errno == EAGAIN)
114                                (void)fprintf(stderr,
115                                    "rcmd: socket: All ports in use\n");
116                        else
117                                (void)fprintf(stderr, "rcmd: socket: %s\n",
118                                    strerror(errno));
119#ifndef __rtems__
120                        sigsetmask(oldmask);
121#endif
122                        return (-1);
123                }
124                fcntl(s, F_SETOWN, pid);
125                bzero(&sin, sizeof sin);
126                sin.sin_len = sizeof(struct sockaddr_in);
127                sin.sin_family = hp->h_addrtype;
128                sin.sin_port = rport;
129                bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
130                if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
131                        break;
132                (void)close(s);
133                if (errno == EADDRINUSE) {
134                        lport--;
135                        continue;
136                }
137                if (errno == ECONNREFUSED && timo <= 16) {
138                        (void)sleep(timo);
139                        timo *= 2;
140                        continue;
141                }
142                if (hp->h_addr_list[1] != NULL) {
143                        int oerrno = errno;
144
145                        (void)fprintf(stderr, "connect to address %s: ",
146                            inet_ntoa(sin.sin_addr));
147                        errno = oerrno;
148                        perror(0);
149                        hp->h_addr_list++;
150                        bcopy(hp->h_addr_list[0], &sin.sin_addr, MIN(hp->h_length, sizeof sin.sin_addr));
151                        (void)fprintf(stderr, "Trying %s...\n",
152                            inet_ntoa(sin.sin_addr));
153                        continue;
154                }
155                (void)fprintf(stderr, "%s: %s\n", hp->h_name, strerror(errno));
156#ifndef __rtems__
157                sigsetmask(oldmask);
158#endif
159                return (-1);
160        }
161        lport--;
162        if (fd2p == 0) {
163                write(s, "", 1);
164                lport = 0;
165        } else {
166                char num[8];
167                int s2 = rresvport(&lport), s3;
168                socklen_t len = sizeof(from);
169                int nfds;
170
171                if (s2 < 0)
172                        goto bad;
173                listen(s2, 1);
174                (void)snprintf(num, sizeof(num), "%d", lport);
175                if (write(s, num, strlen(num)+1) != strlen(num)+1) {
176                        (void)fprintf(stderr,
177                            "rcmd: write (setting up stderr): %s\n",
178                            strerror(errno));
179                        (void)close(s2);
180                        goto bad;
181                }
182                nfds = max(s, s2)+1;
183                if(nfds > FD_SETSIZE) {
184                        fprintf(stderr, "rcmd: too many files\n");
185                        (void)close(s2);
186                        goto bad;
187                }
188again:
189                FD_ZERO(&reads);
190                FD_SET(s, &reads);
191                FD_SET(s2, &reads);
192                errno = 0;
193                if (select(nfds, &reads, 0, 0, 0) < 1 || !FD_ISSET(s2, &reads)){
194                        if (errno != 0)
195                                (void)fprintf(stderr,
196                                    "rcmd: select (setting up stderr): %s\n",
197                                    strerror(errno));
198                        else
199                                (void)fprintf(stderr,
200                                "select: protocol failure in circuit setup\n");
201                        (void)close(s2);
202                        goto bad;
203                }
204                s3 = accept(s2, (struct sockaddr *)&from, &len);
205                /*
206                 * XXX careful for ftp bounce attacks. If discovered, shut them
207                 * down and check for the real auxiliary channel to connect.
208                 */
209                if (from.sin_family == AF_INET && from.sin_port == htons(20)) {
210                        close(s3);
211                        goto again;
212                }
213                (void)close(s2);
214                if (s3 < 0) {
215                        (void)fprintf(stderr,
216                            "rcmd: accept: %s\n", strerror(errno));
217                        lport = 0;
218                        goto bad;
219                }
220                *fd2p = s3;
221                from.sin_port = ntohs((u_short)from.sin_port);
222                if (from.sin_family != AF_INET ||
223                    from.sin_port >= IPPORT_RESERVED ||
224                    from.sin_port < IPPORT_RESERVED / 2) {
225                        (void)fprintf(stderr,
226                            "socket: protocol failure in circuit setup.\n");
227                        goto bad2;
228                }
229        }
230        (void)write(s, locuser, strlen(locuser)+1);
231        (void)write(s, remuser, strlen(remuser)+1);
232        (void)write(s, cmd, strlen(cmd)+1);
233        if (read(s, &c, 1) != 1) {
234                (void)fprintf(stderr,
235                    "rcmd: %s: %s\n", *ahost, strerror(errno));
236                goto bad2;
237        }
238        if (c != 0) {
239                while (read(s, &c, 1) == 1) {
240                        (void)write(STDERR_FILENO, &c, 1);
241                        if (c == '\n')
242                                break;
243                }
244                goto bad2;
245        }
246#ifndef __rtems__
247        sigsetmask(oldmask);
248#endif
249        return (s);
250bad2:
251        if (lport)
252                (void)close(*fd2p);
253bad:
254        (void)close(s);
255#ifndef __rtems__
256        sigsetmask(oldmask);
257#endif
258        return (-1);
259}
260
261int
262rresvport(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
548#endif
Note: See TracBrowser for help on using the repository browser.