122429Sdist /* 222429Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 534203Sbostic * Redistribution and use in source and binary forms are permitted 634936Sbostic * provided that the above copyright notice and this paragraph are 734936Sbostic * duplicated in all such forms and that any documentation, 834936Sbostic * advertising materials, and other materials related to such 934936Sbostic * distribution and use acknowledge that the software was developed 1034936Sbostic * by the University of California, Berkeley. The name of the 1134936Sbostic * University may not be used to endorse or promote products derived 1234936Sbostic * from this software without specific prior written permission. 1334936Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434936Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534936Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1622429Sdist */ 1722429Sdist 1813957Ssam #ifndef lint 1922429Sdist char copyright[] = 2022429Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 2122429Sdist All rights reserved.\n"; 2234203Sbostic #endif /* not lint */ 2313957Ssam 2422429Sdist #ifndef lint 25*37968Sbostic static char sccsid[] = "@(#)lpd.c 5.7 (Berkeley) 05/11/89"; 2634203Sbostic #endif /* not lint */ 2722429Sdist 2812106Sralph /* 2912106Sralph * lpd -- line printer daemon. 3012106Sralph * 3112106Sralph * Listen for a connection and perform the requested operation. 3212106Sralph * Operations are: 3312106Sralph * \1printer\n 3412106Sralph * check the queue for jobs and print any found. 3512106Sralph * \2printer\n 3612106Sralph * receive a job from another machine and queue it. 3712106Sralph * \3printer [users ...] [jobs ...]\n 3812106Sralph * return the current state of the queue (short form). 3912106Sralph * \4printer [users ...] [jobs ...]\n 4012106Sralph * return the current state of the queue (long form). 4112106Sralph * \5printer person [users ...] [jobs ...]\n 4212106Sralph * remove jobs from the queue. 4312106Sralph * 4412106Sralph * Strategy to maintain protected spooling area: 4512106Sralph * 1. Spooling area is writable only by daemon and spooling group 4612106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses 4712106Sralph * root to access any file it wants (verifying things before 4812106Sralph * with an access call) and group id to know how it should 4912106Sralph * set up ownership of files in the spooling area. 5012430Sralph * 3. Files in spooling area are owned by root, group spooling 5112106Sralph * group, with mode 660. 5212106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 5312106Sralph * access files and printer. Users can't get to anything 5412106Sralph * w/o help of lpq and lprm programs. 5512106Sralph */ 5612106Sralph 5712106Sralph #include "lp.h" 58*37968Sbostic #include "pathnames.h" 5912106Sralph 6016761Sralph int lflag; /* log requests flag */ 6112875Sralph 6212106Sralph int reapchild(); 6316761Sralph int mcleanup(); 6412106Sralph 6512106Sralph main(argc, argv) 6612106Sralph int argc; 6712106Sralph char **argv; 6812106Sralph { 6913816Ssam int f, funix, finet, options, defreadfds, fromlen; 7013816Ssam struct sockaddr_un sun, fromunix; 7113816Ssam struct sockaddr_in sin, frominet; 7214837Sralph int omask, lfd; 7312106Sralph 7412106Sralph gethostname(host, sizeof(host)); 7512106Sralph name = argv[0]; 7612106Sralph 7712106Sralph while (--argc > 0) { 7812106Sralph argv++; 7912106Sralph if (argv[0][0] == '-') 8012106Sralph switch (argv[0][1]) { 8112106Sralph case 'd': 8212106Sralph options |= SO_DEBUG; 8312106Sralph break; 8412106Sralph case 'l': 8512430Sralph lflag++; 8612430Sralph break; 8712106Sralph } 8812106Sralph } 8914837Sralph 9012106Sralph #ifndef DEBUG 9112106Sralph /* 9212106Sralph * Set up standard environment by detaching from the parent. 9312106Sralph */ 9412106Sralph if (fork()) 9512106Sralph exit(0); 9616761Sralph for (f = 0; f < 5; f++) 9712106Sralph (void) close(f); 98*37968Sbostic (void) open(_PATH_DEVNULL, O_RDONLY); 99*37968Sbostic (void) open(_PATH_DEVNULL, O_WRONLY); 10016761Sralph (void) dup(1); 101*37968Sbostic f = open(_PATH_TTY, O_RDWR); 10212106Sralph if (f > 0) { 10312106Sralph ioctl(f, TIOCNOTTY, 0); 10412106Sralph (void) close(f); 10512106Sralph } 10612106Sralph #endif 10714837Sralph 10825494Seric openlog("lpd", LOG_PID, LOG_LPR); 10912106Sralph (void) umask(0); 110*37968Sbostic lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644); 11114837Sralph if (lfd < 0) { 112*37968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 11314837Sralph exit(1); 11414837Sralph } 11514837Sralph if (flock(lfd, LOCK_EX|LOCK_NB) < 0) { 11614837Sralph if (errno == EWOULDBLOCK) /* active deamon present */ 11714837Sralph exit(0); 118*37968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 11914837Sralph exit(1); 12014837Sralph } 12114837Sralph ftruncate(lfd, 0); 12212875Sralph /* 12314837Sralph * write process id for others to know 12414837Sralph */ 12514837Sralph sprintf(line, "%u\n", getpid()); 12614837Sralph f = strlen(line); 12714837Sralph if (write(lfd, line, f) != f) { 128*37968Sbostic syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK); 12914837Sralph exit(1); 13014837Sralph } 13114837Sralph signal(SIGCHLD, reapchild); 13214837Sralph /* 13312875Sralph * Restart all the printers. 13412875Sralph */ 13512875Sralph startup(); 136*37968Sbostic (void) unlink(_PATH_SOCKETNAME); 13713816Ssam funix = socket(AF_UNIX, SOCK_STREAM, 0); 13813816Ssam if (funix < 0) { 13916761Sralph syslog(LOG_ERR, "socket: %m"); 14012106Sralph exit(1); 14112106Sralph } 14214149Sralph #define mask(s) (1 << ((s) - 1)) 14314149Sralph omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM)); 14416761Sralph signal(SIGHUP, mcleanup); 14516761Sralph signal(SIGINT, mcleanup); 14616761Sralph signal(SIGQUIT, mcleanup); 14716761Sralph signal(SIGTERM, mcleanup); 14813816Ssam sun.sun_family = AF_UNIX; 149*37968Sbostic strcpy(sun.sun_path, _PATH_SOCKETNAME); 15013816Ssam if (bind(funix, &sun, strlen(sun.sun_path) + 2) < 0) { 15116761Sralph syslog(LOG_ERR, "ubind: %m"); 15212106Sralph exit(1); 15312106Sralph } 15413816Ssam sigsetmask(omask); 15514149Sralph defreadfds = 1 << funix; 15614149Sralph listen(funix, 5); 15713816Ssam finet = socket(AF_INET, SOCK_STREAM, 0); 15813816Ssam if (finet >= 0) { 15913816Ssam struct servent *sp; 16013816Ssam 16113816Ssam if (options & SO_DEBUG) 16213816Ssam if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 16316761Sralph syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m"); 16416761Sralph mcleanup(); 16513816Ssam } 16613816Ssam sp = getservbyname("printer", "tcp"); 16713816Ssam if (sp == NULL) { 16816761Sralph syslog(LOG_ERR, "printer/tcp: unknown service"); 16916761Sralph mcleanup(); 17013816Ssam } 17113816Ssam sin.sin_family = AF_INET; 17213816Ssam sin.sin_port = sp->s_port; 17313816Ssam if (bind(finet, &sin, sizeof(sin), 0) < 0) { 17416761Sralph syslog(LOG_ERR, "bind: %m"); 17516761Sralph mcleanup(); 17613816Ssam } 17714149Sralph defreadfds |= 1 << finet; 17814149Sralph listen(finet, 5); 17913816Ssam } 18012106Sralph /* 18114149Sralph * Main loop: accept, do a request, continue. 18212106Sralph */ 18312106Sralph for (;;) { 18413957Ssam int domain, nfds, s, readfds = defreadfds; 18512106Sralph 18613957Ssam nfds = select(20, &readfds, 0, 0, 0); 18713957Ssam if (nfds <= 0) { 18816761Sralph if (nfds < 0 && errno != EINTR) 18916761Sralph syslog(LOG_WARNING, "select: %m"); 19013957Ssam continue; 19113957Ssam } 19213816Ssam if (readfds & (1 << funix)) { 19313957Ssam domain = AF_UNIX, fromlen = sizeof(fromunix); 19413816Ssam s = accept(funix, &fromunix, &fromlen); 19513957Ssam } else if (readfds & (1 << finet)) { 19613957Ssam domain = AF_INET, fromlen = sizeof(frominet); 19713816Ssam s = accept(finet, &frominet, &fromlen); 19813816Ssam } 19912106Sralph if (s < 0) { 20016761Sralph if (errno != EINTR) 20116761Sralph syslog(LOG_WARNING, "accept: %m"); 20216761Sralph continue; 20312106Sralph } 20412106Sralph if (fork() == 0) { 20513147Ssam signal(SIGCHLD, SIG_IGN); 20614708Sralph signal(SIGHUP, SIG_IGN); 20714708Sralph signal(SIGINT, SIG_IGN); 20814708Sralph signal(SIGQUIT, SIG_IGN); 20914708Sralph signal(SIGTERM, SIG_IGN); 21013816Ssam (void) close(funix); 21113816Ssam (void) close(finet); 21213816Ssam dup2(s, 1); 21313816Ssam (void) close(s); 21413816Ssam if (domain == AF_INET) 21513816Ssam chkhost(&frominet); 21613816Ssam doit(); 21712106Sralph exit(0); 21812106Sralph } 21912106Sralph (void) close(s); 22012106Sralph } 22112106Sralph } 22212106Sralph 22312106Sralph reapchild() 22412106Sralph { 22512106Sralph union wait status; 22612106Sralph 22712106Sralph while (wait3(&status, WNOHANG, 0) > 0) 22812106Sralph ; 22912106Sralph } 23012106Sralph 23116761Sralph mcleanup() 23213816Ssam { 23314149Sralph if (lflag) 23416761Sralph syslog(LOG_INFO, "exiting"); 235*37968Sbostic unlink(_PATH_SOCKETNAME); 23613816Ssam exit(0); 23713816Ssam } 23813816Ssam 23912106Sralph /* 24012106Sralph * Stuff for handling job specifications 24112106Sralph */ 24212106Sralph char *user[MAXUSERS]; /* users to process */ 24312106Sralph int users; /* # of users in user array */ 24412106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 24512106Sralph int requests; /* # of spool requests */ 24612875Sralph char *person; /* name of person doing lprm */ 24712106Sralph 24816761Sralph char fromb[32]; /* buffer for client's machine name */ 24916761Sralph char cbuf[BUFSIZ]; /* command line buffer */ 25016761Sralph char *cmdnames[] = { 25112430Sralph "null", 25212430Sralph "printjob", 25312430Sralph "recvjob", 25412430Sralph "displayq short", 25512430Sralph "displayq long", 25612430Sralph "rmjob" 25712430Sralph }; 25812106Sralph 25913816Ssam doit() 26012106Sralph { 26112106Sralph register char *cp; 26212106Sralph register int n; 26312106Sralph 26412106Sralph for (;;) { 26512106Sralph cp = cbuf; 26612106Sralph do { 26713816Ssam if (cp >= &cbuf[sizeof(cbuf) - 1]) 26813816Ssam fatal("Command line too long"); 26913816Ssam if ((n = read(1, cp, 1)) != 1) { 27012106Sralph if (n < 0) 27112106Sralph fatal("Lost connection"); 27212106Sralph return; 27312106Sralph } 27413816Ssam } while (*cp++ != '\n'); 27512106Sralph *--cp = '\0'; 27612106Sralph cp = cbuf; 27716761Sralph if (lflag) { 27816761Sralph if (*cp >= '\1' && *cp <= '\5') 27916761Sralph syslog(LOG_INFO, "%s requests %s %s", 28016761Sralph from, cmdnames[*cp], cp+1); 28116761Sralph else 28216761Sralph syslog(LOG_INFO, "bad request (%d) from %s", 28316761Sralph *cp, from); 28412430Sralph } 28512106Sralph switch (*cp++) { 28612106Sralph case '\1': /* check the queue and print any jobs there */ 28712106Sralph printer = cp; 28812106Sralph printjob(); 28912106Sralph break; 29012106Sralph case '\2': /* receive files to be queued */ 29112106Sralph printer = cp; 29212106Sralph recvjob(); 29312106Sralph break; 29412430Sralph case '\3': /* display the queue (short form) */ 29512430Sralph case '\4': /* display the queue (long form) */ 29612106Sralph printer = cp; 29712106Sralph while (*cp) { 29812106Sralph if (*cp != ' ') { 29912106Sralph cp++; 30012106Sralph continue; 30112106Sralph } 30212106Sralph *cp++ = '\0'; 30312106Sralph while (isspace(*cp)) 30412106Sralph cp++; 30512106Sralph if (*cp == '\0') 30612106Sralph break; 30712106Sralph if (isdigit(*cp)) { 30812106Sralph if (requests >= MAXREQUESTS) 30912106Sralph fatal("Too many requests"); 31012106Sralph requ[requests++] = atoi(cp); 31112106Sralph } else { 31212106Sralph if (users >= MAXUSERS) 31312106Sralph fatal("Too many users"); 31412106Sralph user[users++] = cp; 31512106Sralph } 31612106Sralph } 31712106Sralph displayq(cbuf[0] - '\3'); 31812106Sralph exit(0); 31912106Sralph case '\5': /* remove a job from the queue */ 32012106Sralph printer = cp; 32112106Sralph while (*cp && *cp != ' ') 32212106Sralph cp++; 32312106Sralph if (!*cp) 32412106Sralph break; 32512106Sralph *cp++ = '\0'; 32612106Sralph person = cp; 32712106Sralph while (*cp) { 32812106Sralph if (*cp != ' ') { 32912106Sralph cp++; 33012106Sralph continue; 33112106Sralph } 33212106Sralph *cp++ = '\0'; 33312106Sralph while (isspace(*cp)) 33412106Sralph cp++; 33512106Sralph if (*cp == '\0') 33612106Sralph break; 33712106Sralph if (isdigit(*cp)) { 33812106Sralph if (requests >= MAXREQUESTS) 33912106Sralph fatal("Too many requests"); 34012106Sralph requ[requests++] = atoi(cp); 34112106Sralph } else { 34212106Sralph if (users >= MAXUSERS) 34312106Sralph fatal("Too many users"); 34412106Sralph user[users++] = cp; 34512106Sralph } 34612106Sralph } 34712106Sralph rmjob(); 34812106Sralph break; 34912106Sralph } 35012106Sralph fatal("Illegal service request"); 35112106Sralph } 35212106Sralph } 35312106Sralph 35412106Sralph /* 35512430Sralph * Make a pass through the printcap database and start printing any 35612430Sralph * files left from the last time the machine went down. 35712430Sralph */ 35812430Sralph startup() 35912430Sralph { 36012430Sralph char buf[BUFSIZ]; 36112430Sralph register char *cp; 36212430Sralph int pid; 36312430Sralph 36412430Sralph printer = buf; 36512430Sralph 36612430Sralph /* 36712430Sralph * Restart the daemons. 36812430Sralph */ 36912430Sralph while (getprent(buf) > 0) { 37012430Sralph for (cp = buf; *cp; cp++) 37112430Sralph if (*cp == '|' || *cp == ':') { 37212430Sralph *cp = '\0'; 37312430Sralph break; 37412430Sralph } 37512430Sralph if ((pid = fork()) < 0) { 37616761Sralph syslog(LOG_WARNING, "startup: cannot fork"); 37716761Sralph mcleanup(); 37812430Sralph } 37912430Sralph if (!pid) { 38012430Sralph endprent(); 38112430Sralph printjob(); 38212430Sralph } 38312430Sralph } 38412430Sralph } 38512430Sralph 38627746Sbloom #define DUMMY ":nobody::" 38727746Sbloom 38812430Sralph /* 38912430Sralph * Check to see if the from host has access to the line printer. 39012430Sralph */ 39113816Ssam chkhost(f) 39213816Ssam struct sockaddr_in *f; 39312430Sralph { 39413816Ssam register struct hostent *hp; 39512430Sralph register FILE *hostf; 39627746Sbloom register char *cp, *sp; 39712430Sralph char ahost[50]; 39815551Sralph int first = 1; 39913816Ssam extern char *inet_ntoa(); 40027746Sbloom int baselen = -1; 40112430Sralph 40213816Ssam f->sin_port = ntohs(f->sin_port); 40313816Ssam if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED) 40413816Ssam fatal("Malformed from address"); 40513816Ssam hp = gethostbyaddr(&f->sin_addr, sizeof(struct in_addr), f->sin_family); 40613816Ssam if (hp == 0) 40713816Ssam fatal("Host name for your address (%s) unknown", 40813816Ssam inet_ntoa(f->sin_addr)); 40913816Ssam 41013816Ssam strcpy(fromb, hp->h_name); 41113816Ssam from = fromb; 41212734Sralph if (!strcmp(from, host)) 41313816Ssam return; 41412734Sralph 41527746Sbloom sp = fromb; 41627746Sbloom cp = ahost; 41727746Sbloom while (*sp) { 41827746Sbloom if (*sp == '.') { 41927746Sbloom if (baselen == -1) 42027746Sbloom baselen = sp - fromb; 42127746Sbloom *cp++ = *sp++; 42227746Sbloom } else { 42327746Sbloom *cp++ = isupper(*sp) ? tolower(*sp++) : *sp++; 42427746Sbloom } 42527746Sbloom } 42627746Sbloom *cp = '\0'; 427*37968Sbostic hostf = fopen(_PATH_HOSTSEQUIV, "r"); 42815551Sralph again: 42915551Sralph if (hostf) { 43027746Sbloom if (!_validuser(hostf, ahost, DUMMY, DUMMY, baselen)) { 43127746Sbloom (void) fclose(hostf); 43227746Sbloom return; 43312430Sralph } 43415551Sralph (void) fclose(hostf); 43512430Sralph } 43615551Sralph if (first == 1) { 43715551Sralph first = 0; 438*37968Sbostic hostf = fopen(_PATH_HOSTSLPD, "r"); 43915551Sralph goto again; 44015551Sralph } 44113816Ssam fatal("Your host does not have line printer access"); 44212430Sralph } 443