recvjob.c revision 78300
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 78300 2001-06-15 22:03:07Z 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>
611553Srgrimes#include <syslog.h>
621553Srgrimes#include <stdio.h>
631553Srgrimes#include <stdlib.h>
641553Srgrimes#include <string.h>
651553Srgrimes#include "lp.h"
661553Srgrimes#include "lp.local.h"
671553Srgrimes#include "extern.h"
681553Srgrimes#include "pathnames.h"
691553Srgrimes
701553Srgrimes#define ack()	(void) write(1, sp, 1);
711553Srgrimes
7227748Simpstatic char	 dfname[NAME_MAX];	/* data files */
731553Srgrimesstatic int	 minfree;       /* keep at least minfree blocks available */
7478146Sgadstatic const char	*sp = "";
7527748Simpstatic char	 tfname[NAME_MAX];	/* tmp copy of cf before linking */
761553Srgrimes
7778146Sgadstatic int	 chksize(int _size);
7878146Sgadstatic void	 frecverr(const char *_msg, ...);
7978146Sgadstatic int	 noresponse(void);
8078146Sgadstatic void	 rcleanup(int _signo);
8178146Sgadstatic int	 read_number(const char *_fn);
8278146Sgadstatic int	 readfile(struct printer *_pp, char *_file, int _size);
8378146Sgadstatic int	 readjob(struct printer *_pp);
841553Srgrimes
851553Srgrimes
861553Srgrimesvoid
8778146Sgadrecvjob(const char *printer)
881553Srgrimes{
891553Srgrimes	struct stat stb;
901553Srgrimes	int status;
9131492Swollman	struct printer myprinter, *pp = &myprinter;
921553Srgrimes
931553Srgrimes	/*
941553Srgrimes	 * Perform lookup for printer name or abbreviation
951553Srgrimes	 */
9632654Swollman	init_printer(pp);
9731492Swollman	status = getprintcap(printer, pp);
9831492Swollman	switch (status) {
9931492Swollman	case PCAPERR_OSERR:
1001553Srgrimes		frecverr("cannot open printer description file");
10131492Swollman		break;
10231492Swollman	case PCAPERR_NOTFOUND:
1031553Srgrimes		frecverr("unknown printer %s", printer);
10431492Swollman		break;
10531492Swollman	case PCAPERR_TCLOOP:
10631492Swollman		fatal(pp, "potential reference loop detected in printcap file");
10731492Swollman	default:
10831492Swollman		break;
10931492Swollman	}
11027748Simp
1111553Srgrimes	(void) close(2);			/* set up log file */
11231492Swollman	if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) {
11331492Swollman		syslog(LOG_ERR, "%s: %m", pp->log_file);
1141553Srgrimes		(void) open(_PATH_DEVNULL, O_WRONLY);
1151553Srgrimes	}
1161553Srgrimes
11731492Swollman	if (chdir(pp->spool_dir) < 0)
11831492Swollman		frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
11931492Swollman	if (stat(pp->lock_file, &stb) == 0) {
1201553Srgrimes		if (stb.st_mode & 010) {
1211553Srgrimes			/* queue is disabled */
1221553Srgrimes			putchar('\1');		/* return error code */
1231553Srgrimes			exit(1);
1241553Srgrimes		}
12531492Swollman	} else if (stat(pp->spool_dir, &stb) < 0)
12631492Swollman		frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
1271553Srgrimes	minfree = 2 * read_number("minfree");	/* scale KB to 512 blocks */
1281553Srgrimes	signal(SIGTERM, rcleanup);
1291553Srgrimes	signal(SIGPIPE, rcleanup);
1301553Srgrimes
13131492Swollman	if (readjob(pp))
13231492Swollman		printjob(pp);
1331553Srgrimes}
1341553Srgrimes
1351553Srgrimes/*
1361553Srgrimes * Read printer jobs sent by lpd and copy them to the spooling directory.
1371553Srgrimes * Return the number of jobs successfully transfered.
1381553Srgrimes */
1391553Srgrimesstatic int
14078146Sgadreadjob(struct printer *pp)
1411553Srgrimes{
14268253Sgad	register int size;
1431553Srgrimes	register char *cp;
14468735Sgad	int cfcnt, dfcnt;
14568735Sgad	char givenid[32], givenhost[MAXHOSTNAMELEN];
1461553Srgrimes
1471553Srgrimes	ack();
14868253Sgad	cfcnt = 0;
14968253Sgad	dfcnt = 0;
1501553Srgrimes	for (;;) {
1511553Srgrimes		/*
1521553Srgrimes		 * Read a command to tell us what to do
1531553Srgrimes		 */
1541553Srgrimes		cp = line;
1551553Srgrimes		do {
1561553Srgrimes			if ((size = read(1, cp, 1)) != 1) {
15768380Sgad				if (size < 0) {
15829780Scharnier					frecverr("%s: lost connection",
15931492Swollman					    pp->printer);
16068380Sgad					/*NOTREACHED*/
16168380Sgad				}
16268253Sgad				return (cfcnt);
1631553Srgrimes			}
16427748Simp		} while (*cp++ != '\n' && (cp - line + 1) < sizeof(line));
16568380Sgad		if (cp - line + 1 >= sizeof(line)) {
16668380Sgad			frecverr("%s: readjob overflow", pp->printer);
16768380Sgad			/*NOTREACHED*/
16868380Sgad		}
1691553Srgrimes		*--cp = '\0';
1701553Srgrimes		cp = line;
1711553Srgrimes		switch (*cp++) {
1721553Srgrimes		case '\1':	/* cleanup because data sent was bad */
1731553Srgrimes			rcleanup(0);
1741553Srgrimes			continue;
1751553Srgrimes
1761553Srgrimes		case '\2':	/* read cf file */
1771553Srgrimes			size = 0;
17868253Sgad			dfcnt = 0;
1791553Srgrimes			while (*cp >= '0' && *cp <= '9')
1801553Srgrimes				size = size * 10 + (*cp++ - '0');
1811553Srgrimes			if (*cp++ != ' ')
1821553Srgrimes				break;
1831553Srgrimes			/*
1841553Srgrimes			 * host name has been authenticated, we use our
1851553Srgrimes			 * view of the host name since we may be passed
1861553Srgrimes			 * something different than what gethostbyaddr()
1871553Srgrimes			 * returns
1881553Srgrimes			 */
18978300Sgad			strncpy(cp + 6, from_host, sizeof(line) + line - cp
19078300Sgad			    - 7);
19127748Simp			line[sizeof(line) - 1 ] = '\0';
19227748Simp			strncpy(tfname, cp, sizeof(tfname) - 1);
19327748Simp			tfname[sizeof (tfname) - 1] = '\0';
1941553Srgrimes			tfname[0] = 't';
19527748Simp			if (strchr(tfname, '/'))
19627748Simp				frecverr("readjob: %s: illegal path name",
19727748Simp				    tfname);
1981553Srgrimes			if (!chksize(size)) {
1991553Srgrimes				(void) write(1, "\2", 1);
2001553Srgrimes				continue;
2011553Srgrimes			}
20268380Sgad			if (!readfile(pp, tfname, size)) {
2031553Srgrimes				rcleanup(0);
2041553Srgrimes				continue;
2051553Srgrimes			}
2061553Srgrimes			if (link(tfname, cp) < 0)
2071553Srgrimes				frecverr("%s: %m", tfname);
2081553Srgrimes			(void) unlink(tfname);
2091553Srgrimes			tfname[0] = '\0';
21068253Sgad			cfcnt++;
2111553Srgrimes			continue;
2121553Srgrimes
2131553Srgrimes		case '\3':	/* read df file */
21468253Sgad			*givenid = '\0';
21568253Sgad			*givenhost = '\0';
2161553Srgrimes			size = 0;
2171553Srgrimes			while (*cp >= '0' && *cp <= '9')
2181553Srgrimes				size = size * 10 + (*cp++ - '0');
2191553Srgrimes			if (*cp++ != ' ')
2201553Srgrimes				break;
2211553Srgrimes			if (!chksize(size)) {
2221553Srgrimes				(void) write(1, "\2", 1);
2231553Srgrimes				continue;
2241553Srgrimes			}
22527748Simp			(void) strncpy(dfname, cp, sizeof(dfname) - 1);
22627748Simp			dfname[sizeof(dfname) - 1] = '\0';
22768380Sgad			if (strchr(dfname, '/')) {
2281553Srgrimes				frecverr("readjob: %s: illegal path name",
2291553Srgrimes					dfname);
23068380Sgad				/*NOTREACHED*/
23168380Sgad			}
23268253Sgad			dfcnt++;
23368253Sgad			trstat_init(pp, dfname, dfcnt);
23468380Sgad			(void) readfile(pp, dfname, size);
23578300Sgad			trstat_write(pp, TR_RECVING, size, givenid, from_host,
23668253Sgad				     givenhost);
2371553Srgrimes			continue;
2381553Srgrimes		}
2391553Srgrimes		frecverr("protocol screwup: %s", line);
24068380Sgad		/*NOTREACHED*/
2411553Srgrimes	}
2421553Srgrimes}
2431553Srgrimes
2441553Srgrimes/*
2451553Srgrimes * Read files send by lpd and copy them to the spooling directory.
2461553Srgrimes */
2471553Srgrimesstatic int
24878146Sgadreadfile(struct printer *pp, char *file, int size)
2491553Srgrimes{
2501553Srgrimes	register char *cp;
2511553Srgrimes	char buf[BUFSIZ];
2521553Srgrimes	register int i, j, amt;
2531553Srgrimes	int fd, err;
2541553Srgrimes
2551553Srgrimes	fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
25668380Sgad	if (fd < 0) {
25768380Sgad		frecverr("%s: readfile: error on open(%s): %m",
25868380Sgad			 pp->printer, file);
25968380Sgad		/*NOTREACHED*/
26068380Sgad	}
2611553Srgrimes	ack();
2621553Srgrimes	err = 0;
2631553Srgrimes	for (i = 0; i < size; i += BUFSIZ) {
2641553Srgrimes		amt = BUFSIZ;
2651553Srgrimes		cp = buf;
2661553Srgrimes		if (i + amt > size)
2671553Srgrimes			amt = size - i;
2681553Srgrimes		do {
2691553Srgrimes			j = read(1, cp, amt);
27068380Sgad			if (j <= 0) {
27168380Sgad				frecverr("%s: lost connection", pp->printer);
27268380Sgad				/*NOTREACHED*/
27368380Sgad			}
2741553Srgrimes			amt -= j;
2751553Srgrimes			cp += j;
2761553Srgrimes		} while (amt > 0);
2771553Srgrimes		amt = BUFSIZ;
2781553Srgrimes		if (i + amt > size)
2791553Srgrimes			amt = size - i;
2801553Srgrimes		if (write(fd, buf, amt) != amt) {
2811553Srgrimes			err++;
2821553Srgrimes			break;
2831553Srgrimes		}
2841553Srgrimes	}
2851553Srgrimes	(void) close(fd);
28668380Sgad	if (err) {
28768380Sgad		frecverr("%s: write error on close(%s)", pp->printer, file);
28868380Sgad		/*NOTREACHED*/
28968380Sgad	}
2901553Srgrimes	if (noresponse()) {		/* file sent had bad data in it */
29130143Simp		if (strchr(file, '/') == NULL)
29230143Simp			(void) unlink(file);
29368380Sgad		return (0);
2941553Srgrimes	}
2951553Srgrimes	ack();
29668380Sgad	return (1);
2971553Srgrimes}
2981553Srgrimes
2991553Srgrimesstatic int
30078146Sgadnoresponse(void)
3011553Srgrimes{
3021553Srgrimes	char resp;
3031553Srgrimes
30468380Sgad	if (read(1, &resp, 1) != 1) {
30568380Sgad		frecverr("lost connection in noresponse()");
30668380Sgad		/*NOTREACHED*/
30768380Sgad	}
3081553Srgrimes	if (resp == '\0')
3091553Srgrimes		return(0);
3101553Srgrimes	return(1);
3111553Srgrimes}
3121553Srgrimes
3131553Srgrimes/*
3141553Srgrimes * Check to see if there is enough space on the disk for size bytes.
3151553Srgrimes * 1 == OK, 0 == Not OK.
3161553Srgrimes */
3171553Srgrimesstatic int
31878146Sgadchksize(int size)
3191553Srgrimes{
3201553Srgrimes	int spacefree;
3211553Srgrimes	struct statfs sfb;
3221553Srgrimes
3231553Srgrimes	if (statfs(".", &sfb) < 0) {
3241553Srgrimes		syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
3251553Srgrimes		return (1);
3261553Srgrimes	}
3271553Srgrimes	spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
3281553Srgrimes	size = (size + 511) / 512;
3291553Srgrimes	if (minfree + size > spacefree)
3301553Srgrimes		return(0);
3311553Srgrimes	return(1);
3321553Srgrimes}
3331553Srgrimes
3341553Srgrimesstatic int
33578146Sgadread_number(const char *fn)
3361553Srgrimes{
3371553Srgrimes	char lin[80];
3381553Srgrimes	register FILE *fp;
3391553Srgrimes
3401553Srgrimes	if ((fp = fopen(fn, "r")) == NULL)
3411553Srgrimes		return (0);
3421553Srgrimes	if (fgets(lin, 80, fp) == NULL) {
3431553Srgrimes		fclose(fp);
3441553Srgrimes		return (0);
3451553Srgrimes	}
3461553Srgrimes	fclose(fp);
3471553Srgrimes	return (atoi(lin));
3481553Srgrimes}
3491553Srgrimes
3501553Srgrimes/*
3511553Srgrimes * Remove all the files associated with the current job being transfered.
3521553Srgrimes */
3531553Srgrimesstatic void
35478146Sgadrcleanup(int signo __unused)
3551553Srgrimes{
35630143Simp	if (tfname[0] && strchr(tfname, '/') == NULL)
3571553Srgrimes		(void) unlink(tfname);
35830143Simp	if (dfname[0] && strchr(dfname, '/') == NULL) {
3591553Srgrimes		do {
3601553Srgrimes			do
3611553Srgrimes				(void) unlink(dfname);
3621553Srgrimes			while (dfname[2]-- != 'A');
3631553Srgrimes			dfname[2] = 'z';
3641553Srgrimes		} while (dfname[0]-- != 'd');
36530143Simp	}
3661553Srgrimes	dfname[0] = '\0';
3671553Srgrimes}
3681553Srgrimes
36927757Simp#ifdef __STDC__
3701553Srgrimes#include <stdarg.h>
3711553Srgrimes#else
3721553Srgrimes#include <varargs.h>
3731553Srgrimes#endif
3741553Srgrimes
3751553Srgrimesstatic void
37627757Simp#ifdef __STDC__
3771553Srgrimesfrecverr(const char *msg, ...)
3781553Srgrimes#else
3791553Srgrimesfrecverr(msg, va_alist)
3801553Srgrimes	char *msg;
3811553Srgrimes        va_dcl
3821553Srgrimes#endif
3831553Srgrimes{
3841553Srgrimes	va_list ap;
38527757Simp#ifdef __STDC__
3861553Srgrimes	va_start(ap, msg);
3871553Srgrimes#else
3881553Srgrimes	va_start(ap);
3891553Srgrimes#endif
39078300Sgad	syslog(LOG_ERR, "Error receiving job from %s:", from_host);
3911553Srgrimes	vsyslog(LOG_ERR, msg, ap);
3921553Srgrimes	va_end(ap);
39368340Sgad	/*
39468735Sgad	 * rcleanup is not called until AFTER logging the error message,
39568735Sgad	 * because rcleanup will zap some variables which may have been
39668735Sgad	 * supplied as parameters for that msg...
39768340Sgad	 */
39868340Sgad	rcleanup(0);
39968340Sgad	/*
40068340Sgad	 * Add a minimal delay before returning the final error code to
40168340Sgad	 * the sending host.  This just in case that machine responds
40268340Sgad	 * this error by INSTANTLY retrying (and instantly re-failing...).
40368340Sgad	 * It would be stupid of the sending host to do that, but if there
40468340Sgad	 * was a broken implementation which did it, the result might be
40568340Sgad	 * obscure performance problems and a flood of syslog messages on
40668340Sgad	 * the receiving host.
40768340Sgad	 */
40868340Sgad	sleep(2);		/* a paranoid throttling measure */
4091553Srgrimes	putchar('\1');		/* return error code */
4101553Srgrimes	exit(1);
4111553Srgrimes}
412