xref: /csrg-svn/usr.sbin/lpr/lpd/recvjob.c (revision 27086)
122438Sdist /*
222438Sdist  * Copyright (c) 1983 Regents of the University of California.
322438Sdist  * All rights reserved.  The Berkeley software License Agreement
422438Sdist  * specifies the terms and conditions for redistribution.
522438Sdist  */
622438Sdist 
713955Ssam #ifndef lint
8*27086Sbloom static char sccsid[] = "@(#)recvjob.c	5.3 (Berkeley) 04/15/86";
922438Sdist #endif not lint
1013955Ssam 
1112115Sralph /*
1212115Sralph  * Receive printer jobs from the network, queue them and
1312115Sralph  * start the printer daemon.
1412115Sralph  */
1512115Sralph 
1612115Sralph #include "lp.h"
1716763Sralph #include <sys/fs.h>
1812115Sralph 
1916763Sralph char	*sp = "";
2016763Sralph #define ack()	(void) write(1, sp, 1);
2112115Sralph 
2216763Sralph char    tfname[40];		/* tmp copy of cf before linking */
2316763Sralph char    *dfname;		/* data files */
2416763Sralph int	minfree;		/* keep at least minfree blocks available */
2516763Sralph char	*ddev;			/* disk device (for checking free space) */
2616763Sralph int	dfd;			/* file system device descriptor */
2716763Sralph 
2816763Sralph char	*find_dev();
2916763Sralph 
3012115Sralph recvjob()
3112115Sralph {
3212464Sralph 	struct stat stb;
3312115Sralph 	char *bp = pbuf;
3416763Sralph 	int status, rcleanup();
3512115Sralph 
3612115Sralph 	/*
3712115Sralph 	 * Perform lookup for printer name or abbreviation
3812115Sralph 	 */
3912115Sralph 	if ((status = pgetent(line, printer)) < 0)
4016763Sralph 		frecverr("cannot open printer description file");
4112115Sralph 	else if (status == 0)
4216763Sralph 		frecverr("unknown printer %s", printer);
4312115Sralph 	if ((LF = pgetstr("lf", &bp)) == NULL)
4412115Sralph 		LF = DEFLOGF;
4512115Sralph 	if ((SD = pgetstr("sd", &bp)) == NULL)
4612115Sralph 		SD = DEFSPOOL;
4712464Sralph 	if ((LO = pgetstr("lo", &bp)) == NULL)
4812464Sralph 		LO = DEFLOCK;
4912115Sralph 
5025498Seric 	(void) close(2);			/* set up log file */
5125498Seric 	if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) {
5225498Seric 		syslog(LOG_ERR, "%s: %m", LF);
5325498Seric 		(void) open("/dev/null", O_WRONLY);
5425498Seric 	}
5525498Seric 
5612115Sralph 	if (chdir(SD) < 0)
5716763Sralph 		frecverr("%s: %s: %m", printer, SD);
5816763Sralph 	if (stat(LO, &stb) == 0) {
5916763Sralph 		if (stb.st_mode & 010) {
6016763Sralph 			/* queue is disabled */
6116763Sralph 			putchar('\1');		/* return error code */
6216763Sralph 			exit(1);
6316763Sralph 		}
6416763Sralph 	} else if (stat(SD, &stb) < 0)
6516763Sralph 		frecverr("%s: %s: %m", printer, SD);
6616763Sralph 	minfree = read_number("minfree");
6716763Sralph 	ddev = find_dev(stb.st_dev, S_IFBLK);
6816763Sralph 	if ((dfd = open(ddev, O_RDONLY)) < 0)
6916763Sralph 		syslog(LOG_WARNING, "%s: %s: %m", printer, ddev);
7016763Sralph 	signal(SIGTERM, rcleanup);
7116763Sralph 	signal(SIGPIPE, rcleanup);
7212115Sralph 
7312115Sralph 	if (readjob())
7412115Sralph 		printjob();
7512115Sralph }
7612115Sralph 
7716763Sralph char *
7816763Sralph find_dev(dev, type)
7916763Sralph 	register dev_t dev;
8016763Sralph 	register int type;
8116763Sralph {
8216763Sralph 	register DIR *dfd = opendir("/dev");
8316763Sralph 	struct direct *dir;
8416763Sralph 	struct stat stb;
8516763Sralph 	char devname[MAXNAMLEN+6];
8616763Sralph 	char *dp;
8712115Sralph 
8816763Sralph 	strcpy(devname, "/dev/");
8916763Sralph 	while ((dir = readdir(dfd))) {
9016763Sralph 		strcpy(devname + 5, dir->d_name);
9116763Sralph 		if (stat(devname, &stb))
9216763Sralph 			continue;
9316763Sralph 		if ((stb.st_mode & S_IFMT) != type)
9416763Sralph 			continue;
9516763Sralph 		if (dev == stb.st_rdev) {
9616763Sralph 			closedir(dfd);
9716763Sralph 			dp = (char *)malloc(strlen(devname)+1);
9816763Sralph 			strcpy(dp, devname);
9916763Sralph 			return(dp);
10016763Sralph 		}
10116763Sralph 	}
10216763Sralph 	closedir(dfd);
10316763Sralph 	frecverr("cannot find device %d, %d", major(dev), minor(dev));
10416763Sralph 	/*NOTREACHED*/
10516763Sralph }
10616763Sralph 
10712115Sralph /*
10812115Sralph  * Read printer jobs sent by lpd and copy them to the spooling directory.
10912115Sralph  * Return the number of jobs successfully transfered.
11012115Sralph  */
11116763Sralph readjob()
11212115Sralph {
11312115Sralph 	register int size, nfiles;
11412115Sralph 	register char *cp;
11512115Sralph 
11612115Sralph 	ack();
11712115Sralph 	nfiles = 0;
11812115Sralph 	for (;;) {
11912115Sralph 		/*
12012115Sralph 		 * Read a command to tell us what to do
12112115Sralph 		 */
12212115Sralph 		cp = line;
12312115Sralph 		do {
12412115Sralph 			if ((size = read(1, cp, 1)) != 1) {
12512115Sralph 				if (size < 0)
12616763Sralph 					frecverr("%s: Lost connection",printer);
12712115Sralph 				return(nfiles);
12812115Sralph 			}
12912115Sralph 		} while (*cp++ != '\n');
13012115Sralph 		*--cp = '\0';
13112115Sralph 		cp = line;
13212115Sralph 		switch (*cp++) {
13312115Sralph 		case '\1':	/* cleanup because data sent was bad */
13416763Sralph 			rcleanup();
13512115Sralph 			continue;
13612115Sralph 
13712115Sralph 		case '\2':	/* read cf file */
13812115Sralph 			size = 0;
13912115Sralph 			while (*cp >= '0' && *cp <= '9')
14012115Sralph 				size = size * 10 + (*cp++ - '0');
14112115Sralph 			if (*cp++ != ' ')
14212115Sralph 				break;
143*27086Sbloom 			/*
144*27086Sbloom 			 * host name has been authenticated, we use our
145*27086Sbloom 			 * view of the host name since we may be passed
146*27086Sbloom 			 * something different than what gethostbyaddr()
147*27086Sbloom 			 * returns
148*27086Sbloom 			 */
149*27086Sbloom 			strcpy(cp + 6, from);
15012115Sralph 			strcpy(tfname, cp);
15112115Sralph 			tfname[0] = 't';
15216763Sralph 			if (!chksize(size)) {
15316763Sralph 				(void) write(1, "\2", 1);
15416763Sralph 				continue;
15516763Sralph 			}
15612115Sralph 			if (!readfile(tfname, size)) {
15716763Sralph 				rcleanup();
15812115Sralph 				continue;
15912115Sralph 			}
16012115Sralph 			if (link(tfname, cp) < 0)
16116763Sralph 				frecverr("%s: %m", tfname);
16212115Sralph 			(void) unlink(tfname);
16312115Sralph 			tfname[0] = '\0';
16412115Sralph 			nfiles++;
16512115Sralph 			continue;
16612115Sralph 
16712115Sralph 		case '\3':	/* read df file */
16812115Sralph 			size = 0;
16912115Sralph 			while (*cp >= '0' && *cp <= '9')
17012115Sralph 				size = size * 10 + (*cp++ - '0');
17112115Sralph 			if (*cp++ != ' ')
17212115Sralph 				break;
17316763Sralph 			if (!chksize(size)) {
17416763Sralph 				(void) write(1, "\2", 1);
17516763Sralph 				continue;
17616763Sralph 			}
17712115Sralph 			(void) readfile(dfname = cp, size);
17812115Sralph 			continue;
17912115Sralph 		}
18016763Sralph 		frecverr("protocol screwup");
18112115Sralph 	}
18212115Sralph }
18312115Sralph 
18412115Sralph /*
18512115Sralph  * Read files send by lpd and copy them to the spooling directory.
18612115Sralph  */
18712115Sralph readfile(file, size)
18812115Sralph 	char *file;
18912115Sralph 	int size;
19012115Sralph {
19112115Sralph 	register char *cp;
19212115Sralph 	char buf[BUFSIZ];
19312115Sralph 	register int i, j, amt;
19412115Sralph 	int fd, err;
19512115Sralph 
19613149Ssam 	fd = open(file, O_WRONLY|O_CREAT, FILMOD);
19712115Sralph 	if (fd < 0)
19816763Sralph 		frecverr("%s: %m", file);
19912115Sralph 	ack();
20012115Sralph 	err = 0;
20112115Sralph 	for (i = 0; i < size; i += BUFSIZ) {
20212115Sralph 		amt = BUFSIZ;
20312115Sralph 		cp = buf;
20412115Sralph 		if (i + amt > size)
20512115Sralph 			amt = size - i;
20612115Sralph 		do {
20712115Sralph 			j = read(1, cp, amt);
20812115Sralph 			if (j <= 0)
20916763Sralph 				frecverr("Lost connection");
21012115Sralph 			amt -= j;
21112115Sralph 			cp += j;
21212115Sralph 		} while (amt > 0);
21312115Sralph 		amt = BUFSIZ;
21412115Sralph 		if (i + amt > size)
21512115Sralph 			amt = size - i;
21613170Sralph 		if (write(fd, buf, amt) != amt) {
21712115Sralph 			err++;
21813170Sralph 			break;
21913170Sralph 		}
22012115Sralph 	}
22112115Sralph 	(void) close(fd);
22212115Sralph 	if (err)
22316763Sralph 		frecverr("%s: write error", file);
22412115Sralph 	if (noresponse()) {		/* file sent had bad data in it */
22512115Sralph 		(void) unlink(file);
22612115Sralph 		return(0);
22712115Sralph 	}
22812115Sralph 	ack();
22912115Sralph 	return(1);
23012115Sralph }
23112115Sralph 
23212115Sralph noresponse()
23312115Sralph {
23412115Sralph 	char resp;
23512115Sralph 
23612115Sralph 	if (read(1, &resp, 1) != 1)
23716763Sralph 		frecverr("Lost connection");
23812115Sralph 	if (resp == '\0')
23912115Sralph 		return(0);
24012115Sralph 	return(1);
24112115Sralph }
24212115Sralph 
24312115Sralph /*
24416763Sralph  * Check to see if there is enough space on the disk for size bytes.
24516763Sralph  * 1 == OK, 0 == Not OK.
24616763Sralph  */
24716763Sralph chksize(size)
24816763Sralph 	int size;
24916763Sralph {
25016763Sralph 	struct stat stb;
25116763Sralph 	register char *ddev;
25216763Sralph 	int spacefree;
25316763Sralph 	struct fs fs;
25416763Sralph 
25516763Sralph 	if (dfd < 0 || lseek(dfd, (long)(SBLOCK * DEV_BSIZE), 0) < 0)
25616763Sralph 		return(1);
25716763Sralph 	if (read(dfd, (char *)&fs, sizeof fs) != sizeof fs)
25816763Sralph 		return(1);
25916763Sralph 	spacefree = (fs.fs_cstotal.cs_nbfree * fs.fs_frag +
26016763Sralph 		fs.fs_cstotal.cs_nffree - fs.fs_dsize * fs.fs_minfree / 100) *
26116763Sralph 			fs.fs_fsize / 1024;
26216763Sralph 	size = (size + 1023) / 1024;
26316763Sralph 	if (minfree + size > spacefree)
26416763Sralph 		return(0);
26516763Sralph 	return(1);
26616763Sralph }
26716763Sralph 
26816763Sralph read_number(fn)
26916763Sralph 	char *fn;
27016763Sralph {
27116763Sralph 	char lin[80];
27216763Sralph 	register FILE *fp;
27316763Sralph 
27416763Sralph 	if ((fp = fopen(fn, "r")) == NULL)
27516763Sralph 		return (0);
27616763Sralph 	if (fgets(lin, 80, fp) == NULL) {
27716763Sralph 		fclose(fp);
27816763Sralph 		return (0);
27916763Sralph 	}
28016763Sralph 	fclose(fp);
28116763Sralph 	return (atoi(lin));
28216763Sralph }
28316763Sralph 
28416763Sralph /*
28512115Sralph  * Remove all the files associated with the current job being transfered.
28612115Sralph  */
28716763Sralph rcleanup()
28812115Sralph {
28912115Sralph 
29012115Sralph 	if (tfname[0])
29112115Sralph 		(void) unlink(tfname);
29212115Sralph 	if (dfname)
29312115Sralph 		do {
29412115Sralph 			do
29512115Sralph 				(void) unlink(dfname);
29616938Sralph 			while (dfname[2]-- != 'A');
29716938Sralph 			dfname[2] = 'z';
29816938Sralph 		} while (dfname[0]-- != 'd');
29912115Sralph }
30012115Sralph 
30116763Sralph frecverr(msg, a1, a2)
30212115Sralph 	char *msg;
30312115Sralph {
30416763Sralph 	rcleanup();
30516763Sralph 	syslog(LOG_ERR, msg, a1, a2);
30612115Sralph 	putchar('\1');		/* return error code */
30712115Sralph 	exit(1);
30812115Sralph }
309