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