122429Sdist /* 222429Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 542802Sbostic * %sccs.include.redist.c% 622429Sdist */ 722429Sdist 813957Ssam #ifndef lint 922429Sdist char copyright[] = 1022429Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 1122429Sdist All rights reserved.\n"; 1234203Sbostic #endif /* not lint */ 1313957Ssam 1422429Sdist #ifndef lint 15*50886Storek static char sccsid[] = "@(#)lpd.c 5.13 (Berkeley) 08/21/91"; 1634203Sbostic #endif /* not lint */ 1722429Sdist 1812106Sralph /* 1912106Sralph * lpd -- line printer daemon. 2012106Sralph * 2112106Sralph * Listen for a connection and perform the requested operation. 2212106Sralph * Operations are: 2312106Sralph * \1printer\n 2412106Sralph * check the queue for jobs and print any found. 2512106Sralph * \2printer\n 2612106Sralph * receive a job from another machine and queue it. 2712106Sralph * \3printer [users ...] [jobs ...]\n 2812106Sralph * return the current state of the queue (short form). 2912106Sralph * \4printer [users ...] [jobs ...]\n 3012106Sralph * return the current state of the queue (long form). 3112106Sralph * \5printer person [users ...] [jobs ...]\n 3212106Sralph * remove jobs from the queue. 3312106Sralph * 3412106Sralph * Strategy to maintain protected spooling area: 3512106Sralph * 1. Spooling area is writable only by daemon and spooling group 3612106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses 3712106Sralph * root to access any file it wants (verifying things before 3812106Sralph * with an access call) and group id to know how it should 3912106Sralph * set up ownership of files in the spooling area. 4012430Sralph * 3. Files in spooling area are owned by root, group spooling 4112106Sralph * group, with mode 660. 4212106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 4312106Sralph * access files and printer. Users can't get to anything 4412106Sralph * w/o help of lpq and lprm programs. 4512106Sralph */ 4612106Sralph 4712106Sralph #include "lp.h" 4837968Sbostic #include "pathnames.h" 4912106Sralph 5016761Sralph int lflag; /* log requests flag */ 5147055Smckusick int from_remote; /* from remote socket */ 5212875Sralph 5346911Sbostic void mcleanup(), reapchild(); 5412106Sralph 5512106Sralph main(argc, argv) 5612106Sralph int argc; 5712106Sralph char **argv; 5812106Sralph { 5945344Skarels int f, funix, finet, options = 0, defreadfds, fromlen; 60*50886Storek struct sockaddr_un un, fromunix; 6113816Ssam struct sockaddr_in sin, frominet; 6214837Sralph int omask, lfd; 6312106Sralph 6412106Sralph gethostname(host, sizeof(host)); 6512106Sralph name = argv[0]; 6612106Sralph 6712106Sralph while (--argc > 0) { 6812106Sralph argv++; 6912106Sralph if (argv[0][0] == '-') 7012106Sralph switch (argv[0][1]) { 7112106Sralph case 'd': 7212106Sralph options |= SO_DEBUG; 7312106Sralph break; 7412106Sralph case 'l': 7512430Sralph lflag++; 7612430Sralph break; 7712106Sralph } 7812106Sralph } 7914837Sralph 8012106Sralph #ifndef DEBUG 8112106Sralph /* 8212106Sralph * Set up standard environment by detaching from the parent. 8312106Sralph */ 8444708Skarels daemon(0, 0); 8512106Sralph #endif 8614837Sralph 8725494Seric openlog("lpd", LOG_PID, LOG_LPR); 8812106Sralph (void) umask(0); 8937968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644); 9014837Sralph if (lfd < 0) { 9137968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 9214837Sralph exit(1); 9314837Sralph } 9414837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 9514837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 9614837Sralph exit(0); 9737968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 9814837Sralph exit(1); 9914837Sralph } 10014837Sralph ftruncate(lfd, 0); 10112875Sralph /* 10214837Sralph * write process id for others to know 10314837Sralph */ 10414837Sralph sprintf(line, "%u\n", getpid()); 10514837Sralph f = strlen(line); 10614837Sralph if (write(lfd, line, f) != f) { 10737968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 10814837Sralph exit(1); 10914837Sralph } 11014837Sralph signal(SIGCHLD, reapchild); 11114837Sralph /* 11212875Sralph * Restart all the printers. 11312875Sralph */ 11412875Sralph startup(); 11537968Sbostic (void) unlink(_PATH_SOCKETNAME); 11613816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 11713816Ssam if (funix < 0) { 11816761Sralph syslog(LOG_ERR, "socket: %m"); 11912106Sralph exit(1); 12012106Sralph } 12114149Sralph #define mask(s) (1 << ((s) - 1)) 12214149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 12316761Sralph signal(SIGHUP, mcleanup); 12416761Sralph signal(SIGINT, mcleanup); 12516761Sralph signal(SIGQUIT, mcleanup); 12616761Sralph signal(SIGTERM, mcleanup); 127*50886Storek un.sun_family = AF_UNIX; 128*50886Storek strcpy(un.sun_path, _PATH_SOCKETNAME); 12946911Sbostic if (bind(funix, 130*50886Storek (struct sockaddr *)&un, strlen(un.sun_path) + 2) < 0) { 13116761Sralph syslog(LOG_ERR, "ubind: %m"); 13212106Sralph exit(1); 13312106Sralph } 13413816Ssam sigsetmask(omask); 13514149Sralph defreadfds = 1 << funix; 13614149Sralph listen(funix, 5); 13713816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 13813816Ssam if (finet >= 0) { 13913816Ssam struct servent *sp; 14013816Ssam 14113816Ssam if (options & SO_DEBUG) 14213816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 14316761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 14416761Sralph mcleanup(); 14513816Ssam } 14613816Ssam sp = getservbyname("printer", "tcp"); 14713816Ssam if (sp == NULL) { 14816761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 14916761Sralph mcleanup(); 15013816Ssam } 15113816Ssam sin.sin_family = AF_INET; 15213816Ssam sin.sin_port = sp->s_port; 15346911Sbostic if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) { 15416761Sralph syslog(LOG_ERR, "bind: %m"); 15516761Sralph mcleanup(); 15613816Ssam } 15714149Sralph defreadfds |= 1 << finet; 15814149Sralph listen(finet, 5); 15913816Ssam } 16012106Sralph /* 16114149Sralph * Main loop: accept, do a request, continue. 16212106Sralph */ 16312106Sralph for (;;) { 16413957Ssam int domain, nfds, s, readfds = defreadfds; 16512106Sralph 16613957Ssam nfds = select(20, &readfds, 0, 0, 0); 16713957Ssam if (nfds <= 0) { 16816761Sralph if (nfds < 0 && errno != EINTR) 16916761Sralph syslog(LOG_WARNING, "select: %m"); 17013957Ssam continue; 17113957Ssam } 17213816Ssam if (readfds & (1 << funix)) { 17313957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 17446911Sbostic s = accept(funix, 17546911Sbostic (struct sockaddr *)&fromunix, &fromlen); 17613957Ssam } else if (readfds & (1 << finet)) { 17713957Ssam domain = AF_INET, fromlen = sizeof(frominet); 17846911Sbostic s = accept(finet, 17946911Sbostic (struct sockaddr *)&frominet, &fromlen); 18013816Ssam } 18112106Sralph if (s < 0) { 18216761Sralph if (errno != EINTR) 18316761Sralph syslog(LOG_WARNING, "accept: %m"); 18416761Sralph continue; 18512106Sralph } 18612106Sralph if (fork() == 0) { 18713147Ssam signal(SIGCHLD, SIG_IGN); 18814708Sralph signal(SIGHUP, SIG_IGN); 18914708Sralph signal(SIGINT, SIG_IGN); 19014708Sralph signal(SIGQUIT, SIG_IGN); 19114708Sralph signal(SIGTERM, SIG_IGN); 19213816Ssam (void) close(funix); 19313816Ssam (void) close(finet); 19413816Ssam dup2(s, 1); 19513816Ssam (void) close(s); 19647055Smckusick if (domain == AF_INET) { 19747055Smckusick from_remote = 1; 19813816Ssam chkhost(&frominet); 19947055Smckusick } else 20047055Smckusick from_remote = 0; 20113816Ssam doit(); 20212106Sralph exit(0); 20312106Sralph } 20412106Sralph (void) close(s); 20512106Sralph } 20612106Sralph } 20712106Sralph 20846911Sbostic void 20912106Sralph reapchild() 21012106Sralph { 21112106Sralph union wait status; 21212106Sralph 21346911Sbostic while (wait3((int *)&status, WNOHANG, 0) > 0) 21412106Sralph ; 21512106Sralph } 21612106Sralph 21746911Sbostic void 21816761Sralph mcleanup() 21913816Ssam { 22014149Sralph if (lflag) 22116761Sralph syslog(LOG_INFO, "exiting"); 22237968Sbostic unlink(_PATH_SOCKETNAME); 22313816Ssam exit(0); 22413816Ssam } 22513816Ssam 22612106Sralph /* 22712106Sralph * Stuff for handling job specifications 22812106Sralph */ 22912106Sralph char *user[MAXUSERS]; /* users to process */ 23012106Sralph int users; /* # of users in user array */ 23112106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 23212106Sralph int requests; /* # of spool requests */ 23312875Sralph char *person; /* name of person doing lprm */ 23412106Sralph 23516761Sralph char fromb[32]; /* buffer for client's machine name */ 23616761Sralph char cbuf[BUFSIZ]; /* command line buffer */ 23716761Sralph char *cmdnames[] = { 23812430Sralph "null", 23912430Sralph "printjob", 24012430Sralph "recvjob", 24112430Sralph "displayq short", 24212430Sralph "displayq long", 24312430Sralph "rmjob" 24412430Sralph }; 24512106Sralph 24613816Ssam doit() 24712106Sralph { 24812106Sralph register char *cp; 24912106Sralph register int n; 25012106Sralph 25112106Sralph for (;;) { 25212106Sralph cp = cbuf; 25312106Sralph do { 25413816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 25513816Ssam fatal("Command line too long"); 25613816Ssam if ((n = read(1, cp, 1)) != 1) { 25712106Sralph if (n < 0) 25812106Sralph fatal("Lost connection"); 25912106Sralph return; 26012106Sralph } 26113816Ssam } while (*cp++ != '\n'); 26212106Sralph *--cp = '\0'; 26312106Sralph cp = cbuf; 26416761Sralph if (lflag) { 26516761Sralph if (*cp >= '\1' && *cp <= '\5') 26616761Sralph syslog(LOG_INFO, "%s requests %s %s", 26716761Sralph from, cmdnames[*cp], cp+1); 26816761Sralph else 26916761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 27016761Sralph *cp, from); 27112430Sralph } 27212106Sralph switch (*cp++) { 27312106Sralph case '\1': /* check the queue and print any jobs there */ 27412106Sralph printer = cp; 27512106Sralph printjob(); 27612106Sralph break; 27712106Sralph case '\2': /* receive files to be queued */ 27847055Smckusick if (!from_remote) { 27947055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 28047055Smckusick exit(1); 28147055Smckusick } 28212106Sralph printer = cp; 28312106Sralph recvjob(); 28412106Sralph break; 28512430Sralph case '\3': /* display the queue (short form) */ 28612430Sralph case '\4': /* display the queue (long form) */ 28712106Sralph printer = cp; 28812106Sralph while (*cp) { 28912106Sralph if (*cp != ' ') { 29012106Sralph cp++; 29112106Sralph continue; 29212106Sralph } 29312106Sralph *cp++ = '\0'; 29412106Sralph while (isspace(*cp)) 29512106Sralph cp++; 29612106Sralph if (*cp == '\0') 29712106Sralph break; 29812106Sralph if (isdigit(*cp)) { 29912106Sralph if (requests >= MAXREQUESTS) 30012106Sralph fatal("Too many requests"); 30112106Sralph requ[requests++] = atoi(cp); 30212106Sralph } else { 30312106Sralph if (users >= MAXUSERS) 30412106Sralph fatal("Too many users"); 30512106Sralph user[users++] = cp; 30612106Sralph } 30712106Sralph } 30812106Sralph displayq(cbuf[0] - '\3'); 30912106Sralph exit(0); 31012106Sralph case '\5': /* remove a job from the queue */ 31147055Smckusick if (!from_remote) { 31247055Smckusick syslog(LOG_INFO, "illegal request (%d)", *cp); 31347055Smckusick exit(1); 31447055Smckusick } 31512106Sralph printer = cp; 31612106Sralph while (*cp && *cp != ' ') 31712106Sralph cp++; 31812106Sralph if (!*cp) 31912106Sralph break; 32012106Sralph *cp++ = '\0'; 32112106Sralph person = cp; 32212106Sralph while (*cp) { 32312106Sralph if (*cp != ' ') { 32412106Sralph cp++; 32512106Sralph continue; 32612106Sralph } 32712106Sralph *cp++ = '\0'; 32812106Sralph while (isspace(*cp)) 32912106Sralph cp++; 33012106Sralph if (*cp == '\0') 33112106Sralph break; 33212106Sralph if (isdigit(*cp)) { 33312106Sralph if (requests >= MAXREQUESTS) 33412106Sralph fatal("Too many requests"); 33512106Sralph requ[requests++] = atoi(cp); 33612106Sralph } else { 33712106Sralph if (users >= MAXUSERS) 33812106Sralph fatal("Too many users"); 33912106Sralph user[users++] = cp; 34012106Sralph } 34112106Sralph } 34212106Sralph rmjob(); 34312106Sralph break; 34412106Sralph } 34512106Sralph fatal("Illegal service request"); 34612106Sralph } 34712106Sralph } 34812106Sralph 34912106Sralph /* 35012430Sralph * Make a pass through the printcap database and start printing any 35112430Sralph * files left from the last time the machine went down. 35212430Sralph */ 35312430Sralph startup() 35412430Sralph { 35512430Sralph char buf[BUFSIZ]; 35612430Sralph register char *cp; 35712430Sralph int pid; 35812430Sralph 35912430Sralph printer = buf; 36012430Sralph 36112430Sralph /* 36212430Sralph * Restart the daemons. 36312430Sralph */ 36412430Sralph while (getprent(buf) > 0) { 36512430Sralph for (cp = buf; *cp; cp++) 36612430Sralph if (*cp == '|' || *cp == ':') { 36712430Sralph *cp = '\0'; 36812430Sralph break; 36912430Sralph } 37012430Sralph if ((pid = fork()) < 0) { 37116761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 37216761Sralph mcleanup(); 37312430Sralph } 37412430Sralph if (!pid) { 37512430Sralph endprent(); 37612430Sralph printjob(); 37712430Sralph } 37812430Sralph } 37912430Sralph } 38012430Sralph 38127746Sbloom #define DUMMY ":nobody::" 38227746Sbloom 38312430Sralph /* 38412430Sralph * Check to see if the from host has access to the line printer. 38512430Sralph */ 38613816Ssam chkhost(f) 38713816Ssam struct sockaddr_in *f; 38812430Sralph { 38913816Ssam register struct hostent *hp; 39012430Sralph register FILE *hostf; 39127746Sbloom register char *cp, *sp; 39212430Sralph char ahost[50]; 39315551Sralph int first = 1; 39413816Ssam extern char *inet_ntoa(); 39527746Sbloom int baselen = -1; 39612430Sralph 39713816Ssam f->sin_port = ntohs(f->sin_port); 39813816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 39913816Ssam fatal("Malformed from address"); 40046911Sbostic hp = gethostbyaddr((char *)&f->sin_addr, 40146911Sbostic sizeof(struct in_addr), f->sin_family); 40213816Ssam if (hp == 0) 40313816Ssam fatal("Host name for your address (%s) unknown", 40413816Ssam inet_ntoa(f->sin_addr)); 40513816Ssam 40613816Ssam strcpy(fromb, hp->h_name); 40713816Ssam from = fromb; 40812734Sralph if (!strcmp(from, host)) 40913816Ssam return; 41012734Sralph 41127746Sbloom sp = fromb; 41227746Sbloom cp = ahost; 41327746Sbloom while (*sp) { 41427746Sbloom if (*sp == '.') { 41527746Sbloom if (baselen == -1) 41627746Sbloom baselen = sp - fromb; 41727746Sbloom *cp++ = *sp++; 41827746Sbloom } else { 41927746Sbloom *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++; 42027746Sbloom } 42127746Sbloom } 42227746Sbloom *cp = '\0'; 42337968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 42415551Sralph again: 42515551Sralph if (hostf) { 42627746Sbloom if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) { 42727746Sbloom (void) fclose(hostf); 42827746Sbloom return; 42912430Sralph } 43015551Sralph (void) fclose(hostf); 43112430Sralph } 43215551Sralph if (first == 1) { 43315551Sralph first = 0; 43437968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 43515551Sralph goto again; 43615551Sralph } 43713816Ssam fatal("Your host does not have line printer access"); 43812430Sralph } 439