xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 14708)
113957Ssam #ifndef lint
2*14708Sralph static char sccsid[] = "@(#)lpd.c	4.9 (Berkeley) 08/18/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 	}
10414149Sralph #define	mask(s)	(1 << ((s) - 1))
10514149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
10614149Sralph 	signal(SIGHUP, cleanup);
10714149Sralph 	signal(SIGINT, cleanup);
10814149Sralph 	signal(SIGQUIT, cleanup);
10914149Sralph 	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);
11714149Sralph 	defreadfds = 1 << funix;
11814149Sralph 	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 		}
13914149Sralph 		defreadfds |= 1 << finet;
14014149Sralph 		listen(finet, 5);
14113816Ssam 	}
14212106Sralph 	/*
14314149Sralph 	 * 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);
172*14708Sralph 			signal(SIGHUP, SIG_IGN);
173*14708Sralph 			signal(SIGINT, SIG_IGN);
174*14708Sralph 			signal(SIGQUIT, SIG_IGN);
175*14708Sralph 			signal(SIGTERM, SIG_IGN);
17613816Ssam 			(void) close(funix);
17713816Ssam 			(void) close(finet);
17813816Ssam 			dup2(s, 1);
17913816Ssam 			(void) close(s);
18013816Ssam 			if (domain == AF_INET)
18113816Ssam 				chkhost(&frominet);
18213816Ssam 			doit();
18312106Sralph 			exit(0);
18412106Sralph 		}
18512106Sralph 		(void) close(s);
18612106Sralph 	}
18712106Sralph }
18812106Sralph 
18912875Sralph static
19012106Sralph reapchild()
19112106Sralph {
19212106Sralph 	union wait status;
19312106Sralph 
19412106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
19512106Sralph 		;
19612106Sralph }
19712106Sralph 
19813816Ssam static
19913816Ssam cleanup()
20013816Ssam {
20114149Sralph 	if (lflag)
20214149Sralph 		log("cleanup()");
20313816Ssam 	unlink(SOCKETNAME);
20413816Ssam 	exit(0);
20513816Ssam }
20613816Ssam 
20712106Sralph /*
20812106Sralph  * Stuff for handling job specifications
20912106Sralph  */
21012106Sralph char	*user[MAXUSERS];	/* users to process */
21112106Sralph int	users;			/* # of users in user array */
21212106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
21312106Sralph int	requests;		/* # of spool requests */
21412875Sralph char	*person;		/* name of person doing lprm */
21512106Sralph 
21612875Sralph static char	fromb[32];	/* buffer for client's machine name */
21712875Sralph static char	cbuf[BUFSIZ];	/* command line buffer */
21812875Sralph static char	*cmdnames[] = {
21912430Sralph 	"null",
22012430Sralph 	"printjob",
22112430Sralph 	"recvjob",
22212430Sralph 	"displayq short",
22312430Sralph 	"displayq long",
22412430Sralph 	"rmjob"
22512430Sralph };
22612106Sralph 
22712875Sralph static
22813816Ssam doit()
22912106Sralph {
23012106Sralph 	register char *cp;
23112106Sralph 	register int n;
23212106Sralph 
23312106Sralph 	for (;;) {
23412106Sralph 		cp = cbuf;
23512106Sralph 		do {
23613816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
23713816Ssam 				fatal("Command line too long");
23813816Ssam 			if ((n = read(1, cp, 1)) != 1) {
23912106Sralph 				if (n < 0)
24012106Sralph 					fatal("Lost connection");
24112106Sralph 				return;
24212106Sralph 			}
24313816Ssam 		} while (*cp++ != '\n');
24412106Sralph 		*--cp = '\0';
24512106Sralph 		cp = cbuf;
24612430Sralph 		if (lflag && *cp >= '\1' && *cp <= '\5') {
24712430Sralph 			printer = NULL;
24812430Sralph 			log("%s requests %s %s", from, cmdnames[*cp], cp+1);
24912430Sralph 		}
25012106Sralph 		switch (*cp++) {
25112106Sralph 		case '\1':	/* check the queue and print any jobs there */
25212106Sralph 			printer = cp;
25312106Sralph 			printjob();
25412106Sralph 			break;
25512106Sralph 		case '\2':	/* receive files to be queued */
25612106Sralph 			printer = cp;
25712106Sralph 			recvjob();
25812106Sralph 			break;
25912430Sralph 		case '\3':	/* display the queue (short form) */
26012430Sralph 		case '\4':	/* display the queue (long form) */
26112106Sralph 			printer = cp;
26212106Sralph 			while (*cp) {
26312106Sralph 				if (*cp != ' ') {
26412106Sralph 					cp++;
26512106Sralph 					continue;
26612106Sralph 				}
26712106Sralph 				*cp++ = '\0';
26812106Sralph 				while (isspace(*cp))
26912106Sralph 					cp++;
27012106Sralph 				if (*cp == '\0')
27112106Sralph 					break;
27212106Sralph 				if (isdigit(*cp)) {
27312106Sralph 					if (requests >= MAXREQUESTS)
27412106Sralph 						fatal("Too many requests");
27512106Sralph 					requ[requests++] = atoi(cp);
27612106Sralph 				} else {
27712106Sralph 					if (users >= MAXUSERS)
27812106Sralph 						fatal("Too many users");
27912106Sralph 					user[users++] = cp;
28012106Sralph 				}
28112106Sralph 			}
28212106Sralph 			displayq(cbuf[0] - '\3');
28312106Sralph 			exit(0);
28412106Sralph 		case '\5':	/* remove a job from the queue */
28512106Sralph 			printer = cp;
28612106Sralph 			while (*cp && *cp != ' ')
28712106Sralph 				cp++;
28812106Sralph 			if (!*cp)
28912106Sralph 				break;
29012106Sralph 			*cp++ = '\0';
29112106Sralph 			person = cp;
29212106Sralph 			while (*cp) {
29312106Sralph 				if (*cp != ' ') {
29412106Sralph 					cp++;
29512106Sralph 					continue;
29612106Sralph 				}
29712106Sralph 				*cp++ = '\0';
29812106Sralph 				while (isspace(*cp))
29912106Sralph 					cp++;
30012106Sralph 				if (*cp == '\0')
30112106Sralph 					break;
30212106Sralph 				if (isdigit(*cp)) {
30312106Sralph 					if (requests >= MAXREQUESTS)
30412106Sralph 						fatal("Too many requests");
30512106Sralph 					requ[requests++] = atoi(cp);
30612106Sralph 				} else {
30712106Sralph 					if (users >= MAXUSERS)
30812106Sralph 						fatal("Too many users");
30912106Sralph 					user[users++] = cp;
31012106Sralph 				}
31112106Sralph 			}
31212106Sralph 			rmjob();
31312106Sralph 			break;
31412106Sralph 		}
31512106Sralph 		fatal("Illegal service request");
31612106Sralph 	}
31712106Sralph }
31812106Sralph 
31912106Sralph /*
32012430Sralph  * Make a pass through the printcap database and start printing any
32112430Sralph  * files left from the last time the machine went down.
32212430Sralph  */
32312875Sralph static
32412430Sralph startup()
32512430Sralph {
32612430Sralph 	char buf[BUFSIZ];
32712430Sralph 	register char *cp;
32812430Sralph 	int pid;
32912430Sralph 
33012430Sralph 	printer = buf;
33112430Sralph 
33212430Sralph 	/*
33312430Sralph 	 * Restart the daemons.
33412430Sralph 	 */
33512430Sralph 	while (getprent(buf) > 0) {
33612430Sralph 		for (cp = buf; *cp; cp++)
33712430Sralph 			if (*cp == '|' || *cp == ':') {
33812430Sralph 				*cp = '\0';
33912430Sralph 				break;
34012430Sralph 			}
34112430Sralph 		if ((pid = fork()) < 0) {
34212430Sralph 			log("startup: cannot fork");
34313816Ssam 			cleanup();
34412430Sralph 		}
34512430Sralph 		if (!pid) {
34612430Sralph 			endprent();
34712430Sralph 			printjob();
34812430Sralph 		}
34912430Sralph 	}
35012430Sralph }
35112430Sralph 
35212430Sralph /*
35312430Sralph  * Check to see if the from host has access to the line printer.
35412430Sralph  */
35512875Sralph static
35613816Ssam chkhost(f)
35713816Ssam 	struct sockaddr_in *f;
35812430Sralph {
35913816Ssam 	register struct hostent *hp;
36012430Sralph 	register FILE *hostf;
36112430Sralph 	register char *cp;
36212430Sralph 	char ahost[50];
36313816Ssam 	extern char *inet_ntoa();
36412430Sralph 
36513816Ssam 	f->sin_port = ntohs(f->sin_port);
36613816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
36713816Ssam 		fatal("Malformed from address");
36813816Ssam 	hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
36913816Ssam 	if (hp == 0)
37013816Ssam 		fatal("Host name for your address (%s) unknown",
37113816Ssam 			inet_ntoa(f->sin_addr));
37213816Ssam 
37313816Ssam 	strcpy(fromb, hp->h_name);
37413816Ssam 	from = fromb;
37512734Sralph 	if (!strcmp(from, host))
37613816Ssam 		return;
37712734Sralph 
37812430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
37912430Sralph 	while (fgets(ahost, sizeof(ahost), hostf)) {
38012430Sralph 		if (cp = index(ahost, '\n'))
38112430Sralph 			*cp = '\0';
38212430Sralph 		cp = index(ahost, ' ');
38312430Sralph 		if (!strcmp(from, ahost) && cp == NULL) {
38412430Sralph 			(void) fclose(hostf);
38513816Ssam 			return;
38612430Sralph 		}
38712430Sralph 	}
38813816Ssam 	fatal("Your host does not have line printer access");
38912430Sralph }
39012430Sralph 
39112106Sralph /*VARARGS1*/
39212106Sralph log(msg, a1, a2, a3)
39312106Sralph 	char *msg;
39412106Sralph {
39512106Sralph 	short console = isatty(fileno(stderr));
39612106Sralph 
39712106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
39812106Sralph 	if (printer)
39912106Sralph 		fprintf(stderr, "%s: ", printer);
40012106Sralph 	fprintf(stderr, msg, a1, a2, a3);
40112106Sralph 	if (console)
40212106Sralph 		putc('\r', stderr);
40312106Sralph 	putc('\n', stderr);
40412106Sralph 	fflush(stderr);
40512106Sralph }
40612106Sralph 
40712875Sralph static
40813816Ssam logerr(msg)
40912106Sralph 	char *msg;
41012106Sralph {
41112106Sralph 	register int err = errno;
41212106Sralph 	short console = isatty(fileno(stderr));
41312106Sralph 	extern int sys_nerr;
41412106Sralph 	extern char *sys_errlist[];
41512106Sralph 
41612106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
41712430Sralph 	if (msg)
41812106Sralph 		fprintf(stderr, "%s: ", msg);
41912106Sralph 	fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
42012106Sralph 	if (console)
42112106Sralph 		putc('\r', stderr);
42212106Sralph 	putc('\n', stderr);
42312106Sralph 	fflush(stderr);
42412106Sralph }
425