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