xref: /csrg-svn/usr.sbin/lpr/lpd/lpd.c (revision 56123)
122429Sdist /*
222429Sdist  * Copyright (c) 1983 Regents of the University of California.
334203Sbostic  * All rights reserved.
434203Sbostic  *
5*56123Selan  * Redistribution and use in source and binary forms, with or without
6*56123Selan  * modification, are permitted provided that the following conditions
7*56123Selan  * are met:
8*56123Selan  * 1. Redistributions of source code must retain the above copyright
9*56123Selan  *    notice, this list of conditions and the following disclaimer.
10*56123Selan  * 2. Redistributions in binary form must reproduce the above copyright
11*56123Selan  *    notice, this list of conditions and the following disclaimer in the
12*56123Selan  *    documentation and/or other materials provided with the distribution.
13*56123Selan  * 3. All advertising materials mentioning features or use of this software
14*56123Selan  *    must display the following acknowledgement:
15*56123Selan  *	This product includes software developed by the University of
16*56123Selan  *	California, Berkeley and its contributors.
17*56123Selan  * 4. Neither the name of the University nor the names of its contributors
18*56123Selan  *    may be used to endorse or promote products derived from this software
19*56123Selan  *    without specific prior written permission.
20*56123Selan  *
21*56123Selan  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*56123Selan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*56123Selan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*56123Selan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*56123Selan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*56123Selan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*56123Selan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*56123Selan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*56123Selan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*56123Selan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*56123Selan  * SUCH DAMAGE.
3222429Sdist  */
3322429Sdist 
3413957Ssam #ifndef lint
3522429Sdist char copyright[] =
3622429Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
3722429Sdist  All rights reserved.\n";
3834203Sbostic #endif /* not lint */
3913957Ssam 
4022429Sdist #ifndef lint
41*56123Selan static char sccsid[] = "@(#)lpd.c	5.16 (Berkeley) 8/6/92";
4234203Sbostic #endif /* not lint */
4322429Sdist 
4412106Sralph /*
4512106Sralph  * lpd -- line printer daemon.
4612106Sralph  *
4712106Sralph  * Listen for a connection and perform the requested operation.
4812106Sralph  * Operations are:
4912106Sralph  *	\1printer\n
5012106Sralph  *		check the queue for jobs and print any found.
5112106Sralph  *	\2printer\n
5212106Sralph  *		receive a job from another machine and queue it.
5312106Sralph  *	\3printer [users ...] [jobs ...]\n
5412106Sralph  *		return the current state of the queue (short form).
5512106Sralph  *	\4printer [users ...] [jobs ...]\n
5612106Sralph  *		return the current state of the queue (long form).
5712106Sralph  *	\5printer person [users ...] [jobs ...]\n
5812106Sralph  *		remove jobs from the queue.
5912106Sralph  *
6012106Sralph  * Strategy to maintain protected spooling area:
6112106Sralph  *	1. Spooling area is writable only by daemon and spooling group
6212106Sralph  *	2. lpr runs setuid root and setgrp spooling group; it uses
6312106Sralph  *	   root to access any file it wants (verifying things before
6412106Sralph  *	   with an access call) and group id to know how it should
6512106Sralph  *	   set up ownership of files in the spooling area.
6612430Sralph  *	3. Files in spooling area are owned by root, group spooling
6712106Sralph  *	   group, with mode 660.
6812106Sralph  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
6912106Sralph  *	   access files and printer.  Users can't get to anything
7012106Sralph  *	   w/o help of lpq and lprm programs.
7112106Sralph  */
7212106Sralph 
7355474Sbostic #include <sys/param.h>
7455474Sbostic #include <sys/wait.h>
75*56123Selan #include <sys/types.h>
7655474Sbostic #include <sys/socket.h>
7755474Sbostic #include <sys/un.h>
78*56123Selan #include <sys/stat.h>
7955474Sbostic #include <netinet/in.h>
80*56123Selan 
8155474Sbostic #include <netdb.h>
82*56123Selan #include <unistd.h>
8355474Sbostic #include <syslog.h>
8455474Sbostic #include <signal.h>
8555474Sbostic #include <errno.h>
8655474Sbostic #include <fcntl.h>
8755474Sbostic #include <dirent.h>
8855474Sbostic #include <stdio.h>
89*56123Selan #include <stdlib.h>
90*56123Selan #include <string.h>
91*56123Selan #include <ctype.h>
9212106Sralph #include "lp.h"
9355474Sbostic #include "lp.local.h"
9437968Sbostic #include "pathnames.h"
9555474Sbostic #include "extern.h"
9612106Sralph 
9716761Sralph int	lflag;				/* log requests flag */
9847055Smckusick int	from_remote;			/* from remote socket */
9912875Sralph 
10055474Sbostic static void       reapchild __P((int));
10155474Sbostic static void       mcleanup __P((int));
10255474Sbostic static void       doit __P((void));
10355474Sbostic static void       startup __P((void));
10455474Sbostic static void       chkhost __P((struct sockaddr_in *));
10512106Sralph 
10655474Sbostic int
10712106Sralph main(argc, argv)
10812106Sralph 	int argc;
10912106Sralph 	char **argv;
11012106Sralph {
111*56123Selan 	int f, funix, finet, options, fromlen;
112*56123Selan 	fd_set defreadfds;
11350886Storek 	struct sockaddr_un un, fromunix;
11413816Ssam 	struct sockaddr_in sin, frominet;
11514837Sralph 	int omask, lfd;
11612106Sralph 
117*56123Selan 	options = 0;
11812106Sralph 	gethostname(host, sizeof(host));
11912106Sralph 	name = argv[0];
12012106Sralph 
12112106Sralph 	while (--argc > 0) {
12212106Sralph 		argv++;
12312106Sralph 		if (argv[0][0] == '-')
12412106Sralph 			switch (argv[0][1]) {
12512106Sralph 			case 'd':
12612106Sralph 				options |= SO_DEBUG;
12712106Sralph 				break;
12812106Sralph 			case 'l':
12912430Sralph 				lflag++;
13012430Sralph 				break;
13112106Sralph 			}
13212106Sralph 	}
13314837Sralph 
13412106Sralph #ifndef DEBUG
13512106Sralph 	/*
13612106Sralph 	 * Set up standard environment by detaching from the parent.
13712106Sralph 	 */
13844708Skarels 	daemon(0, 0);
13912106Sralph #endif
14014837Sralph 
14125494Seric 	openlog("lpd", LOG_PID, LOG_LPR);
14212106Sralph 	(void) umask(0);
14337968Sbostic 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
14414837Sralph 	if (lfd < 0) {
14537968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
14614837Sralph 		exit(1);
14714837Sralph 	}
14814837Sralph 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
14914837Sralph 		if (errno == EWOULDBLOCK)	/* active deamon present */
15014837Sralph 			exit(0);
15137968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
15214837Sralph 		exit(1);
15314837Sralph 	}
15414837Sralph 	ftruncate(lfd, 0);
15512875Sralph 	/*
15614837Sralph 	 * write process id for others to know
15714837Sralph 	 */
15814837Sralph 	sprintf(line, "%u\n", getpid());
15914837Sralph 	f = strlen(line);
16014837Sralph 	if (write(lfd, line, f) != f) {
16137968Sbostic 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
16214837Sralph 		exit(1);
16314837Sralph 	}
16414837Sralph 	signal(SIGCHLD, reapchild);
16514837Sralph 	/*
16612875Sralph 	 * Restart all the printers.
16712875Sralph 	 */
16812875Sralph 	startup();
16937968Sbostic 	(void) unlink(_PATH_SOCKETNAME);
17013816Ssam 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
17113816Ssam 	if (funix < 0) {
17216761Sralph 		syslog(LOG_ERR, "socket: %m");
17312106Sralph 		exit(1);
17412106Sralph 	}
17514149Sralph #define	mask(s)	(1 << ((s) - 1))
17614149Sralph 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
17716761Sralph 	signal(SIGHUP, mcleanup);
17816761Sralph 	signal(SIGINT, mcleanup);
17916761Sralph 	signal(SIGQUIT, mcleanup);
18016761Sralph 	signal(SIGTERM, mcleanup);
18150886Storek 	un.sun_family = AF_UNIX;
18250886Storek 	strcpy(un.sun_path, _PATH_SOCKETNAME);
18346911Sbostic 	if (bind(funix,
18450886Storek 	     (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) {
18516761Sralph 		syslog(LOG_ERR, "ubind: %m");
18612106Sralph 		exit(1);
18712106Sralph 	}
18813816Ssam 	sigsetmask(omask);
189*56123Selan 	FD_ZERO(&defreadfds);
190*56123Selan 	FD_SET(funix, &defreadfds);
19114149Sralph 	listen(funix, 5);
19213816Ssam 	finet = socket(AF_INET, SOCK_STREAM, 0);
19313816Ssam 	if (finet >= 0) {
19413816Ssam 		struct servent *sp;
19513816Ssam 
19613816Ssam 		if (options & SO_DEBUG)
19713816Ssam 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
19816761Sralph 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
19955474Sbostic 				mcleanup(0);
20013816Ssam 			}
20113816Ssam 		sp = getservbyname("printer", "tcp");
20213816Ssam 		if (sp == NULL) {
20316761Sralph 			syslog(LOG_ERR, "printer/tcp: unknown service");
20455474Sbostic 			mcleanup(0);
20513816Ssam 		}
20613816Ssam 		sin.sin_family = AF_INET;
20713816Ssam 		sin.sin_port = sp->s_port;
20846911Sbostic 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
20916761Sralph 			syslog(LOG_ERR, "bind: %m");
21055474Sbostic 			mcleanup(0);
21113816Ssam 		}
212*56123Selan 		FD_SET(finet, &defreadfds);
21314149Sralph 		listen(finet, 5);
21413816Ssam 	}
21512106Sralph 	/*
21614149Sralph 	 * Main loop: accept, do a request, continue.
21712106Sralph 	 */
21812106Sralph 	for (;;) {
219*56123Selan 		int domain, nfds, s;
220*56123Selan 		fd_set readfds;
22112106Sralph 
222*56123Selan 		FD_COPY(&defreadfds, &readfds);
22313957Ssam 		nfds = select(20, &readfds, 0, 0, 0);
22413957Ssam 		if (nfds <= 0) {
22516761Sralph 			if (nfds < 0 && errno != EINTR)
22616761Sralph 				syslog(LOG_WARNING, "select: %m");
22713957Ssam 			continue;
22813957Ssam 		}
229*56123Selan 		if (FD_ISSET(funix, &readfds)) {
23013957Ssam 			domain = AF_UNIX, fromlen = sizeof(fromunix);
23146911Sbostic 			s = accept(funix,
23246911Sbostic 			    (struct sockaddr *)&fromunix, &fromlen);
233*56123Selan 		} else /* if (FD_ISSET(finet, &readfds)) */  {
23413957Ssam 			domain = AF_INET, fromlen = sizeof(frominet);
23546911Sbostic 			s = accept(finet,
23646911Sbostic 			    (struct sockaddr *)&frominet, &fromlen);
23713816Ssam 		}
23812106Sralph 		if (s < 0) {
23916761Sralph 			if (errno != EINTR)
24016761Sralph 				syslog(LOG_WARNING, "accept: %m");
24116761Sralph 			continue;
24212106Sralph 		}
24312106Sralph 		if (fork() == 0) {
24413147Ssam 			signal(SIGCHLD, SIG_IGN);
24514708Sralph 			signal(SIGHUP, SIG_IGN);
24614708Sralph 			signal(SIGINT, SIG_IGN);
24714708Sralph 			signal(SIGQUIT, SIG_IGN);
24814708Sralph 			signal(SIGTERM, SIG_IGN);
24913816Ssam 			(void) close(funix);
25013816Ssam 			(void) close(finet);
25113816Ssam 			dup2(s, 1);
25213816Ssam 			(void) close(s);
25347055Smckusick 			if (domain == AF_INET) {
25447055Smckusick 				from_remote = 1;
25513816Ssam 				chkhost(&frominet);
25647055Smckusick 			} else
25747055Smckusick 				from_remote = 0;
25813816Ssam 			doit();
25912106Sralph 			exit(0);
26012106Sralph 		}
26112106Sralph 		(void) close(s);
26212106Sralph 	}
26312106Sralph }
26412106Sralph 
26555474Sbostic static void
26655474Sbostic reapchild(signo)
26755474Sbostic 	int signo;
26812106Sralph {
26912106Sralph 	union wait status;
27012106Sralph 
27146911Sbostic 	while (wait3((int *)&status, WNOHANG, 0) > 0)
27212106Sralph 		;
27312106Sralph }
27412106Sralph 
27555474Sbostic static void
27655474Sbostic mcleanup(signo)
27755474Sbostic 	int signo;
27813816Ssam {
27914149Sralph 	if (lflag)
28016761Sralph 		syslog(LOG_INFO, "exiting");
28137968Sbostic 	unlink(_PATH_SOCKETNAME);
28213816Ssam 	exit(0);
28313816Ssam }
28413816Ssam 
28512106Sralph /*
28612106Sralph  * Stuff for handling job specifications
28712106Sralph  */
28812106Sralph char	*user[MAXUSERS];	/* users to process */
28912106Sralph int	users;			/* # of users in user array */
29012106Sralph int	requ[MAXREQUESTS];	/* job number of spool entries */
29112106Sralph int	requests;		/* # of spool requests */
29212875Sralph char	*person;		/* name of person doing lprm */
29312106Sralph 
29454546Sleres char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
29554546Sleres char	cbuf[BUFSIZ];		/* command line buffer */
29616761Sralph char	*cmdnames[] = {
29712430Sralph 	"null",
29812430Sralph 	"printjob",
29912430Sralph 	"recvjob",
30012430Sralph 	"displayq short",
30112430Sralph 	"displayq long",
30212430Sralph 	"rmjob"
30312430Sralph };
30412106Sralph 
30555474Sbostic static void
30613816Ssam doit()
30712106Sralph {
30812106Sralph 	register char *cp;
30912106Sralph 	register int n;
31012106Sralph 
31112106Sralph 	for (;;) {
31212106Sralph 		cp = cbuf;
31312106Sralph 		do {
31413816Ssam 			if (cp >= &cbuf[sizeof(cbuf) - 1])
31513816Ssam 				fatal("Command line too long");
31613816Ssam 			if ((n = read(1, cp, 1)) != 1) {
31712106Sralph 				if (n < 0)
31812106Sralph 					fatal("Lost connection");
31912106Sralph 				return;
32012106Sralph 			}
32113816Ssam 		} while (*cp++ != '\n');
32212106Sralph 		*--cp = '\0';
32312106Sralph 		cp = cbuf;
32416761Sralph 		if (lflag) {
32516761Sralph 			if (*cp >= '\1' && *cp <= '\5')
32616761Sralph 				syslog(LOG_INFO, "%s requests %s %s",
32716761Sralph 					from, cmdnames[*cp], cp+1);
32816761Sralph 			else
32916761Sralph 				syslog(LOG_INFO, "bad request (%d) from %s",
33016761Sralph 					*cp, from);
33112430Sralph 		}
33212106Sralph 		switch (*cp++) {
33312106Sralph 		case '\1':	/* check the queue and print any jobs there */
33412106Sralph 			printer = cp;
33512106Sralph 			printjob();
33612106Sralph 			break;
33712106Sralph 		case '\2':	/* receive files to be queued */
33847055Smckusick 			if (!from_remote) {
33947055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
34047055Smckusick 				exit(1);
34147055Smckusick 			}
34212106Sralph 			printer = cp;
34312106Sralph 			recvjob();
34412106Sralph 			break;
34512430Sralph 		case '\3':	/* display the queue (short form) */
34612430Sralph 		case '\4':	/* display the queue (long form) */
34712106Sralph 			printer = cp;
34812106Sralph 			while (*cp) {
34912106Sralph 				if (*cp != ' ') {
35012106Sralph 					cp++;
35112106Sralph 					continue;
35212106Sralph 				}
35312106Sralph 				*cp++ = '\0';
35412106Sralph 				while (isspace(*cp))
35512106Sralph 					cp++;
35612106Sralph 				if (*cp == '\0')
35712106Sralph 					break;
35812106Sralph 				if (isdigit(*cp)) {
35912106Sralph 					if (requests >= MAXREQUESTS)
36012106Sralph 						fatal("Too many requests");
36112106Sralph 					requ[requests++] = atoi(cp);
36212106Sralph 				} else {
36312106Sralph 					if (users >= MAXUSERS)
36412106Sralph 						fatal("Too many users");
36512106Sralph 					user[users++] = cp;
36612106Sralph 				}
36712106Sralph 			}
36812106Sralph 			displayq(cbuf[0] - '\3');
36912106Sralph 			exit(0);
37012106Sralph 		case '\5':	/* remove a job from the queue */
37147055Smckusick 			if (!from_remote) {
37247055Smckusick 				syslog(LOG_INFO, "illegal request (%d)", *cp);
37347055Smckusick 				exit(1);
37447055Smckusick 			}
37512106Sralph 			printer = cp;
37612106Sralph 			while (*cp && *cp != ' ')
37712106Sralph 				cp++;
37812106Sralph 			if (!*cp)
37912106Sralph 				break;
38012106Sralph 			*cp++ = '\0';
38112106Sralph 			person = cp;
38212106Sralph 			while (*cp) {
38312106Sralph 				if (*cp != ' ') {
38412106Sralph 					cp++;
38512106Sralph 					continue;
38612106Sralph 				}
38712106Sralph 				*cp++ = '\0';
38812106Sralph 				while (isspace(*cp))
38912106Sralph 					cp++;
39012106Sralph 				if (*cp == '\0')
39112106Sralph 					break;
39212106Sralph 				if (isdigit(*cp)) {
39312106Sralph 					if (requests >= MAXREQUESTS)
39412106Sralph 						fatal("Too many requests");
39512106Sralph 					requ[requests++] = atoi(cp);
39612106Sralph 				} else {
39712106Sralph 					if (users >= MAXUSERS)
39812106Sralph 						fatal("Too many users");
39912106Sralph 					user[users++] = cp;
40012106Sralph 				}
40112106Sralph 			}
40212106Sralph 			rmjob();
40312106Sralph 			break;
40412106Sralph 		}
40512106Sralph 		fatal("Illegal service request");
40612106Sralph 	}
40712106Sralph }
40812106Sralph 
40912106Sralph /*
41012430Sralph  * Make a pass through the printcap database and start printing any
41112430Sralph  * files left from the last time the machine went down.
41212430Sralph  */
41355474Sbostic static void
41412430Sralph startup()
41512430Sralph {
416*56123Selan 	char *buf;
41712430Sralph 	register char *cp;
41812430Sralph 	int pid;
41912430Sralph 
42012430Sralph 	/*
42112430Sralph 	 * Restart the daemons.
42212430Sralph 	 */
423*56123Selan 	while (cgetnext(&buf, printcapdb) > 0) {
424*56123Selan 		printer = buf;
42512430Sralph 		for (cp = buf; *cp; cp++)
42612430Sralph 			if (*cp == '|' || *cp == ':') {
42712430Sralph 				*cp = '\0';
42812430Sralph 				break;
42912430Sralph 			}
43012430Sralph 		if ((pid = fork()) < 0) {
43116761Sralph 			syslog(LOG_WARNING, "startup: cannot fork");
43255474Sbostic 			mcleanup(0);
43312430Sralph 		}
43412430Sralph 		if (!pid) {
435*56123Selan 			cgetclose();
43612430Sralph 			printjob();
43712430Sralph 		}
43812430Sralph 	}
43912430Sralph }
44012430Sralph 
44127746Sbloom #define DUMMY ":nobody::"
44227746Sbloom 
44312430Sralph /*
44412430Sralph  * Check to see if the from host has access to the line printer.
44512430Sralph  */
44655474Sbostic static void
44713816Ssam chkhost(f)
44813816Ssam 	struct sockaddr_in *f;
44912430Sralph {
45013816Ssam 	register struct hostent *hp;
45112430Sralph 	register FILE *hostf;
45215551Sralph 	int first = 1;
45313816Ssam 	extern char *inet_ntoa();
45412430Sralph 
45513816Ssam 	f->sin_port = ntohs(f->sin_port);
45613816Ssam 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
45713816Ssam 		fatal("Malformed from address");
45854546Sleres 
45954546Sleres 	/* Need real hostname for temporary filenames */
46046911Sbostic 	hp = gethostbyaddr((char *)&f->sin_addr,
46146911Sbostic 	    sizeof(struct in_addr), f->sin_family);
46254546Sleres 	if (hp == NULL)
46313816Ssam 		fatal("Host name for your address (%s) unknown",
46413816Ssam 			inet_ntoa(f->sin_addr));
46513816Ssam 
46654546Sleres 	(void) strncpy(fromb, hp->h_name, sizeof(fromb));
46754546Sleres 	from[sizeof(fromb) - 1] = '\0';
46813816Ssam 	from = fromb;
46912734Sralph 
47037968Sbostic 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
47115551Sralph again:
47215551Sralph 	if (hostf) {
47354546Sleres 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
47454546Sleres 		    DUMMY, DUMMY) == 0) {
47527746Sbloom 			(void) fclose(hostf);
47627746Sbloom 			return;
47712430Sralph 		}
47815551Sralph 		(void) fclose(hostf);
47912430Sralph 	}
48015551Sralph 	if (first == 1) {
48115551Sralph 		first = 0;
48237968Sbostic 		hostf = fopen(_PATH_HOSTSLPD, "r");
48315551Sralph 		goto again;
48415551Sralph 	}
48513816Ssam 	fatal("Your host does not have line printer access");
48654546Sleres 	/*NOTREACHED*/
48712430Sralph }
48855474Sbostic 
48955474Sbostic 
49055474Sbostic 
49155474Sbostic 
49255474Sbostic 
49355474Sbostic 
49455474Sbostic 
49555474Sbostic 
49655474Sbostic 
49755474Sbostic 
49855474Sbostic 
49955474Sbostic 
500