1*13957Ssam #ifndef lint 2*13957Ssam static char sccsid[] = "@(#)lpd.c 4.7 (Berkeley) 07/17/83"; 3*13957Ssam #endif 4*13957Ssam 512106Sralph /* 612106Sralph * lpd -- line printer daemon. 712106Sralph * 812106Sralph * Listen for a connection and perform the requested operation. 912106Sralph * Operations are: 1012106Sralph * \1printer\n 1112106Sralph * check the queue for jobs and print any found. 1212106Sralph * \2printer\n 1312106Sralph * receive a job from another machine and queue it. 1412106Sralph * \3printer [users ...] [jobs ...]\n 1512106Sralph * return the current state of the queue (short form). 1612106Sralph * \4printer [users ...] [jobs ...]\n 1712106Sralph * return the current state of the queue (long form). 1812106Sralph * \5printer person [users ...] [jobs ...]\n 1912106Sralph * remove jobs from the queue. 2012430Sralph * \6printer\n 2112430Sralph * enable queuing on the specified printer queue. 2212430Sralph * \7printer\n 2312430Sralph * disable queuing on the specified printer queue. 2412430Sralph * \8printer\n 2512430Sralph * return the queue status (queuing enabled or disabled). 2612106Sralph * 2712106Sralph * Strategy to maintain protected spooling area: 2812106Sralph * 1. Spooling area is writable only by daemon and spooling group 2912106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses 3012106Sralph * root to access any file it wants (verifying things before 3112106Sralph * with an access call) and group id to know how it should 3212106Sralph * set up ownership of files in the spooling area. 3312430Sralph * 3. Files in spooling area are owned by root, group spooling 3412106Sralph * group, with mode 660. 3512106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 3612106Sralph * access files and printer. Users can't get to anything 3712106Sralph * w/o help of lpq and lprm programs. 3812106Sralph */ 3912106Sralph 4012106Sralph #include "lp.h" 4112106Sralph 4212875Sralph static int lflag; /* log requests flag */ 4312875Sralph static char *logfile = DEFLOGF; 4412875Sralph 4512106Sralph int reapchild(); 4613816Ssam int cleanup(); 4712106Sralph 4812106Sralph main(argc, argv) 4912106Sralph int argc; 5012106Sralph char **argv; 5112106Sralph { 5213816Ssam int f, funix, finet, options, defreadfds, fromlen; 5313816Ssam struct sockaddr_un sun, fromunix; 5413816Ssam struct sockaddr_in sin, frominet; 5513816Ssam int omask; 5612106Sralph 5712106Sralph gethostname(host, sizeof(host)); 5812106Sralph name = argv[0]; 5912106Sralph 6012106Sralph while (--argc > 0) { 6112106Sralph argv++; 6212106Sralph if (argv[0][0] == '-') 6312106Sralph switch (argv[0][1]) { 6412106Sralph case 'd': 6512106Sralph options |= SO_DEBUG; 6612106Sralph break; 6712106Sralph case 'l': 6812430Sralph lflag++; 6912430Sralph break; 7012430Sralph case 'L': 7112106Sralph argc--; 7212106Sralph logfile = *++argv; 7312106Sralph break; 7412106Sralph } 7512106Sralph } 7612106Sralph #ifndef DEBUG 7712106Sralph /* 7812106Sralph * Set up standard environment by detaching from the parent. 7912106Sralph */ 8012106Sralph if (fork()) 8112106Sralph exit(0); 8212106Sralph for (f = 0; f < 3; f++) 8312106Sralph (void) close(f); 8413147Ssam (void) open("/dev/null", O_RDONLY); 8513147Ssam (void) open("/dev/null", O_WRONLY); 8613147Ssam (void) open(logfile, O_WRONLY|O_APPEND); 8713147Ssam f = open("/dev/tty", O_RDWR); 8812106Sralph if (f > 0) { 8912106Sralph ioctl(f, TIOCNOTTY, 0); 9012106Sralph (void) close(f); 9112106Sralph } 9212106Sralph #endif 9313816Ssam #define mask(s) (1 << ((s) - 1)) 9413816Ssam #define ALLINTS mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM) 9513816Ssam omask = sigblock(ALLINTS); 9613816Ssam signal(SIGHUP, cleanup); 9713816Ssam signal(SIGINT, cleanup); 9813816Ssam signal(SIGQUIT, cleanup); 9913816Ssam signal(SIGTERM, cleanup); 10013816Ssam signal(SIGCHLD, reapchild); 10112106Sralph (void) umask(0); 10212875Sralph /* 10312875Sralph * Restart all the printers. 10412875Sralph */ 10512875Sralph startup(); 10613816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 10713816Ssam if (funix < 0) { 10813816Ssam logerr("socket"); 10912106Sralph exit(1); 11012106Sralph } 11113816Ssam sun.sun_family = AF_UNIX; 11213816Ssam strcpy(sun.sun_path, SOCKETNAME); 11313816Ssam if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) { 11413816Ssam logerr("unix domain bind"); 11512106Sralph exit(1); 11612106Sralph } 11713816Ssam sigsetmask(omask); 11813816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 11913816Ssam if (finet >= 0) { 12013816Ssam struct servent *sp; 12113816Ssam 12213816Ssam if (options & SO_DEBUG) 12313816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 12413816Ssam logerr("setsockopt (SO_DEBUG)"); 12513816Ssam cleanup(); 12613816Ssam } 12713816Ssam sp = getservbyname("printer", "tcp"); 12813816Ssam if (sp == NULL) { 12913816Ssam log("printer/tcp: unknown service"); 13013816Ssam cleanup(); 13113816Ssam } 13213816Ssam sin.sin_family = AF_INET; 13313816Ssam sin.sin_port = sp->s_port; 13413816Ssam if (bind(finet, &sin, sizeof(sin), 0) < 0) { 13513816Ssam logerr("internet domain bind"); 13613816Ssam cleanup(); 13713816Ssam } 13813816Ssam } 13912106Sralph /* 14012106Sralph * Main loop: listen, accept, do a request, continue. 14112106Sralph */ 14213816Ssam defreadfds = 1 << funix; 14313816Ssam listen(funix, 5); 14413816Ssam if (finet >= 0) { 14513816Ssam defreadfds |= 1 << finet; 14613816Ssam listen(finet, 5); 14713816Ssam } 14812106Sralph for (;;) { 149*13957Ssam int domain, nfds, s, readfds = defreadfds; 15012106Sralph 151*13957Ssam nfds = select(20, &readfds, 0, 0, 0); 152*13957Ssam if (nfds <= 0) { 153*13957Ssam if (nfds < 0 && errno != EINTR) { 154*13957Ssam logerr("select"); 155*13957Ssam cleanup(); 156*13957Ssam /*NOTREACHED*/ 157*13957Ssam } 158*13957Ssam continue; 159*13957Ssam } 16013816Ssam if (readfds & (1 << funix)) { 161*13957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 16213816Ssam s = accept(funix, &fromunix, &fromlen); 163*13957Ssam } else if (readfds & (1 << finet)) { 164*13957Ssam domain = AF_INET, fromlen = sizeof(frominet); 16513816Ssam s = accept(finet, &frominet, &fromlen); 16613816Ssam } 16712106Sralph if (s < 0) { 16812106Sralph if (errno == EINTR) 16912106Sralph continue; 17013816Ssam logerr("accept"); 17113816Ssam cleanup(); 17212106Sralph } 17312106Sralph if (fork() == 0) { 17413147Ssam signal(SIGCHLD, SIG_IGN); 17513816Ssam (void) close(funix); 17613816Ssam (void) close(finet); 17713816Ssam dup2(s, 1); 17813816Ssam (void) close(s); 17913816Ssam if (domain == AF_INET) 18013816Ssam chkhost(&frominet); 18113816Ssam doit(); 18212106Sralph exit(0); 18312106Sralph } 18412106Sralph (void) close(s); 18512106Sralph } 18612106Sralph } 18712106Sralph 18812875Sralph static 18912106Sralph reapchild() 19012106Sralph { 19112106Sralph union wait status; 19212106Sralph 19312106Sralph while (wait3(&status, WNOHANG, 0) > 0) 19412106Sralph ; 19512106Sralph } 19612106Sralph 19713816Ssam static 19813816Ssam cleanup() 19913816Ssam { 20013816Ssam unlink(SOCKETNAME); 20113816Ssam exit(0); 20213816Ssam } 20313816Ssam 20412106Sralph /* 20512106Sralph * Stuff for handling job specifications 20612106Sralph */ 20712106Sralph char *user[MAXUSERS]; /* users to process */ 20812106Sralph int users; /* # of users in user array */ 20912106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 21012106Sralph int requests; /* # of spool requests */ 21112875Sralph char *person; /* name of person doing lprm */ 21212106Sralph 21312875Sralph static char fromb[32]; /* buffer for client's machine name */ 21412875Sralph static char cbuf[BUFSIZ]; /* command line buffer */ 21512875Sralph static char *cmdnames[] = { 21612430Sralph "null", 21712430Sralph "printjob", 21812430Sralph "recvjob", 21912430Sralph "displayq short", 22012430Sralph "displayq long", 22112430Sralph "rmjob" 22212430Sralph }; 22312106Sralph 22412875Sralph static 22513816Ssam doit() 22612106Sralph { 22712106Sralph register char *cp; 22812106Sralph register int n; 22912106Sralph 23012106Sralph for (;;) { 23112106Sralph cp = cbuf; 23212106Sralph do { 23313816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 23413816Ssam fatal("Command line too long"); 23513816Ssam if ((n = read(1, cp, 1)) != 1) { 23612106Sralph if (n < 0) 23712106Sralph fatal("Lost connection"); 23812106Sralph return; 23912106Sralph } 24013816Ssam } while (*cp++ != '\n'); 24112106Sralph *--cp = '\0'; 24212106Sralph cp = cbuf; 24312430Sralph if (lflag && *cp >= '\1' && *cp <= '\5') { 24412430Sralph printer = NULL; 24512430Sralph log("%s requests %s %s", from, cmdnames[*cp], cp+1); 24612430Sralph } 24712106Sralph switch (*cp++) { 24812106Sralph case '\1': /* check the queue and print any jobs there */ 24912106Sralph printer = cp; 25012106Sralph printjob(); 25112106Sralph break; 25212106Sralph case '\2': /* receive files to be queued */ 25312106Sralph printer = cp; 25412106Sralph recvjob(); 25512106Sralph break; 25612430Sralph case '\3': /* display the queue (short form) */ 25712430Sralph case '\4': /* display the queue (long form) */ 25812106Sralph printer = cp; 25912106Sralph while (*cp) { 26012106Sralph if (*cp != ' ') { 26112106Sralph cp++; 26212106Sralph continue; 26312106Sralph } 26412106Sralph *cp++ = '\0'; 26512106Sralph while (isspace(*cp)) 26612106Sralph cp++; 26712106Sralph if (*cp == '\0') 26812106Sralph break; 26912106Sralph if (isdigit(*cp)) { 27012106Sralph if (requests >= MAXREQUESTS) 27112106Sralph fatal("Too many requests"); 27212106Sralph requ[requests++] = atoi(cp); 27312106Sralph } else { 27412106Sralph if (users >= MAXUSERS) 27512106Sralph fatal("Too many users"); 27612106Sralph user[users++] = cp; 27712106Sralph } 27812106Sralph } 27912106Sralph displayq(cbuf[0] - '\3'); 28012106Sralph exit(0); 28112106Sralph case '\5': /* remove a job from the queue */ 28212106Sralph printer = cp; 28312106Sralph while (*cp && *cp != ' ') 28412106Sralph cp++; 28512106Sralph if (!*cp) 28612106Sralph break; 28712106Sralph *cp++ = '\0'; 28812106Sralph person = cp; 28912106Sralph while (*cp) { 29012106Sralph if (*cp != ' ') { 29112106Sralph cp++; 29212106Sralph continue; 29312106Sralph } 29412106Sralph *cp++ = '\0'; 29512106Sralph while (isspace(*cp)) 29612106Sralph cp++; 29712106Sralph if (*cp == '\0') 29812106Sralph break; 29912106Sralph if (isdigit(*cp)) { 30012106Sralph if (requests >= MAXREQUESTS) 30112106Sralph fatal("Too many requests"); 30212106Sralph requ[requests++] = atoi(cp); 30312106Sralph } else { 30412106Sralph if (users >= MAXUSERS) 30512106Sralph fatal("Too many users"); 30612106Sralph user[users++] = cp; 30712106Sralph } 30812106Sralph } 30912106Sralph rmjob(); 31012106Sralph break; 31112106Sralph } 31212106Sralph fatal("Illegal service request"); 31312106Sralph } 31412106Sralph } 31512106Sralph 31612106Sralph /* 31712430Sralph * Make a pass through the printcap database and start printing any 31812430Sralph * files left from the last time the machine went down. 31912430Sralph */ 32012875Sralph static 32112430Sralph startup() 32212430Sralph { 32312430Sralph char buf[BUFSIZ]; 32412430Sralph register char *cp; 32512430Sralph int pid; 32612430Sralph 32712430Sralph printer = buf; 32812430Sralph 32912430Sralph /* 33012430Sralph * Restart the daemons. 33112430Sralph */ 33212430Sralph while (getprent(buf) > 0) { 33312430Sralph for (cp = buf; *cp; cp++) 33412430Sralph if (*cp == '|' || *cp == ':') { 33512430Sralph *cp = '\0'; 33612430Sralph break; 33712430Sralph } 33812430Sralph if ((pid = fork()) < 0) { 33912430Sralph log("startup: cannot fork"); 34013816Ssam cleanup(); 34112430Sralph } 34212430Sralph if (!pid) { 34312430Sralph endprent(); 34412430Sralph printjob(); 34512430Sralph } 34612430Sralph } 34712430Sralph } 34812430Sralph 34912430Sralph /* 35012430Sralph * Check to see if the from host has access to the line printer. 35112430Sralph */ 35212875Sralph static 35313816Ssam chkhost(f) 35413816Ssam struct sockaddr_in *f; 35512430Sralph { 35613816Ssam register struct hostent *hp; 35712430Sralph register FILE *hostf; 35812430Sralph register char *cp; 35912430Sralph char ahost[50]; 36013816Ssam extern char *inet_ntoa(); 36112430Sralph 36213816Ssam f->sin_port = ntohs(f->sin_port); 36313816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 36413816Ssam fatal("Malformed from address"); 36513816Ssam hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family); 36613816Ssam if (hp == 0) 36713816Ssam fatal("Host name for your address (%s) unknown", 36813816Ssam inet_ntoa(f->sin_addr)); 36913816Ssam 37013816Ssam strcpy(fromb, hp->h_name); 37113816Ssam from = fromb; 37212734Sralph if (!strcmp(from, host)) 37313816Ssam return; 37412734Sralph 37512430Sralph hostf = fopen("/etc/hosts.equiv", "r"); 37612430Sralph while (fgets(ahost, sizeof(ahost), hostf)) { 37712430Sralph if (cp = index(ahost, '\n')) 37812430Sralph *cp = '\0'; 37912430Sralph cp = index(ahost, ' '); 38012430Sralph if (!strcmp(from, ahost) && cp == NULL) { 38112430Sralph (void) fclose(hostf); 38213816Ssam return; 38312430Sralph } 38412430Sralph } 38513816Ssam fatal("Your host does not have line printer access"); 38612430Sralph } 38712430Sralph 38812106Sralph /*VARARGS1*/ 38912106Sralph log(msg, a1, a2, a3) 39012106Sralph char *msg; 39112106Sralph { 39212106Sralph short console = isatty(fileno(stderr)); 39312106Sralph 39412106Sralph fprintf(stderr, console ? "\r\n%s: " : "%s: ", name); 39512106Sralph if (printer) 39612106Sralph fprintf(stderr, "%s: ", printer); 39712106Sralph fprintf(stderr, msg, a1, a2, a3); 39812106Sralph if (console) 39912106Sralph putc('\r', stderr); 40012106Sralph putc('\n', stderr); 40112106Sralph fflush(stderr); 40212106Sralph } 40312106Sralph 40412875Sralph static 40513816Ssam logerr(msg) 40612106Sralph char *msg; 40712106Sralph { 40812106Sralph register int err = errno; 40912106Sralph short console = isatty(fileno(stderr)); 41012106Sralph extern int sys_nerr; 41112106Sralph extern char *sys_errlist[]; 41212106Sralph 41312106Sralph fprintf(stderr, console ? "\r\n%s: " : "%s: ", name); 41412430Sralph if (msg) 41512106Sralph fprintf(stderr, "%s: ", msg); 41612106Sralph fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr); 41712106Sralph if (console) 41812106Sralph putc('\r', stderr); 41912106Sralph putc('\n', stderr); 42012106Sralph fflush(stderr); 42112106Sralph } 422