Home | History | Annotate | Line # | Download | only in lpd
lpd.c revision 1.19
      1  1.19      mjl /*	$NetBSD: lpd.c,v 1.19 1999/12/23 02:10:07 mjl Exp $	*/
      2   1.7      mrg 
      3   1.1      cgd /*
      4   1.4      cgd  * Copyright (c) 1983, 1993, 1994
      5   1.4      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.4      cgd  *
      7   1.1      cgd  *
      8   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      9   1.1      cgd  * modification, are permitted provided that the following conditions
     10   1.1      cgd  * are met:
     11   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     12   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     13   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     15   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     16   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     17   1.1      cgd  *    must display the following acknowledgement:
     18   1.1      cgd  *	This product includes software developed by the University of
     19   1.1      cgd  *	California, Berkeley and its contributors.
     20   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     21   1.1      cgd  *    may be used to endorse or promote products derived from this software
     22   1.1      cgd  *    without specific prior written permission.
     23   1.1      cgd  *
     24   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34   1.1      cgd  * SUCH DAMAGE.
     35   1.1      cgd  */
     36   1.1      cgd 
     37  1.11    mikel #include <sys/cdefs.h>
     38  1.11    mikel 
     39   1.1      cgd #ifndef lint
     40  1.11    mikel __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
     41  1.11    mikel 	The Regents of the University of California.  All rights reserved.\n");
     42   1.1      cgd #endif /* not lint */
     43   1.1      cgd 
     44   1.1      cgd #ifndef lint
     45  1.10    mikel #if 0
     46  1.12      mrg static char sccsid[] = "@(#)lpd.c	8.7 (Berkeley) 5/10/95";
     47  1.10    mikel #else
     48  1.19      mjl __RCSID("$NetBSD: lpd.c,v 1.19 1999/12/23 02:10:07 mjl Exp $");
     49  1.10    mikel #endif
     50   1.1      cgd #endif /* not lint */
     51   1.1      cgd 
     52   1.1      cgd /*
     53   1.1      cgd  * lpd -- line printer daemon.
     54   1.1      cgd  *
     55   1.1      cgd  * Listen for a connection and perform the requested operation.
     56   1.1      cgd  * Operations are:
     57   1.1      cgd  *	\1printer\n
     58   1.1      cgd  *		check the queue for jobs and print any found.
     59   1.1      cgd  *	\2printer\n
     60   1.1      cgd  *		receive a job from another machine and queue it.
     61   1.1      cgd  *	\3printer [users ...] [jobs ...]\n
     62   1.1      cgd  *		return the current state of the queue (short form).
     63   1.1      cgd  *	\4printer [users ...] [jobs ...]\n
     64   1.1      cgd  *		return the current state of the queue (long form).
     65   1.1      cgd  *	\5printer person [users ...] [jobs ...]\n
     66   1.1      cgd  *		remove jobs from the queue.
     67   1.1      cgd  *
     68   1.1      cgd  * Strategy to maintain protected spooling area:
     69   1.1      cgd  *	1. Spooling area is writable only by daemon and spooling group
     70   1.1      cgd  *	2. lpr runs setuid root and setgrp spooling group; it uses
     71   1.1      cgd  *	   root to access any file it wants (verifying things before
     72   1.1      cgd  *	   with an access call) and group id to know how it should
     73   1.1      cgd  *	   set up ownership of files in the spooling area.
     74   1.1      cgd  *	3. Files in spooling area are owned by root, group spooling
     75   1.1      cgd  *	   group, with mode 660.
     76   1.1      cgd  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
     77   1.1      cgd  *	   access files and printer.  Users can't get to anything
     78   1.1      cgd  *	   w/o help of lpq and lprm programs.
     79   1.1      cgd  */
     80   1.1      cgd 
     81   1.4      cgd #include <sys/param.h>
     82   1.4      cgd #include <sys/wait.h>
     83   1.4      cgd #include <sys/types.h>
     84   1.4      cgd #include <sys/socket.h>
     85   1.4      cgd #include <sys/un.h>
     86   1.4      cgd #include <sys/stat.h>
     87  1.12      mrg #include <sys/file.h>
     88   1.4      cgd #include <netinet/in.h>
     89   1.4      cgd 
     90  1.18      mrg #include <err.h>
     91   1.4      cgd #include <netdb.h>
     92   1.4      cgd #include <unistd.h>
     93   1.4      cgd #include <syslog.h>
     94   1.4      cgd #include <signal.h>
     95   1.4      cgd #include <errno.h>
     96   1.4      cgd #include <fcntl.h>
     97   1.4      cgd #include <dirent.h>
     98   1.4      cgd #include <stdio.h>
     99   1.4      cgd #include <stdlib.h>
    100   1.4      cgd #include <string.h>
    101   1.4      cgd #include <ctype.h>
    102  1.11    mikel #include <arpa/inet.h>
    103  1.11    mikel 
    104   1.1      cgd #include "lp.h"
    105   1.4      cgd #include "lp.local.h"
    106   1.1      cgd #include "pathnames.h"
    107   1.4      cgd #include "extern.h"
    108   1.1      cgd 
    109   1.1      cgd int	lflag;				/* log requests flag */
    110  1.18      mrg int	rflag;				/* allow of for remote printers */
    111   1.8    perry int	sflag;				/* secure (no inet) flag */
    112   1.1      cgd int	from_remote;			/* from remote socket */
    113   1.1      cgd 
    114  1.11    mikel int               main __P((int, char **));
    115   1.4      cgd static void       reapchild __P((int));
    116   1.4      cgd static void       mcleanup __P((int));
    117   1.4      cgd static void       doit __P((void));
    118   1.4      cgd static void       startup __P((void));
    119   1.4      cgd static void       chkhost __P((struct sockaddr_in *));
    120  1.12      mrg static int	  ckqueue __P((char *));
    121  1.13      mrg static void	  usage __P((void));
    122   1.1      cgd 
    123   1.5  hpeyerl uid_t	uid, euid;
    124  1.18      mrg int child_count;
    125   1.5  hpeyerl 
    126   1.4      cgd int
    127   1.1      cgd main(argc, argv)
    128   1.1      cgd 	int argc;
    129   1.1      cgd 	char **argv;
    130   1.1      cgd {
    131   1.4      cgd 	int f, funix, finet, options, fromlen;
    132   1.4      cgd 	fd_set defreadfds;
    133   1.4      cgd 	struct sockaddr_un un, fromunix;
    134   1.1      cgd 	struct sockaddr_in sin, frominet;
    135  1.13      mrg 	int omask, lfd, errs, i;
    136  1.18      mrg 	int child_max = 32;	/* more then enough to hose the system */
    137   1.1      cgd 
    138   1.5  hpeyerl 	euid = geteuid();	/* these shouldn't be different */
    139   1.5  hpeyerl 	uid = getuid();
    140   1.4      cgd 	options = 0;
    141   1.1      cgd 	gethostname(host, sizeof(host));
    142  1.16      mrg 	host[sizeof(host) - 1] = '\0';
    143   1.1      cgd 	name = argv[0];
    144   1.1      cgd 
    145  1.13      mrg 	errs = 0;
    146  1.18      mrg 	while ((i = getopt(argc, argv, "dln:srw:")) != -1)
    147  1.13      mrg 		switch (i) {
    148  1.13      mrg 		case 'd':
    149  1.13      mrg 			options |= SO_DEBUG;
    150  1.13      mrg 			break;
    151  1.13      mrg 		case 'l':
    152  1.13      mrg 			lflag++;
    153  1.14      mrg 			break;
    154  1.18      mrg 		case 'n':
    155  1.18      mrg 			child_max = atoi(optarg);
    156  1.18      mrg 			if (child_max < 0 || child_max > 1024)
    157  1.18      mrg 				errx(1, "invalid number of children: %s",
    158  1.18      mrg 				    optarg);
    159  1.18      mrg 			break;
    160  1.18      mrg 		case 'r':
    161  1.18      mrg 			rflag++;
    162  1.18      mrg 			break;
    163  1.14      mrg 		case 's':
    164  1.14      mrg 			sflag++;
    165  1.13      mrg 			break;
    166  1.18      mrg 		case 'w':
    167  1.18      mrg 			wait_time = atoi(optarg);
    168  1.18      mrg 			if (wait_time < 0)
    169  1.18      mrg 				errx(1, "wait time must be postive: %s",
    170  1.18      mrg 				    optarg);
    171  1.18      mrg 			if (wait_time < 30)
    172  1.18      mrg 			    warnx("warning: wait time less than 30 seconds");
    173  1.18      mrg 			break;
    174  1.13      mrg 		default:
    175  1.13      mrg 			errs++;
    176  1.13      mrg 		}
    177  1.13      mrg 	argc -= optind;
    178  1.13      mrg 	argv += optind;
    179  1.13      mrg 	if (errs || argc != 0)
    180  1.13      mrg 		usage();
    181   1.1      cgd 
    182   1.1      cgd #ifndef DEBUG
    183   1.1      cgd 	/*
    184   1.1      cgd 	 * Set up standard environment by detaching from the parent.
    185   1.1      cgd 	 */
    186   1.1      cgd 	daemon(0, 0);
    187   1.1      cgd #endif
    188   1.1      cgd 
    189   1.1      cgd 	openlog("lpd", LOG_PID, LOG_LPR);
    190   1.4      cgd 	syslog(LOG_INFO, "restarted");
    191   1.9      mrg 	(void)umask(0);
    192   1.1      cgd 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
    193   1.1      cgd 	if (lfd < 0) {
    194   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    195   1.1      cgd 		exit(1);
    196   1.1      cgd 	}
    197   1.1      cgd 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
    198   1.1      cgd 		if (errno == EWOULDBLOCK)	/* active deamon present */
    199   1.1      cgd 			exit(0);
    200   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    201   1.1      cgd 		exit(1);
    202   1.1      cgd 	}
    203   1.1      cgd 	ftruncate(lfd, 0);
    204   1.1      cgd 	/*
    205   1.1      cgd 	 * write process id for others to know
    206   1.1      cgd 	 */
    207   1.9      mrg 	(void)snprintf(line, sizeof(line), "%u\n", getpid());
    208   1.1      cgd 	f = strlen(line);
    209   1.1      cgd 	if (write(lfd, line, f) != f) {
    210   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    211   1.1      cgd 		exit(1);
    212   1.1      cgd 	}
    213   1.1      cgd 	signal(SIGCHLD, reapchild);
    214   1.1      cgd 	/*
    215   1.1      cgd 	 * Restart all the printers.
    216   1.1      cgd 	 */
    217   1.1      cgd 	startup();
    218   1.9      mrg 	(void)unlink(_PATH_SOCKETNAME);
    219  1.17    lukem 	funix = socket(AF_LOCAL, SOCK_STREAM, 0);
    220   1.1      cgd 	if (funix < 0) {
    221   1.1      cgd 		syslog(LOG_ERR, "socket: %m");
    222   1.1      cgd 		exit(1);
    223   1.1      cgd 	}
    224   1.1      cgd #define	mask(s)	(1 << ((s) - 1))
    225   1.1      cgd 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
    226   1.1      cgd 	signal(SIGHUP, mcleanup);
    227   1.1      cgd 	signal(SIGINT, mcleanup);
    228   1.1      cgd 	signal(SIGQUIT, mcleanup);
    229   1.1      cgd 	signal(SIGTERM, mcleanup);
    230   1.4      cgd 	memset(&un, 0, sizeof(un));
    231  1.17    lukem 	un.sun_family = AF_LOCAL;
    232   1.9      mrg 	strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
    233   1.4      cgd #ifndef SUN_LEN
    234   1.4      cgd #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    235   1.4      cgd #endif
    236   1.4      cgd 	if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
    237   1.1      cgd 		syslog(LOG_ERR, "ubind: %m");
    238   1.1      cgd 		exit(1);
    239   1.1      cgd 	}
    240   1.1      cgd 	sigsetmask(omask);
    241   1.4      cgd 	FD_ZERO(&defreadfds);
    242   1.4      cgd 	FD_SET(funix, &defreadfds);
    243   1.1      cgd 	listen(funix, 5);
    244   1.8    perry 	if (!sflag)
    245   1.8    perry 		finet = socket(AF_INET, SOCK_STREAM, 0);
    246   1.8    perry 	else
    247   1.8    perry 		finet = -1;	/* pretend we couldn't open TCP socket. */
    248   1.1      cgd 	if (finet >= 0) {
    249   1.1      cgd 		struct servent *sp;
    250   1.1      cgd 
    251   1.1      cgd 		if (options & SO_DEBUG)
    252   1.1      cgd 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
    253   1.1      cgd 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    254   1.4      cgd 				mcleanup(0);
    255   1.1      cgd 			}
    256   1.1      cgd 		sp = getservbyname("printer", "tcp");
    257   1.1      cgd 		if (sp == NULL) {
    258   1.1      cgd 			syslog(LOG_ERR, "printer/tcp: unknown service");
    259   1.4      cgd 			mcleanup(0);
    260   1.1      cgd 		}
    261   1.4      cgd 		memset(&sin, 0, sizeof(sin));
    262   1.1      cgd 		sin.sin_family = AF_INET;
    263   1.1      cgd 		sin.sin_port = sp->s_port;
    264   1.1      cgd 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    265   1.1      cgd 			syslog(LOG_ERR, "bind: %m");
    266   1.4      cgd 			mcleanup(0);
    267   1.1      cgd 		}
    268   1.4      cgd 		FD_SET(finet, &defreadfds);
    269   1.1      cgd 		listen(finet, 5);
    270   1.1      cgd 	}
    271   1.1      cgd 	/*
    272   1.1      cgd 	 * Main loop: accept, do a request, continue.
    273   1.1      cgd 	 */
    274   1.4      cgd 	memset(&frominet, 0, sizeof(frominet));
    275   1.4      cgd 	memset(&fromunix, 0, sizeof(fromunix));
    276   1.1      cgd 	for (;;) {
    277   1.4      cgd 		int domain, nfds, s;
    278   1.4      cgd 		fd_set readfds;
    279  1.18      mrg 		/* "short" so it overflows in about 2 hours */
    280  1.18      mrg 		short sleeptime = 10;
    281  1.18      mrg 
    282  1.18      mrg 		while (child_max < child_count) {
    283  1.18      mrg 			syslog(LOG_WARNING,
    284  1.18      mrg 			    "too many children, sleeping for %d seconds",
    285  1.18      mrg 				sleeptime);
    286  1.18      mrg 			sleep(sleeptime);
    287  1.18      mrg 			sleeptime <<= 1;
    288  1.18      mrg 			if (sleeptime < 0) {
    289  1.18      mrg 				syslog(LOG_CRIT, "sleeptime overflowed! help!");
    290  1.18      mrg 				sleeptime = 10;
    291  1.18      mrg 			}
    292  1.18      mrg 		}
    293   1.1      cgd 
    294   1.4      cgd 		FD_COPY(&defreadfds, &readfds);
    295   1.1      cgd 		nfds = select(20, &readfds, 0, 0, 0);
    296   1.1      cgd 		if (nfds <= 0) {
    297   1.1      cgd 			if (nfds < 0 && errno != EINTR)
    298   1.1      cgd 				syslog(LOG_WARNING, "select: %m");
    299   1.1      cgd 			continue;
    300   1.1      cgd 		}
    301   1.4      cgd 		if (FD_ISSET(funix, &readfds)) {
    302  1.17    lukem 			domain = AF_LOCAL, fromlen = sizeof(fromunix);
    303   1.1      cgd 			s = accept(funix,
    304   1.1      cgd 			    (struct sockaddr *)&fromunix, &fromlen);
    305   1.4      cgd 		} else /* if (FD_ISSET(finet, &readfds)) */  {
    306   1.1      cgd 			domain = AF_INET, fromlen = sizeof(frominet);
    307   1.1      cgd 			s = accept(finet,
    308   1.1      cgd 			    (struct sockaddr *)&frominet, &fromlen);
    309   1.1      cgd 		}
    310   1.1      cgd 		if (s < 0) {
    311   1.1      cgd 			if (errno != EINTR)
    312   1.1      cgd 				syslog(LOG_WARNING, "accept: %m");
    313   1.1      cgd 			continue;
    314   1.1      cgd 		}
    315  1.18      mrg 
    316  1.18      mrg 		switch (fork()) {
    317  1.18      mrg 		case 0:
    318   1.1      cgd 			signal(SIGCHLD, SIG_IGN);
    319   1.1      cgd 			signal(SIGHUP, SIG_IGN);
    320   1.1      cgd 			signal(SIGINT, SIG_IGN);
    321   1.1      cgd 			signal(SIGQUIT, SIG_IGN);
    322   1.1      cgd 			signal(SIGTERM, SIG_IGN);
    323   1.9      mrg 			(void)close(funix);
    324   1.8    perry 			if (!sflag)
    325   1.9      mrg 				(void)close(finet);
    326   1.1      cgd 			dup2(s, 1);
    327   1.9      mrg 			(void)close(s);
    328   1.1      cgd 			if (domain == AF_INET) {
    329   1.1      cgd 				from_remote = 1;
    330   1.1      cgd 				chkhost(&frominet);
    331   1.1      cgd 			} else
    332   1.1      cgd 				from_remote = 0;
    333   1.1      cgd 			doit();
    334   1.1      cgd 			exit(0);
    335  1.18      mrg 		case -1:
    336  1.18      mrg 			syslog(LOG_WARNING, "fork: %m, sleeping for 10 seconds...");
    337  1.18      mrg 			sleep(10);
    338  1.18      mrg 			continue;
    339  1.18      mrg 		default:
    340  1.18      mrg 			child_count++;
    341   1.1      cgd 		}
    342   1.9      mrg 		(void)close(s);
    343   1.1      cgd 	}
    344   1.1      cgd }
    345   1.1      cgd 
    346   1.4      cgd static void
    347   1.4      cgd reapchild(signo)
    348   1.4      cgd 	int signo;
    349   1.1      cgd {
    350   1.1      cgd 	union wait status;
    351   1.1      cgd 
    352   1.1      cgd 	while (wait3((int *)&status, WNOHANG, 0) > 0)
    353  1.18      mrg 		child_count--;
    354   1.1      cgd }
    355   1.1      cgd 
    356   1.4      cgd static void
    357   1.4      cgd mcleanup(signo)
    358   1.4      cgd 	int signo;
    359   1.1      cgd {
    360   1.1      cgd 	if (lflag)
    361   1.1      cgd 		syslog(LOG_INFO, "exiting");
    362   1.1      cgd 	unlink(_PATH_SOCKETNAME);
    363   1.1      cgd 	exit(0);
    364   1.1      cgd }
    365   1.1      cgd 
    366   1.1      cgd /*
    367   1.1      cgd  * Stuff for handling job specifications
    368   1.1      cgd  */
    369   1.1      cgd char	*user[MAXUSERS];	/* users to process */
    370   1.1      cgd int	users;			/* # of users in user array */
    371   1.1      cgd int	requ[MAXREQUESTS];	/* job number of spool entries */
    372   1.1      cgd int	requests;		/* # of spool requests */
    373   1.1      cgd char	*person;		/* name of person doing lprm */
    374   1.1      cgd 
    375   1.4      cgd char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
    376   1.4      cgd char	cbuf[BUFSIZ];		/* command line buffer */
    377   1.1      cgd char	*cmdnames[] = {
    378   1.1      cgd 	"null",
    379   1.1      cgd 	"printjob",
    380   1.1      cgd 	"recvjob",
    381   1.1      cgd 	"displayq short",
    382   1.1      cgd 	"displayq long",
    383   1.1      cgd 	"rmjob"
    384   1.1      cgd };
    385   1.1      cgd 
    386   1.4      cgd static void
    387   1.1      cgd doit()
    388   1.1      cgd {
    389  1.13      mrg 	char *cp;
    390  1.13      mrg 	int n;
    391   1.1      cgd 
    392   1.1      cgd 	for (;;) {
    393   1.1      cgd 		cp = cbuf;
    394   1.1      cgd 		do {
    395   1.1      cgd 			if (cp >= &cbuf[sizeof(cbuf) - 1])
    396   1.1      cgd 				fatal("Command line too long");
    397   1.1      cgd 			if ((n = read(1, cp, 1)) != 1) {
    398   1.1      cgd 				if (n < 0)
    399   1.1      cgd 					fatal("Lost connection");
    400   1.1      cgd 				return;
    401   1.1      cgd 			}
    402   1.1      cgd 		} while (*cp++ != '\n');
    403   1.1      cgd 		*--cp = '\0';
    404   1.1      cgd 		cp = cbuf;
    405   1.1      cgd 		if (lflag) {
    406  1.18      mrg 			if (*cp >= '\1' && *cp <= '\5') {
    407   1.1      cgd 				syslog(LOG_INFO, "%s requests %s %s",
    408  1.10    mikel 					from, cmdnames[(int)*cp], cp+1);
    409  1.18      mrg 				setproctitle("serving %s: %s %s", from,
    410  1.18      mrg 				    cmdnames[(int)*cp], cp+1);
    411  1.18      mrg 			}
    412   1.1      cgd 			else
    413   1.1      cgd 				syslog(LOG_INFO, "bad request (%d) from %s",
    414   1.1      cgd 					*cp, from);
    415   1.1      cgd 		}
    416   1.1      cgd 		switch (*cp++) {
    417   1.1      cgd 		case '\1':	/* check the queue and print any jobs there */
    418   1.1      cgd 			printer = cp;
    419   1.1      cgd 			printjob();
    420   1.1      cgd 			break;
    421   1.1      cgd 		case '\2':	/* receive files to be queued */
    422   1.1      cgd 			if (!from_remote) {
    423   1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    424   1.1      cgd 				exit(1);
    425   1.1      cgd 			}
    426   1.1      cgd 			printer = cp;
    427   1.1      cgd 			recvjob();
    428   1.1      cgd 			break;
    429   1.1      cgd 		case '\3':	/* display the queue (short form) */
    430   1.1      cgd 		case '\4':	/* display the queue (long form) */
    431   1.1      cgd 			printer = cp;
    432   1.1      cgd 			while (*cp) {
    433   1.1      cgd 				if (*cp != ' ') {
    434   1.1      cgd 					cp++;
    435   1.1      cgd 					continue;
    436   1.1      cgd 				}
    437   1.1      cgd 				*cp++ = '\0';
    438   1.1      cgd 				while (isspace(*cp))
    439   1.1      cgd 					cp++;
    440   1.1      cgd 				if (*cp == '\0')
    441   1.1      cgd 					break;
    442   1.1      cgd 				if (isdigit(*cp)) {
    443   1.1      cgd 					if (requests >= MAXREQUESTS)
    444   1.1      cgd 						fatal("Too many requests");
    445   1.1      cgd 					requ[requests++] = atoi(cp);
    446   1.1      cgd 				} else {
    447   1.1      cgd 					if (users >= MAXUSERS)
    448   1.1      cgd 						fatal("Too many users");
    449   1.1      cgd 					user[users++] = cp;
    450   1.1      cgd 				}
    451   1.1      cgd 			}
    452   1.1      cgd 			displayq(cbuf[0] - '\3');
    453   1.1      cgd 			exit(0);
    454   1.1      cgd 		case '\5':	/* remove a job from the queue */
    455   1.1      cgd 			if (!from_remote) {
    456   1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    457   1.1      cgd 				exit(1);
    458   1.1      cgd 			}
    459   1.1      cgd 			printer = cp;
    460   1.1      cgd 			while (*cp && *cp != ' ')
    461   1.1      cgd 				cp++;
    462   1.1      cgd 			if (!*cp)
    463   1.1      cgd 				break;
    464   1.1      cgd 			*cp++ = '\0';
    465   1.1      cgd 			person = cp;
    466   1.1      cgd 			while (*cp) {
    467   1.1      cgd 				if (*cp != ' ') {
    468   1.1      cgd 					cp++;
    469   1.1      cgd 					continue;
    470   1.1      cgd 				}
    471   1.1      cgd 				*cp++ = '\0';
    472   1.1      cgd 				while (isspace(*cp))
    473   1.1      cgd 					cp++;
    474   1.1      cgd 				if (*cp == '\0')
    475   1.1      cgd 					break;
    476   1.1      cgd 				if (isdigit(*cp)) {
    477   1.1      cgd 					if (requests >= MAXREQUESTS)
    478   1.1      cgd 						fatal("Too many requests");
    479   1.1      cgd 					requ[requests++] = atoi(cp);
    480   1.1      cgd 				} else {
    481   1.1      cgd 					if (users >= MAXUSERS)
    482   1.1      cgd 						fatal("Too many users");
    483   1.1      cgd 					user[users++] = cp;
    484   1.1      cgd 				}
    485   1.1      cgd 			}
    486   1.1      cgd 			rmjob();
    487   1.1      cgd 			break;
    488   1.1      cgd 		}
    489   1.1      cgd 		fatal("Illegal service request");
    490   1.1      cgd 	}
    491   1.1      cgd }
    492   1.1      cgd 
    493   1.1      cgd /*
    494   1.1      cgd  * Make a pass through the printcap database and start printing any
    495   1.1      cgd  * files left from the last time the machine went down.
    496   1.1      cgd  */
    497   1.4      cgd static void
    498   1.1      cgd startup()
    499   1.1      cgd {
    500   1.4      cgd 	char *buf;
    501  1.13      mrg 	char *cp;
    502   1.1      cgd 
    503   1.1      cgd 	/*
    504   1.1      cgd 	 * Restart the daemons.
    505   1.1      cgd 	 */
    506   1.4      cgd 	while (cgetnext(&buf, printcapdb) > 0) {
    507  1.12      mrg 		if (ckqueue(buf) <= 0) {
    508  1.12      mrg 			free(buf);
    509  1.12      mrg 			continue;	/* no work to do for this printer */
    510  1.12      mrg 		}
    511   1.1      cgd 		for (cp = buf; *cp; cp++)
    512   1.1      cgd 			if (*cp == '|' || *cp == ':') {
    513   1.1      cgd 				*cp = '\0';
    514   1.1      cgd 				break;
    515   1.1      cgd 			}
    516  1.12      mrg 		if (lflag)
    517  1.12      mrg 			syslog(LOG_INFO, "work for %s", buf);
    518  1.18      mrg 		switch (fork()) {
    519  1.18      mrg 		case -1:
    520   1.1      cgd 			syslog(LOG_WARNING, "startup: cannot fork");
    521   1.4      cgd 			mcleanup(0);
    522  1.18      mrg 		case 0:
    523   1.4      cgd 			printer = buf;
    524  1.18      mrg 			setproctitle("working on printer %s", printer);
    525   1.4      cgd 			cgetclose();
    526   1.1      cgd 			printjob();
    527  1.12      mrg 			/* NOTREACHED */
    528  1.18      mrg 		default:
    529  1.18      mrg 			child_count++;
    530  1.18      mrg 			free(buf);
    531   1.1      cgd 		}
    532  1.12      mrg 	}
    533  1.12      mrg }
    534  1.12      mrg 
    535  1.12      mrg /*
    536  1.12      mrg  * Make sure there's some work to do before forking off a child
    537  1.12      mrg  */
    538  1.12      mrg static int
    539  1.12      mrg ckqueue(cap)
    540  1.12      mrg 	char *cap;
    541  1.12      mrg {
    542  1.13      mrg 	struct dirent *d;
    543  1.12      mrg 	DIR *dirp;
    544  1.12      mrg 	char *spooldir;
    545  1.12      mrg 
    546  1.12      mrg 	if (cgetstr(cap, "sd", &spooldir) == -1)
    547  1.12      mrg 		spooldir = _PATH_DEFSPOOL;
    548  1.12      mrg 	if ((dirp = opendir(spooldir)) == NULL)
    549  1.12      mrg 		return (-1);
    550  1.12      mrg 	while ((d = readdir(dirp)) != NULL) {
    551  1.12      mrg 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    552  1.12      mrg 			continue;	/* daemon control files only */
    553  1.12      mrg 		closedir(dirp);
    554  1.12      mrg 		return (1);		/* found something */
    555   1.1      cgd 	}
    556  1.12      mrg 	closedir(dirp);
    557  1.12      mrg 	return (0);
    558   1.1      cgd }
    559   1.1      cgd 
    560   1.1      cgd #define DUMMY ":nobody::"
    561   1.1      cgd 
    562   1.1      cgd /*
    563   1.1      cgd  * Check to see if the from host has access to the line printer.
    564   1.1      cgd  */
    565   1.4      cgd static void
    566   1.1      cgd chkhost(f)
    567   1.1      cgd 	struct sockaddr_in *f;
    568   1.1      cgd {
    569  1.13      mrg 	struct hostent *hp;
    570  1.13      mrg 	FILE *hostf;
    571  1.13      mrg 	int first = 1, good = 0;
    572   1.1      cgd 
    573   1.1      cgd 	f->sin_port = ntohs(f->sin_port);
    574   1.1      cgd 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
    575   1.1      cgd 		fatal("Malformed from address");
    576   1.4      cgd 
    577   1.4      cgd 	/* Need real hostname for temporary filenames */
    578   1.1      cgd 	hp = gethostbyaddr((char *)&f->sin_addr,
    579   1.1      cgd 	    sizeof(struct in_addr), f->sin_family);
    580   1.4      cgd 	if (hp == NULL)
    581   1.1      cgd 		fatal("Host name for your address (%s) unknown",
    582   1.1      cgd 			inet_ntoa(f->sin_addr));
    583   1.1      cgd 
    584   1.9      mrg 	(void)strncpy(fromb, hp->h_name, sizeof(fromb) - 1);
    585  1.19      mjl 	fromb[sizeof(fromb) - 1] = '\0';
    586   1.1      cgd 	from = fromb;
    587   1.1      cgd 
    588  1.13      mrg 	/* Check for spoof, ala rlogind */
    589  1.13      mrg 	hp = gethostbyname(fromb);
    590  1.13      mrg 	if (!hp)
    591  1.13      mrg 		fatal("hostname for your address (%s) unknown",
    592  1.13      mrg 		    inet_ntoa(f->sin_addr));
    593  1.13      mrg 	for (; good == 0 && hp->h_addr_list[0] != NULL; hp->h_addr_list++) {
    594  1.15    lukem 		if (!memcmp(hp->h_addr_list[0], (caddr_t)&f->sin_addr,
    595  1.13      mrg 		    sizeof(f->sin_addr)))
    596  1.13      mrg 			good = 1;
    597  1.13      mrg 	}
    598  1.13      mrg 	if (good == 0)
    599  1.13      mrg 		fatal("address for your hostname (%s) not matched",
    600  1.13      mrg 		    inet_ntoa(f->sin_addr));
    601  1.18      mrg 	setproctitle("serving %s", from);
    602   1.1      cgd 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
    603   1.1      cgd again:
    604   1.1      cgd 	if (hostf) {
    605   1.4      cgd 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
    606   1.4      cgd 		    DUMMY, DUMMY) == 0) {
    607   1.9      mrg 			(void)fclose(hostf);
    608   1.1      cgd 			return;
    609   1.1      cgd 		}
    610   1.9      mrg 		(void)fclose(hostf);
    611   1.1      cgd 	}
    612   1.1      cgd 	if (first == 1) {
    613   1.1      cgd 		first = 0;
    614   1.1      cgd 		hostf = fopen(_PATH_HOSTSLPD, "r");
    615   1.1      cgd 		goto again;
    616   1.1      cgd 	}
    617   1.1      cgd 	fatal("Your host does not have line printer access");
    618   1.4      cgd 	/*NOTREACHED*/
    619  1.13      mrg }
    620  1.13      mrg 
    621  1.13      mrg static void
    622  1.13      mrg usage()
    623  1.13      mrg {
    624  1.13      mrg 	extern char *__progname;	/* XXX */
    625  1.13      mrg 
    626  1.13      mrg 	fprintf(stderr, "usage: %s [-d] [-l]\n", __progname);
    627  1.13      mrg 	exit(1);
    628   1.1      cgd }
    629