Wed Oct 14 16:02:04 2015 UTC ()
this is syslog-like


(christos)
diff -r1.23 -r1.24 src/usr.sbin/lpr/lpd/recvjob.c

cvs diff -r1.23 -r1.24 src/usr.sbin/lpr/lpd/recvjob.c (switch to unified diff)

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