recvjob.c revision 80133
11553Srgrimes/* 21553Srgrimes * Copyright (c) 1983, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * 61553Srgrimes * Redistribution and use in source and binary forms, with or without 71553Srgrimes * modification, are permitted provided that the following conditions 81553Srgrimes * are met: 91553Srgrimes * 1. Redistributions of source code must retain the above copyright 101553Srgrimes * notice, this list of conditions and the following disclaimer. 111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121553Srgrimes * notice, this list of conditions and the following disclaimer in the 131553Srgrimes * documentation and/or other materials provided with the distribution. 141553Srgrimes * 3. All advertising materials mentioning features or use of this software 151553Srgrimes * must display the following acknowledgement: 161553Srgrimes * This product includes software developed by the University of 171553Srgrimes * California, Berkeley and its contributors. 181553Srgrimes * 4. Neither the name of the University nor the names of its contributors 191553Srgrimes * may be used to endorse or promote products derived from this software 201553Srgrimes * without specific prior written permission. 211553Srgrimes * 221553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321553Srgrimes * SUCH DAMAGE. 331553Srgrimes */ 341553Srgrimes 351553Srgrimes#ifndef lint 3629780Scharnierstatic const char copyright[] = 371553Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 381553Srgrimes The Regents of the University of California. All rights reserved.\n"; 391553Srgrimes#endif /* not lint */ 401553Srgrimes 411553Srgrimes#ifndef lint 4229780Scharnier#if 0 4315648Sjoergstatic char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95"; 4429780Scharnier#endif 4529780Scharnierstatic const char rcsid[] = 4650479Speter "$FreeBSD: head/usr.sbin/lpr/lpd/recvjob.c 80133 2001-07-22 07:25:27Z gad $"; 471553Srgrimes#endif /* not lint */ 481553Srgrimes 491553Srgrimes/* 501553Srgrimes * Receive printer jobs from the network, queue them and 511553Srgrimes * start the printer daemon. 521553Srgrimes */ 531553Srgrimes#include <sys/param.h> 541553Srgrimes#include <sys/mount.h> 551553Srgrimes#include <sys/stat.h> 561553Srgrimes 571553Srgrimes#include <unistd.h> 581553Srgrimes#include <signal.h> 591553Srgrimes#include <fcntl.h> 601553Srgrimes#include <dirent.h> 6179739Sgad#include <errno.h> 621553Srgrimes#include <syslog.h> 631553Srgrimes#include <stdio.h> 641553Srgrimes#include <stdlib.h> 651553Srgrimes#include <string.h> 661553Srgrimes#include "lp.h" 671553Srgrimes#include "lp.local.h" 6879746Sgad#include "ctlinfo.h" 691553Srgrimes#include "extern.h" 701553Srgrimes#include "pathnames.h" 711553Srgrimes 7280113Sgad#define ack() (void) write(STDOUT_FILENO, sp, 1); 731553Srgrimes 7427748Simpstatic char dfname[NAME_MAX]; /* data files */ 751553Srgrimesstatic int minfree; /* keep at least minfree blocks available */ 7678146Sgadstatic const char *sp = ""; 7727748Simpstatic char tfname[NAME_MAX]; /* tmp copy of cf before linking */ 781553Srgrimes 7978146Sgadstatic int chksize(int _size); 8079739Sgadstatic void frecverr(const char *_msg, ...) __printf0like(1, 2); 8178146Sgadstatic int noresponse(void); 8278146Sgadstatic void rcleanup(int _signo); 8378146Sgadstatic int read_number(const char *_fn); 8478146Sgadstatic int readfile(struct printer *_pp, char *_file, int _size); 8578146Sgadstatic int readjob(struct printer *_pp); 861553Srgrimes 871553Srgrimes 881553Srgrimesvoid 8978146Sgadrecvjob(const char *printer) 901553Srgrimes{ 911553Srgrimes struct stat stb; 921553Srgrimes int status; 9331492Swollman struct printer myprinter, *pp = &myprinter; 941553Srgrimes 951553Srgrimes /* 961553Srgrimes * Perform lookup for printer name or abbreviation 971553Srgrimes */ 9832654Swollman init_printer(pp); 9931492Swollman status = getprintcap(printer, pp); 10031492Swollman switch (status) { 10131492Swollman case PCAPERR_OSERR: 1021553Srgrimes frecverr("cannot open printer description file"); 10331492Swollman break; 10431492Swollman case PCAPERR_NOTFOUND: 1051553Srgrimes frecverr("unknown printer %s", printer); 10631492Swollman break; 10731492Swollman case PCAPERR_TCLOOP: 10831492Swollman fatal(pp, "potential reference loop detected in printcap file"); 10931492Swollman default: 11031492Swollman break; 11131492Swollman } 11227748Simp 1131553Srgrimes (void) close(2); /* set up log file */ 11431492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) { 11531492Swollman syslog(LOG_ERR, "%s: %m", pp->log_file); 1161553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1171553Srgrimes } 1181553Srgrimes 11931492Swollman if (chdir(pp->spool_dir) < 0) 12079739Sgad frecverr("%s: chdir(%s): %s", pp->printer, pp->spool_dir, 12179739Sgad strerror(errno)); 12231492Swollman if (stat(pp->lock_file, &stb) == 0) { 1231553Srgrimes if (stb.st_mode & 010) { 1241553Srgrimes /* queue is disabled */ 1251553Srgrimes putchar('\1'); /* return error code */ 1261553Srgrimes exit(1); 1271553Srgrimes } 12831492Swollman } else if (stat(pp->spool_dir, &stb) < 0) 12979739Sgad frecverr("%s: stat(%s): %s", pp->printer, pp->spool_dir, 13079739Sgad strerror(errno)); 1311553Srgrimes minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */ 1321553Srgrimes signal(SIGTERM, rcleanup); 1331553Srgrimes signal(SIGPIPE, rcleanup); 1341553Srgrimes 13531492Swollman if (readjob(pp)) 13631492Swollman printjob(pp); 1371553Srgrimes} 1381553Srgrimes 1391553Srgrimes/* 1401553Srgrimes * Read printer jobs sent by lpd and copy them to the spooling directory. 1411553Srgrimes * Return the number of jobs successfully transfered. 1421553Srgrimes */ 1431553Srgrimesstatic int 14478146Sgadreadjob(struct printer *pp) 1451553Srgrimes{ 14668253Sgad register int size; 1471553Srgrimes register char *cp; 14868735Sgad int cfcnt, dfcnt; 14979746Sgad char *errmsg; 15068735Sgad char givenid[32], givenhost[MAXHOSTNAMELEN]; 1511553Srgrimes 1521553Srgrimes ack(); 15368253Sgad cfcnt = 0; 15468253Sgad dfcnt = 0; 1551553Srgrimes for (;;) { 1561553Srgrimes /* 1571553Srgrimes * Read a command to tell us what to do 1581553Srgrimes */ 1591553Srgrimes cp = line; 1601553Srgrimes do { 16180113Sgad if ((size = read(STDOUT_FILENO, cp, 1)) != 1) { 16268380Sgad if (size < 0) { 16329780Scharnier frecverr("%s: lost connection", 16431492Swollman pp->printer); 16568380Sgad /*NOTREACHED*/ 16668380Sgad } 16768253Sgad return (cfcnt); 1681553Srgrimes } 16927748Simp } while (*cp++ != '\n' && (cp - line + 1) < sizeof(line)); 17068380Sgad if (cp - line + 1 >= sizeof(line)) { 17168380Sgad frecverr("%s: readjob overflow", pp->printer); 17268380Sgad /*NOTREACHED*/ 17368380Sgad } 1741553Srgrimes *--cp = '\0'; 1751553Srgrimes cp = line; 1761553Srgrimes switch (*cp++) { 1771553Srgrimes case '\1': /* cleanup because data sent was bad */ 1781553Srgrimes rcleanup(0); 1791553Srgrimes continue; 1801553Srgrimes 1811553Srgrimes case '\2': /* read cf file */ 1821553Srgrimes size = 0; 18368253Sgad dfcnt = 0; 1841553Srgrimes while (*cp >= '0' && *cp <= '9') 1851553Srgrimes size = size * 10 + (*cp++ - '0'); 1861553Srgrimes if (*cp++ != ' ') 1871553Srgrimes break; 1881553Srgrimes /* 1891553Srgrimes * host name has been authenticated, we use our 1901553Srgrimes * view of the host name since we may be passed 1911553Srgrimes * something different than what gethostbyaddr() 1921553Srgrimes * returns 1931553Srgrimes */ 19480133Sgad strlcpy(cp + 6, from_host, sizeof(line) 19580133Sgad + (size_t)(line - cp - 6)); 19680133Sgad strlcpy(tfname, cp, sizeof(tfname)); 19727748Simp tfname[sizeof (tfname) - 1] = '\0'; 1981553Srgrimes tfname[0] = 't'; 19927748Simp if (strchr(tfname, '/')) 20027748Simp frecverr("readjob: %s: illegal path name", 20127748Simp tfname); 2021553Srgrimes if (!chksize(size)) { 20380113Sgad (void) write(STDOUT_FILENO, "\2", 1); 2041553Srgrimes continue; 2051553Srgrimes } 20668380Sgad if (!readfile(pp, tfname, size)) { 2071553Srgrimes rcleanup(0); 2081553Srgrimes continue; 2091553Srgrimes } 21079746Sgad errmsg = ctl_renametf(pp->printer, tfname); 2111553Srgrimes tfname[0] = '\0'; 21279746Sgad if (errmsg != NULL) { 21379746Sgad frecverr("%s: %s", pp->printer, errmsg); 21479746Sgad /*NOTREACHED*/ 21579746Sgad } 21668253Sgad cfcnt++; 2171553Srgrimes continue; 2181553Srgrimes 2191553Srgrimes case '\3': /* read df file */ 22068253Sgad *givenid = '\0'; 22168253Sgad *givenhost = '\0'; 2221553Srgrimes size = 0; 2231553Srgrimes while (*cp >= '0' && *cp <= '9') 2241553Srgrimes size = size * 10 + (*cp++ - '0'); 2251553Srgrimes if (*cp++ != ' ') 2261553Srgrimes break; 2271553Srgrimes if (!chksize(size)) { 22880113Sgad (void) write(STDOUT_FILENO, "\2", 1); 2291553Srgrimes continue; 2301553Srgrimes } 23180133Sgad strlcpy(dfname, cp, sizeof(dfname)); 23268380Sgad if (strchr(dfname, '/')) { 2331553Srgrimes frecverr("readjob: %s: illegal path name", 2341553Srgrimes dfname); 23568380Sgad /*NOTREACHED*/ 23668380Sgad } 23768253Sgad dfcnt++; 23868253Sgad trstat_init(pp, dfname, dfcnt); 23968380Sgad (void) readfile(pp, dfname, size); 24078300Sgad trstat_write(pp, TR_RECVING, size, givenid, from_host, 24168253Sgad givenhost); 2421553Srgrimes continue; 2431553Srgrimes } 2441553Srgrimes frecverr("protocol screwup: %s", line); 24568380Sgad /*NOTREACHED*/ 2461553Srgrimes } 2471553Srgrimes} 2481553Srgrimes 2491553Srgrimes/* 2501553Srgrimes * Read files send by lpd and copy them to the spooling directory. 2511553Srgrimes */ 2521553Srgrimesstatic int 25378146Sgadreadfile(struct printer *pp, char *file, int size) 2541553Srgrimes{ 2551553Srgrimes register char *cp; 2561553Srgrimes char buf[BUFSIZ]; 2571553Srgrimes register int i, j, amt; 2581553Srgrimes int fd, err; 2591553Srgrimes 2601553Srgrimes fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD); 26168380Sgad if (fd < 0) { 26279739Sgad frecverr("%s: readfile: error on open(%s): %s", 26379739Sgad pp->printer, file, strerror(errno)); 26468380Sgad /*NOTREACHED*/ 26568380Sgad } 2661553Srgrimes ack(); 2671553Srgrimes err = 0; 2681553Srgrimes for (i = 0; i < size; i += BUFSIZ) { 2691553Srgrimes amt = BUFSIZ; 2701553Srgrimes cp = buf; 2711553Srgrimes if (i + amt > size) 2721553Srgrimes amt = size - i; 2731553Srgrimes do { 27480113Sgad j = read(STDOUT_FILENO, cp, amt); 27568380Sgad if (j <= 0) { 27668380Sgad frecverr("%s: lost connection", pp->printer); 27768380Sgad /*NOTREACHED*/ 27868380Sgad } 2791553Srgrimes amt -= j; 2801553Srgrimes cp += j; 2811553Srgrimes } while (amt > 0); 2821553Srgrimes amt = BUFSIZ; 2831553Srgrimes if (i + amt > size) 2841553Srgrimes amt = size - i; 2851553Srgrimes if (write(fd, buf, amt) != amt) { 2861553Srgrimes err++; 2871553Srgrimes break; 2881553Srgrimes } 2891553Srgrimes } 2901553Srgrimes (void) close(fd); 29168380Sgad if (err) { 29268380Sgad frecverr("%s: write error on close(%s)", pp->printer, file); 29368380Sgad /*NOTREACHED*/ 29468380Sgad } 2951553Srgrimes if (noresponse()) { /* file sent had bad data in it */ 29630143Simp if (strchr(file, '/') == NULL) 29730143Simp (void) unlink(file); 29868380Sgad return (0); 2991553Srgrimes } 3001553Srgrimes ack(); 30168380Sgad return (1); 3021553Srgrimes} 3031553Srgrimes 3041553Srgrimesstatic int 30578146Sgadnoresponse(void) 3061553Srgrimes{ 3071553Srgrimes char resp; 3081553Srgrimes 30980113Sgad if (read(STDOUT_FILENO, &resp, 1) != 1) { 31068380Sgad frecverr("lost connection in noresponse()"); 31168380Sgad /*NOTREACHED*/ 31268380Sgad } 3131553Srgrimes if (resp == '\0') 3141553Srgrimes return(0); 3151553Srgrimes return(1); 3161553Srgrimes} 3171553Srgrimes 3181553Srgrimes/* 3191553Srgrimes * Check to see if there is enough space on the disk for size bytes. 3201553Srgrimes * 1 == OK, 0 == Not OK. 3211553Srgrimes */ 3221553Srgrimesstatic int 32378146Sgadchksize(int size) 3241553Srgrimes{ 3251553Srgrimes int spacefree; 3261553Srgrimes struct statfs sfb; 3271553Srgrimes 3281553Srgrimes if (statfs(".", &sfb) < 0) { 3291553Srgrimes syslog(LOG_ERR, "%s: %m", "statfs(\".\")"); 3301553Srgrimes return (1); 3311553Srgrimes } 3321553Srgrimes spacefree = sfb.f_bavail * (sfb.f_bsize / 512); 3331553Srgrimes size = (size + 511) / 512; 3341553Srgrimes if (minfree + size > spacefree) 3351553Srgrimes return(0); 3361553Srgrimes return(1); 3371553Srgrimes} 3381553Srgrimes 3391553Srgrimesstatic int 34078146Sgadread_number(const char *fn) 3411553Srgrimes{ 3421553Srgrimes char lin[80]; 3431553Srgrimes register FILE *fp; 3441553Srgrimes 3451553Srgrimes if ((fp = fopen(fn, "r")) == NULL) 3461553Srgrimes return (0); 3471553Srgrimes if (fgets(lin, 80, fp) == NULL) { 3481553Srgrimes fclose(fp); 3491553Srgrimes return (0); 3501553Srgrimes } 3511553Srgrimes fclose(fp); 3521553Srgrimes return (atoi(lin)); 3531553Srgrimes} 3541553Srgrimes 3551553Srgrimes/* 3561553Srgrimes * Remove all the files associated with the current job being transfered. 3571553Srgrimes */ 3581553Srgrimesstatic void 35978146Sgadrcleanup(int signo __unused) 3601553Srgrimes{ 36130143Simp if (tfname[0] && strchr(tfname, '/') == NULL) 3621553Srgrimes (void) unlink(tfname); 36330143Simp if (dfname[0] && strchr(dfname, '/') == NULL) { 3641553Srgrimes do { 3651553Srgrimes do 3661553Srgrimes (void) unlink(dfname); 3671553Srgrimes while (dfname[2]-- != 'A'); 3681553Srgrimes dfname[2] = 'z'; 3691553Srgrimes } while (dfname[0]-- != 'd'); 37030143Simp } 3711553Srgrimes dfname[0] = '\0'; 3721553Srgrimes} 3731553Srgrimes 37427757Simp#ifdef __STDC__ 3751553Srgrimes#include <stdarg.h> 3761553Srgrimes#else 3771553Srgrimes#include <varargs.h> 3781553Srgrimes#endif 3791553Srgrimes 3801553Srgrimesstatic void 38127757Simp#ifdef __STDC__ 3821553Srgrimesfrecverr(const char *msg, ...) 3831553Srgrimes#else 3841553Srgrimesfrecverr(msg, va_alist) 3851553Srgrimes char *msg; 3861553Srgrimes va_dcl 3871553Srgrimes#endif 3881553Srgrimes{ 3891553Srgrimes va_list ap; 39027757Simp#ifdef __STDC__ 3911553Srgrimes va_start(ap, msg); 3921553Srgrimes#else 3931553Srgrimes va_start(ap); 3941553Srgrimes#endif 39578300Sgad syslog(LOG_ERR, "Error receiving job from %s:", from_host); 3961553Srgrimes vsyslog(LOG_ERR, msg, ap); 3971553Srgrimes va_end(ap); 39868340Sgad /* 39968735Sgad * rcleanup is not called until AFTER logging the error message, 40068735Sgad * because rcleanup will zap some variables which may have been 40168735Sgad * supplied as parameters for that msg... 40268340Sgad */ 40368340Sgad rcleanup(0); 40468340Sgad /* 40568340Sgad * Add a minimal delay before returning the final error code to 40668340Sgad * the sending host. This just in case that machine responds 40768340Sgad * this error by INSTANTLY retrying (and instantly re-failing...). 40868340Sgad * It would be stupid of the sending host to do that, but if there 40968340Sgad * was a broken implementation which did it, the result might be 41068340Sgad * obscure performance problems and a flood of syslog messages on 41168340Sgad * the receiving host. 41268340Sgad */ 41368340Sgad sleep(2); /* a paranoid throttling measure */ 4141553Srgrimes putchar('\1'); /* return error code */ 4151553Srgrimes exit(1); 4161553Srgrimes} 417