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