2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
7 #if defined(DOSCCS) && !defined(lint)
9 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
10 All rights reserved.\n";
12 static char sccsid[] = "@(#)lpd.c 5.4.1 (2.11BSD GTE) 1/1/94";
16 * lpd -- line printer daemon.
18 * Listen for a connection and perform the requested operation.
21 * check the queue for jobs and print any found.
23 * receive a job from another machine and queue it.
24 * \3printer [users ...] [jobs ...]\n
25 * return the current state of the queue (short form).
26 * \4printer [users ...] [jobs ...]\n
27 * return the current state of the queue (long form).
28 * \5printer person [users ...] [jobs ...]\n
29 * remove jobs from the queue.
31 * Strategy to maintain protected spooling area:
32 * 1. Spooling area is writable only by daemon and spooling group
33 * 2. lpr runs setuid root and setgrp spooling group; it uses
34 * root to access any file it wants (verifying things before
35 * with an access call) and group id to know how it should
36 * set up ownership of files in the spooling area.
37 * 3. Files in spooling area are owned by root, group spooling
38 * group, with mode 660.
39 * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
40 * access files and printer. Users can't get to anything
41 * w/o help of lpq and lprm programs.
46 int lflag; /* log requests flag */
50 int unblock, unpause();
56 int f, funix, finet, options, fromlen;
58 struct sockaddr_un sun, fromunix;
59 struct sockaddr_in sin, frominet;
63 gethostname(host, sizeof(host));
68 if (argv[0][0] == '-')
81 * Set up standard environment by detaching from the parent.
85 for (f = 0; f < 5; f++)
87 (void) open("/dev/null", O_RDONLY);
88 (void) open("/dev/null", O_WRONLY);
90 f = open("/dev/tty", O_RDWR);
92 ioctl(f, TIOCNOTTY, 0);
97 openlog("lpd", LOG_PID, LOG_LPR);
99 lfd = open(MASTERLOCK, O_WRONLY|O_CREAT, 0644);
101 syslog(LOG_ERR, "%s: %m", MASTERLOCK);
104 if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
105 if (errno == EWOULDBLOCK) /* active deamon present */
107 syslog(LOG_ERR, "%s: %m", MASTERLOCK);
112 * write process id for others to know
114 sprintf(line, "%u\n", getpid());
116 if (write(lfd, line, f) != f) {
117 syslog(LOG_ERR, "%s: %m", MASTERLOCK);
120 signal(SIGCHLD, reapchild);
122 * Restart all the printers.
125 (void) unlink(SOCKETNAME);
126 funix = socket(AF_UNIX, SOCK_STREAM, 0);
128 if (errno == EPROTONOSUPPORT) {
129 sigblock(sigmask(SIGUSR1));
130 signal(SIGUSR1, unpause);
139 syslog(LOG_ERR, "socket: %m");
142 omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)
143 |sigmask(SIGQUIT)|sigmask(SIGTERM));
144 signal(SIGHUP, mcleanup);
145 signal(SIGINT, mcleanup);
146 signal(SIGQUIT, mcleanup);
147 signal(SIGTERM, mcleanup);
148 sun.sun_family = AF_UNIX;
149 strcpy(sun.sun_path, SOCKETNAME);
150 if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) {
151 syslog(LOG_ERR, "ubind: %m");
155 defreadfds = 1L << funix;
157 finet = socket(AF_INET, SOCK_STREAM, 0);
161 if (options & SO_DEBUG)
162 if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
163 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
166 sp = getservbyname("printer", "tcp");
168 syslog(LOG_ERR, "printer/tcp: unknown service");
171 sin.sin_family = AF_INET;
172 sin.sin_port = sp->s_port;
173 if (bind(finet, &sin, sizeof(sin), 0) < 0) {
174 syslog(LOG_ERR, "bind: %m");
177 defreadfds |= 1L << finet;
181 * Main loop: accept, do a request, continue.
185 long readfds = defreadfds;
187 nfds = select(20, &readfds, 0, 0, 0);
189 if (nfds < 0 && errno != EINTR)
190 syslog(LOG_WARNING, "select: %m");
193 if (readfds & (1L << funix)) {
194 domain = AF_UNIX, fromlen = sizeof(fromunix);
195 s = accept(funix, &fromunix, &fromlen);
196 } else if (readfds & (1L << finet)) {
197 domain = AF_INET, fromlen = sizeof(frominet);
198 s = accept(finet, &frominet, &fromlen);
202 syslog(LOG_WARNING, "accept: %m");
206 signal(SIGCHLD, SIG_IGN);
207 signal(SIGHUP, SIG_IGN);
208 signal(SIGINT, SIG_IGN);
209 signal(SIGQUIT, SIG_IGN);
210 signal(SIGTERM, SIG_IGN);
215 if (domain == AF_INET)
228 while (wait3(&status, WNOHANG, 0) > 0)
235 syslog(LOG_INFO, "exiting");
246 * Stuff for handling job specifications
248 char *user[MAXUSERS]; /* users to process */
249 int users; /* # of users in user array */
250 int requ[MAXREQUESTS]; /* job number of spool entries */
251 int requests; /* # of spool requests */
252 char *person; /* name of person doing lprm */
254 char fromb[32]; /* buffer for client's machine name */
255 char cbuf[BUFSIZ]; /* command line buffer */
273 if (cp >= &cbuf[sizeof(cbuf) - 1])
274 fatal("Command line too long");
275 if ((n = read(1, cp, 1)) != 1) {
277 fatal("Lost connection");
280 } while (*cp++ != '\n');
284 if (*cp >= '\1' && *cp <= '\5')
285 syslog(LOG_INFO, "%s requests %s %s",
286 from, cmdnames[*cp], cp+1);
288 syslog(LOG_INFO, "bad request (%d) from %s",
292 case '\1': /* check the queue and print any jobs there */
296 case '\2': /* receive files to be queued */
300 case '\3': /* display the queue (short form) */
301 case '\4': /* display the queue (long form) */
314 if (requests >= MAXREQUESTS)
315 fatal("Too many requests");
316 requ[requests++] = atoi(cp);
318 if (users >= MAXUSERS)
319 fatal("Too many users");
323 displayq(cbuf[0] - '\3');
325 case '\5': /* remove a job from the queue */
327 while (*cp && *cp != ' ')
344 if (requests >= MAXREQUESTS)
345 fatal("Too many requests");
346 requ[requests++] = atoi(cp);
348 if (users >= MAXUSERS)
349 fatal("Too many users");
356 fatal("Illegal service request");
361 * Make a pass through the printcap database and start printing any
362 * files left from the last time the machine went down.
373 * Restart the daemons.
375 while (getprent(buf) > 0) {
376 for (cp = buf; *cp; cp++)
377 if (*cp == '|' || *cp == ':') {
381 if ((pid = fork()) < 0) {
382 syslog(LOG_WARNING, "startup: cannot fork");
387 signal(SIGCHLD, SIG_IGN);
393 #define DUMMY ":nobody::"
396 * Check to see if the from host has access to the line printer.
399 struct sockaddr_in *f;
401 register struct hostent *hp;
402 register FILE *hostf;
403 register char *cp, *sp;
406 extern char *inet_ntoa();
409 f->sin_port = ntohs(f->sin_port);
410 if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
411 fatal("Malformed from address");
412 hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family);
414 fatal("Host name for your address (%s) unknown",
415 inet_ntoa(f->sin_addr));
417 strcpy(fromb, hp->h_name);
419 if (!strcmp(from, host))
427 baselen = sp - fromb;
430 *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++;
434 hostf = fopen("/etc/hosts.equiv", "r");
437 if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) {
438 (void) fclose(hostf);
441 (void) fclose(hostf);
445 hostf = fopen("/etc/hosts.lpd", "r");
448 fatal("Your host does not have line printer access");