xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 34936)
122429Sdist /*
222429Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
534203Sbostic  * Redistribution and use in source and binary forms are permitted
6*34936Sbostic  * provided that the above copyright notice and this paragraph are
7*34936Sbostic  * duplicated in all such forms and that any documentation,
8*34936Sbostic  * advertising materials, and other materials related to such
9*34936Sbostic  * distribution and use acknowledge that the software was developed
10*34936Sbostic  * by the University of California, Berkeley.  The name of the
11*34936Sbostic  * University may not be used to endorse or promote products derived
12*34936Sbostic  * from this software without specific prior written permission.
13*34936Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34936Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34936Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622429Sdist  */
1722429Sdist 
1813957Ssam #ifndef lint
1922429Sdist char copyright[] =
2022429Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
2122429Sdist  All rights reserved.\n";
2234203Sbostic #endif /* not lint */
2313957Ssam 
2422429Sdist #ifndef lint
25*34936Sbostic static char sccsid[] = "@(#)lpd.c	5.6 (Berkeley) 06/30/88";
2634203Sbostic #endif /* not lint */
2722429Sdist 
2812106Sralph /*
2912106Sralph  * lpd -- line printer daemon.
3012106Sralph  *
3112106Sralph  * Listen for a connection and perform the requested operation.
3212106Sralph  * Operations are:
3312106Sralph  *	\1printer\n
3412106Sralph  *		check the queue for jobs and print any found.
3512106Sralph  *	\2printer\n
3612106Sralph  *		receive a job from another machine and queue it.
3712106Sralph  *	\3printer [users ...] [jobs ...]\n
3812106Sralph  *		return the current state of the queue (short form).
3912106Sralph  *	\4printer [users ...] [jobs ...]\n
4012106Sralph  *		return the current state of the queue (long form).
4112106Sralph  *	\5printer person [users ...] [jobs ...]\n
4212106Sralph  *		remove jobs from the queue.
4312106Sralph  *
4412106Sralph  * Strategy to maintain protected spooling area:
4512106Sralph  *	1. Spooling area is writable only by daemon and spooling group
4612106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
4712106Sralph  *	   root to access any file it wants (verifying things before
4812106Sralph  *	   with an access call) and group id to know how it should
4912106Sralph  *	   set up ownership of files in the spooling area.
5012430Sralph  *	3. Files in spooling area are owned by root, group spooling
5112106Sralph  *	   group, with mode 660.
5212106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
5312106Sralph  *	   access files and printer.  Users can't get to anything
5412106Sralph  *	   w/o help of lpq and lprm programs.
5512106Sralph  */
5612106Sralph 
5712106Sralph #include "lp.h"
5812106Sralph 
5916761Sralph int	lflag;				/* log requests flag */
6012875Sralph 
6112106Sralph int	reapchild();
6216761Sralph int	mcleanup();
6312106Sralph 
6412106Sralph main(argc, argv)
6512106Sralph 	int argc;
6612106Sralph 	char **argv;
6712106Sralph {
6813816Ssam 	int f, funix, finet, options, defreadfds, fromlen;
6913816Ssam 	struct sockaddr_un sun, fromunix;
7013816Ssam 	struct sockaddr_in sin, frominet;
7114837Sralph 	int omask, lfd;
7212106Sralph 
7312106Sralph 	gethostname(host, sizeof(host));
7412106Sralph 	name = argv[0];
7512106Sralph 
7612106Sralph 	while (--argc > 0) {
7712106Sralph 		argv++;
7812106Sralph 		if (argv[0][0] == '-')
7912106Sralph 			switch (argv[0][1]) {
8012106Sralph 			case 'd':
8112106Sralph 				options |= SO_DEBUG;
8212106Sralph 				break;
8312106Sralph 			case 'l':
8412430Sralph 				lflag++;
8512430Sralph 				break;
8612106Sralph 			}
8712106Sralph 	}
8814837Sralph 
8912106Sralph #ifndef DEBUG
9012106Sralph 	/*
9112106Sralph 	 * Set up standard environment by detaching from the parent.
9212106Sralph 	 */
9312106Sralph 	if (fork())
9412106Sralph 		exit(0);
9516761Sralph 	for (f = 0; f < 5; f++)
9612106Sralph 		(void) close(f);
9713147Ssam 	(void) open("/dev/null", O_RDONLY);
9813147Ssam 	(void) open("/dev/null", O_WRONLY);
9916761Sralph 	(void) dup(1);
10013147Ssam 	f = open("/dev/tty", O_RDWR);
10112106Sralph 	if (f > 0) {
10212106Sralph 		ioctl(f, TIOCNOTTY, 0);
10312106Sralph 		(void) close(f);
10412106Sralph 	}
10512106Sralph #endif
10614837Sralph 
10725494Seric 	openlog("lpd", LOG_PID, LOG_LPR);
10812106Sralph 	(void) umask(0);
10914837Sralph 	lfd = open(MASTERLOCK, O_WRONLY|O_CREAT, 0644);
11014837Sralph 	if (lfd < 0) {
11116761Sralph 		syslog(LOG_ERR, "%s: %m", MASTERLOCK);
11214837Sralph 		exit(1);
11314837Sralph 	}
11414837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
11514837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
11614837Sralph 			exit(0);
11716761Sralph 		syslog(LOG_ERR, "%s: %m", MASTERLOCK);
11814837Sralph 		exit(1);
11914837Sralph 	}
12014837Sralph 	ftruncate(lfd, 0);
12112875Sralph 	/*
12214837Sralph 	 * write process id for others to know
12314837Sralph 	 */
12414837Sralph 	sprintf(line, "%u\n", getpid());
12514837Sralph 	f = strlen(line);
12614837Sralph 	if (write(lfd, line, f) != f) {
12716761Sralph 		syslog(LOG_ERR, "%s: %m", MASTERLOCK);
12814837Sralph 		exit(1);
12914837Sralph 	}
13014837Sralph 	signal(SIGCHLD, reapchild);
13114837Sralph 	/*
13212875Sralph 	 * Restart all the printers.
13312875Sralph 	 */
13412875Sralph 	startup();
13514837Sralph 	(void) unlink(SOCKETNAME);
13613816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
13713816Ssam 	if (funix < 0) {
13816761Sralph 		syslog(LOG_ERR, "socket: %m");
13912106Sralph 		exit(1);
14012106Sralph 	}
14114149Sralph #define	mask(s)	(1 << ((s) - 1))
14214149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
14316761Sralph 	signal(SIGHUP, mcleanup);
14416761Sralph 	signal(SIGINT, mcleanup);
14516761Sralph 	signal(SIGQUIT, mcleanup);
14616761Sralph 	signal(SIGTERM, mcleanup);
14713816Ssam 	sun.sun_family = AF_UNIX;
14813816Ssam 	strcpy(sun.sun_path, SOCKETNAME);
14913816Ssam 	if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
15016761Sralph 		syslog(LOG_ERR, "ubind: %m");
15112106Sralph 		exit(1);
15212106Sralph 	}
15313816Ssam 	sigsetmask(omask);
15414149Sralph 	defreadfds = 1 << funix;
15514149Sralph 	listen(funix, 5);
15613816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
15713816Ssam 	if (finet >= 0) {
15813816Ssam 		struct servent *sp;
15913816Ssam 
16013816Ssam 		if (options & SO_DEBUG)
16113816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
16216761Sralph 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
16316761Sralph 				mcleanup();
16413816Ssam 			}
16513816Ssam 		sp = getservbyname("printer", "tcp");
16613816Ssam 		if (sp == NULL) {
16716761Sralph 			syslog(LOG_ERR, "printer/tcp: unknown service");
16816761Sralph 			mcleanup();
16913816Ssam 		}
17013816Ssam 		sin.sin_family = AF_INET;
17113816Ssam 		sin.sin_port = sp->s_port;
17213816Ssam 		if (bind(finet, &sin, sizeof(sin), 0) < 0) {
17316761Sralph 			syslog(LOG_ERR, "bind: %m");
17416761Sralph 			mcleanup();
17513816Ssam 		}
17614149Sralph 		defreadfds |= 1 << finet;
17714149Sralph 		listen(finet, 5);
17813816Ssam 	}
17912106Sralph 	/*
18014149Sralph 	 * Main loop: accept, do a request, continue.
18112106Sralph 	 */
18212106Sralph 	for (;;) {
18313957Ssam 		int domain, nfds, s, readfds = defreadfds;
18412106Sralph 
18513957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
18613957Ssam 		if (nfds <= 0) {
18716761Sralph 			if (nfds < 0 && errno != EINTR)
18816761Sralph 				syslog(LOG_WARNING, "select: %m");
18913957Ssam 			continue;
19013957Ssam 		}
19113816Ssam 		if (readfds & (1 << funix)) {
19213957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
19313816Ssam 			s = accept(funix, &fromunix, &fromlen);
19413957Ssam 		} else if (readfds & (1 << finet)) {
19513957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
19613816Ssam 			s = accept(finet, &frominet, &fromlen);
19713816Ssam 		}
19812106Sralph 		if (s < 0) {
19916761Sralph 			if (errno != EINTR)
20016761Sralph 				syslog(LOG_WARNING, "accept: %m");
20116761Sralph 			continue;
20212106Sralph 		}
20312106Sralph 		if (fork() == 0) {
20413147Ssam 			signal(SIGCHLD, SIG_IGN);
20514708Sralph 			signal(SIGHUP, SIG_IGN);
20614708Sralph 			signal(SIGINT, SIG_IGN);
20714708Sralph 			signal(SIGQUIT, SIG_IGN);
20814708Sralph 			signal(SIGTERM, SIG_IGN);
20913816Ssam 			(void) close(funix);
21013816Ssam 			(void) close(finet);
21113816Ssam 			dup2(s, 1);
21213816Ssam 			(void) close(s);
21313816Ssam 			if (domain == AF_INET)
21413816Ssam 				chkhost(&frominet);
21513816Ssam 			doit();
21612106Sralph 			exit(0);
21712106Sralph 		}
21812106Sralph 		(void) close(s);
21912106Sralph 	}
22012106Sralph }
22112106Sralph 
22212106Sralph reapchild()
22312106Sralph {
22412106Sralph 	union wait status;
22512106Sralph 
22612106Sralph 	while (wait3(&status, WNOHANG, 0) > 0)
22712106Sralph 		;
22812106Sralph }
22912106Sralph 
23016761Sralph mcleanup()
23113816Ssam {
23214149Sralph 	if (lflag)
23316761Sralph 		syslog(LOG_INFO, "exiting");
23413816Ssam 	unlink(SOCKETNAME);
23513816Ssam 	exit(0);
23613816Ssam }
23713816Ssam 
23812106Sralph /*
23912106Sralph  * Stuff for handling job specifications
24012106Sralph  */
24112106Sralph char	*user[MAXUSERS];	/* users to process */
24212106Sralph int	users;			/* # of users in user array */
24312106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
24412106Sralph int	requests;		/* # of spool requests */
24512875Sralph char	*person;		/* name of person doing lprm */
24612106Sralph 
24716761Sralph char	fromb[32];	/* buffer for client's machine name */
24816761Sralph char	cbuf[BUFSIZ];	/* command line buffer */
24916761Sralph char	*cmdnames[] = {
25012430Sralph 	"null",
25112430Sralph 	"printjob",
25212430Sralph 	"recvjob",
25312430Sralph 	"displayq short",
25412430Sralph 	"displayq long",
25512430Sralph 	"rmjob"
25612430Sralph };
25712106Sralph 
25813816Ssam doit()
25912106Sralph {
26012106Sralph 	register char *cp;
26112106Sralph 	register int n;
26212106Sralph 
26312106Sralph 	for (;;) {
26412106Sralph 		cp = cbuf;
26512106Sralph 		do {
26613816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
26713816Ssam 				fatal("Command line too long");
26813816Ssam 			if ((n = read(1, cp, 1)) != 1) {
26912106Sralph 				if (n < 0)
27012106Sralph 					fatal("Lost connection");
27112106Sralph 				return;
27212106Sralph 			}
27313816Ssam 		} while (*cp++ != '\n');
27412106Sralph 		*--cp = '\0';
27512106Sralph 		cp = cbuf;
27616761Sralph 		if (lflag) {
27716761Sralph 			if (*cp >= '\1' && *cp <= '\5')
27816761Sralph 				syslog(LOG_INFO, "%s requests %s %s",
27916761Sralph 					from, cmdnames[*cp], cp+1);
28016761Sralph 			else
28116761Sralph 				syslog(LOG_INFO, "bad request (%d) from %s",
28216761Sralph 					*cp, from);
28312430Sralph 		}
28412106Sralph 		switch (*cp++) {
28512106Sralph 		case '\1':	/* check the queue and print any jobs there */
28612106Sralph 			printer = cp;
28712106Sralph 			printjob();
28812106Sralph 			break;
28912106Sralph 		case '\2':	/* receive files to be queued */
29012106Sralph 			printer = cp;
29112106Sralph 			recvjob();
29212106Sralph 			break;
29312430Sralph 		case '\3':	/* display the queue (short form) */
29412430Sralph 		case '\4':	/* display the queue (long form) */
29512106Sralph 			printer = cp;
29612106Sralph 			while (*cp) {
29712106Sralph 				if (*cp != ' ') {
29812106Sralph 					cp++;
29912106Sralph 					continue;
30012106Sralph 				}
30112106Sralph 				*cp++ = '\0';
30212106Sralph 				while (isspace(*cp))
30312106Sralph 					cp++;
30412106Sralph 				if (*cp == '\0')
30512106Sralph 					break;
30612106Sralph 				if (isdigit(*cp)) {
30712106Sralph 					if (requests >= MAXREQUESTS)
30812106Sralph 						fatal("Too many requests");
30912106Sralph 					requ[requests++] = atoi(cp);
31012106Sralph 				} else {
31112106Sralph 					if (users >= MAXUSERS)
31212106Sralph 						fatal("Too many users");
31312106Sralph 					user[users++] = cp;
31412106Sralph 				}
31512106Sralph 			}
31612106Sralph 			displayq(cbuf[0] - '\3');
31712106Sralph 			exit(0);
31812106Sralph 		case '\5':	/* remove a job from the queue */
31912106Sralph 			printer = cp;
32012106Sralph 			while (*cp && *cp != ' ')
32112106Sralph 				cp++;
32212106Sralph 			if (!*cp)
32312106Sralph 				break;
32412106Sralph 			*cp++ = '\0';
32512106Sralph 			person = cp;
32612106Sralph 			while (*cp) {
32712106Sralph 				if (*cp != ' ') {
32812106Sralph 					cp++;
32912106Sralph 					continue;
33012106Sralph 				}
33112106Sralph 				*cp++ = '\0';
33212106Sralph 				while (isspace(*cp))
33312106Sralph 					cp++;
33412106Sralph 				if (*cp == '\0')
33512106Sralph 					break;
33612106Sralph 				if (isdigit(*cp)) {
33712106Sralph 					if (requests >= MAXREQUESTS)
33812106Sralph 						fatal("Too many requests");
33912106Sralph 					requ[requests++] = atoi(cp);
34012106Sralph 				} else {
34112106Sralph 					if (users >= MAXUSERS)
34212106Sralph 						fatal("Too many users");
34312106Sralph 					user[users++] = cp;
34412106Sralph 				}
34512106Sralph 			}
34612106Sralph 			rmjob();
34712106Sralph 			break;
34812106Sralph 		}
34912106Sralph 		fatal("Illegal service request");
35012106Sralph 	}
35112106Sralph }
35212106Sralph 
35312106Sralph /*
35412430Sralph  * Make a pass through the printcap database and start printing any
35512430Sralph  * files left from the last time the machine went down.
35612430Sralph  */
35712430Sralph startup()
35812430Sralph {
35912430Sralph 	char buf[BUFSIZ];
36012430Sralph 	register char *cp;
36112430Sralph 	int pid;
36212430Sralph 
36312430Sralph 	printer = buf;
36412430Sralph 
36512430Sralph 	/*
36612430Sralph 	 * Restart the daemons.
36712430Sralph 	 */
36812430Sralph 	while (getprent(buf) > 0) {
36912430Sralph 		for (cp = buf; *cp; cp++)
37012430Sralph 			if (*cp == '|' || *cp == ':') {
37112430Sralph 				*cp = '\0';
37212430Sralph 				break;
37312430Sralph 			}
37412430Sralph 		if ((pid = fork()) < 0) {
37516761Sralph 			syslog(LOG_WARNING, "startup: cannot fork");
37616761Sralph 			mcleanup();
37712430Sralph 		}
37812430Sralph 		if (!pid) {
37912430Sralph 			endprent();
38012430Sralph 			printjob();
38112430Sralph 		}
38212430Sralph 	}
38312430Sralph }
38412430Sralph 
38527746Sbloom #define DUMMY ":nobody::"
38627746Sbloom 
38712430Sralph /*
38812430Sralph  * Check to see if the from host has access to the line printer.
38912430Sralph  */
39013816Ssam chkhost(f)
39113816Ssam 	struct sockaddr_in *f;
39212430Sralph {
39313816Ssam 	register struct hostent *hp;
39412430Sralph 	register FILE *hostf;
39527746Sbloom 	register char *cp, *sp;
39612430Sralph 	char ahost[50];
39715551Sralph 	int first = 1;
39813816Ssam 	extern char *inet_ntoa();
39927746Sbloom 	int baselen = -1;
40012430Sralph 
40113816Ssam 	f->sin_port = ntohs(f->sin_port);
40213816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
40313816Ssam 		fatal("Malformed from address");
40413816Ssam 	hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
40513816Ssam 	if (hp == 0)
40613816Ssam 		fatal("Host name for your address (%s) unknown",
40713816Ssam 			inet_ntoa(f->sin_addr));
40813816Ssam 
40913816Ssam 	strcpy(fromb, hp->h_name);
41013816Ssam 	from = fromb;
41112734Sralph 	if (!strcmp(from, host))
41213816Ssam 		return;
41312734Sralph 
41427746Sbloom 	sp = fromb;
41527746Sbloom 	cp = ahost;
41627746Sbloom 	while (*sp) {
41727746Sbloom 		if (*sp == '.') {
41827746Sbloom 			if (baselen == -1)
41927746Sbloom 				baselen = sp - fromb;
42027746Sbloom 			*cp++ = *sp++;
42127746Sbloom 		} else {
42227746Sbloom 			*cp++ = isupper(*sp) ? tolower(*sp++) : *sp++;
42327746Sbloom 		}
42427746Sbloom 	}
42527746Sbloom 	*cp = '\0';
42612430Sralph 	hostf = fopen("/etc/hosts.equiv", "r");
42715551Sralph again:
42815551Sralph 	if (hostf) {
42927746Sbloom 		if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) {
43027746Sbloom 			(void) fclose(hostf);
43127746Sbloom 			return;
43212430Sralph 		}
43315551Sralph 		(void) fclose(hostf);
43412430Sralph 	}
43515551Sralph 	if (first == 1) {
43615551Sralph 		first = 0;
43715551Sralph 		hostf = fopen("/etc/hosts.lpd", "r");
43815551Sralph 		goto again;
43915551Sralph 	}
44013816Ssam 	fatal("Your host does not have line printer access");
44112430Sralph }
442