xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 14837)
113957Ssam #ifndef lint
2*14837Sralph static char sccsid[] = "@(#)lpd.c	4.10 (Berkeley) 08/24/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;
55*14837Sralph 	int omask, lfd;
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 	}
76*14837Sralph 
7712106Sralph #ifndef DEBUG
7812106Sralph 	/*
7912106Sralph 	 * Set up standard environment by detaching from the parent.
8012106Sralph 	 */
8112106Sralph 	if (fork())
8212106Sralph 		exit(0);
8312106Sralph 	for (f = 0; f < 3; f++)
8412106Sralph 		(void) close(f);
8513147Ssam 	(void) open("/dev/null", O_RDONLY);
8613147Ssam 	(void) open("/dev/null", O_WRONLY);
8713147Ssam 	(void) open(logfile, O_WRONLY|O_APPEND);
8813147Ssam 	f = open("/dev/tty", O_RDWR);
8912106Sralph 	if (f > 0) {
9012106Sralph 		ioctl(f, TIOCNOTTY, 0);
9112106Sralph 		(void) close(f);
9212106Sralph 	}
9312106Sralph #endif
94*14837Sralph 
9512106Sralph 	(void) umask(0);
96*14837Sralph 	lfd = open(MASTERLOCK, O_WRONLY|O_CREAT, 0644);
97*14837Sralph 	if (lfd < 0) {
98*14837Sralph 		log("cannot create %s", MASTERLOCK);
99*14837Sralph 		exit(1);
100*14837Sralph 	}
101*14837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
102*14837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
103*14837Sralph 			exit(0);
104*14837Sralph 		log("cannot lock %s", MASTERLOCK);
105*14837Sralph 		exit(1);
106*14837Sralph 	}
107*14837Sralph 	ftruncate(lfd, 0);
10812875Sralph 	/*
109*14837Sralph 	 * write process id for others to know
110*14837Sralph 	 */
111*14837Sralph 	sprintf(line, "%u\n", getpid());
112*14837Sralph 	f = strlen(line);
113*14837Sralph 	if (write(lfd, line, f) != f) {
114*14837Sralph 		log("cannot write daemon pid");
115*14837Sralph 		exit(1);
116*14837Sralph 	}
117*14837Sralph 	signal(SIGCHLD, reapchild);
118*14837Sralph 	/*
11912875Sralph 	 * Restart all the printers.
12012875Sralph 	 */
12112875Sralph 	startup();
122*14837Sralph 	(void) unlink(SOCKETNAME);
12313816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
12413816Ssam 	if (funix < 0) {
12513816Ssam 		logerr("socket");
12612106Sralph 		exit(1);
12712106Sralph 	}
12814149Sralph #define	mask(s)	(1 << ((s) - 1))
12914149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
13014149Sralph 	signal(SIGHUP, cleanup);
13114149Sralph 	signal(SIGINT, cleanup);
13214149Sralph 	signal(SIGQUIT, cleanup);
13314149Sralph 	signal(SIGTERM, cleanup);
13413816Ssam 	sun.sun_family = AF_UNIX;
13513816Ssam 	strcpy(sun.sun_path, SOCKETNAME);
13613816Ssam 	if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
13713816Ssam 		logerr("unix domain bind");
13812106Sralph 		exit(1);
13912106Sralph 	}
14013816Ssam 	sigsetmask(omask);
14114149Sralph 	defreadfds = 1 << funix;
14214149Sralph 	listen(funix, 5);
14313816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
14413816Ssam 	if (finet >= 0) {
14513816Ssam 		struct servent *sp;
14613816Ssam 
14713816Ssam 		if (options & SO_DEBUG)
14813816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
14913816Ssam 				logerr("setsockopt (SO_DEBUG)");
15013816Ssam 				cleanup();
15113816Ssam 			}
15213816Ssam 		sp = getservbyname("printer", "tcp");
15313816Ssam 		if (sp == NULL) {
15413816Ssam 			log("printer/tcp: unknown service");
15513816Ssam 			cleanup();
15613816Ssam 		}
15713816Ssam 		sin.sin_family = AF_INET;
15813816Ssam 		sin.sin_port = sp->s_port;
15913816Ssam 		if (bind(finet, &sin, sizeof(sin), 0) < 0) {
16013816Ssam 			logerr("internet domain bind");
16113816Ssam 			cleanup();
16213816Ssam 		}
16314149Sralph 		defreadfds |= 1 << finet;
16414149Sralph 		listen(finet, 5);
16513816Ssam 	}
16612106Sralph 	/*
16714149Sralph 	 * Main loop: accept, do a request, continue.
16812106Sralph 	 */
16912106Sralph 	for (;;) {
17013957Ssam 		int domain, nfds, s, readfds = defreadfds;
17112106Sralph 
17213957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
17313957Ssam 		if (nfds <= 0) {
17413957Ssam 			if (nfds < 0 && errno != EINTR) {
17513957Ssam 				logerr("select");
17613957Ssam 				cleanup();
17713957Ssam 				/*NOTREACHED*/
17813957Ssam 			}
17913957Ssam 			continue;
18013957Ssam 		}
18113816Ssam 		if (readfds & (1 << funix)) {
18213957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
18313816Ssam 			s = accept(funix, &fromunix, &fromlen);
18413957Ssam 		} else if (readfds & (1 << finet)) {
18513957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
18613816Ssam 			s = accept(finet, &frominet, &fromlen);
18713816Ssam 		}
18812106Sralph 		if (s < 0) {
18912106Sralph 			if (errno == EINTR)
19012106Sralph 				continue;
19113816Ssam 			logerr("accept");
19213816Ssam 			cleanup();
19312106Sralph 		}
19412106Sralph 		if (fork() == 0) {
19513147Ssam 			signal(SIGCHLD, SIG_IGN);
19614708Sralph 			signal(SIGHUP, SIG_IGN);
19714708Sralph 			signal(SIGINT, SIG_IGN);
19814708Sralph 			signal(SIGQUIT, SIG_IGN);
19914708Sralph 			signal(SIGTERM, SIG_IGN);
20013816Ssam 			(void) close(funix);
20113816Ssam 			(void) close(finet);
20213816Ssam 			dup2(s, 1);
20313816Ssam 			(void) close(s);
20413816Ssam 			if (domain == AF_INET)
20513816Ssam 				chkhost(&frominet);
20613816Ssam 			doit();
20712106Sralph 			exit(0);
20812106Sralph 		}
20912106Sralph 		(void) close(s);
21012106Sralph 	}
21112106Sralph }
21212106Sralph 
21312875Sralph static
21412106Sralph reapchild()
21512106Sralph {
21612106Sralph 	union wait status;
21712106Sralph 
21812106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
21912106Sralph 		;
22012106Sralph }
22112106Sralph 
22213816Ssam static
22313816Ssam cleanup()
22413816Ssam {
22514149Sralph 	if (lflag)
22614149Sralph 		log("cleanup()");
22713816Ssam 	unlink(SOCKETNAME);
22813816Ssam 	exit(0);
22913816Ssam }
23013816Ssam 
23112106Sralph /*
23212106Sralph  * Stuff for handling job specifications
23312106Sralph  */
23412106Sralph char	*user[MAXUSERS];	/* users to process */
23512106Sralph int	users;			/* # of users in user array */
23612106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
23712106Sralph int	requests;		/* # of spool requests */
23812875Sralph char	*person;		/* name of person doing lprm */
23912106Sralph 
24012875Sralph static char	fromb[32];	/* buffer for client's machine name */
24112875Sralph static char	cbuf[BUFSIZ];	/* command line buffer */
24212875Sralph static char	*cmdnames[] = {
24312430Sralph 	"null",
24412430Sralph 	"printjob",
24512430Sralph 	"recvjob",
24612430Sralph 	"displayq short",
24712430Sralph 	"displayq long",
24812430Sralph 	"rmjob"
24912430Sralph };
25012106Sralph 
25112875Sralph static
25213816Ssam doit()
25312106Sralph {
25412106Sralph 	register char *cp;
25512106Sralph 	register int n;
25612106Sralph 
25712106Sralph 	for (;;) {
25812106Sralph 		cp = cbuf;
25912106Sralph 		do {
26013816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
26113816Ssam 				fatal("Command line too long");
26213816Ssam 			if ((n = read(1, cp, 1)) != 1) {
26312106Sralph 				if (n < 0)
26412106Sralph 					fatal("Lost connection");
26512106Sralph 				return;
26612106Sralph 			}
26713816Ssam 		} while (*cp++ != '\n');
26812106Sralph 		*--cp = '\0';
26912106Sralph 		cp = cbuf;
27012430Sralph 		if (lflag && *cp >= '\1' && *cp <= '\5') {
27112430Sralph 			printer = NULL;
27212430Sralph 			log("%s requests %s %s", from, cmdnames[*cp], cp+1);
27312430Sralph 		}
27412106Sralph 		switch (*cp++) {
27512106Sralph 		case '\1':	/* check the queue and print any jobs there */
27612106Sralph 			printer = cp;
27712106Sralph 			printjob();
27812106Sralph 			break;
27912106Sralph 		case '\2':	/* receive files to be queued */
28012106Sralph 			printer = cp;
28112106Sralph 			recvjob();
28212106Sralph 			break;
28312430Sralph 		case '\3':	/* display the queue (short form) */
28412430Sralph 		case '\4':	/* display the queue (long form) */
28512106Sralph 			printer = cp;
28612106Sralph 			while (*cp) {
28712106Sralph 				if (*cp != ' ') {
28812106Sralph 					cp++;
28912106Sralph 					continue;
29012106Sralph 				}
29112106Sralph 				*cp++ = '\0';
29212106Sralph 				while (isspace(*cp))
29312106Sralph 					cp++;
29412106Sralph 				if (*cp == '\0')
29512106Sralph 					break;
29612106Sralph 				if (isdigit(*cp)) {
29712106Sralph 					if (requests >= MAXREQUESTS)
29812106Sralph 						fatal("Too many requests");
29912106Sralph 					requ[requests++] = atoi(cp);
30012106Sralph 				} else {
30112106Sralph 					if (users >= MAXUSERS)
30212106Sralph 						fatal("Too many users");
30312106Sralph 					user[users++] = cp;
30412106Sralph 				}
30512106Sralph 			}
30612106Sralph 			displayq(cbuf[0] - '\3');
30712106Sralph 			exit(0);
30812106Sralph 		case '\5':	/* remove a job from the queue */
30912106Sralph 			printer = cp;
31012106Sralph 			while (*cp && *cp != ' ')
31112106Sralph 				cp++;
31212106Sralph 			if (!*cp)
31312106Sralph 				break;
31412106Sralph 			*cp++ = '\0';
31512106Sralph 			person = cp;
31612106Sralph 			while (*cp) {
31712106Sralph 				if (*cp != ' ') {
31812106Sralph 					cp++;
31912106Sralph 					continue;
32012106Sralph 				}
32112106Sralph 				*cp++ = '\0';
32212106Sralph 				while (isspace(*cp))
32312106Sralph 					cp++;
32412106Sralph 				if (*cp == '\0')
32512106Sralph 					break;
32612106Sralph 				if (isdigit(*cp)) {
32712106Sralph 					if (requests >= MAXREQUESTS)
32812106Sralph 						fatal("Too many requests");
32912106Sralph 					requ[requests++] = atoi(cp);
33012106Sralph 				} else {
33112106Sralph 					if (users >= MAXUSERS)
33212106Sralph 						fatal("Too many users");
33312106Sralph 					user[users++] = cp;
33412106Sralph 				}
33512106Sralph 			}
33612106Sralph 			rmjob();
33712106Sralph 			break;
33812106Sralph 		}
33912106Sralph 		fatal("Illegal service request");
34012106Sralph 	}
34112106Sralph }
34212106Sralph 
34312106Sralph /*
34412430Sralph  * Make a pass through the printcap database and start printing any
34512430Sralph  * files left from the last time the machine went down.
34612430Sralph  */
34712875Sralph static
34812430Sralph startup()
34912430Sralph {
35012430Sralph 	char buf[BUFSIZ];
35112430Sralph 	register char *cp;
35212430Sralph 	int pid;
35312430Sralph 
35412430Sralph 	printer = buf;
35512430Sralph 
35612430Sralph 	/*
35712430Sralph 	 * Restart the daemons.
35812430Sralph 	 */
35912430Sralph 	while (getprent(buf) > 0) {
36012430Sralph 		for (cp = buf; *cp; cp++)
36112430Sralph 			if (*cp == '|' || *cp == ':') {
36212430Sralph 				*cp = '\0';
36312430Sralph 				break;
36412430Sralph 			}
36512430Sralph 		if ((pid = fork()) < 0) {
36612430Sralph 			log("startup: cannot fork");
36713816Ssam 			cleanup();
36812430Sralph 		}
36912430Sralph 		if (!pid) {
37012430Sralph 			endprent();
37112430Sralph 			printjob();
37212430Sralph 		}
37312430Sralph 	}
37412430Sralph }
37512430Sralph 
37612430Sralph /*
37712430Sralph  * Check to see if the from host has access to the line printer.
37812430Sralph  */
37912875Sralph static
38013816Ssam chkhost(f)
38113816Ssam 	struct sockaddr_in *f;
38212430Sralph {
38313816Ssam 	register struct hostent *hp;
38412430Sralph 	register FILE *hostf;
38512430Sralph 	register char *cp;
38612430Sralph 	char ahost[50];
38713816Ssam 	extern char *inet_ntoa();
38812430Sralph 
38913816Ssam 	f->sin_port = ntohs(f->sin_port);
39013816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
39113816Ssam 		fatal("Malformed from address");
39213816Ssam 	hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
39313816Ssam 	if (hp == 0)
39413816Ssam 		fatal("Host name for your address (%s) unknown",
39513816Ssam 			inet_ntoa(f->sin_addr));
39613816Ssam 
39713816Ssam 	strcpy(fromb, hp->h_name);
39813816Ssam 	from = fromb;
39912734Sralph 	if (!strcmp(from, host))
40013816Ssam 		return;
40112734Sralph 
40212430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
40312430Sralph 	while (fgets(ahost, sizeof(ahost), hostf)) {
40412430Sralph 		if (cp = index(ahost, '\n'))
40512430Sralph 			*cp = '\0';
40612430Sralph 		cp = index(ahost, ' ');
40712430Sralph 		if (!strcmp(from, ahost) && cp == NULL) {
40812430Sralph 			(void) fclose(hostf);
40913816Ssam 			return;
41012430Sralph 		}
41112430Sralph 	}
41213816Ssam 	fatal("Your host does not have line printer access");
41312430Sralph }
41412430Sralph 
41512106Sralph /*VARARGS1*/
41612106Sralph log(msg, a1, a2, a3)
41712106Sralph 	char *msg;
41812106Sralph {
41912106Sralph 	short console = isatty(fileno(stderr));
42012106Sralph 
42112106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
42212106Sralph 	if (printer)
42312106Sralph 		fprintf(stderr, "%s: ", printer);
42412106Sralph 	fprintf(stderr, msg, a1, a2, a3);
42512106Sralph 	if (console)
42612106Sralph 		putc('\r', stderr);
42712106Sralph 	putc('\n', stderr);
42812106Sralph 	fflush(stderr);
42912106Sralph }
43012106Sralph 
43112875Sralph static
43213816Ssam logerr(msg)
43312106Sralph 	char *msg;
43412106Sralph {
43512106Sralph 	register int err = errno;
43612106Sralph 	short console = isatty(fileno(stderr));
43712106Sralph 	extern int sys_nerr;
43812106Sralph 	extern char *sys_errlist[];
43912106Sralph 
44012106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
44112430Sralph 	if (msg)
44212106Sralph 		fprintf(stderr, "%s: ", msg);
44312106Sralph 	fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
44412106Sralph 	if (console)
44512106Sralph 		putc('\r', stderr);
44612106Sralph 	putc('\n', stderr);
44712106Sralph 	fflush(stderr);
44812106Sralph }
449