xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 12734)
1*12734Sralph /*	lpd.c	4.3	83/05/26	*/
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 
3912430Sralph int	lflag;					/* log requests flag */
4012106Sralph char	*logfile = DEFLOGF;
4112106Sralph struct	sockaddr_in sin = { AF_INET };
4212106Sralph int	reapchild();
4312106Sralph char	*ntoa();
4412106Sralph 
4512106Sralph main(argc, argv)
4612106Sralph 	int argc;
4712106Sralph 	char **argv;
4812106Sralph {
4912106Sralph 	int f, options;
5012430Sralph 	struct sockaddr_in fromaddr;
5112106Sralph 	struct servent *sp;
5212106Sralph 
5312106Sralph 	gethostname(host, sizeof(host));
5412106Sralph 	name = argv[0];
5512106Sralph 
5612106Sralph 	sp = getservbyname("printer", "tcp");
5712106Sralph 	if (sp == NULL) {
5812430Sralph 		fprintf(stderr, "printer/tcp: unknown service");
5912106Sralph 		exit(1);
6012106Sralph 	}
6112106Sralph 	sin.sin_port = sp->s_port;
6212106Sralph 
6312106Sralph 	while (--argc > 0) {
6412106Sralph 		argv++;
6512106Sralph 		if (argv[0][0] == '-')
6612106Sralph 			switch (argv[0][1]) {
6712106Sralph 			case 'd':
6812106Sralph 				options |= SO_DEBUG;
6912106Sralph 				break;
7012106Sralph 			case 'l':
7112430Sralph 				lflag++;
7212430Sralph 				break;
7312430Sralph 			case 'L':
7412106Sralph 				argc--;
7512106Sralph 				logfile = *++argv;
7612106Sralph 				break;
7712106Sralph 			}
7812106Sralph 		else {
7912106Sralph 			int port = atoi(argv[0]);
8012430Sralph 			int c = argv[0][0];
8112106Sralph 
8212430Sralph 			if (c < '0' || c > '9' || port < 0) {
8312430Sralph 				fprintf(stderr, "lpd: %s: bad port number\n", argv[0]);
8412106Sralph 				exit(1);
8512106Sralph 			}
8612106Sralph 			sin.sin_port = htons((u_short) port);
8712106Sralph 		}
8812106Sralph 	}
8912106Sralph #ifndef DEBUG
9012106Sralph 	/*
9112106Sralph 	 * Set up standard environment by detaching from the parent.
9212106Sralph 	 */
9312106Sralph 	if (fork())
9412106Sralph 		exit(0);
9512106Sralph 	for (f = 0; f < 3; f++)
9612106Sralph 		(void) close(f);
9712106Sralph 	(void) open("/dev/null", FRDONLY, 0);
9812106Sralph 	(void) open("/dev/null", FWRONLY, 0);
9912106Sralph 	(void) open(logfile, FWRONLY|FAPPEND, 0);
10012106Sralph 	f = open("/dev/tty", FRDWR, 0);
10112106Sralph 	if (f > 0) {
10212106Sralph 		ioctl(f, TIOCNOTTY, 0);
10312106Sralph 		(void) close(f);
10412106Sralph 	}
10512106Sralph #endif
10612106Sralph 	(void) umask(0);
10712106Sralph 	f = socket(AF_INET, SOCK_STREAM, 0);
10812106Sralph 	if (f < 0) {
10912106Sralph 		logerror("socket");
11012106Sralph 		exit(1);
11112106Sralph 	}
11212106Sralph 	if (options & SO_DEBUG)
11312430Sralph 		if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
11412106Sralph 			logerror("setsockopt (SO_DEBUG)");
11512430Sralph 			exit(1);
11612430Sralph 		}
11712106Sralph 	if (bind(f, &sin, sizeof(sin), 0) < 0) {
11812106Sralph 		logerror("bind");
11912106Sralph 		exit(1);
12012106Sralph 	}
12112106Sralph 	/*
12212106Sralph 	 * Restart all the printers and tell everyone that we are
12312106Sralph 	 * up and running.
12412106Sralph 	 */
12512106Sralph 	startup();
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 
15112106Sralph reapchild()
15212106Sralph {
15312106Sralph 	union wait status;
15412106Sralph 
15512106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
15612106Sralph 		;
15712106Sralph }
15812106Sralph 
15912106Sralph /*
16012106Sralph  * Stuff for handling job specifications
16112106Sralph  */
16212106Sralph char	*user[MAXUSERS];	/* users to process */
16312106Sralph int	users;			/* # of users in user array */
16412106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
16512106Sralph int	requests;		/* # of spool requests */
16612106Sralph 
16712430Sralph char	fromb[32];		/* buffer for client's machine name */
16812106Sralph char	cbuf[BUFSIZ];		/* command line buffer */
16912430Sralph char	*cmdnames[] = {
17012430Sralph 	"null",
17112430Sralph 	"printjob",
17212430Sralph 	"recvjob",
17312430Sralph 	"displayq short",
17412430Sralph 	"displayq long",
17512430Sralph 	"rmjob"
17612430Sralph };
17712106Sralph 
17812430Sralph doit(f, fromaddr)
17912106Sralph 	int f;
18012430Sralph 	struct sockaddr_in *fromaddr;
18112106Sralph {
18212106Sralph 	register char *cp;
18312106Sralph 	register struct hostent *hp;
18412106Sralph 	register int n;
18512106Sralph 	extern char *person;
18612106Sralph 	char c;
18712106Sralph 
18812106Sralph 	dup2(f, 1);
18912106Sralph 	(void) close(f);
19012106Sralph 	f = 1;
19112430Sralph 	fromaddr->sin_port = ntohs(fromaddr->sin_port);
19212430Sralph 	if (fromaddr->sin_family != AF_INET || fromaddr->sin_port >= IPPORT_RESERVED)
19312106Sralph 		fatal("Malformed from address");
19412430Sralph 	hp = gethostbyaddr(&fromaddr->sin_addr, sizeof(struct in_addr),
19512430Sralph 		fromaddr->sin_family);
19612106Sralph 	if (hp == 0)
19712106Sralph 		fatal("Host name for your address (%s) unknown",
19812430Sralph 			ntoa(fromaddr->sin_addr));
19912106Sralph 	strcpy(fromb, hp->h_name);
20012106Sralph 	from = fromb;
20112430Sralph 	if (chkhost())
20212430Sralph 		fatal("Your host does not have line printer access");
20312106Sralph 	for (;;) {
20412106Sralph 		cp = cbuf;
20512106Sralph 		do {
20612106Sralph 			if ((n = read(f, &c, 1)) != 1) {
20712106Sralph 				if (n < 0)
20812106Sralph 					fatal("Lost connection");
20912106Sralph 				return;
21012106Sralph 			}
21112106Sralph 			if (cp >= &cbuf[sizeof(cbuf)])
21212106Sralph 				fatal("Command line too long");
21312106Sralph 			*cp++ = c;
21412106Sralph 		} while (c != '\n');
21512106Sralph 		*--cp = '\0';
21612106Sralph 		cp = cbuf;
21712430Sralph 		if (lflag && *cp >= '\1' && *cp <= '\5') {
21812430Sralph 			printer = NULL;
21912430Sralph 			log("%s requests %s %s", from, cmdnames[*cp], cp+1);
22012430Sralph 		}
22112106Sralph 		switch (*cp++) {
22212106Sralph 		case '\1':	/* check the queue and print any jobs there */
22312106Sralph 			printer = cp;
22412106Sralph 			printjob();
22512106Sralph 			break;
22612106Sralph 		case '\2':	/* receive files to be queued */
22712106Sralph 			printer = cp;
22812106Sralph 			recvjob();
22912106Sralph 			break;
23012430Sralph 		case '\3':	/* display the queue (short form) */
23112430Sralph 		case '\4':	/* display the queue (long form) */
23212106Sralph 			printer = cp;
23312106Sralph 			while (*cp) {
23412106Sralph 				if (*cp != ' ') {
23512106Sralph 					cp++;
23612106Sralph 					continue;
23712106Sralph 				}
23812106Sralph 				*cp++ = '\0';
23912106Sralph 				while (isspace(*cp))
24012106Sralph 					cp++;
24112106Sralph 				if (*cp == '\0')
24212106Sralph 					break;
24312106Sralph 				if (isdigit(*cp)) {
24412106Sralph 					if (requests >= MAXREQUESTS)
24512106Sralph 						fatal("Too many requests");
24612106Sralph 					requ[requests++] = atoi(cp);
24712106Sralph 				} else {
24812106Sralph 					if (users >= MAXUSERS)
24912106Sralph 						fatal("Too many users");
25012106Sralph 					user[users++] = cp;
25112106Sralph 				}
25212106Sralph 			}
25312106Sralph 			displayq(cbuf[0] - '\3');
25412106Sralph 			exit(0);
25512106Sralph 		case '\5':	/* remove a job from the queue */
25612106Sralph 			printer = cp;
25712106Sralph 			while (*cp && *cp != ' ')
25812106Sralph 				cp++;
25912106Sralph 			if (!*cp)
26012106Sralph 				break;
26112106Sralph 			*cp++ = '\0';
26212106Sralph 			person = cp;
26312106Sralph 			while (*cp) {
26412106Sralph 				if (*cp != ' ') {
26512106Sralph 					cp++;
26612106Sralph 					continue;
26712106Sralph 				}
26812106Sralph 				*cp++ = '\0';
26912106Sralph 				while (isspace(*cp))
27012106Sralph 					cp++;
27112106Sralph 				if (*cp == '\0')
27212106Sralph 					break;
27312106Sralph 				if (isdigit(*cp)) {
27412106Sralph 					if (requests >= MAXREQUESTS)
27512106Sralph 						fatal("Too many requests");
27612106Sralph 					requ[requests++] = atoi(cp);
27712106Sralph 				} else {
27812106Sralph 					if (users >= MAXUSERS)
27912106Sralph 						fatal("Too many users");
28012106Sralph 					user[users++] = cp;
28112106Sralph 				}
28212106Sralph 			}
28312106Sralph 			rmjob();
28412106Sralph 			break;
28512106Sralph 		}
28612106Sralph 		fatal("Illegal service request");
28712106Sralph 		exit(1);
28812106Sralph 	}
28912106Sralph }
29012106Sralph 
29112106Sralph /*
29212430Sralph  * Make a pass through the printcap database and start printing any
29312430Sralph  * files left from the last time the machine went down.
29412430Sralph  */
29512430Sralph startup()
29612430Sralph {
29712430Sralph 	char buf[BUFSIZ];
29812430Sralph 	register char *cp;
29912430Sralph 	int pid;
30012430Sralph 
30112430Sralph 	printer = buf;
30212430Sralph 
30312430Sralph 	/*
30412430Sralph 	 * Restart the daemons.
30512430Sralph 	 */
30612430Sralph 	while (getprent(buf) > 0) {
30712430Sralph 		for (cp = buf; *cp; cp++)
30812430Sralph 			if (*cp == '|' || *cp == ':') {
30912430Sralph 				*cp = '\0';
31012430Sralph 				break;
31112430Sralph 			}
31212430Sralph 		if ((pid = fork()) < 0) {
31312430Sralph 			log("startup: cannot fork");
31412430Sralph 			exit(1);
31512430Sralph 		}
31612430Sralph 		if (!pid) {
31712430Sralph 			endprent();
31812430Sralph 			printjob();
31912430Sralph 		}
32012430Sralph 	}
32112430Sralph }
32212430Sralph 
32312430Sralph /*
32412430Sralph  * Check to see if the from host has access to the line printer.
32512430Sralph  */
32612430Sralph chkhost()
32712430Sralph {
32812430Sralph 	register FILE *hostf;
32912430Sralph 	register char *cp;
33012430Sralph 	char ahost[50];
33112430Sralph 
332*12734Sralph 	if (!strcmp(from, host))
333*12734Sralph 		return(0);
334*12734Sralph 
33512430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
33612430Sralph 	while (fgets(ahost, sizeof(ahost), hostf)) {
33712430Sralph 		if (cp = index(ahost, '\n'))
33812430Sralph 			*cp = '\0';
33912430Sralph 		cp = index(ahost, ' ');
34012430Sralph 		if (!strcmp(from, ahost) && cp == NULL) {
34112430Sralph 			(void) fclose(hostf);
34212430Sralph 			return(0);
34312430Sralph 		}
34412430Sralph 	}
34512430Sralph 	(void) fclose(hostf);
34612430Sralph 	return(-1);
34712430Sralph }
34812430Sralph 
34912430Sralph /*
35012106Sralph  * Convert network-format internet address
35112106Sralph  * to base 256 d.d.d.d representation.
35212106Sralph  */
35312106Sralph char *
35412106Sralph ntoa(in)
35512106Sralph 	struct in_addr in;
35612106Sralph {
35712106Sralph 	static char b[18];
35812106Sralph 	register char *p;
35912106Sralph 
36012106Sralph 	p = (char *)&in;
36112106Sralph #define	UC(b)	(((int)b)&0xff)
36212106Sralph 	sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3]));
36312106Sralph 	return (b);
36412106Sralph }
36512106Sralph 
36612106Sralph /*VARARGS1*/
36712106Sralph log(msg, a1, a2, a3)
36812106Sralph 	char *msg;
36912106Sralph {
37012106Sralph 	short console = isatty(fileno(stderr));
37112106Sralph 
37212106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
37312106Sralph 	if (printer)
37412106Sralph 		fprintf(stderr, "%s: ", printer);
37512106Sralph 	fprintf(stderr, msg, a1, a2, a3);
37612106Sralph 	if (console)
37712106Sralph 		putc('\r', stderr);
37812106Sralph 	putc('\n', stderr);
37912106Sralph 	fflush(stderr);
38012106Sralph }
38112106Sralph 
38212106Sralph logerror(msg)
38312106Sralph 	char *msg;
38412106Sralph {
38512106Sralph 	register int err = errno;
38612106Sralph 	short console = isatty(fileno(stderr));
38712106Sralph 	extern int sys_nerr;
38812106Sralph 	extern char *sys_errlist[];
38912106Sralph 
39012106Sralph 	fprintf(stderr, console ? "\r\n%s: " : "%s: ", name);
39112430Sralph 	if (msg)
39212106Sralph 		fprintf(stderr, "%s: ", msg);
39312106Sralph 	fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr);
39412106Sralph 	if (console)
39512106Sralph 		putc('\r', stderr);
39612106Sralph 	putc('\n', stderr);
39712106Sralph 	fflush(stderr);
39812106Sralph }
399