xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 66850)
122429Sdist /*
2*66850Sbostic  * Copyright (c) 1983, 1993, 1994
361843Sbostic  *	The Regents of the University of California.  All rights reserved.
434203Sbostic  *
556123Selan  *
656251Selan  * %sccs.include.redist.c%
722429Sdist  */
822429Sdist 
913957Ssam #ifndef lint
1056265Selan static char copyright[] =
11*66850Sbostic "@(#) Copyright (c) 1983, 1993, 1994\n\
1261843Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1334203Sbostic #endif /* not lint */
1413957Ssam 
1522429Sdist #ifndef lint
16*66850Sbostic static char sccsid[] = "@(#)lpd.c	8.4 (Berkeley) 04/17/94";
1734203Sbostic #endif /* not lint */
1822429Sdist 
1912106Sralph /*
2012106Sralph  * lpd -- line printer daemon.
2112106Sralph  *
2212106Sralph  * Listen for a connection and perform the requested operation.
2312106Sralph  * Operations are:
2412106Sralph  *	\1printer\n
2512106Sralph  *		check the queue for jobs and print any found.
2612106Sralph  *	\2printer\n
2712106Sralph  *		receive a job from another machine and queue it.
2812106Sralph  *	\3printer [users ...] [jobs ...]\n
2912106Sralph  *		return the current state of the queue (short form).
3012106Sralph  *	\4printer [users ...] [jobs ...]\n
3112106Sralph  *		return the current state of the queue (long form).
3212106Sralph  *	\5printer person [users ...] [jobs ...]\n
3312106Sralph  *		remove jobs from the queue.
3412106Sralph  *
3512106Sralph  * Strategy to maintain protected spooling area:
3612106Sralph  *	1. Spooling area is writable only by daemon and spooling group
3712106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
3812106Sralph  *	   root to access any file it wants (verifying things before
3912106Sralph  *	   with an access call) and group id to know how it should
4012106Sralph  *	   set up ownership of files in the spooling area.
4112430Sralph  *	3. Files in spooling area are owned by root, group spooling
4212106Sralph  *	   group, with mode 660.
4312106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
4412106Sralph  *	   access files and printer.  Users can't get to anything
4512106Sralph  *	   w/o help of lpq and lprm programs.
4612106Sralph  */
4712106Sralph 
4855474Sbostic #include <sys/param.h>
4955474Sbostic #include <sys/wait.h>
5056123Selan #include <sys/types.h>
5155474Sbostic #include <sys/socket.h>
5255474Sbostic #include <sys/un.h>
5356123Selan #include <sys/stat.h>
5455474Sbostic #include <netinet/in.h>
5556123Selan 
5655474Sbostic #include <netdb.h>
5756123Selan #include <unistd.h>
5855474Sbostic #include <syslog.h>
5955474Sbostic #include <signal.h>
6055474Sbostic #include <errno.h>
6155474Sbostic #include <fcntl.h>
6255474Sbostic #include <dirent.h>
6355474Sbostic #include <stdio.h>
6456123Selan #include <stdlib.h>
6556123Selan #include <string.h>
6656123Selan #include <ctype.h>
6712106Sralph #include "lp.h"
6855474Sbostic #include "lp.local.h"
6937968Sbostic #include "pathnames.h"
7055474Sbostic #include "extern.h"
7112106Sralph 
7216761Sralph int	lflag;				/* log requests flag */
7347055Smckusick int	from_remote;			/* from remote socket */
7412875Sralph 
7555474Sbostic static void       reapchild __P((int));
7655474Sbostic static void       mcleanup __P((int));
7755474Sbostic static void       doit __P((void));
7855474Sbostic static void       startup __P((void));
7955474Sbostic static void       chkhost __P((struct sockaddr_in *));
8012106Sralph 
8155474Sbostic int
8212106Sralph main(argc, argv)
8312106Sralph 	int argc;
8412106Sralph 	char **argv;
8512106Sralph {
8656123Selan 	int f, funix, finet, options, fromlen;
8756123Selan 	fd_set defreadfds;
8850886Storek 	struct sockaddr_un un, fromunix;
8913816Ssam 	struct sockaddr_in sin, frominet;
9014837Sralph 	int omask, lfd;
9112106Sralph 
9256123Selan 	options = 0;
9312106Sralph 	gethostname(host, sizeof(host));
9412106Sralph 	name = argv[0];
9512106Sralph 
9612106Sralph 	while (--argc > 0) {
9712106Sralph 		argv++;
9812106Sralph 		if (argv[0][0] == '-')
9912106Sralph 			switch (argv[0][1]) {
10012106Sralph 			case 'd':
10112106Sralph 				options |= SO_DEBUG;
10212106Sralph 				break;
10312106Sralph 			case 'l':
10412430Sralph 				lflag++;
10512430Sralph 				break;
10612106Sralph 			}
10712106Sralph 	}
10814837Sralph 
10912106Sralph #ifndef DEBUG
11012106Sralph 	/*
11112106Sralph 	 * Set up standard environment by detaching from the parent.
11212106Sralph 	 */
11344708Skarels 	daemon(0, 0);
11412106Sralph #endif
11514837Sralph 
11625494Seric 	openlog("lpd", LOG_PID, LOG_LPR);
11756923Sbostic 	syslog(LOG_INFO, "restarted");
11812106Sralph 	(void) umask(0);
11937968Sbostic 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
12014837Sralph 	if (lfd < 0) {
12137968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
12214837Sralph 		exit(1);
12314837Sralph 	}
12414837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
12514837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
12614837Sralph 			exit(0);
12737968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
12814837Sralph 		exit(1);
12914837Sralph 	}
13014837Sralph 	ftruncate(lfd, 0);
13112875Sralph 	/*
13214837Sralph 	 * write process id for others to know
13314837Sralph 	 */
13414837Sralph 	sprintf(line, "%u\n", getpid());
13514837Sralph 	f = strlen(line);
13614837Sralph 	if (write(lfd, line, f) != f) {
13737968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
13814837Sralph 		exit(1);
13914837Sralph 	}
14014837Sralph 	signal(SIGCHLD, reapchild);
14114837Sralph 	/*
14212875Sralph 	 * Restart all the printers.
14312875Sralph 	 */
14412875Sralph 	startup();
14537968Sbostic 	(void) unlink(_PATH_SOCKETNAME);
14613816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
14713816Ssam 	if (funix < 0) {
14816761Sralph 		syslog(LOG_ERR, "socket: %m");
14912106Sralph 		exit(1);
15012106Sralph 	}
15114149Sralph #define	mask(s)	(1 << ((s) - 1))
15214149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
15316761Sralph 	signal(SIGHUP, mcleanup);
15416761Sralph 	signal(SIGINT, mcleanup);
15516761Sralph 	signal(SIGQUIT, mcleanup);
15616761Sralph 	signal(SIGTERM, mcleanup);
15766248Sbostic 	memset(&un, 0, sizeof(un));
15850886Storek 	un.sun_family = AF_UNIX;
15950886Storek 	strcpy(un.sun_path, _PATH_SOCKETNAME);
16058240Storek #ifndef SUN_LEN
16158240Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
16258240Storek #endif
16358240Storek 	if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
16416761Sralph 		syslog(LOG_ERR, "ubind: %m");
16512106Sralph 		exit(1);
16612106Sralph 	}
16713816Ssam 	sigsetmask(omask);
16856123Selan 	FD_ZERO(&defreadfds);
16956123Selan 	FD_SET(funix, &defreadfds);
17014149Sralph 	listen(funix, 5);
17113816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
17213816Ssam 	if (finet >= 0) {
17313816Ssam 		struct servent *sp;
17413816Ssam 
17513816Ssam 		if (options & SO_DEBUG)
17613816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
17716761Sralph 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
17855474Sbostic 				mcleanup(0);
17913816Ssam 			}
18013816Ssam 		sp = getservbyname("printer", "tcp");
18113816Ssam 		if (sp == NULL) {
18216761Sralph 			syslog(LOG_ERR, "printer/tcp: unknown service");
18355474Sbostic 			mcleanup(0);
18413816Ssam 		}
18566248Sbostic 		memset(&sin, 0, sizeof(sin));
18613816Ssam 		sin.sin_family = AF_INET;
18713816Ssam 		sin.sin_port = sp->s_port;
18846911Sbostic 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
18916761Sralph 			syslog(LOG_ERR, "bind: %m");
19055474Sbostic 			mcleanup(0);
19113816Ssam 		}
19266248Sbostic 		FD_SET(finet, &defreadfds);
19314149Sralph 		listen(finet, 5);
19413816Ssam 	}
19512106Sralph 	/*
19614149Sralph 	 * Main loop: accept, do a request, continue.
19712106Sralph 	 */
198*66850Sbostic 	memset(&frominet, 0, sizeof(frominet));
199*66850Sbostic 	memset(&fromunix, 0, sizeof(fromunix));
20012106Sralph 	for (;;) {
20156123Selan 		int domain, nfds, s;
20256123Selan 		fd_set readfds;
20312106Sralph 
20456123Selan 		FD_COPY(&defreadfds, &readfds);
20513957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
20613957Ssam 		if (nfds <= 0) {
20716761Sralph 			if (nfds < 0 && errno != EINTR)
20816761Sralph 				syslog(LOG_WARNING, "select: %m");
20913957Ssam 			continue;
21013957Ssam 		}
21156123Selan 		if (FD_ISSET(funix, &readfds)) {
21213957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
21346911Sbostic 			s = accept(funix,
21446911Sbostic 			    (struct sockaddr *)&fromunix, &fromlen);
21556123Selan 		} else /* if (FD_ISSET(finet, &readfds)) */  {
21613957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
21746911Sbostic 			s = accept(finet,
21846911Sbostic 			    (struct sockaddr *)&frominet, &fromlen);
21913816Ssam 		}
22012106Sralph 		if (s < 0) {
22116761Sralph 			if (errno != EINTR)
22216761Sralph 				syslog(LOG_WARNING, "accept: %m");
22316761Sralph 			continue;
22412106Sralph 		}
22512106Sralph 		if (fork() == 0) {
22613147Ssam 			signal(SIGCHLD, SIG_IGN);
22714708Sralph 			signal(SIGHUP, SIG_IGN);
22814708Sralph 			signal(SIGINT, SIG_IGN);
22914708Sralph 			signal(SIGQUIT, SIG_IGN);
23014708Sralph 			signal(SIGTERM, SIG_IGN);
23113816Ssam 			(void) close(funix);
23213816Ssam 			(void) close(finet);
23313816Ssam 			dup2(s, 1);
23413816Ssam 			(void) close(s);
23547055Smckusick 			if (domain == AF_INET) {
23647055Smckusick 				from_remote = 1;
23713816Ssam 				chkhost(&frominet);
23847055Smckusick 			} else
23947055Smckusick 				from_remote = 0;
24013816Ssam 			doit();
24112106Sralph 			exit(0);
24212106Sralph 		}
24312106Sralph 		(void) close(s);
24412106Sralph 	}
24512106Sralph }
24612106Sralph 
24755474Sbostic static void
24855474Sbostic reapchild(signo)
24955474Sbostic 	int signo;
25012106Sralph {
25112106Sralph 	union wait status;
25212106Sralph 
25346911Sbostic 	while (wait3((int *)&status, WNOHANG, 0) > 0)
25412106Sralph 		;
25512106Sralph }
25612106Sralph 
25755474Sbostic static void
25855474Sbostic mcleanup(signo)
25955474Sbostic 	int signo;
26013816Ssam {
26114149Sralph 	if (lflag)
26216761Sralph 		syslog(LOG_INFO, "exiting");
26337968Sbostic 	unlink(_PATH_SOCKETNAME);
26413816Ssam 	exit(0);
26513816Ssam }
26613816Ssam 
26712106Sralph /*
26812106Sralph  * Stuff for handling job specifications
26912106Sralph  */
27012106Sralph char	*user[MAXUSERS];	/* users to process */
27112106Sralph int	users;			/* # of users in user array */
27212106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
27312106Sralph int	requests;		/* # of spool requests */
27412875Sralph char	*person;		/* name of person doing lprm */
27512106Sralph 
27654546Sleres char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
27754546Sleres char	cbuf[BUFSIZ];		/* command line buffer */
27816761Sralph char	*cmdnames[] = {
27912430Sralph 	"null",
28012430Sralph 	"printjob",
28112430Sralph 	"recvjob",
28212430Sralph 	"displayq short",
28312430Sralph 	"displayq long",
28412430Sralph 	"rmjob"
28512430Sralph };
28612106Sralph 
28755474Sbostic static void
28813816Ssam doit()
28912106Sralph {
29012106Sralph 	register char *cp;
29112106Sralph 	register int n;
29212106Sralph 
29312106Sralph 	for (;;) {
29412106Sralph 		cp = cbuf;
29512106Sralph 		do {
29613816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
29713816Ssam 				fatal("Command line too long");
29813816Ssam 			if ((n = read(1, cp, 1)) != 1) {
29912106Sralph 				if (n < 0)
30012106Sralph 					fatal("Lost connection");
30112106Sralph 				return;
30212106Sralph 			}
30313816Ssam 		} while (*cp++ != '\n');
30412106Sralph 		*--cp = '\0';
30512106Sralph 		cp = cbuf;
30616761Sralph 		if (lflag) {
30716761Sralph 			if (*cp >= '\1' && *cp <= '\5')
30816761Sralph 				syslog(LOG_INFO, "%s requests %s %s",
30916761Sralph 					from, cmdnames[*cp], cp+1);
31016761Sralph 			else
31116761Sralph 				syslog(LOG_INFO, "bad request (%d) from %s",
31216761Sralph 					*cp, from);
31312430Sralph 		}
31412106Sralph 		switch (*cp++) {
31512106Sralph 		case '\1':	/* check the queue and print any jobs there */
31612106Sralph 			printer = cp;
31712106Sralph 			printjob();
31812106Sralph 			break;
31912106Sralph 		case '\2':	/* receive files to be queued */
32047055Smckusick 			if (!from_remote) {
32147055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
32247055Smckusick 				exit(1);
32347055Smckusick 			}
32412106Sralph 			printer = cp;
32512106Sralph 			recvjob();
32612106Sralph 			break;
32712430Sralph 		case '\3':	/* display the queue (short form) */
32812430Sralph 		case '\4':	/* display the queue (long form) */
32912106Sralph 			printer = cp;
33012106Sralph 			while (*cp) {
33112106Sralph 				if (*cp != ' ') {
33212106Sralph 					cp++;
33312106Sralph 					continue;
33412106Sralph 				}
33512106Sralph 				*cp++ = '\0';
33612106Sralph 				while (isspace(*cp))
33712106Sralph 					cp++;
33812106Sralph 				if (*cp == '\0')
33912106Sralph 					break;
34012106Sralph 				if (isdigit(*cp)) {
34112106Sralph 					if (requests >= MAXREQUESTS)
34212106Sralph 						fatal("Too many requests");
34312106Sralph 					requ[requests++] = atoi(cp);
34412106Sralph 				} else {
34512106Sralph 					if (users >= MAXUSERS)
34612106Sralph 						fatal("Too many users");
34712106Sralph 					user[users++] = cp;
34812106Sralph 				}
34912106Sralph 			}
35012106Sralph 			displayq(cbuf[0] - '\3');
35112106Sralph 			exit(0);
35212106Sralph 		case '\5':	/* remove a job from the queue */
35347055Smckusick 			if (!from_remote) {
35447055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
35547055Smckusick 				exit(1);
35647055Smckusick 			}
35712106Sralph 			printer = cp;
35812106Sralph 			while (*cp && *cp != ' ')
35912106Sralph 				cp++;
36012106Sralph 			if (!*cp)
36112106Sralph 				break;
36212106Sralph 			*cp++ = '\0';
36312106Sralph 			person = cp;
36412106Sralph 			while (*cp) {
36512106Sralph 				if (*cp != ' ') {
36612106Sralph 					cp++;
36712106Sralph 					continue;
36812106Sralph 				}
36912106Sralph 				*cp++ = '\0';
37012106Sralph 				while (isspace(*cp))
37112106Sralph 					cp++;
37212106Sralph 				if (*cp == '\0')
37312106Sralph 					break;
37412106Sralph 				if (isdigit(*cp)) {
37512106Sralph 					if (requests >= MAXREQUESTS)
37612106Sralph 						fatal("Too many requests");
37712106Sralph 					requ[requests++] = atoi(cp);
37812106Sralph 				} else {
37912106Sralph 					if (users >= MAXUSERS)
38012106Sralph 						fatal("Too many users");
38112106Sralph 					user[users++] = cp;
38212106Sralph 				}
38312106Sralph 			}
38412106Sralph 			rmjob();
38512106Sralph 			break;
38612106Sralph 		}
38712106Sralph 		fatal("Illegal service request");
38812106Sralph 	}
38912106Sralph }
39012106Sralph 
39112106Sralph /*
39212430Sralph  * Make a pass through the printcap database and start printing any
39312430Sralph  * files left from the last time the machine went down.
39412430Sralph  */
39555474Sbostic static void
39612430Sralph startup()
39712430Sralph {
39856123Selan 	char *buf;
39912430Sralph 	register char *cp;
40012430Sralph 	int pid;
40112430Sralph 
40212430Sralph 	/*
40312430Sralph 	 * Restart the daemons.
40412430Sralph 	 */
40556123Selan 	while (cgetnext(&buf, printcapdb) > 0) {
40612430Sralph 		for (cp = buf; *cp; cp++)
40712430Sralph 			if (*cp == '|' || *cp == ':') {
40812430Sralph 				*cp = '\0';
40912430Sralph 				break;
41012430Sralph 			}
41112430Sralph 		if ((pid = fork()) < 0) {
41216761Sralph 			syslog(LOG_WARNING, "startup: cannot fork");
41355474Sbostic 			mcleanup(0);
41412430Sralph 		}
41512430Sralph 		if (!pid) {
41665315Sbostic 			printer = buf;
41756123Selan 			cgetclose();
41812430Sralph 			printjob();
41912430Sralph 		}
42012430Sralph 	}
42112430Sralph }
42212430Sralph 
42327746Sbloom #define DUMMY ":nobody::"
42427746Sbloom 
42512430Sralph /*
42612430Sralph  * Check to see if the from host has access to the line printer.
42712430Sralph  */
42855474Sbostic static void
42913816Ssam chkhost(f)
43013816Ssam 	struct sockaddr_in *f;
43112430Sralph {
43213816Ssam 	register struct hostent *hp;
43312430Sralph 	register FILE *hostf;
43415551Sralph 	int first = 1;
43513816Ssam 	extern char *inet_ntoa();
43612430Sralph 
43713816Ssam 	f->sin_port = ntohs(f->sin_port);
43813816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
43913816Ssam 		fatal("Malformed from address");
44054546Sleres 
44154546Sleres 	/* Need real hostname for temporary filenames */
44246911Sbostic 	hp = gethostbyaddr((char *)&f->sin_addr,
44346911Sbostic 	    sizeof(struct in_addr), f->sin_family);
44454546Sleres 	if (hp == NULL)
44513816Ssam 		fatal("Host name for your address (%s) unknown",
44613816Ssam 			inet_ntoa(f->sin_addr));
44713816Ssam 
44854546Sleres 	(void) strncpy(fromb, hp->h_name, sizeof(fromb));
44954546Sleres 	from[sizeof(fromb) - 1] = '\0';
45013816Ssam 	from = fromb;
45112734Sralph 
45237968Sbostic 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
45315551Sralph again:
45415551Sralph 	if (hostf) {
45554546Sleres 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
45654546Sleres 		    DUMMY, DUMMY) == 0) {
45727746Sbloom 			(void) fclose(hostf);
45827746Sbloom 			return;
45912430Sralph 		}
46015551Sralph 		(void) fclose(hostf);
46112430Sralph 	}
46215551Sralph 	if (first == 1) {
46315551Sralph 		first = 0;
46437968Sbostic 		hostf = fopen(_PATH_HOSTSLPD, "r");
46515551Sralph 		goto again;
46615551Sralph 	}
46713816Ssam 	fatal("Your host does not have line printer access");
46854546Sleres 	/*NOTREACHED*/
46912430Sralph }
47055474Sbostic 
47155474Sbostic 
47255474Sbostic 
47355474Sbostic 
47455474Sbostic 
47555474Sbostic 
47655474Sbostic 
47755474Sbostic 
47855474Sbostic 
47955474Sbostic 
48055474Sbostic 
48155474Sbostic 
482