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