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