122429Sdist /*
266850Sbostic * Copyright (c) 1983, 1993, 1994
361843Sbostic * The Regents of the University of California. All rights reserved.
434203Sbostic *
556123Selan *
656251Selan * %sccs.include.redist.c%
722429Sdist */
822429Sdist
913957Ssam #ifndef lint
1056265Selan static char copyright[] =
1166850Sbostic "@(#) Copyright (c) 1983, 1993, 1994\n\
1261843Sbostic The Regents of the University of California. All rights reserved.\n";
1334203Sbostic #endif /* not lint */
1413957Ssam
1522429Sdist #ifndef lint
16*69331Sbostic static char sccsid[] = "@(#)lpd.c 8.7 (Berkeley) 05/10/95";
1734203Sbostic #endif /* not lint */
1822429Sdist
1912106Sralph /*
2012106Sralph * lpd -- line printer daemon.
2112106Sralph *
2212106Sralph * Listen for a connection and perform the requested operation.
2312106Sralph * Operations are:
2412106Sralph * \1printer\n
2512106Sralph * check the queue for jobs and print any found.
2612106Sralph * \2printer\n
2712106Sralph * receive a job from another machine and queue it.
2812106Sralph * \3printer [users ...] [jobs ...]\n
2912106Sralph * return the current state of the queue (short form).
3012106Sralph * \4printer [users ...] [jobs ...]\n
3112106Sralph * return the current state of the queue (long form).
3212106Sralph * \5printer person [users ...] [jobs ...]\n
3312106Sralph * remove jobs from the queue.
3412106Sralph *
3512106Sralph * Strategy to maintain protected spooling area:
3612106Sralph * 1. Spooling area is writable only by daemon and spooling group
3712106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses
3812106Sralph * root to access any file it wants (verifying things before
3912106Sralph * with an access call) and group id to know how it should
4012106Sralph * set up ownership of files in the spooling area.
4112430Sralph * 3. Files in spooling area are owned by root, group spooling
4212106Sralph * group, with mode 660.
4312106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
4412106Sralph * access files and printer. Users can't get to anything
4512106Sralph * w/o help of lpq and lprm programs.
4612106Sralph */
4712106Sralph
4855474Sbostic #include <sys/param.h>
4955474Sbostic #include <sys/wait.h>
5056123Selan #include <sys/types.h>
5155474Sbostic #include <sys/socket.h>
5255474Sbostic #include <sys/un.h>
5356123Selan #include <sys/stat.h>
5469061Stef #include <sys/file.h>
5555474Sbostic #include <netinet/in.h>
5656123Selan
5755474Sbostic #include <netdb.h>
5856123Selan #include <unistd.h>
5955474Sbostic #include <syslog.h>
6055474Sbostic #include <signal.h>
6155474Sbostic #include <errno.h>
6255474Sbostic #include <fcntl.h>
6355474Sbostic #include <dirent.h>
6455474Sbostic #include <stdio.h>
6556123Selan #include <stdlib.h>
6656123Selan #include <string.h>
6756123Selan #include <ctype.h>
6812106Sralph #include "lp.h"
6955474Sbostic #include "lp.local.h"
7037968Sbostic #include "pathnames.h"
7155474Sbostic #include "extern.h"
7212106Sralph
7316761Sralph int lflag; /* log requests flag */
7447055Smckusick int from_remote; /* from remote socket */
7512875Sralph
7655474Sbostic static void reapchild __P((int));
7755474Sbostic static void mcleanup __P((int));
7855474Sbostic static void doit __P((void));
7955474Sbostic static void startup __P((void));
8055474Sbostic static void chkhost __P((struct sockaddr_in *));
81*69331Sbostic static int ckqueue __P((char *));
8212106Sralph
8355474Sbostic int
main(argc,argv)8412106Sralph main(argc, argv)
8512106Sralph int argc;
8612106Sralph char **argv;
8712106Sralph {
8856123Selan int f, funix, finet, options, fromlen;
8956123Selan fd_set defreadfds;
9050886Storek struct sockaddr_un un, fromunix;
9113816Ssam struct sockaddr_in sin, frominet;
9214837Sralph int omask, lfd;
9312106Sralph
9456123Selan options = 0;
9512106Sralph gethostname(host, sizeof(host));
9612106Sralph name = argv[0];
9712106Sralph
9812106Sralph while (--argc > 0) {
9912106Sralph argv++;
10012106Sralph if (argv[0][0] == '-')
10112106Sralph switch (argv[0][1]) {
10212106Sralph case 'd':
10312106Sralph options |= SO_DEBUG;
10412106Sralph break;
10512106Sralph case 'l':
10612430Sralph lflag++;
10712430Sralph break;
10812106Sralph }
10912106Sralph }
11014837Sralph
11112106Sralph #ifndef DEBUG
11212106Sralph /*
11312106Sralph * Set up standard environment by detaching from the parent.
11412106Sralph */
11544708Skarels daemon(0, 0);
11612106Sralph #endif
11714837Sralph
11825494Seric openlog("lpd", LOG_PID, LOG_LPR);
11956923Sbostic syslog(LOG_INFO, "restarted");
12012106Sralph (void) umask(0);
12137968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
12214837Sralph if (lfd < 0) {
12337968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
12414837Sralph exit(1);
12514837Sralph }
12614837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
12714837Sralph if (errno == EWOULDBLOCK) /* active deamon present */
12814837Sralph exit(0);
12937968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
13014837Sralph exit(1);
13114837Sralph }
13214837Sralph ftruncate(lfd, 0);
13312875Sralph /*
13414837Sralph * write process id for others to know
13514837Sralph */
13614837Sralph sprintf(line, "%u\n", getpid());
13714837Sralph f = strlen(line);
13814837Sralph if (write(lfd, line, f) != f) {
13937968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
14014837Sralph exit(1);
14114837Sralph }
14214837Sralph signal(SIGCHLD, reapchild);
14314837Sralph /*
14412875Sralph * Restart all the printers.
14512875Sralph */
14612875Sralph startup();
14737968Sbostic (void) unlink(_PATH_SOCKETNAME);
14813816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0);
14913816Ssam if (funix < 0) {
15016761Sralph syslog(LOG_ERR, "socket: %m");
15112106Sralph exit(1);
15212106Sralph }
15314149Sralph #define mask(s) (1 << ((s) - 1))
15414149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
15516761Sralph signal(SIGHUP, mcleanup);
15616761Sralph signal(SIGINT, mcleanup);
15716761Sralph signal(SIGQUIT, mcleanup);
15816761Sralph signal(SIGTERM, mcleanup);
15966248Sbostic memset(&un, 0, sizeof(un));
16050886Storek un.sun_family = AF_UNIX;
16150886Storek strcpy(un.sun_path, _PATH_SOCKETNAME);
16258240Storek #ifndef SUN_LEN
16358240Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
16458240Storek #endif
16558240Storek if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
16616761Sralph syslog(LOG_ERR, "ubind: %m");
16712106Sralph exit(1);
16812106Sralph }
16913816Ssam sigsetmask(omask);
17056123Selan FD_ZERO(&defreadfds);
17156123Selan FD_SET(funix, &defreadfds);
17214149Sralph listen(funix, 5);
17313816Ssam finet = socket(AF_INET, SOCK_STREAM, 0);
17413816Ssam if (finet >= 0) {
17513816Ssam struct servent *sp;
17613816Ssam
17713816Ssam if (options & SO_DEBUG)
17813816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
17916761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
18055474Sbostic mcleanup(0);
18113816Ssam }
18213816Ssam sp = getservbyname("printer", "tcp");
18313816Ssam if (sp == NULL) {
18416761Sralph syslog(LOG_ERR, "printer/tcp: unknown service");
18555474Sbostic mcleanup(0);
18613816Ssam }
18766248Sbostic memset(&sin, 0, sizeof(sin));
18813816Ssam sin.sin_family = AF_INET;
18913816Ssam sin.sin_port = sp->s_port;
19046911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
19116761Sralph syslog(LOG_ERR, "bind: %m");
19255474Sbostic mcleanup(0);
19313816Ssam }
19466248Sbostic FD_SET(finet, &defreadfds);
19514149Sralph listen(finet, 5);
19613816Ssam }
19712106Sralph /*
19814149Sralph * Main loop: accept, do a request, continue.
19912106Sralph */
20066850Sbostic memset(&frominet, 0, sizeof(frominet));
20166850Sbostic memset(&fromunix, 0, sizeof(fromunix));
20212106Sralph for (;;) {
20356123Selan int domain, nfds, s;
20456123Selan fd_set readfds;
20512106Sralph
20656123Selan FD_COPY(&defreadfds, &readfds);
20713957Ssam nfds = select(20, &readfds, 0, 0, 0);
20813957Ssam if (nfds <= 0) {
20916761Sralph if (nfds < 0 && errno != EINTR)
21016761Sralph syslog(LOG_WARNING, "select: %m");
21113957Ssam continue;
21213957Ssam }
21356123Selan if (FD_ISSET(funix, &readfds)) {
21413957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix);
21546911Sbostic s = accept(funix,
21646911Sbostic (struct sockaddr *)&fromunix, &fromlen);
21756123Selan } else /* if (FD_ISSET(finet, &readfds)) */ {
21813957Ssam domain = AF_INET, fromlen = sizeof(frominet);
21946911Sbostic s = accept(finet,
22046911Sbostic (struct sockaddr *)&frominet, &fromlen);
22113816Ssam }
22212106Sralph if (s < 0) {
22316761Sralph if (errno != EINTR)
22416761Sralph syslog(LOG_WARNING, "accept: %m");
22516761Sralph continue;
22612106Sralph }
22712106Sralph if (fork() == 0) {
22813147Ssam signal(SIGCHLD, SIG_IGN);
22914708Sralph signal(SIGHUP, SIG_IGN);
23014708Sralph signal(SIGINT, SIG_IGN);
23114708Sralph signal(SIGQUIT, SIG_IGN);
23214708Sralph signal(SIGTERM, SIG_IGN);
23313816Ssam (void) close(funix);
23413816Ssam (void) close(finet);
23513816Ssam dup2(s, 1);
23613816Ssam (void) close(s);
23747055Smckusick if (domain == AF_INET) {
23847055Smckusick from_remote = 1;
23913816Ssam chkhost(&frominet);
24047055Smckusick } else
24147055Smckusick from_remote = 0;
24213816Ssam doit();
24312106Sralph exit(0);
24412106Sralph }
24512106Sralph (void) close(s);
24612106Sralph }
24712106Sralph }
24812106Sralph
24955474Sbostic static void
reapchild(signo)25055474Sbostic reapchild(signo)
25155474Sbostic int signo;
25212106Sralph {
25312106Sralph union wait status;
25412106Sralph
25546911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0)
25612106Sralph ;
25712106Sralph }
25812106Sralph
25955474Sbostic static void
mcleanup(signo)26055474Sbostic mcleanup(signo)
26155474Sbostic int signo;
26213816Ssam {
26314149Sralph if (lflag)
26416761Sralph syslog(LOG_INFO, "exiting");
26537968Sbostic unlink(_PATH_SOCKETNAME);
26613816Ssam exit(0);
26713816Ssam }
26813816Ssam
26912106Sralph /*
27012106Sralph * Stuff for handling job specifications
27112106Sralph */
27212106Sralph char *user[MAXUSERS]; /* users to process */
27312106Sralph int users; /* # of users in user array */
27412106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */
27512106Sralph int requests; /* # of spool requests */
27612875Sralph char *person; /* name of person doing lprm */
27712106Sralph
27854546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */
27954546Sleres char cbuf[BUFSIZ]; /* command line buffer */
28016761Sralph char *cmdnames[] = {
28112430Sralph "null",
28212430Sralph "printjob",
28312430Sralph "recvjob",
28412430Sralph "displayq short",
28512430Sralph "displayq long",
28612430Sralph "rmjob"
28712430Sralph };
28812106Sralph
28955474Sbostic static void
doit()29013816Ssam doit()
29112106Sralph {
29212106Sralph register char *cp;
29312106Sralph register int n;
29412106Sralph
29512106Sralph for (;;) {
29612106Sralph cp = cbuf;
29712106Sralph do {
29813816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1])
29913816Ssam fatal("Command line too long");
30013816Ssam if ((n = read(1, cp, 1)) != 1) {
30112106Sralph if (n < 0)
30212106Sralph fatal("Lost connection");
30312106Sralph return;
30412106Sralph }
30513816Ssam } while (*cp++ != '\n');
30612106Sralph *--cp = '\0';
30712106Sralph cp = cbuf;
30816761Sralph if (lflag) {
30916761Sralph if (*cp >= '\1' && *cp <= '\5')
31016761Sralph syslog(LOG_INFO, "%s requests %s %s",
31116761Sralph from, cmdnames[*cp], cp+1);
31216761Sralph else
31316761Sralph syslog(LOG_INFO, "bad request (%d) from %s",
31416761Sralph *cp, from);
31512430Sralph }
31612106Sralph switch (*cp++) {
31712106Sralph case '\1': /* check the queue and print any jobs there */
31812106Sralph printer = cp;
31912106Sralph printjob();
32012106Sralph break;
32112106Sralph case '\2': /* receive files to be queued */
32247055Smckusick if (!from_remote) {
32347055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp);
32447055Smckusick exit(1);
32547055Smckusick }
32612106Sralph printer = cp;
32712106Sralph recvjob();
32812106Sralph break;
32912430Sralph case '\3': /* display the queue (short form) */
33012430Sralph case '\4': /* display the queue (long form) */
33112106Sralph printer = cp;
33212106Sralph while (*cp) {
33312106Sralph if (*cp != ' ') {
33412106Sralph cp++;
33512106Sralph continue;
33612106Sralph }
33712106Sralph *cp++ = '\0';
33812106Sralph while (isspace(*cp))
33912106Sralph cp++;
34012106Sralph if (*cp == '\0')
34112106Sralph break;
34212106Sralph if (isdigit(*cp)) {
34312106Sralph if (requests >= MAXREQUESTS)
34412106Sralph fatal("Too many requests");
34512106Sralph requ[requests++] = atoi(cp);
34612106Sralph } else {
34712106Sralph if (users >= MAXUSERS)
34812106Sralph fatal("Too many users");
34912106Sralph user[users++] = cp;
35012106Sralph }
35112106Sralph }
35212106Sralph displayq(cbuf[0] - '\3');
35312106Sralph exit(0);
35412106Sralph case '\5': /* remove a job from the queue */
35547055Smckusick if (!from_remote) {
35647055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp);
35747055Smckusick exit(1);
35847055Smckusick }
35912106Sralph printer = cp;
36012106Sralph while (*cp && *cp != ' ')
36112106Sralph cp++;
36212106Sralph if (!*cp)
36312106Sralph break;
36412106Sralph *cp++ = '\0';
36512106Sralph person = cp;
36612106Sralph while (*cp) {
36712106Sralph if (*cp != ' ') {
36812106Sralph cp++;
36912106Sralph continue;
37012106Sralph }
37112106Sralph *cp++ = '\0';
37212106Sralph while (isspace(*cp))
37312106Sralph cp++;
37412106Sralph if (*cp == '\0')
37512106Sralph break;
37612106Sralph if (isdigit(*cp)) {
37712106Sralph if (requests >= MAXREQUESTS)
37812106Sralph fatal("Too many requests");
37912106Sralph requ[requests++] = atoi(cp);
38012106Sralph } else {
38112106Sralph if (users >= MAXUSERS)
38212106Sralph fatal("Too many users");
38312106Sralph user[users++] = cp;
38412106Sralph }
38512106Sralph }
38612106Sralph rmjob();
38712106Sralph break;
38812106Sralph }
38912106Sralph fatal("Illegal service request");
39012106Sralph }
39112106Sralph }
39212106Sralph
39312106Sralph /*
39412430Sralph * Make a pass through the printcap database and start printing any
39512430Sralph * files left from the last time the machine went down.
39612430Sralph */
39755474Sbostic static void
startup()39812430Sralph startup()
39912430Sralph {
40056123Selan char *buf;
40112430Sralph register char *cp;
40212430Sralph int pid;
40312430Sralph
40412430Sralph /*
40512430Sralph * Restart the daemons.
40612430Sralph */
40756123Selan while (cgetnext(&buf, printcapdb) > 0) {
408*69331Sbostic if (ckqueue(buf) <= 0) {
40968972Stef free(buf);
41068972Stef continue; /* no work to do for this printer */
41168972Stef }
41212430Sralph for (cp = buf; *cp; cp++)
41312430Sralph if (*cp == '|' || *cp == ':') {
41412430Sralph *cp = '\0';
41512430Sralph break;
41612430Sralph }
41768972Stef if (lflag)
41868972Stef syslog(LOG_INFO, "work for %s", buf);
41912430Sralph if ((pid = fork()) < 0) {
42016761Sralph syslog(LOG_WARNING, "startup: cannot fork");
42155474Sbostic mcleanup(0);
42212430Sralph }
42312430Sralph if (!pid) {
42465315Sbostic printer = buf;
42556123Selan cgetclose();
42612430Sralph printjob();
42768972Stef /* NOTREACHED */
42812430Sralph }
42968972Stef else free(buf);
43012430Sralph }
43112430Sralph }
43212430Sralph
43368972Stef /*
43468972Stef * Make sure there's some work to do before forking off a child
43568972Stef */
436*69331Sbostic static int
ckqueue(cap)437*69331Sbostic ckqueue(cap)
438*69331Sbostic char *cap;
43968972Stef {
44068972Stef register struct dirent *d;
44168972Stef DIR *dirp;
44268972Stef char *spooldir;
44368972Stef
444*69331Sbostic if (cgetstr(cap, "sd", &spooldir) == -1)
44568972Stef spooldir = _PATH_DEFSPOOL;
44668972Stef if ((dirp = opendir(spooldir)) == NULL)
44768972Stef return (-1);
44868972Stef while ((d = readdir(dirp)) != NULL) {
44968972Stef if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
45068972Stef continue; /* daemon control files only */
45168972Stef closedir(dirp);
45268972Stef return (1); /* found something */
45368972Stef }
45468972Stef closedir(dirp);
45568972Stef return (0);
45668972Stef }
45768972Stef
45827746Sbloom #define DUMMY ":nobody::"
45927746Sbloom
46012430Sralph /*
46112430Sralph * Check to see if the from host has access to the line printer.
46212430Sralph */
46355474Sbostic static void
chkhost(f)46413816Ssam chkhost(f)
46513816Ssam struct sockaddr_in *f;
46612430Sralph {
46713816Ssam register struct hostent *hp;
46812430Sralph register FILE *hostf;
46915551Sralph int first = 1;
47013816Ssam extern char *inet_ntoa();
47112430Sralph
47213816Ssam f->sin_port = ntohs(f->sin_port);
47313816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
47413816Ssam fatal("Malformed from address");
47554546Sleres
47654546Sleres /* Need real hostname for temporary filenames */
47746911Sbostic hp = gethostbyaddr((char *)&f->sin_addr,
47846911Sbostic sizeof(struct in_addr), f->sin_family);
47954546Sleres if (hp == NULL)
48013816Ssam fatal("Host name for your address (%s) unknown",
48113816Ssam inet_ntoa(f->sin_addr));
48213816Ssam
48354546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb));
48454546Sleres from[sizeof(fromb) - 1] = '\0';
48513816Ssam from = fromb;
48612734Sralph
48737968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r");
48815551Sralph again:
48915551Sralph if (hostf) {
49054546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr,
49154546Sleres DUMMY, DUMMY) == 0) {
49227746Sbloom (void) fclose(hostf);
49327746Sbloom return;
49412430Sralph }
49515551Sralph (void) fclose(hostf);
49612430Sralph }
49715551Sralph if (first == 1) {
49815551Sralph first = 0;
49937968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r");
50015551Sralph goto again;
50115551Sralph }
50213816Ssam fatal("Your host does not have line printer access");
50354546Sleres /*NOTREACHED*/
50412430Sralph }
50555474Sbostic
50655474Sbostic
50755474Sbostic
50855474Sbostic
50955474Sbostic
51055474Sbostic
51155474Sbostic
51255474Sbostic
51355474Sbostic
51455474Sbostic
51555474Sbostic
51655474Sbostic
517