lpd.c revision 1.9 1 /* $NetBSD: lpd.c,v 1.9 1996/12/09 09:57:45 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 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1983, 1993, 1994\n\
40 The Regents of the University of California. All rights reserved.\n";
41 #endif /* not lint */
42
43 #ifndef lint
44 static char sccsid[] = "@(#)lpd.c 8.4 (Berkeley) 4/17/94";
45 #endif /* not lint */
46
47 /*
48 * lpd -- line printer daemon.
49 *
50 * Listen for a connection and perform the requested operation.
51 * Operations are:
52 * \1printer\n
53 * check the queue for jobs and print any found.
54 * \2printer\n
55 * receive a job from another machine and queue it.
56 * \3printer [users ...] [jobs ...]\n
57 * return the current state of the queue (short form).
58 * \4printer [users ...] [jobs ...]\n
59 * return the current state of the queue (long form).
60 * \5printer person [users ...] [jobs ...]\n
61 * remove jobs from the queue.
62 *
63 * Strategy to maintain protected spooling area:
64 * 1. Spooling area is writable only by daemon and spooling group
65 * 2. lpr runs setuid root and setgrp spooling group; it uses
66 * root to access any file it wants (verifying things before
67 * with an access call) and group id to know how it should
68 * set up ownership of files in the spooling area.
69 * 3. Files in spooling area are owned by root, group spooling
70 * group, with mode 660.
71 * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
72 * access files and printer. Users can't get to anything
73 * w/o help of lpq and lprm programs.
74 */
75
76 #include <sys/param.h>
77 #include <sys/wait.h>
78 #include <sys/types.h>
79 #include <sys/socket.h>
80 #include <sys/un.h>
81 #include <sys/stat.h>
82 #include <netinet/in.h>
83
84 #include <netdb.h>
85 #include <unistd.h>
86 #include <syslog.h>
87 #include <signal.h>
88 #include <errno.h>
89 #include <fcntl.h>
90 #include <dirent.h>
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <ctype.h>
95 #include "lp.h"
96 #include "lp.local.h"
97 #include "pathnames.h"
98 #include "extern.h"
99
100 int lflag; /* log requests flag */
101 int sflag; /* secure (no inet) flag */
102 int from_remote; /* from remote socket */
103
104 static void reapchild __P((int));
105 static void mcleanup __P((int));
106 static void doit __P((void));
107 static void startup __P((void));
108 static void chkhost __P((struct sockaddr_in *));
109
110 uid_t uid, euid;
111
112 int
113 main(argc, argv)
114 int argc;
115 char **argv;
116 {
117 int f, funix, finet, options, fromlen;
118 fd_set defreadfds;
119 struct sockaddr_un un, fromunix;
120 struct sockaddr_in sin, frominet;
121 int omask, lfd;
122
123 euid = geteuid(); /* these shouldn't be different */
124 uid = getuid();
125 options = 0;
126 gethostname(host, sizeof(host));
127 name = argv[0];
128
129 while (--argc > 0) {
130 argv++;
131 if (argv[0][0] == '-')
132 switch (argv[0][1]) {
133 case 'd':
134 options |= SO_DEBUG;
135 break;
136 case 'l':
137 lflag++;
138 break;
139 case 's':
140 sflag++;
141 break;
142 }
143 }
144
145 #ifndef DEBUG
146 /*
147 * Set up standard environment by detaching from the parent.
148 */
149 daemon(0, 0);
150 #endif
151
152 openlog("lpd", LOG_PID, LOG_LPR);
153 syslog(LOG_INFO, "restarted");
154 (void)umask(0);
155 lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
156 if (lfd < 0) {
157 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
158 exit(1);
159 }
160 if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
161 if (errno == EWOULDBLOCK) /* active deamon present */
162 exit(0);
163 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
164 exit(1);
165 }
166 ftruncate(lfd, 0);
167 /*
168 * write process id for others to know
169 */
170 (void)snprintf(line, sizeof(line), "%u\n", getpid());
171 f = strlen(line);
172 if (write(lfd, line, f) != f) {
173 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
174 exit(1);
175 }
176 signal(SIGCHLD, reapchild);
177 /*
178 * Restart all the printers.
179 */
180 startup();
181 (void)unlink(_PATH_SOCKETNAME);
182 funix = socket(AF_UNIX, SOCK_STREAM, 0);
183 if (funix < 0) {
184 syslog(LOG_ERR, "socket: %m");
185 exit(1);
186 }
187 #define mask(s) (1 << ((s) - 1))
188 omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
189 signal(SIGHUP, mcleanup);
190 signal(SIGINT, mcleanup);
191 signal(SIGQUIT, mcleanup);
192 signal(SIGTERM, mcleanup);
193 memset(&un, 0, sizeof(un));
194 un.sun_family = AF_UNIX;
195 strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
196 #ifndef SUN_LEN
197 #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
198 #endif
199 if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
200 syslog(LOG_ERR, "ubind: %m");
201 exit(1);
202 }
203 sigsetmask(omask);
204 FD_ZERO(&defreadfds);
205 FD_SET(funix, &defreadfds);
206 listen(funix, 5);
207 if (!sflag)
208 finet = socket(AF_INET, SOCK_STREAM, 0);
209 else
210 finet = -1; /* pretend we couldn't open TCP socket. */
211 if (finet >= 0) {
212 struct servent *sp;
213
214 if (options & SO_DEBUG)
215 if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
216 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
217 mcleanup(0);
218 }
219 sp = getservbyname("printer", "tcp");
220 if (sp == NULL) {
221 syslog(LOG_ERR, "printer/tcp: unknown service");
222 mcleanup(0);
223 }
224 memset(&sin, 0, sizeof(sin));
225 sin.sin_family = AF_INET;
226 sin.sin_port = sp->s_port;
227 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
228 syslog(LOG_ERR, "bind: %m");
229 mcleanup(0);
230 }
231 FD_SET(finet, &defreadfds);
232 listen(finet, 5);
233 }
234 /*
235 * Main loop: accept, do a request, continue.
236 */
237 memset(&frominet, 0, sizeof(frominet));
238 memset(&fromunix, 0, sizeof(fromunix));
239 for (;;) {
240 int domain, nfds, s;
241 fd_set readfds;
242
243 FD_COPY(&defreadfds, &readfds);
244 nfds = select(20, &readfds, 0, 0, 0);
245 if (nfds <= 0) {
246 if (nfds < 0 && errno != EINTR)
247 syslog(LOG_WARNING, "select: %m");
248 continue;
249 }
250 if (FD_ISSET(funix, &readfds)) {
251 domain = AF_UNIX, fromlen = sizeof(fromunix);
252 s = accept(funix,
253 (struct sockaddr *)&fromunix, &fromlen);
254 } else /* if (FD_ISSET(finet, &readfds)) */ {
255 domain = AF_INET, fromlen = sizeof(frominet);
256 s = accept(finet,
257 (struct sockaddr *)&frominet, &fromlen);
258 }
259 if (s < 0) {
260 if (errno != EINTR)
261 syslog(LOG_WARNING, "accept: %m");
262 continue;
263 }
264 if (fork() == 0) {
265 signal(SIGCHLD, SIG_IGN);
266 signal(SIGHUP, SIG_IGN);
267 signal(SIGINT, SIG_IGN);
268 signal(SIGQUIT, SIG_IGN);
269 signal(SIGTERM, SIG_IGN);
270 (void)close(funix);
271 if (!sflag)
272 (void)close(finet);
273 dup2(s, 1);
274 (void)close(s);
275 if (domain == AF_INET) {
276 from_remote = 1;
277 chkhost(&frominet);
278 } else
279 from_remote = 0;
280 doit();
281 exit(0);
282 }
283 (void)close(s);
284 }
285 }
286
287 static void
288 reapchild(signo)
289 int signo;
290 {
291 union wait status;
292
293 while (wait3((int *)&status, WNOHANG, 0) > 0)
294 ;
295 }
296
297 static void
298 mcleanup(signo)
299 int signo;
300 {
301 if (lflag)
302 syslog(LOG_INFO, "exiting");
303 unlink(_PATH_SOCKETNAME);
304 exit(0);
305 }
306
307 /*
308 * Stuff for handling job specifications
309 */
310 char *user[MAXUSERS]; /* users to process */
311 int users; /* # of users in user array */
312 int requ[MAXREQUESTS]; /* job number of spool entries */
313 int requests; /* # of spool requests */
314 char *person; /* name of person doing lprm */
315
316 char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */
317 char cbuf[BUFSIZ]; /* command line buffer */
318 char *cmdnames[] = {
319 "null",
320 "printjob",
321 "recvjob",
322 "displayq short",
323 "displayq long",
324 "rmjob"
325 };
326
327 static void
328 doit()
329 {
330 register char *cp;
331 register int n;
332
333 for (;;) {
334 cp = cbuf;
335 do {
336 if (cp >= &cbuf[sizeof(cbuf) - 1])
337 fatal("Command line too long");
338 if ((n = read(1, cp, 1)) != 1) {
339 if (n < 0)
340 fatal("Lost connection");
341 return;
342 }
343 } while (*cp++ != '\n');
344 *--cp = '\0';
345 cp = cbuf;
346 if (lflag) {
347 if (*cp >= '\1' && *cp <= '\5')
348 syslog(LOG_INFO, "%s requests %s %s",
349 from, cmdnames[*cp], cp+1);
350 else
351 syslog(LOG_INFO, "bad request (%d) from %s",
352 *cp, from);
353 }
354 switch (*cp++) {
355 case '\1': /* check the queue and print any jobs there */
356 printer = cp;
357 printjob();
358 break;
359 case '\2': /* receive files to be queued */
360 if (!from_remote) {
361 syslog(LOG_INFO, "illegal request (%d)", *cp);
362 exit(1);
363 }
364 printer = cp;
365 recvjob();
366 break;
367 case '\3': /* display the queue (short form) */
368 case '\4': /* display the queue (long form) */
369 printer = cp;
370 while (*cp) {
371 if (*cp != ' ') {
372 cp++;
373 continue;
374 }
375 *cp++ = '\0';
376 while (isspace(*cp))
377 cp++;
378 if (*cp == '\0')
379 break;
380 if (isdigit(*cp)) {
381 if (requests >= MAXREQUESTS)
382 fatal("Too many requests");
383 requ[requests++] = atoi(cp);
384 } else {
385 if (users >= MAXUSERS)
386 fatal("Too many users");
387 user[users++] = cp;
388 }
389 }
390 displayq(cbuf[0] - '\3');
391 exit(0);
392 case '\5': /* remove a job from the queue */
393 if (!from_remote) {
394 syslog(LOG_INFO, "illegal request (%d)", *cp);
395 exit(1);
396 }
397 printer = cp;
398 while (*cp && *cp != ' ')
399 cp++;
400 if (!*cp)
401 break;
402 *cp++ = '\0';
403 person = cp;
404 while (*cp) {
405 if (*cp != ' ') {
406 cp++;
407 continue;
408 }
409 *cp++ = '\0';
410 while (isspace(*cp))
411 cp++;
412 if (*cp == '\0')
413 break;
414 if (isdigit(*cp)) {
415 if (requests >= MAXREQUESTS)
416 fatal("Too many requests");
417 requ[requests++] = atoi(cp);
418 } else {
419 if (users >= MAXUSERS)
420 fatal("Too many users");
421 user[users++] = cp;
422 }
423 }
424 rmjob();
425 break;
426 }
427 fatal("Illegal service request");
428 }
429 }
430
431 /*
432 * Make a pass through the printcap database and start printing any
433 * files left from the last time the machine went down.
434 */
435 static void
436 startup()
437 {
438 char *buf;
439 register char *cp;
440 int pid;
441
442 /*
443 * Restart the daemons.
444 */
445 while (cgetnext(&buf, printcapdb) > 0) {
446 for (cp = buf; *cp; cp++)
447 if (*cp == '|' || *cp == ':') {
448 *cp = '\0';
449 break;
450 }
451 if ((pid = fork()) < 0) {
452 syslog(LOG_WARNING, "startup: cannot fork");
453 mcleanup(0);
454 }
455 if (!pid) {
456 printer = buf;
457 cgetclose();
458 printjob();
459 }
460 }
461 }
462
463 #define DUMMY ":nobody::"
464
465 /*
466 * Check to see if the from host has access to the line printer.
467 */
468 static void
469 chkhost(f)
470 struct sockaddr_in *f;
471 {
472 register struct hostent *hp;
473 register FILE *hostf;
474 int first = 1;
475 extern char *inet_ntoa();
476
477 f->sin_port = ntohs(f->sin_port);
478 if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
479 fatal("Malformed from address");
480
481 /* Need real hostname for temporary filenames */
482 hp = gethostbyaddr((char *)&f->sin_addr,
483 sizeof(struct in_addr), f->sin_family);
484 if (hp == NULL)
485 fatal("Host name for your address (%s) unknown",
486 inet_ntoa(f->sin_addr));
487
488 (void)strncpy(fromb, hp->h_name, sizeof(fromb) - 1);
489 from[sizeof(fromb) - 1] = '\0';
490 from = fromb;
491
492 hostf = fopen(_PATH_HOSTSEQUIV, "r");
493 again:
494 if (hostf) {
495 if (__ivaliduser(hostf, f->sin_addr.s_addr,
496 DUMMY, DUMMY) == 0) {
497 (void)fclose(hostf);
498 return;
499 }
500 (void)fclose(hostf);
501 }
502 if (first == 1) {
503 first = 0;
504 hostf = fopen(_PATH_HOSTSLPD, "r");
505 goto again;
506 }
507 fatal("Your host does not have line printer access");
508 /*NOTREACHED*/
509 }
510