new copyright notice
[unix-history] / usr / src / usr.sbin / lpr / lpd / lpd.c
index 747ee76..b9ab84c 100644 (file)
@@ -1,6 +1,19 @@
+/*
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)lpd.c      4.11 (Berkeley) %G%";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)lpd.c      5.8 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * lpd -- line printer daemon.
 
 /*
  * lpd -- line printer daemon.
@@ -17,12 +30,6 @@ static char sccsid[] = "@(#)lpd.c    4.11 (Berkeley) %G%";
  *             return the current state of the queue (long form).
  *     \5printer person [users ...] [jobs ...]\n
  *             remove jobs from the queue.
  *             return the current state of the queue (long form).
  *     \5printer person [users ...] [jobs ...]\n
  *             remove jobs from the queue.
- *     \6printer\n
- *             enable queuing on the specified printer queue.
- *     \7printer\n
- *             disable queuing on the specified printer queue.
- *     \8printer\n
- *             return the queue status (queuing enabled or disabled).
  *
  * Strategy to maintain protected spooling area:
  *     1. Spooling area is writable only by daemon and spooling group
  *
  * Strategy to maintain protected spooling area:
  *     1. Spooling area is writable only by daemon and spooling group
@@ -38,12 +45,12 @@ static char sccsid[] = "@(#)lpd.c   4.11 (Berkeley) %G%";
  */
 
 #include "lp.h"
  */
 
 #include "lp.h"
+#include "pathnames.h"
 
 
-static int     lflag;                          /* log requests flag */
-static char    *logfile = DEFLOGF;
+int    lflag;                          /* log requests flag */
 
 int    reapchild();
 
 int    reapchild();
-int    cleanup();
+int    mcleanup();
 
 main(argc, argv)
        int argc;
 
 main(argc, argv)
        int argc;
@@ -67,10 +74,6 @@ main(argc, argv)
                        case 'l':
                                lflag++;
                                break;
                        case 'l':
                                lflag++;
                                break;
-                       case 'L':
-                               argc--;
-                               logfile = *++argv;
-                               break;
                        }
        }
 
                        }
        }
 
@@ -80,28 +83,29 @@ main(argc, argv)
         */
        if (fork())
                exit(0);
         */
        if (fork())
                exit(0);
-       for (f = 0; f < 3; f++)
+       for (f = 0; f < 5; f++)
                (void) close(f);
                (void) close(f);
-       (void) open("/dev/null", O_RDONLY);
-       (void) open("/dev/null", O_WRONLY);
-       (void) open(logfile, O_WRONLY|O_APPEND);
-       f = open("/dev/tty", O_RDWR);
+       (void) open(_PATH_DEVNULL, O_RDONLY);
+       (void) open(_PATH_DEVNULL, O_WRONLY);
+       (void) dup(1);
+       f = open(_PATH_TTY, O_RDWR);
        if (f > 0) {
                ioctl(f, TIOCNOTTY, 0);
                (void) close(f);
        }
 #endif
 
        if (f > 0) {
                ioctl(f, TIOCNOTTY, 0);
                (void) close(f);
        }
 #endif
 
+       openlog("lpd", LOG_PID, LOG_LPR);
        (void) umask(0);
        (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) {
-               log("cannot create %s", 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);
-               log("cannot lock %s", MASTERLOCK);
+               syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
                exit(1);
        }
        ftruncate(lfd, 0);
                exit(1);
        }
        ftruncate(lfd, 0);
@@ -111,7 +115,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) {
-               log("cannot write daemon pid");
+               syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
                exit(1);
        }
        signal(SIGCHLD, reapchild);
                exit(1);
        }
        signal(SIGCHLD, reapchild);
@@ -119,22 +123,22 @@ 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) {
        funix = socket(AF_UNIX, SOCK_STREAM, 0);
        if (funix < 0) {
-               logerr("socket");
+               syslog(LOG_ERR, "socket: %m");
                exit(1);
        }
 #define        mask(s) (1 << ((s) - 1))
        omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
                exit(1);
        }
 #define        mask(s) (1 << ((s) - 1))
        omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
