122429Sdist /* 2*61843Sbostic * Copyright (c) 1983, 1993 3*61843Sbostic * 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[] = 11*61843Sbostic "@(#) Copyright (c) 1983, 1993\n\ 12*61843Sbostic The Regents of the University of California. All rights reserved.\n"; 1334203Sbostic #endif /* not lint */ 1413957Ssam 1522429Sdist #ifndef lint 16*61843Sbostic static char sccsid[] = "@(#)lpd.c 8.1 (Berkeley) 06/06/93"; 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); 15750886Storek un.sun_family = AF_UNIX; 15850886Storek strcpy(un.sun_path, _PATH_SOCKETNAME); 15958240Storek #ifndef SUN_LEN 16058240Storek #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2) 16158240Storek #endif 16258240Storek if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) { 16316761Sralph syslog(LOG_ERR, "ubind: %m"); 16412106Sralph exit(1); 16512106Sralph } 16613816Ssam sigsetmask(omask); 16756123Selan FD_ZERO(&defreadfds); 16856123Selan FD_SET(funix, &defreadfds); 16914149Sralph listen(funix, 5); 17013816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 17113816Ssam if (finet >= 0) { 17213816Ssam struct servent *sp; 17313816Ssam 17413816Ssam if (options & SO_DEBUG) 17513816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 17616761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 17755474Sbostic mcleanup(0); 17813816Ssam } 17913816Ssam sp = getservbyname("printer", "tcp"); 18013816Ssam if (sp == NULL) { 18116761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 18255474Sbostic mcleanup(0); 18313816Ssam } 18413816Ssam sin.sin_family = AF_INET; 18513816Ssam sin.sin_port = sp->s_port; 18646911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 18716761Sralph syslog(LOG_ERR, "bind: %m"); 18855474Sbostic mcleanup(0); 18913816Ssam } 19056123Selan FD_SET(finet, &defreadfds); 19114149Sralph listen(finet, 5); 19213816Ssam } 19312106Sralph /* 19414149Sralph * Main loop: accept, do a request, continue. 19512106Sralph */ 19612106Sralph for (;;) { 19756123Selan int domain, nfds, s; 19856123Selan fd_set readfds; 19912106Sralph 20056123Selan FD_COPY(&defreadfds, &readfds); 20113957Ssam nfds = select(20, &readfds, 0, 0, 0); 20213957Ssam if (nfds <= 0) { 20316761Sralph if (nfds < 0 && errno != EINTR) 20416761Sralph syslog(LOG_WARNING, "select: %m"); 20513957Ssam continue; 20613957Ssam } 20756123Selan if (FD_ISSET(funix, &readfds)) { 20813957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 20946911Sbostic s = accept(funix, 21046911Sbostic (struct sockaddr *)&fromunix, &fromlen); 21156123Selan } else /* if (FD_ISSET(finet, &readfds)) */ { 21213957Ssam domain = AF_INET, fromlen = sizeof(frominet); 21346911Sbostic s = accept(finet, 21446911Sbostic (struct sockaddr *)&frominet, &fromlen); 21513816Ssam } 21612106Sralph if (s < 0) { 21716761Sralph if (errno != EINTR) 21816761Sralph syslog(LOG_WARNING, "accept: %m"); 21916761Sralph continue; 22012106Sralph } 22112106Sralph if (fork() == 0) { 22213147Ssam signal(SIGCHLD, SIG_IGN); 22314708Sralph signal(SIGHUP, SIG_IGN); 22414708Sralph signal(SIGINT, SIG_IGN); 22514708Sralph signal(SIGQUIT, SIG_IGN); 22614708Sralph signal(SIGTERM, SIG_IGN); 22713816Ssam (void) close(funix); 22813816Ssam (void) close(finet); 22913816Ssam dup2(s, 1); 23013816Ssam (void) close(s); 23147055Smckusick if (domain == AF_INET) { 23247055Smckusick from_remote = 1; 23313816Ssam chkhost(&frominet); 23447055Smckusick } else 23547055Smckusick from_remote = 0; 23613816Ssam doit(); 23712106Sralph exit(0); 23812106Sralph } 23912106Sralph (void) close(s); 24012106Sralph } 24112106Sralph } 24212106Sralph 24355474Sbostic static void 24455474Sbostic reapchild(signo) 24555474Sbostic int signo; 24612106Sralph { 24712106Sralph union wait status; 24812106Sralph 24946911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 25012106Sralph ; 25112106Sralph } 25212106Sralph 25355474Sbostic static void 25455474Sbostic mcleanup(signo) 25555474Sbostic int signo; 25613816Ssam { 25714149Sralph if (lflag) 25816761Sralph syslog(LOG_INFO, "exiting"); 25937968Sbostic unlink(_PATH_SOCKETNAME); 26013816Ssam exit(0); 26113816Ssam } 26213816Ssam 26312106Sralph /* 26412106Sralph * Stuff for handling job specifications 26512106Sralph */ 26612106Sralph char *user[MAXUSERS]; /* users to process */ 26712106Sralph int users; /* # of users in user array */ 26812106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 26912106Sralph int requests; /* # of spool requests */ 27012875Sralph char *person; /* name of person doing lprm */ 27112106Sralph 27254546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ 27354546Sleres char cbuf[BUFSIZ]; /* command line buffer */ 27416761Sralph char *cmdnames[] = { 27512430Sralph "null", 27612430Sralph "printjob", 27712430Sralph "recvjob", 27812430Sralph "displayq short", 27912430Sralph "displayq long", 28012430Sralph "rmjob" 28112430Sralph }; 28212106Sralph 28355474Sbostic static void 28413816Ssam doit() 28512106Sralph { 28612106Sralph register char *cp; 28712106Sralph register int n; 28812106Sralph 28912106Sralph for (;;) { 29012106Sralph cp = cbuf; 29112106Sralph do { 29213816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 29313816Ssam fatal("Command line too long"); 29413816Ssam if ((n = read(1, cp, 1)) != 1) { 29512106Sralph if (n < 0) 29612106Sralph fatal("Lost connection"); 29712106Sralph return; 29812106Sralph } 29913816Ssam } while (*cp++ != '\n'); 30012106Sralph *--cp = '\0'; 30112106Sralph cp = cbuf; 30216761Sralph if (lflag) { 30316761Sralph if (*cp >= '\1' && *cp <= '\5') 30416761Sralph syslog(LOG_INFO, "%s requests %s %s", 30516761Sralph from, cmdnames[*cp], cp+1); 30616761Sralph else 30716761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 30816761Sralph *cp, from); 30912430Sralph } 31012106Sralph switch (*cp++) { 31112106Sralph case '\1': /* check the queue and print any jobs there */ 31212106Sralph printer = cp; 31312106Sralph printjob(); 31412106Sralph break; 31512106Sralph case '\2': /* receive files to be queued */ 31647055Smckusick if (!from_remote) { 31747055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 31847055Smckusick exit(1); 31947055Smckusick } 32012106Sralph printer = cp; 32112106Sralph recvjob(); 32212106Sralph break; 32312430Sralph case '\3': /* display the queue (short form) */ 32412430Sralph case '\4': /* display the queue (long form) */ 32512106Sralph printer = cp; 32612106Sralph while (*cp) { 32712106Sralph if (*cp != ' ') { 32812106Sralph cp++; 32912106Sralph continue; 33012106Sralph } 33112106Sralph *cp++ = '\0'; 33212106Sralph while (isspace(*cp)) 33312106Sralph cp++; 33412106Sralph if (*cp == '\0') 33512106Sralph break; 33612106Sralph if (isdigit(*cp)) { 33712106Sralph if (requests >= MAXREQUESTS) 33812106Sralph fatal("Too many requests"); 33912106Sralph requ[requests++] = atoi(cp); 34012106Sralph } else { 34112106Sralph if (users >= MAXUSERS) 34212106Sralph fatal("Too many users"); 34312106Sralph user[users++] = cp; 34412106Sralph } 34512106Sralph } 34612106Sralph displayq(cbuf[0] - '\3'); 34712106Sralph exit(0); 34812106Sralph case '\5': /* remove a job from the queue */ 34947055Smckusick if (!from_remote) { 35047055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 35147055Smckusick exit(1); 35247055Smckusick } 35312106Sralph printer = cp; 35412106Sralph while (*cp && *cp != ' ') 35512106Sralph cp++; 35612106Sralph if (!*cp) 35712106Sralph break; 35812106Sralph *cp++ = '\0'; 35912106Sralph person = cp; 36012106Sralph while (*cp) { 36112106Sralph if (*cp != ' ') { 36212106Sralph cp++; 36312106Sralph continue; 36412106Sralph } 36512106Sralph *cp++ = '\0'; 36612106Sralph while (isspace(*cp)) 36712106Sralph cp++; 36812106Sralph if (*cp == '\0') 36912106Sralph break; 37012106Sralph if (isdigit(*cp)) { 37112106Sralph if (requests >= MAXREQUESTS) 37212106Sralph fatal("Too many requests"); 37312106Sralph requ[requests++] = atoi(cp); 37412106Sralph } else { 37512106Sralph if (users >= MAXUSERS) 37612106Sralph fatal("Too many users"); 37712106Sralph user[users++] = cp; 37812106Sralph } 37912106Sralph } 38012106Sralph rmjob(); 38112106Sralph break; 38212106Sralph } 38312106Sralph fatal("Illegal service request"); 38412106Sralph } 38512106Sralph } 38612106Sralph 38712106Sralph /* 38812430Sralph * Make a pass through the printcap database and start printing any 38912430Sralph * files left from the last time the machine went down. 39012430Sralph */ 39155474Sbostic static void 39212430Sralph startup() 39312430Sralph { 39456123Selan char *buf; 39512430Sralph register char *cp; 39612430Sralph int pid; 39712430Sralph 39812430Sralph /* 39912430Sralph * Restart the daemons. 40012430Sralph */ 40156123Selan while (cgetnext(&buf, printcapdb) > 0) { 40256123Selan printer = buf; 40312430Sralph for (cp = buf; *cp; cp++) 40412430Sralph if (*cp == '|' || *cp == ':') { 40512430Sralph *cp = '\0'; 40612430Sralph break; 40712430Sralph } 40812430Sralph if ((pid = fork()) < 0) { 40916761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 41055474Sbostic mcleanup(0); 41112430Sralph } 41212430Sralph if (!pid) { 41356123Selan cgetclose(); 41412430Sralph printjob(); 41512430Sralph } 41612430Sralph } 41712430Sralph } 41812430Sralph 41927746Sbloom #define DUMMY ":nobody::" 42027746Sbloom 42112430Sralph /* 42212430Sralph * Check to see if the from host has access to the line printer. 42312430Sralph */ 42455474Sbostic static void 42513816Ssam chkhost(f) 42613816Ssam struct sockaddr_in *f; 42712430Sralph { 42813816Ssam register struct hostent *hp; 42912430Sralph register FILE *hostf; 43015551Sralph int first = 1; 43113816Ssam extern char *inet_ntoa(); 43212430Sralph 43313816Ssam f->sin_port = ntohs(f->sin_port); 43413816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 43513816Ssam fatal("Malformed from address"); 43654546Sleres 43754546Sleres /* Need real hostname for temporary filenames */ 43846911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 43946911Sbostic sizeof(struct in_addr), f->sin_family); 44054546Sleres if (hp == NULL) 44113816Ssam fatal("Host name for your address (%s) unknown", 44213816Ssam inet_ntoa(f->sin_addr)); 44313816Ssam 44454546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb)); 44554546Sleres from[sizeof(fromb) - 1] = '\0'; 44613816Ssam from = fromb; 44712734Sralph 44837968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 44915551Sralph again: 45015551Sralph if (hostf) { 45154546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr, 45254546Sleres DUMMY, DUMMY) == 0) { 45327746Sbloom (void) fclose(hostf); 45427746Sbloom return; 45512430Sralph } 45615551Sralph (void) fclose(hostf); 45712430Sralph } 45815551Sralph if (first == 1) { 45915551Sralph first = 0; 46037968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 46115551Sralph goto again; 46215551Sralph } 46313816Ssam fatal("Your host does not have line printer access"); 46454546Sleres /*NOTREACHED*/ 46512430Sralph } 46655474Sbostic 46755474Sbostic 46855474Sbostic 46955474Sbostic 47055474Sbostic 47155474Sbostic 47255474Sbostic 47355474Sbostic 47455474Sbostic 47555474Sbostic 47655474Sbostic 47755474Sbostic 478