113957Ssam #ifndef lint 2*16761Sralph static char sccsid[] = "@(#)lpd.c 4.12 (Berkeley) 07/25/84"; 313957Ssam #endif 413957Ssam 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. 2012106Sralph * 2112106Sralph * Strategy to maintain protected spooling area: 2212106Sralph * 1. Spooling area is writable only by daemon and spooling group 2312106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses 2412106Sralph * root to access any file it wants (verifying things before 2512106Sralph * with an access call) and group id to know how it should 2612106Sralph * set up ownership of files in the spooling area. 2712430Sralph * 3. Files in spooling area are owned by root, group spooling 2812106Sralph * group, with mode 660. 2912106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 3012106Sralph * access files and printer. Users can't get to anything 3112106Sralph * w/o help of lpq and lprm programs. 3212106Sralph */ 3312106Sralph 3412106Sralph #include "lp.h" 3512106Sralph 36*16761Sralph int lflag; /* log requests flag */ 3712875Sralph 3812106Sralph int reapchild(); 39*16761Sralph int mcleanup(); 4012106Sralph 4112106Sralph main(argc, argv) 4212106Sralph int argc; 4312106Sralph char **argv; 4412106Sralph { 4513816Ssam int f, funix, finet, options, defreadfds, fromlen; 4613816Ssam struct sockaddr_un sun, fromunix; 4713816Ssam struct sockaddr_in sin, frominet; 4814837Sralph int omask, lfd; 4912106Sralph 5012106Sralph gethostname(host, sizeof(host)); 5112106Sralph name = argv[0]; 5212106Sralph 5312106Sralph while (--argc > 0) { 5412106Sralph argv++; 5512106Sralph if (argv[0][0] == '-') 5612106Sralph switch (argv[0][1]) { 5712106Sralph case 'd': 5812106Sralph options |= SO_DEBUG; 5912106Sralph break; 6012106Sralph case 'l': 6112430Sralph lflag++; 6212430Sralph break; 6312106Sralph } 6412106Sralph } 6514837Sralph 6612106Sralph #ifndef DEBUG 6712106Sralph /* 6812106Sralph * Set up standard environment by detaching from the parent. 6912106Sralph */ 7012106Sralph if (fork()) 7112106Sralph exit(0); 72*16761Sralph for (f = 0; f < 5; f++) 7312106Sralph (void) close(f); 7413147Ssam (void) open("/dev/null", O_RDONLY); 7513147Ssam (void) open("/dev/null", O_WRONLY); 76*16761Sralph (void) dup(1); 7713147Ssam f = open("/dev/tty", O_RDWR); 7812106Sralph if (f > 0) { 7912106Sralph ioctl(f, TIOCNOTTY, 0); 8012106Sralph (void) close(f); 8112106Sralph } 8212106Sralph #endif 8314837Sralph 84*16761Sralph openlog("lpd", LOG_PID, 0); 8512106Sralph (void) umask(0); 8614837Sralph lfd = open(MASTERLOCK, O_WRONLY|O_CREAT, 0644); 8714837Sralph if (lfd < 0) { 88*16761Sralph syslog(LOG_ERR, "%s: %m", MASTERLOCK); 8914837Sralph exit(1); 9014837Sralph } 9114837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 9214837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 9314837Sralph exit(0); 94*16761Sralph syslog(LOG_ERR, "%s: %m", MASTERLOCK); 9514837Sralph exit(1); 9614837Sralph } 9714837Sralph ftruncate(lfd, 0); 9812875Sralph /* 9914837Sralph * write process id for others to know 10014837Sralph */ 10114837Sralph sprintf(line, "%u\n", getpid()); 10214837Sralph f = strlen(line); 10314837Sralph if (write(lfd, line, f) != f) { 104*16761Sralph syslog(LOG_ERR, "%s: %m", MASTERLOCK); 10514837Sralph exit(1); 10614837Sralph } 10714837Sralph signal(SIGCHLD, reapchild); 10814837Sralph /* 10912875Sralph * Restart all the printers. 11012875Sralph */ 11112875Sralph startup(); 11214837Sralph (void) unlink(SOCKETNAME); 11313816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 11413816Ssam if (funix < 0) { 115*16761Sralph syslog(LOG_ERR, "socket: %m"); 11612106Sralph exit(1); 11712106Sralph } 11814149Sralph #define mask(s) (1 << ((s) - 1)) 11914149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 120*16761Sralph signal(SIGHUP, mcleanup); 121*16761Sralph signal(SIGINT, mcleanup); 122*16761Sralph signal(SIGQUIT, mcleanup); 123*16761Sralph signal(SIGTERM, mcleanup); 12413816Ssam sun.sun_family = AF_UNIX; 12513816Ssam strcpy(sun.sun_path, SOCKETNAME); 12613816Ssam if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) { 127*16761Sralph syslog(LOG_ERR, "ubind: %m"); 12812106Sralph exit(1); 12912106Sralph } 13013816Ssam sigsetmask(omask); 13114149Sralph defreadfds = 1 << funix; 13214149Sralph listen(funix, 5); 13313816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 13413816Ssam if (finet >= 0) { 13513816Ssam struct servent *sp; 13613816Ssam 13713816Ssam if (options & SO_DEBUG) 13813816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 139*16761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 140*16761Sralph mcleanup(); 14113816Ssam } 14213816Ssam sp = getservbyname("printer", "tcp"); 14313816Ssam if (sp == NULL) { 144*16761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 145*16761Sralph mcleanup(); 14613816Ssam } 14713816Ssam sin.sin_family = AF_INET; 14813816Ssam sin.sin_port = sp->s_port; 14913816Ssam if (bind(finet, &sin, sizeof(sin), 0) < 0) { 150*16761Sralph syslog(LOG_ERR, "bind: %m"); 151*16761Sralph mcleanup(); 15213816Ssam } 15314149Sralph defreadfds |= 1 << finet; 15414149Sralph listen(finet, 5); 15513816Ssam } 15612106Sralph /* 15714149Sralph * Main loop: accept, do a request, continue. 15812106Sralph */ 15912106Sralph for (;;) { 16013957Ssam int domain, nfds, s, readfds = defreadfds; 16112106Sralph 16213957Ssam nfds = select(20, &readfds, 0, 0, 0); 16313957Ssam if (nfds <= 0) { 164*16761Sralph if (nfds < 0 && errno != EINTR) 165*16761Sralph syslog(LOG_WARNING, "select: %m"); 16613957Ssam continue; 16713957Ssam } 16813816Ssam if (readfds & (1 << funix)) { 16913957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 17013816Ssam s = accept(funix, &fromunix, &fromlen); 17113957Ssam } else if (readfds & (1 << finet)) { 17213957Ssam domain = AF_INET, fromlen = sizeof(frominet); 17313816Ssam s = accept(finet, &frominet, &fromlen); 17413816Ssam } 17512106Sralph if (s < 0) { 176*16761Sralph if (errno != EINTR) 177*16761Sralph syslog(LOG_WARNING, "accept: %m"); 178*16761Sralph continue; 17912106Sralph } 18012106Sralph if (fork() == 0) { 18113147Ssam signal(SIGCHLD, SIG_IGN); 18214708Sralph signal(SIGHUP, SIG_IGN); 18314708Sralph signal(SIGINT, SIG_IGN); 18414708Sralph signal(SIGQUIT, SIG_IGN); 18514708Sralph signal(SIGTERM, SIG_IGN); 18613816Ssam (void) close(funix); 18713816Ssam (void) close(finet); 18813816Ssam dup2(s, 1); 18913816Ssam (void) close(s); 19013816Ssam if (domain == AF_INET) 19113816Ssam chkhost(&frominet); 19213816Ssam doit(); 19312106Sralph exit(0); 19412106Sralph } 19512106Sralph (void) close(s); 19612106Sralph } 19712106Sralph } 19812106Sralph 19912106Sralph reapchild() 20012106Sralph { 20112106Sralph union wait status; 20212106Sralph 20312106Sralph while (wait3(&status, WNOHANG, 0) > 0) 20412106Sralph ; 20512106Sralph } 20612106Sralph 207*16761Sralph mcleanup() 20813816Ssam { 20914149Sralph if (lflag) 210*16761Sralph syslog(LOG_INFO, "exiting"); 21113816Ssam unlink(SOCKETNAME); 21213816Ssam exit(0); 21313816Ssam } 21413816Ssam 21512106Sralph /* 21612106Sralph * Stuff for handling job specifications 21712106Sralph */ 21812106Sralph char *user[MAXUSERS]; /* users to process */ 21912106Sralph int users; /* # of users in user array */ 22012106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 22112106Sralph int requests; /* # of spool requests */ 22212875Sralph char *person; /* name of person doing lprm */ 22312106Sralph 224*16761Sralph char fromb[32]; /* buffer for client's machine name */ 225*16761Sralph char cbuf[BUFSIZ]; /* command line buffer */ 226*16761Sralph char *cmdnames[] = { 22712430Sralph "null", 22812430Sralph "printjob", 22912430Sralph "recvjob", 23012430Sralph "displayq short", 23112430Sralph "displayq long", 23212430Sralph "rmjob" 23312430Sralph }; 23412106Sralph 23513816Ssam doit() 23612106Sralph { 23712106Sralph register char *cp; 23812106Sralph register int n; 23912106Sralph 24012106Sralph for (;;) { 24112106Sralph cp = cbuf; 24212106Sralph do { 24313816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 24413816Ssam fatal("Command line too long"); 24513816Ssam if ((n = read(1, cp, 1)) != 1) { 24612106Sralph if (n < 0) 24712106Sralph fatal("Lost connection"); 24812106Sralph return; 24912106Sralph } 25013816Ssam } while (*cp++ != '\n'); 25112106Sralph *--cp = '\0'; 25212106Sralph cp = cbuf; 253*16761Sralph if (lflag) { 254*16761Sralph if (*cp >= '\1' && *cp <= '\5') 255*16761Sralph syslog(LOG_INFO, "%s requests %s %s", 256*16761Sralph from, cmdnames[*cp], cp+1); 257*16761Sralph else 258*16761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 259*16761Sralph *cp, from); 26012430Sralph } 26112106Sralph switch (*cp++) { 26212106Sralph case '\1': /* check the queue and print any jobs there */ 26312106Sralph printer = cp; 26412106Sralph printjob(); 26512106Sralph break; 26612106Sralph case '\2': /* receive files to be queued */ 26712106Sralph printer = cp; 26812106Sralph recvjob(); 26912106Sralph break; 27012430Sralph case '\3': /* display the queue (short form) */ 27112430Sralph case '\4': /* display the queue (long form) */ 27212106Sralph printer = cp; 27312106Sralph while (*cp) { 27412106Sralph if (*cp != ' ') { 27512106Sralph cp++; 27612106Sralph continue; 27712106Sralph } 27812106Sralph *cp++ = '\0'; 27912106Sralph while (isspace(*cp)) 28012106Sralph cp++; 28112106Sralph if (*cp == '\0') 28212106Sralph break; 28312106Sralph if (isdigit(*cp)) { 28412106Sralph if (requests >= MAXREQUESTS) 28512106Sralph fatal("Too many requests"); 28612106Sralph requ[requests++] = atoi(cp); 28712106Sralph } else { 28812106Sralph if (users >= MAXUSERS) 28912106Sralph fatal("Too many users"); 29012106Sralph user[users++] = cp; 29112106Sralph } 29212106Sralph } 29312106Sralph displayq(cbuf[0] - '\3'); 29412106Sralph exit(0); 29512106Sralph case '\5': /* remove a job from the queue */ 29612106Sralph printer = cp; 29712106Sralph while (*cp && *cp != ' ') 29812106Sralph cp++; 29912106Sralph if (!*cp) 30012106Sralph break; 30112106Sralph *cp++ = '\0'; 30212106Sralph person = cp; 30312106Sralph while (*cp) { 30412106Sralph if (*cp != ' ') { 30512106Sralph cp++; 30612106Sralph continue; 30712106Sralph } 30812106Sralph *cp++ = '\0'; 30912106Sralph while (isspace(*cp)) 31012106Sralph cp++; 31112106Sralph if (*cp == '\0') 31212106Sralph break; 31312106Sralph if (isdigit(*cp)) { 31412106Sralph if (requests >= MAXREQUESTS) 31512106Sralph fatal("Too many requests"); 31612106Sralph requ[requests++] = atoi(cp); 31712106Sralph } else { 31812106Sralph if (users >= MAXUSERS) 31912106Sralph fatal("Too many users"); 32012106Sralph user[users++] = cp; 32112106Sralph } 32212106Sralph } 32312106Sralph rmjob(); 32412106Sralph break; 32512106Sralph } 32612106Sralph fatal("Illegal service request"); 32712106Sralph } 32812106Sralph } 32912106Sralph 33012106Sralph /* 33112430Sralph * Make a pass through the printcap database and start printing any 33212430Sralph * files left from the last time the machine went down. 33312430Sralph */ 33412430Sralph startup() 33512430Sralph { 33612430Sralph char buf[BUFSIZ]; 33712430Sralph register char *cp; 33812430Sralph int pid; 33912430Sralph 34012430Sralph printer = buf; 34112430Sralph 34212430Sralph /* 34312430Sralph * Restart the daemons. 34412430Sralph */ 34512430Sralph while (getprent(buf) > 0) { 34612430Sralph for (cp = buf; *cp; cp++) 34712430Sralph if (*cp == '|' || *cp == ':') { 34812430Sralph *cp = '\0'; 34912430Sralph break; 35012430Sralph } 35112430Sralph if ((pid = fork()) < 0) { 352*16761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 353*16761Sralph mcleanup(); 35412430Sralph } 35512430Sralph if (!pid) { 35612430Sralph endprent(); 35712430Sralph printjob(); 35812430Sralph } 35912430Sralph } 36012430Sralph } 36112430Sralph 36212430Sralph /* 36312430Sralph * Check to see if the from host has access to the line printer. 36412430Sralph */ 36513816Ssam chkhost(f) 36613816Ssam struct sockaddr_in *f; 36712430Sralph { 36813816Ssam register struct hostent *hp; 36912430Sralph register FILE *hostf; 37012430Sralph register char *cp; 37112430Sralph char ahost[50]; 37215551Sralph int first = 1; 37313816Ssam extern char *inet_ntoa(); 37412430Sralph 37513816Ssam f->sin_port = ntohs(f->sin_port); 37613816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 37713816Ssam fatal("Malformed from address"); 37813816Ssam hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family); 37913816Ssam if (hp == 0) 38013816Ssam fatal("Host name for your address (%s) unknown", 38113816Ssam inet_ntoa(f->sin_addr)); 38213816Ssam 38313816Ssam strcpy(fromb, hp->h_name); 38413816Ssam from = fromb; 38512734Sralph if (!strcmp(from, host)) 38613816Ssam return; 38712734Sralph 38812430Sralph hostf = fopen("/etc/hosts.equiv", "r"); 38915551Sralph again: 39015551Sralph if (hostf) { 39115551Sralph while (fgets(ahost, sizeof (ahost), hostf)) { 39215551Sralph if (cp = index(ahost, '\n')) 39315551Sralph *cp = '\0'; 39415551Sralph cp = index(ahost, ' '); 39515551Sralph if (!strcmp(from, ahost) && cp == NULL) { 39615551Sralph (void) fclose(hostf); 39715551Sralph return; 39815551Sralph } 39912430Sralph } 40015551Sralph (void) fclose(hostf); 40112430Sralph } 40215551Sralph if (first == 1) { 40315551Sralph first = 0; 40415551Sralph hostf = fopen("/etc/hosts.lpd", "r"); 40515551Sralph goto again; 40615551Sralph } 40713816Ssam fatal("Your host does not have line printer access"); 40812430Sralph } 409