BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / lpr / lpd / lpd.c
index bf77d49..edfda93 100644 (file)
@@ -1,18 +1,45 @@
 /*
  * Copyright (c) 1983 Regents of the University of California.
 /*
  * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
  */
 
 #ifndef lint
 char copyright[] =
 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  All rights reserved.\n";
-#endif not lint
+#endif /* not lint */
 
 #ifndef lint
 
 #ifndef lint
-static char sccsid[] = "@(#)lpd.c      5.3 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)lpd.c      5.12 (Berkeley) 3/7/91";
+#endif /* not lint */
 
 /*
  * lpd -- line printer daemon.
 
 /*
  * lpd -- line printer daemon.
@@ -44,17 +71,18 @@ static char sccsid[] = "@(#)lpd.c   5.3 (Berkeley) %G%";
  */
 
 #include "lp.h"
  */
 
 #include "lp.h"
+#include "pathnames.h"
 
 int    lflag;                          /* log requests flag */
 
 int    lflag;                          /* log requests flag */
+int    from_remote;                    /* from remote socket */
 
 
-int    reapchild();
-int    mcleanup();
+void mcleanup(), reapchild();
 
 main(argc, argv)
        int argc;
        char **argv;
 {
 
 main(argc, argv)
        int argc;
        char **argv;
 {
-       int f, funix, finet, options, defreadfds, fromlen;
+       int f, funix, finet, options = 0, defreadfds, fromlen;
        struct sockaddr_un sun, fromunix;
        struct sockaddr_in sin, frominet;
        int omask, lfd;
        struct sockaddr_un sun, fromunix;
        struct sockaddr_in sin, frominet;
        int omask, lfd;
@@ -79,31 +107,20 @@ main(argc, argv)
        /*
         * Set up standard environment by detaching from the parent.
         */
        /*
         * Set up standard environment by detaching from the parent.
         */
-       if (fork())
-               exit(0);
-       for (f = 0; f < 5; f++)
-               (void) close(f);
-       (void) open("/dev/null", O_RDONLY);
-       (void) open("/dev/null", O_WRONLY);
-       (void) dup(1);
-       f = open("/dev/tty", O_RDWR);
-       if (f > 0) {
-               ioctl(f, TIOCNOTTY, 0);
-               (void) close(f);
-       }
+       daemon(0, 0);
 #endif
 
        openlog("lpd", LOG_PID, LOG_LPR);
        (void) umask(0);
 #endif
 
        openlog("lpd", LOG_PID, LOG_LPR);
        (void) umask(0);
-       lfd = open(MASTERLOCK, O_WRONLY|O_CREAT, 0644);
+       lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
        if (lfd < 0) {
        if (lfd < 0) {
-               syslog(LOG_ERR, "%s: %m", MASTERLOCK);
+               syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
                exit(1);
        }
        if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
                if (errno == EWOULDBLOCK)       /* active deamon present */
                        exit(0);
                exit(1);
        }
        if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
                if (errno == EWOULDBLOCK)       /* active deamon present */
                        exit(0);
-               syslog(LOG_ERR, "%s: %m", MASTERLOCK);
+               syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
                exit(1);
        }
        ftruncate(lfd, 0);
                exit(1);
        }
        ftruncate(lfd, 0);
@@ -113,7 +130,7 @@ main(argc, argv)
        sprintf(line, "%u\n", getpid());
        f = strlen(line);
        if (write(lfd, line, f) != f) {
        sprintf(line, "%u\n", getpid());
        f = strlen(line);
        if (write(lfd, line, f) != f) {
-               syslog(LOG_ERR, "%s: %m", MASTERLOCK);
+               syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
                exit(1);
        }
        signal(SIGCHLD, reapchild);
                exit(1);
        }
        signal(SIGCHLD, reapchild);
@@ -121,7 +138,7 @@ main(argc, argv)
         * Restart all the printers.
         */
        startup();
         * Restart all the printers.
         */
        startup();
