xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 46911)
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*46911Sbostic static char sccsid[] = "@(#)lpd.c	5.11 (Berkeley) 03/02/91";
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 
4712106Sralph #include "lp.h"
4837968Sbostic #include "pathnames.h"
4912106Sralph 
5016761Sralph int	lflag;				/* log requests flag */
5112875Sralph 
52*46911Sbostic void mcleanup(), reapchild();
5312106Sralph 
5412106Sralph main(argc, argv)
5512106Sralph 	int argc;
5612106Sralph 	char **argv;
5712106Sralph {
5845344Skarels 	int f, funix, finet, options = 0, defreadfds, fromlen;
5913816Ssam 	struct sockaddr_un sun, fromunix;
6013816Ssam 	struct sockaddr_in sin, frominet;
6114837Sralph 	int omask, lfd;
6212106Sralph 
6312106Sralph 	gethostname(host, sizeof(host));
6412106Sralph 	name = argv[0];
6512106Sralph 
6612106Sralph 	while (--argc > 0) {
6712106Sralph 		argv++;
6812106Sralph 		if (argv[0][0] == '-')
6912106Sralph 			switch (argv[0][1]) {
7012106Sralph 			case 'd':
7112106Sralph 				options |= SO_DEBUG;
7212106Sralph 				break;
7312106Sralph 			case 'l':
7412430Sralph 				lflag++;
7512430Sralph 				break;
7612106Sralph 			}
7712106Sralph 	}
7814837Sralph 
7912106Sralph #ifndef DEBUG
8012106Sralph 	/*
8112106Sralph 	 * Set up standard environment by detaching from the parent.
8212106Sralph 	 */
8344708Skarels 	daemon(0, 0);
8412106Sralph #endif
8514837Sralph 
8625494Seric 	openlog("lpd", LOG_PID, LOG_LPR);
8712106Sralph 	(void) umask(0);
8837968Sbostic 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
8914837Sralph 	if (lfd < 0) {
9037968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
9114837Sralph 		exit(1);
9214837Sralph 	}
9314837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
9414837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
9514837Sralph 			exit(0);
9637968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
9714837Sralph 		exit(1);
9814837Sralph 	}
9914837Sralph 	ftruncate(lfd, 0);
10012875Sralph 	/*
10114837Sralph 	 * write process id for others to know
10214837Sralph 	 */
10314837Sralph 	sprintf(line, "%u\n", getpid());
10414837Sralph 	f = strlen(line);
10514837Sralph 	if (write(lfd, line, f) != f) {
10637968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
10714837Sralph 		exit(1);
10814837Sralph 	}
10914837Sralph 	signal(SIGCHLD, reapchild);
11014837Sralph 	/*
11112875Sralph 	 * Restart all the printers.
11212875Sralph 	 */
11312875Sralph 	startup();
11437968Sbostic 	(void) unlink(_PATH_SOCKETNAME);
11513816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
11613816Ssam 	if (funix < 0) {
11716761Sralph 		syslog(LOG_ERR, "socket: %m");
11812106Sralph 		exit(1);
11912106Sralph 	}
12014149Sralph #define	mask(s)	(1 << ((s) - 1))
12114149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
12216761Sralph 	signal(SIGHUP, mcleanup);
12316761Sralph 	signal(SIGINT, mcleanup);
12416761Sralph 	signal(SIGQUIT, mcleanup);
12516761Sralph 	signal(SIGTERM, mcleanup);
12613816Ssam 	sun.sun_family = AF_UNIX;
12737968Sbostic 	strcpy(sun.sun_path, _PATH_SOCKETNAME);
128*46911Sbostic 	if (bind(funix,
129*46911Sbostic 	     (struct sockaddr *)&sun, strlen(sun.sun_path) + 2) < 0) {
13016761Sralph 		syslog(LOG_ERR, "ubind: %m");
13112106Sralph 		exit(1);
13212106Sralph 	}
13313816Ssam 	sigsetmask(omask);
13414149Sralph 	defreadfds = 1 << funix;
13514149Sralph 	listen(funix, 5);
13613816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
13713816Ssam 	if (finet >= 0) {
13813816Ssam 		struct servent *sp;
13913816Ssam 
14013816Ssam 		if (options & SO_DEBUG)
14113816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
14216761Sralph 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
14316761Sralph 				mcleanup();
14413816Ssam 			}
14513816Ssam 		sp = getservbyname("printer", "tcp");
14613816Ssam 		if (sp == NULL) {
14716761Sralph 			syslog(LOG_ERR, "printer/tcp: unknown service");
14816761Sralph 			mcleanup();
14913816Ssam 		}
15013816Ssam 		sin.sin_family = AF_INET;
15113816Ssam 		sin.sin_port = sp->s_port;
152*46911Sbostic 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
15316761Sralph 			syslog(LOG_ERR, "bind: %m");
15416761Sralph 			mcleanup();
15513816Ssam 		}
15614149Sralph 		defreadfds |= 1 << finet;
15714149Sralph 		listen(finet, 5);
15813816Ssam 	}
15912106Sralph 	/*
16014149Sralph 	 * Main loop: accept, do a request, continue.
16112106Sralph 	 */
16212106Sralph 	for (;;) {
16313957Ssam 		int domain, nfds, s, readfds = defreadfds;
16412106Sralph 
16513957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
16613957Ssam 		if (nfds <= 0) {
16716761Sralph 			if (nfds < 0 && errno != EINTR)
16816761Sralph 				syslog(LOG_WARNING, "select: %m");
16913957Ssam 			continue;
17013957Ssam 		}
17113816Ssam 		if (readfds & (1 << funix)) {
17213957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
173*46911Sbostic 			s = accept(funix,
174*46911Sbostic 			    (struct sockaddr *)&fromunix, &fromlen);
17513957Ssam 		} else if (readfds & (1 << finet)) {
17613957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
177*46911Sbostic 			s = accept(finet,
178*46911Sbostic 			    (struct sockaddr *)&frominet, &fromlen);
17913816Ssam 		}
18012106Sralph 		if (s < 0) {
18116761Sralph 			if (errno != EINTR)
18216761Sralph 				syslog(LOG_WARNING, "accept: %m");
18316761Sralph 			continue;
18412106Sralph 		}
18512106Sralph 		if (fork() == 0) {
18613147Ssam 			signal(SIGCHLD, SIG_IGN);
18714708Sralph 			signal(SIGHUP, SIG_IGN);
18814708Sralph 			signal(SIGINT, SIG_IGN);
18914708Sralph 			signal(SIGQUIT, SIG_IGN);
19014708Sralph 			signal(SIGTERM, SIG_IGN);
19113816Ssam 			(void) close(funix);
19213816Ssam 			(void) close(finet);
19313816Ssam 			dup2(s, 1);
19413816Ssam 			(void) close(s);
19513816Ssam 			if (domain == AF_INET)
19613816Ssam 				chkhost(&frominet);
19713816Ssam 			doit();
19812106Sralph 			exit(0);
19912106Sralph 		}
20012106Sralph 		(void) close(s);
20112106Sralph 	}
20212106Sralph }
20312106Sralph 
204*46911Sbostic void
20512106Sralph reapchild()
20612106Sralph {
20712106Sralph 	union wait status;
20812106Sralph 
209*46911Sbostic 	while (wait3((int *)&status, WNOHANG, 0) > 0)
21012106Sralph 		;
21112106Sralph }
21212106Sralph 
213*46911Sbostic void
21416761Sralph mcleanup()
21513816Ssam {
21614149Sralph 	if (lflag)
21716761Sralph 		syslog(LOG_INFO, "exiting");
21837968Sbostic 	unlink(_PATH_SOCKETNAME);
21913816Ssam 	exit(0);
22013816Ssam }
22113816Ssam 
22212106Sralph /*
22312106Sralph  * Stuff for handling job specifications
22412106Sralph  */
22512106Sralph char	*user[MAXUSERS];	/* users to process */
22612106Sralph int	users;			/* # of users in user array */
22712106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
22812106Sralph int	requests;		/* # of spool requests */
22912875Sralph char	*person;		/* name of person doing lprm */
23012106Sralph 
23116761Sralph char	fromb[32];	/* buffer for client's machine name */
23216761Sralph char	cbuf[BUFSIZ];	/* command line buffer */
23316761Sralph char	*cmdnames[] = {
23412430Sralph 	"null",
23512430Sralph 	"printjob",
23612430Sralph 	"recvjob",
23712430Sralph 	"displayq short",
23812430Sralph 	"displayq long",
23912430Sralph 	"rmjob"
24012430Sralph };
24112106Sralph 
24213816Ssam doit()
24312106Sralph {
24412106Sralph 	register char *cp;
24512106Sralph 	register int n;
24612106Sralph 
24712106Sralph 	for (;;) {
24812106Sralph 		cp = cbuf;
24912106Sralph 		do {
25013816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
25113816Ssam 				fatal("Command line too long");
25213816Ssam 			if ((n = read(1, cp, 1)) != 1) {
25312106Sralph 				if (n < 0)
25412106Sralph 					fatal("Lost connection");
25512106Sralph 				return;
25612106Sralph 			}
25713816Ssam 		} while (*cp++ != '\n');
25812106Sralph 		*--cp = '\0';
25912106Sralph 		cp = cbuf;
26016761Sralph 		if (lflag) {
26116761Sralph 			if (*cp >= '\1' && *cp <= '\5')
26216761Sralph 				syslog(LOG_INFO, "%s requests %s %s",
26316761Sralph 					from, cmdnames[*cp], cp+1);
26416761Sralph 			else
26516761Sralph 				syslog(LOG_INFO, "bad request (%d) from %s",
26616761Sralph 					*cp, from);
26712430Sralph 		}
26812106Sralph 		switch (*cp++) {
26912106Sralph 		case '\1':	/* check the queue and print any jobs there */
27012106Sralph 			printer = cp;
27112106Sralph 			printjob();
27212106Sralph 			break;
27312106Sralph 		case '\2':	/* receive files to be queued */
27412106Sralph 			printer = cp;
27512106Sralph 			recvjob();
27612106Sralph 			break;
27712430Sralph 		case '\3':	/* display the queue (short form) */
27812430Sralph 		case '\4':	/* display the queue (long form) */
27912106Sralph 			printer = cp;
28012106Sralph 			while (*cp) {
28112106Sralph 				if (*cp != ' ') {
28212106Sralph 					cp++;
28312106Sralph 					continue;
28412106Sralph 				}
28512106Sralph 				*cp++ = '\0';
28612106Sralph 				while (isspace(*cp))
28712106Sralph 					cp++;
28812106Sralph 				if (*cp == '\0')
28912106Sralph 					break;
29012106Sralph 				if (isdigit(*cp)) {
29112106Sralph 					if (requests >= MAXREQUESTS)
29212106Sralph 						fatal("Too many requests");
29312106Sralph 					requ[requests++] = atoi(cp);
29412106Sralph 				} else {
29512106Sralph 					if (users >= MAXUSERS)
29612106Sralph 						fatal("Too many users");
29712106Sralph 					user[users++] = cp;
29812106Sralph 				}
29912106Sralph 			}
30012106Sralph 			displayq(cbuf[0] - '\3');
30112106Sralph 			exit(0);
30212106Sralph 		case '\5':	/* remove a job from the queue */
30312106Sralph 			printer = cp;
30412106Sralph 			while (*cp && *cp != ' ')
30512106Sralph 				cp++;
30612106Sralph 			if (!*cp)
30712106Sralph 				break;
30812106Sralph 			*cp++ = '\0';
30912106Sralph 			person = cp;
31012106Sralph 			while (*cp) {
31112106Sralph 				if (*cp != ' ') {
31212106Sralph 					cp++;
31312106Sralph 					continue;
31412106Sralph 				}
31512106Sralph 				*cp++ = '\0';
31612106Sralph 				while (isspace(*cp))
31712106Sralph 					cp++;
31812106Sralph 				if (*cp == '\0')
31912106Sralph 					break;
32012106Sralph 				if (isdigit(*cp)) {
32112106Sralph 					if (requests >= MAXREQUESTS)
32212106Sralph 						fatal("Too many requests");
32312106Sralph 					requ[requests++] = atoi(cp);
32412106Sralph 				} else {
32512106Sralph 					if (users >= MAXUSERS)
32612106Sralph 						fatal("Too many users");
32712106Sralph 					user[users++] = cp;
32812106Sralph 				}
32912106Sralph 			}
33012106Sralph 			rmjob();
33112106Sralph 			break;
33212106Sralph 		}
33312106Sralph 		fatal("Illegal service request");
33412106Sralph 	}
33512106Sralph }
33612106Sralph 
33712106Sralph /*
33812430Sralph  * Make a pass through the printcap database and start printing any
33912430Sralph  * files left from the last time the machine went down.
34012430Sralph  */
34112430Sralph startup()
34212430Sralph {
34312430Sralph 	char buf[BUFSIZ];
34412430Sralph 	register char *cp;
34512430Sralph 	int pid;
34612430Sralph 
34712430Sralph 	printer = buf;
34812430Sralph 
34912430Sralph 	/*
35012430Sralph 	 * Restart the daemons.
35112430Sralph 	 */
35212430Sralph 	while (getprent(buf) > 0) {
35312430Sralph 		for (cp = buf; *cp; cp++)
35412430Sralph 			if (*cp == '|' || *cp == ':') {
35512430Sralph 				*cp = '\0';
35612430Sralph 				break;
35712430Sralph 			}
35812430Sralph 		if ((pid = fork()) < 0) {
35916761Sralph 			syslog(LOG_WARNING, "startup: cannot fork");
36016761Sralph 			mcleanup();
36112430Sralph 		}
36212430Sralph 		if (!pid) {
36312430Sralph 			endprent();
36412430Sralph 			printjob();
36512430Sralph 		}
36612430Sralph 	}
36712430Sralph }
36812430Sralph 
36927746Sbloom #define DUMMY ":nobody::"
37027746Sbloom 
37112430Sralph /*
37212430Sralph  * Check to see if the from host has access to the line printer.
37312430Sralph  */
37413816Ssam chkhost(f)
37513816Ssam 	struct sockaddr_in *f;
37612430Sralph {
37713816Ssam 	register struct hostent *hp;
37812430Sralph 	register FILE *hostf;
37927746Sbloom 	register char *cp, *sp;
38012430Sralph 	char ahost[50];
38115551Sralph 	int first = 1;
38213816Ssam 	extern char *inet_ntoa();
38327746Sbloom 	int baselen = -1;
38412430Sralph 
38513816Ssam 	f->sin_port = ntohs(f->sin_port);
38613816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
38713816Ssam 		fatal("Malformed from address");
388*46911Sbostic 	hp = gethostbyaddr((char *)&f->sin_addr,
389*46911Sbostic 	    sizeof(struct in_addr), f->sin_family);
39013816Ssam 	if (hp == 0)
39113816Ssam 		fatal("Host name for your address (%s) unknown",
39213816Ssam 			inet_ntoa(f->sin_addr));
39313816Ssam 
39413816Ssam 	strcpy(fromb, hp->h_name);
39513816Ssam 	from = fromb;
39612734Sralph 	if (!strcmp(from, host))
39713816Ssam 		return;
39812734Sralph 
39927746Sbloom 	sp = fromb;
40027746Sbloom 	cp = ahost;
40127746Sbloom 	while (*sp) {
40227746Sbloom 		if (*sp == '.') {
40327746Sbloom 			if (baselen == -1)
40427746Sbloom 				baselen = sp - fromb;
40527746Sbloom 			*cp++ = *sp++;
40627746Sbloom 		} else {
40727746Sbloom 			*cp++ = isupper(*sp) ? tolower(*sp++) : *sp++;
40827746Sbloom 		}
40927746Sbloom 	}
41027746Sbloom 	*cp = '\0';
41137968Sbostic 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
41215551Sralph again:
41315551Sralph 	if (hostf) {
41427746Sbloom 		if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) {
41527746Sbloom 			(void) fclose(hostf);
41627746Sbloom 			return;
41712430Sralph 		}
41815551Sralph 		(void) fclose(hostf);
41912430Sralph 	}
42015551Sralph 	if (first == 1) {
42115551Sralph 		first = 0;
42237968Sbostic 		hostf = fopen(_PATH_HOSTSLPD, "r");
42315551Sralph 		goto again;
42415551Sralph 	}
42513816Ssam 	fatal("Your host does not have line printer access");
42612430Sralph }
427