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