Home | History | Annotate | Line # | Download | only in lpd
lpd.c revision 1.7
      1 /*	$NetBSD: lpd.c,v 1.7 1996/04/24 14:54:06 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #ifndef lint
     38 static char copyright[] =
     39 "@(#) Copyright (c) 1983, 1993, 1994\n\
     40 	The Regents of the University of California.  All rights reserved.\n";
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 static char sccsid[] = "@(#)lpd.c	8.4 (Berkeley) 4/17/94";
     45 #endif /* not lint */
     46 
     47 /*
     48  * lpd -- line printer daemon.
     49  *
     50  * Listen for a connection and perform the requested operation.
     51  * Operations are:
     52  *	\1printer\n
     53  *		check the queue for jobs and print any found.
     54  *	\2printer\n
     55  *		receive a job from another machine and queue it.
     56  *	\3printer [users ...] [jobs ...]\n
     57  *		return the current state of the queue (short form).
     58  *	\4printer [users ...] [jobs ...]\n
     59  *		return the current state of the queue (long form).
     60  *	\5printer person [users ...] [jobs ...]\n
     61  *		remove jobs from the queue.
     62  *
     63  * Strategy to maintain protected spooling area:
     64  *	1. Spooling area is writable only by daemon and spooling group
     65  *	2. lpr runs setuid root and setgrp spooling group; it uses
     66  *	   root to access any file it wants (verifying things before
     67  *	   with an access call) and group id to know how it should
     68  *	   set up ownership of files in the spooling area.
     69  *	3. Files in spooling area are owned by root, group spooling
     70  *	   group, with mode 660.
     71  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
     72  *	   access files and printer.  Users can't get to anything
     73  *	   w/o help of lpq and lprm programs.
     74  */
     75 
     76 #include <sys/param.h>
     77 #include <sys/wait.h>
     78 #include <sys/types.h>
     79 #include <sys/socket.h>
     80 #include <sys/un.h>
     81 #include <sys/stat.h>
     82 #include <netinet/in.h>
     83 
     84 #include <netdb.h>
     85 #include <unistd.h>
     86 #include <syslog.h>
     87 #include <signal.h>
     88 #include <errno.h>
     89 #include <fcntl.h>
     90 #include <dirent.h>
     91 #include <stdio.h>
     92 #include <stdlib.h>
     93 #include <string.h>
     94 #include <ctype.h>
     95 #include "lp.h"
     96 #include "lp.local.h"
     97 #include "pathnames.h"
     98 #include "extern.h"
     99 
    100 int	lflag;				/* log requests flag */
    101 int	from_remote;			/* from remote socket */
    102 
    103 static void       reapchild __P((int));
    104 static void       mcleanup __P((int));
    105 static void       doit __P((void));
    106 static void       startup __P((void));
    107 static void       chkhost __P((struct sockaddr_in *));
    108 
    109 uid_t	uid, euid;
    110 
    111 int
    112 main(argc, argv)
    113 	int argc;
    114 	char **argv;
    115 {
    116 	int f, funix, finet, options, fromlen;
    117 	fd_set defreadfds;
    118 	struct sockaddr_un un, fromunix;
    119 	struct sockaddr_in sin, frominet;
    120 	int omask, lfd;
    121 
    122 	euid = geteuid();	/* these shouldn't be different */
    123 	uid = getuid();
    124 	options = 0;
    125 	gethostname(host, sizeof(host));
    126 	name = argv[0];
    127 
    128 	while (--argc > 0) {
    129 		argv++;
    130 		if (argv[0][0] == '-')
    131 			switch (argv[0][1]) {
    132 			case 'd':
    133 				options |= SO_DEBUG;
    134 				break;
    135 			case 'l':
    136 				lflag++;
    137 				break;
    138 			}
    139 	}
    140 
    141 #ifndef DEBUG
    142 	/*
    143 	 * Set up standard environment by detaching from the parent.
    144 	 */
    145 	daemon(0, 0);
    146 #endif
    147 
    148 	openlog("lpd", LOG_PID, LOG_LPR);
    149 	syslog(LOG_INFO, "restarted");
    150 	(void) umask(0);
    151 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
    152 	if (lfd < 0) {
    153 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    154 		exit(1);
    155 	}
    156 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
    157 		if (errno == EWOULDBLOCK)	/* active deamon present */
    158 			exit(0);
    159 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    160 		exit(1);
    161 	}
    162 	ftruncate(lfd, 0);
    163 	/*
    164 	 * write process id for others to know
    165 	 */
    166 	sprintf(line, "%u\n", getpid());
    167 	f = strlen(line);
    168 	if (write(lfd, line, f) != f) {
    169 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    170 		exit(1);
    171 	}
    172 	signal(SIGCHLD, reapchild);
    173 	/*
    174 	 * Restart all the printers.
    175 	 */
    176 	startup();
    177 	(void) unlink(_PATH_SOCKETNAME);
    178 	funix = socket(AF_UNIX, SOCK_STREAM, 0);
    179 	if (funix < 0) {
    180 		syslog(LOG_ERR, "socket: %m");
    181 		exit(1);
    182 	}
    183 #define	mask(s)	(1 << ((s) - 1))
    184 	omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
    185 	signal(SIGHUP, mcleanup);
    186 	signal(SIGINT, mcleanup);
    187 	signal(SIGQUIT, mcleanup);
    188 	signal(SIGTERM, mcleanup);
    189 	memset(&un, 0, sizeof(un));
    190 	un.sun_family = AF_UNIX;
    191 	strcpy(un.sun_path, _PATH_SOCKETNAME);
    192 #ifndef SUN_LEN
    193 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
    194 #endif
    195 	if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
    196 		syslog(LOG_ERR, "ubind: %m");
    197 		exit(1);
    198 	}
    199 	sigsetmask(omask);
    200 	FD_ZERO(&defreadfds);
    201 	FD_SET(funix, &defreadfds);
    202 	listen(funix, 5);
    203 	finet = socket(AF_INET, SOCK_STREAM, 0);
    204 	if (finet >= 0) {
    205 		struct servent *sp;
    206 
    207 		if (options & SO_DEBUG)
    208 			if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
    209 				syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
    210 				mcleanup(0);
    211 			}
    212 		sp = getservbyname("printer", "tcp");
    213 		if (sp == NULL) {
    214 			syslog(LOG_ERR, "printer/tcp: unknown service");
    215 			mcleanup(0);
    216 		}
    217 		memset(&sin, 0, sizeof(sin));
    218 		sin.sin_family = AF_INET;
    219 		sin.sin_port = sp->s_port;
    220 		if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    221 			syslog(LOG_ERR, "bind: %m");
    222 			mcleanup(0);
    223 		}
    224 		FD_SET(finet, &defreadfds);
    225 		listen(finet, 5);
    226 	}
    227 	/*
    228 	 * Main loop: accept, do a request, continue.
    229 	 */
    230 	memset(&frominet, 0, sizeof(frominet));
    231 	memset(&fromunix, 0, sizeof(fromunix));
    232 	for (;;) {
    233 		int domain, nfds, s;
    234 		fd_set readfds;
    235 
    236 		FD_COPY(&defreadfds, &readfds);
    237 		nfds = select(20, &readfds, 0, 0, 0);
    238 		if (nfds <= 0) {
    239 			if (nfds < 0 && errno != EINTR)
    240 				syslog(LOG_WARNING, "select: %m");
    241 			continue;
    242 		}
    243 		if (FD_ISSET(funix, &readfds)) {
    244 			domain = AF_UNIX, fromlen = sizeof(fromunix);
    245 			s = accept(funix,
    246 			    (struct sockaddr *)&fromunix, &fromlen);
    247 		} else /* if (FD_ISSET(finet, &readfds)) */  {
    248 			domain = AF_INET, fromlen = sizeof(frominet);
    249 			s = accept(finet,
    250 			    (struct sockaddr *)&frominet, &fromlen);
    251 		}
    252 		if (s < 0) {
    253 			if (errno != EINTR)
    254 				syslog(LOG_WARNING, "accept: %m");
    255 			continue;
    256 		}
    257 		if (fork() == 0) {
    258 			signal(SIGCHLD, SIG_IGN);
    259 			signal(SIGHUP, SIG_IGN);
    260 			signal(SIGINT, SIG_IGN);
    261 			signal(SIGQUIT, SIG_IGN);
    262 			signal(SIGTERM, SIG_IGN);
    263 			(void) close(funix);
    264 			(void) close(finet);
    265 			dup2(s, 1);
    266 			(void) close(s);
    267 			if (domain == AF_INET) {
    268 				from_remote = 1;
    269 				chkhost(&frominet);
    270 			} else
    271 				from_remote = 0;
    272 			doit();
    273 			exit(0);
    274 		}
    275 		(void) close(s);
    276 	}
    277 }
    278 
    279 static void
    280 reapchild(signo)
    281 	int signo;
    282 {
    283 	union wait status;
    284 
    285 	while (wait3((int *)&status, WNOHANG, 0) > 0)
    286 		;
    287 }
    288 
    289 static void
    290 mcleanup(signo)
    291 	int signo;
    292 {
    293 	if (lflag)
    294 		syslog(LOG_INFO, "exiting");
    295 	unlink(_PATH_SOCKETNAME);
    296 	exit(0);
    297 }
    298 
    299 /*
    300  * Stuff for handling job specifications
    301  */
    302 char	*user[MAXUSERS];	/* users to process */
    303 int	users;			/* # of users in user array */
    304 int	requ[MAXREQUESTS];	/* job number of spool entries */
    305 int	requests;		/* # of spool requests */
    306 char	*person;		/* name of person doing lprm */
    307 
    308 char	fromb[MAXHOSTNAMELEN];	/* buffer for client's machine name */
    309 char	cbuf[BUFSIZ];		/* command line buffer */
    310 char	*cmdnames[] = {
    311 	"null",
    312 	"printjob",
    313 	"recvjob",
    314 	"displayq short",
    315 	"displayq long",
    316 	"rmjob"
    317 };
    318 
    319 static void
    320 doit()
    321 {
    322 	register char *cp;
    323 	register int n;
    324 
    325 	for (;;) {
    326 		cp = cbuf;
    327 		do {
    328 			if (cp >= &cbuf[sizeof(cbuf) - 1])
    329 				fatal("Command line too long");
    330 			if ((n = read(1, cp, 1)) != 1) {
    331 				if (n < 0)
    332 					fatal("Lost connection");
    333 				return;
    334 			}
    335 		} while (*cp++ != '\n');
    336 		*--cp = '\0';
    337 		cp = cbuf;
    338 		if (lflag) {
    339 			if (*cp >= '\1' && *cp <= '\5')
    340 				syslog(LOG_INFO, "%s requests %s %s",
    341 					from, cmdnames[*cp], cp+1);
    342 			else
    343 				syslog(LOG_INFO, "bad request (%d) from %s",
    344 					*cp, from);
    345 		}
    346 		switch (*cp++) {
    347 		case '\1':	/* check the queue and print any jobs there */
    348 			printer = cp;
    349 			printjob();
    350 			break;
    351 		case '\2':	/* receive files to be queued */
    352 			if (!from_remote) {
    353 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    354 				exit(1);
    355 			}
    356 			printer = cp;
    357 			recvjob();
    358 			break;
    359 		case '\3':	/* display the queue (short form) */
    360 		case '\4':	/* display the queue (long form) */
    361 			printer = cp;
    362 			while (*cp) {
    363 				if (*cp != ' ') {
    364 					cp++;
    365 					continue;
    366 				}
    367 				*cp++ = '\0';
    368 				while (isspace(*cp))
    369 					cp++;
    370 				if (*cp == '\0')
    371 					break;
    372 				if (isdigit(*cp)) {
    373 					if (requests >= MAXREQUESTS)
    374 						fatal("Too many requests");
    375 					requ[requests++] = atoi(cp);
    376 				} else {
    377 					if (users >= MAXUSERS)
    378 						fatal("Too many users");
    379 					user[users++] = cp;
    380 				}
    381 			}
    382 			displayq(cbuf[0] - '\3');
    383 			exit(0);
    384 		case '\5':	/* remove a job from the queue */
    385 			if (!from_remote) {
    386 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    387 				exit(1);
    388 			}
    389 			printer = cp;
    390 			while (*cp && *cp != ' ')
    391 				cp++;
    392 			if (!*cp)
    393 				break;
    394 			*cp++ = '\0';
    395 			person = cp;
    396 			while (*cp) {
    397 				if (*cp != ' ') {
    398 					cp++;
    399 					continue;
    400 				}
    401 				*cp++ = '\0';
    402 				while (isspace(*cp))
    403 					cp++;
    404 				if (*cp == '\0')
    405 					break;
    406 				if (isdigit(*cp)) {
    407 					if (requests >= MAXREQUESTS)
    408 						fatal("Too many requests");
    409 					requ[requests++] = atoi(cp);
    410 				} else {
    411 					if (users >= MAXUSERS)
    412 						fatal("Too many users");
    413 					user[users++] = cp;
    414 				}
    415 			}
    416 			rmjob();
    417 			break;
    418 		}
    419 		fatal("Illegal service request");
    420 	}
    421 }
    422 
    423 /*
    424  * Make a pass through the printcap database and start printing any
    425  * files left from the last time the machine went down.
    426  */
    427 static void
    428 startup()
    429 {
    430 	char *buf;
    431 	register char *cp;
    432 	int pid;
    433 
    434 	/*
    435 	 * Restart the daemons.
    436 	 */
    437 	while (cgetnext(&buf, printcapdb) > 0) {
    438 		for (cp = buf; *cp; cp++)
    439 			if (*cp == '|' || *cp == ':') {
    440 				*cp = '\0';
    441 				break;
    442 			}
    443 		if ((pid = fork()) < 0) {
    444 			syslog(LOG_WARNING, "startup: cannot fork");
    445 			mcleanup(0);
    446 		}
    447 		if (!pid) {
    448 			printer = buf;
    449 			cgetclose();
    450 			printjob();
    451 		}
    452 	}
    453 }
    454 
    455 #define DUMMY ":nobody::"
    456 
    457 /*
    458  * Check to see if the from host has access to the line printer.
    459  */
    460 static void
    461 chkhost(f)
    462 	struct sockaddr_in *f;
    463 {
    464 	register struct hostent *hp;
    465 	register FILE *hostf;
    466 	int first = 1;
    467 	extern char *inet_ntoa();
    468 
    469 	f->sin_port = ntohs(f->sin_port);
    470 	if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
    471 		fatal("Malformed from address");
    472 
    473 	/* Need real hostname for temporary filenames */
    474 	hp = gethostbyaddr((char *)&f->sin_addr,
    475 	    sizeof(struct in_addr), f->sin_family);
    476 	if (hp == NULL)
    477 		fatal("Host name for your address (%s) unknown",
    478 			inet_ntoa(f->sin_addr));
    479 
    480 	(void) strncpy(fromb, hp->h_name, sizeof(fromb));
    481 	from[sizeof(fromb) - 1] = '\0';
    482 	from = fromb;
    483 
    484 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
    485 again:
    486 	if (hostf) {
    487 		if (__ivaliduser(hostf, f->sin_addr.s_addr,
    488 		    DUMMY, DUMMY) == 0) {
    489 			(void) fclose(hostf);
    490 			return;
    491 		}
    492 		(void) fclose(hostf);
    493 	}
    494 	if (first == 1) {
    495 		first = 0;
    496 		hostf = fopen(_PATH_HOSTSLPD, "r");
    497 		goto again;
    498 	}
    499 	fatal("Your host does not have line printer access");
    500 	/*NOTREACHED*/
    501 }
    502