recvjob.c revision 32654
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[] =
4632654Swollman	"$Id: recvjob.c,v 1.12 1997/12/02 20:45:59 wollman Exp $";
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 */
741553Srgrimesstatic char	*sp = "";
7527748Simpstatic char	 tfname[NAME_MAX];	/* tmp copy of cf before linking */
761553Srgrimes
771553Srgrimesstatic int        chksize __P((int));
781553Srgrimesstatic void       frecverr __P((const char *, ...));
791553Srgrimesstatic int        noresponse __P((void));
801553Srgrimesstatic void       rcleanup __P((int));
811553Srgrimesstatic int        read_number __P((char *));
821553Srgrimesstatic int        readfile __P((char *, int));
8331492Swollmanstatic int        readjob __P((struct printer *pp));
841553Srgrimes
851553Srgrimes
861553Srgrimesvoid
8731492Swollmanrecvjob(printer)
8831492Swollman	const char *printer;
891553Srgrimes{
901553Srgrimes	struct stat stb;
911553Srgrimes	int status;
9231492Swollman	struct printer myprinter, *pp = &myprinter;
931553Srgrimes
941553Srgrimes	/*
951553Srgrimes	 * Perform lookup for printer name or abbreviation
961553Srgrimes	 */
9732654Swollman	init_printer(pp);
9831492Swollman	status = getprintcap(printer, pp);
9931492Swollman	switch (status) {
10031492Swollman	case PCAPERR_OSERR:
1011553Srgrimes		frecverr("cannot open printer description file");
10231492Swollman		break;
10331492Swollman	case PCAPERR_NOTFOUND:
1041553Srgrimes		frecverr("unknown printer %s", printer);
10531492Swollman		break;
10631492Swollman	case PCAPERR_TCLOOP:
10731492Swollman		fatal(pp, "potential reference loop detected in printcap file");
10831492Swollman	default:
10931492Swollman		break;
11031492Swollman	}
11127748Simp
1121553Srgrimes	(void) close(2);			/* set up log file */
11331492Swollman	if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) {
11431492Swollman		syslog(LOG_ERR, "%s: %m", pp->log_file);
1151553Srgrimes		(void) open(_PATH_DEVNULL, O_WRONLY);
1161553Srgrimes	}
1171553Srgrimes
11831492Swollman	if (chdir(pp->spool_dir) < 0)
11931492Swollman		frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
12031492Swollman	if (stat(pp->lock_file, &stb) == 0) {
1211553Srgrimes		if (stb.st_mode & 010) {
1221553Srgrimes			/* queue is disabled */
1231553Srgrimes			putchar('\1');		/* return error code */
1241553Srgrimes			exit(1);
1251553Srgrimes		}
12631492Swollman	} else if (stat(pp->spool_dir, &stb) < 0)
12731492Swollman		frecverr("%s: %s: %m", pp->printer, pp->spool_dir);
1281553Srgrimes	minfree = 2 * read_number("minfree");	/* scale KB to 512 blocks */
1291553Srgrimes	signal(SIGTERM, rcleanup);
1301553Srgrimes	signal(SIGPIPE, rcleanup);
1311553Srgrimes
13231492Swollman	if (readjob(pp))
13331492Swollman		printjob(pp);
1341553Srgrimes}
1351553Srgrimes
1361553Srgrimes/*
1371553Srgrimes * Read printer jobs sent by lpd and copy them to the spooling directory.
1381553Srgrimes * Return the number of jobs successfully transfered.
1391553Srgrimes */
1401553Srgrimesstatic int
14131492Swollmanreadjob(pp)
14231492Swollman	struct printer *pp;
1431553Srgrimes{
1441553Srgrimes	register int size, nfiles;
1451553Srgrimes	register char *cp;
1461553Srgrimes
1471553Srgrimes	ack();
1481553Srgrimes	nfiles = 0;
1491553Srgrimes	for (;;) {
1501553Srgrimes		/*
1511553Srgrimes		 * Read a command to tell us what to do
1521553Srgrimes		 */
1531553Srgrimes		cp = line;
1541553Srgrimes		do {
1551553Srgrimes			if ((size = read(1, cp, 1)) != 1) {
1561553Srgrimes				if (size < 0)
15729780Scharnier					frecverr("%s: lost connection",
15831492Swollman					    pp->printer);
1591553Srgrimes				return(nfiles);
1601553Srgrimes			}
16127748Simp		} while (*cp++ != '\n' && (cp - line + 1) < sizeof(line));
16227748Simp		if (cp - line + 1 >= sizeof(line))
16327748Simp			frecverr("readjob overflow");
1641553Srgrimes		*--cp = '\0';
1651553Srgrimes		cp = line;
1661553Srgrimes		switch (*cp++) {
1671553Srgrimes		case '\1':	/* cleanup because data sent was bad */
1681553Srgrimes			rcleanup(0);
1691553Srgrimes			continue;
1701553Srgrimes
1711553Srgrimes		case '\2':	/* read cf file */
1721553Srgrimes			size = 0;
1731553Srgrimes			while (*cp >= '0' && *cp <= '9')
1741553Srgrimes				size = size * 10 + (*cp++ - '0');
1751553Srgrimes			if (*cp++ != ' ')
1761553Srgrimes				break;
1771553Srgrimes			/*
1781553Srgrimes			 * host name has been authenticated, we use our
1791553Srgrimes			 * view of the host name since we may be passed
1801553Srgrimes			 * something different than what gethostbyaddr()
1811553Srgrimes			 * returns
1821553Srgrimes			 */
18327748Simp			strncpy(cp + 6, from, sizeof(line) + line - cp - 7);
18427748Simp			line[sizeof(line) - 1 ] = '\0';
18527748Simp			strncpy(tfname, cp, sizeof(tfname) - 1);
18627748Simp			tfname[sizeof (tfname) - 1] = '\0';
1871553Srgrimes			tfname[0] = 't';
18827748Simp			if (strchr(tfname, '/'))
18927748Simp				frecverr("readjob: %s: illegal path name",
19027748Simp				    tfname);
1911553Srgrimes			if (!chksize(size)) {
1921553Srgrimes				(void) write(1, "\2", 1);
1931553Srgrimes				continue;
1941553Srgrimes			}
1951553Srgrimes			if (!readfile(tfname, size)) {
1961553Srgrimes				rcleanup(0);
1971553Srgrimes				continue;
1981553Srgrimes			}
1991553Srgrimes			if (link(tfname, cp) < 0)
2001553Srgrimes				frecverr("%s: %m", tfname);
2011553Srgrimes			(void) unlink(tfname);
2021553Srgrimes			tfname[0] = '\0';
2031553Srgrimes			nfiles++;
2041553Srgrimes			continue;
2051553Srgrimes
2061553Srgrimes		case '\3':	/* read df file */
2071553Srgrimes			size = 0;
2081553Srgrimes			while (*cp >= '0' && *cp <= '9')
2091553Srgrimes				size = size * 10 + (*cp++ - '0');
2101553Srgrimes			if (*cp++ != ' ')
2111553Srgrimes				break;
2121553Srgrimes			if (!chksize(size)) {
2131553Srgrimes				(void) write(1, "\2", 1);
2141553Srgrimes				continue;
2151553Srgrimes			}
21627748Simp			(void) strncpy(dfname, cp, sizeof(dfname) - 1);
21727748Simp			dfname[sizeof(dfname) - 1] = '\0';
21827635Simp			if (strchr(dfname, '/'))
2191553Srgrimes				frecverr("readjob: %s: illegal path name",
2201553Srgrimes					dfname);
2211553Srgrimes			(void) readfile(dfname, size);
2221553Srgrimes			continue;
2231553Srgrimes		}
2241553Srgrimes		frecverr("protocol screwup: %s", line);
2251553Srgrimes	}
2261553Srgrimes}
2271553Srgrimes
2281553Srgrimes/*
2291553Srgrimes * Read files send by lpd and copy them to the spooling directory.
2301553Srgrimes */
2311553Srgrimesstatic int
2321553Srgrimesreadfile(file, size)
2331553Srgrimes	char *file;
2341553Srgrimes	int size;
2351553Srgrimes{
2361553Srgrimes	register char *cp;
2371553Srgrimes	char buf[BUFSIZ];
2381553Srgrimes	register int i, j, amt;
2391553Srgrimes	int fd, err;
2401553Srgrimes
2411553Srgrimes	fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
2421553Srgrimes	if (fd < 0)
2431553Srgrimes		frecverr("readfile: %s: illegal path name: %m", file);
2441553Srgrimes	ack();
2451553Srgrimes	err = 0;
2461553Srgrimes	for (i = 0; i < size; i += BUFSIZ) {
2471553Srgrimes		amt = BUFSIZ;
2481553Srgrimes		cp = buf;
2491553Srgrimes		if (i + amt > size)
2501553Srgrimes			amt = size - i;
2511553Srgrimes		do {
2521553Srgrimes			j = read(1, cp, amt);
2531553Srgrimes			if (j <= 0)
25429780Scharnier				frecverr("lost connection");
2551553Srgrimes			amt -= j;
2561553Srgrimes			cp += j;
2571553Srgrimes		} while (amt > 0);
2581553Srgrimes		amt = BUFSIZ;
2591553Srgrimes		if (i + amt > size)
2601553Srgrimes			amt = size - i;
2611553Srgrimes		if (write(fd, buf, amt) != amt) {
2621553Srgrimes			err++;
2631553Srgrimes			break;
2641553Srgrimes		}
2651553Srgrimes	}
2661553Srgrimes	(void) close(fd);
2671553Srgrimes	if (err)
2681553Srgrimes		frecverr("%s: write error", file);
2691553Srgrimes	if (noresponse()) {		/* file sent had bad data in it */
27030143Simp		if (strchr(file, '/') == NULL)
27130143Simp			(void) unlink(file);
2721553Srgrimes		return(0);
2731553Srgrimes	}
2741553Srgrimes	ack();
2751553Srgrimes	return(1);
2761553Srgrimes}
2771553Srgrimes
2781553Srgrimesstatic int
2791553Srgrimesnoresponse()
2801553Srgrimes{
2811553Srgrimes	char resp;
2821553Srgrimes
2831553Srgrimes	if (read(1, &resp, 1) != 1)
28429780Scharnier		frecverr("lost connection");
2851553Srgrimes	if (resp == '\0')
2861553Srgrimes		return(0);
2871553Srgrimes	return(1);
2881553Srgrimes}
2891553Srgrimes
2901553Srgrimes/*
2911553Srgrimes * Check to see if there is enough space on the disk for size bytes.
2921553Srgrimes * 1 == OK, 0 == Not OK.
2931553Srgrimes */
2941553Srgrimesstatic int
2951553Srgrimeschksize(size)
2961553Srgrimes	int size;
2971553Srgrimes{
2981553Srgrimes	int spacefree;
2991553Srgrimes	struct statfs sfb;
3001553Srgrimes
3011553Srgrimes	if (statfs(".", &sfb) < 0) {
3021553Srgrimes		syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
3031553Srgrimes		return (1);
3041553Srgrimes	}
3051553Srgrimes	spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
3061553Srgrimes	size = (size + 511) / 512;
3071553Srgrimes	if (minfree + size > spacefree)
3081553Srgrimes		return(0);
3091553Srgrimes	return(1);
3101553Srgrimes}
3111553Srgrimes
3121553Srgrimesstatic int
3131553Srgrimesread_number(fn)
3141553Srgrimes	char *fn;
3151553Srgrimes{
3161553Srgrimes	char lin[80];
3171553Srgrimes	register FILE *fp;
3181553Srgrimes
3191553Srgrimes	if ((fp = fopen(fn, "r")) == NULL)
3201553Srgrimes		return (0);
3211553Srgrimes	if (fgets(lin, 80, fp) == NULL) {
3221553Srgrimes		fclose(fp);
3231553Srgrimes		return (0);
3241553Srgrimes	}
3251553Srgrimes	fclose(fp);
3261553Srgrimes	return (atoi(lin));
3271553Srgrimes}
3281553Srgrimes
3291553Srgrimes/*
3301553Srgrimes * Remove all the files associated with the current job being transfered.
3311553Srgrimes */
3321553Srgrimesstatic void
3331553Srgrimesrcleanup(signo)
3341553Srgrimes	int signo;
3351553Srgrimes{
33630143Simp	if (tfname[0] && strchr(tfname, '/') == NULL)
3371553Srgrimes		(void) unlink(tfname);
33830143Simp	if (dfname[0] && strchr(dfname, '/') == NULL) {
3391553Srgrimes		do {
3401553Srgrimes			do
3411553Srgrimes				(void) unlink(dfname);
3421553Srgrimes			while (dfname[2]-- != 'A');
3431553Srgrimes			dfname[2] = 'z';
3441553Srgrimes		} while (dfname[0]-- != 'd');
34530143Simp	}
3461553Srgrimes	dfname[0] = '\0';
3471553Srgrimes}
3481553Srgrimes
34927757Simp#ifdef __STDC__
3501553Srgrimes#include <stdarg.h>
3511553Srgrimes#else
3521553Srgrimes#include <varargs.h>
3531553Srgrimes#endif
3541553Srgrimes
3551553Srgrimesstatic void
35627757Simp#ifdef __STDC__
3571553Srgrimesfrecverr(const char *msg, ...)
3581553Srgrimes#else
3591553Srgrimesfrecverr(msg, va_alist)
3601553Srgrimes	char *msg;
3611553Srgrimes        va_dcl
3621553Srgrimes#endif
3631553Srgrimes{
3641553Srgrimes	va_list ap;
36527757Simp#ifdef __STDC__
3661553Srgrimes	va_start(ap, msg);
3671553Srgrimes#else
3681553Srgrimes	va_start(ap);
3691553Srgrimes#endif
3701553Srgrimes	rcleanup(0);
3711553Srgrimes	syslog(LOG_ERR, "%s", fromb);
3721553Srgrimes	vsyslog(LOG_ERR, msg, ap);
3731553Srgrimes	va_end(ap);
3741553Srgrimes	putchar('\1');		/* return error code */
3751553Srgrimes	exit(1);
3761553Srgrimes}
377