lpd.c revision 1.13 1 /* $NetBSD: lpd.c,v 1.13 1997/10/05 15:12:13 mrg 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.13 1997/10/05 15:12:13 mrg 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 <netdb.h>
91 #include <unistd.h>
92 #include <syslog.h>
93 #include <signal.h>
94 #include <errno.h>
95 #include <fcntl.h>
96 #include <dirent.h>
97 #include <stdio.h>
98 #include <stdlib.h>
99 #include <string.h>
100 #include <ctype.h>
101 #include <arpa/inet.h>
102
103 #include "lp.h"
104 #include "lp.local.h"
105 #include "pathnames.h"
106 #include "extern.h"
107
108 int lflag; /* log requests flag */
109 int sflag; /* secure (no inet) flag */
110 int from_remote; /* from remote socket */
111
112 int main __P((int, char **));
113 static void reapchild __P((int));
114 static void mcleanup __P((int));
115 static void doit __P((void));
116 static void startup __P((void));
117 static void chkhost __P((struct sockaddr_in *));
118 static int ckqueue __P((char *));
119 static void usage __P((void));
120
121 uid_t uid, euid;
122
123 int
124 main(argc, argv)
125 int argc;
126 char **argv;
127 {
128 int f, funix, finet, options, fromlen;
129 fd_set defreadfds;
130 struct sockaddr_un un, fromunix;
131 struct sockaddr_in sin, frominet;
132 int omask, lfd, errs, i;
133
134 euid = geteuid(); /* these shouldn't be different */
135 uid = getuid();
136 options = 0;
137 gethostname(host, sizeof(host));
138 name = argv[0];
139
140 errs = 0;
141 while ((i = getopt(argc, argv, "dl")) != -1)
142 switch (i) {
143 case 'd':
144 options |= SO_DEBUG;
145 break;
146 case 'l':
147 lflag++;
148 break;
149 default:
150 errs++;
151 }
152 argc -= optind;
153 argv += optind;
154 if (errs || argc != 0)
155 usage();
156
157 #ifndef DEBUG
158 /*
159 * Set up standard environment by detaching from the parent.
160 */
161 daemon(0, 0);
162 #endif
163
164 openlog("lpd", LOG_PID, LOG_LPR);
165 syslog(LOG_INFO, "restarted");
166 (void)umask(0);
167 lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
168 if (lfd < 0) {
169 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
170 exit(1);
171 }
172 if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
173 if (errno == EWOULDBLOCK) /* active deamon present */
174 exit(0);
175 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
176 exit(1);
177 }
178 ftruncate(lfd, 0);
179 /*
180 * write process id for others to know
181 */
182 (void)snprintf(line, sizeof(line), "%u\n", getpid());
183 f = strlen(line);
184 if (write(lfd, line, f) != f) {
185 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
186 exit(1);
187 }
188 signal(SIGCHLD, reapchild);
189 /*
190 * Restart all the printers.
191 */
192 startup();
193 (void)unlink(_PATH_SOCKETNAME);
194 funix = socket(AF_UNIX, SOCK_STREAM, 0);
195 if (funix < 0) {
196 syslog(LOG_ERR, "socket: %m");
197 exit(1);
198 }
199 #define mask(s) (1 << ((s) - 1))
200 omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
201 signal(SIGHUP, mcleanup);
202 signal(SIGINT, mcleanup);
203 signal(SIGQUIT, mcleanup);
204 signal(SIGTERM, mcleanup);
205 memset(&un, 0, sizeof(un));
206 un.sun_family = AF_UNIX;
207 strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
208 #ifndef SUN_LEN
209 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
210 #endif
211 if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
212 syslog(LOG_ERR, "ubind: %m");
213 exit(1);
214 }
215 sigsetmask(omask);
216 FD_ZERO(&defreadfds);
217 FD_SET(funix, &defreadfds);
218 listen(funix, 5);
219 if (!sflag)
220 finet = socket(AF_INET, SOCK_STREAM, 0);
221 else
222 finet = -1; /* pretend we couldn't open TCP socket. */
223 if (finet >= 0) {
224 struct servent *sp;
225
226 if (options & SO_DEBUG)
227 if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
228 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
229 mcleanup(0);
230 }
231 sp = getservbyname("printer", "tcp");
232 if (sp == NULL) {
233 syslog(LOG_ERR, "printer/tcp: unknown service");
234 mcleanup(0);
235 }
236 memset(&sin, 0, sizeof(sin));
237 sin.sin_family = AF_INET;
238 sin.sin_port = sp->s_port;
239 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
240 syslog(LOG_ERR, "bind: %m");
241 mcleanup(0);
242 }
243 FD_SET(finet, &defreadfds);
244 listen(finet, 5);
245 }
246 /*
247 * Main loop: accept, do a request, continue.
248 */
249 memset(&frominet, 0, sizeof(frominet));
250 memset(&fromunix, 0, sizeof(fromunix));
251 for (;;) {
252 int domain, nfds, s;
253 fd_set readfds;
254
255 FD_COPY(&defreadfds, &readfds);
256 nfds = select(20, &readfds, 0, 0, 0);
257 if (nfds <= 0) {
258 if (nfds < 0 && errno != EINTR)
259 syslog(LOG_WARNING, "select: %m");
260 continue;
261 }
262 if (FD_ISSET(funix, &readfds)) {
263 domain = AF_UNIX, fromlen = sizeof(fromunix);
264 s = accept(funix,
265 (struct sockaddr *)&fromunix, &fromlen);
266 } else /* if (FD_ISSET(finet, &readfds)) */ {
267 domain = AF_INET, fromlen = sizeof(frominet);
268 s = accept(finet,
269 (struct sockaddr *)&frominet, &fromlen);
270 }
271 if (s < 0) {
272 if (errno != EINTR)
273 syslog(LOG_WARNING, "accept: %m");
274 continue;
275 }
276 if (fork() == 0) {
277 signal(SIGCHLD, SIG_IGN);
278 signal(SIGHUP, SIG_IGN);
279 signal(SIGINT, SIG_IGN);
280 signal(SIGQUIT, SIG_IGN);
281 signal(SIGTERM, SIG_IGN);
282 (void)close(funix);
283 if (!sflag)
284 (void)close(finet);
285 dup2(s, 1);
286 (void)close(s);
287 if (domain == AF_INET) {
288 from_remote = 1;
289 chkhost(&frominet);
290 } else
291 from_remote = 0;
292 doit();
293 exit(0);
294 }
295 (void)close(s);
296 }
297 }
298
299 static void
300 reapchild(signo)
301 int signo;
302 {
303 union wait status;
304
305 while (wait3((int *)&status, WNOHANG, 0) > 0)
306 ;
307 }
308
309 static void
310 mcleanup(signo)
311 int signo;
312 {
313 if (lflag)
314 syslog(LOG_INFO, "exiting");
315 unlink(_PATH_SOCKETNAME);
316 exit(0);
317 }
318
319 /*
320 * Stuff for handling job specifications
321 */
322 char *user[MAXUSERS]; /* users to process */
323 int users; /* # of users in user array */
324 int requ[MAXREQUESTS]; /* job number of spool entries */
325 int requests; /* # of spool requests */
326 char *person; /* name of person doing lprm */
327
328 char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */
329 char cbuf[BUFSIZ]; /* command line buffer */
330 char *cmdnames[] = {
331 "null",
332 "printjob",
333 "recvjob",
334 "displayq short",
335 "displayq long",
336 "rmjob"
337 };
338
339 static void
340 doit()
341 {
342 char *cp;
343 int n;
344
345 for (;;) {
346 cp = cbuf;
347 do {
348 if (cp >= &cbuf[sizeof(cbuf) - 1])
349 fatal("Command line too long");
350 if ((n = read(1, cp, 1)) != 1) {
351 if (n < 0)
352 fatal("Lost connection");
353 return;
354 }
355 } while (*cp++ != '\n');
356 *--cp = '\0';
357 cp = cbuf;
358 if (lflag) {
359 if (*cp >= '\1' && *cp <= '\5')
360 syslog(LOG_INFO, "%s requests %s %s",
361 from, cmdnames[(int)*cp], cp+1);
362 else
363 syslog(LOG_INFO, "bad request (%d) from %s",
364 *cp, from);
365 }
366 switch (*cp++) {
367 case '\1': /* check the queue and print any jobs there */
368 printer = cp;
369 printjob();
370 break;
371 case '\2': /* receive files to be queued */
372 if (!from_remote) {
373 syslog(LOG_INFO, "illegal request (%d)", *cp);
374 exit(1);
375 }
376 printer = cp;
377 recvjob();
378 break;
379 case '\3': /* display the queue (short form) */
380 case '\4': /* display the queue (long form) */
381 printer = cp;
382 while (*cp) {
383 if (*cp != ' ') {
384 cp++;
385 continue;
386 }
387 *cp++ = '\0';
388 while (isspace(*cp))
389 cp++;
390 if (*cp == '\0')
391 break;
392 if (isdigit(*cp)) {
393 if (requests >= MAXREQUESTS)
394 fatal("Too many requests");
395 requ[requests++] = atoi(cp);
396 } else {
397 if (users >= MAXUSERS)
398 fatal("Too many users");
399 user[users++] = cp;
400 }
401 }
402 displayq(cbuf[0] - '\3');
403 exit(0);
404 case '\5': /* remove a job from the queue */
405 if (!from_remote) {
406 syslog(LOG_INFO, "illegal request (%d)", *cp);
407 exit(1);
408 }
409 printer = cp;
410 while (*cp && *cp != ' ')
411 cp++;
412 if (!*cp)
413 break;
414 *cp++ = '\0';
415 person = cp;
416 while (*cp) {
417 if (*cp != ' ') {
418 cp++;
419 continue;
420 }
421 *cp++ = '\0';
422 while (isspace(*cp))
423 cp++;
424 if (*cp == '\0')
425 break;
426 if (isdigit(*cp)) {
427 if (requests >= MAXREQUESTS)
428 fatal("Too many requests");
429 requ[requests++] = atoi(cp);
430 } else {
431 if (users >= MAXUSERS)
432 fatal("Too many users");
433 user[users++] = cp;
434 }
435 }
436 rmjob();
437 break;
438 }
439 fatal("Illegal service request");
440 }
441 }
442
443 /*
444 * Make a pass through the printcap database and start printing any
445 * files left from the last time the machine went down.
446 */
447 static void
448 startup()
449 {
450 char *buf;
451 char *cp;
452 int pid;
453
454 /*
455 * Restart the daemons.
456 */
457 while (cgetnext(&buf, printcapdb) > 0) {
458 if (ckqueue(buf) <= 0) {
459 free(buf);
460 continue; /* no work to do for this printer */
461 }
462 for (cp = buf; *cp; cp++)
463 if (*cp == '|' || *cp == ':') {
464 *cp = '\0';
465 break;
466 }
467 if (lflag)
468 syslog(LOG_INFO, "work for %s", buf);
469 if ((pid = fork()) < 0) {
470 syslog(LOG_WARNING, "startup: cannot fork");
471 mcleanup(0);
472 }
473 if (!pid) {
474 printer = buf;
475 cgetclose();
476 printjob();
477 /* NOTREACHED */
478 }
479 else free(buf);
480 }
481 }
482
483 /*
484 * Make sure there's some work to do before forking off a child
485 */
486 static int
487 ckqueue(cap)
488 char *cap;
489 {
490 struct dirent *d;
491 DIR *dirp;
492 char *spooldir;
493
494 if (cgetstr(cap, "sd", &spooldir) == -1)
495 spooldir = _PATH_DEFSPOOL;
496 if ((dirp = opendir(spooldir)) == NULL)
497 return (-1);
498 while ((d = readdir(dirp)) != NULL) {
499 if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
500 continue; /* daemon control files only */
501 closedir(dirp);
502 return (1); /* found something */
503 }
504 closedir(dirp);
505 return (0);
506 }
507
508 #define DUMMY ":nobody::"
509
510 /*
511 * Check to see if the from host has access to the line printer.
512 */
513 static void
514 chkhost(f)
515 struct sockaddr_in *f;
516 {
517 struct hostent *hp;
518 FILE *hostf;
519 int first = 1, good = 0;
520
521 f->sin_port = ntohs(f->sin_port);
522 if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
523 fatal("Malformed from address");
524
525 /* Need real hostname for temporary filenames */
526 hp = gethostbyaddr((char *)&f->sin_addr,
527 sizeof(struct in_addr), f->sin_family);
528 if (hp == NULL)
529 fatal("Host name for your address (%s) unknown",
530 inet_ntoa(f->sin_addr));
531
532 (void)strncpy(fromb, hp->h_name, sizeof(fromb) - 1);
533 from[sizeof(fromb) - 1] = '\0';
534 from = fromb;
535
536 /* Check for spoof, ala rlogind */
537 hp = gethostbyname(fromb);
538 if (!hp)
539 fatal("hostname for your address (%s) unknown",
540 inet_ntoa(f->sin_addr));
541 for (; good == 0 && hp->h_addr_list[0] != NULL; hp->h_addr_list++) {
542 if (!bcmp(hp->h_addr_list[0], (caddr_t)&f->sin_addr,
543 sizeof(f->sin_addr)))
544 good = 1;
545 }
546 if (good == 0)
547 fatal("address for your hostname (%s) not matched",
548 inet_ntoa(f->sin_addr));
549 hostf = fopen(_PATH_HOSTSEQUIV, "r");
550 again:
551 if (hostf) {
552 if (__ivaliduser(hostf, f->sin_addr.s_addr,
553 DUMMY, DUMMY) == 0) {
554 (void)fclose(hostf);
555 return;
556 }
557 (void)fclose(hostf);
558 }
559 if (first == 1) {
560 first = 0;
561 hostf = fopen(_PATH_HOSTSLPD, "r");
562 goto again;
563 }
564 fatal("Your host does not have line printer access");
565 /*NOTREACHED*/
566 }
567
568 static void
569 usage()
570 {
571 extern char *__progname; /* XXX */
572
573 fprintf(stderr, "usage: %s [-d] [-l]\n", __progname);
574 exit(1);
575 }
576