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