recvjob.c revision 27635
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 *    must display the following acknowledgement:
16 *	This product includes software developed by the University of
17 *	California, Berkeley and its contributors.
18 * 4. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#ifndef lint
36static char copyright[] =
37"@(#) Copyright (c) 1983, 1993\n\
38	The Regents of the University of California.  All rights reserved.\n";
39#endif /* not lint */
40
41#ifndef lint
42static char sccsid[] = "@(#)recvjob.c	8.2 (Berkeley) 4/27/95";
43#endif /* not lint */
44
45/*
46 * Receive printer jobs from the network, queue them and
47 * start the printer daemon.
48 */
49#include <sys/param.h>
50#include <sys/mount.h>
51#include <sys/stat.h>
52
53#include <unistd.h>
54#include <signal.h>
55#include <fcntl.h>
56#include <dirent.h>
57#include <syslog.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <string.h>
61#include "lp.h"
62#include "lp.local.h"
63#include "extern.h"
64#include "pathnames.h"
65
66#define ack()	(void) write(1, sp, 1);
67
68static char	 dfname[256];	/* data files */
69static int	 minfree;       /* keep at least minfree blocks available */
70static char	*sp = "";
71static char	 tfname[256];	/* tmp copy of cf before linking */
72
73static int        chksize __P((int));
74static void       frecverr __P((const char *, ...));
75static int        noresponse __P((void));
76static void       rcleanup __P((int));
77static int        read_number __P((char *));
78static int        readfile __P((char *, int));
79static int        readjob __P((void));
80
81
82void
83recvjob()
84{
85	struct stat stb;
86	int status;
87
88	/*
89	 * Perform lookup for printer name or abbreviation
90	 */
91	if ((status = cgetent(&bp, printcapdb, printer)) == -2)
92		frecverr("cannot open printer description file");
93	else if (status == -1)
94		frecverr("unknown printer %s", printer);
95	else if (status == -3)
96		fatal("potential reference loop detected in printcap file");
97
98	if (cgetstr(bp, "lf", &LF) == -1)
99		LF = _PATH_CONSOLE;
100	if (cgetstr(bp, "sd", &SD) == -1)
101		SD = _PATH_DEFSPOOL;
102	if (cgetstr(bp, "lo", &LO) == -1)
103		LO = DEFLOCK;
104
105	(void) close(2);			/* set up log file */
106	if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) {
107		syslog(LOG_ERR, "%s: %m", LF);
108		(void) open(_PATH_DEVNULL, O_WRONLY);
109	}
110
111	if (chdir(SD) < 0)
112		frecverr("%s: %s: %m", printer, SD);
113	if (stat(LO, &stb) == 0) {
114		if (stb.st_mode & 010) {
115			/* queue is disabled */
116			putchar('\1');		/* return error code */
117			exit(1);
118		}
119	} else if (stat(SD, &stb) < 0)
120		frecverr("%s: %s: %m", printer, SD);
121	minfree = 2 * read_number("minfree");	/* scale KB to 512 blocks */
122	signal(SIGTERM, rcleanup);
123	signal(SIGPIPE, rcleanup);
124
125	if (readjob())
126		printjob();
127}
128
129/*
130 * Read printer jobs sent by lpd and copy them to the spooling directory.
131 * Return the number of jobs successfully transfered.
132 */
133static int
134readjob()
135{
136	register int size, nfiles;
137	register char *cp;
138
139	ack();
140	nfiles = 0;
141	for (;;) {
142		/*
143		 * Read a command to tell us what to do
144		 */
145		cp = line;
146		do {
147			if ((size = read(1, cp, 1)) != 1) {
148				if (size < 0)
149					frecverr("%s: Lost connection",printer);
150				return(nfiles);
151			}
152		} while (*cp++ != '\n');
153		*--cp = '\0';
154		cp = line;
155		switch (*cp++) {
156		case '\1':	/* cleanup because data sent was bad */
157			rcleanup(0);
158			continue;
159
160		case '\2':	/* read cf file */
161			size = 0;
162			while (*cp >= '0' && *cp <= '9')
163				size = size * 10 + (*cp++ - '0');
164			if (*cp++ != ' ')
165				break;
166			/*
167			 * host name has been authenticated, we use our
168			 * view of the host name since we may be passed
169			 * something different than what gethostbyaddr()
170			 * returns
171			 */
172			strcpy(cp + 6, from);
173			strncpy(tfname, cp, sizeof tfname-1);
174			tfname[sizeof tfname-1] = '\0';
175			tfname[0] = 't';
176			if (!chksize(size)) {
177				(void) write(1, "\2", 1);
178				continue;
179			}
180			if (!readfile(tfname, size)) {
181				rcleanup(0);
182				continue;
183			}
184			if (link(tfname, cp) < 0)
185				frecverr("%s: %m", tfname);
186			(void) unlink(tfname);
187			tfname[0] = '\0';
188			nfiles++;
189			continue;
190
191		case '\3':	/* read df file */
192			size = 0;
193			while (*cp >= '0' && *cp <= '9')
194				size = size * 10 + (*cp++ - '0');
195			if (*cp++ != ' ')
196				break;
197			if (!chksize(size)) {
198				(void) write(1, "\2", 1);
199				continue;
200			}
201			(void) strncpy(dfname, cp, sizeof dfname-1);
202			dfname[sizeof dfname-1] = '\0';
203			if (strchr(dfname, '/'))
204				frecverr("readjob: %s: illegal path name",
205					dfname);
206			(void) readfile(dfname, size);
207			continue;
208		}
209		frecverr("protocol screwup: %s", line);
210	}
211}
212
213/*
214 * Read files send by lpd and copy them to the spooling directory.
215 */
216static int
217readfile(file, size)
218	char *file;
219	int size;
220{
221	register char *cp;
222	char buf[BUFSIZ];
223	register int i, j, amt;
224	int fd, err;
225
226	fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
227	if (fd < 0)
228		frecverr("readfile: %s: illegal path name: %m", file);
229	ack();
230	err = 0;
231	for (i = 0; i < size; i += BUFSIZ) {
232		amt = BUFSIZ;
233		cp = buf;
234		if (i + amt > size)
235			amt = size - i;
236		do {
237			j = read(1, cp, amt);
238			if (j <= 0)
239				frecverr("Lost connection");
240			amt -= j;
241			cp += j;
242		} while (amt > 0);
243		amt = BUFSIZ;
244		if (i + amt > size)
245			amt = size - i;
246		if (write(fd, buf, amt) != amt) {
247			err++;
248			break;
249		}
250	}
251	(void) close(fd);
252	if (err)
253		frecverr("%s: write error", file);
254	if (noresponse()) {		/* file sent had bad data in it */
255		(void) unlink(file);
256		return(0);
257	}
258	ack();
259	return(1);
260}
261
262static int
263noresponse()
264{
265	char resp;
266
267	if (read(1, &resp, 1) != 1)
268		frecverr("Lost connection");
269	if (resp == '\0')
270		return(0);
271	return(1);
272}
273
274/*
275 * Check to see if there is enough space on the disk for size bytes.
276 * 1 == OK, 0 == Not OK.
277 */
278static int
279chksize(size)
280	int size;
281{
282	int spacefree;
283	struct statfs sfb;
284
285	if (statfs(".", &sfb) < 0) {
286		syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
287		return (1);
288	}
289	spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
290	size = (size + 511) / 512;
291	if (minfree + size > spacefree)
292		return(0);
293	return(1);
294}
295
296static int
297read_number(fn)
298	char *fn;
299{
300	char lin[80];
301	register FILE *fp;
302
303	if ((fp = fopen(fn, "r")) == NULL)
304		return (0);
305	if (fgets(lin, 80, fp) == NULL) {
306		fclose(fp);
307		return (0);
308	}
309	fclose(fp);
310	return (atoi(lin));
311}
312
313/*
314 * Remove all the files associated with the current job being transfered.
315 */
316static void
317rcleanup(signo)
318	int signo;
319{
320	if (tfname[0])
321		(void) unlink(tfname);
322	if (dfname[0])
323		do {
324			do
325				(void) unlink(dfname);
326			while (dfname[2]-- != 'A');
327			dfname[2] = 'z';
328		} while (dfname[0]-- != 'd');
329	dfname[0] = '\0';
330}
331
332#if __STDC__
333#include <stdarg.h>
334#else
335#include <varargs.h>
336#endif
337
338static void
339#if __STDC__
340frecverr(const char *msg, ...)
341#else
342frecverr(msg, va_alist)
343	char *msg;
344        va_dcl
345#endif
346{
347	va_list ap;
348#if __STDC__
349	va_start(ap, msg);
350#else
351	va_start(ap);
352#endif
353	rcleanup(0);
354	syslog(LOG_ERR, "%s", fromb);
355	vsyslog(LOG_ERR, msg, ap);
356	va_end(ap);
357	putchar('\1');		/* return error code */
358	exit(1);
359}
360