-       signal(SIGHUP, cleanup);
-       signal(SIGINT, cleanup);
-       signal(SIGQUIT, cleanup);
-       signal(SIGTERM, cleanup);
+       signal(SIGHUP, mcleanup);
+       signal(SIGINT, mcleanup);
+       signal(SIGQUIT, mcleanup);
+       signal(SIGTERM, mcleanup);
        sun.sun_family = AF_UNIX;
        sun.sun_family = AF_UNIX;
-       strcpy(sun.sun_path, SOCKETNAME);
+       strcpy(sun.sun_path, _PATH_SOCKETNAME);
        if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
        if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
-               logerr("unix domain bind");
+               syslog(LOG_ERR, "ubind: %m");
                exit(1);
        }
        sigsetmask(omask);
                exit(1);
        }
        sigsetmask(omask);
@@ -146,19 +150,19 @@ main(argc, argv)
 
                if (options & SO_DEBUG)
                        if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
 
                if (options & SO_DEBUG)
                        if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
-                               logerr("setsockopt (SO_DEBUG)");
-                               cleanup();
+                               syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
+                               mcleanup();
                        }
                sp = getservbyname("printer", "tcp");
                if (sp == NULL) {
                        }
                sp = getservbyname("printer", "tcp");
                if (sp == NULL) {
-                       log("printer/tcp: unknown service");
-                       cleanup();
+                       syslog(LOG_ERR, "printer/tcp: unknown service");
+                       mcleanup();
                }
                sin.sin_family = AF_INET;
                sin.sin_port = sp->s_port;
                if (bind(finet, &sin, sizeof(sin), 0) < 0) {
                }
                sin.sin_family = AF_INET;
                sin.sin_port = sp->s_port;
                if (bind(finet, &sin, sizeof(sin), 0) < 0) {
-                       logerr("internet domain bind");
-                       cleanup();
+                       syslog(LOG_ERR, "bind: %m");
+                       mcleanup();
                }
                defreadfds |= 1 << finet;
                listen(finet, 5);
                }
                defreadfds |= 1 << finet;
                listen(finet, 5);
@@ -171,11 +175,8 @@ main(argc, argv)
 
                nfds = select(20, &readfds, 0, 0, 0);
                if (nfds <= 0) {
 
                nfds = select(20, &readfds, 0, 0, 0);
                if (nfds <= 0) {
-                       if (nfds < 0 && errno != EINTR) {
-                               logerr("select");
-                               cleanup();
-                               /*NOTREACHED*/
-                       }
+                       if (nfds < 0 && errno != EINTR)
+                               syslog(LOG_WARNING, "select: %m");
                        continue;
                }
                if (readfds & (1 << funix)) {
                        continue;
                }
                if (readfds & (1 << funix)) {
@@ -186,10 +187,9 @@ main(argc, argv)
                        s = accept(finet, &frominet, &fromlen);
                }
                if (s < 0) {
                        s = accept(finet, &frominet, &fromlen);
                }
                if (s < 0) {
-                       if (errno == EINTR)
-                               continue;
-                       logerr("accept");
-                       cleanup();
+                       if (errno != EINTR)
+                               syslog(LOG_WARNING, "accept: %m");
+                       continue;
                }
                if (fork() == 0) {
                        signal(SIGCHLD, SIG_IGN);
                }
                if (fork() == 0) {
                        signal(SIGCHLD, SIG_IGN);
@@ -210,7 +210,6 @@ main(argc, argv)
        }
 }
 
        }
 }
 
-static
 reapchild()
 {
        union wait status;
 reapchild()
 {
        union wait status;
@@ -219,12 +218,11 @@ reapchild()
                ;
 }
 
                ;
 }
 
-static
-cleanup()
+mcleanup()
 {
        if (lflag)
 {
        if (lflag)
-               log("cleanup()");
-       unlink(SOCKETNAME);
+               syslog(LOG_INFO, "exiting");
+       unlink(_PATH_SOCKETNAME);
        exit(0);
 }
 
        exit(0);
 }
 
