recvjob.c revision 117587
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 4129780Scharnier#if 0 42117587Sgad#ifndef lint 4315648Sjoergstatic char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95"; 44117587Sgad#endif /* not lint */ 4529780Scharnier#endif 461553Srgrimes 47117554Sgad#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 48117554Sgad__FBSDID("$FreeBSD: head/usr.sbin/lpr/lpd/recvjob.c 117587 2003-07-14 19:59:33Z gad $"); 49117554Sgad 501553Srgrimes/* 511553Srgrimes * Receive printer jobs from the network, queue them and 521553Srgrimes * start the printer daemon. 531553Srgrimes */ 541553Srgrimes#include <sys/param.h> 551553Srgrimes#include <sys/mount.h> 561553Srgrimes#include <sys/stat.h> 571553Srgrimes 581553Srgrimes#include <unistd.h> 591553Srgrimes#include <signal.h> 601553Srgrimes#include <fcntl.h> 611553Srgrimes#include <dirent.h> 6279739Sgad#include <errno.h> 631553Srgrimes#include <syslog.h> 641553Srgrimes#include <stdio.h> 651553Srgrimes#include <stdlib.h> 661553Srgrimes#include <string.h> 671553Srgrimes#include "lp.h" 681553Srgrimes#include "lp.local.h" 6979746Sgad#include "ctlinfo.h" 701553Srgrimes#include "extern.h" 711553Srgrimes#include "pathnames.h" 721553Srgrimes 7380171Sgad#define ack() (void) write(STDOUT_FILENO, sp, (size_t)1); 741553Srgrimes 7527748Simpstatic char dfname[NAME_MAX]; /* data files */ 761553Srgrimesstatic int minfree; /* keep at least minfree blocks available */ 7778146Sgadstatic const char *sp = ""; 7827748Simpstatic char tfname[NAME_MAX]; /* tmp copy of cf before linking */ 791553Srgrimes 8078146Sgadstatic int chksize(int _size); 8179739Sgadstatic void frecverr(const char *_msg, ...) __printf0like(1, 2); 8278146Sgadstatic int noresponse(void); 8378146Sgadstatic void rcleanup(int _signo); 8478146Sgadstatic int read_number(const char *_fn); 8580171Sgadstatic int readfile(struct printer *_pp, char *_file, size_t _size); 8678146Sgadstatic int readjob(struct printer *_pp); 871553Srgrimes 881553Srgrimes 891553Srgrimesvoid 9078146Sgadrecvjob(const char *printer) 911553Srgrimes{ 921553Srgrimes struct stat stb; 931553Srgrimes int status; 9431492Swollman struct printer myprinter, *pp = &myprinter; 951553Srgrimes 961553Srgrimes /* 971553Srgrimes * Perform lookup for printer name or abbreviation 981553Srgrimes */ 9932654Swollman init_printer(pp); 10031492Swollman status = getprintcap(printer, pp); 10131492Swollman switch (status) { 10231492Swollman case PCAPERR_OSERR: 1031553Srgrimes frecverr("cannot open printer description file"); 10431492Swollman break; 10531492Swollman case PCAPERR_NOTFOUND: 1061553Srgrimes frecverr("unknown printer %s", printer); 10731492Swollman break; 10831492Swollman case PCAPERR_TCLOOP: 10931492Swollman fatal(pp, "potential reference loop detected in printcap file"); 11031492Swollman default: 11131492Swollman break; 11231492Swollman } 11327748Simp 1141553Srgrimes (void) close(2); /* set up log file */ 11531492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) { 11631492Swollman syslog(LOG_ERR, "%s: %m", pp->log_file); 1171553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1181553Srgrimes } 1191553Srgrimes 12031492Swollman if (chdir(pp->spool_dir) < 0) 12179739Sgad frecverr("%s: chdir(%s): %s", pp->printer, pp->spool_dir, 12279739Sgad strerror(errno)); 12331492Swollman if (stat(pp->lock_file, &stb) == 0) { 1241553Srgrimes if (stb.st_mode & 010) { 1251553Srgrimes /* queue is disabled */ 1261553Srgrimes putchar('\1'); /* return error code */ 1271553Srgrimes exit(1); 1281553Srgrimes } 12931492Swollman } else if (stat(pp->spool_dir, &stb) < 0) 13079739Sgad frecverr("%s: stat(%s): %s", pp->printer, pp->spool_dir, 13179739Sgad strerror(errno)); 1321553Srgrimes minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */ 1331553Srgrimes signal(SIGTERM, rcleanup); 1341553Srgrimes signal(SIGPIPE, rcleanup); 1351553Srgrimes 13631492Swollman if (readjob(pp)) 13731492Swollman printjob(pp); 1381553Srgrimes} 1391553Srgrimes 1401553Srgrimes/* 1411553Srgrimes * Read printer jobs sent by lpd and copy them to the spooling directory. 1421553Srgrimes * Return the number of jobs successfully transfered. 1431553Srgrimes */ 1441553Srgrimesstatic int 14578146Sgadreadjob(struct printer *pp) 1461553Srgrimes{ 14768253Sgad register int size; 14868735Sgad int cfcnt, dfcnt; 14980171Sgad char *cp, *clastp, *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; 16080171Sgad clastp = line + sizeof(line) - 1; 1611553Srgrimes do { 16280171Sgad size = read(STDOUT_FILENO, cp, (size_t)1); 16380171Sgad if (size != (ssize_t)1) { 16480171Sgad if (size < (ssize_t)0) { 16529780Scharnier frecverr("%s: lost connection", 16631492Swollman pp->printer); 16768380Sgad /*NOTREACHED*/ 16868380Sgad } 16968253Sgad return (cfcnt); 1701553Srgrimes } 17180171Sgad } while ((*cp++ != '\n') && (cp <= clastp)); 17280171Sgad if (cp > clastp) { 17368380Sgad frecverr("%s: readjob overflow", pp->printer); 17468380Sgad /*NOTREACHED*/ 17568380Sgad } 1761553Srgrimes *--cp = '\0'; 1771553Srgrimes cp = line; 1781553Srgrimes switch (*cp++) { 1791553Srgrimes case '\1': /* cleanup because data sent was bad */ 1801553Srgrimes rcleanup(0); 1811553Srgrimes continue; 1821553Srgrimes 1831553Srgrimes case '\2': /* read cf file */ 1841553Srgrimes size = 0; 18568253Sgad dfcnt = 0; 1861553Srgrimes while (*cp >= '0' && *cp <= '9') 1871553Srgrimes size = size * 10 + (*cp++ - '0'); 1881553Srgrimes if (*cp++ != ' ') 1891553Srgrimes break; 1901553Srgrimes /* 1911553Srgrimes * host name has been authenticated, we use our 1921553Srgrimes * view of the host name since we may be passed 1931553Srgrimes * something different than what gethostbyaddr() 1941553Srgrimes * returns 1951553Srgrimes */ 19680133Sgad strlcpy(cp + 6, from_host, sizeof(line) 19780133Sgad + (size_t)(line - cp - 6)); 19887375Sgad if (strchr(cp, '/')) { 19987375Sgad frecverr("readjob: %s: illegal path name", cp); 20087375Sgad /*NOTREACHED*/ 20187375Sgad } 20280133Sgad strlcpy(tfname, cp, sizeof(tfname)); 20327748Simp tfname[sizeof (tfname) - 1] = '\0'; 2041553Srgrimes tfname[0] = 't'; 2051553Srgrimes if (!chksize(size)) { 20680171Sgad (void) write(STDOUT_FILENO, "\2", (size_t)1); 2071553Srgrimes continue; 2081553Srgrimes } 20980171Sgad if (!readfile(pp, tfname, (size_t)size)) { 2101553Srgrimes rcleanup(0); 2111553Srgrimes continue; 2121553Srgrimes } 21379746Sgad errmsg = ctl_renametf(pp->printer, tfname); 2141553Srgrimes tfname[0] = '\0'; 21579746Sgad if (errmsg != NULL) { 21679746Sgad frecverr("%s: %s", pp->printer, errmsg); 21779746Sgad /*NOTREACHED*/ 21879746Sgad } 21968253Sgad cfcnt++; 2201553Srgrimes continue; 2211553Srgrimes 2221553Srgrimes case '\3': /* read df file */ 22368253Sgad *givenid = '\0'; 22468253Sgad *givenhost = '\0'; 2251553Srgrimes size = 0; 2261553Srgrimes while (*cp >= '0' && *cp <= '9') 2271553Srgrimes size = size * 10 + (*cp++ - '0'); 2281553Srgrimes if (*cp++ != ' ') 2291553Srgrimes break; 23087375Sgad if (strchr(cp, '/')) { 23187375Sgad frecverr("readjob: %s: illegal path name", cp); 23287375Sgad /*NOTREACHED*/ 23387375Sgad } 2341553Srgrimes if (!chksize(size)) { 23580171Sgad (void) write(STDOUT_FILENO, "\2", (size_t)1); 2361553Srgrimes continue; 2371553Srgrimes } 23880133Sgad strlcpy(dfname, cp, sizeof(dfname)); 23968253Sgad dfcnt++; 24068253Sgad trstat_init(pp, dfname, dfcnt); 24180171Sgad (void) readfile(pp, dfname, (size_t)size); 24280171Sgad trstat_write(pp, TR_RECVING, (size_t)size, givenid, 24380171Sgad from_host, givenhost); 2441553Srgrimes continue; 2451553Srgrimes } 2461553Srgrimes frecverr("protocol screwup: %s", line); 24768380Sgad /*NOTREACHED*/ 2481553Srgrimes } 2491553Srgrimes} 2501553Srgrimes 2511553Srgrimes/* 2521553Srgrimes * Read files send by lpd and copy them to the spooling directory. 2531553Srgrimes */ 2541553Srgrimesstatic int 25580171Sgadreadfile(struct printer *pp, char *file, size_t size) 2561553Srgrimes{ 2571553Srgrimes register char *cp; 2581553Srgrimes char buf[BUFSIZ]; 25980171Sgad size_t amt, i; 26080171Sgad int err, fd, j; 2611553Srgrimes 2621553Srgrimes fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD); 26368380Sgad if (fd < 0) { 26479739Sgad frecverr("%s: readfile: error on open(%s): %s", 26579739Sgad pp->printer, file, strerror(errno)); 26668380Sgad /*NOTREACHED*/ 26768380Sgad } 2681553Srgrimes ack(); 2691553Srgrimes err = 0; 2701553Srgrimes for (i = 0; i < size; i += BUFSIZ) { 2711553Srgrimes amt = BUFSIZ; 2721553Srgrimes cp = buf; 2731553Srgrimes if (i + amt > size) 2741553Srgrimes amt = size - i; 2751553Srgrimes do { 27680113Sgad j = read(STDOUT_FILENO, cp, amt); 27768380Sgad if (j <= 0) { 27868380Sgad frecverr("%s: lost connection", pp->printer); 27968380Sgad /*NOTREACHED*/ 28068380Sgad } 2811553Srgrimes amt -= j; 2821553Srgrimes cp += j; 2831553Srgrimes } while (amt > 0); 2841553Srgrimes amt = BUFSIZ; 2851553Srgrimes if (i + amt > size) 2861553Srgrimes amt = size - i; 28780171Sgad if (write(fd, buf, amt) != (ssize_t)amt) { 2881553Srgrimes err++; 2891553Srgrimes break; 2901553Srgrimes } 2911553Srgrimes } 2921553Srgrimes (void) close(fd); 29368380Sgad if (err) { 29468380Sgad frecverr("%s: write error on close(%s)", pp->printer, file); 29568380Sgad /*NOTREACHED*/ 29668380Sgad } 2971553Srgrimes if (noresponse()) { /* file sent had bad data in it */ 29830143Simp if (strchr(file, '/') == NULL) 29930143Simp (void) unlink(file); 30068380Sgad return (0); 3011553Srgrimes } 3021553Srgrimes ack(); 30368380Sgad return (1); 3041553Srgrimes} 3051553Srgrimes 3061553Srgrimesstatic int 30778146Sgadnoresponse(void) 3081553Srgrimes{ 3091553Srgrimes char resp; 3101553Srgrimes 31180171Sgad if (read(STDOUT_FILENO, &resp, (size_t)1) != 1) { 31268380Sgad frecverr("lost connection in noresponse()"); 31368380Sgad /*NOTREACHED*/ 31468380Sgad } 3151553Srgrimes if (resp == '\0') 3161553Srgrimes return(0); 3171553Srgrimes return(1); 3181553Srgrimes} 3191553Srgrimes 3201553Srgrimes/* 3211553Srgrimes * Check to see if there is enough space on the disk for size bytes. 3221553Srgrimes * 1 == OK, 0 == Not OK. 3231553Srgrimes */ 3241553Srgrimesstatic int 32578146Sgadchksize(int size) 3261553Srgrimes{ 3271553Srgrimes int spacefree; 3281553Srgrimes struct statfs sfb; 3291553Srgrimes 3301553Srgrimes if (statfs(".", &sfb) < 0) { 3311553Srgrimes syslog(LOG_ERR, "%s: %m", "statfs(\".\")"); 3321553Srgrimes return (1); 3331553Srgrimes } 3341553Srgrimes spacefree = sfb.f_bavail * (sfb.f_bsize / 512); 3351553Srgrimes size = (size + 511) / 512; 3361553Srgrimes if (minfree + size > spacefree) 3371553Srgrimes return(0); 3381553Srgrimes return(1); 3391553Srgrimes} 3401553Srgrimes 3411553Srgrimesstatic int 34278146Sgadread_number(const char *fn) 3431553Srgrimes{ 3441553Srgrimes char lin[80]; 3451553Srgrimes register FILE *fp; 3461553Srgrimes 3471553Srgrimes if ((fp = fopen(fn, "r")) == NULL) 3481553Srgrimes return (0); 3491553Srgrimes if (fgets(lin, 80, fp) == NULL) { 3501553Srgrimes fclose(fp); 3511553Srgrimes return (0); 3521553Srgrimes } 3531553Srgrimes fclose(fp); 3541553Srgrimes return (atoi(lin)); 3551553Srgrimes} 3561553Srgrimes 3571553Srgrimes/* 3581553Srgrimes * Remove all the files associated with the current job being transfered. 3591553Srgrimes */ 3601553Srgrimesstatic void 36178146Sgadrcleanup(int signo __unused) 3621553Srgrimes{ 36330143Simp if (tfname[0] && strchr(tfname, '/') == NULL) 3641553Srgrimes (void) unlink(tfname); 36530143Simp if (dfname[0] && strchr(dfname, '/') == NULL) { 3661553Srgrimes do { 3671553Srgrimes do 3681553Srgrimes (void) unlink(dfname); 3691553Srgrimes while (dfname[2]-- != 'A'); 3701553Srgrimes dfname[2] = 'z'; 3711553Srgrimes } while (dfname[0]-- != 'd'); 37230143Simp } 3731553Srgrimes dfname[0] = '\0'; 3741553Srgrimes} 3751553Srgrimes 3761553Srgrimes#include <stdarg.h> 3771553Srgrimes 3781553Srgrimesstatic void 3791553Srgrimesfrecverr(const char *msg, ...) 3801553Srgrimes{ 3811553Srgrimes va_list ap; 3821553Srgrimes va_start(ap, msg); 38378300Sgad syslog(LOG_ERR, "Error receiving job from %s:", from_host); 3841553Srgrimes vsyslog(LOG_ERR, msg, ap); 3851553Srgrimes va_end(ap); 38668340Sgad /* 38768735Sgad * rcleanup is not called until AFTER logging the error message, 38868735Sgad * because rcleanup will zap some variables which may have been 38968735Sgad * supplied as parameters for that msg... 39068340Sgad */ 39168340Sgad rcleanup(0); 39268340Sgad /* 39368340Sgad * Add a minimal delay before returning the final error code to 39468340Sgad * the sending host. This just in case that machine responds 39568340Sgad * this error by INSTANTLY retrying (and instantly re-failing...). 39668340Sgad * It would be stupid of the sending host to do that, but if there 39768340Sgad * was a broken implementation which did it, the result might be 39868340Sgad * obscure performance problems and a flood of syslog messages on 39968340Sgad * the receiving host. 40068340Sgad */ 40168340Sgad sleep(2); /* a paranoid throttling measure */ 4021553Srgrimes putchar('\1'); /* return error code */ 4031553Srgrimes exit(1); 4041553Srgrimes} 405