-       (void) unlink(SOCKETNAME);
+       (void) unlink(_PATH_SOCKETNAME);
        funix = socket(AF_UNIX, SOCK_STREAM, 0);
        if (funix < 0) {
                syslog(LOG_ERR, "socket: %m");
        funix = socket(AF_UNIX, SOCK_STREAM, 0);
        if (funix < 0) {
                syslog(LOG_ERR, "socket: %m");
@@ -134,8 +151,9 @@ main(argc, argv)
        signal(SIGQUIT, mcleanup);
        signal(SIGTERM, mcleanup);
        sun.sun_family = AF_UNIX;
        signal(SIGQUIT, mcleanup);
        signal(SIGTERM, mcleanup);
        sun.sun_family = AF_UNIX;
-       strcpy(sun.sun_path, SOCKETNAME);
-       if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
+       strcpy(sun.sun_path, _PATH_SOCKETNAME);
+       if (bind(funix,
+            (struct sockaddr *)&sun, strlen(sun.sun_path) + 2) < 0) {
                syslog(LOG_ERR, "ubind: %m");
                exit(1);
        }
                syslog(LOG_ERR, "ubind: %m");
                exit(1);
        }
@@ -158,7 +176,7 @@ main(argc, argv)
                }
                sin.sin_family = AF_INET;
                sin.sin_port = sp->s_port;
                }
                sin.sin_family = AF_INET;
                sin.sin_port = sp->s_port;
-               if (bind(finet, &sin, sizeof(sin), 0) < 0) {
+               if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
                        syslog(LOG_ERR, "bind: %m");
                        mcleanup();
                }
                        syslog(LOG_ERR, "bind: %m");
                        mcleanup();
                }
@@ -179,10 +197,12 @@ main(argc, argv)
                }
                if (readfds & (1 << funix)) {
                        domain = AF_UNIX, fromlen = sizeof(fromunix);
                }
                if (readfds & (1 << funix)) {
                        domain = AF_UNIX, fromlen = sizeof(fromunix);
-                       s = accept(funix, &fromunix, &fromlen);
+                       s = accept(funix,
+                           (struct sockaddr *)&fromunix, &fromlen);
                } else if (readfds & (1 << finet)) {
                        domain = AF_INET, fromlen = sizeof(frominet);
                } else if (readfds & (1 << finet)) {
                        domain = AF_INET, fromlen = sizeof(frominet);
-                       s = accept(finet, &frominet, &fromlen);
+                       s = accept(finet,
+                           (struct sockaddr *)&frominet, &fromlen);
                }
                if (s < 0) {
                        if (errno != EINTR)
                }
                if (s < 0) {
                        if (errno != EINTR)
@@ -199,8 +219,11 @@ main(argc, argv)
                        (void) close(finet);
                        dup2(s, 1);
                        (void) close(s);
                        (void) close(finet);
                        dup2(s, 1);
                        (void) close(s);
-                       if (domain == AF_INET)
+                       if (domain == AF_INET) {
+                               from_remote = 1;
                                chkhost(&frominet);
                                chkhost(&frominet);
+                       } else
+                               from_remote = 0;
                        doit();
                        exit(0);
                }
                        doit();
                        exit(0);
                }
@@ -208,19 +231,21 @@ main(argc, argv)
        }
 }
 
        }
 }
 
+void
 reapchild()
 {
        union wait status;
 
 reapchild()
 {
        union wait status;
 
-       while (wait3(&status, WNOHANG, 0) > 0)
+       while (wait3((int *)&status, WNOHANG, 0) > 0)
                ;
 }
 
                ;
 }
 
+void
 mcleanup()
 {
        if (lflag)
                syslog(LOG_INFO, "exiting");
 mcleanup()
 {
        if (lflag)
                syslog(LOG_INFO, "exiting");
-       unlink(SOCKETNAME);
+       unlink(_PATH_SOCKETNAME);
        exit(0);
 }
 
        exit(0);
 }
 
