2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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. 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
18 * without specific prior written permission.
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
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
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
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
34 static const char copyright[] =
35 "@(#) Copyright (c) 1983, 1993\n\
36 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95";
45 #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
46 __FBSDID("$FreeBSD$");
49 * Receive printer jobs from the network, queue them and
50 * start the printer daemon.
52 #include <sys/param.h>
53 #include <sys/mount.h>
69 #include "pathnames.h"
71 #define ack() (void) write(STDOUT_FILENO, sp, (size_t)1);
74 * The buffer size to use when reading/writing spool files.
76 #define SPL_BUFSIZ BUFSIZ
78 static char dfname[NAME_MAX]; /* data files */
79 static int minfree; /* keep at least minfree blocks available */
80 static const char *sp = "";
81 static char tfname[NAME_MAX]; /* tmp copy of cf before linking */
83 static int chksize(int _size);
84 static void frecverr(const char *_msg, ...) __printf0like(1, 2);
85 static int noresponse(void);
86 static void rcleanup(int _signo);
87 static int read_number(const char *_fn);
88 static int readfile(struct printer *_pp, char *_file, size_t _size);
89 static int readjob(struct printer *_pp);
93 recvjob(const char *printer)
97 struct printer myprinter, *pp = &myprinter;
100 * Perform lookup for printer name or abbreviation
103 status = getprintcap(printer, pp);
106 frecverr("cannot open printer description file");
108 case PCAPERR_NOTFOUND:
109 frecverr("unknown printer %s", printer);
112 fatal(pp, "potential reference loop detected in printcap file");
117 (void) close(STDERR_FILENO); /* set up log file */
118 if (open(pp->log_file, O_WRONLY|O_APPEND, 0664) < 0) {
119 syslog(LOG_ERR, "%s: %m", pp->log_file);
120 (void) open(_PATH_DEVNULL, O_WRONLY);
123 if (chdir(pp->spool_dir) < 0)
124 frecverr("%s: chdir(%s): %s", pp->printer, pp->spool_dir,
126 if (stat(pp->lock_file, &stb) == 0) {
127 if (stb.st_mode & 010) {
128 /* queue is disabled */
129 putchar('\1'); /* return error code */
132 } else if (stat(pp->spool_dir, &stb) < 0)
133 frecverr("%s: stat(%s): %s", pp->printer, pp->spool_dir,
135 minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */
136 signal(SIGTERM, rcleanup);
137 signal(SIGPIPE, rcleanup);
144 * Read printer jobs sent by lpd and copy them to the spooling directory.
145 * Return the number of jobs successfully transferred.
148 readjob(struct printer *pp)
152 char *cp, *clastp, *errmsg;
153 char givenid[32], givenhost[MAXHOSTNAMELEN];
160 * Read a command to tell us what to do
163 clastp = line + sizeof(line) - 1;
165 size = read(STDOUT_FILENO, cp, (size_t)1);
166 if (size != (ssize_t)1) {
167 if (size < (ssize_t)0) {
168 frecverr("%s: lost connection",
174 } while ((*cp++ != '\n') && (cp <= clastp));
176 frecverr("%s: readjob overflow", pp->printer);
182 case '\1': /* cleanup because data sent was bad */
186 case '\2': /* read cf file */
189 while (*cp >= '0' && *cp <= '9')
190 size = size * 10 + (*cp++ - '0');
194 * host name has been authenticated, we use our
195 * view of the host name since we may be passed
196 * something different than what gethostbyaddr()
199 strlcpy(cp + 6, from_host, sizeof(line)
200 + (size_t)(line - cp - 6));
201 if (strchr(cp, '/')) {
202 frecverr("readjob: %s: illegal path name", cp);
205 strlcpy(tfname, cp, sizeof(tfname));
206 tfname[sizeof (tfname) - 1] = '\0';
208 if (!chksize(size)) {
209 (void) write(STDOUT_FILENO, "\2", (size_t)1);
212 if (!readfile(pp, tfname, (size_t)size)) {
216 errmsg = ctl_renametf(pp->printer, tfname);
218 if (errmsg != NULL) {
219 frecverr("%s: %s", pp->printer, errmsg);
225 case '\3': /* read df file */
229 while (*cp >= '0' && *cp <= '9')
230 size = size * 10 + (*cp++ - '0');
233 if (strchr(cp, '/')) {
234 frecverr("readjob: %s: illegal path name", cp);
237 if (!chksize(size)) {
238 (void) write(STDOUT_FILENO, "\2", (size_t)1);
241 strlcpy(dfname, cp, sizeof(dfname));
243 trstat_init(pp, dfname, dfcnt);
244 (void) readfile(pp, dfname, (size_t)size);
245 trstat_write(pp, TR_RECVING, (size_t)size, givenid,
246 from_host, givenhost);
249 frecverr("protocol screwup: %s", line);
255 * Read files send by lpd and copy them to the spooling directory.
258 readfile(struct printer *pp, char *file, size_t size)
261 char buf[SPL_BUFSIZ];
265 fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
267 frecverr("%s: readfile: error on open(%s): %s",
268 pp->printer, file, strerror(errno));
273 for (i = 0; i < size; i += SPL_BUFSIZ) {
279 j = read(STDOUT_FILENO, cp, amt);
281 frecverr("%s: lost connection", pp->printer);
290 if (write(fd, buf, amt) != (ssize_t)amt) {
297 frecverr("%s: write error on close(%s)", pp->printer, file);
300 if (noresponse()) { /* file sent had bad data in it */
301 if (strchr(file, '/') == NULL)
314 if (read(STDOUT_FILENO, &resp, (size_t)1) != 1) {
315 frecverr("lost connection in noresponse()");
324 * Check to see if there is enough space on the disk for size bytes.
325 * 1 == OK, 0 == Not OK.
333 if (statfs(".", &sfb) < 0) {
334 syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
337 spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
338 size = (size + 511) / 512;
339 if (minfree + size > spacefree)
345 read_number(const char *fn)
350 if ((fp = fopen(fn, "r")) == NULL)
352 if (fgets(lin, sizeof(lin), fp) == NULL) {
361 * Remove all the files associated with the current job being transferred.
364 rcleanup(int signo __unused)
366 if (tfname[0] && strchr(tfname, '/') == NULL)
367 (void) unlink(tfname);
368 if (dfname[0] && strchr(dfname, '/') == NULL) {
371 (void) unlink(dfname);
372 while (dfname[2]-- != 'A');
374 } while (dfname[0]-- != 'd');
382 frecverr(const char *msg, ...)
386 syslog(LOG_ERR, "Error receiving job from %s:", from_host);
387 vsyslog(LOG_ERR, msg, ap);
390 * rcleanup is not called until AFTER logging the error message,
391 * because rcleanup will zap some variables which may have been
392 * supplied as parameters for that msg...
396 * Add a minimal delay before returning the final error code to
397 * the sending host. This just in case that machine responds
398 * this error by INSTANTLY retrying (and instantly re-failing...).
399 * It would be stupid of the sending host to do that, but if there
400 * was a broken implementation which did it, the result might be
401 * obscure performance problems and a flood of syslog messages on
402 * the receiving host.
404 sleep(2); /* a paranoid throttling measure */
405 putchar('\1'); /* return error code */