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