@@ -276,6 +301,10 @@ doit()
                        printjob();
                        break;
                case '\2':      /* receive files to be queued */
                        printjob();
                        break;
                case '\2':      /* receive files to be queued */
+                       if (!from_remote) {
+                               syslog(LOG_INFO, "illegal request (%d)", *cp);
+                               exit(1);
+                       }
                        printer = cp;
                        recvjob();
                        break;
                        printer = cp;
                        recvjob();
                        break;
@@ -305,6 +334,10 @@ doit()
                        displayq(cbuf[0] - '\3');
                        exit(0);
                case '\5':      /* remove a job from the queue */
                        displayq(cbuf[0] - '\3');
                        exit(0);
                case '\5':      /* remove a job from the queue */
+                       if (!from_remote) {
+                               syslog(LOG_INFO, "illegal request (%d)", *cp);
+                               exit(1);
+                       }
                        printer = cp;
                        while (*cp && *cp != ' ')
                                cp++;
                        printer = cp;
                        while (*cp && *cp != ' ')
                                cp++;
@@ -371,6 +404,8 @@ startup()
        }
 }
 
        }
 }
 
+#define DUMMY ":nobody::"
+
 /*
  * Check to see if the from host has access to the line printer.
  */
 /*
  * Check to see if the from host has access to the line printer.
  */
@@ -379,15 +414,17 @@ chkhost(f)
 {
        register struct hostent *hp;
        register FILE *hostf;
 {
        register struct hostent *hp;
        register FILE *hostf;
-       register char *cp;
+       register char *cp, *sp;
        char ahost[50];
        int first = 1;
        extern char *inet_ntoa();
        char ahost[50];
        int first = 1;
        extern char *inet_ntoa();
+       int baselen = -1;
 
        f->sin_port = ntohs(f->sin_port);
        if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
                fatal("Malformed from address");
 
        f->sin_port = ntohs(f->sin_port);
        if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
                fatal("Malformed from address");
-       hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
+       hp = gethostbyaddr((char *)&f->sin_addr,
+           sizeof(struct in_addr), f->sin_family);
        if (hp == 0)
                fatal("Host name for your address (%s) unknown",
                        inet_ntoa(f->sin_addr));
        if (hp == 0)
                fatal("Host name for your address (%s) unknown",
                        inet_ntoa(f->sin_addr));
@@ -397,23 +434,30 @@ chkhost(f)
        if (!strcmp(from, host))
                return;
 
        if (!strcmp(from, host))
                return;
 
-       hostf = fopen("/etc/hosts.equiv", "r");
+       sp = fromb;
+       cp = ahost;
+       while (*sp) {
+               if (*sp == '.') {
+                       if (baselen == -1)
+                               baselen = sp - fromb;
+                       *cp++ = *sp++;
+               } else {
+                       *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++;
+               }
+       }
+       *cp = '\0';
+       hostf = fopen(_PATH_HOSTSEQUIV, "r");
 again:
        if (hostf) {
 again:
        if (hostf) {
-               while (fgets(ahost, sizeof (ahost), hostf)) {
-                       if (cp = index(ahost, '\n'))
-                               *cp = '\0';
-                       cp = index(ahost, ' ');
-                       if (!strcmp(from, ahost) && cp == NULL) {
-                               (void) fclose(hostf);
-                               return;
-                       }
+               if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) {
+                       (void) fclose(hostf);
+                       return;
                }
                (void) fclose(hostf);
        }
        if (first == 1) {
                first = 0;
                }
                (void) fclose(hostf);
        }
        if (first == 1) {
                first = 0;
-               hostf = fopen("/etc/hosts.lpd", "r");
+               hostf = fopen(_PATH_HOSTSLPD, "r");
                goto again;
        }
        fatal("Your host does not have line printer access");
                goto again;
        }
        fatal("Your host does not have line printer access");