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 1056265Selan 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*56923Sbostic static char sccsid[] = "@(#)lpd.c 5.19 (Berkeley) 11/30/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); 117*56923Sbostic 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); 15946911Sbostic if (bind(funix, 16050886Storek (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) { 16116761Sralph syslog(LOG_ERR, "ubind: %m"); 16212106Sralph exit(1); 16312106Sralph } 16413816Ssam sigsetmask(omask); 16556123Selan FD_ZERO(&defreadfds); 16656123Selan FD_SET(funix, &defreadfds); 16714149Sralph listen(funix, 5); 16813816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 16913816Ssam if (finet >= 0) { 17013816Ssam struct servent *sp; 17113816Ssam 17213816Ssam if (options & SO_DEBUG) 17313816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 17416761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 17555474Sbostic mcleanup(0); 17613816Ssam } 17713816Ssam sp = getservbyname("printer", "tcp"); 17813816Ssam if (sp == NULL) { 17916761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 18055474Sbostic mcleanup(0); 18113816Ssam } 18213816Ssam sin.sin_family = AF_INET; 18313816Ssam sin.sin_port = sp->s_port; 18446911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 18516761Sralph syslog(LOG_ERR, "bind: %m"); 18655474Sbostic mcleanup(0); 18713816Ssam } 18856123Selan FD_SET(finet, &defreadfds); 18914149Sralph listen(finet, 5); 19013816Ssam } 19112106Sralph /* 19214149Sralph * Main loop: accept, do a request, continue. 19312106Sralph */ 19412106Sralph for (;;) { 19556123Selan int domain, nfds, s; 19656123Selan fd_set readfds; 19712106Sralph 19856123Selan FD_COPY(&defreadfds, &readfds); 19913957Ssam nfds = select(20, &readfds, 0, 0, 0); 20013957Ssam if (nfds <= 0) { 20116761Sralph if (nfds < 0 && errno != EINTR) 20216761Sralph syslog(LOG_WARNING, "select: %m"); 20313957Ssam continue; 20413957Ssam } 20556123Selan if (FD_ISSET(funix, &readfds)) { 20613957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 20746911Sbostic s = accept(funix, 20846911Sbostic (struct sockaddr *)&fromunix, &fromlen); 20956123Selan } else /* if (FD_ISSET(finet, &readfds)) */ { 21013957Ssam domain = AF_INET, fromlen = sizeof(frominet); 21146911Sbostic s = accept(finet, 21246911Sbostic (struct sockaddr *)&frominet, &fromlen); 21313816Ssam } 21412106Sralph if (s < 0) { 21516761Sralph if (errno != EINTR) 21616761Sralph syslog(LOG_WARNING, "accept: %m"); 21716761Sralph continue; 21812106Sralph } 21912106Sralph if (fork() == 0) { 22013147Ssam signal(SIGCHLD, SIG_IGN); 22114708Sralph signal(SIGHUP, SIG_IGN); 22214708Sralph signal(SIGINT, SIG_IGN); 22314708Sralph signal(SIGQUIT, SIG_IGN); 22414708Sralph signal(SIGTERM, SIG_IGN); 22513816Ssam (void) close(funix); 22613816Ssam (void) close(finet); 22713816Ssam dup2(s, 1); 22813816Ssam (void) close(s); 22947055Smckusick if (domain == AF_INET) { 23047055Smckusick from_remote = 1; 23113816Ssam chkhost(&frominet); 23247055Smckusick } else 23347055Smckusick from_remote = 0; 23413816Ssam doit(); 23512106Sralph exit(0); 23612106Sralph } 23712106Sralph (void) close(s); 23812106Sralph } 23912106Sralph } 24012106Sralph 24155474Sbostic static void 24255474Sbostic reapchild(signo) 24355474Sbostic int signo; 24412106Sralph { 24512106Sralph union wait status; 24612106Sralph 24746911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 24812106Sralph ; 24912106Sralph } 25012106Sralph 25155474Sbostic static void 25255474Sbostic mcleanup(signo) 25355474Sbostic int signo; 25413816Ssam { 25514149Sralph if (lflag) 25616761Sralph syslog(LOG_INFO, "exiting"); 25737968Sbostic unlink(_PATH_SOCKETNAME); 25813816Ssam exit(0); 25913816Ssam } 26013816Ssam 26112106Sralph /* 26212106Sralph * Stuff for handling job specifications 26312106Sralph */ 26412106Sralph char *user[MAXUSERS]; /* users to process */ 26512106Sralph int users; /* # of users in user array */ 26612106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 26712106Sralph int requests; /* # of spool requests */ 26812875Sralph char *person; /* name of person doing lprm */ 26912106Sralph 27054546Sleres char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */ 27154546Sleres char cbuf[BUFSIZ]; /* command line buffer */ 27216761Sralph char *cmdnames[] = { 27312430Sralph "null", 27412430Sralph "printjob", 27512430Sralph "recvjob", 27612430Sralph "displayq short", 27712430Sralph "displayq long", 27812430Sralph "rmjob" 27912430Sralph }; 28012106Sralph 28155474Sbostic static void 28213816Ssam doit() 28312106Sralph { 28412106Sralph register char *cp; 28512106Sralph register int n; 28612106Sralph 28712106Sralph for (;;) { 28812106Sralph cp = cbuf; 28912106Sralph do { 29013816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 29113816Ssam fatal("Command line too long"); 29213816Ssam if ((n = read(1, cp, 1)) != 1) { 29312106Sralph if (n < 0) 29412106Sralph fatal("Lost connection"); 29512106Sralph return; 29612106Sralph } 29713816Ssam } while (*cp++ != '\n'); 29812106Sralph *--cp = '\0'; 29912106Sralph cp = cbuf; 30016761Sralph if (lflag) { 30116761Sralph if (*cp >= '\1' && *cp <= '\5') 30216761Sralph syslog(LOG_INFO, "%s requests %s %s", 30316761Sralph from, cmdnames[*cp], cp+1); 30416761Sralph else 30516761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 30616761Sralph *cp, from); 30712430Sralph } 30812106Sralph switch (*cp++) { 30912106Sralph case '\1': /* check the queue and print any jobs there */ 31012106Sralph printer = cp; 31112106Sralph printjob(); 31212106Sralph break; 31312106Sralph case '\2': /* receive files to be queued */ 31447055Smckusick if (!from_remote) { 31547055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 31647055Smckusick exit(1); 31747055Smckusick } 31812106Sralph printer = cp; 31912106Sralph recvjob(); 32012106Sralph break; 32112430Sralph case '\3': /* display the queue (short form) */ 32212430Sralph case '\4': /* display the queue (long form) */ 32312106Sralph printer = cp; 32412106Sralph while (*cp) { 32512106Sralph if (*cp != ' ') { 32612106Sralph cp++; 32712106Sralph continue; 32812106Sralph } 32912106Sralph *cp++ = '\0'; 33012106Sralph while (isspace(*cp)) 33112106Sralph cp++; 33212106Sralph if (*cp == '\0') 33312106Sralph break; 33412106Sralph if (isdigit(*cp)) { 33512106Sralph if (requests >= MAXREQUESTS) 33612106Sralph fatal("Too many requests"); 33712106Sralph requ[requests++] = atoi(cp); 33812106Sralph } else { 33912106Sralph if (users >= MAXUSERS) 34012106Sralph fatal("Too many users"); 34112106Sralph user[users++] = cp; 34212106Sralph } 34312106Sralph } 34412106Sralph displayq(cbuf[0] - '\3'); 34512106Sralph exit(0); 34612106Sralph case '\5': /* remove a job from the queue */ 34747055Smckusick if (!from_remote) { 34847055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 34947055Smckusick exit(1); 35047055Smckusick } 35112106Sralph printer = cp; 35212106Sralph while (*cp && *cp != ' ') 35312106Sralph cp++; 35412106Sralph if (!*cp) 35512106Sralph break; 35612106Sralph *cp++ = '\0'; 35712106Sralph person = cp; 35812106Sralph while (*cp) { 35912106Sralph if (*cp != ' ') { 36012106Sralph cp++; 36112106Sralph continue; 36212106Sralph } 36312106Sralph *cp++ = '\0'; 36412106Sralph while (isspace(*cp)) 36512106Sralph cp++; 36612106Sralph if (*cp == '\0') 36712106Sralph break; 36812106Sralph if (isdigit(*cp)) { 36912106Sralph if (requests >= MAXREQUESTS) 37012106Sralph fatal("Too many requests"); 37112106Sralph requ[requests++] = atoi(cp); 37212106Sralph } else { 37312106Sralph if (users >= MAXUSERS) 37412106Sralph fatal("Too many users"); 37512106Sralph user[users++] = cp; 37612106Sralph } 37712106Sralph } 37812106Sralph rmjob(); 37912106Sralph break; 38012106Sralph } 38112106Sralph fatal("Illegal service request"); 38212106Sralph } 38312106Sralph } 38412106Sralph 38512106Sralph /* 38612430Sralph * Make a pass through the printcap database and start printing any 38712430Sralph * files left from the last time the machine went down. 38812430Sralph */ 38955474Sbostic static void 39012430Sralph startup() 39112430Sralph { 39256123Selan char *buf; 39312430Sralph register char *cp; 39412430Sralph int pid; 39512430Sralph 39612430Sralph /* 39712430Sralph * Restart the daemons. 39812430Sralph */ 39956123Selan while (cgetnext(&buf, printcapdb) > 0) { 40056123Selan printer = buf; 40112430Sralph for (cp = buf; *cp; cp++) 40212430Sralph if (*cp == '|' || *cp == ':') { 40312430Sralph *cp = '\0'; 40412430Sralph break; 40512430Sralph } 40612430Sralph if ((pid = fork()) < 0) { 40716761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 40855474Sbostic mcleanup(0); 40912430Sralph } 41012430Sralph if (!pid) { 41156123Selan cgetclose(); 41212430Sralph printjob(); 41312430Sralph } 41412430Sralph } 41512430Sralph } 41612430Sralph 41727746Sbloom #define DUMMY ":nobody::" 41827746Sbloom 41912430Sralph /* 42012430Sralph * Check to see if the from host has access to the line printer. 42112430Sralph */ 42255474Sbostic static void 42313816Ssam chkhost(f) 42413816Ssam struct sockaddr_in *f; 42512430Sralph { 42613816Ssam register struct hostent *hp; 42712430Sralph register FILE *hostf; 42815551Sralph int first = 1; 42913816Ssam extern char *inet_ntoa(); 43012430Sralph 43113816Ssam f->sin_port = ntohs(f->sin_port); 43213816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 43313816Ssam fatal("Malformed from address"); 43454546Sleres 43554546Sleres /* Need real hostname for temporary filenames */ 43646911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 43746911Sbostic sizeof(struct in_addr), f->sin_family); 43854546Sleres if (hp == NULL) 43913816Ssam fatal("Host name for your address (%s) unknown", 44013816Ssam inet_ntoa(f->sin_addr)); 44113816Ssam 44254546Sleres (void) strncpy(fromb, hp->h_name, sizeof(fromb)); 44354546Sleres from[sizeof(fromb) - 1] = '\0'; 44413816Ssam from = fromb; 44512734Sralph 44637968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 44715551Sralph again: 44815551Sralph if (hostf) { 44954546Sleres if (__ivaliduser(hostf, f->sin_addr.s_addr, 45054546Sleres DUMMY, DUMMY) == 0) { 45127746Sbloom (void) fclose(hostf); 45227746Sbloom return; 45312430Sralph } 45415551Sralph (void) fclose(hostf); 45512430Sralph } 45615551Sralph if (first == 1) { 45715551Sralph first = 0; 45837968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 45915551Sralph goto again; 46015551Sralph } 46113816Ssam fatal("Your host does not have line printer access"); 46254546Sleres /*NOTREACHED*/ 46312430Sralph } 46455474Sbostic 46555474Sbostic 46655474Sbostic 46755474Sbostic 46855474Sbostic 46955474Sbostic 47055474Sbostic 47155474Sbostic 47255474Sbostic 47355474Sbostic 47455474Sbostic 47555474Sbostic 476