xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 14149)
113957Ssam #ifndef lint
2*14149Sralph static char sccsid[] = "@(#)lpd.c	4.8 (Berkeley) 07/27/83";
313957Ssam #endif
413957Ssam 
512106Sralph /*
612106Sralph  * lpd -- line printer daemon.
712106Sralph  *
812106Sralph  * Listen for a connection and perform the requested operation.
912106Sralph  * Operations are:
1012106Sralph  *	\1printer\n
1112106Sralph  *		check the queue for jobs and print any found.
1212106Sralph  *	\2printer\n
1312106Sralph  *		receive a job from another machine and queue it.
1412106Sralph  *	\3printer [users ...] [jobs ...]\n
1512106Sralph  *		return the current state of the queue (short form).
1612106Sralph  *	\4printer [users ...] [jobs ...]\n
1712106Sralph  *		return the current state of the queue (long form).
1812106Sralph  *	\5printer person [users ...] [jobs ...]\n
1912106Sralph  *		remove jobs from the queue.
2012430Sralph  *	\6printer\n
2112430Sralph  *		enable queuing on the specified printer queue.
2212430Sralph  *	\7printer\n
2312430Sralph  *		disable queuing on the specified printer queue.
2412430Sralph  *	\8printer\n
2512430Sralph  *		return the queue status (queuing enabled or disabled).
2612106Sralph  *
2712106Sralph  * Strategy to maintain protected spooling area:
2812106Sralph  *	1. Spooling area is writable only by daemon and spooling group
2912106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
3012106Sralph  *	   root to access any file it wants (verifying things before
3112106Sralph  *	   with an access call) and group id to know how it should
3212106Sralph  *	   set up ownership of files in the spooling area.
3312430Sralph  *	3. Files in spooling area are owned by root, group spooling
3412106Sralph  *	   group, with mode 660.
3512106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
3612106Sralph  *	   access files and printer.  Users can't get to anything
3712106Sralph  *	   w/o help of lpq and lprm programs.
3812106Sralph  */
3912106Sralph 
4012106Sralph #include "lp.h"
4112106Sralph 
4212875Sralph static int	lflag;				/* log requests flag */
4312875Sralph static char	*logfile = DEFLOGF;
4412875Sralph 
4512106Sralph int	reapchild();
4613816Ssam int	cleanup();
4712106Sralph 
4812106Sralph main(argc, argv)
4912106Sralph 	int argc;
5012106Sralph 	char **argv;
5112106Sralph {
5213816Ssam 	int f, funix, finet, options, defreadfds, fromlen;
5313816Ssam 	struct sockaddr_un sun, fromunix;
5413816Ssam 	struct sockaddr_in sin, frominet;
5513816Ssam 	int omask;
5612106Sralph 
5712106Sralph 	gethostname(host, sizeof(host));
5812106Sralph 	name = argv[0];
5912106Sralph 
6012106Sralph 	while (--argc > 0) {
6112106Sralph 		argv++;
6212106Sralph 		if (argv[0][0] == '-')
6312106Sralph 			switch (argv[0][1]) {
6412106Sralph 			case 'd':
6512106Sralph 				options |= SO_DEBUG;
6612106Sralph 				break;
6712106Sralph 			case 'l':
6812430Sralph 				lflag++;
6912430Sralph 				break;
7012430Sralph 			case 'L':
7112106Sralph 				argc--;
7212106Sralph 				logfile = *++argv;
7312106Sralph 				break;
7412106Sralph 			}
7512106Sralph 	}
7612106Sralph #ifndef DEBUG
7712106Sralph 	/*
7812106Sralph 	 * Set up standard environment by detaching from the parent.
7912106Sralph 	 */
8012106Sralph 	if (fork())
8112106Sralph 		exit(0);
8212106Sralph 	for (f = 0; f < 3; f++)
8312106Sralph 		(void) close(f);
8413147Ssam 	(void) open("/dev/null", O_RDONLY);
8513147Ssam 	(void) open("/dev/null", O_WRONLY);
8613147Ssam 	(void) open(logfile, O_WRONLY|O_APPEND);
8713147Ssam 	f = open("/dev/tty", O_RDWR);
8812106Sralph 	if (f > 0) {
8912106Sralph 		ioctl(f, TIOCNOTTY, 0);
9012106Sralph 		(void) close(f);
9112106Sralph 	}
9212106Sralph #endif
9313816Ssam 	signal(SIGCHLD, reapchild);
9412106Sralph 	(void) umask(0);
9512875Sralph 	/*
9612875Sralph 	 * Restart all the printers.
9712875Sralph 	 */
9812875Sralph 	startup();
9913816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
10013816Ssam 	if (funix < 0) {
10113816Ssam 		logerr("socket");
10212106Sralph 		exit(1);
10312106Sralph 	}
104*14149Sralph #define	mask(s)	(1 << ((s) - 1))
105*14149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
106*14149Sralph 	signal(SIGHUP, cleanup);
107*14149Sralph 	signal(SIGINT, cleanup);
108*14149Sralph 	signal(SIGQUIT, cleanup);
109*14149Sralph 	signal(SIGTERM, cleanup);
11013816Ssam 	sun.sun_family = AF_UNIX;
11113816Ssam 	strcpy(sun.sun_path, SOCKETNAME);
11213816Ssam 	if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
11313816Ssam 		logerr("unix domain bind");
11412106Sralph 		exit(1);
11512106Sralph 	}
11613816Ssam 	sigsetmask(omask);
117*14149Sralph 	defreadfds = 1 << funix;
118*14149Sralph 	listen(funix, 5);
11913816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
12013816Ssam 	if (finet >= 0) {
12113816Ssam 		struct servent *sp;
12213816Ssam 
12313816Ssam 		if (options & SO_DEBUG)
12413816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
12513816Ssam 				logerr("setsockopt (SO_DEBUG)");
12613816Ssam 				cleanup();
12713816Ssam 			}
12813816Ssam 		sp = getservbyname("printer", "tcp");
12913816Ssam 		if (sp == NULL) {
13013816Ssam 			log("printer/tcp: unknown service");
13113816Ssam 			cleanup();
13213816Ssam 		}
13313816Ssam 		sin.sin_family = AF_INET;
13413816Ssam 		sin.sin_port = sp->s_port;
13513816Ssam 		if (bind(finet, &sin, sizeof(sin), 0) < 0) {
13613816Ssam 			logerr("internet domain bind");
13713816Ssam 			cleanup();
13813816Ssam 		}
139*14149Sralph 		defreadfds |= 1 << finet;
140*14149Sralph 		listen(finet, 5);
14113816Ssam 	}
14212106Sralph 	/*
143*14149Sralph 	 * Main loop: accept, do a request, continue.
14412106Sralph 	 */
14512106Sralph 	for (;;) {
14613957Ssam 		int domain, nfds, s, readfds = defreadfds;
14712106Sralph 
14813957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
14913957Ssam 		if (nfds <= 0) {
15013957Ssam 			if (nfds < 0 && errno != EINTR) {
15113957Ssam 				logerr("select");
15213957Ssam 				cleanup();
15313957Ssam 				/*NOTREACHED*/
15413957Ssam 			}
15513957Ssam 			continue;
15613957Ssam 		}
15713816Ssam 		if (readfds & (1 << funix)) {
15813957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
15913816Ssam 			s = accept(funix, &fromunix, &fromlen);
16013957Ssam 		} else if (readfds & (1 << finet)) {
16113957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
16213816Ssam 			s = accept(finet, &frominet, &fromlen);
16313816Ssam 		}
16412106Sralph 		if (s < 0) {
16512106Sralph 			if (errno == EINTR)
16612106Sralph 				continue;
16713816Ssam 			logerr("accept");
16813816Ssam 			cleanup();
16912106Sralph 		}
17012106Sralph 		if (fork() == 0) {
17113147Ssam 			signal(SIGCHLD, SIG_IGN);
17213816Ssam 			(void) close(funix);
17313816Ssam 			(void) close(finet);
17413816Ssam 			dup2(s, 1);
17513816Ssam 			(void) close(s);
17613816Ssam 			if (domain == AF_INET)
17713816Ssam 				chkhost(&frominet);
17813816Ssam 			doit();
17912106Sralph 			exit(0);
18012106Sralph 		}
18112106Sralph 		(void) close(s);
18212106Sralph 	}
18312106Sralph }
18412106Sralph 
18512875Sralph static
18612106Sralph reapchild()
18712106Sralph {
18812106Sralph 	union wait status;
18912106Sralph 
19012106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
19112106Sralph 		;
19212106Sralph }
19312106Sralph 
19413816Ssam static
19513816Ssam cleanup()
19613816Ssam {
197*14149Sralph 	if (lflag)
198*14149Sralph 		log("cleanup()");
19913816Ssam 	unlink(SOCKETNAME);
20013816Ssam 	exit(0);
20113816Ssam }
20213816Ssam 
20312106Sralph /*
20412106Sralph  * Stuff for handling job specifications
20512106Sralph  */
20612106Sralph char	*user[MAXUSERS];	/* users to process */
20712106Sralph int	users;			/* # of users in user array */
20812106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
20912106Sralph int	requests;		/* # of spool requests */
21012875Sralph char	*person;		/* name of person doing lprm */
21112106Sralph 
21212875Sralph static char	fromb[32];	/* buffer for client's machine name */
21312875Sralph static char	cbuf[BUFSIZ];	/* command line buffer */
21412875Sralph static char	*cmdnames[] = {
21512430Sralph 	"null",
21612430Sralph 	"printjob",
21712430Sralph 	"recvjob",
21812430Sralph 	"displayq short",
21912430Sralph 	"displayq long",
22012430Sralph 	"rmjob"
22112430Sralph };
22212106Sralph 
22312875Sralph static
22413816Ssam doit()
22512106Sralph {
22612106Sralph 	register char *cp;
22712106Sralph 	register int n;
22812106Sralph 
22912106Sralph 	for (;;) {
23012106Sralph 		cp = cbuf;
23112106Sralph 		do {
23213816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
23313816Ssam 				fatal("Command line too long");
23413816Ssam 			if ((n = read(1, cp, 1)) != 1) {
23512106Sralph 				if (n < 0)
23612106Sralph 					fatal("Lost connection");
23712106Sralph 				return;
23812106Sralph 			}
23913816Ssam 		} while (*cp++ != '\n');
24012106Sralph 		*--cp = '\0';
24112106Sralph 		cp = cbuf;
24212430Sralph 		if (lflag && *cp >= '\1' && *cp <= '\5') {
24312430Sralph 			printer = NULL;
24412430Sralph 			log("%s requests %s %s", from, cmdnames[*cp], cp+1);
24512430Sralph 		}
24612106Sralph 		switch (*cp++) {
24712106Sralph 		case '\1':	/* check the queue and print any jobs there */
24812106Sralph 			printer = cp;
24912106Sralph 			printjob();
25012106Sralph 			break;
25112106Sralph 		case '\2':	/* receive files to be queued */
25212106Sralph 			printer = cp;
25312106Sralph 			recvjob();
25412106Sralph 			break;
25512430Sralph 		case '\3':	/* display the queue (short form) */
25612430Sralph 		case '\4':	/* display the queue (long form) */
25712106Sralph 			printer = cp;
25812106Sralph 			while (*cp) {
25912106Sralph 				if (*cp != ' ') {
26012106Sralph 					cp++;
26112106Sralph 					continue;
26212106Sralph 				}
26312106Sralph 				*cp++ = '\0';
26412106Sralph 				while (isspace(*cp))
26512106Sralph 					cp++;
26612106Sralph 				if (*cp == '\0')
26712106Sralph 					break;
26812106Sralph 				if (isdigit(*cp)) {
26912106Sralph 					if (requests >= MAXREQUESTS)
27012106Sralph 						fatal("Too many requests");
27112106Sralph 					requ[requests++] = atoi(cp);
27212106Sralph 				} else {
27312106Sralph 					if (users >= MAXUSERS)
27412106Sralph 						fatal("Too many users");
27512106Sralph 					user[users++] = cp;
27612106Sralph 				}
27712106Sralph 			}
27812106Sralph 			displayq(cbuf[0] - '\3');
27912106Sralph 			exit(0);
28012106Sralph 		case '\5':	/* remove a job from the queue */
28112106Sralph 			printer = cp;
28212106Sralph 			while (*cp && *cp != ' ')
28312106Sralph 				cp++;
28412106Sralph 			if (!*cp)
28512106Sralph 				break;
28612106Sralph 			*cp++ = '\0';
28712106Sralph 			person = cp;
28812106Sralph 			while (*cp) {
28912106Sralph 				if (*cp != ' ') {
29012106Sralph 					cp++;
29112106Sralph 					continue;
29212106Sralph 				}
29312106Sralph 				*cp++ = '\0';
29412106Sralph 				while (isspace(*cp))
29512106Sralph 					cp++;
29612106Sralph 				if (*cp == '\0')
29712106Sralph 					break;
29812106Sralph 				if (isdigit(*cp)) {
29912106Sralph 					if (requests >= MAXREQUESTS)
30012106Sralph 						fatal("Too many requests");
30112106Sralph 					requ[requests++] = atoi(cp);
30212106Sralph 				} else {
30312106Sralph 					if (users >= MAXUSERS)
30412106Sralph 						fatal("Too many users");
30512106Sralph 					user[users++] = cp;
30612106Sralph 				}
30712106Sralph 			}
30812106Sralph 			rmjob();
30912106Sralph 			break;
31012106Sralph 		}
31112106Sralph 		fatal("Illegal service request");
31212106Sralph 	}
31312106Sralph }
31412106Sralph 
31512106Sralph /*
31612430Sralph  * Make a pass through the printcap database and start printing any
31712430Sralph  * files left from the last time the machine went down.
31812430Sralph  */
31912875Sralph static
32012430Sralph startup()
32112430Sralph {
32212430Sralph 	char buf[BUFSIZ];
32312430Sralph 	register char *cp;
32412430Sralph 	int pid;
32512430Sralph 
32612430Sralph 	printer = buf;
32712430Sralph 
32812430Sralph 	/*
32912430Sralph 	 * Restart the daemons.
33012430Sralph 	 */
33112430Sralph 	while (getprent(buf) > 0) {
33212430Sralph 		for (cp = buf; *cp; cp++)
33312430Sralph 			if (*cp == '|' || *cp == ':') {
33412430Sralph 				*cp = '\0';
33512430Sralph 				break;
33612430Sralph 			}
33712430Sralph 		if ((pid = fork()) < 0) {
33812430Sralph 			log("startup: cannot fork");
33913816Ssam 			cleanup();
34012430Sralph 		}
34112430Sralph 		if (!pid) {
34212430Sralph 			endprent();
34312430Sralph 			printjob();
34412430Sralph 		}
34512430Sralph 	}
34612430Sralph }
34712430Sralph 
34812430Sralph /*
34912430Sralph  * Check to see if the from host has access to the line printer.
35012430Sralph  */
35112875Sralph static
35213816Ssam chkhost(f)
35313816Ssam 	struct sockaddr_in *f;
35412430Sralph {
35513816Ssam 	register struct hostent *hp;
35612430Sralph 	register FILE *hostf;
35712430Sralph 	register char *cp;
35812430Sralph 	char ahost[50];
35913816Ssam 	extern char *inet_ntoa();
36012430Sralph 
36113816Ssam 	f->sin_port = ntohs(f->sin_port);
36213816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
36313816Ssam 		fatal("Malformed from address");
36413816Ssam 	hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
36513816Ssam 	if (hp == 0)
36613816Ssam 		fatal("Host name for your address (%s) unknown",
36713816Ssam 			inet_ntoa(f->sin_addr));
36813816Ssam 
36913816Ssam 	strcpy(fromb, hp->h_name);
37013816Ssam 	from = fromb;
37112734Sralph 	if (!strcmp(from, host))
37213816Ssam 		return;
37312734Sralph 
37412430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
37512430Sralph 	while (fgets(ahost, sizeof(ahost), hostf)) {
37612430Sralph 		if (cp = index(ahost, '\n'))
37712430Sralph 			*cp = '\0';
37812430Sralph 		cp = index(ahost, ' ');
37912430Sralph 		if (!strcmp(from, ahost) && cp == NULL) {
38012430Sralph 			(void) fclose(hostf);
38113816Ssam 			return;
38212430Sralph 		}
38312430Sralph 	}
38413816Ssam 	fatal("Your host does not have line printer access");
38512430Sralph }
38612430Sralph 
38712106Sralph /*VARARGS1*/
38812106Sralph log(msg, a1, a2, a3)
38912106Sralph 	char *msg;
39012106Sralph {
39112106Sralph 	short console = isatty(fileno(stderr));
39212106Sralph 
39312106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
39412106Sralph 	if (printer)
39512106Sralph 		fprintf(stderr, "%s: ", printer);
39612106Sralph 	fprintf(stderr, msg, a1, a2, a3);
39712106Sralph 	if (console)
39812106Sralph 		putc('\r', stderr);
39912106Sralph 	putc('\n', stderr);
40012106Sralph 	fflush(stderr);
40112106Sralph }
40212106Sralph 
40312875Sralph static
40413816Ssam logerr(msg)
40512106Sralph 	char *msg;
40612106Sralph {
40712106Sralph 	register int err = errno;
40812106Sralph 	short console = isatty(fileno(stderr));
40912106Sralph 	extern int sys_nerr;
41012106Sralph 	extern char *sys_errlist[];
41112106Sralph 
41212106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
41312430Sralph 	if (msg)
41412106Sralph 		fprintf(stderr, "%s: ", msg);
41512106Sralph 	fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
41612106Sralph 	if (console)
41712106Sralph 		putc('\r', stderr);
41812106Sralph 	putc('\n', stderr);
41912106Sralph 	fflush(stderr);
42012106Sralph }
421