@@ -237,9 +235,9 @@ int requ[MAXREQUESTS];      /* job number of spool entries */
 int    requests;               /* # of spool requests */
 char   *person;                /* name of person doing lprm */
 
 int    requests;               /* # of spool requests */
 char   *person;                /* name of person doing lprm */
 
-static char    fromb[32];      /* buffer for client's machine name */
-static char    cbuf[BUFSIZ];   /* command line buffer */
-static char    *cmdnames[] = {
+char   fromb[32];      /* buffer for client's machine name */
+char   cbuf[BUFSIZ];   /* command line buffer */
+char   *cmdnames[] = {
        "null",
        "printjob",
        "recvjob",
        "null",
        "printjob",
        "recvjob",
@@ -248,7 +246,6 @@ static char *cmdnames[] = {
        "rmjob"
 };
 
        "rmjob"
 };
 
-static
 doit()
 {
        register char *cp;
 doit()
 {
        register char *cp;
@@ -267,9 +264,13 @@ doit()
                } while (*cp++ != '\n');
                *--cp = '\0';
                cp = cbuf;
                } while (*cp++ != '\n');
                *--cp = '\0';
                cp = cbuf;
-               if (lflag && *cp >= '\1' && *cp <= '\5') {
-                       printer = NULL;
-                       log("%s requests %s %s", from, cmdnames[*cp], cp+1);
+               if (lflag) {
+                       if (*cp >= '\1' && *cp <= '\5')
+                               syslog(LOG_INFO, "%s requests %s %s",
+                                       from, cmdnames[*cp], cp+1);
+                       else
+                               syslog(LOG_INFO, "bad request (%d) from %s",
+                                       *cp, from);
                }
                switch (*cp++) {
                case '\1':      /* check the queue and print any jobs there */
                }
                switch (*cp++) {
                case '\1':      /* check the queue and print any jobs there */
@@ -344,7 +345,6 @@ doit()
  * Make a pass through the printcap database and start printing any
  * files left from the last time the machine went down.
  */
  * Make a pass through the printcap database and start printing any
  * files left from the last time the machine went down.
  */
-static
 startup()
 {
        char buf[BUFSIZ];
 startup()
 {
        char buf[BUFSIZ];
@@ -363,8 +363,8 @@ startup()
                                break;
                        }
                if ((pid = fork()) < 0) {
                                break;
                        }
                if ((pid = fork()) < 0) {
-                       log("startup: cannot fork");
-                       cleanup();
+                       syslog(LOG_WARNING, "startup: cannot fork");
+                       mcleanup();
                }
                if (!pid) {
                        endprent();
                }
                if (!pid) {
                        endprent();
@@ -373,19 +373,21 @@ 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.
  */
-static
 chkhost(f)
        struct sockaddr_in *f;
 {
        register struct hostent *hp;
        register FILE *hostf;
 chkhost(f)
        struct sockaddr_in *f;
 {
        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)
 
        f->sin_port = ntohs(f->sin_port);
        if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
@@ -400,59 +402,31 @@ 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");
 }
-
-/*VARARGS1*/
-log(msg, a1, a2, a3)
-       char *msg;
-{
-       short console = isatty(fileno(stderr));
-
-       fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
-       if (printer)
-               fprintf(stderr, "%s: ", printer);
-       fprintf(stderr, msg, a1, a2, a3);
-       if (console)
-               putc('\r', stderr);
-       putc('\n', stderr);
-       fflush(stderr);
-}
-
-static
-logerr(msg)
-       char *msg;
-{
-       register int err = errno;
-       short console = isatty(fileno(stderr));
-       extern int sys_nerr;
-       extern char *sys_errlist[];
-
-       fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
-       if (msg)
-               fprintf(stderr, "%s: ", msg);
-       fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
-       if (console)
-               putc('\r', stderr);
-       putc('\n', stderr);
-       fflush(stderr);
-}