1*12430Sralph /* lpd.c 4.2 83/05/13 */ 212106Sralph /* 312106Sralph * lpd -- line printer daemon. 412106Sralph * 512106Sralph * Listen for a connection and perform the requested operation. 612106Sralph * Operations are: 712106Sralph * \1printer\n 812106Sralph * check the queue for jobs and print any found. 912106Sralph * \2printer\n 1012106Sralph * receive a job from another machine and queue it. 1112106Sralph * \3printer [users ...] [jobs ...]\n 1212106Sralph * return the current state of the queue (short form). 1312106Sralph * \4printer [users ...] [jobs ...]\n 1412106Sralph * return the current state of the queue (long form). 1512106Sralph * \5printer person [users ...] [jobs ...]\n 1612106Sralph * remove jobs from the queue. 17*12430Sralph * \6printer\n 18*12430Sralph * enable queuing on the specified printer queue. 19*12430Sralph * \7printer\n 20*12430Sralph * disable queuing on the specified printer queue. 21*12430Sralph * \8printer\n 22*12430Sralph * return the queue status (queuing enabled or disabled). 2312106Sralph * 2412106Sralph * Strategy to maintain protected spooling area: 2512106Sralph * 1. Spooling area is writable only by daemon and spooling group 2612106Sralph * 2. lpr runs setuid root and setgrp spooling group; it uses 2712106Sralph * root to access any file it wants (verifying things before 2812106Sralph * with an access call) and group id to know how it should 2912106Sralph * set up ownership of files in the spooling area. 30*12430Sralph * 3. Files in spooling area are owned by root, group spooling 3112106Sralph * group, with mode 660. 3212106Sralph * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to 3312106Sralph * access files and printer. Users can't get to anything 3412106Sralph * w/o help of lpq and lprm programs. 3512106Sralph */ 3612106Sralph 3712106Sralph #include "lp.h" 3812106Sralph 39*12430Sralph int lflag; /* log requests flag */ 4012106Sralph char *logfile = DEFLOGF; 4112106Sralph struct sockaddr_in sin = { AF_INET }; 4212106Sralph int reapchild(); 4312106Sralph char *ntoa(); 4412106Sralph 4512106Sralph main(argc, argv) 4612106Sralph int argc; 4712106Sralph char **argv; 4812106Sralph { 4912106Sralph int f, options; 50*12430Sralph struct sockaddr_in fromaddr; 5112106Sralph struct servent *sp; 5212106Sralph 5312106Sralph gethostname(host, sizeof(host)); 5412106Sralph name = argv[0]; 5512106Sralph 5612106Sralph sp = getservbyname("printer", "tcp"); 5712106Sralph if (sp == NULL) { 58*12430Sralph fprintf(stderr, "printer/tcp: unknown service"); 5912106Sralph exit(1); 6012106Sralph } 6112106Sralph sin.sin_port = sp->s_port; 6212106Sralph 6312106Sralph while (--argc > 0) { 6412106Sralph argv++; 6512106Sralph if (argv[0][0] == '-') 6612106Sralph switch (argv[0][1]) { 6712106Sralph case 'd': 6812106Sralph options |= SO_DEBUG; 6912106Sralph break; 7012106Sralph case 'l': 71*12430Sralph lflag++; 72*12430Sralph break; 73*12430Sralph case 'L': 7412106Sralph argc--; 7512106Sralph logfile = *++argv; 7612106Sralph break; 7712106Sralph } 7812106Sralph else { 7912106Sralph int port = atoi(argv[0]); 80*12430Sralph int c = argv[0][0]; 8112106Sralph 82*12430Sralph if (c < '0' || c > '9' || port < 0) { 83*12430Sralph fprintf(stderr, "lpd: %s: bad port number\n", argv[0]); 8412106Sralph exit(1); 8512106Sralph } 8612106Sralph sin.sin_port = htons((u_short) port); 8712106Sralph } 8812106Sralph } 8912106Sralph #ifndef DEBUG 9012106Sralph /* 9112106Sralph * Set up standard environment by detaching from the parent. 9212106Sralph */ 9312106Sralph if (fork()) 9412106Sralph exit(0); 9512106Sralph for (f = 0; f < 3; f++) 9612106Sralph (void) close(f); 9712106Sralph (void) open("/dev/null", FRDONLY, 0); 9812106Sralph (void) open("/dev/null", FWRONLY, 0); 9912106Sralph (void) open(logfile, FWRONLY|FAPPEND, 0); 10012106Sralph f = open("/dev/tty", FRDWR, 0); 10112106Sralph if (f > 0) { 10212106Sralph ioctl(f, TIOCNOTTY, 0); 10312106Sralph (void) close(f); 10412106Sralph } 10512106Sralph #endif 10612106Sralph (void) umask(0); 10712106Sralph f = socket(AF_INET, SOCK_STREAM, 0); 10812106Sralph if (f < 0) { 10912106Sralph logerror("socket"); 11012106Sralph exit(1); 11112106Sralph } 11212106Sralph if (options & SO_DEBUG) 113*12430Sralph if (setsockopt(f, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) { 11412106Sralph logerror("setsockopt (SO_DEBUG)"); 115*12430Sralph exit(1); 116*12430Sralph } 11712106Sralph if (bind(f, &sin, sizeof(sin), 0) < 0) { 11812106Sralph logerror("bind"); 11912106Sralph exit(1); 12012106Sralph } 12112106Sralph /* 12212106Sralph * Restart all the printers and tell everyone that we are 12312106Sralph * up and running. 12412106Sralph */ 12512106Sralph startup(); 12612106Sralph /* 12712106Sralph * Main loop: listen, accept, do a request, continue. 12812106Sralph */ 12912106Sralph sigset(SIGCHLD, reapchild); 13012106Sralph listen(f, 10); 13112106Sralph for (;;) { 132*12430Sralph int s, len = sizeof(fromaddr); 13312106Sralph 134*12430Sralph s = accept(f, &fromaddr, &len, 0); 13512106Sralph if (s < 0) { 13612106Sralph if (errno == EINTR) 13712106Sralph continue; 13812106Sralph logerror("accept"); 139*12430Sralph exit(1); 14012106Sralph } 14112106Sralph if (fork() == 0) { 14212106Sralph sigset(SIGCHLD, SIG_IGN); 14312106Sralph (void) close(f); 144*12430Sralph doit(s, &fromaddr); 14512106Sralph exit(0); 14612106Sralph } 14712106Sralph (void) close(s); 14812106Sralph } 14912106Sralph } 15012106Sralph 15112106Sralph reapchild() 15212106Sralph { 15312106Sralph union wait status; 15412106Sralph 15512106Sralph while (wait3(&status, WNOHANG, 0) > 0) 15612106Sralph ; 15712106Sralph } 15812106Sralph 15912106Sralph /* 16012106Sralph * Stuff for handling job specifications 16112106Sralph */ 16212106Sralph char *user[MAXUSERS]; /* users to process */ 16312106Sralph int users; /* # of users in user array */ 16412106Sralph int requ[MAXREQUESTS]; /* job number of spool entries */ 16512106Sralph int requests; /* # of spool requests */ 16612106Sralph 167*12430Sralph char fromb[32]; /* buffer for client's machine name */ 16812106Sralph char cbuf[BUFSIZ]; /* command line buffer */ 169*12430Sralph char *cmdnames[] = { 170*12430Sralph "null", 171*12430Sralph "printjob", 172*12430Sralph "recvjob", 173*12430Sralph "displayq short", 174*12430Sralph "displayq long", 175*12430Sralph "rmjob" 176*12430Sralph }; 17712106Sralph 178*12430Sralph doit(f, fromaddr) 17912106Sralph int f; 180*12430Sralph struct sockaddr_in *fromaddr; 18112106Sralph { 18212106Sralph register char *cp; 18312106Sralph register struct hostent *hp; 18412106Sralph register int n; 18512106Sralph extern char *person; 18612106Sralph char c; 18712106Sralph 18812106Sralph dup2(f, 1); 18912106Sralph (void) close(f); 19012106Sralph f = 1; 191*12430Sralph fromaddr->sin_port = ntohs(fromaddr->sin_port); 192*12430Sralph if (fromaddr->sin_family != AF_INET || fromaddr->sin_port >= IPPORT_RESERVED) 19312106Sralph fatal("Malformed from address"); 194*12430Sralph hp = gethostbyaddr(&fromaddr->sin_addr, sizeof(struct in_addr), 195*12430Sralph fromaddr->sin_family); 19612106Sralph if (hp == 0) 19712106Sralph fatal("Host name for your address (%s) unknown", 198*12430Sralph ntoa(fromaddr->sin_addr)); 19912106Sralph strcpy(fromb, hp->h_name); 20012106Sralph from = fromb; 201*12430Sralph if (chkhost()) 202*12430Sralph fatal("Your host does not have line printer access"); 20312106Sralph for (;;) { 20412106Sralph cp = cbuf; 20512106Sralph do { 20612106Sralph if ((n = read(f, &c, 1)) != 1) { 20712106Sralph if (n < 0) 20812106Sralph fatal("Lost connection"); 20912106Sralph return; 21012106Sralph } 21112106Sralph if (cp >= &cbuf[sizeof(cbuf)]) 21212106Sralph fatal("Command line too long"); 21312106Sralph *cp++ = c; 21412106Sralph } while (c != '\n'); 21512106Sralph *--cp = '\0'; 21612106Sralph cp = cbuf; 217*12430Sralph if (lflag && *cp >= '\1' && *cp <= '\5') { 218*12430Sralph printer = NULL; 219*12430Sralph log("%s requests %s %s", from, cmdnames[*cp], cp+1); 220*12430Sralph } 22112106Sralph switch (*cp++) { 22212106Sralph case '\1': /* check the queue and print any jobs there */ 22312106Sralph printer = cp; 22412106Sralph printjob(); 22512106Sralph break; 22612106Sralph case '\2': /* receive files to be queued */ 22712106Sralph printer = cp; 22812106Sralph recvjob(); 22912106Sralph break; 230*12430Sralph case '\3': /* display the queue (short form) */ 231*12430Sralph case '\4': /* display the queue (long form) */ 23212106Sralph printer = cp; 23312106Sralph while (*cp) { 23412106Sralph if (*cp != ' ') { 23512106Sralph cp++; 23612106Sralph continue; 23712106Sralph } 23812106Sralph *cp++ = '\0'; 23912106Sralph while (isspace(*cp)) 24012106Sralph cp++; 24112106Sralph if (*cp == '\0') 24212106Sralph break; 24312106Sralph if (isdigit(*cp)) { 24412106Sralph if (requests >= MAXREQUESTS) 24512106Sralph fatal("Too many requests"); 24612106Sralph requ[requests++] = atoi(cp); 24712106Sralph } else { 24812106Sralph if (users >= MAXUSERS) 24912106Sralph fatal("Too many users"); 25012106Sralph user[users++] = cp; 25112106Sralph } 25212106Sralph } 25312106Sralph displayq(cbuf[0] - '\3'); 25412106Sralph exit(0); 25512106Sralph case '\5': /* remove a job from the queue */ 25612106Sralph printer = cp; 25712106Sralph while (*cp && *cp != ' ') 25812106Sralph cp++; 25912106Sralph if (!*cp) 26012106Sralph break; 26112106Sralph *cp++ = '\0'; 26212106Sralph person = cp; 26312106Sralph while (*cp) { 26412106Sralph if (*cp != ' ') { 26512106Sralph cp++; 26612106Sralph continue; 26712106Sralph } 26812106Sralph *cp++ = '\0'; 26912106Sralph while (isspace(*cp)) 27012106Sralph cp++; 27112106Sralph if (*cp == '\0') 27212106Sralph break; 27312106Sralph if (isdigit(*cp)) { 27412106Sralph if (requests >= MAXREQUESTS) 27512106Sralph fatal("Too many requests"); 27612106Sralph requ[requests++] = atoi(cp); 27712106Sralph } else { 27812106Sralph if (users >= MAXUSERS) 27912106Sralph fatal("Too many users"); 28012106Sralph user[users++] = cp; 28112106Sralph } 28212106Sralph } 28312106Sralph rmjob(); 28412106Sralph break; 28512106Sralph } 28612106Sralph fatal("Illegal service request"); 28712106Sralph exit(1); 28812106Sralph } 28912106Sralph } 29012106Sralph 29112106Sralph /* 292*12430Sralph * Make a pass through the printcap database and start printing any 293*12430Sralph * files left from the last time the machine went down. 294*12430Sralph */ 295*12430Sralph startup() 296*12430Sralph { 297*12430Sralph char buf[BUFSIZ]; 298*12430Sralph register char *cp; 299*12430Sralph int pid; 300*12430Sralph 301*12430Sralph printer = buf; 302*12430Sralph 303*12430Sralph /* 304*12430Sralph * Restart the daemons. 305*12430Sralph */ 306*12430Sralph while (getprent(buf) > 0) { 307*12430Sralph for (cp = buf; *cp; cp++) 308*12430Sralph if (*cp == '|' || *cp == ':') { 309*12430Sralph *cp = '\0'; 310*12430Sralph break; 311*12430Sralph } 312*12430Sralph if ((pid = fork()) < 0) { 313*12430Sralph log("startup: cannot fork"); 314*12430Sralph exit(1); 315*12430Sralph } 316*12430Sralph if (!pid) { 317*12430Sralph endprent(); 318*12430Sralph printjob(); 319*12430Sralph } 320*12430Sralph } 321*12430Sralph } 322*12430Sralph 323*12430Sralph /* 324*12430Sralph * Check to see if the from host has access to the line printer. 325*12430Sralph */ 326*12430Sralph chkhost() 327*12430Sralph { 328*12430Sralph register FILE *hostf; 329*12430Sralph register char *cp; 330*12430Sralph char ahost[50]; 331*12430Sralph 332*12430Sralph hostf = fopen("/etc/hosts.equiv", "r"); 333*12430Sralph while (fgets(ahost, sizeof(ahost), hostf)) { 334*12430Sralph if (cp = index(ahost, '\n')) 335*12430Sralph *cp = '\0'; 336*12430Sralph cp = index(ahost, ' '); 337*12430Sralph if (!strcmp(from, ahost) && cp == NULL) { 338*12430Sralph (void) fclose(hostf); 339*12430Sralph return(0); 340*12430Sralph } 341*12430Sralph } 342*12430Sralph (void) fclose(hostf); 343*12430Sralph return(-1); 344*12430Sralph } 345*12430Sralph 346*12430Sralph /* 34712106Sralph * Convert network-format internet address 34812106Sralph * to base 256 d.d.d.d representation. 34912106Sralph */ 35012106Sralph char * 35112106Sralph ntoa(in) 35212106Sralph struct in_addr in; 35312106Sralph { 35412106Sralph static char b[18]; 35512106Sralph register char *p; 35612106Sralph 35712106Sralph p = (char *)∈ 35812106Sralph #define UC(b) (((int)b)&0xff) 35912106Sralph sprintf(b, "%d.%d.%d.%d", UC(p[0]), UC(p[1]), UC(p[2]), UC(p[3])); 36012106Sralph return (b); 36112106Sralph } 36212106Sralph 36312106Sralph /*VARARGS1*/ 36412106Sralph log(msg, a1, a2, a3) 36512106Sralph char *msg; 36612106Sralph { 36712106Sralph short console = isatty(fileno(stderr)); 36812106Sralph 36912106Sralph fprintf(stderr, console ? "\r\n%s: " : "%s: ", name); 37012106Sralph if (printer) 37112106Sralph fprintf(stderr, "%s: ", printer); 37212106Sralph fprintf(stderr, msg, a1, a2, a3); 37312106Sralph if (console) 37412106Sralph putc('\r', stderr); 37512106Sralph putc('\n', stderr); 37612106Sralph fflush(stderr); 37712106Sralph } 37812106Sralph 37912106Sralph logerror(msg) 38012106Sralph char *msg; 38112106Sralph { 38212106Sralph register int err = errno; 38312106Sralph short console = isatty(fileno(stderr)); 38412106Sralph extern int sys_nerr; 38512106Sralph extern char *sys_errlist[]; 38612106Sralph 38712106Sralph fprintf(stderr, console ? "\r\n%s: " : "%s: ", name); 388*12430Sralph if (msg) 38912106Sralph fprintf(stderr, "%s: ", msg); 39012106Sralph fputs(err < sys_nerr ? sys_errlist[err] : "Unknown error" , stderr); 39112106Sralph if (console) 39212106Sralph putc('\r', stderr); 39312106Sralph putc('\n', stderr); 39412106Sralph fflush(stderr); 39512106Sralph } 396