Home | History | Annotate | Line # | Download | only in lpd
lpd.c revision 1.39
      1 /*	$NetBSD: lpd.c,v 1.39 2002/09/19 20:08:58 mycroft 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 #include <sys/cdefs.h>
     38 
     39 #ifndef lint
     40 __COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
     41 	The Regents of the University of California.  All rights reserved.\n");
     42 #endif /* not lint */
     43 
     44 #ifndef lint
     45 #if 0
     46 static char sccsid[] = "@(#)lpd.c	8.7 (Berkeley) 5/10/95";
     47 #else
     48 __RCSID("$NetBSD: lpd.c,v 1.39 2002/09/19 20:08:58 mycroft Exp $");
     49 #endif
     50 #endif /* not lint */
     51 
     52 /*
     53  * lpd -- line printer daemon.
     54  *
     55  * Listen for a connection and perform the requested operation.
     56  * Operations are:
     57  *	\1printer\n
     58  *		check the queue for jobs and print any found.
     59  *	\2printer\n
     60  *		receive a job from another machine and queue it.
     61  *	\3printer [users ...] [jobs ...]\n
     62  *		return the current state of the queue (short form).
     63  *	\4printer [users ...] [jobs ...]\n
     64  *		return the current state of the queue (long form).
     65  *	\5printer person [users ...] [jobs ...]\n
     66  *		remove jobs from the queue.
     67  *
     68  * Strategy to maintain protected spooling area:
     69  *	1. Spooling area is writable only by daemon and spooling group
     70  *	2. lpr runs setuid root and setgrp spooling group; it uses
     71  *	   root to access any file it wants (verifying things before
     72  *	   with an access call) and group id to know how it should
     73  *	   set up ownership of files in the spooling area.
     74  *	3. Files in spooling area are owned by root, group spooling
     75  *	   group, with mode 660.
     76  *	4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
     77  *	   access files and printer.  Users can't get to anything
     78  *	   w/o help of lpq and lprm programs.
     79  */
     80 
     81 #include <sys/param.h>
     82 #include <sys/wait.h>
     83 #include <sys/types.h>
     84 #include <sys/socket.h>
     85 #include <sys/un.h>
     86 #include <sys/stat.h>
     87 #include <sys/file.h>
     88 #include <sys/poll.h>
     89 #include <netinet/in.h>
     90 
     91 #include <err.h>
     92 #include <netdb.h>
     93 #include <unistd.h>
     94 #include <syslog.h>
     95 #include <signal.h>
     96 #include <errno.h>
     97 #include <fcntl.h>
     98 #include <dirent.h>
     99 #include <stdarg.h>
    100 #include <stdio.h>
    101 #include <stdlib.h>
    102 #include <string.h>
    103 #include <ctype.h>
    104 #include <arpa/inet.h>
    105 
    106 #ifdef LIBWRAP
    107 #include <tcpd.h>
    108 #endif
    109 
    110 #include "lp.h"
    111 #include "lp.local.h"
    112 #include "pathnames.h"
    113 #include "extern.h"
    114 
    115 /* XXX from libc/net/rcmd.c */
    116 extern int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
    117 			   const char *, const char *);
    118 
    119 #ifdef LIBWRAP
    120 int allow_severity = LOG_AUTH|LOG_INFO;
    121 int deny_severity = LOG_AUTH|LOG_WARNING;
    122 #endif
    123 
    124 int	lflag;				/* log requests flag */
    125 int	rflag;				/* allow of for remote printers */
    126 int	sflag;				/* secure (no inet) flag */
    127 int	from_remote;			/* from remote socket */
    128 char	**blist;			/* list of addresses to bind(2) to */
    129 int	blist_size;
    130 int	blist_addrs;
    131 
    132 int			main(int, char **);
    133 static void		reapchild(int);
    134 static void		mcleanup(int);
    135 static void		doit(void);
    136 static void		startup(void);
    137 static void		chkhost(struct sockaddr *, int);
    138 static int		ckqueue(char *);
    139 static void		usage(void);
    140 static struct pollfd	*socksetup(int, int, const char *, int *);
    141 
    142 uid_t	uid, euid;
    143 int child_count;
    144 
    145 #define LPD_NOPORTCHK	0001		/* skip reserved-port check */
    146 
    147 int
    148 main(int argc, char **argv)
    149 {
    150 	struct sockaddr_un fromunix;
    151 	struct sockaddr_storage frominet;
    152 	sigset_t nmask, omask;
    153 	int lfd, errs, i, f, nfds;
    154 	struct pollfd *socks;
    155 	int child_max = 32;	/* more than enough to hose the system */
    156 	int options = 0, check_options = 0;
    157 	struct servent *sp;
    158 	const char *port = "printer";
    159 
    160 	euid = geteuid();	/* these shouldn't be different */
    161 	uid = getuid();
    162 	gethostname(host, sizeof(host));
    163 	host[sizeof(host) - 1] = '\0';
    164 	name = argv[0];
    165 
    166 	errs = 0;
    167 	while ((i = getopt(argc, argv, "b:dln:srw:W")) != -1)
    168 		switch (i) {
    169 		case 'b':
    170 			if (blist_addrs >= blist_size) {
    171 				blist_size += sizeof(char *) * 4;
    172 				if (blist == NULL)
    173 					blist = malloc(blist_size);
    174 				else
    175 					blist = realloc(blist, blist_size);
    176 				if (blist == NULL)
    177 					err(1, "cant allocate bind addr list");
    178 			}
    179 			blist[blist_addrs++] = strdup(optarg);
    180 			break;
    181 		case 'd':
    182 			options |= SO_DEBUG;
    183 			break;
    184 		case 'l':
    185 			lflag++;
    186 			break;
    187 		case 'n':
    188 			child_max = atoi(optarg);
    189 			if (child_max < 0 || child_max > 1024)
    190 				errx(1, "invalid number of children: %s",
    191 				    optarg);
    192 			break;
    193 		case 'r':
    194 			rflag++;
    195 			break;
    196 		case 's':
    197 			sflag++;
    198 			break;
    199 		case 'w':
    200 			wait_time = atoi(optarg);
    201 			if (wait_time < 0)
    202 				errx(1, "wait time must be postive: %s",
    203 				    optarg);
    204 			if (wait_time < 30)
    205 			    warnx("warning: wait time less than 30 seconds");
    206 			break;
    207 		case 'W':/* allow connections coming from a non-reserved port */
    208 			 /* (done by some lpr-implementations for MS-Windows) */
    209 			check_options |= LPD_NOPORTCHK;
    210 			break;
    211 		default:
    212 			errs++;
    213 		}
    214 	argc -= optind;
    215 	argv += optind;
    216 	if (errs)
    217 		usage();
    218 
    219 	switch (argc) {
    220 	case 1:
    221 		if ((i = atoi(argv[0])) == 0)
    222 			usage();
    223 		if (i < 0 || i > USHRT_MAX)
    224 			errx(1, "port # %d is invalid", i);
    225 
    226 		port = argv[0];
    227 		break;
    228 	case 0:
    229 		sp = getservbyname(port, "tcp");
    230 		if (sp == NULL)
    231 			errx(1, "%s/tcp: unknown service", port);
    232 		break;
    233 	default:
    234 		usage();
    235 	}
    236 
    237 #ifndef DEBUG
    238 	/*
    239 	 * Set up standard environment by detaching from the parent.
    240 	 */
    241 	daemon(0, 0);
    242 #endif
    243 
    244 	openlog("lpd", LOG_PID, LOG_LPR);
    245 	syslog(LOG_INFO, "restarted");
    246 	(void)umask(0);
    247 	lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
    248 	if (lfd < 0) {
    249 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    250 		exit(1);
    251 	}
    252 	if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
    253 		if (errno == EWOULDBLOCK)	/* active daemon present */
    254 			exit(0);
    255 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    256 		exit(1);
    257 	}
    258 	ftruncate(lfd, 0);
    259 	/*
    260 	 * write process id for others to know
    261 	 */
    262 	(void)snprintf(line, sizeof(line), "%u\n", getpid());
    263 	f = strlen(line);
    264 	if (write(lfd, line, f) != f) {
    265 		syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
    266 		exit(1);
    267 	}
    268 	signal(SIGCHLD, reapchild);
    269 	/*
    270 	 * Restart all the printers.
    271 	 */
    272 	startup();
    273 
    274 	sigemptyset(&nmask);
    275 	sigaddset(&nmask, SIGHUP);
    276 	sigaddset(&nmask, SIGINT);
    277 	sigaddset(&nmask, SIGQUIT);
    278 	sigaddset(&nmask, SIGTERM);
    279 	sigprocmask(SIG_BLOCK, &nmask, &omask);
    280 
    281 	signal(SIGHUP, mcleanup);
    282 	signal(SIGINT, mcleanup);
    283 	signal(SIGQUIT, mcleanup);
    284 	signal(SIGTERM, mcleanup);
    285 
    286 	socks = socksetup(PF_UNSPEC, options, port, &nfds);
    287 
    288 	sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
    289 
    290 	if (blist != NULL) {
    291 		for (i = 0; i < blist_addrs; i++)
    292 			free(blist[i]);
    293 		free(blist);
    294 	}
    295 
    296 	/*
    297 	 * Main loop: accept, do a request, continue.
    298 	 */
    299 	memset(&frominet, 0, sizeof(frominet));
    300 	memset(&fromunix, 0, sizeof(fromunix));
    301 	for (;;) {
    302 		int domain, rv, s, fromlen;
    303 		/* "short" so it overflows in about 2 hours */
    304 		struct timespec sleeptime = {10, 0};
    305 
    306 		while (child_max < child_count) {
    307 			syslog(LOG_WARNING,
    308 			    "too many children, sleeping for %ld seconds",
    309 				sleeptime.tv_sec);
    310 			nanosleep(&sleeptime, NULL);
    311 			sleeptime.tv_sec <<= 1;
    312 			if (sleeptime.tv_sec <= 0) {
    313 				syslog(LOG_CRIT, "sleeptime overflowed! help!");
    314 				sleeptime.tv_sec = 10;
    315 			}
    316 		}
    317 
    318 		rv = poll(socks, nfds, INFTIM);
    319 		if (rv <= 0) {
    320 			if (rv < 0 && errno != EINTR)
    321 				syslog(LOG_WARNING, "poll: %m");
    322 			continue;
    323 		}
    324                 for (i = 0; i < nfds; i++)
    325 			if (socks[i].revents & POLLIN) {
    326 				if (i == 0) {
    327 					domain = AF_LOCAL;
    328 					fromlen = sizeof(fromunix);
    329 					s = accept(socks[i].fd, (struct sockaddr *)&fromunix, &fromlen);
    330 				} else {
    331 					domain = AF_INET;
    332 					fromlen = sizeof(frominet);
    333 					s = accept(socks[i].fd, (struct sockaddr *)&frominet, &fromlen);
    334 				}
    335 				break;
    336 			}
    337 		if (s < 0) {
    338 			if (errno != EINTR)
    339 				syslog(LOG_WARNING, "accept: %m");
    340 			continue;
    341 		}
    342 
    343 		switch (fork()) {
    344 		case 0:
    345 			signal(SIGCHLD, SIG_IGN);
    346 			signal(SIGHUP, SIG_IGN);
    347 			signal(SIGINT, SIG_IGN);
    348 			signal(SIGQUIT, SIG_IGN);
    349 			signal(SIGTERM, SIG_IGN);
    350                        	for (i = 0; i < nfds; i++)
    351 				(void)close(socks[i].fd);
    352 			dup2(s, 1);
    353 			(void)close(s);
    354 			if (domain == AF_INET) {
    355 				/* for both AF_INET and AF_INET6 */
    356 				from_remote = 1;
    357 				chkhost((struct sockaddr *)&frominet, check_options);
    358 			} else
    359 				from_remote = 0;
    360 			doit();
    361 			exit(0);
    362 		case -1:
    363 			syslog(LOG_WARNING, "fork: %m, sleeping for 10 seconds...");
    364 			sleep(10);
    365 			continue;
    366 		default:
    367 			child_count++;
    368 		}
    369 		(void)close(s);
    370 	}
    371 }
    372 
    373 static void
    374 reapchild(int signo)
    375 {
    376 	union wait status;
    377 
    378 	while (wait3((int *)&status, WNOHANG, 0) > 0)
    379 		child_count--;
    380 }
    381 
    382 static void
    383 mcleanup(int signo)
    384 {
    385 	if (lflag)
    386 		syslog(LOG_INFO, "exiting");
    387 	unlink(_PATH_SOCKETNAME);
    388 	exit(0);
    389 }
    390 
    391 /*
    392  * Stuff for handling job specifications
    393  */
    394 char	*user[MAXUSERS];	/* users to process */
    395 int	users;			/* # of users in user array */
    396 int	requ[MAXREQUESTS];	/* job number of spool entries */
    397 int	requests;		/* # of spool requests */
    398 char	*person;		/* name of person doing lprm */
    399 
    400 char	fromb[NI_MAXHOST];	/* buffer for client's machine name */
    401 char	cbuf[BUFSIZ];		/* command line buffer */
    402 char	*cmdnames[] = {
    403 	"null",
    404 	"printjob",
    405 	"recvjob",
    406 	"displayq short",
    407 	"displayq long",
    408 	"rmjob"
    409 };
    410 
    411 static void
    412 doit(void)
    413 {
    414 	char *cp;
    415 	int n;
    416 
    417 	for (;;) {
    418 		cp = cbuf;
    419 		do {
    420 			if (cp >= &cbuf[sizeof(cbuf) - 1])
    421 				fatal("Command line too long");
    422 			if ((n = read(STDOUT_FILENO, cp, 1)) != 1) {
    423 				if (n < 0)
    424 					fatal("Lost connection");
    425 				return;
    426 			}
    427 		} while (*cp++ != '\n');
    428 		*--cp = '\0';
    429 		cp = cbuf;
    430 		if (lflag) {
    431 			if (*cp >= '\1' && *cp <= '\5') {
    432 				syslog(LOG_INFO, "%s requests %s %s",
    433 					from, cmdnames[(int)*cp], cp+1);
    434 				setproctitle("serving %s: %s %s", from,
    435 				    cmdnames[(int)*cp], cp+1);
    436 			}
    437 			else
    438 				syslog(LOG_INFO, "bad request (%d) from %s",
    439 					*cp, from);
    440 		}
    441 		switch (*cp++) {
    442 		case '\1':	/* check the queue and print any jobs there */
    443 			printer = cp;
    444 			if (*printer == '\0')
    445 				printer = DEFLP;
    446 			printjob();
    447 			break;
    448 		case '\2':	/* receive files to be queued */
    449 			if (!from_remote) {
    450 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    451 				exit(1);
    452 			}
    453 			printer = cp;
    454 			if (*printer == '\0')
    455 				printer = DEFLP;
    456 			recvjob();
    457 			break;
    458 		case '\3':	/* display the queue (short form) */
    459 		case '\4':	/* display the queue (long form) */
    460 			printer = cp;
    461 			if (*printer == '\0')
    462 				printer = DEFLP;
    463 			while (*cp) {
    464 				if (*cp != ' ') {
    465 					cp++;
    466 					continue;
    467 				}
    468 				*cp++ = '\0';
    469 				while (isspace(*cp))
    470 					cp++;
    471 				if (*cp == '\0')
    472 					break;
    473 				if (isdigit(*cp)) {
    474 					if (requests >= MAXREQUESTS)
    475 						fatal("Too many requests");
    476 					requ[requests++] = atoi(cp);
    477 				} else {
    478 					if (users >= MAXUSERS)
    479 						fatal("Too many users");
    480 					user[users++] = cp;
    481 				}
    482 			}
    483 			displayq(cbuf[0] - '\3');
    484 			exit(0);
    485 		case '\5':	/* remove a job from the queue */
    486 			if (!from_remote) {
    487 				syslog(LOG_INFO, "illegal request (%d)", *cp);
    488 				exit(1);
    489 			}
    490 			printer = cp;
    491 			if (*printer == '\0')
    492 				printer = DEFLP;
    493 			while (*cp && *cp != ' ')
    494 				cp++;
    495 			if (!*cp)
    496 				break;
    497 			*cp++ = '\0';
    498 			person = cp;
    499 			while (*cp) {
    500 				if (*cp != ' ') {
    501 					cp++;
    502 					continue;
    503 				}
    504 				*cp++ = '\0';
    505 				while (isspace(*cp))
    506 					cp++;
    507 				if (*cp == '\0')
    508 					break;
    509 				if (isdigit(*cp)) {
    510 					if (requests >= MAXREQUESTS)
    511 						fatal("Too many requests");
    512 					requ[requests++] = atoi(cp);
    513 				} else {
    514 					if (users >= MAXUSERS)
    515 						fatal("Too many users");
    516 					user[users++] = cp;
    517 				}
    518 			}
    519 			rmjob();
    520 			break;
    521 		}
    522 		fatal("Illegal service request");
    523 	}
    524 }
    525 
    526 /*
    527  * Make a pass through the printcap database and start printing any
    528  * files left from the last time the machine went down.
    529  */
    530 static void
    531 startup(void)
    532 {
    533 	char *buf;
    534 	char *cp;
    535 
    536 	/*
    537 	 * Restart the daemons.
    538 	 */
    539 	while (cgetnext(&buf, printcapdb) > 0) {
    540 		if (ckqueue(buf) <= 0) {
    541 			free(buf);
    542 			continue;	/* no work to do for this printer */
    543 		}
    544 		for (cp = buf; *cp; cp++)
    545 			if (*cp == '|' || *cp == ':') {
    546 				*cp = '\0';
    547 				break;
    548 			}
    549 		if (lflag)
    550 			syslog(LOG_INFO, "work for %s", buf);
    551 		switch (fork()) {
    552 		case -1:
    553 			syslog(LOG_WARNING, "startup: cannot fork");
    554 			mcleanup(0);
    555 		case 0:
    556 			printer = buf;
    557 			setproctitle("working on printer %s", printer);
    558 			cgetclose();
    559 			printjob();
    560 			/* NOTREACHED */
    561 		default:
    562 			child_count++;
    563 			free(buf);
    564 		}
    565 	}
    566 }
    567 
    568 /*
    569  * Make sure there's some work to do before forking off a child
    570  */
    571 static int
    572 ckqueue(char *cap)
    573 {
    574 	struct dirent *d;
    575 	DIR *dirp;
    576 	char *spooldir;
    577 
    578 	if (cgetstr(cap, "sd", &spooldir) == -1)
    579 		spooldir = _PATH_DEFSPOOL;
    580 	if ((dirp = opendir(spooldir)) == NULL)
    581 		return (-1);
    582 	while ((d = readdir(dirp)) != NULL) {
    583 		if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
    584 			continue;	/* daemon control files only */
    585 		closedir(dirp);
    586 		return (1);		/* found something */
    587 	}
    588 	closedir(dirp);
    589 	return (0);
    590 }
    591 
    592 #define DUMMY ":nobody::"
    593 
    594 /*
    595  * Check to see if the from host has access to the line printer.
    596  */
    597 static void
    598 chkhost(struct sockaddr *f, int check_opts)
    599 {
    600 	struct addrinfo hints, *res, *r;
    601 	FILE *hostf;
    602 	int good = 0;
    603 	char host[NI_MAXHOST], ip[NI_MAXHOST];
    604 	char serv[NI_MAXSERV];
    605 	int error;
    606 #ifdef LIBWRAP
    607 	struct request_info req;
    608 #endif
    609 
    610 	error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
    611 			    NI_NUMERICSERV);
    612 	if (error)
    613 		fatal("Malformed from address: %s", gai_strerror(error));
    614 
    615          if (!(check_opts & LPD_NOPORTCHK) &&
    616 	       atoi(serv) >= IPPORT_RESERVED)
    617 		fatal("Connect from invalid port (%s)", serv);
    618 
    619 	/* Need real hostname for temporary filenames */
    620 	error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    621 			    NI_NAMEREQD);
    622 	if (error) {
    623 		error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    624 				    NI_NUMERICHOST);
    625 		if (error)
    626 			fatal("Host name for your address unknown");
    627 		else
    628 			fatal("Host name for your address (%s) unknown", host);
    629 	}
    630 
    631 	(void)strncpy(fromb, host, sizeof(fromb) - 1);
    632 	fromb[sizeof(fromb) - 1] = '\0';
    633 	from = fromb;
    634 
    635 	/* need address in stringform for comparison (no DNS lookup here) */
    636 	error = getnameinfo(f, f->sa_len, host, sizeof(host), NULL, 0,
    637 			    NI_NUMERICHOST);
    638 	if (error)
    639 		fatal("Cannot print address");
    640 
    641 	/* Check for spoof, ala rlogind */
    642 	memset(&hints, 0, sizeof(hints));
    643 	hints.ai_family = PF_UNSPEC;
    644 	hints.ai_socktype = SOCK_DGRAM;	/*dummy*/
    645 	error = getaddrinfo(fromb, NULL, &hints, &res);
    646 	if (error) {
    647 		fatal("hostname for your address (%s) unknown: %s", host,
    648 		    gai_strerror(error));
    649 	}
    650 	good = 0;
    651 	for (r = res; good == 0 && r; r = r->ai_next) {
    652 		error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
    653 				    NULL, 0, NI_NUMERICHOST);
    654 		if (!error && !strcmp(host, ip))
    655 			good = 1;
    656 	}
    657 	if (res)
    658 		freeaddrinfo(res);
    659 	if (good == 0)
    660 		fatal("address for your hostname (%s) not matched", host);
    661 
    662 	setproctitle("serving %s", from);
    663 
    664 #ifdef LIBWRAP
    665 	request_init(&req, RQ_DAEMON, "lpd", RQ_CLIENT_SIN, f, NULL);
    666 	fromhost(&req);
    667 	if (!hosts_access(&req))
    668 		goto denied;
    669 #endif
    670 
    671 	hostf = fopen(_PATH_HOSTSEQUIV, "r");
    672 	if (hostf) {
    673 		if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
    674 			(void)fclose(hostf);
    675 			return;
    676 		}
    677 		(void)fclose(hostf);
    678 	}
    679 	hostf = fopen(_PATH_HOSTSLPD, "r");
    680 	if (hostf) {
    681 		if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
    682 			(void)fclose(hostf);
    683 			return;
    684 		}
    685 		(void)fclose(hostf);
    686 	}
    687 #ifdef LIBWRAP
    688   denied:
    689 #endif
    690 	fatal("Your host does not have line printer access");
    691 	/*NOTREACHED*/
    692 }
    693 
    694 
    695 static void
    696 usage(void)
    697 {
    698 
    699 	fprintf(stderr, "usage: %s [-dlrsW] [-b bind-address] [-n maxchild] "
    700 	    "[-w maxwait] [port]\n", getprogname());
    701 	exit(1);
    702 }
    703 
    704 /* setup server socket for specified address family */
    705 /* if af is PF_UNSPEC more than one socket may be returned */
    706 /* the returned list is dynamically allocated, so caller needs to free it */
    707 struct pollfd *
    708 socksetup(int af, int options, const char *port, int *nfds)
    709 {
    710 	struct sockaddr_un un;
    711 	struct addrinfo hints, *res, *r;
    712 	int error, s, blidx = 0, n;
    713 	struct pollfd *socks;
    714 	const int on = 1;
    715 
    716 	*nfds = 0;
    717 
    718 	socks = malloc(1 * sizeof(int));
    719 	if (!socks) {
    720 		syslog(LOG_ERR, "couldn't allocate memory for sockets");
    721 		mcleanup(0);
    722 	}
    723 
    724 	s = socket(AF_LOCAL, SOCK_STREAM, 0);
    725 	if (s < 0) {
    726 		syslog(LOG_ERR, "socket(): %m");
    727 		exit(1);
    728 	}
    729 	memset(&un, 0, sizeof(un));
    730 	un.sun_family = AF_LOCAL;
    731 	strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
    732 	un.sun_len = SUN_LEN(&un);
    733 	(void)umask(07);
    734 	(void)unlink(_PATH_SOCKETNAME);
    735 	if (bind(s, (struct sockaddr *)&un, un.sun_len) < 0) {
    736 		syslog(LOG_ERR, "bind(): %m");
    737 		exit(1);
    738 	}
    739 	(void)umask(0);
    740 	listen(s, 5);
    741 	socks[*nfds].fd = s;
    742 	socks[*nfds].events = POLLIN;
    743 	(*nfds)++;
    744 
    745 	if (sflag && !blist_addrs)
    746 		return (socks);
    747 
    748 	do {
    749 		memset(&hints, 0, sizeof(hints));
    750 		hints.ai_flags = AI_PASSIVE;
    751 		hints.ai_family = af;
    752 		hints.ai_socktype = SOCK_STREAM;
    753 		error = getaddrinfo((blist_addrs == 0) ? NULL : blist[blidx],
    754 		    port ? port : "printer", &hints, &res);
    755 		if (error) {
    756 			if (blist_addrs)
    757 				syslog(LOG_ERR, "%s: %s", blist[blidx],
    758 				    gai_strerror(error));
    759 			else
    760 				syslog(LOG_ERR, "%s", gai_strerror(error));
    761 			mcleanup(0);
    762 		}
    763 
    764 		/* Count max number of sockets we may open */
    765 		for (r = res, n = 0; r; r = r->ai_next, n++)
    766 			;
    767 		socks = realloc(socks, (*nfds + n) * sizeof(int));
    768 		if (!socks) {
    769 			syslog(LOG_ERR, "couldn't allocate memory for sockets");
    770 			mcleanup(0);
    771 		}
    772 
    773 		for (r = res; r; r = r->ai_next) {
    774 			s = socket(r->ai_family, r->ai_socktype,
    775 			    r->ai_protocol);
    776 			if (s < 0) {
    777 				syslog(LOG_DEBUG, "socket(): %m");
    778 				continue;
    779 			}
    780 			if (options & SO_DEBUG)
    781 				if (setsockopt(s, SOL_SOCKET, SO_DEBUG,
    782 					       &on, sizeof(on)) < 0) {
    783 					syslog(LOG_ERR,
    784 					       "setsockopt (SO_DEBUG): %m");
    785 					close(s);
    786 					continue;
    787 				}
    788 			if (setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &on,
    789 			    sizeof(on)) < 0) {
    790 				syslog(LOG_ERR,
    791 				    "setsockopt (SO_REUSEPORT): %m");
    792 				close(s);
    793 				continue;
    794 			}
    795 			if (bind(s, r->ai_addr, r->ai_addrlen) < 0) {
    796 				syslog(LOG_DEBUG, "bind(): %m");
    797 				close(s);
    798 				continue;
    799 			}
    800 			listen(s, 5);
    801 			socks[*nfds].fd = s;
    802 			socks[*nfds].events = POLLIN;
    803 			(*nfds)++;
    804 		}
    805 
    806 		if (res)
    807 			freeaddrinfo(res);
    808 	} while (++blidx < blist_addrs);
    809 
    810 	return (socks);
    811 }
    812