Home | History | Annotate | Line # | Download | only in lpd
lpd.c revision 1.8
      1  1.8    perry /*	$NetBSD: lpd.c,v 1.8 1996/09/21 15:57:22 perry 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.1      cgd #ifndef lint
     38  1.4      cgd static char copyright[] =
     39  1.4      cgd "@(#) Copyright (c) 1983, 1993, 1994\n\
     40  1.4      cgd 	The Regents of the University of California.  All rights reserved.\n";
     41  1.1      cgd #endif /* not lint */
     42  1.1      cgd 
     43  1.1      cgd #ifndef lint
     44  1.4      cgd static char sccsid[] = "@(#)lpd.c	8.4 (Berkeley) 4/17/94";
     45  1.1      cgd #endif /* not lint */
     46  1.1      cgd 
     47  1.1      cgd /*
     48  1.1      cgd  * lpd -- line printer daemon.
     49  1.1      cgd  *
     50  1.1      cgd  * Listen for a connection and perform the requested operation.
     51  1.1      cgd  * Operations are:
     52  1.1      cgd  *	\1printer\n
     53  1.1      cgd  *		check the queue for jobs and print any found.
     54  1.1      cgd  *	\2printer\n
     55  1.1      cgd  *		receive a job from another machine and queue it.
     56  1.1      cgd  *	\3printer [users ...] [jobs ...]\n
     57  1.1      cgd  *		return the current state of the queue (short form).
     58  1.1      cgd  *	\4printer [users ...] [jobs ...]\n
     59  1.1      cgd  *		return the current state of the queue (long form).
     60  1.1      cgd  *	\5printer person [users ...] [jobs ...]\n
     61  1.1      cgd  *		remove jobs from the queue.
     62  1.1      cgd  *
     63  1.1      cgd  * Strategy to maintain protected spooling area:
     64  1.1      cgd  *	1. Spooling area is writable only by daemon and spooling group
     65  1.1      cgd  *	2. lpr runs setuid root and setgrp spooling group; it uses
     66  1.1      cgd  *	   root to access any file it wants (verifying things before
     67  1.1      cgd  *	   with an access call) and group id to know how it should
     68  1.1      cgd  *	   set up ownership of files in the spooling area.
     69  1.1      cgd  *	3. Files in spooling area are owned by root, group spooling
     70  1.1      cgd  *	   group, with mode 660.
     71  1.1      cgd  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
     72  1.1      cgd  *	   access files and printer.  Users can't get to anything
     73  1.1      cgd  *	   w/o help of lpq and lprm programs.
     74  1.1      cgd  */
     75  1.1      cgd 
     76  1.4      cgd #include <sys/param.h>
     77  1.4      cgd #include <sys/wait.h>
     78  1.4      cgd #include <sys/types.h>
     79  1.4      cgd #include <sys/socket.h>
     80  1.4      cgd #include <sys/un.h>
     81  1.4      cgd #include <sys/stat.h>
     82  1.4      cgd #include <netinet/in.h>
     83  1.4      cgd 
     84  1.4      cgd #include <netdb.h>
     85  1.4      cgd #include <unistd.h>
     86  1.4      cgd #include <syslog.h>
     87  1.4      cgd #include <signal.h>
     88  1.4      cgd #include <errno.h>
     89  1.4      cgd #include <fcntl.h>
     90  1.4      cgd #include <dirent.h>
     91  1.4      cgd #include <stdio.h>
     92  1.4      cgd #include <stdlib.h>
     93  1.4      cgd #include <string.h>
     94  1.4      cgd #include <ctype.h>
     95  1.1      cgd #include "lp.h"
     96  1.4      cgd #include "lp.local.h"
     97  1.1      cgd #include "pathnames.h"
     98  1.4      cgd #include "extern.h"
     99  1.1      cgd 
    100  1.1      cgd int	lflag;				/* log requests flag */
    101  1.8    perry int	sflag;				/* secure (no inet) flag */
    102  1.1      cgd int	from_remote;			/* from remote socket */
    103  1.1      cgd 
    104  1.4      cgd static void       reapchild __P((int));
    105  1.4      cgd static void       mcleanup __P((int));
    106  1.4      cgd static void       doit __P((void));
    107  1.4      cgd static void       startup __P((void));
    108  1.4      cgd static void       chkhost __P((struct sockaddr_in *));
    109  1.1      cgd 
    110  1.5  hpeyerl uid_t	uid, euid;
    111  1.5  hpeyerl 
    112  1.4      cgd int
    113  1.1      cgd main(argc, argv)
    114  1.1      cgd 	int argc;
    115  1.1      cgd 	char **argv;
    116  1.1      cgd {
    117  1.4      cgd 	int f, funix, finet, options, fromlen;
    118  1.4      cgd 	fd_set defreadfds;
    119  1.4      cgd 	struct sockaddr_un un, fromunix;
    120  1.1      cgd 	struct sockaddr_in sin, frominet;
    121  1.1      cgd 	int omask, lfd;
    122  1.1      cgd 
    123  1.5  hpeyerl 	euid = geteuid();	/* these shouldn't be different */
    124  1.5  hpeyerl 	uid = getuid();
    125  1.4      cgd 	options = 0;
    126  1.1      cgd 	gethostname(host, sizeof(host));
    127  1.1      cgd 	name = argv[0];
    128  1.1      cgd 
    129  1.1      cgd 	while (--argc > 0) {
    130  1.1      cgd 		argv++;
    131  1.1      cgd 		if (argv[0][0] == '-')
    132  1.1      cgd 			switch (argv[0][1]) {
    133  1.1      cgd 			case 'd':
    134  1.1      cgd 				options |= SO_DEBUG;
    135  1.1      cgd 				break;
    136  1.1      cgd 			case 'l':
    137  1.1      cgd 				lflag++;
    138  1.1      cgd 				break;
    139  1.8    perry 			case 's':
    140  1.8    perry 				sflag++;
    141  1.8    perry 				break;
    142  1.1      cgd 			}
    143  1.1      cgd 	}
    144  1.1      cgd 
    145  1.1      cgd #ifndef DEBUG
    146  1.1      cgd 	/*
    147  1.1      cgd 	 * Set up standard environment by detaching from the parent.
    148  1.1      cgd 	 */
    149  1.1      cgd 	daemon(0, 0);
    150  1.1      cgd #endif
    151  1.1      cgd 
    152  1.1      cgd 	openlog("lpd", LOG_PID, LOG_LPR);
    153  1.4      cgd 	syslog(LOG_INFO, "restarted");
    154  1.1      cgd 	(void) umask(0);
    155  1.1      cgd 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
    156  1.1      cgd 	if (lfd < 0) {
    157  1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    158  1.1      cgd 		exit(1);
    159  1.1      cgd 	}
    160  1.1      cgd 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
    161  1.1      cgd 		if (errno == EWOULDBLOCK)	/* active deamon present */
    162  1.1      cgd 			exit(0);
    163  1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    164  1.1      cgd 		exit(1);
    165  1.1      cgd 	}
    166  1.1      cgd 	ftruncate(lfd, 0);
    167  1.1      cgd 	/*
    168  1.1      cgd 	 * write process id for others to know
    169  1.1      cgd 	 */
    170  1.1      cgd 	sprintf(line, "%u\n", getpid());
    171  1.1      cgd 	f = strlen(line);
    172  1.1      cgd 	if (write(lfd, line, f) != f) {
    173  1.1      cgd 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    174  1.1      cgd 		exit(1);
    175  1.1      cgd 	}
    176  1.1      cgd 	signal(SIGCHLD, reapchild);
    177  1.1      cgd 	/*
    178  1.1      cgd 	 * Restart all the printers.
    179  1.1      cgd 	 */
    180  1.1      cgd 	startup();
    181  1.1      cgd 	(void) unlink(_PATH_SOCKETNAME);
    182  1.1      cgd 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
    183  1.1      cgd 	if (funix < 0) {
    184  1.1      cgd 		syslog(LOG_ERR, "socket: %m");
    185  1.1      cgd 		exit(1);
    186  1.1      cgd 	}
    187  1.1      cgd #define	mask(s)	(1 << ((s) - 1))
    188  1.1      cgd 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
    189  1.1      cgd 	signal(SIGHUP, mcleanup);
    190  1.1      cgd 	signal(SIGINT, mcleanup);
    191  1.1      cgd 	signal(SIGQUIT, mcleanup);
    192  1.1      cgd 	signal(SIGTERM, mcleanup);
    193  1.4      cgd 	memset(&un, 0, sizeof(un));
    194  1.4      cgd 	un.sun_family = AF_UNIX;
    195  1.4      cgd 	strcpy(un.sun_path, _PATH_SOCKETNAME);
    196  1.4      cgd #ifndef SUN_LEN
    197  1.4      cgd #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    198  1.4      cgd #endif
    199  1.4      cgd 	if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
    200  1.1      cgd 		syslog(LOG_ERR, "ubind: %m");
    201  1.1      cgd 		exit(1);
    202  1.1      cgd 	}
    203  1.1      cgd 	sigsetmask(omask);
    204  1.4      cgd 	FD_ZERO(&defreadfds);
    205  1.4      cgd 	FD_SET(funix, &defreadfds);
    206  1.1      cgd 	listen(funix, 5);
    207  1.8    perry 	if (!sflag)
    208  1.8    perry 		finet = socket(AF_INET, SOCK_STREAM, 0);
    209  1.8    perry 	else
    210  1.8    perry 		finet = -1;	/* pretend we couldn't open TCP socket. */
    211  1.1      cgd 	if (finet >= 0) {
    212  1.1      cgd 		struct servent *sp;
    213  1.1      cgd 
    214  1.1      cgd 		if (options & SO_DEBUG)
    215  1.1      cgd 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
    216  1.1      cgd 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    217  1.4      cgd 				mcleanup(0);
    218  1.1      cgd 			}
    219  1.1      cgd 		sp = getservbyname("printer", "tcp");
    220  1.1      cgd 		if (sp == NULL) {
    221  1.1      cgd 			syslog(LOG_ERR, "printer/tcp: unknown service");
    222  1.4      cgd 			mcleanup(0);
    223  1.1      cgd 		}
    224  1.4      cgd 		memset(&sin, 0, sizeof(sin));
    225  1.1      cgd 		sin.sin_family = AF_INET;
    226  1.1      cgd 		sin.sin_port = sp->s_port;
    227  1.1      cgd 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    228  1.1      cgd 			syslog(LOG_ERR, "bind: %m");
    229  1.4      cgd 			mcleanup(0);
    230  1.1      cgd 		}
    231  1.4      cgd 		FD_SET(finet, &defreadfds);
    232  1.1      cgd 		listen(finet, 5);
    233  1.1      cgd 	}
    234  1.1      cgd 	/*
    235  1.1      cgd 	 * Main loop: accept, do a request, continue.
    236  1.1      cgd 	 */
    237  1.4      cgd 	memset(&frominet, 0, sizeof(frominet));
    238  1.4      cgd 	memset(&fromunix, 0, sizeof(fromunix));
    239  1.1      cgd 	for (;;) {
    240  1.4      cgd 		int domain, nfds, s;
    241  1.4      cgd 		fd_set readfds;
    242  1.1      cgd 
    243  1.4      cgd 		FD_COPY(&defreadfds, &readfds);
    244  1.1      cgd 		nfds = select(20, &readfds, 0, 0, 0);
    245  1.1      cgd 		if (nfds <= 0) {
    246  1.1      cgd 			if (nfds < 0 && errno != EINTR)
    247  1.1      cgd 				syslog(LOG_WARNING, "select: %m");
    248  1.1      cgd 			continue;
    249  1.1      cgd 		}
    250  1.4      cgd 		if (FD_ISSET(funix, &readfds)) {
    251  1.1      cgd 			domain = AF_UNIX, fromlen = sizeof(fromunix);
    252  1.1      cgd 			s = accept(funix,
    253  1.1      cgd 			    (struct sockaddr *)&fromunix, &fromlen);
    254  1.4      cgd 		} else /* if (FD_ISSET(finet, &readfds)) */  {
    255  1.1      cgd 			domain = AF_INET, fromlen = sizeof(frominet);
    256  1.1      cgd 			s = accept(finet,
    257  1.1      cgd 			    (struct sockaddr *)&frominet, &fromlen);
    258  1.1      cgd 		}
    259  1.1      cgd 		if (s < 0) {
    260  1.1      cgd 			if (errno != EINTR)
    261  1.1      cgd 				syslog(LOG_WARNING, "accept: %m");
    262  1.1      cgd 			continue;
    263  1.1      cgd 		}
    264  1.1      cgd 		if (fork() == 0) {
    265  1.1      cgd 			signal(SIGCHLD, SIG_IGN);
    266  1.1      cgd 			signal(SIGHUP, SIG_IGN);
    267  1.1      cgd 			signal(SIGINT, SIG_IGN);
    268  1.1      cgd 			signal(SIGQUIT, SIG_IGN);
    269  1.1      cgd 			signal(SIGTERM, SIG_IGN);
    270  1.1      cgd 			(void) close(funix);
    271  1.8    perry 			if (!sflag)
    272  1.8    perry 				(void) close(finet);
    273  1.1      cgd 			dup2(s, 1);
    274  1.1      cgd 			(void) close(s);
    275  1.1      cgd 			if (domain == AF_INET) {
    276  1.1      cgd 				from_remote = 1;
    277  1.1      cgd 				chkhost(&frominet);
    278  1.1      cgd 			} else
    279  1.1      cgd 				from_remote = 0;
    280  1.1      cgd 			doit();
    281  1.1      cgd 			exit(0);
    282  1.1      cgd 		}
    283  1.1      cgd 		(void) close(s);
    284  1.1      cgd 	}
    285  1.1      cgd }
    286  1.1      cgd 
    287  1.4      cgd static void
    288  1.4      cgd reapchild(signo)
    289  1.4      cgd 	int signo;
    290  1.1      cgd {
    291  1.1      cgd 	union wait status;
    292  1.1      cgd 
    293  1.1      cgd 	while (wait3((int *)&status, WNOHANG, 0) > 0)
    294  1.1      cgd 		;
    295  1.1      cgd }
    296  1.1      cgd 
    297  1.4      cgd static void
    298  1.4      cgd mcleanup(signo)
    299  1.4      cgd 	int signo;
    300  1.1      cgd {
    301  1.1      cgd 	if (lflag)
    302  1.1      cgd 		syslog(LOG_INFO, "exiting");
    303  1.1      cgd 	unlink(_PATH_SOCKETNAME);
    304  1.1      cgd 	exit(0);
    305  1.1      cgd }
    306  1.1      cgd 
    307  1.1      cgd /*
    308  1.1      cgd  * Stuff for handling job specifications
    309  1.1      cgd  */
    310  1.1      cgd char	*user[MAXUSERS];	/* users to process */
    311  1.1      cgd int	users;			/* # of users in user array */
    312  1.1      cgd int	requ[MAXREQUESTS];	/* job number of spool entries */
    313  1.1      cgd int	requests;		/* # of spool requests */
    314  1.1      cgd char	*person;		/* name of person doing lprm */
    315  1.1      cgd 
    316  1.4      cgd char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
    317  1.4      cgd char	cbuf[BUFSIZ];		/* command line buffer */
    318  1.1      cgd char	*cmdnames[] = {
    319  1.1      cgd 	"null",
    320  1.1      cgd 	"printjob",
    321  1.1      cgd 	"recvjob",
    322  1.1      cgd 	"displayq short",
    323  1.1      cgd 	"displayq long",
    324  1.1      cgd 	"rmjob"
    325  1.1      cgd };
    326  1.1      cgd 
    327  1.4      cgd static void
    328  1.1      cgd doit()
    329  1.1      cgd {
    330  1.1      cgd 	register char *cp;
    331  1.1      cgd 	register int n;
    332  1.1      cgd 
    333  1.1      cgd 	for (;;) {
    334  1.1      cgd 		cp = cbuf;
    335  1.1      cgd 		do {
    336  1.1      cgd 			if (cp >= &cbuf[sizeof(cbuf) - 1])
    337  1.1      cgd 				fatal("Command line too long");
    338  1.1      cgd 			if ((n = read(1, cp, 1)) != 1) {
    339  1.1      cgd 				if (n < 0)
    340  1.1      cgd 					fatal("Lost connection");
    341  1.1      cgd 				return;
    342  1.1      cgd 			}
    343  1.1      cgd 		} while (*cp++ != '\n');
    344  1.1      cgd 		*--cp = '\0';
    345  1.1      cgd 		cp = cbuf;
    346  1.1      cgd 		if (lflag) {
    347  1.1      cgd 			if (*cp >= '\1' && *cp <= '\5')
    348  1.1      cgd 				syslog(LOG_INFO, "%s requests %s %s",
    349  1.1      cgd 					from, cmdnames[*cp], cp+1);
    350  1.1      cgd 			else
    351  1.1      cgd 				syslog(LOG_INFO, "bad request (%d) from %s",
    352  1.1      cgd 					*cp, from);
    353  1.1      cgd 		}
    354  1.1      cgd 		switch (*cp++) {
    355  1.1      cgd 		case '\1':	/* check the queue and print any jobs there */
    356  1.1      cgd 			printer = cp;
    357  1.1      cgd 			printjob();
    358  1.1      cgd 			break;
    359  1.1      cgd 		case '\2':	/* receive files to be queued */
    360  1.1      cgd 			if (!from_remote) {
    361  1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    362  1.1      cgd 				exit(1);
    363  1.1      cgd 			}
    364  1.1      cgd 			printer = cp;
    365  1.1      cgd 			recvjob();
    366  1.1      cgd 			break;
    367  1.1      cgd 		case '\3':	/* display the queue (short form) */
    368  1.1      cgd 		case '\4':	/* display the queue (long form) */
    369  1.1      cgd 			printer = cp;
    370  1.1      cgd 			while (*cp) {
    371  1.1      cgd 				if (*cp != ' ') {
    372  1.1      cgd 					cp++;
    373  1.1      cgd 					continue;
    374  1.1      cgd 				}
    375  1.1      cgd 				*cp++ = '\0';
    376  1.1      cgd 				while (isspace(*cp))
    377  1.1      cgd 					cp++;
    378  1.1      cgd 				if (*cp == '\0')
    379  1.1      cgd 					break;
    380  1.1      cgd 				if (isdigit(*cp)) {
    381  1.1      cgd 					if (requests >= MAXREQUESTS)
    382  1.1      cgd 						fatal("Too many requests");
    383  1.1      cgd 					requ[requests++] = atoi(cp);
    384  1.1      cgd 				} else {
    385  1.1      cgd 					if (users >= MAXUSERS)
    386  1.1      cgd 						fatal("Too many users");
    387  1.1      cgd 					user[users++] = cp;
    388  1.1      cgd 				}
    389  1.1      cgd 			}
    390  1.1      cgd 			displayq(cbuf[0] - '\3');
    391  1.1      cgd 			exit(0);
    392  1.1      cgd 		case '\5':	/* remove a job from the queue */
    393  1.1      cgd 			if (!from_remote) {
    394  1.1      cgd 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    395  1.1      cgd 				exit(1);
    396  1.1      cgd 			}
    397  1.1      cgd 			printer = cp;
    398  1.1      cgd 			while (*cp && *cp != ' ')
    399  1.1      cgd 				cp++;
    400  1.1      cgd 			if (!*cp)
    401  1.1      cgd 				break;
    402  1.1      cgd 			*cp++ = '\0';
    403  1.1      cgd 			person = cp;
    404  1.1      cgd 			while (*cp) {
    405  1.1      cgd 				if (*cp != ' ') {
    406  1.1      cgd 					cp++;
    407  1.1      cgd 					continue;
    408  1.1      cgd 				}
    409  1.1      cgd 				*cp++ = '\0';
    410  1.1      cgd 				while (isspace(*cp))
    411  1.1      cgd 					cp++;
    412  1.1      cgd 				if (*cp == '\0')
    413  1.1      cgd 					break;
    414  1.1      cgd 				if (isdigit(*cp)) {
    415  1.1      cgd 					if (requests >= MAXREQUESTS)
    416  1.1      cgd 						fatal("Too many requests");
    417  1.1      cgd 					requ[requests++] = atoi(cp);
    418  1.1      cgd 				} else {
    419  1.1      cgd 					if (users >= MAXUSERS)
    420  1.1      cgd 						fatal("Too many users");
    421  1.1      cgd 					user[users++] = cp;
    422  1.1      cgd 				}
    423  1.1      cgd 			}
    424  1.1      cgd 			rmjob();
    425  1.1      cgd 			break;
    426  1.1      cgd 		}
    427  1.1      cgd 		fatal("Illegal service request");
    428  1.1      cgd 	}
    429  1.1      cgd }
    430  1.1      cgd 
    431  1.1      cgd /*
    432  1.1      cgd  * Make a pass through the printcap database and start printing any
    433  1.1      cgd  * files left from the last time the machine went down.
    434  1.1      cgd  */
    435  1.4      cgd static void
    436  1.1      cgd startup()
    437  1.1      cgd {
    438  1.4      cgd 	char *buf;
    439  1.1      cgd 	register char *cp;
    440  1.1      cgd 	int pid;
    441  1.1      cgd 
    442  1.1      cgd 	/*
    443  1.1      cgd 	 * Restart the daemons.
    444  1.1      cgd 	 */
    445  1.4      cgd 	while (cgetnext(&buf, printcapdb) > 0) {
    446  1.1      cgd 		for (cp = buf; *cp; cp++)
    447  1.1      cgd 			if (*cp == '|' || *cp == ':') {
    448  1.1      cgd 				*cp = '\0';
    449  1.1      cgd 				break;
    450  1.1      cgd 			}
    451  1.1      cgd 		if ((pid = fork()) < 0) {
    452  1.1      cgd 			syslog(LOG_WARNING, "startup: cannot fork");
    453  1.4      cgd 			mcleanup(0);
    454  1.1      cgd 		}
    455  1.1      cgd 		if (!pid) {
    456  1.4      cgd 			printer = buf;
    457  1.4      cgd 			cgetclose();
    458  1.1      cgd 			printjob();
    459  1.1      cgd 		}
    460  1.1      cgd 	}
    461  1.1      cgd }
    462  1.1      cgd 
    463  1.1      cgd #define DUMMY ":nobody::"
    464  1.1      cgd 
    465  1.1      cgd /*
    466  1.1      cgd  * Check to see if the from host has access to the line printer.
    467  1.1      cgd  */
    468  1.4      cgd static void
    469  1.1      cgd chkhost(f)
    470  1.1      cgd 	struct sockaddr_in *f;
    471  1.1      cgd {
    472  1.1      cgd 	register struct hostent *hp;
    473  1.1      cgd 	register FILE *hostf;
    474  1.1      cgd 	int first = 1;
    475  1.1      cgd 	extern char *inet_ntoa();
    476  1.1      cgd 
    477  1.1      cgd 	f->sin_port = ntohs(f->sin_port);
    478  1.1      cgd 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
    479  1.1      cgd 		fatal("Malformed from address");
    480  1.4      cgd 
    481  1.4      cgd 	/* Need real hostname for temporary filenames */
    482  1.1      cgd 	hp = gethostbyaddr((char *)&f->sin_addr,
    483  1.1      cgd 	    sizeof(struct in_addr), f->sin_family);
    484  1.4      cgd 	if (hp == NULL)
    485  1.1      cgd 		fatal("Host name for your address (%s) unknown",
    486  1.1      cgd 			inet_ntoa(f->sin_addr));
    487  1.1      cgd 
    488  1.4      cgd 	(void) strncpy(fromb, hp->h_name, sizeof(fromb));
    489  1.4      cgd 	from[sizeof(fromb) - 1] = '\0';
    490  1.1      cgd 	from = fromb;
    491  1.1      cgd 
    492  1.1      cgd 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
    493  1.1      cgd again:
    494  1.1      cgd 	if (hostf) {
    495  1.4      cgd 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
    496  1.4      cgd 		    DUMMY, DUMMY) == 0) {
    497  1.1      cgd 			(void) fclose(hostf);
    498  1.1      cgd 			return;
    499  1.1      cgd 		}
    500  1.1      cgd 		(void) fclose(hostf);
    501  1.1      cgd 	}
    502  1.1      cgd 	if (first == 1) {
    503  1.1      cgd 		first = 0;
    504  1.1      cgd 		hostf = fopen(_PATH_HOSTSLPD, "r");
    505  1.1      cgd 		goto again;
    506  1.1      cgd 	}
    507  1.1      cgd 	fatal("Your host does not have line printer access");
    508  1.4      cgd 	/*NOTREACHED*/
    509  1.1      cgd }
    510