122429Sdist /* 256251Selan * Copyright (c) 1983 The Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 556123Selan * 656251Selan * %sccs.include.redist.c% 722429Sdist */ 822429Sdist 913957Ssam #ifndef lint 10*56265Selan static char copyright[] = 1156251Selan "@(#) Copyright (c) 1983 The Regents of the University of California.\n\ 1222429Sdist All rights reserved.\n"; 1334203Sbostic #endif /* not lint */ 1413957Ssam 1522429Sdist #ifndef lint 16*56265Selan static char sccsid[] = "@(#)lpd.c 5.18 (Berkeley) 09/16/92"; 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); 11712106Sralph (void) umask(0); 11837968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644); 11914837Sralph if (lfd < 0) { 12037968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12114837Sralph exit(1); 12214837Sralph } 12314837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 12414837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 12514837Sralph exit(0); 12637968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12714837Sralph exit(1); 12814837Sralph } 12914837Sralph ftruncate(lfd, 0); 13012875Sralph /* 13114837Sralph * write process id for others to know 13214837Sralph */ 13314837Sralph sprintf(line, "%u\n", getpid()); 13414837Sralph f = strlen(line); 13514837Sralph if (write(lfd, line, f) != f) { 13637968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 13714837Sralph exit(1); 13814837Sralph } 13914837Sralph signal(SIGCHLD, reapchild); 14014837Sralph /* 14112875Sralph * Restart all the printers. 14212875Sralph */ 14312875Sralph startup(); 14437968Sbostic (void) unlink(_PATH_SOCKETNAME); 14513816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 14613816Ssam if (funix < 0) { 14716761Sralph syslog(LOG_ERR, "socket: %m"); 14812106Sralph exit(1); 14912106Sralph } 15014149Sralph #define mask(s) (1 << ((s) - 1)) 15114149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 15216761Sralph signal(SIGHUP, mcleanup); 15316761Sralph signal(SIGINT, mcleanup); 15416761Sralph signal(SIGQUIT, mcleanup); 15516761Sralph signal(SIGTERM, mcleanup); 15650886Storek un.sun_family = AF_UNIX; 15750886Storek strcpy(un.sun_path, _PATH_SOCKETNAME); 15846911Sbostic if (bind(funix, 15950886Storek (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) { 16016761Sralph syslog(LOG_ERR, "ubind: %m"); 16112106Sralph exit(1); 16212106Sralph } 16313816Ssam sigsetmask(omask); 16456123Selan FD_ZERO(&defreadfds); 16556123Selan FD_SET(funix, &defreadfds); 16614149Sralph listen(funix, 5); 16713816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 16813816Ssam if (finet >= 0) { 16913816Ssam struct servent *sp; 17013816Ssam 17113816Ssam if (options & SO_DEBUG) 17213816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 17316761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 17455474Sbostic mcleanup(0); 17513816Ssam } 17613816Ssam sp = getservbyname("printer", "tcp"); 17713816Ssam if (sp == NULL) { 17816761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 17955474Sbostic mcleanup(0); 18013816Ssam } 18113816Ssam sin.sin_family = AF_INET; 18213816Ssam sin.sin_port = sp->s_port; 18346911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 18416761Sralph syslog(LOG_ERR, "bind: %m"); 18555474Sbostic mcleanup(0); 18613816Ssam } 18756123Selan FD_SET(finet, &defreadfds); 18814149Sralph listen(finet, 5); 18913816Ssam } 19012106Sralph /* 19114149Sralph * Main loop: accept, do a request, continue. 19212106Sralph */ 19312106Sralph for (;;) { 19456123Selan int domain, nfds, s; 19556123Selan fd_set readfds; 19612106Sralph 19756123Selan FD_COPY(&defreadfds, &readfds); 19813957Ssam nfds = select(20, &readfds, 0, 0, 0); 19913957Ssam if (nfds <= 0) { 20016761Sralph if (nfds < 0 && errno != EINTR) 20116761Sralph syslog(LOG_WARNING, "select: %m"); 20213957Ssam continue; 20313957Ssam } 20456123Selan if (FD_ISSET(funix, &readfds)) { 20513957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 20646911Sbostic s = accept(funix, 20746911Sbostic (struct sockaddr *)&fromunix, &fromlen); 20856123Selan } else /* if (FD_ISSET(finet, &readfds)) */ { 20913957Ssam domain = AF_INET, fromlen = sizeof(frominet); 21046911Sbostic s = accept(finet, 21146911Sbostic (struct sockaddr *)&frominet, &fromlen); 21213816Ssam } 21312106Sralph if (s < 0) { 21416761Sralph if (errno != EINTR) 21516761Sralph syslog(LOG_WARNING, "accept: %m"); 21616761Sralph continue; 21712106Sralph } 21812106Sralph if (fork() == 0) { 21913147Ssam signal(SIGCHLD, SIG_IGN); 22014708Sralph signal(SIGHUP, SIG_IGN); 22114708Sralph signal(SIGINT, SIG_IGN); 22214708Sralph signal(SIGQUIT, SIG_IGN); 22314708Sralph signal(SIGTERM, SIG_IGN); 22413816Ssam (void) close(funix); 22513816Ssam (void) close(finet); 22613816Ssam dup2(s, 1); 22713816Ssam (void) close(s); 22847055Smckusick if (domain == AF_INET) { 22947055Smckusick from_remote = 1; 23013816Ssam chkhost(&frominet); 23147055Smckusick } else 23247055Smckusick from_remote = 0; 23313816Ssam doit(); 23412106Sralph exit(0); 23512106Sralph } 23612106Sralph (void) close(s); 23712106Sralph } 23812106Sralph } 23912106Sralph 24055474Sbostic static void 24155474Sbostic reapchild(signo) 24255474Sbostic int signo; 24312106Sralph { 24412106Sralph union wait status; 24512106Sralph 24646911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 24712106Sralph ; 24812106Sralph } 24912106Sralph 25055474Sbostic static void 25155474Sbostic mcleanup(signo) 25255474Sbostic int signo; 25313816Ssam { 25414149Sralph if (lflag) 25516761Sralph syslog(LOG_INFO, "exiting"); 25637968Sbostic unlink(_PATH_SOCKETNAME); 25713816Ssam exit(0); 25813816Ssam } 25913816Ssam 26012106Sralph /* 26112106Sralph * Stuff for handling job specifications 26212106Sralph */ 26312106Sralph char *user[MAXUSERS]; /* users to process */ 26412106Sralph int users; /* # of users in user array */ 26512106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 26612106Sralph int requests; /* # of spool requests */ 26712875Sralph char *person; /* name of person doing lprm */ 26812106Sralph 26954546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ 27054546Sleres char cbuf[BUFSIZ]; /* command line buffer */ 27116761Sralph char *cmdnames[] = { 27212430Sralph "null", 27312430Sralph "printjob", 27412430Sralph "recvjob", 27512430Sralph "displayq short", 27612430Sralph "displayq long", 27712430Sralph "rmjob" 27812430Sralph }; 27912106Sralph 28055474Sbostic static void 28113816Ssam doit() 28212106Sralph { 28312106Sralph register char *cp; 28412106Sralph register int n; 28512106Sralph 28612106Sralph for (;;) { 28712106Sralph cp = cbuf; 28812106Sralph do { 28913816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 29013816Ssam fatal("Command line too long"); 29113816Ssam if ((n = read(1, cp, 1)) != 1) { 29212106Sralph if (n < 0) 29312106Sralph fatal("Lost connection"); 29412106Sralph return; 29512106Sralph } 29613816Ssam } while (*cp++ != '\n'); 29712106Sralph *--cp = '\0'; 29812106Sralph cp = cbuf; 29916761Sralph if (lflag) { 30016761Sralph if (*cp >= '\1' && *cp <= '\5') 30116761Sralph syslog(LOG_INFO, "%s requests %s %s", 30216761Sralph from, cmdnames[*cp], cp+1); 30316761Sralph else 30416761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 30516761Sralph *cp, from); 30612430Sralph } 30712106Sralph switch (*cp++) { 30812106Sralph case '\1': /* check the queue and print any jobs there */ 30912106Sralph printer = cp; 31012106Sralph printjob(); 31112106Sralph break; 31212106Sralph case '\2': /* receive files to be queued */ 31347055Smckusick if (!from_remote) { 31447055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 31547055Smckusick exit(1); 31647055Smckusick } 31712106Sralph printer = cp; 31812106Sralph recvjob(); 31912106Sralph break; 32012430Sralph case '\3': /* display the queue (short form) */ 32112430Sralph case '\4': /* display the queue (long form) */ 32212106Sralph printer = cp; 32312106Sralph while (*cp) { 32412106Sralph if (*cp != ' ') { 32512106Sralph cp++; 32612106Sralph continue; 32712106Sralph } 32812106Sralph *cp++ = '\0'; 32912106Sralph while (isspace(*cp)) 33012106Sralph cp++; 33112106Sralph if (*cp == '\0') 33212106Sralph break; 33312106Sralph if (isdigit(*cp)) { 33412106Sralph if (requests >= MAXREQUESTS) 33512106Sralph fatal("Too many requests"); 33612106Sralph requ[requests++] = atoi(cp); 33712106Sralph } else { 33812106Sralph if (users >= MAXUSERS) 33912106Sralph fatal("Too many users"); 34012106Sralph user[users++] = cp; 34112106Sralph } 34212106Sralph } 34312106Sralph displayq(cbuf[0] - '\3'); 34412106Sralph exit(0); 34512106Sralph case '\5': /* remove a job from the queue */ 34647055Smckusick if (!from_remote) { 34747055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 34847055Smckusick exit(1); 34947055Smckusick } 35012106Sralph printer = cp; 35112106Sralph while (*cp && *cp != ' ') 35212106Sralph cp++; 35312106Sralph if (!*cp) 35412106Sralph break; 35512106Sralph *cp++ = '\0'; 35612106Sralph person = cp; 35712106Sralph while (*cp) { 35812106Sralph if (*cp != ' ') { 35912106Sralph cp++; 36012106Sralph continue; 36112106Sralph } 36212106Sralph *cp++ = '\0'; 36312106Sralph while (isspace(*cp)) 36412106Sralph cp++; 36512106Sralph if (*cp == '\0') 36612106Sralph break; 36712106Sralph if (isdigit(*cp)) { 36812106Sralph if (requests >= MAXREQUESTS) 36912106Sralph fatal("Too many requests"); 37012106Sralph requ[requests++] = atoi(cp); 37112106Sralph } else { 37212106Sralph if (users >= MAXUSERS) 37312106Sralph fatal("Too many users"); 37412106Sralph user[users++] = cp; 37512106Sralph } 37612106Sralph } 37712106Sralph rmjob(); 37812106Sralph break; 37912106Sralph } 38012106Sralph fatal("Illegal service request"); 38112106Sralph } 38212106Sralph } 38312106Sralph 38412106Sralph /* 38512430Sralph * Make a pass through the printcap database and start printing any 38612430Sralph * files left from the last time the machine went down. 38712430Sralph */ 38855474Sbostic static void 38912430Sralph startup() 39012430Sralph { 39156123Selan char *buf; 39212430Sralph register char *cp; 39312430Sralph int pid; 39412430Sralph 39512430Sralph /* 39612430Sralph * Restart the daemons. 39712430Sralph */ 39856123Selan while (cgetnext(&buf, printcapdb) > 0) { 39956123Selan printer = buf; 40012430Sralph for (cp = buf; *cp; cp++) 40112430Sralph if (*cp == '|' || *cp == ':') { 40212430Sralph *cp = '\0'; 40312430Sralph break; 40412430Sralph } 40512430Sralph if ((pid = fork()) < 0) { 40616761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 40755474Sbostic mcleanup(0); 40812430Sralph } 40912430Sralph if (!pid) { 41056123Selan cgetclose(); 41112430Sralph printjob(); 41212430Sralph } 41312430Sralph } 41412430Sralph } 41512430Sralph 41627746Sbloom #define DUMMY ":nobody::" 41727746Sbloom 41812430Sralph /* 41912430Sralph * Check to see if the from host has access to the line printer. 42012430Sralph */ 42155474Sbostic static void 42213816Ssam chkhost(f) 42313816Ssam struct sockaddr_in *f; 42412430Sralph { 42513816Ssam register struct hostent *hp; 42612430Sralph register FILE *hostf; 42715551Sralph int first = 1; 42813816Ssam extern char *inet_ntoa(); 42912430Sralph 43013816Ssam f->sin_port = ntohs(f->sin_port); 43113816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 43213816Ssam fatal("Malformed from address"); 43354546Sleres 43454546Sleres /* Need real hostname for temporary filenames */ 43546911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 43646911Sbostic sizeof(struct in_addr), f->sin_family); 43754546Sleres if (hp == NULL) 43813816Ssam fatal("Host name for your address (%s) unknown", 43913816Ssam inet_ntoa(f->sin_addr)); 44013816Ssam 44154546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb)); 44254546Sleres from[sizeof(fromb) - 1] = '\0'; 44313816Ssam from = fromb; 44412734Sralph 44537968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 44615551Sralph again: 44715551Sralph if (hostf) { 44854546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr, 44954546Sleres DUMMY, DUMMY) == 0) { 45027746Sbloom (void) fclose(hostf); 45127746Sbloom return; 45212430Sralph } 45315551Sralph (void) fclose(hostf); 45412430Sralph } 45515551Sralph if (first == 1) { 45615551Sralph first = 0; 45737968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 45815551Sralph goto again; 45915551Sralph } 46013816Ssam fatal("Your host does not have line printer access"); 46154546Sleres /*NOTREACHED*/ 46212430Sralph } 46355474Sbostic 46455474Sbostic 46555474Sbostic 46655474Sbostic 46755474Sbostic 46855474Sbostic 46955474Sbostic 47055474Sbostic 47155474Sbostic 47255474Sbostic 47355474Sbostic 47455474Sbostic 475