xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 12875)
1*12875Sralph /*	lpd.c	4.4	83/06/02	*/
212106Sralph /*
312106Sralph  * lpd -- line printer daemon.
412106Sralph  *
512106Sralph  * Listen for a connection and perform the requested operation.
612106Sralph  * Operations are:
712106Sralph  *	\1printer\n
812106Sralph  *		check the queue for jobs and print any found.
912106Sralph  *	\2printer\n
1012106Sralph  *		receive a job from another machine and queue it.
1112106Sralph  *	\3printer [users ...] [jobs ...]\n
1212106Sralph  *		return the current state of the queue (short form).
1312106Sralph  *	\4printer [users ...] [jobs ...]\n
1412106Sralph  *		return the current state of the queue (long form).
1512106Sralph  *	\5printer person [users ...] [jobs ...]\n
1612106Sralph  *		remove jobs from the queue.
1712430Sralph  *	\6printer\n
1812430Sralph  *		enable queuing on the specified printer queue.
1912430Sralph  *	\7printer\n
2012430Sralph  *		disable queuing on the specified printer queue.
2112430Sralph  *	\8printer\n
2212430Sralph  *		return the queue status (queuing enabled or disabled).
2312106Sralph  *
2412106Sralph  * Strategy to maintain protected spooling area:
2512106Sralph  *	1. Spooling area is writable only by daemon and spooling group
2612106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
2712106Sralph  *	   root to access any file it wants (verifying things before
2812106Sralph  *	   with an access call) and group id to know how it should
2912106Sralph  *	   set up ownership of files in the spooling area.
3012430Sralph  *	3. Files in spooling area are owned by root, group spooling
3112106Sralph  *	   group, with mode 660.
3212106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
3312106Sralph  *	   access files and printer.  Users can't get to anything
3412106Sralph  *	   w/o help of lpq and lprm programs.
3512106Sralph  */
3612106Sralph 
3712106Sralph #include "lp.h"
3812106Sralph 
39*12875Sralph static int	lflag;				/* log requests flag */
40*12875Sralph static char	*logfile = DEFLOGF;
41*12875Sralph static struct	sockaddr_in sin = { AF_INET };
42*12875Sralph 
4312106Sralph int	reapchild();
4412106Sralph char	*ntoa();
4512106Sralph 
4612106Sralph main(argc, argv)
4712106Sralph 	int argc;
4812106Sralph 	char **argv;
4912106Sralph {
5012106Sralph 	int f, options;
5112430Sralph 	struct sockaddr_in fromaddr;
5212106Sralph 	struct servent *sp;
5312106Sralph 
5412106Sralph 	gethostname(host, sizeof(host));
5512106Sralph 	name = argv[0];
5612106Sralph 
5712106Sralph 	sp = getservbyname("printer", "tcp");
5812106Sralph 	if (sp == NULL) {
5912430Sralph 		fprintf(stderr, "printer/tcp: unknown service");
6012106Sralph 		exit(1);
6112106Sralph 	}
6212106Sralph 	sin.sin_port = sp->s_port;
6312106Sralph 
6412106Sralph 	while (--argc > 0) {
6512106Sralph 		argv++;
6612106Sralph 		if (argv[0][0] == '-')
6712106Sralph 			switch (argv[0][1]) {
6812106Sralph 			case 'd':
6912106Sralph 				options |= SO_DEBUG;
7012106Sralph 				break;
7112106Sralph 			case 'l':
7212430Sralph 				lflag++;
7312430Sralph 				break;
7412430Sralph 			case 'L':
7512106Sralph 				argc--;
7612106Sralph 				logfile = *++argv;
7712106Sralph 				break;
7812106Sralph 			}
7912106Sralph 		else {
8012106Sralph 			int port = atoi(argv[0]);
8112430Sralph 			int c = argv[0][0];
8212106Sralph 
8312430Sralph 			if (c < '0' || c > '9' || port < 0) {
8412430Sralph 				fprintf(stderr, "lpd: %s: bad port number\n", argv[0]);
8512106Sralph 				exit(1);
8612106Sralph 			}
8712106Sralph 			sin.sin_port = htons((u_short) port);
8812106Sralph 		}
8912106Sralph 	}
9012106Sralph #ifndef DEBUG
9112106Sralph 	/*
9212106Sralph 	 * Set up standard environment by detaching from the parent.
9312106Sralph 	 */
9412106Sralph 	if (fork())
9512106Sralph 		exit(0);
9612106Sralph 	for (f = 0; f < 3; f++)
9712106Sralph 		(void) close(f);
9812106Sralph 	(void) open("/dev/null", FRDONLY, 0);
9912106Sralph 	(void) open("/dev/null", FWRONLY, 0);
10012106Sralph 	(void) open(logfile, FWRONLY|FAPPEND, 0);
10112106Sralph 	f = open("/dev/tty", FRDWR, 0);
10212106Sralph 	if (f > 0) {
10312106Sralph 		ioctl(f, TIOCNOTTY, 0);
10412106Sralph 		(void) close(f);
10512106Sralph 	}
10612106Sralph #endif
10712106Sralph 	(void) umask(0);
108*12875Sralph 	/*
109*12875Sralph 	 * Restart all the printers.
110*12875Sralph 	 */
111*12875Sralph 	startup();
11212106Sralph 	f = socket(AF_INET, SOCK_STREAM, 0);
11312106Sralph 	if (f < 0) {
11412106Sralph 		logerror("socket");
11512106Sralph 		exit(1);
11612106Sralph 	}
11712106Sralph 	if (options & SO_DEBUG)
11812430Sralph 		if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
11912106Sralph 			logerror("setsockopt (SO_DEBUG)");
12012430Sralph 			exit(1);
12112430Sralph 		}
12212106Sralph 	if (bind(f, &sin, sizeof(sin), 0) < 0) {
12312106Sralph 		logerror("bind");
12412106Sralph 		exit(1);
12512106Sralph 	}
12612106Sralph 	/*
12712106Sralph 	 * Main loop: listen, accept, do a request, continue.
12812106Sralph 	 */
12912106Sralph 	sigset(SIGCHLD, reapchild);
13012106Sralph 	listen(f, 10);
13112106Sralph 	for (;;) {
13212430Sralph 		int s, len = sizeof(fromaddr);
13312106Sralph 
13412430Sralph 		s = accept(f, &fromaddr, &len, 0);
13512106Sralph 		if (s < 0) {
13612106Sralph 			if (errno == EINTR)
13712106Sralph 				continue;
13812106Sralph 			logerror("accept");
13912430Sralph 			exit(1);
14012106Sralph 		}
14112106Sralph 		if (fork() == 0) {
14212106Sralph 			sigset(SIGCHLD, SIG_IGN);
14312106Sralph 			(void) close(f);
14412430Sralph 			doit(s, &fromaddr);
14512106Sralph 			exit(0);
14612106Sralph 		}
14712106Sralph 		(void) close(s);
14812106Sralph 	}
14912106Sralph }
15012106Sralph 
151*12875Sralph static
15212106Sralph reapchild()
15312106Sralph {
15412106Sralph 	union wait status;
15512106Sralph 
15612106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
15712106Sralph 		;
15812106Sralph }
15912106Sralph 
16012106Sralph /*
16112106Sralph  * Stuff for handling job specifications
16212106Sralph  */
16312106Sralph char	*user[MAXUSERS];	/* users to process */
16412106Sralph int	users;			/* # of users in user array */
16512106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
16612106Sralph int	requests;		/* # of spool requests */
167*12875Sralph char	*person;		/* name of person doing lprm */
16812106Sralph 
169*12875Sralph static char	fromb[32];	/* buffer for client's machine name */
170*12875Sralph static char	cbuf[BUFSIZ];	/* command line buffer */
171*12875Sralph static char	*cmdnames[] = {
17212430Sralph 	"null",
17312430Sralph 	"printjob",
17412430Sralph 	"recvjob",
17512430Sralph 	"displayq short",
17612430Sralph 	"displayq long",
17712430Sralph 	"rmjob"
17812430Sralph };
17912106Sralph 
180*12875Sralph static
18112430Sralph doit(f, fromaddr)
18212106Sralph 	int f;
18312430Sralph 	struct sockaddr_in *fromaddr;
18412106Sralph {
18512106Sralph 	register char *cp;
18612106Sralph 	register struct hostent *hp;
18712106Sralph 	register int n;
18812106Sralph 	char c;
18912106Sralph 
19012106Sralph 	dup2(f, 1);
19112106Sralph 	(void) close(f);
19212106Sralph 	f = 1;
19312430Sralph 	fromaddr->sin_port = ntohs(fromaddr->sin_port);
19412430Sralph 	if (fromaddr->sin_family != AF_INET || fromaddr->sin_port >= IPPORT_RESERVED)
19512106Sralph 		fatal("Malformed from address");
19612430Sralph 	hp = gethostbyaddr(&fromaddr->sin_addr, sizeof(struct in_addr),
19712430Sralph 		fromaddr->sin_family);
19812106Sralph 	if (hp == 0)
19912106Sralph 		fatal("Host name for your address (%s) unknown",
20012430Sralph 			ntoa(fromaddr->sin_addr));
20112106Sralph 	strcpy(fromb, hp->h_name);
20212106Sralph 	from = fromb;
20312430Sralph 	if (chkhost())
20412430Sralph 		fatal("Your host does not have line printer access");
20512106Sralph 	for (;;) {
20612106Sralph 		cp = cbuf;
20712106Sralph 		do {
20812106Sralph 			if ((n = read(f, &c, 1)) != 1) {
20912106Sralph 				if (n < 0)
21012106Sralph 					fatal("Lost connection");
21112106Sralph 				return;
21212106Sralph 			}
21312106Sralph 			if (cp >= &cbuf[sizeof(cbuf)])
21412106Sralph 				fatal("Command line too long");
21512106Sralph 			*cp++ = c;
21612106Sralph 		} while (c != '\n');
21712106Sralph 		*--cp = '\0';
21812106Sralph 		cp = cbuf;
21912430Sralph 		if (lflag && *cp >= '\1' && *cp <= '\5') {
22012430Sralph 			printer = NULL;
22112430Sralph 			log("%s requests %s %s", from, cmdnames[*cp], cp+1);
22212430Sralph 		}
22312106Sralph 		switch (*cp++) {
22412106Sralph 		case '\1':	/* check the queue and print any jobs there */
22512106Sralph 			printer = cp;
22612106Sralph 			printjob();
22712106Sralph 			break;
22812106Sralph 		case '\2':	/* receive files to be queued */
22912106Sralph 			printer = cp;
23012106Sralph 			recvjob();
23112106Sralph 			break;
23212430Sralph 		case '\3':	/* display the queue (short form) */
23312430Sralph 		case '\4':	/* display the queue (long form) */
23412106Sralph 			printer = cp;
23512106Sralph 			while (*cp) {
23612106Sralph 				if (*cp != ' ') {
23712106Sralph 					cp++;
23812106Sralph 					continue;
23912106Sralph 				}
24012106Sralph 				*cp++ = '\0';
24112106Sralph 				while (isspace(*cp))
24212106Sralph 					cp++;
24312106Sralph 				if (*cp == '\0')
24412106Sralph 					break;
24512106Sralph 				if (isdigit(*cp)) {
24612106Sralph 					if (requests >= MAXREQUESTS)
24712106Sralph 						fatal("Too many requests");
24812106Sralph 					requ[requests++] = atoi(cp);
24912106Sralph 				} else {
25012106Sralph 					if (users >= MAXUSERS)
25112106Sralph 						fatal("Too many users");
25212106Sralph 					user[users++] = cp;
25312106Sralph 				}
25412106Sralph 			}
25512106Sralph 			displayq(cbuf[0] - '\3');
25612106Sralph 			exit(0);
25712106Sralph 		case '\5':	/* remove a job from the queue */
25812106Sralph 			printer = cp;
25912106Sralph 			while (*cp && *cp != ' ')
26012106Sralph 				cp++;
26112106Sralph 			if (!*cp)
26212106Sralph 				break;
26312106Sralph 			*cp++ = '\0';
26412106Sralph 			person = cp;
26512106Sralph 			while (*cp) {
26612106Sralph 				if (*cp != ' ') {
26712106Sralph 					cp++;
26812106Sralph 					continue;
26912106Sralph 				}
27012106Sralph 				*cp++ = '\0';
27112106Sralph 				while (isspace(*cp))
27212106Sralph 					cp++;
27312106Sralph 				if (*cp == '\0')
27412106Sralph 					break;
27512106Sralph 				if (isdigit(*cp)) {
27612106Sralph 					if (requests >= MAXREQUESTS)
27712106Sralph 						fatal("Too many requests");
27812106Sralph 					requ[requests++] = atoi(cp);
27912106Sralph 				} else {
28012106Sralph 					if (users >= MAXUSERS)
28112106Sralph 						fatal("Too many users");
28212106Sralph 					user[users++] = cp;
28312106Sralph 				}
28412106Sralph 			}
28512106Sralph 			rmjob();
28612106Sralph 			break;
28712106Sralph 		}
28812106Sralph 		fatal("Illegal service request");
28912106Sralph 		exit(1);
29012106Sralph 	}
29112106Sralph }
29212106Sralph 
29312106Sralph /*
29412430Sralph  * Make a pass through the printcap database and start printing any
29512430Sralph  * files left from the last time the machine went down.
29612430Sralph  */
297*12875Sralph static
29812430Sralph startup()
29912430Sralph {
30012430Sralph 	char buf[BUFSIZ];
30112430Sralph 	register char *cp;
30212430Sralph 	int pid;
30312430Sralph 
30412430Sralph 	printer = buf;
30512430Sralph 
30612430Sralph 	/*
30712430Sralph 	 * Restart the daemons.
30812430Sralph 	 */
30912430Sralph 	while (getprent(buf) > 0) {
31012430Sralph 		for (cp = buf; *cp; cp++)
31112430Sralph 			if (*cp == '|' || *cp == ':') {
31212430Sralph 				*cp = '\0';
31312430Sralph 				break;
31412430Sralph 			}
31512430Sralph 		if ((pid = fork()) < 0) {
31612430Sralph 			log("startup: cannot fork");
31712430Sralph 			exit(1);
31812430Sralph 		}
31912430Sralph 		if (!pid) {
32012430Sralph 			endprent();
32112430Sralph 			printjob();
32212430Sralph 		}
32312430Sralph 	}
32412430Sralph }
32512430Sralph 
32612430Sralph /*
32712430Sralph  * Check to see if the from host has access to the line printer.
32812430Sralph  */
329*12875Sralph static
33012430Sralph chkhost()
33112430Sralph {
33212430Sralph 	register FILE *hostf;
33312430Sralph 	register char *cp;
33412430Sralph 	char ahost[50];
33512430Sralph 
33612734Sralph 	if (!strcmp(from, host))
33712734Sralph 		return(0);
33812734Sralph 
33912430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
34012430Sralph 	while (fgets(ahost, sizeof(ahost), hostf)) {
34112430Sralph 		if (cp = index(ahost, '\n'))
34212430Sralph 			*cp = '\0';
34312430Sralph 		cp = index(ahost, ' ');
34412430Sralph 		if (!strcmp(from, ahost) && cp == NULL) {
34512430Sralph 			(void) fclose(hostf);
34612430Sralph 			return(0);
34712430Sralph 		}
34812430Sralph 	}
34912430Sralph 	(void) fclose(hostf);
35012430Sralph 	return(-1);
35112430Sralph }
35212430Sralph 
35312430Sralph /*
35412106Sralph  * Convert network-format internet address
35512106Sralph  * to base 256 d.d.d.d representation.
35612106Sralph  */
357*12875Sralph static char *
35812106Sralph ntoa(in)
35912106Sralph 	struct in_addr in;
36012106Sralph {
36112106Sralph 	static char b[18];
36212106Sralph 	register char *p;
36312106Sralph 
36412106Sralph 	p = (char *)&in;
36512106Sralph #define	UC(b)	(((int)b)&0xff)
36612106Sralph 	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
36712106Sralph 	return (b);
36812106Sralph }
36912106Sralph 
37012106Sralph /*VARARGS1*/
37112106Sralph log(msg, a1, a2, a3)
37212106Sralph 	char *msg;
37312106Sralph {
37412106Sralph 	short console = isatty(fileno(stderr));
37512106Sralph 
37612106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
37712106Sralph 	if (printer)
37812106Sralph 		fprintf(stderr, "%s: ", printer);
37912106Sralph 	fprintf(stderr, msg, a1, a2, a3);
38012106Sralph 	if (console)
38112106Sralph 		putc('\r', stderr);
38212106Sralph 	putc('\n', stderr);
38312106Sralph 	fflush(stderr);
38412106Sralph }
38512106Sralph 
386*12875Sralph static
38712106Sralph logerror(msg)
38812106Sralph 	char *msg;
38912106Sralph {
39012106Sralph 	register int err = errno;
39112106Sralph 	short console = isatty(fileno(stderr));
39212106Sralph 	extern int sys_nerr;
39312106Sralph 	extern char *sys_errlist[];
39412106Sralph 
39512106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
39612430Sralph 	if (msg)
39712106Sralph 		fprintf(stderr, "%s: ", msg);
39812106Sralph 	fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
39912106Sralph 	if (console)
40012106Sralph 		putc('\r', stderr);
40112106Sralph 	putc('\n', stderr);
40212106Sralph 	fflush(stderr);
40312106Sralph }
404