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