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*69061Stef static char sccsid[] = "@(#)lpd.c 8.6 (Berkeley) 04/28/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> 54*69061Stef #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 *)); 8112106Sralph 8255474Sbostic int 8312106Sralph main(argc, argv) 8412106Sralph int argc; 8512106Sralph char **argv; 8612106Sralph { 8756123Selan int f, funix, finet, options, fromlen; 8856123Selan fd_set defreadfds; 8950886Storek struct sockaddr_un un, fromunix; 9013816Ssam struct sockaddr_in sin, frominet; 9114837Sralph int omask, lfd; 9212106Sralph 9356123Selan options = 0; 9412106Sralph gethostname(host, sizeof(host)); 9512106Sralph name = argv[0]; 9612106Sralph 9712106Sralph while (--argc > 0) { 9812106Sralph argv++; 9912106Sralph if (argv[0][0] == '-') 10012106Sralph switch (argv[0][1]) { 10112106Sralph case 'd': 10212106Sralph options |= SO_DEBUG; 10312106Sralph break; 10412106Sralph case 'l': 10512430Sralph lflag++; 10612430Sralph break; 10712106Sralph } 10812106Sralph } 10914837Sralph 11012106Sralph #ifndef DEBUG 11112106Sralph /* 11212106Sralph * Set up standard environment by detaching from the parent. 11312106Sralph */ 11444708Skarels daemon(0, 0); 11512106Sralph #endif 11614837Sralph 11725494Seric openlog("lpd", LOG_PID, LOG_LPR); 11856923Sbostic syslog(LOG_INFO, "restarted"); 11912106Sralph (void) umask(0); 12037968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644); 12114837Sralph if (lfd < 0) { 12237968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12314837Sralph exit(1); 12414837Sralph } 12514837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 12614837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 12714837Sralph exit(0); 12837968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12914837Sralph exit(1); 13014837Sralph } 13114837Sralph ftruncate(lfd, 0); 13212875Sralph /* 13314837Sralph * write process id for others to know 13414837Sralph */ 13514837Sralph sprintf(line, "%u\n", getpid()); 13614837Sralph f = strlen(line); 13714837Sralph if (write(lfd, line, f) != f) { 13837968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 13914837Sralph exit(1); 14014837Sralph } 14114837Sralph signal(SIGCHLD, reapchild); 14214837Sralph /* 14312875Sralph * Restart all the printers. 14412875Sralph */ 14512875Sralph startup(); 14637968Sbostic (void) unlink(_PATH_SOCKETNAME); 14713816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 14813816Ssam if (funix < 0) { 14916761Sralph syslog(LOG_ERR, "socket: %m"); 15012106Sralph exit(1); 15112106Sralph } 15214149Sralph #define mask(s) (1 << ((s) - 1)) 15314149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 15416761Sralph signal(SIGHUP, mcleanup); 15516761Sralph signal(SIGINT, mcleanup); 15616761Sralph signal(SIGQUIT, mcleanup); 15716761Sralph signal(SIGTERM, mcleanup); 15866248Sbostic memset(&un, 0, sizeof(un)); 15950886Storek un.sun_family = AF_UNIX; 16050886Storek strcpy(un.sun_path, _PATH_SOCKETNAME); 16158240Storek #ifndef SUN_LEN 16258240Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 16358240Storek #endif 16458240Storek if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 16516761Sralph syslog(LOG_ERR, "ubind: %m"); 16612106Sralph exit(1); 16712106Sralph } 16813816Ssam sigsetmask(omask); 16956123Selan FD_ZERO(&defreadfds); 17056123Selan FD_SET(funix, &defreadfds); 17114149Sralph listen(funix, 5); 17213816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 17313816Ssam if (finet >= 0) { 17413816Ssam struct servent *sp; 17513816Ssam 17613816Ssam if (options & SO_DEBUG) 17713816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 17816761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 17955474Sbostic mcleanup(0); 18013816Ssam } 18113816Ssam sp = getservbyname("printer", "tcp"); 18213816Ssam if (sp == NULL) { 18316761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 18455474Sbostic mcleanup(0); 18513816Ssam } 18666248Sbostic memset(&sin, 0, sizeof(sin)); 18713816Ssam sin.sin_family = AF_INET; 18813816Ssam sin.sin_port = sp->s_port; 18946911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 19016761Sralph syslog(LOG_ERR, "bind: %m"); 19155474Sbostic mcleanup(0); 19213816Ssam } 19366248Sbostic FD_SET(finet, &defreadfds); 19414149Sralph listen(finet, 5); 19513816Ssam } 19612106Sralph /* 19714149Sralph * Main loop: accept, do a request, continue. 19812106Sralph */ 19966850Sbostic memset(&frominet, 0, sizeof(frominet)); 20066850Sbostic memset(&fromunix, 0, sizeof(fromunix)); 20112106Sralph for (;;) { 20256123Selan int domain, nfds, s; 20356123Selan fd_set readfds; 20412106Sralph 20556123Selan FD_COPY(&defreadfds, &readfds); 20613957Ssam nfds = select(20, &readfds, 0, 0, 0); 20713957Ssam if (nfds <= 0) { 20816761Sralph if (nfds < 0 && errno != EINTR) 20916761Sralph syslog(LOG_WARNING, "select: %m"); 21013957Ssam continue; 21113957Ssam } 21256123Selan if (FD_ISSET(funix, &readfds)) { 21313957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 21446911Sbostic s = accept(funix, 21546911Sbostic (struct sockaddr *)&fromunix, &fromlen); 21656123Selan } else /* if (FD_ISSET(finet, &readfds)) */ { 21713957Ssam domain = AF_INET, fromlen = sizeof(frominet); 21846911Sbostic s = accept(finet, 21946911Sbostic (struct sockaddr *)&frominet, &fromlen); 22013816Ssam } 22112106Sralph if (s < 0) { 22216761Sralph if (errno != EINTR) 22316761Sralph syslog(LOG_WARNING, "accept: %m"); 22416761Sralph continue; 22512106Sralph } 22612106Sralph if (fork() == 0) { 22713147Ssam signal(SIGCHLD, SIG_IGN); 22814708Sralph signal(SIGHUP, SIG_IGN); 22914708Sralph signal(SIGINT, SIG_IGN); 23014708Sralph signal(SIGQUIT, SIG_IGN); 23114708Sralph signal(SIGTERM, SIG_IGN); 23213816Ssam (void) close(funix); 23313816Ssam (void) close(finet); 23413816Ssam dup2(s, 1); 23513816Ssam (void) close(s); 23647055Smckusick if (domain == AF_INET) { 23747055Smckusick from_remote = 1; 23813816Ssam chkhost(&frominet); 23947055Smckusick } else 24047055Smckusick from_remote = 0; 24113816Ssam doit(); 24212106Sralph exit(0); 24312106Sralph } 24412106Sralph (void) close(s); 24512106Sralph } 24612106Sralph } 24712106Sralph 24855474Sbostic static void 24955474Sbostic reapchild(signo) 25055474Sbostic int signo; 25112106Sralph { 25212106Sralph union wait status; 25312106Sralph 25446911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 25512106Sralph ; 25612106Sralph } 25712106Sralph 25855474Sbostic static void 25955474Sbostic mcleanup(signo) 26055474Sbostic int signo; 26113816Ssam { 26214149Sralph if (lflag) 26316761Sralph syslog(LOG_INFO, "exiting"); 26437968Sbostic unlink(_PATH_SOCKETNAME); 26513816Ssam exit(0); 26613816Ssam } 26713816Ssam 26812106Sralph /* 26912106Sralph * Stuff for handling job specifications 27012106Sralph */ 27112106Sralph char *user[MAXUSERS]; /* users to process */ 27212106Sralph int users; /* # of users in user array */ 27312106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 27412106Sralph int requests; /* # of spool requests */ 27512875Sralph char *person; /* name of person doing lprm */ 27612106Sralph 27754546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ 27854546Sleres char cbuf[BUFSIZ]; /* command line buffer */ 27916761Sralph char *cmdnames[] = { 28012430Sralph "null", 28112430Sralph "printjob", 28212430Sralph "recvjob", 28312430Sralph "displayq short", 28412430Sralph "displayq long", 28512430Sralph "rmjob" 28612430Sralph }; 28712106Sralph 28855474Sbostic static void 28913816Ssam doit() 29012106Sralph { 29112106Sralph register char *cp; 29212106Sralph register int n; 29312106Sralph 29412106Sralph for (;;) { 29512106Sralph cp = cbuf; 29612106Sralph do { 29713816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 29813816Ssam fatal("Command line too long"); 29913816Ssam if ((n = read(1, cp, 1)) != 1) { 30012106Sralph if (n < 0) 30112106Sralph fatal("Lost connection"); 30212106Sralph return; 30312106Sralph } 30413816Ssam } while (*cp++ != '\n'); 30512106Sralph *--cp = '\0'; 30612106Sralph cp = cbuf; 30716761Sralph if (lflag) { 30816761Sralph if (*cp >= '\1' && *cp <= '\5') 30916761Sralph syslog(LOG_INFO, "%s requests %s %s", 31016761Sralph from, cmdnames[*cp], cp+1); 31116761Sralph else 31216761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 31316761Sralph *cp, from); 31412430Sralph } 31512106Sralph switch (*cp++) { 31612106Sralph case '\1': /* check the queue and print any jobs there */ 31712106Sralph printer = cp; 31812106Sralph printjob(); 31912106Sralph break; 32012106Sralph case '\2': /* receive files to be queued */ 32147055Smckusick if (!from_remote) { 32247055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 32347055Smckusick exit(1); 32447055Smckusick } 32512106Sralph printer = cp; 32612106Sralph recvjob(); 32712106Sralph break; 32812430Sralph case '\3': /* display the queue (short form) */ 32912430Sralph case '\4': /* display the queue (long form) */ 33012106Sralph printer = cp; 33112106Sralph while (*cp) { 33212106Sralph if (*cp != ' ') { 33312106Sralph cp++; 33412106Sralph continue; 33512106Sralph } 33612106Sralph *cp++ = '\0'; 33712106Sralph while (isspace(*cp)) 33812106Sralph cp++; 33912106Sralph if (*cp == '\0') 34012106Sralph break; 34112106Sralph if (isdigit(*cp)) { 34212106Sralph if (requests >= MAXREQUESTS) 34312106Sralph fatal("Too many requests"); 34412106Sralph requ[requests++] = atoi(cp); 34512106Sralph } else { 34612106Sralph if (users >= MAXUSERS) 34712106Sralph fatal("Too many users"); 34812106Sralph user[users++] = cp; 34912106Sralph } 35012106Sralph } 35112106Sralph displayq(cbuf[0] - '\3'); 35212106Sralph exit(0); 35312106Sralph case '\5': /* remove a job from the queue */ 35447055Smckusick if (!from_remote) { 35547055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 35647055Smckusick exit(1); 35747055Smckusick } 35812106Sralph printer = cp; 35912106Sralph while (*cp && *cp != ' ') 36012106Sralph cp++; 36112106Sralph if (!*cp) 36212106Sralph break; 36312106Sralph *cp++ = '\0'; 36412106Sralph person = cp; 36512106Sralph while (*cp) { 36612106Sralph if (*cp != ' ') { 36712106Sralph cp++; 36812106Sralph continue; 36912106Sralph } 37012106Sralph *cp++ = '\0'; 37112106Sralph while (isspace(*cp)) 37212106Sralph cp++; 37312106Sralph if (*cp == '\0') 37412106Sralph break; 37512106Sralph if (isdigit(*cp)) { 37612106Sralph if (requests >= MAXREQUESTS) 37712106Sralph fatal("Too many requests"); 37812106Sralph requ[requests++] = atoi(cp); 37912106Sralph } else { 38012106Sralph if (users >= MAXUSERS) 38112106Sralph fatal("Too many users"); 38212106Sralph user[users++] = cp; 38312106Sralph } 38412106Sralph } 38512106Sralph rmjob(); 38612106Sralph break; 38712106Sralph } 38812106Sralph fatal("Illegal service request"); 38912106Sralph } 39012106Sralph } 39112106Sralph 39212106Sralph /* 39312430Sralph * Make a pass through the printcap database and start printing any 39412430Sralph * files left from the last time the machine went down. 39512430Sralph */ 39655474Sbostic static void 39712430Sralph startup() 39812430Sralph { 39956123Selan char *buf; 40012430Sralph register char *cp; 40112430Sralph int pid; 40212430Sralph 40312430Sralph /* 40412430Sralph * Restart the daemons. 40512430Sralph */ 40656123Selan while (cgetnext(&buf, printcapdb) > 0) { 407*69061Stef if (ckqueue() <= 0) { 40868972Stef free(buf); 40968972Stef continue; /* no work to do for this printer */ 41068972Stef } 41112430Sralph for (cp = buf; *cp; cp++) 41212430Sralph if (*cp == '|' || *cp == ':') { 41312430Sralph *cp = '\0'; 41412430Sralph break; 41512430Sralph } 41668972Stef if (lflag) 41768972Stef syslog(LOG_INFO, "work for %s", buf); 41812430Sralph if ((pid = fork()) < 0) { 41916761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 42055474Sbostic mcleanup(0); 42112430Sralph } 42212430Sralph if (!pid) { 42365315Sbostic printer = buf; 42456123Selan cgetclose(); 42512430Sralph printjob(); 42668972Stef /* NOTREACHED */ 42712430Sralph } 42868972Stef else free(buf); 42912430Sralph } 43012430Sralph } 43112430Sralph 43268972Stef /* 43368972Stef * Make sure there's some work to do before forking off a child 43468972Stef */ 43568972Stef ckqueue() 43668972Stef { 43768972Stef register struct dirent *d; 43868972Stef DIR *dirp; 43968972Stef char *spooldir; 44068972Stef 44168972Stef if (cgetstr(bp, "sd", &spooldir) == -1) 44268972Stef spooldir = _PATH_DEFSPOOL; 44368972Stef if ((dirp = opendir(spooldir)) == NULL) 44468972Stef return (-1); 44568972Stef while ((d = readdir(dirp)) != NULL) { 44668972Stef if (d->d_name[0] != 'c' || d->d_name[1] != 'f') 44768972Stef continue; /* daemon control files only */ 44868972Stef closedir(dirp); 44968972Stef return (1); /* found something */ 45068972Stef } 45168972Stef closedir(dirp); 45268972Stef return (0); 45368972Stef } 45468972Stef 45527746Sbloom #define DUMMY ":nobody::" 45627746Sbloom 45712430Sralph /* 45812430Sralph * Check to see if the from host has access to the line printer. 45912430Sralph */ 46055474Sbostic static void 46113816Ssam chkhost(f) 46213816Ssam struct sockaddr_in *f; 46312430Sralph { 46413816Ssam register struct hostent *hp; 46512430Sralph register FILE *hostf; 46615551Sralph int first = 1; 46713816Ssam extern char *inet_ntoa(); 46812430Sralph 46913816Ssam f->sin_port = ntohs(f->sin_port); 47013816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 47113816Ssam fatal("Malformed from address"); 47254546Sleres 47354546Sleres /* Need real hostname for temporary filenames */ 47446911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 47546911Sbostic sizeof(struct in_addr), f->sin_family); 47654546Sleres if (hp == NULL) 47713816Ssam fatal("Host name for your address (%s) unknown", 47813816Ssam inet_ntoa(f->sin_addr)); 47913816Ssam 48054546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb)); 48154546Sleres from[sizeof(fromb) - 1] = '\0'; 48213816Ssam from = fromb; 48312734Sralph 48437968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 48515551Sralph again: 48615551Sralph if (hostf) { 48754546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr, 48854546Sleres DUMMY, DUMMY) == 0) { 48927746Sbloom (void) fclose(hostf); 49027746Sbloom return; 49112430Sralph } 49215551Sralph (void) fclose(hostf); 49312430Sralph } 49415551Sralph if (first == 1) { 49515551Sralph first = 0; 49637968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 49715551Sralph goto again; 49815551Sralph } 49913816Ssam fatal("Your host does not have line printer access"); 50054546Sleres /*NOTREACHED*/ 50112430Sralph } 50255474Sbostic 50355474Sbostic 50455474Sbostic 50555474Sbostic 50655474Sbostic 50755474Sbostic 50855474Sbostic 50955474Sbostic 51055474Sbostic 51155474Sbostic 51255474Sbostic 51355474Sbostic 514