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*44708Skarels static char sccsid[] = "@(#)lpd.c 5.9 (Berkeley) 06/29/90"; 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 */ 5112875Sralph 5212106Sralph int reapchild(); 5316761Sralph int mcleanup(); 5412106Sralph 5512106Sralph main(argc, argv) 5612106Sralph int argc; 5712106Sralph char **argv; 5812106Sralph { 5913816Ssam int f, funix, finet, options, defreadfds, fromlen; 6013816Ssam struct sockaddr_un sun, 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 */ 84*44708Skarels 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); 12713816Ssam sun.sun_family = AF_UNIX; 12837968Sbostic strcpy(sun.sun_path, _PATH_SOCKETNAME); 12913816Ssam if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) { 13016761Sralph syslog(LOG_ERR, "ubind: %m"); 13112106Sralph exit(1); 13212106Sralph } 13313816Ssam sigsetmask(omask); 13414149Sralph defreadfds = 1 << funix; 13514149Sralph listen(funix, 5); 13613816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 13713816Ssam if (finet >= 0) { 13813816Ssam struct servent *sp; 13913816Ssam 14013816Ssam if (options & SO_DEBUG) 14113816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 14216761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 14316761Sralph mcleanup(); 14413816Ssam } 14513816Ssam sp = getservbyname("printer", "tcp"); 14613816Ssam if (sp == NULL) { 14716761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 14816761Sralph mcleanup(); 14913816Ssam } 15013816Ssam sin.sin_family = AF_INET; 15113816Ssam sin.sin_port = sp->s_port; 15213816Ssam if (bind(finet, &sin, sizeof(sin), 0) < 0) { 15316761Sralph syslog(LOG_ERR, "bind: %m"); 15416761Sralph mcleanup(); 15513816Ssam } 15614149Sralph defreadfds |= 1 << finet; 15714149Sralph listen(finet, 5); 15813816Ssam } 15912106Sralph /* 16014149Sralph * Main loop: accept, do a request, continue. 16112106Sralph */ 16212106Sralph for (;;) { 16313957Ssam int domain, nfds, s, readfds = defreadfds; 16412106Sralph 16513957Ssam nfds = select(20, &readfds, 0, 0, 0); 16613957Ssam if (nfds <= 0) { 16716761Sralph if (nfds < 0 && errno != EINTR) 16816761Sralph syslog(LOG_WARNING, "select: %m"); 16913957Ssam continue; 17013957Ssam } 17113816Ssam if (readfds & (1 << funix)) { 17213957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 17313816Ssam s = accept(funix, &fromunix, &fromlen); 17413957Ssam } else if (readfds & (1 << finet)) { 17513957Ssam domain = AF_INET, fromlen = sizeof(frominet); 17613816Ssam s = accept(finet, &frominet, &fromlen); 17713816Ssam } 17812106Sralph if (s < 0) { 17916761Sralph if (errno != EINTR) 18016761Sralph syslog(LOG_WARNING, "accept: %m"); 18116761Sralph continue; 18212106Sralph } 18312106Sralph if (fork() == 0) { 18413147Ssam signal(SIGCHLD, SIG_IGN); 18514708Sralph signal(SIGHUP, SIG_IGN); 18614708Sralph signal(SIGINT, SIG_IGN); 18714708Sralph signal(SIGQUIT, SIG_IGN); 18814708Sralph signal(SIGTERM, SIG_IGN); 18913816Ssam (void) close(funix); 19013816Ssam (void) close(finet); 19113816Ssam dup2(s, 1); 19213816Ssam (void) close(s); 19313816Ssam if (domain == AF_INET) 19413816Ssam chkhost(&frominet); 19513816Ssam doit(); 19612106Sralph exit(0); 19712106Sralph } 19812106Sralph (void) close(s); 19912106Sralph } 20012106Sralph } 20112106Sralph 20212106Sralph reapchild() 20312106Sralph { 20412106Sralph union wait status; 20512106Sralph 20612106Sralph while (wait3(&status, WNOHANG, 0) > 0) 20712106Sralph ; 20812106Sralph } 20912106Sralph 21016761Sralph mcleanup() 21113816Ssam { 21214149Sralph if (lflag) 21316761Sralph syslog(LOG_INFO, "exiting"); 21437968Sbostic unlink(_PATH_SOCKETNAME); 21513816Ssam exit(0); 21613816Ssam } 21713816Ssam 21812106Sralph /* 21912106Sralph * Stuff for handling job specifications 22012106Sralph */ 22112106Sralph char *user[MAXUSERS]; /* users to process */ 22212106Sralph int users; /* # of users in user array */ 22312106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 22412106Sralph int requests; /* # of spool requests */ 22512875Sralph char *person; /* name of person doing lprm */ 22612106Sralph 22716761Sralph char fromb[32]; /* buffer for client's machine name */ 22816761Sralph char cbuf[BUFSIZ]; /* command line buffer */ 22916761Sralph char *cmdnames[] = { 23012430Sralph "null", 23112430Sralph "printjob", 23212430Sralph "recvjob", 23312430Sralph "displayq short", 23412430Sralph "displayq long", 23512430Sralph "rmjob" 23612430Sralph }; 23712106Sralph 23813816Ssam doit() 23912106Sralph { 24012106Sralph register char *cp; 24112106Sralph register int n; 24212106Sralph 24312106Sralph for (;;) { 24412106Sralph cp = cbuf; 24512106Sralph do { 24613816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 24713816Ssam fatal("Command line too long"); 24813816Ssam if ((n = read(1, cp, 1)) != 1) { 24912106Sralph if (n < 0) 25012106Sralph fatal("Lost connection"); 25112106Sralph return; 25212106Sralph } 25313816Ssam } while (*cp++ != '\n'); 25412106Sralph *--cp = '\0'; 25512106Sralph cp = cbuf; 25616761Sralph if (lflag) { 25716761Sralph if (*cp >= '\1' && *cp <= '\5') 25816761Sralph syslog(LOG_INFO, "%s requests %s %s", 25916761Sralph from, cmdnames[*cp], cp+1); 26016761Sralph else 26116761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 26216761Sralph *cp, from); 26312430Sralph } 26412106Sralph switch (*cp++) { 26512106Sralph case '\1': /* check the queue and print any jobs there */ 26612106Sralph printer = cp; 26712106Sralph printjob(); 26812106Sralph break; 26912106Sralph case '\2': /* receive files to be queued */ 27012106Sralph printer = cp; 27112106Sralph recvjob(); 27212106Sralph break; 27312430Sralph case '\3': /* display the queue (short form) */ 27412430Sralph case '\4': /* display the queue (long form) */ 27512106Sralph printer = cp; 27612106Sralph while (*cp) { 27712106Sralph if (*cp != ' ') { 27812106Sralph cp++; 27912106Sralph continue; 28012106Sralph } 28112106Sralph *cp++ = '\0'; 28212106Sralph while (isspace(*cp)) 28312106Sralph cp++; 28412106Sralph if (*cp == '\0') 28512106Sralph break; 28612106Sralph if (isdigit(*cp)) { 28712106Sralph if (requests >= MAXREQUESTS) 28812106Sralph fatal("Too many requests"); 28912106Sralph requ[requests++] = atoi(cp); 29012106Sralph } else { 29112106Sralph if (users >= MAXUSERS) 29212106Sralph fatal("Too many users"); 29312106Sralph user[users++] = cp; 29412106Sralph } 29512106Sralph } 29612106Sralph displayq(cbuf[0] - '\3'); 29712106Sralph exit(0); 29812106Sralph case '\5': /* remove a job from the queue */ 29912106Sralph printer = cp; 30012106Sralph while (*cp && *cp != ' ') 30112106Sralph cp++; 30212106Sralph if (!*cp) 30312106Sralph break; 30412106Sralph *cp++ = '\0'; 30512106Sralph person = cp; 30612106Sralph while (*cp) { 30712106Sralph if (*cp != ' ') { 30812106Sralph cp++; 30912106Sralph continue; 31012106Sralph } 31112106Sralph *cp++ = '\0'; 31212106Sralph while (isspace(*cp)) 31312106Sralph cp++; 31412106Sralph if (*cp == '\0') 31512106Sralph break; 31612106Sralph if (isdigit(*cp)) { 31712106Sralph if (requests >= MAXREQUESTS) 31812106Sralph fatal("Too many requests"); 31912106Sralph requ[requests++] = atoi(cp); 32012106Sralph } else { 32112106Sralph if (users >= MAXUSERS) 32212106Sralph fatal("Too many users"); 32312106Sralph user[users++] = cp; 32412106Sralph } 32512106Sralph } 32612106Sralph rmjob(); 32712106Sralph break; 32812106Sralph } 32912106Sralph fatal("Illegal service request"); 33012106Sralph } 33112106Sralph } 33212106Sralph 33312106Sralph /* 33412430Sralph * Make a pass through the printcap database and start printing any 33512430Sralph * files left from the last time the machine went down. 33612430Sralph */ 33712430Sralph startup() 33812430Sralph { 33912430Sralph char buf[BUFSIZ]; 34012430Sralph register char *cp; 34112430Sralph int pid; 34212430Sralph 34312430Sralph printer = buf; 34412430Sralph 34512430Sralph /* 34612430Sralph * Restart the daemons. 34712430Sralph */ 34812430Sralph while (getprent(buf) > 0) { 34912430Sralph for (cp = buf; *cp; cp++) 35012430Sralph if (*cp == '|' || *cp == ':') { 35112430Sralph *cp = '\0'; 35212430Sralph break; 35312430Sralph } 35412430Sralph if ((pid = fork()) < 0) { 35516761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 35616761Sralph mcleanup(); 35712430Sralph } 35812430Sralph if (!pid) { 35912430Sralph endprent(); 36012430Sralph printjob(); 36112430Sralph } 36212430Sralph } 36312430Sralph } 36412430Sralph 36527746Sbloom #define DUMMY ":nobody::" 36627746Sbloom 36712430Sralph /* 36812430Sralph * Check to see if the from host has access to the line printer. 36912430Sralph */ 37013816Ssam chkhost(f) 37113816Ssam struct sockaddr_in *f; 37212430Sralph { 37313816Ssam register struct hostent *hp; 37412430Sralph register FILE *hostf; 37527746Sbloom register char *cp, *sp; 37612430Sralph char ahost[50]; 37715551Sralph int first = 1; 37813816Ssam extern char *inet_ntoa(); 37927746Sbloom int baselen = -1; 38012430Sralph 38113816Ssam f->sin_port = ntohs(f->sin_port); 38213816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 38313816Ssam fatal("Malformed from address"); 38413816Ssam hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family); 38513816Ssam if (hp == 0) 38613816Ssam fatal("Host name for your address (%s) unknown", 38713816Ssam inet_ntoa(f->sin_addr)); 38813816Ssam 38913816Ssam strcpy(fromb, hp->h_name); 39013816Ssam from = fromb; 39112734Sralph if (!strcmp(from, host)) 39213816Ssam return; 39312734Sralph 39427746Sbloom sp = fromb; 39527746Sbloom cp = ahost; 39627746Sbloom while (*sp) { 39727746Sbloom if (*sp == '.') { 39827746Sbloom if (baselen == -1) 39927746Sbloom baselen = sp - fromb; 40027746Sbloom *cp++ = *sp++; 40127746Sbloom } else { 40227746Sbloom *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++; 40327746Sbloom } 40427746Sbloom } 40527746Sbloom *cp = '\0'; 40637968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 40715551Sralph again: 40815551Sralph if (hostf) { 40927746Sbloom if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) { 41027746Sbloom (void) fclose(hostf); 41127746Sbloom return; 41212430Sralph } 41315551Sralph (void) fclose(hostf); 41412430Sralph } 41515551Sralph if (first == 1) { 41615551Sralph first = 0; 41737968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 41815551Sralph goto again; 41915551Sralph } 42013816Ssam fatal("Your host does not have line printer access"); 42112430Sralph } 422