xref: /freebsd/usr.sbin/lpr/lpd/recvjob.c (revision 0de89efe5c443f213c7ea28773ef2dc6cf3af2ed)
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
36 static const 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
42 #if 0
43 static char sccsid[] = "@(#)recvjob.c	8.2 (Berkeley) 4/27/95";
44 #endif
45 static const char rcsid[] =
46 	"$Id$";
47 #endif /* not lint */
48 
49 /*
50  * Receive printer jobs from the network, queue them and
51  * start the printer daemon.
52  */
53 #include <sys/param.h>
54 #include <sys/mount.h>
55 #include <sys/stat.h>
56 
57 #include <unistd.h>
58 #include <signal.h>
59 #include <fcntl.h>
60 #include <dirent.h>
61 #include <syslog.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include "lp.h"
66 #include "lp.local.h"
67 #include "extern.h"
68 #include "pathnames.h"
69 
70 #define ack()	(void) write(1, sp, 1);
71 
72 static char	 dfname[NAME_MAX];	/* data files */
73 static int	 minfree;       /* keep at least minfree blocks available */
74 static char	*sp = "";
75 static char	 tfname[NAME_MAX];	/* tmp copy of cf before linking */
76 
77 static int        chksize __P((int));
78 static void       frecverr __P((const char *, ...));
79 static int        noresponse __P((void));
80 static void       rcleanup __P((int));
81 static int        read_number __P((char *));
82 static int        readfile __P((char *, int));
83 static int        readjob __P((void));
84 
85 
86 void
87 recvjob()
88 {
89 	struct stat stb;
90 	int status;
91 
92 	/*
93 	 * Perform lookup for printer name or abbreviation
94 	 */
95 	if ((status = cgetent(&bp, printcapdb, printer)) == -2)
96 		frecverr("cannot open printer description file");
97 	else if (status == -1)
98 		frecverr("unknown printer %s", printer);
99 	else if (status == -3)
100 		fatal("potential reference loop detected in printcap file");
101 
102 	if (cgetstr(bp, "lf", &LF) == -1)
103 		LF = _PATH_CONSOLE;
104 	if (cgetstr(bp, "sd", &SD) == -1)
105 		SD = _PATH_DEFSPOOL;
106 	if (cgetstr(bp, "lo", &LO) == -1)
107 		LO = DEFLOCK;
108 
109 	(void) close(2);			/* set up log file */
110 	if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) {
111 		syslog(LOG_ERR, "%s: %m", LF);
112 		(void) open(_PATH_DEVNULL, O_WRONLY);
113 	}
114 
115 	if (chdir(SD) < 0)
116 		frecverr("%s: %s: %m", printer, SD);
117 	if (stat(LO, &stb) == 0) {
118 		if (stb.st_mode & 010) {
119 			/* queue is disabled */
120 			putchar('\1');		/* return error code */
121 			exit(1);
122 		}
123 	} else if (stat(SD, &stb) < 0)
124 		frecverr("%s: %s: %m", printer, SD);
125 	minfree = 2 * read_number("minfree");	/* scale KB to 512 blocks */
126 	signal(SIGTERM, rcleanup);
127 	signal(SIGPIPE, rcleanup);
128 
129 	if (readjob())
130 		printjob();
131 }
132 
133 /*
134  * Read printer jobs sent by lpd and copy them to the spooling directory.
135  * Return the number of jobs successfully transfered.
136  */
137 static int
138 readjob()
139 {
140 	register int size, nfiles;
141 	register char *cp;
142 
143 	ack();
144 	nfiles = 0;
145 	for (;;) {
146 		/*
147 		 * Read a command to tell us what to do
148 		 */
149 		cp = line;
150 		do {
151 			if ((size = read(1, cp, 1)) != 1) {
152 				if (size < 0)
153 					frecverr("%s: lost connection",
154 					    printer);
155 				return(nfiles);
156 			}
157 		} while (*cp++ != '\n' && (cp - line + 1) < sizeof(line));
158 		if (cp - line + 1 >= sizeof(line))
159 			frecverr("readjob overflow");
160 		*--cp = '\0';
161 		cp = line;
162 		switch (*cp++) {
163 		case '\1':	/* cleanup because data sent was bad */
164 			rcleanup(0);
165 			continue;
166 
167 		case '\2':	/* read cf file */
168 			size = 0;
169 			while (*cp >= '0' && *cp <= '9')
170 				size = size * 10 + (*cp++ - '0');
171 			if (*cp++ != ' ')
172 				break;
173 			/*
174 			 * host name has been authenticated, we use our
175 			 * view of the host name since we may be passed
176 			 * something different than what gethostbyaddr()
177 			 * returns
178 			 */
179 			strncpy(cp + 6, from, sizeof(line) + line - cp - 7);
180 			line[sizeof(line) - 1 ] = '\0';
181 			strncpy(tfname, cp, sizeof(tfname) - 1);
182 			tfname[sizeof (tfname) - 1] = '\0';
183 			tfname[0] = 't';
184 			if (strchr(tfname, '/'))
185 				frecverr("readjob: %s: illegal path name",
186 				    tfname);
187 			if (!chksize(size)) {
188 				(void) write(1, "\2", 1);
189 				continue;
190 			}
191 			if (!readfile(tfname, size)) {
192 				rcleanup(0);
193 				continue;
194 			}
195 			if (link(tfname, cp) < 0)
196 				frecverr("%s: %m", tfname);
197 			(void) unlink(tfname);
198 			tfname[0] = '\0';
199 			nfiles++;
200 			continue;
201 
202 		case '\3':	/* read df file */
203 			size = 0;
204 			while (*cp >= '0' && *cp <= '9')
205 				size = size * 10 + (*cp++ - '0');
206 			if (*cp++ != ' ')
207 				break;
208 			if (!chksize(size)) {
209 				(void) write(1, "\2", 1);
210 				continue;
211 			}
212 			(void) strncpy(dfname, cp, sizeof(dfname) - 1);
213 			dfname[sizeof(dfname) - 1] = '\0';
214 			if (strchr(dfname, '/'))
215 				frecverr("readjob: %s: illegal path name",
216 					dfname);
217 			(void) readfile(dfname, size);
218 			continue;
219 		}
220 		frecverr("protocol screwup: %s", line);
221 	}
222 }
223 
224 /*
225  * Read files send by lpd and copy them to the spooling directory.
226  */
227 static int
228 readfile(file, size)
229 	char *file;
230 	int size;
231 {
232 	register char *cp;
233 	char buf[BUFSIZ];
234 	register int i, j, amt;
235 	int fd, err;
236 
237 	fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
238 	if (fd < 0)
239 		frecverr("readfile: %s: illegal path name: %m", file);
240 	ack();
241 	err = 0;
242 	for (i = 0; i < size; i += BUFSIZ) {
243 		amt = BUFSIZ;
244 		cp = buf;
245 		if (i + amt > size)
246 			amt = size - i;
247 		do {
248 			j = read(1, cp, amt);
249 			if (j <= 0)
250 				frecverr("lost connection");
251 			amt -= j;
252 			cp += j;
253 		} while (amt > 0);
254 		amt = BUFSIZ;
255 		if (i + amt > size)
256 			amt = size - i;
257 		if (write(fd, buf, amt) != amt) {
258 			err++;
259 			break;
260 		}
261 	}
262 	(void) close(fd);
263 	if (err)
264 		frecverr("%s: write error", file);
265 	if (noresponse()) {		/* file sent had bad data in it */
266 		(void) unlink(file);
267 		return(0);
268 	}
269 	ack();
270 	return(1);
271 }
272 
273 static int
274 noresponse()
275 {
276 	char resp;
277 
278 	if (read(1, &resp, 1) != 1)
279 		frecverr("lost connection");
280 	if (resp == '\0')
281 		return(0);
282 	return(1);
283 }
284 
285 /*
286  * Check to see if there is enough space on the disk for size bytes.
287  * 1 == OK, 0 == Not OK.
288  */
289 static int
290 chksize(size)
291 	int size;
292 {
293 	int spacefree;
294 	struct statfs sfb;
295 
296 	if (statfs(".", &sfb) < 0) {
297 		syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
298 		return (1);
299 	}
300 	spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
301 	size = (size + 511) / 512;
302 	if (minfree + size > spacefree)
303 		return(0);
304 	return(1);
305 }
306 
307 static int
308 read_number(fn)
309 	char *fn;
310 {
311 	char lin[80];
312 	register FILE *fp;
313 
314 	if ((fp = fopen(fn, "r")) == NULL)
315 		return (0);
316 	if (fgets(lin, 80, fp) == NULL) {
317 		fclose(fp);
318 		return (0);
319 	}
320 	fclose(fp);
321 	return (atoi(lin));
322 }
323 
324 /*
325  * Remove all the files associated with the current job being transfered.
326  */
327 static void
328 rcleanup(signo)
329 	int signo;
330 {
331 	if (tfname[0])
332 		(void) unlink(tfname);
333 	if (dfname[0])
334 		do {
335 			do
336 				(void) unlink(dfname);
337 			while (dfname[2]-- != 'A');
338 			dfname[2] = 'z';
339 		} while (dfname[0]-- != 'd');
340 	dfname[0] = '\0';
341 }
342 
343 #ifdef __STDC__
344 #include <stdarg.h>
345 #else
346 #include <varargs.h>
347 #endif
348 
349 static void
350 #ifdef __STDC__
351 frecverr(const char *msg, ...)
352 #else
353 frecverr(msg, va_alist)
354 	char *msg;
355         va_dcl
356 #endif
357 {
358 	va_list ap;
359 #ifdef __STDC__
360 	va_start(ap, msg);
361 #else
362 	va_start(ap);
363 #endif
364 	rcleanup(0);
365 	syslog(LOG_ERR, "%s", fromb);
366 	vsyslog(LOG_ERR, msg, ap);
367 	va_end(ap);
368 	putchar('\1');		/* return error code */
369 	exit(1);
370 }
371