Home | History | Annotate | Line # | Download | only in lpd
lpd.c revision 1.20
      1  1.20   itojun /*	$NetBSD: lpd.c,v 1.20 2000/01/27 05:39:50 itojun 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.20   itojun __RCSID("$NetBSD: lpd.c,v 1.20 2000/01/27 05:39:50 itojun 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.20   itojun static void       chkhost __P((struct sockaddr *));
    120  1.12      mrg static int	  ckqueue __P((char *));
    121  1.13      mrg static void	  usage __P((void));
    122  1.20   itojun static int	  *socksetup __P((int, int));
    123   1.1      cgd 
    124   1.5  hpeyerl uid_t	uid, euid;
    125  1.18      mrg int child_count;
    126   1.5  hpeyerl 
    127   1.4      cgd int
    128   1.1      cgd main(argc, argv)
    129   1.1      cgd 	int argc;
    130   1.1      cgd 	char **argv;
    131   1.1      cgd {
    132  1.20   itojun 	int f, funix, *finet, options, fromlen;
    133   1.4      cgd 	fd_set defreadfds;
    134   1.4      cgd 	struct sockaddr_un un, fromunix;
    135  1.20   itojun 	struct sockaddr_storage frominet;
    136  1.13      mrg 	int omask, lfd, errs, i;
    137  1.18      mrg 	int child_max = 32;	/* more then enough to hose the system */
    138   1.1      cgd 
    139   1.5  hpeyerl 	euid = geteuid();	/* these shouldn't be different */
    140   1.5  hpeyerl 	uid = getuid();
    141   1.4      cgd 	options = 0;
    142   1.1      cgd 	gethostname(host, sizeof(host));
    143  1.16      mrg 	host[sizeof(host) - 1] = '\0';
    144   1.1      cgd 	name = argv[0];
    145   1.1      cgd 
    146  1.13      mrg 	errs = 0;
    147  1.18      mrg 	while ((i = getopt(argc, argv, "dln:srw:")) != -1)
    148  1.13      mrg 		switch (i) {
    149  1.13      mrg 		case 'd':
    150  1.13      mrg 			options |= SO_DEBUG;
    151  1.13      mrg 			break;
    152  1.13      mrg 		case 'l':
    153  1.13      mrg 			lflag++;
    154  1.14      mrg 			break;
    155  1.18      mrg 		case 'n':
    156  1.18      mrg 			child_max = atoi(optarg);
    157  1.18      mrg 			if (child_max < 0 || child_max > 1024)
    158  1.18      mrg 				errx(1, "invalid number of children: %s",
    159  1.18      mrg 				    optarg);
    160  1.18      mrg 			break;
    161  1.18      mrg 		case 'r':
    162  1.18      mrg 			rflag++;
    163  1.18      mrg 			break;
    164  1.14      mrg 		case 's':
    165  1.14      mrg 			sflag++;
    166  1.13      mrg 			break;
    167  1.18      mrg 		case 'w':
    168  1.18      mrg 			wait_time = atoi(optarg);
    169  1.18      mrg 			if (wait_time < 0)
    170  1.18      mrg 				errx(1, "wait time must be postive: %s",
    171  1.18      mrg 				    optarg);
    172  1.18      mrg 			if (wait_time < 30)
    173  1.18      mrg 			    warnx("warning: wait time less than 30 seconds");
    174  1.18      mrg 			break;
    175  1.13      mrg 		default:
    176  1.13      mrg 			errs++;
    177  1.13      mrg 		}
    178  1.13      mrg 	argc -= optind;
    179  1.13      mrg 	argv += optind;
    180  1.13      mrg 	if (errs || argc != 0)
    181  1.13      mrg 		usage();
    182   1.1      cgd 
    183   1.1      cgd #ifndef DEBUG
    184   1.1      cgd 	/*
    185   1.1      cgd 	 * Set up standard environment by detaching from the parent.
    186   1.1      cgd 	 */
    187   1.1      cgd 	daemon(0, 0);
    188   1.1      cgd #endif
    189   1.1      cgd 
    190   1.1      cgd 	openlog("lpd", LOG_PID, LOG_LPR);
    191   1.4      cgd 	syslog(LOG_INFO, "restarted");
    192   1.9      mrg 	(void)umask(0);
    193   1.1      cgd 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
    194   1.1      cgd 	if (lfd < 0) {
    195   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    196   1.1      cgd 		exit(1);
    197   1.1      cgd 	}
    198   1.1      cgd 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
    199   1.1      cgd 		if (errno == EWOULDBLOCK)	/* active deamon present */
    200   1.1      cgd 			exit(0);
    201   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    202   1.1      cgd 		exit(1);
    203   1.1      cgd 	}
    204   1.1      cgd 	ftruncate(lfd, 0);
    205   1.1      cgd 	/*
    206   1.1      cgd 	 * write process id for others to know
    207   1.1      cgd 	 */
    208   1.9      mrg 	(void)snprintf(line, sizeof(line), "%u\n", getpid());
    209   1.1      cgd 	f = strlen(line);
    210   1.1      cgd 	if (write(lfd, line, f) != f) {
    211   1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    212   1.1      cgd 		exit(1);
    213   1.1      cgd 	}
    214   1.1      cgd 	signal(SIGCHLD, reapchild);
    215   1.1      cgd 	/*
    216   1.1      cgd 	 * Restart all the printers.
    217   1.1      cgd 	 */
    218   1.1      cgd 	startup();
    219   1.9      mrg 	(void)unlink(_PATH_SOCKETNAME);
    220  1.17    lukem 	funix = socket(AF_LOCAL, SOCK_STREAM, 0);
    221   1.1      cgd 	if (funix < 0) {
    222   1.1      cgd 		syslog(LOG_ERR, "socket: %m");
    223   1.1      cgd 		exit(1);
    224   1.1      cgd 	}
    225   1.1      cgd #define	mask(s)	(1 << ((s) - 1))
    226   1.1      cgd 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
    227   1.1      cgd 	signal(SIGHUP, mcleanup);
    228   1.1      cgd 	signal(SIGINT, mcleanup);
    229   1.1      cgd 	signal(SIGQUIT, mcleanup);
    230   1.1      cgd 	signal(SIGTERM, mcleanup);
    231   1.4      cgd 	memset(&un, 0, sizeof(un));
    232  1.17    lukem 	un.sun_family = AF_LOCAL;
    233   1.9      mrg 	strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
    234   1.4      cgd #ifndef SUN_LEN
    235   1.4      cgd #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    236   1.4      cgd #endif
    237   1.4      cgd 	if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
    238   1.1      cgd 		syslog(LOG_ERR, "ubind: %m");
    239   1.1      cgd 		exit(1);
    240   1.1      cgd 	}
    241   1.1      cgd 	sigsetmask(omask);
    242   1.4      cgd 	FD_ZERO(&defreadfds);
    243   1.4      cgd 	FD_SET(funix, &defreadfds);
    244   1.1      cgd 	listen(funix, 5);
    245   1.8    perry 	if (!sflag)
    246  1.20   itojun 		finet = socksetup(PF_UNSPEC, options);
    247   1.8    perry 	else
    248  1.20   itojun 		finet = NULL;	/* pretend we couldn't open TCP socket. */
    249   1.1      cgd 
    250  1.20   itojun 	if (finet) {
    251  1.20   itojun 		for (i = 1; i <= *finet; i++) {
    252  1.20   itojun 			FD_SET(finet[i], &defreadfds);
    253  1.20   itojun 			listen(finet[i], 5);
    254   1.1      cgd 		}
    255   1.1      cgd 	}
    256   1.1      cgd 	/*
    257   1.1      cgd 	 * Main loop: accept, do a request, continue.
    258   1.1      cgd 	 */
    259   1.4      cgd 	memset(&frominet, 0, sizeof(frominet));
    260   1.4      cgd 	memset(&fromunix, 0, sizeof(fromunix));
    261   1.1      cgd 	for (;;) {
    262   1.4      cgd 		int domain, nfds, s;
    263   1.4      cgd 		fd_set readfds;
    264  1.18      mrg 		/* "short" so it overflows in about 2 hours */
    265  1.18      mrg 		short sleeptime = 10;
    266  1.18      mrg 
    267  1.18      mrg 		while (child_max < child_count) {
    268  1.18      mrg 			syslog(LOG_WARNING,
    269  1.18      mrg 			    "too many children, sleeping for %d seconds",
    270  1.18      mrg 				sleeptime);
    271  1.18      mrg 			sleep(sleeptime);
    272  1.18      mrg 			sleeptime <<= 1;
    273  1.18      mrg 			if (sleeptime < 0) {
    274  1.18      mrg 				syslog(LOG_CRIT, "sleeptime overflowed! help!");
    275  1.18      mrg 				sleeptime = 10;
    276  1.18      mrg 			}
    277  1.18      mrg 		}
    278   1.1      cgd 
    279   1.4      cgd 		FD_COPY(&defreadfds, &readfds);
    280   1.1      cgd 		nfds = select(20, &readfds, 0, 0, 0);
    281   1.1      cgd 		if (nfds <= 0) {
    282   1.1      cgd 			if (nfds < 0 && errno != EINTR)
    283   1.1      cgd 				syslog(LOG_WARNING, "select: %m");
    284   1.1      cgd 			continue;
    285   1.1      cgd 		}
    286   1.4      cgd 		if (FD_ISSET(funix, &readfds)) {
    287  1.17    lukem 			domain = AF_LOCAL, fromlen = sizeof(fromunix);
    288   1.1      cgd 			s = accept(funix,
    289   1.1      cgd 			    (struct sockaddr *)&fromunix, &fromlen);
    290  1.20   itojun 		} else {
    291  1.20   itojun                         for (i = 1; i <= *finet; i++)
    292  1.20   itojun 				if (FD_ISSET(finet[i], &readfds)) {
    293  1.20   itojun 					domain = AF_INET, fromlen = sizeof(frominet);
    294  1.20   itojun 					s = accept(finet[i], (struct sockaddr *)&frominet, &fromlen);
    295  1.20   itojun 				}
    296   1.1      cgd 		}
    297   1.1      cgd 		if (s < 0) {
    298   1.1      cgd 			if (errno != EINTR)
    299   1.1      cgd 				syslog(LOG_WARNING, "accept: %m");
    300   1.1      cgd 			continue;
    301   1.1      cgd 		}
    302  1.18      mrg 
    303  1.18      mrg 		switch (fork()) {
    304  1.18      mrg 		case 0:
    305   1.1      cgd 			signal(SIGCHLD, SIG_IGN);
    306   1.1      cgd 			signal(SIGHUP, SIG_IGN);
    307   1.1      cgd 			signal(SIGINT, SIG_IGN);
    308   1.1      cgd 			signal(SIGQUIT, SIG_IGN);
    309   1.1      cgd 			signal(SIGTERM, SIG_IGN);
    310   1.9      mrg 			(void)close(funix);
    311  1.20   itojun 			if (!sflag && finet)
    312  1.20   itojun                         	for (i = 1; i <= *finet; i++)
    313  1.20   itojun 					(void)close(finet[i]);
    314   1.1      cgd 			dup2(s, 1);
    315   1.9      mrg 			(void)close(s);
    316   1.1      cgd 			if (domain == AF_INET) {
    317  1.20   itojun 				/* for both AF_INET and AF_INET6 */
    318   1.1      cgd 				from_remote = 1;
    319  1.20   itojun 				chkhost((struct sockaddr *)&frominet);
    320   1.1      cgd 			} else
    321   1.1      cgd 				from_remote = 0;
    322   1.1      cgd 			doit();
    323   1.1      cgd 			exit(0);
    324  1.18      mrg 		case -1:
    325  1.18      mrg 			syslog(LOG_WARNING, "fork: %m, sleeping for 10 seconds...");
    326  1.18      mrg 			sleep(10);
    327  1.18      mrg 			continue;
    328  1.18      mrg 		default:
    329  1.18      mrg 			child_count++;
    330   1.1      cgd 		}
    331   1.9      mrg 		(void)close(s);
    332   1.1      cgd 	}
    333   1.1      cgd }
    334   1.1      cgd 
    335   1.4      cgd static void
    336   1.4      cgd reapchild(signo)
    337   1.4      cgd 	int signo;
    338   1.1      cgd {
    339   1.1      cgd 	union wait status;
    340   1.1      cgd 
    341   1.1      cgd 	while (wait3((int *)&status, WNOHANG, 0) > 0)
    342  1.18      mrg 		child_count--;
    343   1.1      cgd }
    344   1.1      cgd 
    345   1.4      cgd static void
    346   1.4      cgd mcleanup(signo)
    347   1.4      cgd 	int signo;
    348   1.1      cgd {
    349   1.1      cgd 	if (lflag)
    350   1.1      cgd 		syslog(LOG_INFO, "exiting");
    351   1.1      cgd 	unlink(_PATH_SOCKETNAME);
    352   1.1      cgd 	exit(0);
    353   1.1      cgd }
    354   1.1      cgd 
    355   1.1      cgd /*
    356   1.1      cgd  * Stuff for handling job specifications
    357   1.1      cgd  */
    358   1.1      cgd char	*user[MAXUSERS];	/* users to process */
    359   1.1      cgd int	users;			/* # of users in user array */
    360   1.1      cgd int	requ[MAXREQUESTS];	/* job number of spool entries */
    361   1.1      cgd int	requests;		/* # of spool requests */
    362   1.1      cgd char	*person;		/* name of person doing lprm */
    363   1.1      cgd 
    364  1.20   itojun char	fromb[NI_MAXHOST];	/* buffer for client's machine name */
    365   1.4      cgd char	cbuf[BUFSIZ];		/* command line buffer */
    366   1.1      cgd char	*cmdnames[] = {
    367   1.1      cgd 	"null",
    368   1.1      cgd 	"printjob",
    369   1.1      cgd 	"recvjob",
    370   1.1      cgd 	"displayq short",
    371   1.1      cgd 	"displayq long",
    372   1.1      cgd 	"rmjob"
    373   1.1      cgd };
    374   1.1      cgd 
    375   1.4      cgd static void
    376   1.1      cgd doit()
    377   1.1      cgd {
    378  1.13      mrg 	char *cp;
    379  1.13      mrg 	int n;
    380   1.1      cgd 
    381   1.1      cgd 	for (;;) {
    382   1.1      cgd 		cp = cbuf;
    383   1.1      cgd 		do {
    384   1.1      cgd 			if (cp >= &cbuf[sizeof(cbuf) - 1])
    385   1.1      cgd 				fatal("Command line too long");
    386   1.1      cgd 			if ((n = read(1, cp, 1)) != 1) {
    387   1.1      cgd 				if (n < 0)
    388   1.1      cgd 					fatal("Lost connection");
    389   1.1      cgd 				return;
    390   1.1      cgd 			}
    391   1.1      cgd 		} while (*cp++ != '\n');
    392   1.1      cgd 		*--cp = '\0';
    393   1.1      cgd 		cp = cbuf;
    394   1.1      cgd 		if (lflag) {
    395  1.18      mrg 			if (*cp >= '\1' && *cp <= '\5') {
    396   1.1      cgd 				syslog(LOG_INFO, "%s requests %s %s",
    397  1.10    mikel 					from, cmdnames[(int)*cp], cp+1);
    398  1.18      mrg 				setproctitle("serving %s: %s %s", from,
    399  1.18      mrg 				    cmdnames[(int)*cp], cp+1);
    400  1.18      mrg 			}
    401   1.1      cgd 			else
    402   1.1      cgd 				syslog(LOG_INFO, "bad request (%d) from %s",
    403   1.1      cgd 					*cp, from);
    404   1.1      cgd 		}
    405   1.1      cgd 		switch (*cp++) {
    406   1.1      cgd 		case '\1':	/* check the queue and print any jobs there */
    407   1.1      cgd 			printer = cp;
    408   1.1      cgd 			printjob();
    409   1.1      cgd 			break;
    410   1.1      cgd 		case '\2':	/* receive files to be queued */
    411   1.1      cgd 			if (!from_remote) {
    412   1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    413   1.1      cgd 				exit(1);
    414   1.1      cgd 			}
    415   1.1      cgd 			printer = cp;
    416   1.1      cgd 			recvjob();
    417   1.1      cgd 			break;
    418   1.1      cgd 		case '\3':	/* display the queue (short form) */
    419   1.1      cgd 		case '\4':	/* display the queue (long form) */
    420   1.1      cgd 			printer = cp;
    421   1.1      cgd 			while (*cp) {
    422   1.1      cgd 				if (*cp != ' ') {
    423   1.1      cgd 					cp++;
    424   1.1      cgd 					continue;
    425   1.1      cgd 				}
    426   1.1      cgd 				*cp++ = '\0';
    427   1.1      cgd 				while (isspace(*cp))
    428   1.1      cgd 					cp++;
    429   1.1      cgd 				if (*cp == '\0')
    430   1.1      cgd 					break;
    431   1.1      cgd 				if (isdigit(*cp)) {
    432   1.1      cgd 					if (requests >= MAXREQUESTS)
    433   1.1      cgd 						fatal("Too many requests");
    434   1.1      cgd 					requ[requests++] = atoi(cp);
    435   1.1      cgd 				} else {
    436   1.1      cgd 					if (users >= MAXUSERS)
    437   1.1      cgd 						fatal("Too many users");
    438   1.1      cgd 					user[users++] = cp;
    439   1.1      cgd 				}
    440   1.1      cgd 			}
    441   1.1      cgd 			displayq(cbuf[0] - '\3');
    442   1.1      cgd 			exit(0);
    443   1.1      cgd 		case '\5':	/* remove a job from the queue */
    444   1.1      cgd 			if (!from_remote) {
    445   1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    446   1.1      cgd 				exit(1);
    447   1.1      cgd 			}
    448   1.1      cgd 			printer = cp;
    449   1.1      cgd 			while (*cp && *cp != ' ')
    450   1.1      cgd 				cp++;
    451   1.1      cgd 			if (!*cp)
    452   1.1      cgd 				break;
    453   1.1      cgd 			*cp++ = '\0';
    454   1.1      cgd 			person = cp;
    455   1.1      cgd 			while (*cp) {
    456   1.1      cgd 				if (*cp != ' ') {
    457   1.1      cgd 					cp++;
    458   1.1      cgd 					continue;
    459   1.1      cgd 				}
    460   1.1      cgd 				*cp++ = '\0';
    461   1.1      cgd 				while (isspace(*cp))
    462   1.1      cgd 					cp++;
    463   1.1      cgd 				if (*cp == '\0')
    464   1.1      cgd 					break;
    465   1.1      cgd 				if (isdigit(*cp)) {
    466   1.1      cgd 					if (requests >= MAXREQUESTS)
    467   1.1      cgd 						fatal("Too many requests");
    468   1.1      cgd 					requ[requests++] = atoi(cp);
    469   1.1      cgd 				} else {
    470   1.1      cgd 					if (users >= MAXUSERS)
    471   1.1      cgd 						fatal("Too many users");
    472   1.1      cgd 					user[users++] = cp;
    473   1.1      cgd 				}
    474   1.1      cgd 			}
    475   1.1      cgd 			rmjob();
    476   1.1      cgd 			break;
    477   1.1      cgd 		}
    478   1.1      cgd 		fatal("Illegal service request");
    479   1.1      cgd 	}
    480   1.1      cgd }
    481   1.1      cgd 
    482   1.1      cgd /*
    483   1.1      cgd  * Make a pass through the printcap database and start printing any
    484   1.1      cgd  * files left from the last time the machine went down.
    485   1.1      cgd  */
    486   1.4      cgd static void
    487   1.1      cgd startup()
    488   1.1      cgd {
    489   1.4      cgd 	char *buf;
    490  1.13      mrg 	char *cp;
    491   1.1      cgd 
    492   1.1      cgd 	/*
    493   1.1      cgd 	 * Restart the daemons.
    494   1.1      cgd 	 */
    495   1.4      cgd 	while (cgetnext(&buf, printcapdb) > 0) {
    496  1.12      mrg 		if (ckqueue(buf) <= 0) {
    497  1.12      mrg 			free(buf);
    498  1.12      mrg 			continue;	/* no work to do for this printer */
    499  1.12      mrg 		}
    500   1.1      cgd 		for (cp = buf; *cp; cp++)
    501   1.1      cgd 			if (*cp == '|' || *cp == ':') {
    502   1.1      cgd 				*cp = '\0';
    503   1.1      cgd 				break;
    504   1.1      cgd 			}
    505  1.12      mrg 		if (lflag)
    506  1.12      mrg 			syslog(LOG_INFO, "work for %s", buf);
    507  1.18      mrg 		switch (fork()) {
    508  1.18      mrg 		case -1:
    509   1.1      cgd 			syslog(LOG_WARNING, "startup: cannot fork");
    510   1.4      cgd 			mcleanup(0);
    511  1.18      mrg 		case 0:
    512   1.4      cgd 			printer = buf;
    513  1.18      mrg 			setproctitle("working on printer %s", printer);
    514   1.4      cgd 			cgetclose();
    515   1.1      cgd 			printjob();
    516  1.12      mrg 			/* NOTREACHED */
    517  1.18      mrg 		default:
    518  1.18      mrg 			child_count++;
    519  1.18      mrg 			free(buf);
    520   1.1      cgd 		}
    521  1.12      mrg 	}
    522  1.12      mrg }
    523  1.12      mrg 
    524  1.12      mrg /*
    525  1.12      mrg  * Make sure there's some work to do before forking off a child
    526  1.12      mrg  */
    527  1.12      mrg static int
    528  1.12      mrg ckqueue(cap)
    529  1.12      mrg 	char *cap;
    530  1.12      mrg {
    531  1.13      mrg 	struct dirent *d;
    532  1.12      mrg 	DIR *dirp;
    533  1.12      mrg 	char *spooldir;
    534  1.12      mrg 
    535  1.12      mrg 	if (cgetstr(cap, "sd", &spooldir) == -1)
    536  1.12      mrg 		spooldir = _PATH_DEFSPOOL;
    537  1.12      mrg 	if ((dirp = opendir(spooldir)) == NULL)
    538  1.12      mrg 		return (-1);
    539  1.12      mrg 	while ((d = readdir(dirp)) != NULL) {
    540  1.12      mrg 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    541  1.12      mrg 			continue;	/* daemon control files only */
    542  1.12      mrg 		closedir(dirp);
    543  1.12      mrg 		return (1);		/* found something */
    544   1.1      cgd 	}
    545  1.12      mrg 	closedir(dirp);
    546  1.12      mrg 	return (0);
    547   1.1      cgd }
    548   1.1      cgd 
    549   1.1      cgd #define DUMMY ":nobody::"
    550   1.1      cgd 
    551   1.1      cgd /*
    552   1.1      cgd  * Check to see if the from host has access to the line printer.
    553   1.1      cgd  */
    554   1.4      cgd static void
    555   1.1      cgd chkhost(f)
    556  1.20   itojun 	struct sockaddr *f;
    557   1.1      cgd {
    558  1.20   itojun 	struct addrinfo hints, *res, *r;
    559  1.13      mrg 	FILE *hostf;
    560  1.13      mrg 	int first = 1, good = 0;
    561  1.20   itojun 	char host[NI_MAXHOST], ip[NI_MAXHOST];
    562  1.20   itojun 	char serv[NI_MAXSERV];
    563  1.20   itojun 	int error;
    564  1.20   itojun 
    565  1.20   itojun 	error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
    566  1.20   itojun 			    NI_NUMERICSERV);
    567  1.20   itojun 	if (error || atoi(serv) >= IPPORT_RESERVED)
    568   1.1      cgd 		fatal("Malformed from address");
    569   1.4      cgd 
    570   1.4      cgd 	/* Need real hostname for temporary filenames */
    571  1.20   itojun 	error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    572  1.20   itojun 			    NI_NAMEREQD);
    573  1.20   itojun 	if (error) {
    574  1.20   itojun 		error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    575  1.20   itojun 				    NI_NUMERICHOST);
    576  1.20   itojun 		if (error)
    577  1.20   itojun 			fatal("Host name for your address unknown");
    578  1.20   itojun 		else
    579  1.20   itojun 			fatal("Host name for your address (%s) unknown", host);
    580  1.20   itojun 	}
    581   1.1      cgd 
    582  1.20   itojun 	(void)strncpy(fromb, host, sizeof(fromb) - 1);
    583  1.19      mjl 	fromb[sizeof(fromb) - 1] = '\0';
    584   1.1      cgd 	from = fromb;
    585   1.1      cgd 
    586  1.20   itojun 	/* need address in stringform for comparison (no DNS lookup here) */
    587  1.20   itojun 	error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    588  1.20   itojun 			    NI_NUMERICHOST);
    589  1.20   itojun 	if (error)
    590  1.20   itojun 		fatal("Cannot print address");
    591  1.20   itojun 
    592  1.13      mrg 	/* Check for spoof, ala rlogind */
    593  1.20   itojun 	memset(&hints, 0, sizeof(hints));
    594  1.20   itojun 	hints.ai_family = PF_UNSPEC;
    595  1.20   itojun 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    596  1.20   itojun 	error = getaddrinfo(fromb, NULL, &hints, &res);
    597  1.20   itojun 	if (error) {
    598  1.20   itojun 		fatal("hostname for your address (%s) unknown: %s", host,
    599  1.20   itojun 		    gai_strerror(error));
    600  1.20   itojun 	}
    601  1.20   itojun 	good = 0;
    602  1.20   itojun 	for (r = res; good == 0 && r; r = r->ai_next) {
    603  1.20   itojun 		error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
    604  1.20   itojun 				    NULL, 0, NI_NUMERICHOST);
    605  1.20   itojun 		if (!error && !strcmp(host, ip))
    606  1.13      mrg 			good = 1;
    607  1.13      mrg 	}
    608  1.20   itojun 	if (res)
    609  1.20   itojun 		freeaddrinfo(res);
    610  1.13      mrg 	if (good == 0)
    611  1.20   itojun 		fatal("address for your hostname (%s) not matched", host);
    612  1.18      mrg 	setproctitle("serving %s", from);
    613   1.1      cgd 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
    614   1.1      cgd again:
    615   1.1      cgd 	if (hostf) {
    616  1.20   itojun 		if (__ivaliduser_sa(hostf, f, DUMMY, DUMMY) == 0) {
    617   1.9      mrg 			(void)fclose(hostf);
    618   1.1      cgd 			return;
    619   1.1      cgd 		}
    620   1.9      mrg 		(void)fclose(hostf);
    621   1.1      cgd 	}
    622   1.1      cgd 	if (first == 1) {
    623   1.1      cgd 		first = 0;
    624   1.1      cgd 		hostf = fopen(_PATH_HOSTSLPD, "r");
    625   1.1      cgd 		goto again;
    626   1.1      cgd 	}
    627   1.1      cgd 	fatal("Your host does not have line printer access");
    628   1.4      cgd 	/*NOTREACHED*/
    629  1.13      mrg }
    630  1.13      mrg 
    631  1.13      mrg static void
    632  1.13      mrg usage()
    633  1.13      mrg {
    634  1.13      mrg 	extern char *__progname;	/* XXX */
    635  1.13      mrg 
    636  1.13      mrg 	fprintf(stderr, "usage: %s [-d] [-l]\n", __progname);
    637  1.13      mrg 	exit(1);
    638  1.20   itojun }
    639  1.20   itojun 
    640  1.20   itojun /* setup server socket for specified address family */
    641  1.20   itojun /* if af is PF_UNSPEC more than one socket may be returned */
    642  1.20   itojun /* the returned list is dynamically allocated, so caller needs to free it */
    643  1.20   itojun int *
    644  1.20   itojun socksetup(af, options)
    645  1.20   itojun         int af, options;
    646  1.20   itojun {
    647  1.20   itojun 	struct addrinfo hints, *res, *r;
    648  1.20   itojun 	int error, maxs, *s, *socks;
    649  1.20   itojun 	const int on = 1;
    650  1.20   itojun 
    651  1.20   itojun 	memset(&hints, 0, sizeof(hints));
    652  1.20   itojun 	hints.ai_flags = AI_PASSIVE;
    653  1.20   itojun 	hints.ai_family = af;
    654  1.20   itojun 	hints.ai_socktype = SOCK_STREAM;
    655  1.20   itojun 	error = getaddrinfo(NULL, "printer", &hints, &res);
    656  1.20   itojun 	if (error) {
    657  1.20   itojun 		syslog(LOG_ERR, (gai_strerror(error)));
    658  1.20   itojun 		mcleanup(0);
    659  1.20   itojun 	}
    660  1.20   itojun 
    661  1.20   itojun 	/* Count max number of sockets we may open */
    662  1.20   itojun 	for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
    663  1.20   itojun 		;
    664  1.20   itojun 	socks = malloc((maxs + 1) * sizeof(int));
    665  1.20   itojun 	if (!socks) {
    666  1.20   itojun 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
    667  1.20   itojun 		mcleanup(0);
    668  1.20   itojun 	}
    669  1.20   itojun 
    670  1.20   itojun 	*socks = 0;   /* num of sockets counter at start of array */
    671  1.20   itojun 	s = socks + 1;
    672  1.20   itojun 	for (r = res; r; r = r->ai_next) {
    673  1.20   itojun 		*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
    674  1.20   itojun 		if (*s < 0) {
    675  1.20   itojun 			syslog(LOG_DEBUG, "socket(): %m");
    676  1.20   itojun 			continue;
    677  1.20   itojun 		}
    678  1.20   itojun 		if (options & SO_DEBUG)
    679  1.20   itojun 			if (setsockopt(*s, SOL_SOCKET, SO_DEBUG,
    680  1.20   itojun 				       &on, sizeof(on)) < 0) {
    681  1.20   itojun 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    682  1.20   itojun 				close (*s);
    683  1.20   itojun 				continue;
    684  1.20   itojun 			}
    685  1.20   itojun 		if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
    686  1.20   itojun 			syslog(LOG_DEBUG, "bind(): %m");
    687  1.20   itojun 			close (*s);
    688  1.20   itojun 			continue;
    689  1.20   itojun 		}
    690  1.20   itojun 		*socks = *socks + 1;
    691  1.20   itojun 		s++;
    692  1.20   itojun 	}
    693  1.20   itojun 
    694  1.20   itojun 	if (res)
    695  1.20   itojun 		freeaddrinfo(res);
    696  1.20   itojun 
    697  1.20   itojun 	if (*socks == 0) {
    698  1.20   itojun 		syslog(LOG_ERR, "Couldn't bind to any socket");
    699  1.20   itojun 		free(socks);
    700  1.20   itojun 		mcleanup(0);
    701  1.20   itojun 	}
    702  1.20   itojun 	return(socks);
    703   1.1      cgd }
    704