122429Sdist /* 261843Sbostic * Copyright (c) 1983, 1993 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[] = 1161843Sbostic "@(#) Copyright (c) 1983, 1993\n\ 1261843Sbostic The Regents of the University of California. All rights reserved.\n"; 1334203Sbostic #endif /* not lint */ 1413957Ssam 1522429Sdist #ifndef lint 16*66248Sbostic static char sccsid[] = "@(#)lpd.c 8.3 (Berkeley) 02/23/94"; 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> 5455474Sbostic #include <netinet/in.h> 5556123Selan 5655474Sbostic #include <netdb.h> 5756123Selan #include <unistd.h> 5855474Sbostic #include <syslog.h> 5955474Sbostic #include <signal.h> 6055474Sbostic #include <errno.h> 6155474Sbostic #include <fcntl.h> 6255474Sbostic #include <dirent.h> 6355474Sbostic #include <stdio.h> 6456123Selan #include <stdlib.h> 6556123Selan #include <string.h> 6656123Selan #include <ctype.h> 6712106Sralph #include "lp.h" 6855474Sbostic #include "lp.local.h" 6937968Sbostic #include "pathnames.h" 7055474Sbostic #include "extern.h" 7112106Sralph 7216761Sralph int lflag; /* log requests flag */ 7347055Smckusick int from_remote; /* from remote socket */ 7412875Sralph 7555474Sbostic static void reapchild __P((int)); 7655474Sbostic static void mcleanup __P((int)); 7755474Sbostic static void doit __P((void)); 7855474Sbostic static void startup __P((void)); 7955474Sbostic static void chkhost __P((struct sockaddr_in *)); 8012106Sralph 8155474Sbostic int 8212106Sralph main(argc, argv) 8312106Sralph int argc; 8412106Sralph char **argv; 8512106Sralph { 8656123Selan int f, funix, finet, options, fromlen; 8756123Selan fd_set defreadfds; 8850886Storek struct sockaddr_un un, fromunix; 8913816Ssam struct sockaddr_in sin, frominet; 9014837Sralph int omask, lfd; 9112106Sralph 9256123Selan options = 0; 9312106Sralph gethostname(host, sizeof(host)); 9412106Sralph name = argv[0]; 9512106Sralph 9612106Sralph while (--argc > 0) { 9712106Sralph argv++; 9812106Sralph if (argv[0][0] == '-') 9912106Sralph switch (argv[0][1]) { 10012106Sralph case 'd': 10112106Sralph options |= SO_DEBUG; 10212106Sralph break; 10312106Sralph case 'l': 10412430Sralph lflag++; 10512430Sralph break; 10612106Sralph } 10712106Sralph } 10814837Sralph 10912106Sralph #ifndef DEBUG 11012106Sralph /* 11112106Sralph * Set up standard environment by detaching from the parent. 11212106Sralph */ 11344708Skarels daemon(0, 0); 11412106Sralph #endif 11514837Sralph 11625494Seric openlog("lpd", LOG_PID, LOG_LPR); 11756923Sbostic syslog(LOG_INFO, "restarted"); 11812106Sralph (void) umask(0); 11937968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644); 12014837Sralph if (lfd < 0) { 12137968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12214837Sralph exit(1); 12314837Sralph } 12414837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 12514837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 12614837Sralph exit(0); 12737968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12814837Sralph exit(1); 12914837Sralph } 13014837Sralph ftruncate(lfd, 0); 13112875Sralph /* 13214837Sralph * write process id for others to know 13314837Sralph */ 13414837Sralph sprintf(line, "%u\n", getpid()); 13514837Sralph f = strlen(line); 13614837Sralph if (write(lfd, line, f) != f) { 13737968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 13814837Sralph exit(1); 13914837Sralph } 14014837Sralph signal(SIGCHLD, reapchild); 14114837Sralph /* 14212875Sralph * Restart all the printers. 14312875Sralph */ 14412875Sralph startup(); 14537968Sbostic (void) unlink(_PATH_SOCKETNAME); 14613816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 14713816Ssam if (funix < 0) { 14816761Sralph syslog(LOG_ERR, "socket: %m"); 14912106Sralph exit(1); 15012106Sralph } 15114149Sralph #define mask(s) (1 << ((s) - 1)) 15214149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 15316761Sralph signal(SIGHUP, mcleanup); 15416761Sralph signal(SIGINT, mcleanup); 15516761Sralph signal(SIGQUIT, mcleanup); 15616761Sralph signal(SIGTERM, mcleanup); 157*66248Sbostic memset(&un, 0, sizeof(un)); 15850886Storek un.sun_family = AF_UNIX; 15950886Storek strcpy(un.sun_path, _PATH_SOCKETNAME); 16058240Storek #ifndef SUN_LEN 16158240Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 16258240Storek #endif 16358240Storek if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 16416761Sralph syslog(LOG_ERR, "ubind: %m"); 16512106Sralph exit(1); 16612106Sralph } 16713816Ssam sigsetmask(omask); 16856123Selan FD_ZERO(&defreadfds); 16956123Selan FD_SET(funix, &defreadfds); 17014149Sralph listen(funix, 5); 17113816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 17213816Ssam if (finet >= 0) { 17313816Ssam struct servent *sp; 17413816Ssam 17513816Ssam if (options & SO_DEBUG) 17613816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 17716761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 17855474Sbostic mcleanup(0); 17913816Ssam } 18013816Ssam sp = getservbyname("printer", "tcp"); 18113816Ssam if (sp == NULL) { 18216761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 18355474Sbostic mcleanup(0); 18413816Ssam } 185*66248Sbostic memset(&sin, 0, sizeof(sin)); 18613816Ssam sin.sin_family = AF_INET; 18713816Ssam sin.sin_port = sp->s_port; 18846911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 18916761Sralph syslog(LOG_ERR, "bind: %m"); 19055474Sbostic mcleanup(0); 19113816Ssam } 192*66248Sbostic FD_SET(finet, &defreadfds); 19314149Sralph listen(finet, 5); 19413816Ssam } 19512106Sralph /* 19614149Sralph * Main loop: accept, do a request, continue. 19712106Sralph */ 19812106Sralph for (;;) { 19956123Selan int domain, nfds, s; 20056123Selan fd_set readfds; 20112106Sralph 20256123Selan FD_COPY(&defreadfds, &readfds); 20313957Ssam nfds = select(20, &readfds, 0, 0, 0); 20413957Ssam if (nfds <= 0) { 20516761Sralph if (nfds < 0 && errno != EINTR) 20616761Sralph syslog(LOG_WARNING, "select: %m"); 20713957Ssam continue; 20813957Ssam } 20956123Selan if (FD_ISSET(funix, &readfds)) { 21013957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 21146911Sbostic s = accept(funix, 21246911Sbostic (struct sockaddr *)&fromunix, &fromlen); 21356123Selan } else /* if (FD_ISSET(finet, &readfds)) */ { 21413957Ssam domain = AF_INET, fromlen = sizeof(frominet); 21546911Sbostic s = accept(finet, 21646911Sbostic (struct sockaddr *)&frominet, &fromlen); 21713816Ssam } 21812106Sralph if (s < 0) { 21916761Sralph if (errno != EINTR) 22016761Sralph syslog(LOG_WARNING, "accept: %m"); 22116761Sralph continue; 22212106Sralph } 22312106Sralph if (fork() == 0) { 22413147Ssam signal(SIGCHLD, SIG_IGN); 22514708Sralph signal(SIGHUP, SIG_IGN); 22614708Sralph signal(SIGINT, SIG_IGN); 22714708Sralph signal(SIGQUIT, SIG_IGN); 22814708Sralph signal(SIGTERM, SIG_IGN); 22913816Ssam (void) close(funix); 23013816Ssam (void) close(finet); 23113816Ssam dup2(s, 1); 23213816Ssam (void) close(s); 23347055Smckusick if (domain == AF_INET) { 23447055Smckusick from_remote = 1; 23513816Ssam chkhost(&frominet); 23647055Smckusick } else 23747055Smckusick from_remote = 0; 23813816Ssam doit(); 23912106Sralph exit(0); 24012106Sralph } 24112106Sralph (void) close(s); 24212106Sralph } 24312106Sralph } 24412106Sralph 24555474Sbostic static void 24655474Sbostic reapchild(signo) 24755474Sbostic int signo; 24812106Sralph { 24912106Sralph union wait status; 25012106Sralph 25146911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 25212106Sralph ; 25312106Sralph } 25412106Sralph 25555474Sbostic static void 25655474Sbostic mcleanup(signo) 25755474Sbostic int signo; 25813816Ssam { 25914149Sralph if (lflag) 26016761Sralph syslog(LOG_INFO, "exiting"); 26137968Sbostic unlink(_PATH_SOCKETNAME); 26213816Ssam exit(0); 26313816Ssam } 26413816Ssam 26512106Sralph /* 26612106Sralph * Stuff for handling job specifications 26712106Sralph */ 26812106Sralph char *user[MAXUSERS]; /* users to process */ 26912106Sralph int users; /* # of users in user array */ 27012106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 27112106Sralph int requests; /* # of spool requests */ 27212875Sralph char *person; /* name of person doing lprm */ 27312106Sralph 27454546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ 27554546Sleres char cbuf[BUFSIZ]; /* command line buffer */ 27616761Sralph char *cmdnames[] = { 27712430Sralph "null", 27812430Sralph "printjob", 27912430Sralph "recvjob", 28012430Sralph "displayq short", 28112430Sralph "displayq long", 28212430Sralph "rmjob" 28312430Sralph }; 28412106Sralph 28555474Sbostic static void 28613816Ssam doit() 28712106Sralph { 28812106Sralph register char *cp; 28912106Sralph register int n; 29012106Sralph 29112106Sralph for (;;) { 29212106Sralph cp = cbuf; 29312106Sralph do { 29413816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 29513816Ssam fatal("Command line too long"); 29613816Ssam if ((n = read(1, cp, 1)) != 1) { 29712106Sralph if (n < 0) 29812106Sralph fatal("Lost connection"); 29912106Sralph return; 30012106Sralph } 30113816Ssam } while (*cp++ != '\n'); 30212106Sralph *--cp = '\0'; 30312106Sralph cp = cbuf; 30416761Sralph if (lflag) { 30516761Sralph if (*cp >= '\1' && *cp <= '\5') 30616761Sralph syslog(LOG_INFO, "%s requests %s %s", 30716761Sralph from, cmdnames[*cp], cp+1); 30816761Sralph else 30916761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 31016761Sralph *cp, from); 31112430Sralph } 31212106Sralph switch (*cp++) { 31312106Sralph case '\1': /* check the queue and print any jobs there */ 31412106Sralph printer = cp; 31512106Sralph printjob(); 31612106Sralph break; 31712106Sralph case '\2': /* receive files to be queued */ 31847055Smckusick if (!from_remote) { 31947055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 32047055Smckusick exit(1); 32147055Smckusick } 32212106Sralph printer = cp; 32312106Sralph recvjob(); 32412106Sralph break; 32512430Sralph case '\3': /* display the queue (short form) */ 32612430Sralph case '\4': /* display the queue (long form) */ 32712106Sralph printer = cp; 32812106Sralph while (*cp) { 32912106Sralph if (*cp != ' ') { 33012106Sralph cp++; 33112106Sralph continue; 33212106Sralph } 33312106Sralph *cp++ = '\0'; 33412106Sralph while (isspace(*cp)) 33512106Sralph cp++; 33612106Sralph if (*cp == '\0') 33712106Sralph break; 33812106Sralph if (isdigit(*cp)) { 33912106Sralph if (requests >= MAXREQUESTS) 34012106Sralph fatal("Too many requests"); 34112106Sralph requ[requests++] = atoi(cp); 34212106Sralph } else { 34312106Sralph if (users >= MAXUSERS) 34412106Sralph fatal("Too many users"); 34512106Sralph user[users++] = cp; 34612106Sralph } 34712106Sralph } 34812106Sralph displayq(cbuf[0] - '\3'); 34912106Sralph exit(0); 35012106Sralph case '\5': /* remove a job from the queue */ 35147055Smckusick if (!from_remote) { 35247055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 35347055Smckusick exit(1); 35447055Smckusick } 35512106Sralph printer = cp; 35612106Sralph while (*cp && *cp != ' ') 35712106Sralph cp++; 35812106Sralph if (!*cp) 35912106Sralph break; 36012106Sralph *cp++ = '\0'; 36112106Sralph person = cp; 36212106Sralph while (*cp) { 36312106Sralph if (*cp != ' ') { 36412106Sralph cp++; 36512106Sralph continue; 36612106Sralph } 36712106Sralph *cp++ = '\0'; 36812106Sralph while (isspace(*cp)) 36912106Sralph cp++; 37012106Sralph if (*cp == '\0') 37112106Sralph break; 37212106Sralph if (isdigit(*cp)) { 37312106Sralph if (requests >= MAXREQUESTS) 37412106Sralph fatal("Too many requests"); 37512106Sralph requ[requests++] = atoi(cp); 37612106Sralph } else { 37712106Sralph if (users >= MAXUSERS) 37812106Sralph fatal("Too many users"); 37912106Sralph user[users++] = cp; 38012106Sralph } 38112106Sralph } 38212106Sralph rmjob(); 38312106Sralph break; 38412106Sralph } 38512106Sralph fatal("Illegal service request"); 38612106Sralph } 38712106Sralph } 38812106Sralph 38912106Sralph /* 39012430Sralph * Make a pass through the printcap database and start printing any 39112430Sralph * files left from the last time the machine went down. 39212430Sralph */ 39355474Sbostic static void 39412430Sralph startup() 39512430Sralph { 39656123Selan char *buf; 39712430Sralph register char *cp; 39812430Sralph int pid; 39912430Sralph 40012430Sralph /* 40112430Sralph * Restart the daemons. 40212430Sralph */ 40356123Selan while (cgetnext(&buf, printcapdb) > 0) { 40412430Sralph for (cp = buf; *cp; cp++) 40512430Sralph if (*cp == '|' || *cp == ':') { 40612430Sralph *cp = '\0'; 40712430Sralph break; 40812430Sralph } 40912430Sralph if ((pid = fork()) < 0) { 41016761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 41155474Sbostic mcleanup(0); 41212430Sralph } 41312430Sralph if (!pid) { 41465315Sbostic printer = buf; 41556123Selan cgetclose(); 41612430Sralph printjob(); 41712430Sralph } 41812430Sralph } 41912430Sralph } 42012430Sralph 42127746Sbloom #define DUMMY ":nobody::" 42227746Sbloom 42312430Sralph /* 42412430Sralph * Check to see if the from host has access to the line printer. 42512430Sralph */ 42655474Sbostic static void 42713816Ssam chkhost(f) 42813816Ssam struct sockaddr_in *f; 42912430Sralph { 43013816Ssam register struct hostent *hp; 43112430Sralph register FILE *hostf; 43215551Sralph int first = 1; 43313816Ssam extern char *inet_ntoa(); 43412430Sralph 43513816Ssam f->sin_port = ntohs(f->sin_port); 43613816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 43713816Ssam fatal("Malformed from address"); 43854546Sleres 43954546Sleres /* Need real hostname for temporary filenames */ 44046911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 44146911Sbostic sizeof(struct in_addr), f->sin_family); 44254546Sleres if (hp == NULL) 44313816Ssam fatal("Host name for your address (%s) unknown", 44413816Ssam inet_ntoa(f->sin_addr)); 44513816Ssam 44654546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb)); 44754546Sleres from[sizeof(fromb) - 1] = '\0'; 44813816Ssam from = fromb; 44912734Sralph 45037968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 45115551Sralph again: 45215551Sralph if (hostf) { 45354546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr, 45454546Sleres DUMMY, DUMMY) == 0) { 45527746Sbloom (void) fclose(hostf); 45627746Sbloom return; 45712430Sralph } 45815551Sralph (void) fclose(hostf); 45912430Sralph } 46015551Sralph if (first == 1) { 46115551Sralph first = 0; 46237968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 46315551Sralph goto again; 46415551Sralph } 46513816Ssam fatal("Your host does not have line printer access"); 46654546Sleres /*NOTREACHED*/ 46712430Sralph } 46855474Sbostic 46955474Sbostic 47055474Sbostic 47155474Sbostic 47255474Sbostic 47355474Sbostic 47455474Sbostic 47555474Sbostic 47655474Sbostic 47755474Sbostic 47855474Sbostic 47955474Sbostic 480