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