printjob.c revision 78300
11553Srgrimes/*
21553Srgrimes * Copyright (c) 1983, 1993
31553Srgrimes *	The Regents of the University of California.  All rights reserved.
41553Srgrimes *
51553Srgrimes *
61553Srgrimes * Redistribution and use in source and binary forms, with or without
71553Srgrimes * modification, are permitted provided that the following conditions
81553Srgrimes * are met:
91553Srgrimes * 1. Redistributions of source code must retain the above copyright
101553Srgrimes *    notice, this list of conditions and the following disclaimer.
111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
121553Srgrimes *    notice, this list of conditions and the following disclaimer in the
131553Srgrimes *    documentation and/or other materials provided with the distribution.
141553Srgrimes * 3. All advertising materials mentioning features or use of this software
151553Srgrimes *    must display the following acknowledgement:
161553Srgrimes *	This product includes software developed by the University of
171553Srgrimes *	California, Berkeley and its contributors.
181553Srgrimes * 4. Neither the name of the University nor the names of its contributors
191553Srgrimes *    may be used to endorse or promote products derived from this software
201553Srgrimes *    without specific prior written permission.
211553Srgrimes *
221553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251553Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321553Srgrimes * SUCH DAMAGE.
331553Srgrimes */
341553Srgrimes
351553Srgrimes#ifndef lint
3631492Swollmanstatic const char copyright[] =
371553Srgrimes"@(#) Copyright (c) 1983, 1993\n\
381553Srgrimes	The Regents of the University of California.  All rights reserved.\n";
391553Srgrimes#endif /* not lint */
401553Srgrimes
411553Srgrimes#ifndef lint
4231492Swollman/*
4315648Sjoergstatic char sccsid[] = "@(#)printjob.c	8.7 (Berkeley) 5/10/95";
4431492Swollman*/
4531492Swollmanstatic const char rcsid[] =
4650479Speter  "$FreeBSD: head/usr.sbin/lpr/lpd/printjob.c 78300 2001-06-15 22:03:07Z gad $";
471553Srgrimes#endif /* not lint */
481553Srgrimes
491553Srgrimes
501553Srgrimes/*
511553Srgrimes * printjob -- print jobs in the queue.
521553Srgrimes *
531553Srgrimes *	NOTE: the lock file is used to pass information to lpq and lprm.
541553Srgrimes *	it does not need to be removed because file locks are dynamic.
551553Srgrimes */
561553Srgrimes
571553Srgrimes#include <sys/param.h>
581553Srgrimes#include <sys/wait.h>
591553Srgrimes#include <sys/stat.h>
601553Srgrimes#include <sys/types.h>
611553Srgrimes
621553Srgrimes#include <pwd.h>
631553Srgrimes#include <unistd.h>
641553Srgrimes#include <signal.h>
651553Srgrimes#include <syslog.h>
661553Srgrimes#include <fcntl.h>
671553Srgrimes#include <dirent.h>
681553Srgrimes#include <errno.h>
691553Srgrimes#include <stdio.h>
701553Srgrimes#include <string.h>
711553Srgrimes#include <stdlib.h>
7215032Ssef#include <sys/ioctl.h>
7315032Ssef#include <termios.h>
7415703Sjoerg#include <time.h>
751553Srgrimes#include "lp.h"
761553Srgrimes#include "lp.local.h"
771553Srgrimes#include "pathnames.h"
781553Srgrimes#include "extern.h"
791553Srgrimes
801553Srgrimes#define DORETURN	0	/* absorb fork error */
811553Srgrimes#define DOABORT		1	/* abort if dofork fails */
821553Srgrimes
831553Srgrimes/*
841553Srgrimes * Error tokens
851553Srgrimes */
861553Srgrimes#define REPRINT		-2
871553Srgrimes#define ERROR		-1
881553Srgrimes#define	OK		0
891553Srgrimes#define	FATALERR	1
901553Srgrimes#define	NOACCT		2
911553Srgrimes#define	FILTERERR	3
921553Srgrimes#define	ACCESS		4
931553Srgrimes
941553Srgrimesstatic dev_t	 fdev;		/* device of file pointed to by symlink */
951553Srgrimesstatic ino_t	 fino;		/* inode of file pointed to by symlink */
961553Srgrimesstatic FILE	*cfp;		/* control file */
971553Srgrimesstatic int	 child;		/* id of any filters */
9868253Sgadstatic int	 job_dfcnt;	/* count of datafiles in current user job */
991553Srgrimesstatic int	 lfd;		/* lock file descriptor */
1001553Srgrimesstatic int	 ofd;		/* output filter file descriptor */
1011553Srgrimesstatic int	 ofilter;	/* id of output filter, if any */
10224831Sbrianstatic int	 tfd = -1;	/* output filter temp file output */
1031553Srgrimesstatic int	 pfd;		/* prstatic inter file descriptor */
1041553Srgrimesstatic int	 pid;		/* pid of lpd process */
1051553Srgrimesstatic int	 prchild;	/* id of pr process */
1061553Srgrimesstatic char	 title[80];	/* ``pr'' title */
10753956Sachestatic char      locale[80];    /* ``pr'' locale */
1081553Srgrimes
1091553Srgrimesstatic char	class[32];		/* classification field */
11078300Sgadstatic char	origin_host[MAXHOSTNAMELEN];	/* user's host machine */
1111553Srgrimes				/* indentation size in static characters */
1128857Srgrimesstatic char	indent[10] = "-i0";
1131553Srgrimesstatic char	jobname[100];		/* job or file name */
1141553Srgrimesstatic char	length[10] = "-l";	/* page length in lines */
1151553Srgrimesstatic char	logname[32];		/* user's login name */
1161553Srgrimesstatic char	pxlength[10] = "-y";	/* page length in pixels */
1171553Srgrimesstatic char	pxwidth[10] = "-x";	/* page width in pixels */
11868664Sgad/* tempstderr is the filename used to catch stderr from exec-ing filters */
11968664Sgadstatic char	tempstderr[] = "errs.XXXXXXX";
1201553Srgrimesstatic char	width[10] = "-w";	/* page width in static characters */
12124831Sbrian#define TFILENAME "fltXXXXXX"
12224831Sbrianstatic char	tfile[] = TFILENAME;	/* file name for filter output */
1231553Srgrimes
12478146Sgadstatic void	 abortpr(int _signo);
12578146Sgadstatic void	 alarmhandler(int _signo);
12678146Sgadstatic void	 banner(struct printer *_pp, char *_name1, char *_name2);
12778146Sgadstatic int	 dofork(const struct printer *_pp, int _action);
12878146Sgadstatic int	 dropit(int _c);
12978146Sgadstatic void	 init(struct printer *_pp);
13078146Sgadstatic void	 openpr(const struct printer *_pp);
13178146Sgadstatic void	 opennet(const struct printer *_pp);
13278146Sgadstatic void	 opentty(const struct printer *_pp);
13378146Sgadstatic void	 openrem(const struct printer *pp);
13478146Sgadstatic int	 print(struct printer *_pp, int _format, char *_file);
13578146Sgadstatic int	 printit(struct printer *_pp, char *_file);
13678146Sgadstatic void	 pstatus(const struct printer *_pp, const char *_msg, ...);
13778146Sgadstatic char	 response(const struct printer *_pp);
13878146Sgadstatic void	 scan_out(struct printer *_pp, int _scfd, char *_scsp,
13978146Sgad		    int _dlm);
14078146Sgadstatic char	*scnline(int _key, char *_p, int _c);
14178146Sgadstatic int	 sendfile(struct printer *_pp, int _type, char *_file,
14278146Sgad		    char _format);
14378146Sgadstatic int	 sendit(struct printer *_pp, char *_file);
14478146Sgadstatic void	 sendmail(struct printer *_pp, char *_user, int _bombed);
14578146Sgadstatic void	 setty(const struct printer *_pp);
1461553Srgrimes
1471553Srgrimesvoid
14878146Sgadprintjob(struct printer *pp)
1491553Srgrimes{
1501553Srgrimes	struct stat stb;
15168401Sgad	register struct jobqueue *q, **qp;
15268401Sgad	struct jobqueue **queue;
1531553Srgrimes	register int i, nitems;
15468733Sgad	off_t pidoff;
15568733Sgad	int errcnt, jobcount, tempfd;
1561553Srgrimes
15768733Sgad	jobcount = 0;
15831492Swollman	init(pp); /* set up capabilities */
15931492Swollman	(void) write(1, "", 1);	/* ack that daemon is started */
1601553Srgrimes	(void) close(2);			/* set up log file */
16131492Swollman	if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
16231492Swollman		syslog(LOG_ERR, "%s: %m", pp->log_file);
1631553Srgrimes		(void) open(_PATH_DEVNULL, O_WRONLY);
1641553Srgrimes	}
1651553Srgrimes	setgid(getegid());
1661553Srgrimes	pid = getpid();				/* for use with lprm */
1671553Srgrimes	setpgrp(0, pid);
1681553Srgrimes	signal(SIGHUP, abortpr);
1691553Srgrimes	signal(SIGINT, abortpr);
1701553Srgrimes	signal(SIGQUIT, abortpr);
1711553Srgrimes	signal(SIGTERM, abortpr);
1721553Srgrimes
1731553Srgrimes	/*
1741553Srgrimes	 * uses short form file names
1751553Srgrimes	 */
17631492Swollman	if (chdir(pp->spool_dir) < 0) {
17731492Swollman		syslog(LOG_ERR, "%s: %m", pp->spool_dir);
1781553Srgrimes		exit(1);
1791553Srgrimes	}
18031492Swollman	if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS))
1811553Srgrimes		exit(0);		/* printing disabled */
18231492Swollman	lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
18331492Swollman		   LOCK_FILE_MODE);
1841553Srgrimes	if (lfd < 0) {
18531492Swollman		if (errno == EWOULDBLOCK)	/* active daemon present */
18631492Swollman			exit(0);
18731492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
1881553Srgrimes		exit(1);
1891553Srgrimes	}
19031492Swollman	/* turn off non-blocking mode (was turned on for lock effects only) */
19131492Swollman	if (fcntl(lfd, F_SETFL, 0) < 0) {
19231492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
1931553Srgrimes		exit(1);
1941553Srgrimes	}
1951553Srgrimes	ftruncate(lfd, 0);
1961553Srgrimes	/*
1971553Srgrimes	 * write process id for others to know
1981553Srgrimes	 */
1991553Srgrimes	sprintf(line, "%u\n", pid);
2001553Srgrimes	pidoff = i = strlen(line);
2011553Srgrimes	if (write(lfd, line, i) != i) {
20231492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
2031553Srgrimes		exit(1);
2041553Srgrimes	}
2051553Srgrimes	/*
2061553Srgrimes	 * search the spool directory for work and sort by queue order.
2071553Srgrimes	 */
20831492Swollman	if ((nitems = getq(pp, &queue)) < 0) {
20931492Swollman		syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
21031492Swollman		       pp->spool_dir);
2111553Srgrimes		exit(1);
2121553Srgrimes	}
2131553Srgrimes	if (nitems == 0)		/* no work to do */
2141553Srgrimes		exit(0);
21531492Swollman	if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
21631492Swollman		if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
21731492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
21831492Swollman			       pp->lock_file);
2191553Srgrimes	}
22068664Sgad
22168664Sgad	/* create a file which will be used to hold stderr from filters */
22268664Sgad	if ((tempfd = mkstemp(tempstderr)) == -1) {
22368664Sgad		syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
22468664Sgad		       tempstderr);
22568732Sgad		exit(1);
22668664Sgad	}
22768664Sgad	if ((i = fchmod(tempfd, 0664)) == -1) {
22868664Sgad		syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
22968664Sgad		       tempstderr);
23068732Sgad		exit(1);
23168664Sgad	}
23268664Sgad	/* lpd doesn't need it to be open, it just needs it to exist */
23368664Sgad	close(tempfd);
23468664Sgad
23531492Swollman	openpr(pp);			/* open printer or remote */
2361553Srgrimesagain:
2371553Srgrimes	/*
2381553Srgrimes	 * we found something to do now do it --
2391553Srgrimes	 *    write the name of the current control file into the lock file
2401553Srgrimes	 *    so the spool queue program can tell what we're working on
2411553Srgrimes	 */
2421553Srgrimes	for (qp = queue; nitems--; free((char *) q)) {
2431553Srgrimes		q = *qp++;
24468401Sgad		if (stat(q->job_cfname, &stb) < 0)
2451553Srgrimes			continue;
24615648Sjoerg		errcnt = 0;
2471553Srgrimes	restart:
24815648Sjoerg		(void) lseek(lfd, pidoff, 0);
24968401Sgad		(void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
2501553Srgrimes		i = strlen(line);
2511553Srgrimes		if (write(lfd, line, i) != i)
25231492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
25331492Swollman			       pp->lock_file);
25431492Swollman		if (!pp->remote)
25568401Sgad			i = printit(pp, q->job_cfname);
2561553Srgrimes		else
25768401Sgad			i = sendit(pp, q->job_cfname);
2581553Srgrimes		/*
2591553Srgrimes		 * Check to see if we are supposed to stop printing or
2601553Srgrimes		 * if we are to rebuild the queue.
2611553Srgrimes		 */
2621553Srgrimes		if (fstat(lfd, &stb) == 0) {
2631553Srgrimes			/* stop printing before starting next job? */
26431492Swollman			if (stb.st_mode & LFM_PRINT_DIS)
2651553Srgrimes				goto done;
2661553Srgrimes			/* rebuild queue (after lpc topq) */
26731492Swollman			if (stb.st_mode & LFM_RESET_QUE) {
26831492Swollman				for (free(q); nitems--; free(q))
2691553Srgrimes					q = *qp++;
27031492Swollman				if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
27131492Swollman				    < 0)
2721553Srgrimes					syslog(LOG_WARNING, "%s: %s: %m",
27331492Swollman					       pp->printer, pp->lock_file);
2741553Srgrimes				break;
2751553Srgrimes			}
2761553Srgrimes		}
27768733Sgad		if (i == OK)		/* all files of this job printed */
27868733Sgad			jobcount++;
27915648Sjoerg		else if (i == REPRINT && ++errcnt < 5) {
28015648Sjoerg			/* try reprinting the job */
28131492Swollman			syslog(LOG_INFO, "restarting %s", pp->printer);
2821553Srgrimes			if (ofilter > 0) {
2831553Srgrimes				kill(ofilter, SIGCONT);	/* to be sure */
2841553Srgrimes				(void) close(ofd);
28515648Sjoerg				while ((i = wait(NULL)) > 0 && i != ofilter)
2861553Srgrimes					;
2871553Srgrimes				ofilter = 0;
2881553Srgrimes			}
2891553Srgrimes			(void) close(pfd);	/* close printer */
2901553Srgrimes			if (ftruncate(lfd, pidoff) < 0)
29131492Swollman				syslog(LOG_WARNING, "%s: %s: %m",
29231492Swollman				       pp->printer, pp->lock_file);
29331492Swollman			openpr(pp);		/* try to reopen printer */
2941553Srgrimes			goto restart;
29515648Sjoerg		} else {
29631492Swollman			syslog(LOG_WARNING, "%s: job could not be %s (%s)",
29731492Swollman			       pp->printer,
29831492Swollman			       pp->remote ? "sent to remote host" : "printed",
29968401Sgad			       q->job_cfname);
30015648Sjoerg			if (i == REPRINT) {
30127748Simp				/* ensure we don't attempt this job again */
30268401Sgad				(void) unlink(q->job_cfname);
30368401Sgad				q->job_cfname[0] = 'd';
30468401Sgad				(void) unlink(q->job_cfname);
30515648Sjoerg				if (logname[0])
30631492Swollman					sendmail(pp, logname, FATALERR);
30715648Sjoerg			}
3081553Srgrimes		}
3091553Srgrimes	}
31031492Swollman	free(queue);
3111553Srgrimes	/*
3121553Srgrimes	 * search the spool directory for more work.
3131553Srgrimes	 */
31431492Swollman	if ((nitems = getq(pp, &queue)) < 0) {
31531492Swollman		syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
31631492Swollman		       pp->spool_dir);
3171553Srgrimes		exit(1);
3181553Srgrimes	}
3191553Srgrimes	if (nitems == 0) {		/* no more work to do */
3201553Srgrimes	done:
32168733Sgad		if (jobcount > 0) {	/* jobs actually printed */
32231492Swollman			if (!pp->no_formfeed && !pp->tof)
32331492Swollman				(void) write(ofd, pp->form_feed,
32431492Swollman					     strlen(pp->form_feed));
32531492Swollman			if (pp->trailer != NULL) /* output trailer */
32631492Swollman				(void) write(ofd, pp->trailer,
32731492Swollman					     strlen(pp->trailer));
3281553Srgrimes		}
32919202Simp		(void) close(ofd);
33019202Simp		(void) wait(NULL);
33168664Sgad		(void) unlink(tempstderr);
3321553Srgrimes		exit(0);
3331553Srgrimes	}
3341553Srgrimes	goto again;
3351553Srgrimes}
3361553Srgrimes
3371553Srgrimeschar	fonts[4][50];	/* fonts for troff */
3381553Srgrimes
3391553Srgrimeschar ifonts[4][40] = {
3401553Srgrimes	_PATH_VFONTR,
3411553Srgrimes	_PATH_VFONTI,
3421553Srgrimes	_PATH_VFONTB,
3431553Srgrimes	_PATH_VFONTS,
3441553Srgrimes};
3451553Srgrimes
3461553Srgrimes/*
3471553Srgrimes * The remaining part is the reading of the control file (cf)
3481553Srgrimes * and performing the various actions.
3491553Srgrimes */
3501553Srgrimesstatic int
35178146Sgadprintit(struct printer *pp, char *file)
3521553Srgrimes{
3531553Srgrimes	register int i;
35468734Sgad	char *cp;
35568734Sgad	int bombed, didignorehdr;
3561553Srgrimes
35768734Sgad	bombed = OK;
35868734Sgad	didignorehdr = 0;
3591553Srgrimes	/*
3601553Srgrimes	 * open control file; ignore if no longer there.
3611553Srgrimes	 */
3621553Srgrimes	if ((cfp = fopen(file, "r")) == NULL) {
36331492Swollman		syslog(LOG_INFO, "%s: %s: %m", pp->printer, file);
3641553Srgrimes		return(OK);
3651553Srgrimes	}
3661553Srgrimes	/*
3671553Srgrimes	 * Reset troff fonts.
3681553Srgrimes	 */
3691553Srgrimes	for (i = 0; i < 4; i++)
3701553Srgrimes		strcpy(fonts[i], ifonts[i]);
37131492Swollman	sprintf(&width[2], "%ld", pp->page_width);
3721553Srgrimes	strcpy(indent+2, "0");
3731553Srgrimes
37468253Sgad	/* initialize job-specific count of datafiles processed */
37568253Sgad	job_dfcnt = 0;
37668253Sgad
3771553Srgrimes	/*
3781553Srgrimes	 *      read the control file for work to do
3791553Srgrimes	 *
3801553Srgrimes	 *      file format -- first character in the line is a command
3811553Srgrimes	 *      rest of the line is the argument.
3821553Srgrimes	 *      valid commands are:
3831553Srgrimes	 *
3841553Srgrimes	 *		S -- "stat info" for symbolic link protection
3851553Srgrimes	 *		J -- "job name" on banner page
3861553Srgrimes	 *		C -- "class name" on banner page
3871553Srgrimes	 *              L -- "literal" user's name to print on banner
3881553Srgrimes	 *		T -- "title" for pr
3891553Srgrimes	 *		H -- "host name" of machine where lpr was done
3901553Srgrimes	 *              P -- "person" user's login name
3911553Srgrimes	 *              I -- "indent" amount to indent output
39215648Sjoerg	 *		R -- laser dpi "resolution"
3931553Srgrimes	 *              f -- "file name" name of text file to print
3941553Srgrimes	 *		l -- "file name" text file with control chars
3951553Srgrimes	 *		p -- "file name" text file to print with pr(1)
3961553Srgrimes	 *		t -- "file name" troff(1) file to print
3971553Srgrimes	 *		n -- "file name" ditroff(1) file to print
3981553Srgrimes	 *		d -- "file name" dvi file to print
3991553Srgrimes	 *		g -- "file name" plot(1G) file to print
4001553Srgrimes	 *		v -- "file name" plain raster file to print
4011553Srgrimes	 *		c -- "file name" cifplot file to print
4021553Srgrimes	 *		1 -- "R font file" for troff
4031553Srgrimes	 *		2 -- "I font file" for troff
4041553Srgrimes	 *		3 -- "B font file" for troff
4051553Srgrimes	 *		4 -- "S font file" for troff
4061553Srgrimes	 *		N -- "name" of file (used by lpq)
4071553Srgrimes	 *              U -- "unlink" name of file to remove
4081553Srgrimes	 *                    (after we print it. (Pass 2 only)).
4091553Srgrimes	 *		M -- "mail" to user when done printing
41053956Sache	 *              Z -- "locale" for pr
4111553Srgrimes	 *
4121553Srgrimes	 *      getline reads a line and expands tabs to blanks
4131553Srgrimes	 */
4141553Srgrimes
4151553Srgrimes	/* pass 1 */
4161553Srgrimes
4171553Srgrimes	while (getline(cfp))
4181553Srgrimes		switch (line[0]) {
4191553Srgrimes		case 'H':
42078300Sgad			strlcpy(origin_host, line + 1, sizeof(origin_host));
42127748Simp			if (class[0] == '\0') {
42227748Simp				strncpy(class, line+1, sizeof(class) - 1);
42327748Simp				class[sizeof(class) - 1] = '\0';
42427748Simp			}
4251553Srgrimes			continue;
4261553Srgrimes
4271553Srgrimes		case 'P':
42827748Simp			strncpy(logname, line+1, sizeof(logname) - 1);
42927748Simp			logname[sizeof(logname) - 1] = '\0';
43031492Swollman			if (pp->restricted) { /* restricted */
4311553Srgrimes				if (getpwnam(logname) == NULL) {
4321553Srgrimes					bombed = NOACCT;
43331492Swollman					sendmail(pp, line+1, bombed);
4341553Srgrimes					goto pass2;
4351553Srgrimes				}
4361553Srgrimes			}
4371553Srgrimes			continue;
4381553Srgrimes
4391553Srgrimes		case 'S':
4401553Srgrimes			cp = line+1;
4411553Srgrimes			i = 0;
4421553Srgrimes			while (*cp >= '0' && *cp <= '9')
4431553Srgrimes				i = i * 10 + (*cp++ - '0');
4441553Srgrimes			fdev = i;
4451553Srgrimes			cp++;
4461553Srgrimes			i = 0;
4471553Srgrimes			while (*cp >= '0' && *cp <= '9')
4481553Srgrimes				i = i * 10 + (*cp++ - '0');
4491553Srgrimes			fino = i;
4501553Srgrimes			continue;
4511553Srgrimes
4521553Srgrimes		case 'J':
45327748Simp			if (line[1] != '\0') {
45427748Simp				strncpy(jobname, line+1, sizeof(jobname) - 1);
45527748Simp				jobname[sizeof(jobname) - 1] = '\0';
45627748Simp			} else
4571553Srgrimes				strcpy(jobname, " ");
4581553Srgrimes			continue;
4591553Srgrimes
4601553Srgrimes		case 'C':
4611553Srgrimes			if (line[1] != '\0')
46227748Simp				strncpy(class, line+1, sizeof(class) - 1);
4631553Srgrimes			else if (class[0] == '\0')
4641553Srgrimes				gethostname(class, sizeof(class));
46527748Simp			class[sizeof(class) - 1] = '\0';
4661553Srgrimes			continue;
4671553Srgrimes
4681553Srgrimes		case 'T':	/* header title for pr */
46927748Simp			strncpy(title, line+1, sizeof(title) - 1);
47027748Simp			title[sizeof(title) - 1] = '\0';
4711553Srgrimes			continue;
4721553Srgrimes
4731553Srgrimes		case 'L':	/* identification line */
47431492Swollman			if (!pp->no_header && !pp->header_last)
47531492Swollman				banner(pp, line+1, jobname);
4761553Srgrimes			continue;
4771553Srgrimes
4781553Srgrimes		case '1':	/* troff fonts */
4791553Srgrimes		case '2':
4801553Srgrimes		case '3':
4811553Srgrimes		case '4':
48227748Simp			if (line[1] != '\0') {
48327748Simp				strncpy(fonts[line[0]-'1'], line+1,
48427748Simp				    50-1);
48527748Simp				fonts[line[0]-'1'][50-1] = '\0';
48627748Simp			}
4871553Srgrimes			continue;
4881553Srgrimes
4891553Srgrimes		case 'W':	/* page width */
49027748Simp			strncpy(width+2, line+1, sizeof(width) - 3);
49127748Simp			width[2+sizeof(width) - 3] = '\0';
4921553Srgrimes			continue;
4931553Srgrimes
4941553Srgrimes		case 'I':	/* indent amount */
49527748Simp			strncpy(indent+2, line+1, sizeof(indent) - 3);
49627748Simp			indent[2+sizeof(indent) - 3] = '\0';
4971553Srgrimes			continue;
4981553Srgrimes
49953956Sache		case 'Z':       /* locale for pr */
50053956Sache			strncpy(locale, line+1, sizeof(locale) - 1);
50153956Sache			locale[sizeof(locale) - 1] = '\0';
50253956Sache			continue;
50353956Sache
5041553Srgrimes		default:	/* some file to print */
50568467Sgad			/* only lowercase cmd-codes include a file-to-print */
50668467Sgad			if ((line[0] < 'a') || (line[0] > 'z')) {
50768467Sgad				/* ignore any other lines */
50868467Sgad				if (lflag <= 1)
50968467Sgad					continue;
51068467Sgad				if (!didignorehdr) {
51168467Sgad					syslog(LOG_INFO, "%s: in %s :",
51268467Sgad					       pp->printer, file);
51368467Sgad					didignorehdr = 1;
51468467Sgad				}
51568467Sgad				syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
51668467Sgad				       pp->printer, line[0], &line[1]);
51768467Sgad				continue;
51868467Sgad			}
51968467Sgad			i = print(pp, line[0], line+1);
52068467Sgad			switch (i) {
5211553Srgrimes			case ERROR:
5221553Srgrimes				if (bombed == OK)
5231553Srgrimes					bombed = FATALERR;
5241553Srgrimes				break;
5251553Srgrimes			case REPRINT:
5261553Srgrimes				(void) fclose(cfp);
5271553Srgrimes				return(REPRINT);
5281553Srgrimes			case FILTERERR:
5291553Srgrimes			case ACCESS:
5301553Srgrimes				bombed = i;
53131492Swollman				sendmail(pp, logname, bombed);
5321553Srgrimes			}
5331553Srgrimes			title[0] = '\0';
5341553Srgrimes			continue;
5351553Srgrimes
5361553Srgrimes		case 'N':
5371553Srgrimes		case 'U':
5381553Srgrimes		case 'M':
53915648Sjoerg		case 'R':
5401553Srgrimes			continue;
5411553Srgrimes		}
5421553Srgrimes
5431553Srgrimes	/* pass 2 */
5441553Srgrimes
5451553Srgrimespass2:
5461553Srgrimes	fseek(cfp, 0L, 0);
5471553Srgrimes	while (getline(cfp))
5481553Srgrimes		switch (line[0]) {
5491553Srgrimes		case 'L':	/* identification line */
55031492Swollman			if (!pp->no_header && pp->header_last)
55131492Swollman				banner(pp, line+1, jobname);
5521553Srgrimes			continue;
5531553Srgrimes
5541553Srgrimes		case 'M':
5551553Srgrimes			if (bombed < NOACCT)	/* already sent if >= NOACCT */
55631492Swollman				sendmail(pp, line+1, bombed);
5571553Srgrimes			continue;
5581553Srgrimes
5591553Srgrimes		case 'U':
56027748Simp			if (strchr(line+1, '/'))
56127748Simp				continue;
5621553Srgrimes			(void) unlink(line+1);
5631553Srgrimes		}
5641553Srgrimes	/*
5651553Srgrimes	 * clean-up in case another control file exists
5661553Srgrimes	 */
5671553Srgrimes	(void) fclose(cfp);
5681553Srgrimes	(void) unlink(file);
5691553Srgrimes	return(bombed == OK ? OK : ERROR);
5701553Srgrimes}
5711553Srgrimes
5721553Srgrimes/*
5731553Srgrimes * Print a file.
5741553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
5751553Srgrimes * Return -1 if a non-recoverable error occured,
5761553Srgrimes * 2 if the filter detected some errors (but printed the job anyway),
5771553Srgrimes * 1 if we should try to reprint this job and
5781553Srgrimes * 0 if all is well.
5791553Srgrimes * Note: all filters take stdin as the file, stdout as the printer,
5801553Srgrimes * stderr as the log file, and must not ignore SIGINT.
5811553Srgrimes */
5821553Srgrimesstatic int
58378146Sgadprint(struct printer *pp, int format, char *file)
5841553Srgrimes{
58553956Sache	register int n, i;
5861553Srgrimes	register char *prog;
58731492Swollman	int fi, fo;
5881553Srgrimes	FILE *fp;
5891553Srgrimes	char *av[15], buf[BUFSIZ];
59068734Sgad	int pid, p[2], stopped;
5911553Srgrimes	union wait status;
5921553Srgrimes	struct stat stb;
5931553Srgrimes
59468467Sgad	if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
59568467Sgad		syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
59668467Sgad		       pp->printer, file, format);
5971553Srgrimes		return(ERROR);
59868467Sgad	}
5991553Srgrimes	/*
6001553Srgrimes	 * Check to see if data file is a symbolic link. If so, it should
6011553Srgrimes	 * still point to the same file or someone is trying to print
6021553Srgrimes	 * something he shouldn't.
6031553Srgrimes	 */
6041553Srgrimes	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
6051553Srgrimes	    (stb.st_dev != fdev || stb.st_ino != fino))
6061553Srgrimes		return(ACCESS);
60768253Sgad
60868253Sgad	job_dfcnt++;		/* increment datafile counter for this job */
60968734Sgad	stopped = 0;		/* output filter is not stopped */
61068253Sgad
61168253Sgad	/* everything seems OK, start it up */
61231492Swollman	if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
61331492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
61431492Swollman		pp->tof = 1;
6151553Srgrimes	}
61631492Swollman	if (pp->filters[LPF_INPUT] == NULL
61731492Swollman	    && (format == 'f' || format == 'l')) {
61831492Swollman		pp->tof = 0;
6191553Srgrimes		while ((n = read(fi, buf, BUFSIZ)) > 0)
6201553Srgrimes			if (write(ofd, buf, n) != n) {
6211553Srgrimes				(void) close(fi);
6221553Srgrimes				return(REPRINT);
6231553Srgrimes			}
6241553Srgrimes		(void) close(fi);
6251553Srgrimes		return(OK);
6261553Srgrimes	}
6271553Srgrimes	switch (format) {
6281553Srgrimes	case 'p':	/* print file using 'pr' */
62931492Swollman		if (pp->filters[LPF_INPUT] == NULL) {	/* use output filter */
6301553Srgrimes			prog = _PATH_PR;
63153956Sache			i = 0;
63253956Sache			av[i++] = "pr";
63353956Sache			av[i++] = width;
63453956Sache			av[i++] = length;
63553956Sache			av[i++] = "-h";
63653956Sache			av[i++] = *title ? title : " ";
63753956Sache			av[i++] = "-L";
63853956Sache			av[i++] = *locale ? locale : "C";
63953956Sache			av[i++] = "-F";
64053956Sache			av[i] = 0;
6411553Srgrimes			fo = ofd;
6421553Srgrimes			goto start;
6431553Srgrimes		}
6441553Srgrimes		pipe(p);
64531492Swollman		if ((prchild = dofork(pp, DORETURN)) == 0) {	/* child */
6461553Srgrimes			dup2(fi, 0);		/* file is stdin */
6471553Srgrimes			dup2(p[1], 1);		/* pipe is stdout */
6488094Sjkh			closelog();
64931492Swollman			closeallfds(3);
6501553Srgrimes			execl(_PATH_PR, "pr", width, length,
65153956Sache			    "-h", *title ? title : " ",
65253956Sache			    "-L", *locale ? locale : "C",
65353956Sache			    "-F", 0);
6541553Srgrimes			syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
6551553Srgrimes			exit(2);
6561553Srgrimes		}
6571553Srgrimes		(void) close(p[1]);		/* close output side */
6581553Srgrimes		(void) close(fi);
6591553Srgrimes		if (prchild < 0) {
6601553Srgrimes			prchild = 0;
6611553Srgrimes			(void) close(p[0]);
6621553Srgrimes			return(ERROR);
6631553Srgrimes		}
6641553Srgrimes		fi = p[0];			/* use pipe for input */
6651553Srgrimes	case 'f':	/* print plain text file */
66631492Swollman		prog = pp->filters[LPF_INPUT];
6671553Srgrimes		av[1] = width;
6681553Srgrimes		av[2] = length;
6691553Srgrimes		av[3] = indent;
6701553Srgrimes		n = 4;
6711553Srgrimes		break;
6721553Srgrimes	case 'l':	/* like 'f' but pass control characters */
67331492Swollman		prog = pp->filters[LPF_INPUT];
6741553Srgrimes		av[1] = "-c";
6751553Srgrimes		av[2] = width;
6761553Srgrimes		av[3] = length;
6771553Srgrimes		av[4] = indent;
6781553Srgrimes		n = 5;
6791553Srgrimes		break;
6801553Srgrimes	case 'r':	/* print a fortran text file */
68131492Swollman		prog = pp->filters[LPF_FORTRAN];
6821553Srgrimes		av[1] = width;
6831553Srgrimes		av[2] = length;
6841553Srgrimes		n = 3;
6851553Srgrimes		break;
6861553Srgrimes	case 't':	/* print troff output */
6871553Srgrimes	case 'n':	/* print ditroff output */
6881553Srgrimes	case 'd':	/* print tex output */
6891553Srgrimes		(void) unlink(".railmag");
6901553Srgrimes		if ((fo = creat(".railmag", FILMOD)) < 0) {
69131492Swollman			syslog(LOG_ERR, "%s: cannot create .railmag",
69231492Swollman			       pp->printer);
6931553Srgrimes			(void) unlink(".railmag");
6941553Srgrimes		} else {
6951553Srgrimes			for (n = 0; n < 4; n++) {
6961553Srgrimes				if (fonts[n][0] != '/')
6971553Srgrimes					(void) write(fo, _PATH_VFONT,
6981553Srgrimes					    sizeof(_PATH_VFONT) - 1);
6991553Srgrimes				(void) write(fo, fonts[n], strlen(fonts[n]));
7001553Srgrimes				(void) write(fo, "\n", 1);
7011553Srgrimes			}
7021553Srgrimes			(void) close(fo);
7031553Srgrimes		}
70431492Swollman		prog = (format == 't') ? pp->filters[LPF_TROFF]
70531492Swollman			: ((format == 'n') ? pp->filters[LPF_DITROFF]
70631492Swollman			   : pp->filters[LPF_DVI]);
7071553Srgrimes		av[1] = pxwidth;
7081553Srgrimes		av[2] = pxlength;
7091553Srgrimes		n = 3;
7101553Srgrimes		break;
7111553Srgrimes	case 'c':	/* print cifplot output */
71231492Swollman		prog = pp->filters[LPF_CIFPLOT];
7131553Srgrimes		av[1] = pxwidth;
7141553Srgrimes		av[2] = pxlength;
7151553Srgrimes		n = 3;
7161553Srgrimes		break;
7171553Srgrimes	case 'g':	/* print plot(1G) output */
71831492Swollman		prog = pp->filters[LPF_GRAPH];
7191553Srgrimes		av[1] = pxwidth;
7201553Srgrimes		av[2] = pxlength;
7211553Srgrimes		n = 3;
7221553Srgrimes		break;
7231553Srgrimes	case 'v':	/* print raster output */
72431492Swollman		prog = pp->filters[LPF_RASTER];
7251553Srgrimes		av[1] = pxwidth;
7261553Srgrimes		av[2] = pxlength;
7271553Srgrimes		n = 3;
7281553Srgrimes		break;
7291553Srgrimes	default:
7301553Srgrimes		(void) close(fi);
7311553Srgrimes		syslog(LOG_ERR, "%s: illegal format character '%c'",
73231492Swollman			pp->printer, format);
7331553Srgrimes		return(ERROR);
7341553Srgrimes	}
73515648Sjoerg	if (prog == NULL) {
73615648Sjoerg		(void) close(fi);
73715648Sjoerg		syslog(LOG_ERR,
73815648Sjoerg		   "%s: no filter found in printcap for format character '%c'",
73931492Swollman		   pp->printer, format);
74015648Sjoerg		return(ERROR);
74115648Sjoerg	}
74227635Simp	if ((av[0] = strrchr(prog, '/')) != NULL)
7431553Srgrimes		av[0]++;
7441553Srgrimes	else
7451553Srgrimes		av[0] = prog;
7461553Srgrimes	av[n++] = "-n";
7471553Srgrimes	av[n++] = logname;
7481553Srgrimes	av[n++] = "-h";
74978300Sgad	av[n++] = origin_host;
75031492Swollman	av[n++] = pp->acct_file;
7511553Srgrimes	av[n] = 0;
7521553Srgrimes	fo = pfd;
7531553Srgrimes	if (ofilter > 0) {		/* stop output filter */
7541553Srgrimes		write(ofd, "\031\1", 2);
7551553Srgrimes		while ((pid =
7561553Srgrimes		    wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter)
7571553Srgrimes			;
7581553Srgrimes		if (status.w_stopval != WSTOPPED) {
7591553Srgrimes			(void) close(fi);
76015648Sjoerg			syslog(LOG_WARNING,
76131492Swollman			       "%s: output filter died "
76231492Swollman			       "(retcode=%d termsig=%d)",
76331492Swollman				pp->printer, status.w_retcode,
76431492Swollman			       status.w_termsig);
7651553Srgrimes			return(REPRINT);
7661553Srgrimes		}
7671553Srgrimes		stopped++;
7681553Srgrimes	}
7691553Srgrimesstart:
77031492Swollman	if ((child = dofork(pp, DORETURN)) == 0) { /* child */
7711553Srgrimes		dup2(fi, 0);
7721553Srgrimes		dup2(fo, 1);
77368664Sgad		/* setup stderr for the filter (child process)
77468664Sgad		 * so it goes to our temporary errors file */
77568664Sgad		n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
7761553Srgrimes		if (n >= 0)
7771553Srgrimes			dup2(n, 2);
7788094Sjkh		closelog();
77931492Swollman		closeallfds(3);
7801553Srgrimes		execv(prog, av);
7811553Srgrimes		syslog(LOG_ERR, "cannot execv %s", prog);
7821553Srgrimes		exit(2);
7831553Srgrimes	}
7841553Srgrimes	(void) close(fi);
7851553Srgrimes	if (child < 0)
7861553Srgrimes		status.w_retcode = 100;
7871553Srgrimes	else
7881553Srgrimes		while ((pid = wait((int *)&status)) > 0 && pid != child)
7891553Srgrimes			;
7901553Srgrimes	child = 0;
7911553Srgrimes	prchild = 0;
7921553Srgrimes	if (stopped) {		/* restart output filter */
7931553Srgrimes		if (kill(ofilter, SIGCONT) < 0) {
7941553Srgrimes			syslog(LOG_ERR, "cannot restart output filter");
7951553Srgrimes			exit(1);
7961553Srgrimes		}
7971553Srgrimes	}
79831492Swollman	pp->tof = 0;
7991553Srgrimes
80068664Sgad	/* Copy the filter's output to "lf" logfile */
80168664Sgad	if ((fp = fopen(tempstderr, "r"))) {
8021553Srgrimes		while (fgets(buf, sizeof(buf), fp))
8031553Srgrimes			fputs(buf, stderr);
8041553Srgrimes		fclose(fp);
8051553Srgrimes	}
8061553Srgrimes
8071553Srgrimes	if (!WIFEXITED(status)) {
80815648Sjoerg		syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
80931492Swollman			pp->printer, format, status.w_termsig);
8101553Srgrimes		return(ERROR);
8111553Srgrimes	}
8121553Srgrimes	switch (status.w_retcode) {
8131553Srgrimes	case 0:
81431492Swollman		pp->tof = 1;
8151553Srgrimes		return(OK);
8161553Srgrimes	case 1:
8171553Srgrimes		return(REPRINT);
81815648Sjoerg	case 2:
81915648Sjoerg		return(ERROR);
8201553Srgrimes	default:
82115648Sjoerg		syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
82231492Swollman			pp->printer, format, status.w_retcode);
82315648Sjoerg		return(FILTERERR);
8241553Srgrimes	}
8251553Srgrimes}
8261553Srgrimes
8271553Srgrimes/*
8281553Srgrimes * Send the daemon control file (cf) and any data files.
8291553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and
8301553Srgrimes * 0 if all is well.
8311553Srgrimes */
8321553Srgrimesstatic int
83378146Sgadsendit(struct printer *pp, char *file)
8341553Srgrimes{
8351553Srgrimes	register int i, err = OK;
8361553Srgrimes	char *cp, last[BUFSIZ];
8371553Srgrimes
8381553Srgrimes	/*
8391553Srgrimes	 * open control file
8401553Srgrimes	 */
8411553Srgrimes	if ((cfp = fopen(file, "r")) == NULL)
8421553Srgrimes		return(OK);
84368253Sgad
84468253Sgad	/* initialize job-specific count of datafiles processed */
84568253Sgad	job_dfcnt = 0;
84668253Sgad
8471553Srgrimes	/*
8481553Srgrimes	 *      read the control file for work to do
8491553Srgrimes	 *
8501553Srgrimes	 *      file format -- first character in the line is a command
8511553Srgrimes	 *      rest of the line is the argument.
8521553Srgrimes	 *      commands of interest are:
8531553Srgrimes	 *
8541553Srgrimes	 *            a-z -- "file name" name of file to print
8551553Srgrimes	 *              U -- "unlink" name of file to remove
8561553Srgrimes	 *                    (after we print it. (Pass 2 only)).
8571553Srgrimes	 */
8581553Srgrimes
8591553Srgrimes	/*
8601553Srgrimes	 * pass 1
8611553Srgrimes	 */
8621553Srgrimes	while (getline(cfp)) {
8631553Srgrimes	again:
8641553Srgrimes		if (line[0] == 'S') {
8651553Srgrimes			cp = line+1;
8661553Srgrimes			i = 0;
8671553Srgrimes			while (*cp >= '0' && *cp <= '9')
8681553Srgrimes				i = i * 10 + (*cp++ - '0');
8691553Srgrimes			fdev = i;
8701553Srgrimes			cp++;
8711553Srgrimes			i = 0;
8721553Srgrimes			while (*cp >= '0' && *cp <= '9')
8731553Srgrimes				i = i * 10 + (*cp++ - '0');
8741553Srgrimes			fino = i;
87524831Sbrian		} else if (line[0] == 'H') {
87678300Sgad			strlcpy(origin_host, line + 1, sizeof(origin_host));
87768343Sgad			if (class[0] == '\0') {
87827748Simp				strncpy(class, line+1, sizeof(class) - 1);
87968343Sgad				class[sizeof(class) - 1] = '\0';
88068343Sgad			}
88124831Sbrian		} else if (line[0] == 'P') {
88227748Simp			strncpy(logname, line+1, sizeof(logname) - 1);
88368741Sgad			logname[sizeof(logname) - 1] = '\0';
88431492Swollman			if (pp->restricted) { /* restricted */
88524831Sbrian				if (getpwnam(logname) == NULL) {
88631492Swollman					sendmail(pp, line+1, NOACCT);
88724831Sbrian					err = ERROR;
88824831Sbrian					break;
88924831Sbrian				}
89024831Sbrian			}
89124831Sbrian		} else if (line[0] == 'I') {
89227748Simp			strncpy(indent+2, line+1, sizeof(indent) - 3);
89368741Sgad			indent[2 + sizeof(indent) - 3] = '\0';
89424831Sbrian		} else if (line[0] >= 'a' && line[0] <= 'z') {
8951553Srgrimes			strcpy(last, line);
89631492Swollman			while ((i = getline(cfp)) != 0)
8971553Srgrimes				if (strcmp(last, line))
8981553Srgrimes					break;
89931492Swollman			switch (sendfile(pp, '\3', last+1, *last)) {
9001553Srgrimes			case OK:
9011553Srgrimes				if (i)
9021553Srgrimes					goto again;
9031553Srgrimes				break;
9041553Srgrimes			case REPRINT:
9051553Srgrimes				(void) fclose(cfp);
9061553Srgrimes				return(REPRINT);
9071553Srgrimes			case ACCESS:
90831492Swollman				sendmail(pp, logname, ACCESS);
9091553Srgrimes			case ERROR:
9101553Srgrimes				err = ERROR;
9111553Srgrimes			}
9121553Srgrimes			break;
9131553Srgrimes		}
9141553Srgrimes	}
91531492Swollman	if (err == OK && sendfile(pp, '\2', file, '\0') > 0) {
9161553Srgrimes		(void) fclose(cfp);
9171553Srgrimes		return(REPRINT);
9181553Srgrimes	}
9191553Srgrimes	/*
9201553Srgrimes	 * pass 2
9211553Srgrimes	 */
9221553Srgrimes	fseek(cfp, 0L, 0);
9231553Srgrimes	while (getline(cfp))
92427748Simp		if (line[0] == 'U' && !strchr(line+1, '/'))
9251553Srgrimes			(void) unlink(line+1);
9261553Srgrimes	/*
9271553Srgrimes	 * clean-up in case another control file exists
9281553Srgrimes	 */
9291553Srgrimes	(void) fclose(cfp);
9301553Srgrimes	(void) unlink(file);
9311553Srgrimes	return(err);
9321553Srgrimes}
9331553Srgrimes
9341553Srgrimes/*
9351553Srgrimes * Send a data file to the remote machine and spool it.
9361553Srgrimes * Return positive if we should try resending.
9371553Srgrimes */
9381553Srgrimesstatic int
93978146Sgadsendfile(struct printer *pp, int type, char *file, char format)
9401553Srgrimes{
9411553Srgrimes	register int f, i, amt;
9421553Srgrimes	struct stat stb;
94368664Sgad	FILE *fp;
9441553Srgrimes	char buf[BUFSIZ];
94574124Sgad	int closedpr, resp, sizerr, statrc;
9461553Srgrimes
94774124Sgad	statrc = lstat(file, &stb);
94874124Sgad	if (statrc < 0) {
94974124Sgad		syslog(LOG_ERR, "%s: error from lstat(%s): %m",
95074124Sgad		    pp->printer, file);
9511553Srgrimes		return(ERROR);
95274124Sgad	}
95374124Sgad	f = open(file, O_RDONLY);
95474124Sgad	if (f < 0) {
95574124Sgad		syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
95674124Sgad		    pp->printer, file);
95774124Sgad		return(ERROR);
95874124Sgad	}
9591553Srgrimes	/*
9601553Srgrimes	 * Check to see if data file is a symbolic link. If so, it should
9611553Srgrimes	 * still point to the same file or someone is trying to print something
9621553Srgrimes	 * he shouldn't.
9631553Srgrimes	 */
9641553Srgrimes	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 &&
9651553Srgrimes	    (stb.st_dev != fdev || stb.st_ino != fino))
9661553Srgrimes		return(ACCESS);
96724831Sbrian
96868253Sgad	job_dfcnt++;		/* increment datafile counter for this job */
96968253Sgad
97068253Sgad	/* everything seems OK, start it up */
97124831Sbrian	sizerr = 0;
97224831Sbrian	closedpr = 0;
97324831Sbrian	if (type == '\3') {
97431492Swollman		if (pp->filters[LPF_INPUT]) {
97524831Sbrian			/*
97674124Sgad			 * We're sending something with an ifilter.  We have to
97774124Sgad			 * run the ifilter and store the output as a temporary
97874124Sgad			 * spool file (tfile...), because the protocol requires
97974124Sgad			 * us to send the file size before we start sending any
98074124Sgad			 * of the data.
98124831Sbrian			 */
98224831Sbrian			char *av[15];
98324831Sbrian			int n;
98424831Sbrian			int ifilter;
98531492Swollman			union wait status; /* XXX */
98624831Sbrian
98724831Sbrian			strcpy(tfile,TFILENAME);
98824831Sbrian			if ((tfd = mkstemp(tfile)) == -1) {
98924831Sbrian				syslog(LOG_ERR, "mkstemp: %m");
99024831Sbrian				return(ERROR);
99124831Sbrian			}
99231492Swollman			if ((av[0] = strrchr(pp->filters[LPF_INPUT], '/')) == NULL)
99331492Swollman				av[0] = pp->filters[LPF_INPUT];
99424831Sbrian			else
99524831Sbrian				av[0]++;
99624831Sbrian			if (format == 'l')
99724831Sbrian				av[n=1] = "-c";
99824831Sbrian			else
99924831Sbrian				n = 0;
100024831Sbrian			av[++n] = width;
100124831Sbrian			av[++n] = length;
100224831Sbrian			av[++n] = indent;
100324831Sbrian			av[++n] = "-n";
100424831Sbrian			av[++n] = logname;
100524831Sbrian			av[++n] = "-h";
100678300Sgad			av[++n] = origin_host;
100731492Swollman			av[++n] = pp->acct_file;
100824831Sbrian			av[++n] = 0;
100931492Swollman			if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */
101024831Sbrian				dup2(f, 0);
101124831Sbrian				dup2(tfd, 1);
101268664Sgad				/* setup stderr for the filter (child process)
101368664Sgad				 * so it goes to our temporary errors file */
101468664Sgad				n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
101524831Sbrian				if (n >= 0)
101624831Sbrian					dup2(n, 2);
101724831Sbrian				closelog();
101831492Swollman				closeallfds(3);
101931492Swollman				execv(pp->filters[LPF_INPUT], av);
102031492Swollman				syslog(LOG_ERR, "cannot execv %s",
102131492Swollman				       pp->filters[LPF_INPUT]);
102224831Sbrian				exit(2);
102324831Sbrian			}
102424831Sbrian			(void) close(f);
102524831Sbrian			if (ifilter < 0)
102624831Sbrian				status.w_retcode = 100;
102724831Sbrian			else
102824831Sbrian				while ((pid = wait((int *)&status)) > 0 &&
102924831Sbrian					pid != ifilter)
103024831Sbrian					;
103168664Sgad			/* Copy the filter's output to "lf" logfile */
103268664Sgad			if ((fp = fopen(tempstderr, "r"))) {
103368664Sgad				while (fgets(buf, sizeof(buf), fp))
103468664Sgad					fputs(buf, stderr);
103568664Sgad				fclose(fp);
103668664Sgad			}
103768664Sgad			/* process the return-code from the filter */
103824831Sbrian			switch (status.w_retcode) {
103924831Sbrian			case 0:
104024831Sbrian				break;
104124831Sbrian			case 1:
104224831Sbrian				unlink(tfile);
104324831Sbrian				return(REPRINT);
104424831Sbrian			case 2:
104524831Sbrian				unlink(tfile);
104624831Sbrian				return(ERROR);
104724831Sbrian			default:
104824831Sbrian				syslog(LOG_WARNING, "%s: filter '%c' exited"
104924831Sbrian					" (retcode=%d)",
105031492Swollman					pp->printer, format, status.w_retcode);
105124831Sbrian				unlink(tfile);
105224831Sbrian				return(FILTERERR);
105324831Sbrian			}
105474124Sgad			statrc = fstat(tfd, &stb);   /* to find size of tfile */
105574124Sgad			if (statrc < 0)	{
105674124Sgad				syslog(LOG_ERR, "%s: error processing 'if', fstat(%s): %m",
105774124Sgad				    pp->printer, tfile);
105824831Sbrian				return(ERROR);
105974124Sgad			}
106024831Sbrian			f = tfd;
106124831Sbrian			lseek(f,0,SEEK_SET);
106224831Sbrian		} else if (ofilter) {
106324831Sbrian			/*
106424831Sbrian			 * We're sending something with an ofilter, we have to
106524831Sbrian			 * store the output as a temporary file (tfile)... the
106624831Sbrian			 * protocol requires us to send the file size
106724831Sbrian			 */
106824831Sbrian			int i;
106924831Sbrian			for (i = 0; i < stb.st_size; i += BUFSIZ) {
107024831Sbrian				amt = BUFSIZ;
107124831Sbrian				if (i + amt > stb.st_size)
107224831Sbrian					amt = stb.st_size - i;
107324831Sbrian				if (sizerr == 0 && read(f, buf, amt) != amt) {
107424831Sbrian					sizerr = 1;
107524831Sbrian					break;
107624831Sbrian				}
107724831Sbrian				if (write(ofd, buf, amt) != amt) {
107824831Sbrian					(void) close(f);
107924831Sbrian					return(REPRINT);
108024831Sbrian				}
108124831Sbrian			}
108224831Sbrian			close(ofd);
108324831Sbrian			close(f);
108424831Sbrian			while ((i = wait(NULL)) > 0 && i != ofilter)
108524831Sbrian				;
108624831Sbrian			ofilter = 0;
108774124Sgad			statrc = fstat(tfd, &stb);   /* to find size of tfile */
108874124Sgad			if (statrc < 0)	{
108974124Sgad				syslog(LOG_ERR, "%s: error processing 'of', fstat(%s): %m",
109074124Sgad				    pp->printer, tfile);
109131492Swollman				openpr(pp);
109224831Sbrian				return(ERROR);
109324831Sbrian			}
109424831Sbrian			f = tfd;
109524831Sbrian			lseek(f,0,SEEK_SET);
109624831Sbrian			closedpr = 1;
109724831Sbrian		}
109824831Sbrian	}
109924831Sbrian
11001553Srgrimes	(void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file);
11011553Srgrimes	amt = strlen(buf);
11021553Srgrimes	for (i = 0;  ; i++) {
11031553Srgrimes		if (write(pfd, buf, amt) != amt ||
110431492Swollman		    (resp = response(pp)) < 0 || resp == '\1') {
11051553Srgrimes			(void) close(f);
110624831Sbrian			if (tfd != -1 && type == '\3') {
110724831Sbrian				tfd = -1;
110824831Sbrian				unlink(tfile);
110924831Sbrian				if (closedpr)
111031492Swollman					openpr(pp);
111124831Sbrian			}
11121553Srgrimes			return(REPRINT);
11131553Srgrimes		} else if (resp == '\0')
11141553Srgrimes			break;
11151553Srgrimes		if (i == 0)
111631492Swollman			pstatus(pp,
111731492Swollman				"no space on remote; waiting for queue to drain");
11181553Srgrimes		if (i == 10)
11191553Srgrimes			syslog(LOG_ALERT, "%s: can't send to %s; queue full",
112031492Swollman				pp->printer, pp->remote_host);
11211553Srgrimes		sleep(5 * 60);
11221553Srgrimes	}
11231553Srgrimes	if (i)
112431492Swollman		pstatus(pp, "sending to %s", pp->remote_host);
112568253Sgad	if (type == '\3')
112668253Sgad		trstat_init(pp, file, job_dfcnt);
11271553Srgrimes	for (i = 0; i < stb.st_size; i += BUFSIZ) {
11281553Srgrimes		amt = BUFSIZ;
11291553Srgrimes		if (i + amt > stb.st_size)
11301553Srgrimes			amt = stb.st_size - i;
11311553Srgrimes		if (sizerr == 0 && read(f, buf, amt) != amt)
11321553Srgrimes			sizerr = 1;
11331553Srgrimes		if (write(pfd, buf, amt) != amt) {
11341553Srgrimes			(void) close(f);
113524831Sbrian			if (tfd != -1 && type == '\3') {
113624831Sbrian				tfd = -1;
113724831Sbrian				unlink(tfile);
113824831Sbrian				if (closedpr)
113931492Swollman					openpr(pp);
114024831Sbrian			}
11411553Srgrimes			return(REPRINT);
11421553Srgrimes		}
11431553Srgrimes	}
11441553Srgrimes
11451553Srgrimes	(void) close(f);
114624831Sbrian	if (tfd != -1 && type == '\3') {
114724831Sbrian		tfd = -1;
114824831Sbrian		unlink(tfile);
114924831Sbrian	}
11501553Srgrimes	if (sizerr) {
115131492Swollman		syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
11521553Srgrimes		/* tell recvjob to ignore this file */
11531553Srgrimes		(void) write(pfd, "\1", 1);
115424831Sbrian		if (closedpr)
115531492Swollman			openpr(pp);
11561553Srgrimes		return(ERROR);
11571553Srgrimes	}
115831492Swollman	if (write(pfd, "", 1) != 1 || response(pp)) {
115924831Sbrian		if (closedpr)
116031492Swollman			openpr(pp);
11611553Srgrimes		return(REPRINT);
116224831Sbrian	}
116324831Sbrian	if (closedpr)
116431492Swollman		openpr(pp);
116568253Sgad	if (type == '\3')
116668253Sgad		trstat_write(pp, TR_SENDING, stb.st_size, logname,
116778300Sgad				 pp->remote_host, origin_host);
11681553Srgrimes	return(OK);
11691553Srgrimes}
11701553Srgrimes
11711553Srgrimes/*
11721553Srgrimes * Check to make sure there have been no errors and that both programs
11731553Srgrimes * are in sync with eachother.
11741553Srgrimes * Return non-zero if the connection was lost.
11751553Srgrimes */
11761553Srgrimesstatic char
117778146Sgadresponse(const struct printer *pp)
11781553Srgrimes{
11791553Srgrimes	char resp;
11801553Srgrimes
11811553Srgrimes	if (read(pfd, &resp, 1) != 1) {
118231492Swollman		syslog(LOG_INFO, "%s: lost connection", pp->printer);
11831553Srgrimes		return(-1);
11841553Srgrimes	}
11851553Srgrimes	return(resp);
11861553Srgrimes}
11871553Srgrimes
11881553Srgrimes/*
11891553Srgrimes * Banner printing stuff
11901553Srgrimes */
11911553Srgrimesstatic void
119278146Sgadbanner(struct printer *pp, char *name1, char *name2)
11931553Srgrimes{
11941553Srgrimes	time_t tvec;
11951553Srgrimes
11961553Srgrimes	time(&tvec);
119731492Swollman	if (!pp->no_formfeed && !pp->tof)
119831492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
119931492Swollman	if (pp->short_banner) {	/* short banner only */
12001553Srgrimes		if (class[0]) {
12011553Srgrimes			(void) write(ofd, class, strlen(class));
12021553Srgrimes			(void) write(ofd, ":", 1);
12031553Srgrimes		}
12041553Srgrimes		(void) write(ofd, name1, strlen(name1));
12051553Srgrimes		(void) write(ofd, "  Job: ", 7);
12061553Srgrimes		(void) write(ofd, name2, strlen(name2));
12071553Srgrimes		(void) write(ofd, "  Date: ", 8);
12081553Srgrimes		(void) write(ofd, ctime(&tvec), 24);
12091553Srgrimes		(void) write(ofd, "\n", 1);
12101553Srgrimes	} else {	/* normal banner */
12111553Srgrimes		(void) write(ofd, "\n\n\n", 3);
121231492Swollman		scan_out(pp, ofd, name1, '\0');
12131553Srgrimes		(void) write(ofd, "\n\n", 2);
121431492Swollman		scan_out(pp, ofd, name2, '\0');
12151553Srgrimes		if (class[0]) {
12161553Srgrimes			(void) write(ofd,"\n\n\n",3);
121731492Swollman			scan_out(pp, ofd, class, '\0');
12181553Srgrimes		}
12191553Srgrimes		(void) write(ofd, "\n\n\n\n\t\t\t\t\tJob:  ", 15);
12201553Srgrimes		(void) write(ofd, name2, strlen(name2));
12211553Srgrimes		(void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
12221553Srgrimes		(void) write(ofd, ctime(&tvec), 24);
12231553Srgrimes		(void) write(ofd, "\n", 1);
12241553Srgrimes	}
122531492Swollman	if (!pp->no_formfeed)
122631492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
122731492Swollman	pp->tof = 1;
12281553Srgrimes}
12291553Srgrimes
12301553Srgrimesstatic char *
123178146Sgadscnline(int key, char *p, int c)
12321553Srgrimes{
123339084Swollman	register int scnwidth;
12341553Srgrimes
12351553Srgrimes	for (scnwidth = WIDTH; --scnwidth;) {
12361553Srgrimes		key <<= 1;
12371553Srgrimes		*p++ = key & 0200 ? c : BACKGND;
12381553Srgrimes	}
12391553Srgrimes	return (p);
12401553Srgrimes}
12411553Srgrimes
12421553Srgrimes#define TRC(q)	(((q)-' ')&0177)
12431553Srgrimes
12441553Srgrimesstatic void
124578146Sgadscan_out(struct printer *pp, int scfd, char *scsp, int dlm)
12461553Srgrimes{
12471553Srgrimes	register char *strp;
124839084Swollman	register int nchrs, j;
12491553Srgrimes	char outbuf[LINELEN+1], *sp, c, cc;
12501553Srgrimes	int d, scnhgt;
12511553Srgrimes
12521553Srgrimes	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
12531553Srgrimes		strp = &outbuf[0];
12541553Srgrimes		sp = scsp;
12551553Srgrimes		for (nchrs = 0; ; ) {
12561553Srgrimes			d = dropit(c = TRC(cc = *sp++));
12571553Srgrimes			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
12581553Srgrimes				for (j = WIDTH; --j;)
12591553Srgrimes					*strp++ = BACKGND;
12601553Srgrimes			else
126131492Swollman				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
126231492Swollman			if (*sp == dlm || *sp == '\0' ||
126331492Swollman			    nchrs++ >= pp->page_width/(WIDTH+1)-1)
12641553Srgrimes				break;
12651553Srgrimes			*strp++ = BACKGND;
12661553Srgrimes			*strp++ = BACKGND;
12671553Srgrimes		}
12681553Srgrimes		while (*--strp == BACKGND && strp >= outbuf)
12691553Srgrimes			;
12701553Srgrimes		strp++;
12718857Srgrimes		*strp++ = '\n';
12721553Srgrimes		(void) write(scfd, outbuf, strp-outbuf);
12731553Srgrimes	}
12741553Srgrimes}
12751553Srgrimes
12761553Srgrimesstatic int
127778146Sgaddropit(int c)
12781553Srgrimes{
12791553Srgrimes	switch(c) {
12801553Srgrimes
12811553Srgrimes	case TRC('_'):
12821553Srgrimes	case TRC(';'):
12831553Srgrimes	case TRC(','):
12841553Srgrimes	case TRC('g'):
12851553Srgrimes	case TRC('j'):
12861553Srgrimes	case TRC('p'):
12871553Srgrimes	case TRC('q'):
12881553Srgrimes	case TRC('y'):
12891553Srgrimes		return (DROP);
12901553Srgrimes
12911553Srgrimes	default:
12921553Srgrimes		return (0);
12931553Srgrimes	}
12941553Srgrimes}
12951553Srgrimes
12961553Srgrimes/*
12971553Srgrimes * sendmail ---
12981553Srgrimes *   tell people about job completion
12991553Srgrimes */
13001553Srgrimesstatic void
130178146Sgadsendmail(struct printer *pp, char *user, int bombed)
13021553Srgrimes{
13031553Srgrimes	register int i;
13041553Srgrimes	int p[2], s;
130578146Sgad	register const char *cp;
13061553Srgrimes	struct stat stb;
13071553Srgrimes	FILE *fp;
13081553Srgrimes
13091553Srgrimes	pipe(p);
131031492Swollman	if ((s = dofork(pp, DORETURN)) == 0) {		/* child */
13111553Srgrimes		dup2(p[0], 0);
13128094Sjkh		closelog();
131331492Swollman		closeallfds(3);
131427635Simp		if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
13151553Srgrimes			cp++;
131631492Swollman		else
13171553Srgrimes			cp = _PATH_SENDMAIL;
131827757Simp		execl(_PATH_SENDMAIL, cp, "-t", 0);
131931492Swollman		_exit(0);
13201553Srgrimes	} else if (s > 0) {				/* parent */
13211553Srgrimes		dup2(p[1], 1);
132278300Sgad		printf("To: %s@%s\n", user, origin_host);
132331492Swollman		printf("Subject: %s printer job \"%s\"\n", pp->printer,
132415648Sjoerg			*jobname ? jobname : "<unknown>");
132578300Sgad		printf("Reply-To: root@%s\n\n", local_host);
13261553Srgrimes		printf("Your printer job ");
13271553Srgrimes		if (*jobname)
13281553Srgrimes			printf("(%s) ", jobname);
132931492Swollman
133031492Swollman		cp = "XXX compiler confusion"; /* XXX shut GCC up */
13311553Srgrimes		switch (bombed) {
13321553Srgrimes		case OK:
13331553Srgrimes			printf("\ncompleted successfully\n");
133415648Sjoerg			cp = "OK";
13351553Srgrimes			break;
13361553Srgrimes		default:
13371553Srgrimes		case FATALERR:
13381553Srgrimes			printf("\ncould not be printed\n");
133915648Sjoerg			cp = "FATALERR";
13401553Srgrimes			break;
13411553Srgrimes		case NOACCT:
134278300Sgad			printf("\ncould not be printed without an account on %s\n",
134378300Sgad			    local_host);
134415648Sjoerg			cp = "NOACCT";
13451553Srgrimes			break;
13461553Srgrimes		case FILTERERR:
134768664Sgad			if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
134868664Sgad			    || (fp = fopen(tempstderr, "r")) == NULL) {
134915648Sjoerg				printf("\nhad some errors and may not have printed\n");
13501553Srgrimes				break;
13511553Srgrimes			}
135215648Sjoerg			printf("\nhad the following errors and may not have printed:\n");
13531553Srgrimes			while ((i = getc(fp)) != EOF)
13541553Srgrimes				putchar(i);
13551553Srgrimes			(void) fclose(fp);
135615648Sjoerg			cp = "FILTERERR";
13571553Srgrimes			break;
13581553Srgrimes		case ACCESS:
13591553Srgrimes			printf("\nwas not printed because it was not linked to the original file\n");
136015648Sjoerg			cp = "ACCESS";
13611553Srgrimes		}
13621553Srgrimes		fflush(stdout);
13631553Srgrimes		(void) close(1);
136431492Swollman	} else {
136531492Swollman		syslog(LOG_WARNING, "unable to send mail to %s: %m", user);
136631492Swollman		return;
13671553Srgrimes	}
13681553Srgrimes	(void) close(p[0]);
13691553Srgrimes	(void) close(p[1]);
137015648Sjoerg	wait(NULL);
137115648Sjoerg	syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
137231492Swollman		user, *jobname ? jobname : "<unknown>", pp->printer, cp);
13731553Srgrimes}
13741553Srgrimes
13751553Srgrimes/*
13761553Srgrimes * dofork - fork with retries on failure
13771553Srgrimes */
13781553Srgrimesstatic int
137978146Sgaddofork(const struct printer *pp, int action)
13801553Srgrimes{
138178146Sgad	register int i, forkpid;
138260871Smpp	struct passwd *pwd;
13831553Srgrimes
13841553Srgrimes	for (i = 0; i < 20; i++) {
138578146Sgad		if ((forkpid = fork()) < 0) {
13861553Srgrimes			sleep((unsigned)(i*i));
13871553Srgrimes			continue;
13881553Srgrimes		}
13891553Srgrimes		/*
13901553Srgrimes		 * Child should run as daemon instead of root
13911553Srgrimes		 */
139278146Sgad		if (forkpid == 0) {
139360871Smpp			if ((pwd = getpwuid(pp->daemon_user)) == NULL) {
139468379Sgad				syslog(LOG_ERR, "Can't lookup default daemon uid (%ld) in password file",
139560871Smpp				    pp->daemon_user);
139660871Smpp				break;
139760871Smpp			}
139860871Smpp			initgroups(pwd->pw_name, pwd->pw_gid);
139960871Smpp			setgid(pwd->pw_gid);
140031492Swollman			setuid(pp->daemon_user);
140160871Smpp		}
140278146Sgad		return(forkpid);
14031553Srgrimes	}
14041553Srgrimes	syslog(LOG_ERR, "can't fork");
14051553Srgrimes
14061553Srgrimes	switch (action) {
14071553Srgrimes	case DORETURN:
14081553Srgrimes		return (-1);
14091553Srgrimes	default:
14101553Srgrimes		syslog(LOG_ERR, "bad action (%d) to dofork", action);
14111553Srgrimes		/*FALL THRU*/
14121553Srgrimes	case DOABORT:
14131553Srgrimes		exit(1);
14141553Srgrimes	}
14151553Srgrimes	/*NOTREACHED*/
14161553Srgrimes}
14171553Srgrimes
14181553Srgrimes/*
14191553Srgrimes * Kill child processes to abort current job.
14201553Srgrimes */
14211553Srgrimesstatic void
142278146Sgadabortpr(int signo __unused)
14231553Srgrimes{
142468664Sgad
142568664Sgad	(void) unlink(tempstderr);
14261553Srgrimes	kill(0, SIGINT);
14271553Srgrimes	if (ofilter > 0)
14281553Srgrimes		kill(ofilter, SIGCONT);
14291553Srgrimes	while (wait(NULL) > 0)
14301553Srgrimes		;
143124831Sbrian	if (ofilter > 0 && tfd != -1)
143224831Sbrian		unlink(tfile);
14331553Srgrimes	exit(0);
14341553Srgrimes}
14351553Srgrimes
14361553Srgrimesstatic void
143778146Sgadinit(struct printer *pp)
14381553Srgrimes{
14391553Srgrimes	char *s;
14401553Srgrimes
144131492Swollman	sprintf(&width[2], "%ld", pp->page_width);
144231492Swollman	sprintf(&length[2], "%ld", pp->page_length);
144331492Swollman	sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
144431492Swollman	sprintf(&pxlength[2], "%ld", pp->page_plength);
144531492Swollman	if ((s = checkremote(pp)) != 0) {
144631492Swollman		syslog(LOG_WARNING, "%s", s);
144731492Swollman		free(s);
144831492Swollman	}
144931492Swollman}
145031492Swollman
145131492Swollmanvoid
145278146Sgadstartprinting(const char *printer)
145331492Swollman{
145431492Swollman	struct printer myprinter, *pp = &myprinter;
145531492Swollman	int status;
145631492Swollman
145731492Swollman	init_printer(pp);
145831492Swollman	status = getprintcap(printer, pp);
145931492Swollman	switch(status) {
146031492Swollman	case PCAPERR_OSERR:
146131492Swollman		syslog(LOG_ERR, "can't open printer description file: %m");
14621553Srgrimes		exit(1);
146331492Swollman	case PCAPERR_NOTFOUND:
14641553Srgrimes		syslog(LOG_ERR, "unknown printer: %s", printer);
14651553Srgrimes		exit(1);
146631492Swollman	case PCAPERR_TCLOOP:
146731492Swollman		fatal(pp, "potential reference loop detected in printcap file");
146831492Swollman	default:
146931492Swollman		break;
147031492Swollman	}
147131492Swollman	printjob(pp);
14721553Srgrimes}
14731553Srgrimes
14741553Srgrimes/*
14751553Srgrimes * Acquire line printer or remote connection.
14761553Srgrimes */
14771553Srgrimesstatic void
147878146Sgadopenpr(const struct printer *pp)
14791553Srgrimes{
148031492Swollman	int p[2];
148115648Sjoerg	char *cp;
14821553Srgrimes
148331492Swollman	if (pp->remote) {
148431492Swollman		openrem(pp);
148531492Swollman	} else if (*pp->lp) {
148631492Swollman		if ((cp = strchr(pp->lp, '@')) != NULL)
148731492Swollman			opennet(pp);
148815648Sjoerg		else
148931492Swollman			opentty(pp);
14901553Srgrimes	} else {
14911553Srgrimes		syslog(LOG_ERR, "%s: no line printer device or host name",
149231492Swollman			pp->printer);
14931553Srgrimes		exit(1);
14941553Srgrimes	}
149515648Sjoerg
14961553Srgrimes	/*
14971553Srgrimes	 * Start up an output filter, if needed.
14981553Srgrimes	 */
149931492Swollman	if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) {
15001553Srgrimes		pipe(p);
150131492Swollman		if (pp->remote) {
150231492Swollman			strcpy(tfile, TFILENAME);
150324831Sbrian			tfd = mkstemp(tfile);
150424831Sbrian		}
150531492Swollman		if ((ofilter = dofork(pp, DOABORT)) == 0) {	/* child */
15061553Srgrimes			dup2(p[0], 0);		/* pipe is std in */
150724831Sbrian			/* tfile/printer is stdout */
150831492Swollman			dup2(pp->remote ? tfd : pfd, 1);
15098094Sjkh			closelog();
151031492Swollman			closeallfds(3);
151131492Swollman			if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
151231492Swollman				cp = pp->filters[LPF_OUTPUT];
15131553Srgrimes			else
15141553Srgrimes				cp++;
151531492Swollman			execl(pp->filters[LPF_OUTPUT], cp, width, length, 0);
151631492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
151731492Swollman			       pp->filters[LPF_OUTPUT]);
15181553Srgrimes			exit(1);
15191553Srgrimes		}
15201553Srgrimes		(void) close(p[0]);		/* close input side */
15211553Srgrimes		ofd = p[1];			/* use pipe for output */
15221553Srgrimes	} else {
15231553Srgrimes		ofd = pfd;
15241553Srgrimes		ofilter = 0;
15251553Srgrimes	}
15261553Srgrimes}
15271553Srgrimes
152815648Sjoerg/*
152915648Sjoerg * Printer connected directly to the network
153015648Sjoerg * or to a terminal server on the net
153115648Sjoerg */
153215648Sjoergstatic void
153378146Sgadopennet(const struct printer *pp)
153415648Sjoerg{
153515648Sjoerg	register int i;
153631492Swollman	int resp;
153731492Swollman	u_long port;
153831492Swollman	char *ep;
153930407Sjoerg	void (*savealrm)(int);
154015648Sjoerg
154131492Swollman	port = strtoul(pp->lp, &ep, 0);
154238470Sbrian	if (*ep != '@' || port > 65535) {
154331492Swollman		syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
154431492Swollman		       pp->lp);
154515648Sjoerg		exit(1);
154615648Sjoerg	}
154731492Swollman	ep++;
154815648Sjoerg
154915648Sjoerg	for (i = 1; ; i = i < 256 ? i << 1 : i) {
155015648Sjoerg		resp = -1;
155130407Sjoerg		savealrm = signal(SIGALRM, alarmhandler);
155231492Swollman		alarm(pp->conn_timeout);
155331492Swollman		pfd = getport(pp, ep, port);
155431020Sjoerg		alarm(0);
155530407Sjoerg		(void)signal(SIGALRM, savealrm);
155615648Sjoerg		if (pfd < 0 && errno == ECONNREFUSED)
155715648Sjoerg			resp = 1;
155815648Sjoerg		else if (pfd >= 0) {
155915648Sjoerg			/*
156015648Sjoerg			 * need to delay a bit for rs232 lines
156115648Sjoerg			 * to stabilize in case printer is
156215648Sjoerg			 * connected via a terminal server
156315648Sjoerg			 */
156415648Sjoerg			delay(500);
156515648Sjoerg			break;
156615648Sjoerg		}
156715648Sjoerg		if (i == 1) {
156831492Swollman			if (resp < 0)
156931492Swollman				pstatus(pp, "waiting for %s to come up",
157031492Swollman					pp->lp);
157131492Swollman			else
157231492Swollman				pstatus(pp,
157331492Swollman					"waiting for access to printer on %s",
157431492Swollman					pp->lp);
157515648Sjoerg		}
157615648Sjoerg		sleep(i);
157715648Sjoerg	}
157831492Swollman	pstatus(pp, "sending to %s port %d", ep, port);
157915648Sjoerg}
158015648Sjoerg
158115648Sjoerg/*
158215648Sjoerg * Printer is connected to an RS232 port on this host
158315648Sjoerg */
158415648Sjoergstatic void
158578146Sgadopentty(const struct printer *pp)
158615648Sjoerg{
158715648Sjoerg	register int i;
158815648Sjoerg
158915648Sjoerg	for (i = 1; ; i = i < 32 ? i << 1 : i) {
159031492Swollman		pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
159115648Sjoerg		if (pfd >= 0) {
159215648Sjoerg			delay(500);
159315648Sjoerg			break;
159415648Sjoerg		}
159515648Sjoerg		if (errno == ENOENT) {
159631492Swollman			syslog(LOG_ERR, "%s: %m", pp->lp);
159715648Sjoerg			exit(1);
159815648Sjoerg		}
159915648Sjoerg		if (i == 1)
160031492Swollman			pstatus(pp,
160131492Swollman				"waiting for %s to become ready (offline?)",
160231492Swollman				pp->printer);
160315648Sjoerg		sleep(i);
160415648Sjoerg	}
160515648Sjoerg	if (isatty(pfd))
160631492Swollman		setty(pp);
160731492Swollman	pstatus(pp, "%s is ready and printing", pp->printer);
160815648Sjoerg}
160915648Sjoerg
161015648Sjoerg/*
161115648Sjoerg * Printer is on a remote host
161215648Sjoerg */
161315648Sjoergstatic void
161478146Sgadopenrem(const struct printer *pp)
161515648Sjoerg{
161631492Swollman	register int i;
161727748Simp	int resp;
161830407Sjoerg	void (*savealrm)(int);
161915648Sjoerg
162015648Sjoerg	for (i = 1; ; i = i < 256 ? i << 1 : i) {
162115648Sjoerg		resp = -1;
162230407Sjoerg		savealrm = signal(SIGALRM, alarmhandler);
162331492Swollman		alarm(pp->conn_timeout);
162431492Swollman		pfd = getport(pp, pp->remote_host, 0);
162531020Sjoerg		alarm(0);
162630407Sjoerg		(void)signal(SIGALRM, savealrm);
162715648Sjoerg		if (pfd >= 0) {
162831492Swollman			if ((writel(pfd, "\2", pp->remote_queue, "\n",
162931492Swollman				    (char *)0)
163031492Swollman			     == 2 + strlen(pp->remote_queue))
163131492Swollman			    && (resp = response(pp)) == 0)
163215648Sjoerg				break;
163315648Sjoerg			(void) close(pfd);
163415648Sjoerg		}
163515648Sjoerg		if (i == 1) {
163615648Sjoerg			if (resp < 0)
163731492Swollman				pstatus(pp, "waiting for %s to come up",
163831492Swollman					pp->remote_host);
163915648Sjoerg			else {
164031492Swollman				pstatus(pp,
164131492Swollman					"waiting for queue to be enabled on %s",
164231492Swollman					pp->remote_host);
164315648Sjoerg				i = 256;
164415648Sjoerg			}
164515648Sjoerg		}
164615648Sjoerg		sleep(i);
164715648Sjoerg	}
164831492Swollman	pstatus(pp, "sending to %s", pp->remote_host);
164915648Sjoerg}
165015648Sjoerg
16511553Srgrimes/*
16521553Srgrimes * setup tty lines.
16531553Srgrimes */
16541553Srgrimesstatic void
165578146Sgadsetty(const struct printer *pp)
16561553Srgrimes{
165715032Ssef	struct termios ttybuf;
16581553Srgrimes
16591553Srgrimes	if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
166031492Swollman		syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
16611553Srgrimes		exit(1);
16621553Srgrimes	}
166315032Ssef	if (tcgetattr(pfd, &ttybuf) < 0) {
166431492Swollman		syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
16651553Srgrimes		exit(1);
16661553Srgrimes	}
166731492Swollman	if (pp->baud_rate > 0)
166831492Swollman		cfsetspeed(&ttybuf, pp->baud_rate);
166931492Swollman	if (pp->mode_set) {
167031492Swollman		char *s = strdup(pp->mode_set), *tmp;
167115032Ssef
167231492Swollman		while ((tmp = strsep(&s, ",")) != NULL) {
167339084Swollman			(void) msearch(tmp, &ttybuf);
16741553Srgrimes		}
16751553Srgrimes	}
167631492Swollman	if (pp->mode_set != 0 || pp->baud_rate > 0) {
167715032Ssef		if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
167831492Swollman			syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
16791553Srgrimes		}
16801553Srgrimes	}
16811553Srgrimes}
16821553Srgrimes
168327757Simp#ifdef __STDC__
16841553Srgrimes#include <stdarg.h>
16851553Srgrimes#else
16861553Srgrimes#include <varargs.h>
16871553Srgrimes#endif
16881553Srgrimes
168915648Sjoergstatic void
169027757Simp#ifdef __STDC__
169131492Swollmanpstatus(const struct printer *pp, const char *msg, ...)
16921553Srgrimes#else
169331492Swollmanpstatus(pp, msg, va_alist)
169431492Swollman	const struct printer *pp;
16951553Srgrimes	char *msg;
16961553Srgrimes        va_dcl
16971553Srgrimes#endif
16981553Srgrimes{
169931492Swollman	int fd;
170031492Swollman	char *buf;
17011553Srgrimes	va_list ap;
170227757Simp#ifdef __STDC__
17031553Srgrimes	va_start(ap, msg);
17041553Srgrimes#else
17051553Srgrimes	va_start(ap);
17061553Srgrimes#endif
17071553Srgrimes
17081553Srgrimes	umask(0);
170931492Swollman	fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
171031492Swollman	if (fd < 0) {
171131492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file);
17121553Srgrimes		exit(1);
17131553Srgrimes	}
17141553Srgrimes	ftruncate(fd, 0);
171531492Swollman	vasprintf(&buf, msg, ap);
17161553Srgrimes	va_end(ap);
171731492Swollman	writel(fd, buf, "\n", (char *)0);
171831492Swollman	close(fd);
171931492Swollman	free(buf);
17201553Srgrimes}
172130407Sjoerg
172230407Sjoergvoid
172378146Sgadalarmhandler(int signo __unused)
172430407Sjoerg{
172578146Sgad	/* the signal is ignored */
172678146Sgad	/* (the '__unused' is just to avoid a compile-time warning) */
172730407Sjoerg}
1728