xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 55474)
122429Sdist /*
222429Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
542802Sbostic  * %sccs.include.redist.c%
622429Sdist  */
722429Sdist 
813957Ssam #ifndef lint
922429Sdist char copyright[] =
1022429Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1122429Sdist  All rights reserved.\n";
1234203Sbostic #endif /* not lint */
1313957Ssam 
1422429Sdist #ifndef lint
15*55474Sbostic static char sccsid[] = "@(#)lpd.c	5.15 (Berkeley) 07/21/92";
1634203Sbostic #endif /* not lint */
1722429Sdist 
1812106Sralph /*
1912106Sralph  * lpd -- line printer daemon.
2012106Sralph  *
2112106Sralph  * Listen for a connection and perform the requested operation.
2212106Sralph  * Operations are:
2312106Sralph  *	\1printer\n
2412106Sralph  *		check the queue for jobs and print any found.
2512106Sralph  *	\2printer\n
2612106Sralph  *		receive a job from another machine and queue it.
2712106Sralph  *	\3printer [users ...] [jobs ...]\n
2812106Sralph  *		return the current state of the queue (short form).
2912106Sralph  *	\4printer [users ...] [jobs ...]\n
3012106Sralph  *		return the current state of the queue (long form).
3112106Sralph  *	\5printer person [users ...] [jobs ...]\n
3212106Sralph  *		remove jobs from the queue.
3312106Sralph  *
3412106Sralph  * Strategy to maintain protected spooling area:
3512106Sralph  *	1. Spooling area is writable only by daemon and spooling group
3612106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
3712106Sralph  *	   root to access any file it wants (verifying things before
3812106Sralph  *	   with an access call) and group id to know how it should
3912106Sralph  *	   set up ownership of files in the spooling area.
4012430Sralph  *	3. Files in spooling area are owned by root, group spooling
4112106Sralph  *	   group, with mode 660.
4212106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
4312106Sralph  *	   access files and printer.  Users can't get to anything
4412106Sralph  *	   w/o help of lpq and lprm programs.
4512106Sralph  */
4612106Sralph 
47*55474Sbostic #include <sys/param.h>
48*55474Sbostic #include <sys/wait.h>
49*55474Sbostic 
50*55474Sbostic #include <sys/socket.h>
51*55474Sbostic #include <sys/un.h>
52*55474Sbostic #include <netinet/in.h>
53*55474Sbostic #include <netdb.h>
54*55474Sbostic 
55*55474Sbostic #include <syslog.h>
56*55474Sbostic #include <signal.h>
57*55474Sbostic #include <errno.h>
58*55474Sbostic #include <fcntl.h>
59*55474Sbostic #include <dirent.h>
60*55474Sbostic #include <stdio.h>
6112106Sralph #include "lp.h"
62*55474Sbostic #include "lp.local.h"
6337968Sbostic #include "pathnames.h"
64*55474Sbostic #include "extern.h"
6512106Sralph 
6616761Sralph int	lflag;				/* log requests flag */
6747055Smckusick int	from_remote;			/* from remote socket */
6812875Sralph 
69*55474Sbostic static void       reapchild __P((int));
70*55474Sbostic static void       mcleanup __P((int));
71*55474Sbostic static void       doit __P((void));
72*55474Sbostic static void       startup __P((void));
73*55474Sbostic static void       chkhost __P((struct sockaddr_in *));
7412106Sralph 
75*55474Sbostic int
7612106Sralph main(argc, argv)
7712106Sralph 	int argc;
7812106Sralph 	char **argv;
7912106Sralph {
8045344Skarels 	int f, funix, finet, options = 0, defreadfds, fromlen;
8150886Storek 	struct sockaddr_un un, fromunix;
8213816Ssam 	struct sockaddr_in sin, frominet;
8314837Sralph 	int omask, lfd;
8412106Sralph 
8512106Sralph 	gethostname(host, sizeof(host));
8612106Sralph 	name = argv[0];
8712106Sralph 
8812106Sralph 	while (--argc > 0) {
8912106Sralph 		argv++;
9012106Sralph 		if (argv[0][0] == '-')
9112106Sralph 			switch (argv[0][1]) {
9212106Sralph 			case 'd':
9312106Sralph 				options |= SO_DEBUG;
9412106Sralph 				break;
9512106Sralph 			case 'l':
9612430Sralph 				lflag++;
9712430Sralph 				break;
9812106Sralph 			}
9912106Sralph 	}
10014837Sralph 
10112106Sralph #ifndef DEBUG
10212106Sralph 	/*
10312106Sralph 	 * Set up standard environment by detaching from the parent.
10412106Sralph 	 */
10544708Skarels 	daemon(0, 0);
10612106Sralph #endif
10714837Sralph 
10825494Seric 	openlog("lpd", LOG_PID, LOG_LPR);
10912106Sralph 	(void) umask(0);
11037968Sbostic 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
11114837Sralph 	if (lfd < 0) {
11237968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
11314837Sralph 		exit(1);
11414837Sralph 	}
11514837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
11614837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
11714837Sralph 			exit(0);
11837968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
11914837Sralph 		exit(1);
12014837Sralph 	}
12114837Sralph 	ftruncate(lfd, 0);
12212875Sralph 	/*
12314837Sralph 	 * write process id for others to know
12414837Sralph 	 */
12514837Sralph 	sprintf(line, "%u\n", getpid());
12614837Sralph 	f = strlen(line);
12714837Sralph 	if (write(lfd, line, f) != f) {
12837968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
12914837Sralph 		exit(1);
13014837Sralph 	}
13114837Sralph 	signal(SIGCHLD, reapchild);
13214837Sralph 	/*
13312875Sralph 	 * Restart all the printers.
13412875Sralph 	 */
13512875Sralph 	startup();
13637968Sbostic 	(void) unlink(_PATH_SOCKETNAME);
13713816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
13813816Ssam 	if (funix < 0) {
13916761Sralph 		syslog(LOG_ERR, "socket: %m");
14012106Sralph 		exit(1);
14112106Sralph 	}
14214149Sralph #define	mask(s)	(1 << ((s) - 1))
14314149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
14416761Sralph 	signal(SIGHUP, mcleanup);
14516761Sralph 	signal(SIGINT, mcleanup);
14616761Sralph 	signal(SIGQUIT, mcleanup);
14716761Sralph 	signal(SIGTERM, mcleanup);
14850886Storek 	un.sun_family = AF_UNIX;
14950886Storek 	strcpy(un.sun_path, _PATH_SOCKETNAME);
15046911Sbostic 	if (bind(funix,
15150886Storek 	     (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) {
15216761Sralph 		syslog(LOG_ERR, "ubind: %m");
15312106Sralph 		exit(1);
15412106Sralph 	}
15513816Ssam 	sigsetmask(omask);
15614149Sralph 	defreadfds = 1 << funix;
15714149Sralph 	listen(funix, 5);
15813816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
15913816Ssam 	if (finet >= 0) {
16013816Ssam 		struct servent *sp;
16113816Ssam 
16213816Ssam 		if (options & SO_DEBUG)
16313816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
16416761Sralph 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
165*55474Sbostic 				mcleanup(0);
16613816Ssam 			}
16713816Ssam 		sp = getservbyname("printer", "tcp");
16813816Ssam 		if (sp == NULL) {
16916761Sralph 			syslog(LOG_ERR, "printer/tcp: unknown service");
170*55474Sbostic 			mcleanup(0);
17113816Ssam 		}
17213816Ssam 		sin.sin_family = AF_INET;
17313816Ssam 		sin.sin_port = sp->s_port;
17446911Sbostic 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
17516761Sralph 			syslog(LOG_ERR, "bind: %m");
176*55474Sbostic 			mcleanup(0);
17713816Ssam 		}
17814149Sralph 		defreadfds |= 1 << finet;
17914149Sralph 		listen(finet, 5);
18013816Ssam 	}
18112106Sralph 	/*
18214149Sralph 	 * Main loop: accept, do a request, continue.
18312106Sralph 	 */
18412106Sralph 	for (;;) {
18513957Ssam 		int domain, nfds, s, readfds = defreadfds;
18612106Sralph 
18713957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
18813957Ssam 		if (nfds <= 0) {
18916761Sralph 			if (nfds < 0 && errno != EINTR)
19016761Sralph 				syslog(LOG_WARNING, "select: %m");
19113957Ssam 			continue;
19213957Ssam 		}
19313816Ssam 		if (readfds & (1 << funix)) {
19413957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
19546911Sbostic 			s = accept(funix,
19646911Sbostic 			    (struct sockaddr *)&fromunix, &fromlen);
19713957Ssam 		} else if (readfds & (1 << finet)) {
19813957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
19946911Sbostic 			s = accept(finet,
20046911Sbostic 			    (struct sockaddr *)&frominet, &fromlen);
20113816Ssam 		}
20212106Sralph 		if (s < 0) {
20316761Sralph 			if (errno != EINTR)
20416761Sralph 				syslog(LOG_WARNING, "accept: %m");
20516761Sralph 			continue;
20612106Sralph 		}
20712106Sralph 		if (fork() == 0) {
20813147Ssam 			signal(SIGCHLD, SIG_IGN);
20914708Sralph 			signal(SIGHUP, SIG_IGN);
21014708Sralph 			signal(SIGINT, SIG_IGN);
21114708Sralph 			signal(SIGQUIT, SIG_IGN);
21214708Sralph 			signal(SIGTERM, SIG_IGN);
21313816Ssam 			(void) close(funix);
21413816Ssam 			(void) close(finet);
21513816Ssam 			dup2(s, 1);
21613816Ssam 			(void) close(s);
21747055Smckusick 			if (domain == AF_INET) {
21847055Smckusick 				from_remote = 1;
21913816Ssam 				chkhost(&frominet);
22047055Smckusick 			} else
22147055Smckusick 				from_remote = 0;
22213816Ssam 			doit();
22312106Sralph 			exit(0);
22412106Sralph 		}
22512106Sralph 		(void) close(s);
22612106Sralph 	}
22712106Sralph }
22812106Sralph 
229*55474Sbostic static void
230*55474Sbostic reapchild(signo)
231*55474Sbostic 	int signo;
23212106Sralph {
23312106Sralph 	union wait status;
23412106Sralph 
23546911Sbostic 	while (wait3((int *)&status, WNOHANG, 0) > 0)
23612106Sralph 		;
23712106Sralph }
23812106Sralph 
239*55474Sbostic static void
240*55474Sbostic mcleanup(signo)
241*55474Sbostic 	int signo;
24213816Ssam {
24314149Sralph 	if (lflag)
24416761Sralph 		syslog(LOG_INFO, "exiting");
24537968Sbostic 	unlink(_PATH_SOCKETNAME);
24613816Ssam 	exit(0);
24713816Ssam }
24813816Ssam 
24912106Sralph /*
25012106Sralph  * Stuff for handling job specifications
25112106Sralph  */
25212106Sralph char	*user[MAXUSERS];	/* users to process */
25312106Sralph int	users;			/* # of users in user array */
25412106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
25512106Sralph int	requests;		/* # of spool requests */
25612875Sralph char	*person;		/* name of person doing lprm */
25712106Sralph 
25854546Sleres char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
25954546Sleres char	cbuf[BUFSIZ];		/* command line buffer */
26016761Sralph char	*cmdnames[] = {
26112430Sralph 	"null",
26212430Sralph 	"printjob",
26312430Sralph 	"recvjob",
26412430Sralph 	"displayq short",
26512430Sralph 	"displayq long",
26612430Sralph 	"rmjob"
26712430Sralph };
26812106Sralph 
269*55474Sbostic static void
27013816Ssam doit()
27112106Sralph {
27212106Sralph 	register char *cp;
27312106Sralph 	register int n;
27412106Sralph 
27512106Sralph 	for (;;) {
27612106Sralph 		cp = cbuf;
27712106Sralph 		do {
27813816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
27913816Ssam 				fatal("Command line too long");
28013816Ssam 			if ((n = read(1, cp, 1)) != 1) {
28112106Sralph 				if (n < 0)
28212106Sralph 					fatal("Lost connection");
28312106Sralph 				return;
28412106Sralph 			}
28513816Ssam 		} while (*cp++ != '\n');
28612106Sralph 		*--cp = '\0';
28712106Sralph 		cp = cbuf;
28816761Sralph 		if (lflag) {
28916761Sralph 			if (*cp >= '\1' && *cp <= '\5')
29016761Sralph 				syslog(LOG_INFO, "%s requests %s %s",
29116761Sralph 					from, cmdnames[*cp], cp+1);
29216761Sralph 			else
29316761Sralph 				syslog(LOG_INFO, "bad request (%d) from %s",
29416761Sralph 					*cp, from);
29512430Sralph 		}
29612106Sralph 		switch (*cp++) {
29712106Sralph 		case '\1':	/* check the queue and print any jobs there */
29812106Sralph 			printer = cp;
29912106Sralph 			printjob();
30012106Sralph 			break;
30112106Sralph 		case '\2':	/* receive files to be queued */
30247055Smckusick 			if (!from_remote) {
30347055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
30447055Smckusick 				exit(1);
30547055Smckusick 			}
30612106Sralph 			printer = cp;
30712106Sralph 			recvjob();
30812106Sralph 			break;
30912430Sralph 		case '\3':	/* display the queue (short form) */
31012430Sralph 		case '\4':	/* display the queue (long form) */
31112106Sralph 			printer = cp;
31212106Sralph 			while (*cp) {
31312106Sralph 				if (*cp != ' ') {
31412106Sralph 					cp++;
31512106Sralph 					continue;
31612106Sralph 				}
31712106Sralph 				*cp++ = '\0';
31812106Sralph 				while (isspace(*cp))
31912106Sralph 					cp++;
32012106Sralph 				if (*cp == '\0')
32112106Sralph 					break;
32212106Sralph 				if (isdigit(*cp)) {
32312106Sralph 					if (requests >= MAXREQUESTS)
32412106Sralph 						fatal("Too many requests");
32512106Sralph 					requ[requests++] = atoi(cp);
32612106Sralph 				} else {
32712106Sralph 					if (users >= MAXUSERS)
32812106Sralph 						fatal("Too many users");
32912106Sralph 					user[users++] = cp;
33012106Sralph 				}
33112106Sralph 			}
33212106Sralph 			displayq(cbuf[0] - '\3');
33312106Sralph 			exit(0);
33412106Sralph 		case '\5':	/* remove a job from the queue */
33547055Smckusick 			if (!from_remote) {
33647055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
33747055Smckusick 				exit(1);
33847055Smckusick 			}
33912106Sralph 			printer = cp;
34012106Sralph 			while (*cp && *cp != ' ')
34112106Sralph 				cp++;
34212106Sralph 			if (!*cp)
34312106Sralph 				break;
34412106Sralph 			*cp++ = '\0';
34512106Sralph 			person = cp;
34612106Sralph 			while (*cp) {
34712106Sralph 				if (*cp != ' ') {
34812106Sralph 					cp++;
34912106Sralph 					continue;
35012106Sralph 				}
35112106Sralph 				*cp++ = '\0';
35212106Sralph 				while (isspace(*cp))
35312106Sralph 					cp++;
35412106Sralph 				if (*cp == '\0')
35512106Sralph 					break;
35612106Sralph 				if (isdigit(*cp)) {
35712106Sralph 					if (requests >= MAXREQUESTS)
35812106Sralph 						fatal("Too many requests");
35912106Sralph 					requ[requests++] = atoi(cp);
36012106Sralph 				} else {
36112106Sralph 					if (users >= MAXUSERS)
36212106Sralph 						fatal("Too many users");
36312106Sralph 					user[users++] = cp;
36412106Sralph 				}
36512106Sralph 			}
36612106Sralph 			rmjob();
36712106Sralph 			break;
36812106Sralph 		}
36912106Sralph 		fatal("Illegal service request");
37012106Sralph 	}
37112106Sralph }
37212106Sralph 
37312106Sralph /*
37412430Sralph  * Make a pass through the printcap database and start printing any
37512430Sralph  * files left from the last time the machine went down.
37612430Sralph  */
377*55474Sbostic static void
37812430Sralph startup()
37912430Sralph {
38012430Sralph 	char buf[BUFSIZ];
38112430Sralph 	register char *cp;
38212430Sralph 	int pid;
38312430Sralph 
38412430Sralph 	printer = buf;
38512430Sralph 
38612430Sralph 	/*
38712430Sralph 	 * Restart the daemons.
38812430Sralph 	 */
38912430Sralph 	while (getprent(buf) > 0) {
39012430Sralph 		for (cp = buf; *cp; cp++)
39112430Sralph 			if (*cp == '|' || *cp == ':') {
39212430Sralph 				*cp = '\0';
39312430Sralph 				break;
39412430Sralph 			}
39512430Sralph 		if ((pid = fork()) < 0) {
39616761Sralph 			syslog(LOG_WARNING, "startup: cannot fork");
397*55474Sbostic 			mcleanup(0);
39812430Sralph 		}
39912430Sralph 		if (!pid) {
40012430Sralph 			endprent();
40112430Sralph 			printjob();
40212430Sralph 		}
40312430Sralph 	}
40412430Sralph }
40512430Sralph 
40627746Sbloom #define DUMMY ":nobody::"
40727746Sbloom 
40812430Sralph /*
40912430Sralph  * Check to see if the from host has access to the line printer.
41012430Sralph  */
411*55474Sbostic static void
41213816Ssam chkhost(f)
41313816Ssam 	struct sockaddr_in *f;
41412430Sralph {
41513816Ssam 	register struct hostent *hp;
41612430Sralph 	register FILE *hostf;
41727746Sbloom 	register char *cp, *sp;
41815551Sralph 	int first = 1;
41913816Ssam 	extern char *inet_ntoa();
42012430Sralph 
42113816Ssam 	f->sin_port = ntohs(f->sin_port);
42213816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
42313816Ssam 		fatal("Malformed from address");
42454546Sleres 
42554546Sleres 	/* Need real hostname for temporary filenames */
42646911Sbostic 	hp = gethostbyaddr((char *)&f->sin_addr,
42746911Sbostic 	    sizeof(struct in_addr), f->sin_family);
42854546Sleres 	if (hp == NULL)
42913816Ssam 		fatal("Host name for your address (%s) unknown",
43013816Ssam 			inet_ntoa(f->sin_addr));
43113816Ssam 
43254546Sleres 	(void) strncpy(fromb, hp->h_name, sizeof(fromb));
43354546Sleres 	from[sizeof(fromb) - 1] = '\0';
43413816Ssam 	from = fromb;
43512734Sralph 
43637968Sbostic 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
43715551Sralph again:
43815551Sralph 	if (hostf) {
43954546Sleres 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
44054546Sleres 		    DUMMY, DUMMY) == 0) {
44127746Sbloom 			(void) fclose(hostf);
44227746Sbloom 			return;
44312430Sralph 		}
44415551Sralph 		(void) fclose(hostf);
44512430Sralph 	}
44615551Sralph 	if (first == 1) {
44715551Sralph 		first = 0;
44837968Sbostic 		hostf = fopen(_PATH_HOSTSLPD, "r");
44915551Sralph 		goto again;
45015551Sralph 	}
45113816Ssam 	fatal("Your host does not have line printer access");
45254546Sleres 	/*NOTREACHED*/
45312430Sralph }
454*55474Sbostic 
455*55474Sbostic 
456*55474Sbostic 
457*55474Sbostic 
458*55474Sbostic 
459*55474Sbostic 
460*55474Sbostic 
461*55474Sbostic 
462*55474Sbostic 
463*55474Sbostic 
464*55474Sbostic 
465*55474Sbostic 
466