syscons util remove use kbdcontrol & vidcontrol instead
[unix-history] / usr.sbin / lpr / lpd / lpd.c
CommitLineData
15637ed4
RG
1/*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35char copyright[] =
36"@(#) Copyright (c) 1983 Regents of the University of California.\n\
37 All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)lpd.c 5.12 (Berkeley) 3/7/91";
42#endif /* not lint */
43
44/*
45 * lpd -- line printer daemon.
46 *
47 * Listen for a connection and perform the requested operation.
48 * Operations are:
49 * \1printer\n
50 * check the queue for jobs and print any found.
51 * \2printer\n
52 * receive a job from another machine and queue it.
53 * \3printer [users ...] [jobs ...]\n
54 * return the current state of the queue (short form).
55 * \4printer [users ...] [jobs ...]\n
56 * return the current state of the queue (long form).
57 * \5printer person [users ...] [jobs ...]\n
58 * remove jobs from the queue.
59 *
60 * Strategy to maintain protected spooling area:
61 * 1. Spooling area is writable only by daemon and spooling group
62 * 2. lpr runs setuid root and setgrp spooling group; it uses
63 * root to access any file it wants (verifying things before
64 * with an access call) and group id to know how it should
65 * set up ownership of files in the spooling area.
66 * 3. Files in spooling area are owned by root, group spooling
67 * group, with mode 660.
68 * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
69 * access files and printer. Users can't get to anything
70 * w/o help of lpq and lprm programs.
71 */
72
73#include "lp.h"
74#include "pathnames.h"
75
76int lflag; /* log requests flag */
77int from_remote; /* from remote socket */
78
79void mcleanup(), reapchild();
80
81main(argc, argv)
82 int argc;
83 char **argv;
84{
85 int f, funix, finet, options = 0, defreadfds, fromlen;
86 struct sockaddr_un sun, fromunix;
87 struct sockaddr_in sin, frominet;
88 int omask, lfd;
89
90 gethostname(host, sizeof(host));
91 name = argv[0];
92
93 while (--argc > 0) {
94 argv++;
95 if (argv[0][0] == '-')
96 switch (argv[0][1]) {
97 case 'd':
98 options |= SO_DEBUG;
99 break;
100 case 'l':
101 lflag++;
102 break;
103 }
104 }
105
106#ifndef DEBUG
107 /*
108 * Set up standard environment by detaching from the parent.
109 */
110 daemon(0, 0);
111#endif
112
113 openlog("lpd", LOG_PID, LOG_LPR);
114 (void) umask(0);
115 lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
116 if (lfd < 0) {
117 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
118 exit(1);
119 }
120 if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
121 if (errno == EWOULDBLOCK) /* active deamon present */
122 exit(0);
123 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
124 exit(1);
125 }
126 ftruncate(lfd, 0);
127 /*
128 * write process id for others to know
129 */
130 sprintf(line, "%u\n", getpid());
131 f = strlen(line);
132 if (write(lfd, line, f) != f) {
133 syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
134 exit(1);
135 }
136 signal(SIGCHLD, reapchild);
137 /*
138 * Restart all the printers.
139 */
140 startup();
141 (void) unlink(_PATH_SOCKETNAME);
142 funix = socket(AF_UNIX, SOCK_STREAM, 0);
143 if (funix < 0) {
144 syslog(LOG_ERR, "socket: %m");
145 exit(1);
146 }
147#define mask(s) (1 << ((s) - 1))
148 omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
149 signal(SIGHUP, mcleanup);
150 signal(SIGINT, mcleanup);
151 signal(SIGQUIT, mcleanup);
152 signal(SIGTERM, mcleanup);
153 sun.sun_family = AF_UNIX;
154 strcpy(sun.sun_path, _PATH_SOCKETNAME);
155 if (bind(funix,
156 (struct sockaddr *)&sun, strlen(sun.sun_path) + 2) < 0) {
157 syslog(LOG_ERR, "ubind: %m");
158 exit(1);
159 }
160 sigsetmask(omask);
161 defreadfds = 1 << funix;
162 listen(funix, 5);
163 finet = socket(AF_INET, SOCK_STREAM, 0);
164 if (finet >= 0) {
165 struct servent *sp;
166
167 if (options & SO_DEBUG)
168 if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
169 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
170 mcleanup();
171 }
172 sp = getservbyname("printer", "tcp");
173 if (sp == NULL) {
174 syslog(LOG_ERR, "printer/tcp: unknown service");
175 mcleanup();
176 }
6ed78091 177 sin.sin_addr.s_addr=INADDR_ANY;
15637ed4
RG
178 sin.sin_family = AF_INET;
179 sin.sin_port = sp->s_port;
180 if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
181 syslog(LOG_ERR, "bind: %m");
182 mcleanup();
183 }
184 defreadfds |= 1 << finet;
185 listen(finet, 5);
186 }
187 /*
188 * Main loop: accept, do a request, continue.
189 */
190 for (;;) {
191 int domain, nfds, s, readfds = defreadfds;
192
193 nfds = select(20, &readfds, 0, 0, 0);
194 if (nfds <= 0) {
195 if (nfds < 0 && errno != EINTR)
196 syslog(LOG_WARNING, "select: %m");
197 continue;
198 }
199 if (readfds & (1 << funix)) {
200 domain = AF_UNIX, fromlen = sizeof(fromunix);
201 s = accept(funix,
202 (struct sockaddr *)&fromunix, &fromlen);
203 } else if (readfds & (1 << finet)) {
204 domain = AF_INET, fromlen = sizeof(frominet);
205 s = accept(finet,
206 (struct sockaddr *)&frominet, &fromlen);
207 }
208 if (s < 0) {
209 if (errno != EINTR)
210 syslog(LOG_WARNING, "accept: %m");
211 continue;
212 }
213 if (fork() == 0) {
214 signal(SIGCHLD, SIG_IGN);
215 signal(SIGHUP, SIG_IGN);
216 signal(SIGINT, SIG_IGN);
217 signal(SIGQUIT, SIG_IGN);
218 signal(SIGTERM, SIG_IGN);
219 (void) close(funix);
220 (void) close(finet);
221 dup2(s, 1);
222 (void) close(s);
223 if (domain == AF_INET) {
224 from_remote = 1;
225 chkhost(&frominet);
226 } else
227 from_remote = 0;
228 doit();
229 exit(0);
230 }
231 (void) close(s);
232 }
233}
234
235void
236reapchild()
237{
238 union wait status;
239
240 while (wait3((int *)&status, WNOHANG, 0) > 0)
241 ;
242}
243
244void
245mcleanup()
246{
247 if (lflag)
248 syslog(LOG_INFO, "exiting");
249 unlink(_PATH_SOCKETNAME);
250 exit(0);
251}
252
253/*
254 * Stuff for handling job specifications
255 */
256char *user[MAXUSERS]; /* users to process */
257int users; /* # of users in user array */
258int requ[MAXREQUESTS]; /* job number of spool entries */
259int requests; /* # of spool requests */
260char *person; /* name of person doing lprm */
261
262char fromb[32]; /* buffer for client's machine name */
263char cbuf[BUFSIZ]; /* command line buffer */
264char *cmdnames[] = {
265 "null",
266 "printjob",
267 "recvjob",
268 "displayq short",
269 "displayq long",
270 "rmjob"
271};
272
273doit()
274{
275 register char *cp;
276 register int n;
277
278 for (;;) {
279 cp = cbuf;
280 do {
281 if (cp >= &cbuf[sizeof(cbuf) - 1])
282 fatal("Command line too long");
283 if ((n = read(1, cp, 1)) != 1) {
284 if (n < 0)
285 fatal("Lost connection");
286 return;
287 }
288 } while (*cp++ != '\n');
289 *--cp = '\0';
290 cp = cbuf;
291 if (lflag) {
292 if (*cp >= '\1' && *cp <= '\5')
293 syslog(LOG_INFO, "%s requests %s %s",
294 from, cmdnames[*cp], cp+1);
295 else
296 syslog(LOG_INFO, "bad request (%d) from %s",
297 *cp, from);
298 }
299 switch (*cp++) {
300 case '\1': /* check the queue and print any jobs there */
301 printer = cp;
302 printjob();
303 break;
304 case '\2': /* receive files to be queued */
305 if (!from_remote) {
306 syslog(LOG_INFO, "illegal request (%d)", *cp);
307 exit(1);
308 }
309 printer = cp;
310 recvjob();
311 break;
312 case '\3': /* display the queue (short form) */
313 case '\4': /* display the queue (long form) */
314 printer = cp;
315 while (*cp) {
316 if (*cp != ' ') {
317 cp++;
318 continue;
319 }
320 *cp++ = '\0';
321 while (isspace(*cp))
322 cp++;
323 if (*cp == '\0')
324 break;
325 if (isdigit(*cp)) {
326 if (requests >= MAXREQUESTS)
327 fatal("Too many requests");
328 requ[requests++] = atoi(cp);
329 } else {
330 if (users >= MAXUSERS)
331 fatal("Too many users");
332 user[users++] = cp;
333 }
334 }
335 displayq(cbuf[0] - '\3');
336 exit(0);
337 case '\5': /* remove a job from the queue */
338 if (!from_remote) {
339 syslog(LOG_INFO, "illegal request (%d)", *cp);
340 exit(1);
341 }
342 printer = cp;
343 while (*cp && *cp != ' ')
344 cp++;
345 if (!*cp)
346 break;
347 *cp++ = '\0';
348 person = cp;
349 while (*cp) {
350 if (*cp != ' ') {
351 cp++;
352 continue;
353 }
354 *cp++ = '\0';
355 while (isspace(*cp))
356 cp++;
357 if (*cp == '\0')
358 break;
359 if (isdigit(*cp)) {
360 if (requests >= MAXREQUESTS)
361 fatal("Too many requests");
362 requ[requests++] = atoi(cp);
363 } else {
364 if (users >= MAXUSERS)
365 fatal("Too many users");
366 user[users++] = cp;
367 }
368 }
369 rmjob();
370 break;
371 }
372 fatal("Illegal service request");
373 }
374}
375
376/*
377 * Make a pass through the printcap database and start printing any
378 * files left from the last time the machine went down.
379 */
380startup()
381{
382 char buf[BUFSIZ];
383 register char *cp;
384 int pid;
385
386 printer = buf;
387
388 /*
389 * Restart the daemons.
390 */
391 while (getprent(buf) > 0) {
392 for (cp = buf; *cp; cp++)
393 if (*cp == '|' || *cp == ':') {
394 *cp = '\0';
395 break;
396 }
397 if ((pid = fork()) < 0) {
398 syslog(LOG_WARNING, "startup: cannot fork");
399 mcleanup();
400 }
401 if (!pid) {
402 endprent();
403 printjob();
404 }
405 }
406}
407
408#define DUMMY ":nobody::"
409
410/*
411 * Check to see if the from host has access to the line printer.
412 */
413chkhost(f)
414 struct sockaddr_in *f;
415{
416 register struct hostent *hp;
417 register FILE *hostf;
418 register char *cp, *sp;
419 char ahost[50];
420 int first = 1;
421 extern char *inet_ntoa();
422 int baselen = -1;
423
424 f->sin_port = ntohs(f->sin_port);
425 if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
426 fatal("Malformed from address");
427 hp = gethostbyaddr((char *)&f->sin_addr,
428 sizeof(struct in_addr), f->sin_family);
429 if (hp == 0)
430 fatal("Host name for your address (%s) unknown",
431 inet_ntoa(f->sin_addr));
432
433 strcpy(fromb, hp->h_name);
434 from = fromb;
435 if (!strcmp(from, host))
436 return;
437
438 sp = fromb;
439 cp = ahost;
440 while (*sp) {
441 if (*sp == '.') {
442 if (baselen == -1)
443 baselen = sp - fromb;
444 *cp++ = *sp++;
445 } else {
446 *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++;
447 }
448 }
449 *cp = '\0';
450 hostf = fopen(_PATH_HOSTSEQUIV, "r");
451again:
452 if (hostf) {
453 if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) {
454 (void) fclose(hostf);
455 return;
456 }
457 (void) fclose(hostf);
458 }
459 if (first == 1) {
460 first = 0;
461 hostf = fopen(_PATH_HOSTSLPD, "r");
462 goto again;
463 }
464 fatal("Your host does not have line printer access");
465}