122438Sdist /* 222438Sdist * Copyright (c) 1983 Regents of the University of California. 334203Sbostic * All rights reserved. 434203Sbostic * 542802Sbostic * %sccs.include.redist.c% 622438Sdist */ 722438Sdist 813955Ssam #ifndef lint 9*48850Sbostic static char sccsid[] = "@(#)recvjob.c 5.13 (Berkeley) 04/30/91"; 1034203Sbostic #endif /* not lint */ 1113955Ssam 1212115Sralph /* 1312115Sralph * Receive printer jobs from the network, queue them and 1412115Sralph * start the printer daemon. 1512115Sralph */ 1612115Sralph 1712115Sralph #include "lp.h" 1838493Skarels #include <ufs/fs.h> 1937968Sbostic #include "pathnames.h" 2012115Sralph 2116763Sralph char *sp = ""; 2216763Sralph #define ack() (void) write(1, sp, 1); 2312115Sralph 2416763Sralph char tfname[40]; /* tmp copy of cf before linking */ 2529385Smckusick char dfname[40]; /* data files */ 2616763Sralph int minfree; /* keep at least minfree blocks available */ 2716763Sralph char *ddev; /* disk device (for checking free space) */ 2816763Sralph int dfd; /* file system device descriptor */ 2916763Sralph 3016763Sralph char *find_dev(); 3146913Sbostic void rcleanup(); 3216763Sralph 3312115Sralph recvjob() 3412115Sralph { 3512464Sralph struct stat stb; 3612115Sralph char *bp = pbuf; 3746913Sbostic int status; 3812115Sralph 3912115Sralph /* 4012115Sralph * Perform lookup for printer name or abbreviation 4112115Sralph */ 4212115Sralph if ((status = pgetent(line, printer)) < 0) 4316763Sralph frecverr("cannot open printer description file"); 4412115Sralph else if (status == 0) 4516763Sralph frecverr("unknown printer %s", printer); 4612115Sralph if ((LF = pgetstr("lf", &bp)) == NULL) 4737968Sbostic LF = _PATH_CONSOLE; 4812115Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 4937968Sbostic SD = _PATH_DEFSPOOL; 5012464Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 5112464Sralph LO = DEFLOCK; 5212115Sralph 5325498Seric (void) close(2); /* set up log file */ 5425498Seric if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) { 5525498Seric syslog(LOG_ERR, "%s: %m", LF); 5637968Sbostic (void) open(_PATH_DEVNULL, O_WRONLY); 5725498Seric } 5825498Seric 5912115Sralph if (chdir(SD) < 0) 6016763Sralph frecverr("%s: %s: %m", printer, SD); 6116763Sralph if (stat(LO, &stb) == 0) { 6216763Sralph if (stb.st_mode & 010) { 6316763Sralph /* queue is disabled */ 6416763Sralph putchar('\1'); /* return error code */ 6516763Sralph exit(1); 6616763Sralph } 6716763Sralph } else if (stat(SD, &stb) < 0) 6816763Sralph frecverr("%s: %s: %m", printer, SD); 6916763Sralph minfree = read_number("minfree"); 7016763Sralph ddev = find_dev(stb.st_dev, S_IFBLK); 7116763Sralph if ((dfd = open(ddev, O_RDONLY)) < 0) 7216763Sralph syslog(LOG_WARNING, "%s: %s: %m", printer, ddev); 7316763Sralph signal(SIGTERM, rcleanup); 7416763Sralph signal(SIGPIPE, rcleanup); 7512115Sralph 7612115Sralph if (readjob()) 7712115Sralph printjob(); 7812115Sralph } 7912115Sralph 8016763Sralph char * 8116763Sralph find_dev(dev, type) 8216763Sralph register dev_t dev; 8316763Sralph register int type; 8416763Sralph { 8537968Sbostic register DIR *dfd = opendir(_PATH_DEV); 8616763Sralph struct direct *dir; 8716763Sralph struct stat stb; 8816763Sralph char devname[MAXNAMLEN+6]; 8916763Sralph char *dp; 9012115Sralph 9137968Sbostic strcpy(devname, _PATH_DEV); 9216763Sralph while ((dir = readdir(dfd))) { 9316763Sralph strcpy(devname + 5, dir->d_name); 9416763Sralph if (stat(devname, &stb)) 9516763Sralph continue; 9616763Sralph if ((stb.st_mode & S_IFMT) != type) 9716763Sralph continue; 9816763Sralph if (dev == stb.st_rdev) { 9916763Sralph closedir(dfd); 10016763Sralph dp = (char *)malloc(strlen(devname)+1); 10116763Sralph strcpy(dp, devname); 10216763Sralph return(dp); 10316763Sralph } 10416763Sralph } 10516763Sralph closedir(dfd); 10616763Sralph frecverr("cannot find device %d, %d", major(dev), minor(dev)); 10716763Sralph /*NOTREACHED*/ 10816763Sralph } 10916763Sralph 11012115Sralph /* 11112115Sralph * Read printer jobs sent by lpd and copy them to the spooling directory. 11212115Sralph * Return the number of jobs successfully transfered. 11312115Sralph */ 11416763Sralph readjob() 11512115Sralph { 11612115Sralph register int size, nfiles; 11712115Sralph register char *cp; 11812115Sralph 11912115Sralph ack(); 12012115Sralph nfiles = 0; 12112115Sralph for (;;) { 12212115Sralph /* 12312115Sralph * Read a command to tell us what to do 12412115Sralph */ 12512115Sralph cp = line; 12612115Sralph do { 12712115Sralph if ((size = read(1, cp, 1)) != 1) { 12812115Sralph if (size < 0) 12916763Sralph frecverr("%s: Lost connection",printer); 13012115Sralph return(nfiles); 13112115Sralph } 13212115Sralph } while (*cp++ != '\n'); 13312115Sralph *--cp = '\0'; 13412115Sralph cp = line; 13512115Sralph switch (*cp++) { 13612115Sralph case '\1': /* cleanup because data sent was bad */ 13716763Sralph rcleanup(); 13812115Sralph continue; 13912115Sralph 14012115Sralph case '\2': /* read cf file */ 14112115Sralph size = 0; 14212115Sralph while (*cp >= '0' && *cp <= '9') 14312115Sralph size = size * 10 + (*cp++ - '0'); 14412115Sralph if (*cp++ != ' ') 14512115Sralph break; 14627086Sbloom /* 14727086Sbloom * host name has been authenticated, we use our 14827086Sbloom * view of the host name since we may be passed 14927086Sbloom * something different than what gethostbyaddr() 15027086Sbloom * returns 15127086Sbloom */ 15227086Sbloom strcpy(cp + 6, from); 15312115Sralph strcpy(tfname, cp); 15412115Sralph tfname[0] = 't'; 15516763Sralph if (!chksize(size)) { 15616763Sralph (void) write(1, "\2", 1); 15716763Sralph continue; 15816763Sralph } 15912115Sralph if (!readfile(tfname, size)) { 16016763Sralph rcleanup(); 16112115Sralph continue; 16212115Sralph } 16312115Sralph if (link(tfname, cp) < 0) 16416763Sralph frecverr("%s: %m", tfname); 16512115Sralph (void) unlink(tfname); 16612115Sralph tfname[0] = '\0'; 16712115Sralph nfiles++; 16812115Sralph continue; 16912115Sralph 17012115Sralph case '\3': /* read df file */ 17112115Sralph size = 0; 17212115Sralph while (*cp >= '0' && *cp <= '9') 17312115Sralph size = size * 10 + (*cp++ - '0'); 17412115Sralph if (*cp++ != ' ') 17512115Sralph break; 17616763Sralph if (!chksize(size)) { 17716763Sralph (void) write(1, "\2", 1); 17816763Sralph continue; 17916763Sralph } 18039220Sbostic (void) strcpy(dfname, cp); 18139220Sbostic if (index(dfname, '/')) 18239220Sbostic frecverr("illegal path name"); 18329385Smckusick (void) readfile(dfname, size); 18412115Sralph continue; 18512115Sralph } 18616763Sralph frecverr("protocol screwup"); 18712115Sralph } 18812115Sralph } 18912115Sralph 19012115Sralph /* 19112115Sralph * Read files send by lpd and copy them to the spooling directory. 19212115Sralph */ 19312115Sralph readfile(file, size) 19412115Sralph char *file; 19512115Sralph int size; 19612115Sralph { 19712115Sralph register char *cp; 19812115Sralph char buf[BUFSIZ]; 19912115Sralph register int i, j, amt; 20012115Sralph int fd, err; 20112115Sralph 202*48850Sbostic fd = open(file, O_CREAT|O_EXECL|O_WRONLY, FILMOD); 203*48850Sbostic frecverr("illegal path name"); 20412115Sralph if (fd < 0) 20516763Sralph frecverr("%s: %m", file); 20612115Sralph ack(); 20712115Sralph err = 0; 20812115Sralph for (i = 0; i < size; i += BUFSIZ) { 20912115Sralph amt = BUFSIZ; 21012115Sralph cp = buf; 21112115Sralph if (i + amt > size) 21212115Sralph amt = size - i; 21312115Sralph do { 21412115Sralph j = read(1, cp, amt); 21512115Sralph if (j <= 0) 21616763Sralph frecverr("Lost connection"); 21712115Sralph amt -= j; 21812115Sralph cp += j; 21912115Sralph } while (amt > 0); 22012115Sralph amt = BUFSIZ; 22112115Sralph if (i + amt > size) 22212115Sralph amt = size - i; 22313170Sralph if (write(fd, buf, amt) != amt) { 22412115Sralph err++; 22513170Sralph break; 22613170Sralph } 22712115Sralph } 22812115Sralph (void) close(fd); 22912115Sralph if (err) 23016763Sralph frecverr("%s: write error", file); 23112115Sralph if (noresponse()) { /* file sent had bad data in it */ 23212115Sralph (void) unlink(file); 23312115Sralph return(0); 23412115Sralph } 23512115Sralph ack(); 23612115Sralph return(1); 23712115Sralph } 23812115Sralph 23912115Sralph noresponse() 24012115Sralph { 24112115Sralph char resp; 24212115Sralph 24312115Sralph if (read(1, &resp, 1) != 1) 24416763Sralph frecverr("Lost connection"); 24512115Sralph if (resp == '\0') 24612115Sralph return(0); 24712115Sralph return(1); 24812115Sralph } 24912115Sralph 25012115Sralph /* 25116763Sralph * Check to see if there is enough space on the disk for size bytes. 25216763Sralph * 1 == OK, 0 == Not OK. 25316763Sralph */ 25416763Sralph chksize(size) 25516763Sralph int size; 25616763Sralph { 25716763Sralph int spacefree; 25816763Sralph struct fs fs; 25916763Sralph 26030656Smckusick if (dfd < 0 || lseek(dfd, (long)(SBOFF), 0) < 0) 26116763Sralph return(1); 26216763Sralph if (read(dfd, (char *)&fs, sizeof fs) != sizeof fs) 26316763Sralph return(1); 26430656Smckusick spacefree = freespace(&fs, fs.fs_minfree) * fs.fs_fsize / 1024; 26516763Sralph size = (size + 1023) / 1024; 26616763Sralph if (minfree + size > spacefree) 26716763Sralph return(0); 26816763Sralph return(1); 26916763Sralph } 27016763Sralph 27116763Sralph read_number(fn) 27216763Sralph char *fn; 27316763Sralph { 27416763Sralph char lin[80]; 27516763Sralph register FILE *fp; 27616763Sralph 27716763Sralph if ((fp = fopen(fn, "r")) == NULL) 27816763Sralph return (0); 27916763Sralph if (fgets(lin, 80, fp) == NULL) { 28016763Sralph fclose(fp); 28116763Sralph return (0); 28216763Sralph } 28316763Sralph fclose(fp); 28416763Sralph return (atoi(lin)); 28516763Sralph } 28616763Sralph 28716763Sralph /* 28812115Sralph * Remove all the files associated with the current job being transfered. 28912115Sralph */ 29046913Sbostic void 29116763Sralph rcleanup() 29212115Sralph { 29312115Sralph 29412115Sralph if (tfname[0]) 29512115Sralph (void) unlink(tfname); 29629385Smckusick if (dfname[0]) 29712115Sralph do { 29812115Sralph do 29912115Sralph (void) unlink(dfname); 30016938Sralph while (dfname[2]-- != 'A'); 30116938Sralph dfname[2] = 'z'; 30216938Sralph } while (dfname[0]-- != 'd'); 30329385Smckusick dfname[0] = '\0'; 30412115Sralph } 30512115Sralph 30616763Sralph frecverr(msg, a1, a2) 30712115Sralph char *msg; 30812115Sralph { 30916763Sralph rcleanup(); 31016763Sralph syslog(LOG_ERR, msg, a1, a2); 31112115Sralph putchar('\1'); /* return error code */ 31212115Sralph exit(1); 31312115Sralph } 314