printjob.c revision 97781
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 97781 2002-06-03 20:47:01Z 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
8080230Sgad#define DORETURN	0	/* dofork should return "can't fork" error */
8180230Sgad#define DOABORT		1	/* dofork should just die if fork() 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
10980230Sgad/* these two are set from pp->daemon_user, but only if they are needed */
11080230Sgadstatic char	*daemon_uname;	/* set from pwd->pw_name */
11180230Sgadstatic int	 daemon_defgid;
11280230Sgad
1131553Srgrimesstatic char	class[32];		/* classification field */
11478300Sgadstatic char	origin_host[MAXHOSTNAMELEN];	/* user's host machine */
1151553Srgrimes				/* indentation size in static characters */
1168857Srgrimesstatic char	indent[10] = "-i0";
1171553Srgrimesstatic char	jobname[100];		/* job or file name */
1181553Srgrimesstatic char	length[10] = "-l";	/* page length in lines */
1191553Srgrimesstatic char	logname[32];		/* user's login name */
1201553Srgrimesstatic char	pxlength[10] = "-y";	/* page length in pixels */
1211553Srgrimesstatic char	pxwidth[10] = "-x";	/* page width in pixels */
12268664Sgad/* tempstderr is the filename used to catch stderr from exec-ing filters */
12368664Sgadstatic char	tempstderr[] = "errs.XXXXXXX";
1241553Srgrimesstatic char	width[10] = "-w";	/* page width in static characters */
12524831Sbrian#define TFILENAME "fltXXXXXX"
12624831Sbrianstatic char	tfile[] = TFILENAME;	/* file name for filter output */
1271553Srgrimes
12878146Sgadstatic void	 abortpr(int _signo);
12978146Sgadstatic void	 alarmhandler(int _signo);
13078146Sgadstatic void	 banner(struct printer *_pp, char *_name1, char *_name2);
13178146Sgadstatic int	 dofork(const struct printer *_pp, int _action);
13278146Sgadstatic int	 dropit(int _c);
13394032Sgadstatic int	 execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
13494032Sgad		    int _infd, int _outfd);
13578146Sgadstatic void	 init(struct printer *_pp);
13678146Sgadstatic void	 openpr(const struct printer *_pp);
13778146Sgadstatic void	 opennet(const struct printer *_pp);
13878146Sgadstatic void	 opentty(const struct printer *_pp);
13978146Sgadstatic void	 openrem(const struct printer *pp);
14078146Sgadstatic int	 print(struct printer *_pp, int _format, char *_file);
14178146Sgadstatic int	 printit(struct printer *_pp, char *_file);
14279739Sgadstatic void	 pstatus(const struct printer *_pp, const char *_msg, ...)
14379739Sgad		    __printflike(2, 3);
14478146Sgadstatic char	 response(const struct printer *_pp);
14578146Sgadstatic void	 scan_out(struct printer *_pp, int _scfd, char *_scsp,
14678146Sgad		    int _dlm);
14778146Sgadstatic char	*scnline(int _key, char *_p, int _c);
14878146Sgadstatic int	 sendfile(struct printer *_pp, int _type, char *_file,
14995293Sgad		    char _format, int _copyreq);
15078146Sgadstatic int	 sendit(struct printer *_pp, char *_file);
15194038Sgadstatic void	 sendmail(struct printer *_pp, char *_userid, int _bombed);
15278146Sgadstatic void	 setty(const struct printer *_pp);
1531553Srgrimes
1541553Srgrimesvoid
15578146Sgadprintjob(struct printer *pp)
1561553Srgrimes{
1571553Srgrimes	struct stat stb;
15868401Sgad	register struct jobqueue *q, **qp;
15968401Sgad	struct jobqueue **queue;
1601553Srgrimes	register int i, nitems;
16168733Sgad	off_t pidoff;
16268733Sgad	int errcnt, jobcount, tempfd;
1631553Srgrimes
16468733Sgad	jobcount = 0;
16531492Swollman	init(pp); /* set up capabilities */
16631492Swollman	(void) write(1, "", 1);	/* ack that daemon is started */
1671553Srgrimes	(void) close(2);			/* set up log file */
16831492Swollman	if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
16931492Swollman		syslog(LOG_ERR, "%s: %m", pp->log_file);
1701553Srgrimes		(void) open(_PATH_DEVNULL, O_WRONLY);
1711553Srgrimes	}
1721553Srgrimes	setgid(getegid());
1731553Srgrimes	pid = getpid();				/* for use with lprm */
1741553Srgrimes	setpgrp(0, pid);
17579735Sgad
17679735Sgad	/*
17779735Sgad	 * At initial lpd startup, printjob may be called with various
17879735Sgad	 * signal handlers in effect.  After that initial startup, any
17979735Sgad	 * calls to printjob will have a *different* set of signal-handlers
18079735Sgad	 * in effect.  Make sure all handlers are the ones we want.
18179735Sgad	 */
18279735Sgad	signal(SIGCHLD, SIG_DFL);
1831553Srgrimes	signal(SIGHUP, abortpr);
1841553Srgrimes	signal(SIGINT, abortpr);
1851553Srgrimes	signal(SIGQUIT, abortpr);
1861553Srgrimes	signal(SIGTERM, abortpr);
1871553Srgrimes
1881553Srgrimes	/*
1891553Srgrimes	 * uses short form file names
1901553Srgrimes	 */
19131492Swollman	if (chdir(pp->spool_dir) < 0) {
19231492Swollman		syslog(LOG_ERR, "%s: %m", pp->spool_dir);
1931553Srgrimes		exit(1);
1941553Srgrimes	}
19531492Swollman	if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS))
1961553Srgrimes		exit(0);		/* printing disabled */
19731492Swollman	lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
19831492Swollman		   LOCK_FILE_MODE);
1991553Srgrimes	if (lfd < 0) {
20031492Swollman		if (errno == EWOULDBLOCK)	/* active daemon present */
20131492Swollman			exit(0);
20231492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
2031553Srgrimes		exit(1);
2041553Srgrimes	}
20531492Swollman	/* turn off non-blocking mode (was turned on for lock effects only) */
20631492Swollman	if (fcntl(lfd, F_SETFL, 0) < 0) {
20731492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
2081553Srgrimes		exit(1);
2091553Srgrimes	}
2101553Srgrimes	ftruncate(lfd, 0);
2111553Srgrimes	/*
2121553Srgrimes	 * write process id for others to know
2131553Srgrimes	 */
2141553Srgrimes	sprintf(line, "%u\n", pid);
2151553Srgrimes	pidoff = i = strlen(line);
2161553Srgrimes	if (write(lfd, line, i) != i) {
21731492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
2181553Srgrimes		exit(1);
2191553Srgrimes	}
2201553Srgrimes	/*
2211553Srgrimes	 * search the spool directory for work and sort by queue order.
2221553Srgrimes	 */
22331492Swollman	if ((nitems = getq(pp, &queue)) < 0) {
22431492Swollman		syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
22531492Swollman		       pp->spool_dir);
2261553Srgrimes		exit(1);
2271553Srgrimes	}
2281553Srgrimes	if (nitems == 0)		/* no work to do */
2291553Srgrimes		exit(0);
23031492Swollman	if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
23131492Swollman		if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
23231492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
23331492Swollman			       pp->lock_file);
2341553Srgrimes	}
23568664Sgad
23668664Sgad	/* create a file which will be used to hold stderr from filters */
23768664Sgad	if ((tempfd = mkstemp(tempstderr)) == -1) {
23868664Sgad		syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
23968664Sgad		       tempstderr);
24068732Sgad		exit(1);
24168664Sgad	}
24268664Sgad	if ((i = fchmod(tempfd, 0664)) == -1) {
24368664Sgad		syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
24468664Sgad		       tempstderr);
24568732Sgad		exit(1);
24668664Sgad	}
24768664Sgad	/* lpd doesn't need it to be open, it just needs it to exist */
24868664Sgad	close(tempfd);
24968664Sgad
25031492Swollman	openpr(pp);			/* open printer or remote */
2511553Srgrimesagain:
2521553Srgrimes	/*
2531553Srgrimes	 * we found something to do now do it --
2541553Srgrimes	 *    write the name of the current control file into the lock file
2551553Srgrimes	 *    so the spool queue program can tell what we're working on
2561553Srgrimes	 */
2571553Srgrimes	for (qp = queue; nitems--; free((char *) q)) {
2581553Srgrimes		q = *qp++;
25968401Sgad		if (stat(q->job_cfname, &stb) < 0)
2601553Srgrimes			continue;
26115648Sjoerg		errcnt = 0;
2621553Srgrimes	restart:
26315648Sjoerg		(void) lseek(lfd, pidoff, 0);
26468401Sgad		(void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
2651553Srgrimes		i = strlen(line);
2661553Srgrimes		if (write(lfd, line, i) != i)
26731492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
26831492Swollman			       pp->lock_file);
26931492Swollman		if (!pp->remote)
27068401Sgad			i = printit(pp, q->job_cfname);
2711553Srgrimes		else
27268401Sgad			i = sendit(pp, q->job_cfname);
2731553Srgrimes		/*
2741553Srgrimes		 * Check to see if we are supposed to stop printing or
2751553Srgrimes		 * if we are to rebuild the queue.
2761553Srgrimes		 */
2771553Srgrimes		if (fstat(lfd, &stb) == 0) {
2781553Srgrimes			/* stop printing before starting next job? */
27931492Swollman			if (stb.st_mode & LFM_PRINT_DIS)
2801553Srgrimes				goto done;
2811553Srgrimes			/* rebuild queue (after lpc topq) */
28231492Swollman			if (stb.st_mode & LFM_RESET_QUE) {
28331492Swollman				for (free(q); nitems--; free(q))
2841553Srgrimes					q = *qp++;
28531492Swollman				if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
28631492Swollman				    < 0)
2871553Srgrimes					syslog(LOG_WARNING, "%s: %s: %m",
28831492Swollman					       pp->printer, pp->lock_file);
2891553Srgrimes				break;
2901553Srgrimes			}
2911553Srgrimes		}
29268733Sgad		if (i == OK)		/* all files of this job printed */
29368733Sgad			jobcount++;
29415648Sjoerg		else if (i == REPRINT && ++errcnt < 5) {
29515648Sjoerg			/* try reprinting the job */
29631492Swollman			syslog(LOG_INFO, "restarting %s", pp->printer);
2971553Srgrimes			if (ofilter > 0) {
2981553Srgrimes				kill(ofilter, SIGCONT);	/* to be sure */
2991553Srgrimes				(void) close(ofd);
30015648Sjoerg				while ((i = wait(NULL)) > 0 && i != ofilter)
3011553Srgrimes					;
30279735Sgad				if (i < 0)
30379735Sgad					syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
30479735Sgad					    pp->printer, ofilter);
3051553Srgrimes				ofilter = 0;
3061553Srgrimes			}
3071553Srgrimes			(void) close(pfd);	/* close printer */
3081553Srgrimes			if (ftruncate(lfd, pidoff) < 0)
30931492Swollman				syslog(LOG_WARNING, "%s: %s: %m",
31031492Swollman				       pp->printer, pp->lock_file);
31131492Swollman			openpr(pp);		/* try to reopen printer */
3121553Srgrimes			goto restart;
31315648Sjoerg		} else {
31431492Swollman			syslog(LOG_WARNING, "%s: job could not be %s (%s)",
31531492Swollman			       pp->printer,
31631492Swollman			       pp->remote ? "sent to remote host" : "printed",
31768401Sgad			       q->job_cfname);
31815648Sjoerg			if (i == REPRINT) {
31927748Simp				/* ensure we don't attempt this job again */
32068401Sgad				(void) unlink(q->job_cfname);
32168401Sgad				q->job_cfname[0] = 'd';
32268401Sgad				(void) unlink(q->job_cfname);
32315648Sjoerg				if (logname[0])
32431492Swollman					sendmail(pp, logname, FATALERR);
32515648Sjoerg			}
3261553Srgrimes		}
3271553Srgrimes	}
32831492Swollman	free(queue);
3291553Srgrimes	/*
3301553Srgrimes	 * search the spool directory for more work.
3311553Srgrimes	 */
33231492Swollman	if ((nitems = getq(pp, &queue)) < 0) {
33331492Swollman		syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
33431492Swollman		       pp->spool_dir);
3351553Srgrimes		exit(1);
3361553Srgrimes	}
3371553Srgrimes	if (nitems == 0) {		/* no more work to do */
3381553Srgrimes	done:
33968733Sgad		if (jobcount > 0) {	/* jobs actually printed */
34031492Swollman			if (!pp->no_formfeed && !pp->tof)
34131492Swollman				(void) write(ofd, pp->form_feed,
34231492Swollman					     strlen(pp->form_feed));
34331492Swollman			if (pp->trailer != NULL) /* output trailer */
34431492Swollman				(void) write(ofd, pp->trailer,
34531492Swollman					     strlen(pp->trailer));
3461553Srgrimes		}
34719202Simp		(void) close(ofd);
34819202Simp		(void) wait(NULL);
34968664Sgad		(void) unlink(tempstderr);
3501553Srgrimes		exit(0);
3511553Srgrimes	}
3521553Srgrimes	goto again;
3531553Srgrimes}
3541553Srgrimes
3551553Srgrimeschar	fonts[4][50];	/* fonts for troff */
3561553Srgrimes
3571553Srgrimeschar ifonts[4][40] = {
3581553Srgrimes	_PATH_VFONTR,
3591553Srgrimes	_PATH_VFONTI,
3601553Srgrimes	_PATH_VFONTB,
3611553Srgrimes	_PATH_VFONTS,
3621553Srgrimes};
3631553Srgrimes
3641553Srgrimes/*
3651553Srgrimes * The remaining part is the reading of the control file (cf)
3661553Srgrimes * and performing the various actions.
3671553Srgrimes */
3681553Srgrimesstatic int
36978146Sgadprintit(struct printer *pp, char *file)
3701553Srgrimes{
3711553Srgrimes	register int i;
37268734Sgad	char *cp;
37368734Sgad	int bombed, didignorehdr;
3741553Srgrimes
37568734Sgad	bombed = OK;
37668734Sgad	didignorehdr = 0;
3771553Srgrimes	/*
3781553Srgrimes	 * open control file; ignore if no longer there.
3791553Srgrimes	 */
3801553Srgrimes	if ((cfp = fopen(file, "r")) == NULL) {
38131492Swollman		syslog(LOG_INFO, "%s: %s: %m", pp->printer, file);
3821553Srgrimes		return(OK);
3831553Srgrimes	}
3841553Srgrimes	/*
3851553Srgrimes	 * Reset troff fonts.
3861553Srgrimes	 */
3871553Srgrimes	for (i = 0; i < 4; i++)
3881553Srgrimes		strcpy(fonts[i], ifonts[i]);
38931492Swollman	sprintf(&width[2], "%ld", pp->page_width);
3901553Srgrimes	strcpy(indent+2, "0");
3911553Srgrimes
39268253Sgad	/* initialize job-specific count of datafiles processed */
39368253Sgad	job_dfcnt = 0;
39468253Sgad
3951553Srgrimes	/*
3961553Srgrimes	 *      read the control file for work to do
3971553Srgrimes	 *
3981553Srgrimes	 *      file format -- first character in the line is a command
3991553Srgrimes	 *      rest of the line is the argument.
4001553Srgrimes	 *      valid commands are:
4011553Srgrimes	 *
4021553Srgrimes	 *		S -- "stat info" for symbolic link protection
4031553Srgrimes	 *		J -- "job name" on banner page
4041553Srgrimes	 *		C -- "class name" on banner page
4051553Srgrimes	 *              L -- "literal" user's name to print on banner
4061553Srgrimes	 *		T -- "title" for pr
4071553Srgrimes	 *		H -- "host name" of machine where lpr was done
4081553Srgrimes	 *              P -- "person" user's login name
4091553Srgrimes	 *              I -- "indent" amount to indent output
41015648Sjoerg	 *		R -- laser dpi "resolution"
4111553Srgrimes	 *              f -- "file name" name of text file to print
4121553Srgrimes	 *		l -- "file name" text file with control chars
41383684Sgad	 *		o -- "file name" postscript file, according to
41483684Sgad	 *		     the RFC.  Here it is treated like an 'f'.
4151553Srgrimes	 *		p -- "file name" text file to print with pr(1)
4161553Srgrimes	 *		t -- "file name" troff(1) file to print
4171553Srgrimes	 *		n -- "file name" ditroff(1) file to print
4181553Srgrimes	 *		d -- "file name" dvi file to print
4191553Srgrimes	 *		g -- "file name" plot(1G) file to print
4201553Srgrimes	 *		v -- "file name" plain raster file to print
4211553Srgrimes	 *		c -- "file name" cifplot file to print
4221553Srgrimes	 *		1 -- "R font file" for troff
4231553Srgrimes	 *		2 -- "I font file" for troff
4241553Srgrimes	 *		3 -- "B font file" for troff
4251553Srgrimes	 *		4 -- "S font file" for troff
4261553Srgrimes	 *		N -- "name" of file (used by lpq)
4271553Srgrimes	 *              U -- "unlink" name of file to remove
4281553Srgrimes	 *                    (after we print it. (Pass 2 only)).
4291553Srgrimes	 *		M -- "mail" to user when done printing
43053956Sache	 *              Z -- "locale" for pr
4311553Srgrimes	 *
4321553Srgrimes	 *      getline reads a line and expands tabs to blanks
4331553Srgrimes	 */
4341553Srgrimes
4351553Srgrimes	/* pass 1 */
4361553Srgrimes
4371553Srgrimes	while (getline(cfp))
4381553Srgrimes		switch (line[0]) {
4391553Srgrimes		case 'H':
44078300Sgad			strlcpy(origin_host, line + 1, sizeof(origin_host));
44127748Simp			if (class[0] == '\0') {
44280133Sgad				strlcpy(class, line+1, sizeof(class));
44327748Simp			}
4441553Srgrimes			continue;
4451553Srgrimes
4461553Srgrimes		case 'P':
44780133Sgad			strlcpy(logname, line + 1, sizeof(logname));
44831492Swollman			if (pp->restricted) { /* restricted */
4491553Srgrimes				if (getpwnam(logname) == NULL) {
4501553Srgrimes					bombed = NOACCT;
45131492Swollman					sendmail(pp, line+1, bombed);
4521553Srgrimes					goto pass2;
4531553Srgrimes				}
4541553Srgrimes			}
4551553Srgrimes			continue;
4561553Srgrimes
4571553Srgrimes		case 'S':
4581553Srgrimes			cp = line+1;
4591553Srgrimes			i = 0;
4601553Srgrimes			while (*cp >= '0' && *cp <= '9')
4611553Srgrimes				i = i * 10 + (*cp++ - '0');
4621553Srgrimes			fdev = i;
4631553Srgrimes			cp++;
4641553Srgrimes			i = 0;
4651553Srgrimes			while (*cp >= '0' && *cp <= '9')
4661553Srgrimes				i = i * 10 + (*cp++ - '0');
4671553Srgrimes			fino = i;
4681553Srgrimes			continue;
4691553Srgrimes
4701553Srgrimes		case 'J':
47127748Simp			if (line[1] != '\0') {
47280133Sgad				strlcpy(jobname, line + 1, sizeof(jobname));
47327748Simp			} else
4741553Srgrimes				strcpy(jobname, " ");
4751553Srgrimes			continue;
4761553Srgrimes
4771553Srgrimes		case 'C':
4781553Srgrimes			if (line[1] != '\0')
47980133Sgad				strlcpy(class, line + 1, sizeof(class));
48080133Sgad			else if (class[0] == '\0') {
48180133Sgad				/* XXX - why call gethostname instead of
48280133Sgad				 *       just strlcpy'ing local_host? */
4831553Srgrimes				gethostname(class, sizeof(class));
48480133Sgad				class[sizeof(class) - 1] = '\0';
48580133Sgad			}
4861553Srgrimes			continue;
4871553Srgrimes
4881553Srgrimes		case 'T':	/* header title for pr */
48980133Sgad			strlcpy(title, line + 1, sizeof(title));
4901553Srgrimes			continue;
4911553Srgrimes
4921553Srgrimes		case 'L':	/* identification line */
49331492Swollman			if (!pp->no_header && !pp->header_last)
49431492Swollman				banner(pp, line+1, jobname);
4951553Srgrimes			continue;
4961553Srgrimes
4971553Srgrimes		case '1':	/* troff fonts */
4981553Srgrimes		case '2':
4991553Srgrimes		case '3':
5001553Srgrimes		case '4':
50127748Simp			if (line[1] != '\0') {
50280133Sgad				strlcpy(fonts[line[0]-'1'], line + 1,
50380133Sgad				    (size_t)50);
50427748Simp			}
5051553Srgrimes			continue;
5061553Srgrimes
5071553Srgrimes		case 'W':	/* page width */
50880133Sgad			strlcpy(width+2, line + 1, sizeof(width) - 2);
5091553Srgrimes			continue;
5101553Srgrimes
5111553Srgrimes		case 'I':	/* indent amount */
51280133Sgad			strlcpy(indent+2, line + 1, sizeof(indent) - 2);
5131553Srgrimes			continue;
5141553Srgrimes
51553956Sache		case 'Z':       /* locale for pr */
51680133Sgad			strlcpy(locale, line + 1, sizeof(locale));
51753956Sache			continue;
51853956Sache
5191553Srgrimes		default:	/* some file to print */
52068467Sgad			/* only lowercase cmd-codes include a file-to-print */
52168467Sgad			if ((line[0] < 'a') || (line[0] > 'z')) {
52268467Sgad				/* ignore any other lines */
52368467Sgad				if (lflag <= 1)
52468467Sgad					continue;
52568467Sgad				if (!didignorehdr) {
52668467Sgad					syslog(LOG_INFO, "%s: in %s :",
52768467Sgad					       pp->printer, file);
52868467Sgad					didignorehdr = 1;
52968467Sgad				}
53068467Sgad				syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
53168467Sgad				       pp->printer, line[0], &line[1]);
53268467Sgad				continue;
53368467Sgad			}
53468467Sgad			i = print(pp, line[0], line+1);
53568467Sgad			switch (i) {
5361553Srgrimes			case ERROR:
5371553Srgrimes				if (bombed == OK)
5381553Srgrimes					bombed = FATALERR;
5391553Srgrimes				break;
5401553Srgrimes			case REPRINT:
5411553Srgrimes				(void) fclose(cfp);
5421553Srgrimes				return(REPRINT);
5431553Srgrimes			case FILTERERR:
5441553Srgrimes			case ACCESS:
5451553Srgrimes				bombed = i;
54631492Swollman				sendmail(pp, logname, bombed);
5471553Srgrimes			}
5481553Srgrimes			title[0] = '\0';
5491553Srgrimes			continue;
5501553Srgrimes
5511553Srgrimes		case 'N':
5521553Srgrimes		case 'U':
5531553Srgrimes		case 'M':
55415648Sjoerg		case 'R':
5551553Srgrimes			continue;
5561553Srgrimes		}
5571553Srgrimes
5581553Srgrimes	/* pass 2 */
5591553Srgrimes
5601553Srgrimespass2:
5611553Srgrimes	fseek(cfp, 0L, 0);
5621553Srgrimes	while (getline(cfp))
5631553Srgrimes		switch (line[0]) {
5641553Srgrimes		case 'L':	/* identification line */
56531492Swollman			if (!pp->no_header && pp->header_last)
56631492Swollman				banner(pp, line+1, jobname);
5671553Srgrimes			continue;
5681553Srgrimes
5691553Srgrimes		case 'M':
5701553Srgrimes			if (bombed < NOACCT)	/* already sent if >= NOACCT */
57131492Swollman				sendmail(pp, line+1, bombed);
5721553Srgrimes			continue;
5731553Srgrimes
5741553Srgrimes		case 'U':
57527748Simp			if (strchr(line+1, '/'))
57627748Simp				continue;
5771553Srgrimes			(void) unlink(line+1);
5781553Srgrimes		}
5791553Srgrimes	/*
5801553Srgrimes	 * clean-up in case another control file exists
5811553Srgrimes	 */
5821553Srgrimes	(void) fclose(cfp);
5831553Srgrimes	(void) unlink(file);
5841553Srgrimes	return(bombed == OK ? OK : ERROR);
5851553Srgrimes}
5861553Srgrimes
5871553Srgrimes/*
5881553Srgrimes * Print a file.
5891553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
5901553Srgrimes * Return -1 if a non-recoverable error occured,
5911553Srgrimes * 2 if the filter detected some errors (but printed the job anyway),
5921553Srgrimes * 1 if we should try to reprint this job and
5931553Srgrimes * 0 if all is well.
5941553Srgrimes * Note: all filters take stdin as the file, stdout as the printer,
5951553Srgrimes * stderr as the log file, and must not ignore SIGINT.
5961553Srgrimes */
5971553Srgrimesstatic int
59878146Sgadprint(struct printer *pp, int format, char *file)
5991553Srgrimes{
60053956Sache	register int n, i;
6011553Srgrimes	register char *prog;
60231492Swollman	int fi, fo;
6031553Srgrimes	FILE *fp;
6041553Srgrimes	char *av[15], buf[BUFSIZ];
60597781Sgad	int pid, p[2], retcode, stopped, wstatus;
6061553Srgrimes	struct stat stb;
6071553Srgrimes
60868467Sgad	if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
60968467Sgad		syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
61068467Sgad		       pp->printer, file, format);
6111553Srgrimes		return(ERROR);
61268467Sgad	}
6131553Srgrimes	/*
6141553Srgrimes	 * Check to see if data file is a symbolic link. If so, it should
6151553Srgrimes	 * still point to the same file or someone is trying to print
6161553Srgrimes	 * something he shouldn't.
6171553Srgrimes	 */
6181553Srgrimes	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
6191553Srgrimes	    (stb.st_dev != fdev || stb.st_ino != fino))
6201553Srgrimes		return(ACCESS);
62168253Sgad
62268253Sgad	job_dfcnt++;		/* increment datafile counter for this job */
62368734Sgad	stopped = 0;		/* output filter is not stopped */
62468253Sgad
62568253Sgad	/* everything seems OK, start it up */
62631492Swollman	if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
62731492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
62831492Swollman		pp->tof = 1;
6291553Srgrimes	}
63031492Swollman	if (pp->filters[LPF_INPUT] == NULL
63183684Sgad	    && (format == 'f' || format == 'l' || format == 'o')) {
63231492Swollman		pp->tof = 0;
6331553Srgrimes		while ((n = read(fi, buf, BUFSIZ)) > 0)
6341553Srgrimes			if (write(ofd, buf, n) != n) {
6351553Srgrimes				(void) close(fi);
6361553Srgrimes				return(REPRINT);
6371553Srgrimes			}
6381553Srgrimes		(void) close(fi);
6391553Srgrimes		return(OK);
6401553Srgrimes	}
6411553Srgrimes	switch (format) {
6421553Srgrimes	case 'p':	/* print file using 'pr' */
64331492Swollman		if (pp->filters[LPF_INPUT] == NULL) {	/* use output filter */
6441553Srgrimes			prog = _PATH_PR;
64553956Sache			i = 0;
64653956Sache			av[i++] = "pr";
64753956Sache			av[i++] = width;
64853956Sache			av[i++] = length;
64953956Sache			av[i++] = "-h";
65053956Sache			av[i++] = *title ? title : " ";
65153956Sache			av[i++] = "-L";
65253956Sache			av[i++] = *locale ? locale : "C";
65353956Sache			av[i++] = "-F";
65453956Sache			av[i] = 0;
6551553Srgrimes			fo = ofd;
6561553Srgrimes			goto start;
6571553Srgrimes		}
6581553Srgrimes		pipe(p);
65931492Swollman		if ((prchild = dofork(pp, DORETURN)) == 0) {	/* child */
6601553Srgrimes			dup2(fi, 0);		/* file is stdin */
6611553Srgrimes			dup2(p[1], 1);		/* pipe is stdout */
6628094Sjkh			closelog();
66331492Swollman			closeallfds(3);
6641553Srgrimes			execl(_PATH_PR, "pr", width, length,
66553956Sache			    "-h", *title ? title : " ",
66653956Sache			    "-L", *locale ? locale : "C",
66779452Sbrian			    "-F", (char *)0);
6681553Srgrimes			syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
6691553Srgrimes			exit(2);
6701553Srgrimes		}
6711553Srgrimes		(void) close(p[1]);		/* close output side */
6721553Srgrimes		(void) close(fi);
6731553Srgrimes		if (prchild < 0) {
6741553Srgrimes			prchild = 0;
6751553Srgrimes			(void) close(p[0]);
6761553Srgrimes			return(ERROR);
6771553Srgrimes		}
6781553Srgrimes		fi = p[0];			/* use pipe for input */
6791553Srgrimes	case 'f':	/* print plain text file */
68031492Swollman		prog = pp->filters[LPF_INPUT];
6811553Srgrimes		av[1] = width;
6821553Srgrimes		av[2] = length;
6831553Srgrimes		av[3] = indent;
6841553Srgrimes		n = 4;
6851553Srgrimes		break;
68686935Sgad	case 'o':	/* print postscript file */
68786935Sgad		/*
68886935Sgad		 * Treat this as a "plain file with control characters", and
68986935Sgad		 * assume the standard LPF_INPUT filter will recognize that
69086935Sgad		 * the data is postscript and know what to do with it.  These
69186935Sgad		 * 'o'-file requests could come from MacOS 10.1 systems.
69286935Sgad		 * (later versions of MacOS 10 will explicitly use 'l')
69386935Sgad		 * A postscript file can contain binary data, which is why 'l'
69486935Sgad		 * is somewhat more appropriate than 'f'.
69586935Sgad		 */
69686935Sgad		/* FALLTHROUGH */
6971553Srgrimes	case 'l':	/* like 'f' but pass control characters */
69831492Swollman		prog = pp->filters[LPF_INPUT];
6991553Srgrimes		av[1] = "-c";
7001553Srgrimes		av[2] = width;
7011553Srgrimes		av[3] = length;
7021553Srgrimes		av[4] = indent;
7031553Srgrimes		n = 5;
7041553Srgrimes		break;
7051553Srgrimes	case 'r':	/* print a fortran text file */
70631492Swollman		prog = pp->filters[LPF_FORTRAN];
7071553Srgrimes		av[1] = width;
7081553Srgrimes		av[2] = length;
7091553Srgrimes		n = 3;
7101553Srgrimes		break;
7111553Srgrimes	case 't':	/* print troff output */
7121553Srgrimes	case 'n':	/* print ditroff output */
7131553Srgrimes	case 'd':	/* print tex output */
7141553Srgrimes		(void) unlink(".railmag");
7151553Srgrimes		if ((fo = creat(".railmag", FILMOD)) < 0) {
71631492Swollman			syslog(LOG_ERR, "%s: cannot create .railmag",
71731492Swollman			       pp->printer);
7181553Srgrimes			(void) unlink(".railmag");
7191553Srgrimes		} else {
7201553Srgrimes			for (n = 0; n < 4; n++) {
7211553Srgrimes				if (fonts[n][0] != '/')
7221553Srgrimes					(void) write(fo, _PATH_VFONT,
7231553Srgrimes					    sizeof(_PATH_VFONT) - 1);
7241553Srgrimes				(void) write(fo, fonts[n], strlen(fonts[n]));
7251553Srgrimes				(void) write(fo, "\n", 1);
7261553Srgrimes			}
7271553Srgrimes			(void) close(fo);
7281553Srgrimes		}
72931492Swollman		prog = (format == 't') ? pp->filters[LPF_TROFF]
73031492Swollman			: ((format == 'n') ? pp->filters[LPF_DITROFF]
73131492Swollman			   : pp->filters[LPF_DVI]);
7321553Srgrimes		av[1] = pxwidth;
7331553Srgrimes		av[2] = pxlength;
7341553Srgrimes		n = 3;
7351553Srgrimes		break;
7361553Srgrimes	case 'c':	/* print cifplot output */
73731492Swollman		prog = pp->filters[LPF_CIFPLOT];
7381553Srgrimes		av[1] = pxwidth;
7391553Srgrimes		av[2] = pxlength;
7401553Srgrimes		n = 3;
7411553Srgrimes		break;
7421553Srgrimes	case 'g':	/* print plot(1G) output */
74331492Swollman		prog = pp->filters[LPF_GRAPH];
7441553Srgrimes		av[1] = pxwidth;
7451553Srgrimes		av[2] = pxlength;
7461553Srgrimes		n = 3;
7471553Srgrimes		break;
7481553Srgrimes	case 'v':	/* print raster output */
74931492Swollman		prog = pp->filters[LPF_RASTER];
7501553Srgrimes		av[1] = pxwidth;
7511553Srgrimes		av[2] = pxlength;
7521553Srgrimes		n = 3;
7531553Srgrimes		break;
7541553Srgrimes	default:
7551553Srgrimes		(void) close(fi);
7561553Srgrimes		syslog(LOG_ERR, "%s: illegal format character '%c'",
75731492Swollman			pp->printer, format);
7581553Srgrimes		return(ERROR);
7591553Srgrimes	}
76015648Sjoerg	if (prog == NULL) {
76115648Sjoerg		(void) close(fi);
76215648Sjoerg		syslog(LOG_ERR,
76315648Sjoerg		   "%s: no filter found in printcap for format character '%c'",
76431492Swollman		   pp->printer, format);
76515648Sjoerg		return(ERROR);
76615648Sjoerg	}
76727635Simp	if ((av[0] = strrchr(prog, '/')) != NULL)
7681553Srgrimes		av[0]++;
7691553Srgrimes	else
7701553Srgrimes		av[0] = prog;
7711553Srgrimes	av[n++] = "-n";
7721553Srgrimes	av[n++] = logname;
7731553Srgrimes	av[n++] = "-h";
77478300Sgad	av[n++] = origin_host;
77531492Swollman	av[n++] = pp->acct_file;
7761553Srgrimes	av[n] = 0;
7771553Srgrimes	fo = pfd;
7781553Srgrimes	if (ofilter > 0) {		/* stop output filter */
7791553Srgrimes		write(ofd, "\031\1", 2);
7801553Srgrimes		while ((pid =
78197781Sgad		    wait3(&wstatus, WUNTRACED, 0)) > 0 && pid != ofilter)
7821553Srgrimes			;
78379735Sgad		if (pid < 0)
78479735Sgad			syslog(LOG_WARNING, "%s: after stopping 'of', wait3() returned: %m",
78579735Sgad			    pp->printer);
78697781Sgad		else if (!WIFSTOPPED(wstatus)) {
7871553Srgrimes			(void) close(fi);
78897781Sgad			syslog(LOG_WARNING, "%s: output filter died "
78997781Sgad			    "(pid=%d retcode=%d termsig=%d)",
79097781Sgad			    pp->printer, ofilter, WEXITSTATUS(wstatus),
79197781Sgad			    WTERMSIG(wstatus));
7921553Srgrimes			return(REPRINT);
7931553Srgrimes		}
7941553Srgrimes		stopped++;
7951553Srgrimes	}
7961553Srgrimesstart:
79731492Swollman	if ((child = dofork(pp, DORETURN)) == 0) { /* child */
7981553Srgrimes		dup2(fi, 0);
7991553Srgrimes		dup2(fo, 1);
80068664Sgad		/* setup stderr for the filter (child process)
80168664Sgad		 * so it goes to our temporary errors file */
80268664Sgad		n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
8031553Srgrimes		if (n >= 0)
8041553Srgrimes			dup2(n, 2);
8058094Sjkh		closelog();
80631492Swollman		closeallfds(3);
8071553Srgrimes		execv(prog, av);
80895067Sgad		syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
80995067Sgad		    prog);
8101553Srgrimes		exit(2);
8111553Srgrimes	}
8121553Srgrimes	(void) close(fi);
8131553Srgrimes	if (child < 0)
81497781Sgad		retcode = 100;
81579735Sgad	else {
81697781Sgad		while ((pid = wait(&wstatus)) > 0 && pid != child)
8171553Srgrimes			;
81879735Sgad		if (pid < 0) {
81997781Sgad			retcode = 100;
82079735Sgad			syslog(LOG_WARNING, "%s: after execv(%s), wait() returned: %m",
82179735Sgad			    pp->printer, prog);
82297781Sgad		} else
82397781Sgad			retcode = WEXITSTATUS(wstatus);
82479735Sgad	}
8251553Srgrimes	child = 0;
8261553Srgrimes	prchild = 0;
8271553Srgrimes	if (stopped) {		/* restart output filter */
8281553Srgrimes		if (kill(ofilter, SIGCONT) < 0) {
8291553Srgrimes			syslog(LOG_ERR, "cannot restart output filter");
8301553Srgrimes			exit(1);
8311553Srgrimes		}
8321553Srgrimes	}
83331492Swollman	pp->tof = 0;
8341553Srgrimes
83568664Sgad	/* Copy the filter's output to "lf" logfile */
83668664Sgad	if ((fp = fopen(tempstderr, "r"))) {
8371553Srgrimes		while (fgets(buf, sizeof(buf), fp))
8381553Srgrimes			fputs(buf, stderr);
8391553Srgrimes		fclose(fp);
8401553Srgrimes	}
8411553Srgrimes
84297781Sgad	if (!WIFEXITED(wstatus)) {
84315648Sjoerg		syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
84497781Sgad		    pp->printer, format, WTERMSIG(wstatus));
8451553Srgrimes		return(ERROR);
8461553Srgrimes	}
84797781Sgad	switch (retcode) {
8481553Srgrimes	case 0:
84931492Swollman		pp->tof = 1;
8501553Srgrimes		return(OK);
8511553Srgrimes	case 1:
8521553Srgrimes		return(REPRINT);
85315648Sjoerg	case 2:
85415648Sjoerg		return(ERROR);
8551553Srgrimes	default:
85615648Sjoerg		syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
85797781Sgad			pp->printer, format, retcode);
85815648Sjoerg		return(FILTERERR);
8591553Srgrimes	}
8601553Srgrimes}
8611553Srgrimes
8621553Srgrimes/*
8631553Srgrimes * Send the daemon control file (cf) and any data files.
8641553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and
8651553Srgrimes * 0 if all is well.
8661553Srgrimes */
8671553Srgrimesstatic int
86878146Sgadsendit(struct printer *pp, char *file)
8691553Srgrimes{
87095293Sgad	int dfcopies, err, i;
8711553Srgrimes	char *cp, last[BUFSIZ];
8721553Srgrimes
8731553Srgrimes	/*
8741553Srgrimes	 * open control file
8751553Srgrimes	 */
8761553Srgrimes	if ((cfp = fopen(file, "r")) == NULL)
8771553Srgrimes		return(OK);
87868253Sgad
87968253Sgad	/* initialize job-specific count of datafiles processed */
88068253Sgad	job_dfcnt = 0;
88168253Sgad
8821553Srgrimes	/*
8831553Srgrimes	 *      read the control file for work to do
8841553Srgrimes	 *
8851553Srgrimes	 *      file format -- first character in the line is a command
8861553Srgrimes	 *      rest of the line is the argument.
8871553Srgrimes	 *      commands of interest are:
8881553Srgrimes	 *
8891553Srgrimes	 *            a-z -- "file name" name of file to print
8901553Srgrimes	 *              U -- "unlink" name of file to remove
8911553Srgrimes	 *                    (after we print it. (Pass 2 only)).
8921553Srgrimes	 */
8931553Srgrimes
8941553Srgrimes	/*
8951553Srgrimes	 * pass 1
8961553Srgrimes	 */
89795293Sgad	err = OK;
8981553Srgrimes	while (getline(cfp)) {
8991553Srgrimes	again:
9001553Srgrimes		if (line[0] == 'S') {
9011553Srgrimes			cp = line+1;
9021553Srgrimes			i = 0;
9031553Srgrimes			while (*cp >= '0' && *cp <= '9')
9041553Srgrimes				i = i * 10 + (*cp++ - '0');
9051553Srgrimes			fdev = i;
9061553Srgrimes			cp++;
9071553Srgrimes			i = 0;
9081553Srgrimes			while (*cp >= '0' && *cp <= '9')
9091553Srgrimes				i = i * 10 + (*cp++ - '0');
9101553Srgrimes			fino = i;
91124831Sbrian		} else if (line[0] == 'H') {
91278300Sgad			strlcpy(origin_host, line + 1, sizeof(origin_host));
91368343Sgad			if (class[0] == '\0') {
91480133Sgad				strlcpy(class, line + 1, sizeof(class));
91568343Sgad			}
91624831Sbrian		} else if (line[0] == 'P') {
91780133Sgad			strlcpy(logname, line + 1, sizeof(logname));
91831492Swollman			if (pp->restricted) { /* restricted */
91924831Sbrian				if (getpwnam(logname) == NULL) {
92031492Swollman					sendmail(pp, line+1, NOACCT);
92124831Sbrian					err = ERROR;
92224831Sbrian					break;
92324831Sbrian				}
92424831Sbrian			}
92524831Sbrian		} else if (line[0] == 'I') {
92680133Sgad			strlcpy(indent+2, line + 1, sizeof(indent) - 2);
92724831Sbrian		} else if (line[0] >= 'a' && line[0] <= 'z') {
92895293Sgad			dfcopies = 1;
9291553Srgrimes			strcpy(last, line);
93095293Sgad			while ((i = getline(cfp)) != 0) {
93195293Sgad				if (strcmp(last, line) != 0)
9321553Srgrimes					break;
93395293Sgad				dfcopies++;
93495293Sgad			}
93595293Sgad			switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
9361553Srgrimes			case OK:
9371553Srgrimes				if (i)
9381553Srgrimes					goto again;
9391553Srgrimes				break;
9401553Srgrimes			case REPRINT:
9411553Srgrimes				(void) fclose(cfp);
9421553Srgrimes				return(REPRINT);
9431553Srgrimes			case ACCESS:
94431492Swollman				sendmail(pp, logname, ACCESS);
9451553Srgrimes			case ERROR:
9461553Srgrimes				err = ERROR;
9471553Srgrimes			}
9481553Srgrimes			break;
9491553Srgrimes		}
9501553Srgrimes	}
95195293Sgad	if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
9521553Srgrimes		(void) fclose(cfp);
9531553Srgrimes		return(REPRINT);
9541553Srgrimes	}
9551553Srgrimes	/*
9561553Srgrimes	 * pass 2
9571553Srgrimes	 */
9581553Srgrimes	fseek(cfp, 0L, 0);
9591553Srgrimes	while (getline(cfp))
96027748Simp		if (line[0] == 'U' && !strchr(line+1, '/'))
9611553Srgrimes			(void) unlink(line+1);
9621553Srgrimes	/*
9631553Srgrimes	 * clean-up in case another control file exists
9641553Srgrimes	 */
9651553Srgrimes	(void) fclose(cfp);
9661553Srgrimes	(void) unlink(file);
9671553Srgrimes	return(err);
9681553Srgrimes}
9691553Srgrimes
9701553Srgrimes/*
9711553Srgrimes * Send a data file to the remote machine and spool it.
9721553Srgrimes * Return positive if we should try resending.
9731553Srgrimes */
9741553Srgrimesstatic int
97595293Sgadsendfile(struct printer *pp, int type, char *file, char format, int copyreq)
9761553Srgrimes{
97794036Sgad	int i, amt;
9781553Srgrimes	struct stat stb;
97994032Sgad	char *av[15], *filtcmd;
98094032Sgad	char buf[BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
98195293Sgad	int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;
9821553Srgrimes
98374124Sgad	statrc = lstat(file, &stb);
98474124Sgad	if (statrc < 0) {
98574124Sgad		syslog(LOG_ERR, "%s: error from lstat(%s): %m",
98674124Sgad		    pp->printer, file);
98794036Sgad		return (ERROR);
98874124Sgad	}
98994036Sgad	sfd = open(file, O_RDONLY);
99094036Sgad	if (sfd < 0) {
99174124Sgad		syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
99274124Sgad		    pp->printer, file);
99394036Sgad		return (ERROR);
99474124Sgad	}
9951553Srgrimes	/*
9961553Srgrimes	 * Check to see if data file is a symbolic link. If so, it should
9971553Srgrimes	 * still point to the same file or someone is trying to print something
9981553Srgrimes	 * he shouldn't.
9991553Srgrimes	 */
100094036Sgad	if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
100194036Sgad	    (stb.st_dev != fdev || stb.st_ino != fino)) {
100294036Sgad		close(sfd);
100394036Sgad		return (ACCESS);
100494036Sgad	}
100524831Sbrian
100694032Sgad	/* Everything seems OK for reading the file, now to send it */
100794032Sgad	filtcmd = NULL;
100824831Sbrian	sizerr = 0;
100994032Sgad	tfd = -1;
101024831Sbrian	if (type == '\3') {
101194032Sgad		/*
101294032Sgad		 * Type == 3 means this is a datafile, not a control file.
101394032Sgad		 * Increment the counter of data-files in this job, and
101494032Sgad		 * then check for input or output filters (which are only
101594032Sgad		 * applied to datafiles, not control files).
101694032Sgad		 */
101794032Sgad		job_dfcnt++;
101894032Sgad
101994032Sgad		/*
102094032Sgad		 * Note that here we are filtering datafiles, one at a time,
102194032Sgad		 * as they are sent to the remote machine.  Here, the *only*
102294032Sgad		 * difference between an input filter (`if=') and an output
102394032Sgad		 * filter (`of=') is the argument list that the filter is
102494032Sgad		 * started up with.  Here, the output filter is executed
102594032Sgad		 * for each individual file as it is sent.  This is not the
102694032Sgad		 * same as local print queues, where the output filter is
102794032Sgad		 * started up once, and then all jobs are passed thru that
102894032Sgad		 * single invocation of the output filter.
102994032Sgad		 *
103094032Sgad		 * Also note that a queue for a remote-machine can have an
103194032Sgad		 * input filter or an output filter, but not both.
103294032Sgad		 */
103331492Swollman		if (pp->filters[LPF_INPUT]) {
103494032Sgad			filtcmd = pp->filters[LPF_INPUT];
103594032Sgad			av[0] = filtcmd;
103694032Sgad			narg = 0;
103794032Sgad			strcpy(opt_c, "-c");
103894032Sgad			strcpy(opt_h, "-h");
103994032Sgad			strcpy(opt_n, "-n");
104024831Sbrian			if (format == 'l')
104194032Sgad				av[++narg] = opt_c;
104294032Sgad			av[++narg] = width;
104394032Sgad			av[++narg] = length;
104494032Sgad			av[++narg] = indent;
104594032Sgad			av[++narg] = opt_n;
104694032Sgad			av[++narg] = logname;
104794032Sgad			av[++narg] = opt_h;
104894032Sgad			av[++narg] = origin_host;
104994032Sgad			av[++narg] = pp->acct_file;
105094032Sgad			av[++narg] = NULL;
105194032Sgad		} else if (pp->filters[LPF_OUTPUT]) {
105294032Sgad			filtcmd = pp->filters[LPF_OUTPUT];
105394032Sgad			av[0] = filtcmd;
105494032Sgad			narg = 0;
105594032Sgad			av[++narg] = width;
105694032Sgad			av[++narg] = length;
105794032Sgad			av[++narg] = NULL;
105824831Sbrian		}
105924831Sbrian	}
106094032Sgad	if (filtcmd) {
106194032Sgad		/*
106294032Sgad		 * If there is an input or output filter, we have to run
106394032Sgad		 * the datafile thru that filter and store the result as
106494032Sgad		 * a temporary spool file, because the protocol requires
106594032Sgad		 * that we send the remote host the file-size before we
106694032Sgad		 * start to send any of the data.
106794032Sgad		 */
106894032Sgad		strcpy(tfile, TFILENAME);
106994032Sgad		tfd = mkstemp(tfile);
107094032Sgad		if (tfd == -1) {
107194032Sgad			syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
107294032Sgad			    TFILENAME);
107394036Sgad			sfres = ERROR;
107494036Sgad			goto return_sfres;
107594032Sgad		}
107694036Sgad		filtstat = execfilter(pp, filtcmd, av, sfd, tfd);
107724831Sbrian
107894032Sgad		/* process the return-code from the filter */
107994032Sgad		switch (filtstat) {
108094032Sgad		case 0:
108194032Sgad			break;
108294032Sgad		case 1:
108394036Sgad			sfres = REPRINT;
108494036Sgad			goto return_sfres;
108594032Sgad		case 2:
108694036Sgad			sfres = ERROR;
108794036Sgad			goto return_sfres;
108894032Sgad		default:
108994032Sgad			syslog(LOG_WARNING,
109094032Sgad			    "%s: filter '%c' exited (retcode=%d)",
109194032Sgad			    pp->printer, format, filtstat);
109294036Sgad			sfres = FILTERERR;
109394036Sgad			goto return_sfres;
109494032Sgad		}
109594032Sgad		statrc = fstat(tfd, &stb);   /* to find size of tfile */
109694032Sgad		if (statrc < 0)	{
109794032Sgad			syslog(LOG_ERR,
109894032Sgad			    "%s: error processing 'if', fstat(%s): %m",
109994032Sgad			    pp->printer, tfile);
110094036Sgad			sfres = ERROR;
110194036Sgad			goto return_sfres;
110294032Sgad		}
110394036Sgad		close(sfd);
110494036Sgad		sfd = tfd;
110594036Sgad		lseek(sfd, 0, SEEK_SET);
110694032Sgad	}
110794032Sgad
110895293Sgad	copycnt = 0;
110995293Sgadsendagain:
111095293Sgad	copycnt++;
111195293Sgad
111295293Sgad	if (copycnt < 2)
111395293Sgad		(void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file);
111495293Sgad	else
111595293Sgad		(void) sprintf(buf, "%c%qd %s_c%d\n", type, stb.st_size,
111695293Sgad		    file, copycnt);
11171553Srgrimes	amt = strlen(buf);
11181553Srgrimes	for (i = 0;  ; i++) {
11191553Srgrimes		if (write(pfd, buf, amt) != amt ||
112031492Swollman		    (resp = response(pp)) < 0 || resp == '\1') {
112194036Sgad			sfres = REPRINT;
112294036Sgad			goto return_sfres;
11231553Srgrimes		} else if (resp == '\0')
11241553Srgrimes			break;
11251553Srgrimes		if (i == 0)
112631492Swollman			pstatus(pp,
112731492Swollman				"no space on remote; waiting for queue to drain");
11281553Srgrimes		if (i == 10)
11291553Srgrimes			syslog(LOG_ALERT, "%s: can't send to %s; queue full",
113031492Swollman				pp->printer, pp->remote_host);
11311553Srgrimes		sleep(5 * 60);
11321553Srgrimes	}
11331553Srgrimes	if (i)
113431492Swollman		pstatus(pp, "sending to %s", pp->remote_host);
113595293Sgad	/*
113695293Sgad	 * XXX - we should change trstat_init()/trstat_write() to include
113795293Sgad	 *	 the copycnt in the statistics record it may write.
113895293Sgad	 */
113968253Sgad	if (type == '\3')
114068253Sgad		trstat_init(pp, file, job_dfcnt);
11411553Srgrimes	for (i = 0; i < stb.st_size; i += BUFSIZ) {
11421553Srgrimes		amt = BUFSIZ;
11431553Srgrimes		if (i + amt > stb.st_size)
11441553Srgrimes			amt = stb.st_size - i;
114594036Sgad		if (sizerr == 0 && read(sfd, buf, amt) != amt)
11461553Srgrimes			sizerr = 1;
11471553Srgrimes		if (write(pfd, buf, amt) != amt) {
114894036Sgad			sfres = REPRINT;
114994036Sgad			goto return_sfres;
11501553Srgrimes		}
11511553Srgrimes	}
11521553Srgrimes
11531553Srgrimes	if (sizerr) {
115431492Swollman		syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
11551553Srgrimes		/* tell recvjob to ignore this file */
11561553Srgrimes		(void) write(pfd, "\1", 1);
115794036Sgad		sfres = ERROR;
115894036Sgad		goto return_sfres;
11591553Srgrimes	}
116031492Swollman	if (write(pfd, "", 1) != 1 || response(pp)) {
116194036Sgad		sfres = REPRINT;
116294036Sgad		goto return_sfres;
116324831Sbrian	}
116495293Sgad	if (type == '\3') {
116568253Sgad		trstat_write(pp, TR_SENDING, stb.st_size, logname,
116695293Sgad		    pp->remote_host, origin_host);
116795293Sgad		/*
116895293Sgad		 * Usually we only need to send one copy of a datafile,
116995293Sgad		 * because the control-file will simply print the same
117095293Sgad		 * file multiple times.  However, some printers ignore
117195293Sgad		 * the control file, and simply print each data file as
117295293Sgad		 * it arrives.  For such "remote hosts", we need to
117395293Sgad		 * transfer the same data file multiple times.  Such a
117495293Sgad		 * a host is indicated by adding 'rc' to the printcap
117595293Sgad		 * entry.
117695293Sgad		 * XXX - Right now this ONLY works for remote hosts which
117795293Sgad		 *	do ignore the name of the data file, because
117895293Sgad		 *	this sends the file multiple times with slight
117995293Sgad		 *	changes to the filename.  To do this right would
118095293Sgad		 *	require that we also rewrite the control file
118195293Sgad		 *	to match those filenames.
118295293Sgad		 */
118395293Sgad		if (pp->resend_copies && (copycnt < copyreq)) {
118495293Sgad			lseek(sfd, 0, SEEK_SET);
118595293Sgad			goto sendagain;
118695293Sgad		}
118795293Sgad	}
118894036Sgad	sfres = OK;
118994036Sgad
119094036Sgadreturn_sfres:
119194036Sgad	(void)close(sfd);
119294036Sgad	if (tfd != -1) {
119394036Sgad		/*
119494036Sgad		 * If tfd is set, then it is the same value as sfd, and
119594036Sgad		 * therefore it is already closed at this point.  All
119694036Sgad		 * we need to do is remove the temporary file.
119794036Sgad		 */
119894036Sgad		tfd = -1;
119994036Sgad		unlink(tfile);
120094036Sgad	}
120194036Sgad	return (sfres);
12021553Srgrimes}
12031553Srgrimes
12041553Srgrimes/*
120594032Sgad *  This routine is called to execute one of the filters as was
120694036Sgad *  specified in a printcap entry.  While the child-process will read
120794036Sgad *  all of 'infd', it is up to the caller to close that file descriptor
120894036Sgad *  in the parent process.
120994032Sgad */
121094032Sgadstatic int
121194032Sgadexecfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
121294032Sgad{
121397781Sgad	int errfd, fpid, retcode, wpid, wstatus;
121494032Sgad	FILE *errfp;
121594032Sgad	char buf[BUFSIZ], *slash;
121694032Sgad
121794032Sgad	fpid = dofork(pp, DORETURN);
121894032Sgad	if (fpid != 0) {
121994032Sgad		/*
122094032Sgad		 * This is the parent process, which just waits for the child
122194032Sgad		 * to complete and then returns the result.  Note that it is
122294032Sgad		 * the child process which reads the input stream.
122394032Sgad		 */
122494032Sgad		if (fpid < 0)
122597781Sgad			retcode = 100;
122694032Sgad		else {
122797781Sgad			while ((wpid = wait(&wstatus)) > 0 &&
122894032Sgad			    wpid != fpid)
122994032Sgad				;
123094032Sgad			if (wpid < 0) {
123197781Sgad				retcode = 100;
123294032Sgad				syslog(LOG_WARNING,
123394032Sgad				    "%s: after execv(%s), wait() returned: %m",
123494032Sgad				    pp->printer, f_cmd);
123597781Sgad			} else
123697781Sgad				retcode = WEXITSTATUS(wstatus);
123794032Sgad		}
123894032Sgad
123994032Sgad		/*
124094032Sgad		 * Copy everything the filter wrote to stderr from our
124194032Sgad		 * temporary errors file to the "lf=" logfile.
124294032Sgad		 */
124394032Sgad		errfp = fopen(tempstderr, "r");
124494032Sgad		if (errfp) {
124594032Sgad			while (fgets(buf, sizeof(buf), errfp))
124694032Sgad				fputs(buf, stderr);
124794032Sgad			fclose(errfp);
124894032Sgad		}
124994032Sgad
125097781Sgad		return (retcode);
125194032Sgad	}
125294032Sgad
125394032Sgad	/*
125494032Sgad	 * This is the child process, which is the one that executes the
125594032Sgad	 * given filter.
125694032Sgad	 */
125794032Sgad	/*
125894032Sgad	 * If the first parameter has any slashes in it, then change it
125994032Sgad	 * to point to the first character after the last slash.
126094032Sgad	 */
126194032Sgad	slash = strrchr(f_av[0], '/');
126294032Sgad	if (slash != NULL)
126394032Sgad		f_av[0] = slash + 1;
126494032Sgad	/*
126594032Sgad	 * XXX - in the future, this should setup an explicit list of
126694032Sgad	 *       environment variables and use execve()!
126794032Sgad	 */
126894032Sgad
126994032Sgad	/*
127094032Sgad	 * Setup stdin, stdout, and stderr as we want them when the filter
127194032Sgad	 * is running.  Stderr is setup so it points to a temporary errors
127294032Sgad	 * file, and the parent process will copy that temporary file to
127394032Sgad	 * the real logfile after the filter completes.
127494032Sgad	 */
127594032Sgad	dup2(infd, 0);
127694032Sgad	dup2(outfd, 1);
127794032Sgad	errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
127894032Sgad	if (errfd >= 0)
127994032Sgad		dup2(errfd, 2);
128094032Sgad	closelog();
128194032Sgad	closeallfds(3);
128294032Sgad	execv(f_cmd, f_av);
128395067Sgad	syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
128494032Sgad	exit(2);
128594032Sgad	/* NOTREACHED */
128694032Sgad}
128794032Sgad
128894032Sgad/*
12891553Srgrimes * Check to make sure there have been no errors and that both programs
12901553Srgrimes * are in sync with eachother.
12911553Srgrimes * Return non-zero if the connection was lost.
12921553Srgrimes */
12931553Srgrimesstatic char
129478146Sgadresponse(const struct printer *pp)
12951553Srgrimes{
12961553Srgrimes	char resp;
12971553Srgrimes
12981553Srgrimes	if (read(pfd, &resp, 1) != 1) {
129931492Swollman		syslog(LOG_INFO, "%s: lost connection", pp->printer);
13001553Srgrimes		return(-1);
13011553Srgrimes	}
13021553Srgrimes	return(resp);
13031553Srgrimes}
13041553Srgrimes
13051553Srgrimes/*
13061553Srgrimes * Banner printing stuff
13071553Srgrimes */
13081553Srgrimesstatic void
130978146Sgadbanner(struct printer *pp, char *name1, char *name2)
13101553Srgrimes{
13111553Srgrimes	time_t tvec;
13121553Srgrimes
13131553Srgrimes	time(&tvec);
131431492Swollman	if (!pp->no_formfeed && !pp->tof)
131531492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
131631492Swollman	if (pp->short_banner) {	/* short banner only */
13171553Srgrimes		if (class[0]) {
13181553Srgrimes			(void) write(ofd, class, strlen(class));
13191553Srgrimes			(void) write(ofd, ":", 1);
13201553Srgrimes		}
13211553Srgrimes		(void) write(ofd, name1, strlen(name1));
13221553Srgrimes		(void) write(ofd, "  Job: ", 7);
13231553Srgrimes		(void) write(ofd, name2, strlen(name2));
13241553Srgrimes		(void) write(ofd, "  Date: ", 8);
13251553Srgrimes		(void) write(ofd, ctime(&tvec), 24);
13261553Srgrimes		(void) write(ofd, "\n", 1);
13271553Srgrimes	} else {	/* normal banner */
13281553Srgrimes		(void) write(ofd, "\n\n\n", 3);
132931492Swollman		scan_out(pp, ofd, name1, '\0');
13301553Srgrimes		(void) write(ofd, "\n\n", 2);
133131492Swollman		scan_out(pp, ofd, name2, '\0');
13321553Srgrimes		if (class[0]) {
13331553Srgrimes			(void) write(ofd,"\n\n\n",3);
133431492Swollman			scan_out(pp, ofd, class, '\0');
13351553Srgrimes		}
13361553Srgrimes		(void) write(ofd, "\n\n\n\n\t\t\t\t\tJob:  ", 15);
13371553Srgrimes		(void) write(ofd, name2, strlen(name2));
13381553Srgrimes		(void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
13391553Srgrimes		(void) write(ofd, ctime(&tvec), 24);
13401553Srgrimes		(void) write(ofd, "\n", 1);
13411553Srgrimes	}
134231492Swollman	if (!pp->no_formfeed)
134331492Swollman		(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
134431492Swollman	pp->tof = 1;
13451553Srgrimes}
13461553Srgrimes
13471553Srgrimesstatic char *
134878146Sgadscnline(int key, char *p, int c)
13491553Srgrimes{
135039084Swollman	register int scnwidth;
13511553Srgrimes
13521553Srgrimes	for (scnwidth = WIDTH; --scnwidth;) {
13531553Srgrimes		key <<= 1;
13541553Srgrimes		*p++ = key & 0200 ? c : BACKGND;
13551553Srgrimes	}
13561553Srgrimes	return (p);
13571553Srgrimes}
13581553Srgrimes
13591553Srgrimes#define TRC(q)	(((q)-' ')&0177)
13601553Srgrimes
13611553Srgrimesstatic void
136278146Sgadscan_out(struct printer *pp, int scfd, char *scsp, int dlm)
13631553Srgrimes{
13641553Srgrimes	register char *strp;
136539084Swollman	register int nchrs, j;
13661553Srgrimes	char outbuf[LINELEN+1], *sp, c, cc;
13671553Srgrimes	int d, scnhgt;
13681553Srgrimes
13691553Srgrimes	for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
13701553Srgrimes		strp = &outbuf[0];
13711553Srgrimes		sp = scsp;
13721553Srgrimes		for (nchrs = 0; ; ) {
13731553Srgrimes			d = dropit(c = TRC(cc = *sp++));
13741553Srgrimes			if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
13751553Srgrimes				for (j = WIDTH; --j;)
13761553Srgrimes					*strp++ = BACKGND;
13771553Srgrimes			else
137831492Swollman				strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
137931492Swollman			if (*sp == dlm || *sp == '\0' ||
138031492Swollman			    nchrs++ >= pp->page_width/(WIDTH+1)-1)
13811553Srgrimes				break;
13821553Srgrimes			*strp++ = BACKGND;
13831553Srgrimes			*strp++ = BACKGND;
13841553Srgrimes		}
13851553Srgrimes		while (*--strp == BACKGND && strp >= outbuf)
13861553Srgrimes			;
13871553Srgrimes		strp++;
13888857Srgrimes		*strp++ = '\n';
13891553Srgrimes		(void) write(scfd, outbuf, strp-outbuf);
13901553Srgrimes	}
13911553Srgrimes}
13921553Srgrimes
13931553Srgrimesstatic int
139478146Sgaddropit(int c)
13951553Srgrimes{
13961553Srgrimes	switch(c) {
13971553Srgrimes
13981553Srgrimes	case TRC('_'):
13991553Srgrimes	case TRC(';'):
14001553Srgrimes	case TRC(','):
14011553Srgrimes	case TRC('g'):
14021553Srgrimes	case TRC('j'):
14031553Srgrimes	case TRC('p'):
14041553Srgrimes	case TRC('q'):
14051553Srgrimes	case TRC('y'):
14061553Srgrimes		return (DROP);
14071553Srgrimes
14081553Srgrimes	default:
14091553Srgrimes		return (0);
14101553Srgrimes	}
14111553Srgrimes}
14121553Srgrimes
14131553Srgrimes/*
14141553Srgrimes * sendmail ---
14151553Srgrimes *   tell people about job completion
14161553Srgrimes */
14171553Srgrimesstatic void
141894038Sgadsendmail(struct printer *pp, char *userid, int bombed)
14191553Srgrimes{
14201553Srgrimes	register int i;
14211553Srgrimes	int p[2], s;
142278146Sgad	register const char *cp;
14231553Srgrimes	struct stat stb;
14241553Srgrimes	FILE *fp;
14251553Srgrimes
14261553Srgrimes	pipe(p);
142731492Swollman	if ((s = dofork(pp, DORETURN)) == 0) {		/* child */
14281553Srgrimes		dup2(p[0], 0);
14298094Sjkh		closelog();
143031492Swollman		closeallfds(3);
143127635Simp		if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
14321553Srgrimes			cp++;
143331492Swollman		else
14341553Srgrimes			cp = _PATH_SENDMAIL;
143579452Sbrian		execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
143631492Swollman		_exit(0);
14371553Srgrimes	} else if (s > 0) {				/* parent */
14381553Srgrimes		dup2(p[1], 1);
143994038Sgad		printf("To: %s@%s\n", userid, origin_host);
144031492Swollman		printf("Subject: %s printer job \"%s\"\n", pp->printer,
144115648Sjoerg			*jobname ? jobname : "<unknown>");
144278300Sgad		printf("Reply-To: root@%s\n\n", local_host);
14431553Srgrimes		printf("Your printer job ");
14441553Srgrimes		if (*jobname)
14451553Srgrimes			printf("(%s) ", jobname);
144694040Sgad
14471553Srgrimes		switch (bombed) {
14481553Srgrimes		case OK:
144994040Sgad			cp = "OK";
14501553Srgrimes			printf("\ncompleted successfully\n");
14511553Srgrimes			break;
14521553Srgrimes		default:
14531553Srgrimes		case FATALERR:
145494040Sgad			cp = "FATALERR";
14551553Srgrimes			printf("\ncould not be printed\n");
14561553Srgrimes			break;
14571553Srgrimes		case NOACCT:
145894040Sgad			cp = "NOACCT";
145978300Sgad			printf("\ncould not be printed without an account on %s\n",
146078300Sgad			    local_host);
14611553Srgrimes			break;
14621553Srgrimes		case FILTERERR:
146394040Sgad			cp = "FILTERERR";
146468664Sgad			if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
146568664Sgad			    || (fp = fopen(tempstderr, "r")) == NULL) {
146615648Sjoerg				printf("\nhad some errors and may not have printed\n");
14671553Srgrimes				break;
14681553Srgrimes			}
146915648Sjoerg			printf("\nhad the following errors and may not have printed:\n");
14701553Srgrimes			while ((i = getc(fp)) != EOF)
14711553Srgrimes				putchar(i);
14721553Srgrimes			(void) fclose(fp);
14731553Srgrimes			break;
14741553Srgrimes		case ACCESS:
147594040Sgad			cp = "ACCESS";
14761553Srgrimes			printf("\nwas not printed because it was not linked to the original file\n");
14771553Srgrimes		}
14781553Srgrimes		fflush(stdout);
14791553Srgrimes		(void) close(1);
148031492Swollman	} else {
148194038Sgad		syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
148231492Swollman		return;
14831553Srgrimes	}
14841553Srgrimes	(void) close(p[0]);
14851553Srgrimes	(void) close(p[1]);
148615648Sjoerg	wait(NULL);
148715648Sjoerg	syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
148894038Sgad	    userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
14891553Srgrimes}
14901553Srgrimes
14911553Srgrimes/*
14921553Srgrimes * dofork - fork with retries on failure
14931553Srgrimes */
14941553Srgrimesstatic int
149578146Sgaddofork(const struct printer *pp, int action)
14961553Srgrimes{
149780230Sgad	int i, fail, forkpid;
149860871Smpp	struct passwd *pwd;
14991553Srgrimes
150080230Sgad	forkpid = -1;
150180230Sgad	if (daemon_uname == NULL) {
150280230Sgad		pwd = getpwuid(pp->daemon_user);
150380230Sgad		if (pwd == NULL) {
150480230Sgad			syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
150580230Sgad			    pp->printer, pp->daemon_user);
150680230Sgad			goto error_ret;
150780230Sgad		}
150880230Sgad		daemon_uname = strdup(pwd->pw_name);
150980230Sgad		daemon_defgid = pwd->pw_gid;
151080230Sgad	}
151180230Sgad
15121553Srgrimes	for (i = 0; i < 20; i++) {
151380230Sgad		forkpid = fork();
151480230Sgad		if (forkpid < 0) {
15151553Srgrimes			sleep((unsigned)(i*i));
15161553Srgrimes			continue;
15171553Srgrimes		}
15181553Srgrimes		/*
15191553Srgrimes		 * Child should run as daemon instead of root
15201553Srgrimes		 */
152178146Sgad		if (forkpid == 0) {
152280230Sgad			errno = 0;
152380230Sgad			fail = initgroups(daemon_uname, daemon_defgid);
152480230Sgad			if (fail) {
152580230Sgad				syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
152680230Sgad				    pp->printer, daemon_uname, daemon_defgid);
152760871Smpp				break;
152860871Smpp			}
152980230Sgad			fail = setgid(daemon_defgid);
153080230Sgad			if (fail) {
153180230Sgad				syslog(LOG_ERR, "%s: setgid(%u): %m",
153280230Sgad				    pp->printer, daemon_defgid);
153380230Sgad				break;
153480230Sgad			}
153580230Sgad			fail = setuid(pp->daemon_user);
153680230Sgad			if (fail) {
153780230Sgad				syslog(LOG_ERR, "%s: setuid(%ld): %m",
153880230Sgad				    pp->printer, pp->daemon_user);
153980230Sgad				break;
154080230Sgad			}
154160871Smpp		}
154280230Sgad		return forkpid;
15431553Srgrimes	}
15441553Srgrimes
154580230Sgad	/*
154680230Sgad	 * An error occurred.  If the error is in the child process, then
154780230Sgad	 * this routine MUST always exit().  DORETURN only effects how
154880230Sgad	 * errors should be handled in the parent process.
154980230Sgad	 */
155080230Sgaderror_ret:
155180230Sgad	if (forkpid == 0) {
155280230Sgad		syslog(LOG_ERR, "%s: dofork(): aborting child process...",
155380230Sgad		    pp->printer);
155480230Sgad		exit(1);
155580230Sgad	}
155680230Sgad	syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);
155780230Sgad
155880230Sgad	sleep(1);		/* throttle errors, as a safety measure */
15591553Srgrimes	switch (action) {
15601553Srgrimes	case DORETURN:
156180230Sgad		return -1;
15621553Srgrimes	default:
15631553Srgrimes		syslog(LOG_ERR, "bad action (%d) to dofork", action);
156480230Sgad		/* FALLTHROUGH */
15651553Srgrimes	case DOABORT:
15661553Srgrimes		exit(1);
15671553Srgrimes	}
15681553Srgrimes	/*NOTREACHED*/
15691553Srgrimes}
15701553Srgrimes
15711553Srgrimes/*
15721553Srgrimes * Kill child processes to abort current job.
15731553Srgrimes */
15741553Srgrimesstatic void
157578146Sgadabortpr(int signo __unused)
15761553Srgrimes{
157768664Sgad
157868664Sgad	(void) unlink(tempstderr);
15791553Srgrimes	kill(0, SIGINT);
15801553Srgrimes	if (ofilter > 0)
15811553Srgrimes		kill(ofilter, SIGCONT);
15821553Srgrimes	while (wait(NULL) > 0)
15831553Srgrimes		;
158424831Sbrian	if (ofilter > 0 && tfd != -1)
158524831Sbrian		unlink(tfile);
15861553Srgrimes	exit(0);
15871553Srgrimes}
15881553Srgrimes
15891553Srgrimesstatic void
159078146Sgadinit(struct printer *pp)
15911553Srgrimes{
15921553Srgrimes	char *s;
15931553Srgrimes
159431492Swollman	sprintf(&width[2], "%ld", pp->page_width);
159531492Swollman	sprintf(&length[2], "%ld", pp->page_length);
159631492Swollman	sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
159731492Swollman	sprintf(&pxlength[2], "%ld", pp->page_plength);
159831492Swollman	if ((s = checkremote(pp)) != 0) {
159931492Swollman		syslog(LOG_WARNING, "%s", s);
160031492Swollman		free(s);
160131492Swollman	}
160231492Swollman}
160331492Swollman
160431492Swollmanvoid
160578146Sgadstartprinting(const char *printer)
160631492Swollman{
160731492Swollman	struct printer myprinter, *pp = &myprinter;
160831492Swollman	int status;
160931492Swollman
161031492Swollman	init_printer(pp);
161131492Swollman	status = getprintcap(printer, pp);
161231492Swollman	switch(status) {
161331492Swollman	case PCAPERR_OSERR:
161431492Swollman		syslog(LOG_ERR, "can't open printer description file: %m");
16151553Srgrimes		exit(1);
161631492Swollman	case PCAPERR_NOTFOUND:
16171553Srgrimes		syslog(LOG_ERR, "unknown printer: %s", printer);
16181553Srgrimes		exit(1);
161931492Swollman	case PCAPERR_TCLOOP:
162031492Swollman		fatal(pp, "potential reference loop detected in printcap file");
162131492Swollman	default:
162231492Swollman		break;
162331492Swollman	}
162431492Swollman	printjob(pp);
16251553Srgrimes}
16261553Srgrimes
16271553Srgrimes/*
16281553Srgrimes * Acquire line printer or remote connection.
16291553Srgrimes */
16301553Srgrimesstatic void
163178146Sgadopenpr(const struct printer *pp)
16321553Srgrimes{
163331492Swollman	int p[2];
163415648Sjoerg	char *cp;
16351553Srgrimes
163631492Swollman	if (pp->remote) {
163731492Swollman		openrem(pp);
163894032Sgad		/*
163994032Sgad		 * Lpd does support the setting of 'of=' filters for
164094032Sgad		 * jobs going to remote machines, but that does not
164194032Sgad		 * have the same meaning as 'of=' does when handling
164294032Sgad		 * local print queues.  For remote machines, all 'of='
164394032Sgad		 * filter processing is handled in sendfile(), and that
164494032Sgad		 * does not use these global "output filter" variables.
164594032Sgad		 */
164694032Sgad		ofd = -1;
164794032Sgad		ofilter = 0;
164894032Sgad		return;
164931492Swollman	} else if (*pp->lp) {
165031492Swollman		if ((cp = strchr(pp->lp, '@')) != NULL)
165131492Swollman			opennet(pp);
165215648Sjoerg		else
165331492Swollman			opentty(pp);
16541553Srgrimes	} else {
16551553Srgrimes		syslog(LOG_ERR, "%s: no line printer device or host name",
165631492Swollman			pp->printer);
16571553Srgrimes		exit(1);
16581553Srgrimes	}
165915648Sjoerg
16601553Srgrimes	/*
16611553Srgrimes	 * Start up an output filter, if needed.
16621553Srgrimes	 */
166331492Swollman	if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) {
16641553Srgrimes		pipe(p);
166531492Swollman		if (pp->remote) {
166631492Swollman			strcpy(tfile, TFILENAME);
166724831Sbrian			tfd = mkstemp(tfile);
166824831Sbrian		}
166931492Swollman		if ((ofilter = dofork(pp, DOABORT)) == 0) {	/* child */
16701553Srgrimes			dup2(p[0], 0);		/* pipe is std in */
167124831Sbrian			/* tfile/printer is stdout */
167231492Swollman			dup2(pp->remote ? tfd : pfd, 1);
16738094Sjkh			closelog();
167431492Swollman			closeallfds(3);
167531492Swollman			if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
167631492Swollman				cp = pp->filters[LPF_OUTPUT];
16771553Srgrimes			else
16781553Srgrimes				cp++;
167979452Sbrian			execl(pp->filters[LPF_OUTPUT], cp, width, length,
168079452Sbrian			      (char *)0);
168131492Swollman			syslog(LOG_ERR, "%s: %s: %m", pp->printer,
168231492Swollman			       pp->filters[LPF_OUTPUT]);
16831553Srgrimes			exit(1);
16841553Srgrimes		}
16851553Srgrimes		(void) close(p[0]);		/* close input side */
16861553Srgrimes		ofd = p[1];			/* use pipe for output */
16871553Srgrimes	} else {
16881553Srgrimes		ofd = pfd;
16891553Srgrimes		ofilter = 0;
16901553Srgrimes	}
16911553Srgrimes}
16921553Srgrimes
169315648Sjoerg/*
169415648Sjoerg * Printer connected directly to the network
169515648Sjoerg * or to a terminal server on the net
169615648Sjoerg */
169715648Sjoergstatic void
169878146Sgadopennet(const struct printer *pp)
169915648Sjoerg{
170015648Sjoerg	register int i;
170131492Swollman	int resp;
170231492Swollman	u_long port;
170331492Swollman	char *ep;
170430407Sjoerg	void (*savealrm)(int);
170515648Sjoerg
170631492Swollman	port = strtoul(pp->lp, &ep, 0);
170738470Sbrian	if (*ep != '@' || port > 65535) {
170831492Swollman		syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
170931492Swollman		       pp->lp);
171015648Sjoerg		exit(1);
171115648Sjoerg	}
171231492Swollman	ep++;
171315648Sjoerg
171415648Sjoerg	for (i = 1; ; i = i < 256 ? i << 1 : i) {
171515648Sjoerg		resp = -1;
171630407Sjoerg		savealrm = signal(SIGALRM, alarmhandler);
171731492Swollman		alarm(pp->conn_timeout);
171831492Swollman		pfd = getport(pp, ep, port);
171931020Sjoerg		alarm(0);
172030407Sjoerg		(void)signal(SIGALRM, savealrm);
172115648Sjoerg		if (pfd < 0 && errno == ECONNREFUSED)
172215648Sjoerg			resp = 1;
172315648Sjoerg		else if (pfd >= 0) {
172415648Sjoerg			/*
172515648Sjoerg			 * need to delay a bit for rs232 lines
172615648Sjoerg			 * to stabilize in case printer is
172715648Sjoerg			 * connected via a terminal server
172815648Sjoerg			 */
172915648Sjoerg			delay(500);
173015648Sjoerg			break;
173115648Sjoerg		}
173215648Sjoerg		if (i == 1) {
173331492Swollman			if (resp < 0)
173431492Swollman				pstatus(pp, "waiting for %s to come up",
173531492Swollman					pp->lp);
173631492Swollman			else
173731492Swollman				pstatus(pp,
173831492Swollman					"waiting for access to printer on %s",
173931492Swollman					pp->lp);
174015648Sjoerg		}
174115648Sjoerg		sleep(i);
174215648Sjoerg	}
174379739Sgad	pstatus(pp, "sending to %s port %lu", ep, port);
174415648Sjoerg}
174515648Sjoerg
174615648Sjoerg/*
174715648Sjoerg * Printer is connected to an RS232 port on this host
174815648Sjoerg */
174915648Sjoergstatic void
175078146Sgadopentty(const struct printer *pp)
175115648Sjoerg{
175215648Sjoerg	register int i;
175315648Sjoerg
175415648Sjoerg	for (i = 1; ; i = i < 32 ? i << 1 : i) {
175531492Swollman		pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
175615648Sjoerg		if (pfd >= 0) {
175715648Sjoerg			delay(500);
175815648Sjoerg			break;
175915648Sjoerg		}
176015648Sjoerg		if (errno == ENOENT) {
176131492Swollman			syslog(LOG_ERR, "%s: %m", pp->lp);
176215648Sjoerg			exit(1);
176315648Sjoerg		}
176415648Sjoerg		if (i == 1)
176531492Swollman			pstatus(pp,
176631492Swollman				"waiting for %s to become ready (offline?)",
176731492Swollman				pp->printer);
176815648Sjoerg		sleep(i);
176915648Sjoerg	}
177015648Sjoerg	if (isatty(pfd))
177131492Swollman		setty(pp);
177231492Swollman	pstatus(pp, "%s is ready and printing", pp->printer);
177315648Sjoerg}
177415648Sjoerg
177515648Sjoerg/*
177615648Sjoerg * Printer is on a remote host
177715648Sjoerg */
177815648Sjoergstatic void
177978146Sgadopenrem(const struct printer *pp)
178015648Sjoerg{
178131492Swollman	register int i;
178227748Simp	int resp;
178330407Sjoerg	void (*savealrm)(int);
178415648Sjoerg
178515648Sjoerg	for (i = 1; ; i = i < 256 ? i << 1 : i) {
178615648Sjoerg		resp = -1;
178730407Sjoerg		savealrm = signal(SIGALRM, alarmhandler);
178831492Swollman		alarm(pp->conn_timeout);
178931492Swollman		pfd = getport(pp, pp->remote_host, 0);
179031020Sjoerg		alarm(0);
179130407Sjoerg		(void)signal(SIGALRM, savealrm);
179215648Sjoerg		if (pfd >= 0) {
179331492Swollman			if ((writel(pfd, "\2", pp->remote_queue, "\n",
179431492Swollman				    (char *)0)
179531492Swollman			     == 2 + strlen(pp->remote_queue))
179631492Swollman			    && (resp = response(pp)) == 0)
179715648Sjoerg				break;
179815648Sjoerg			(void) close(pfd);
179915648Sjoerg		}
180015648Sjoerg		if (i == 1) {
180115648Sjoerg			if (resp < 0)
180231492Swollman				pstatus(pp, "waiting for %s to come up",
180331492Swollman					pp->remote_host);
180415648Sjoerg			else {
180531492Swollman				pstatus(pp,
180631492Swollman					"waiting for queue to be enabled on %s",
180731492Swollman					pp->remote_host);
180815648Sjoerg				i = 256;
180915648Sjoerg			}
181015648Sjoerg		}
181115648Sjoerg		sleep(i);
181215648Sjoerg	}
181331492Swollman	pstatus(pp, "sending to %s", pp->remote_host);
181415648Sjoerg}
181515648Sjoerg
18161553Srgrimes/*
18171553Srgrimes * setup tty lines.
18181553Srgrimes */
18191553Srgrimesstatic void
182078146Sgadsetty(const struct printer *pp)
18211553Srgrimes{
182215032Ssef	struct termios ttybuf;
18231553Srgrimes
18241553Srgrimes	if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
182531492Swollman		syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
18261553Srgrimes		exit(1);
18271553Srgrimes	}
182815032Ssef	if (tcgetattr(pfd, &ttybuf) < 0) {
182931492Swollman		syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
18301553Srgrimes		exit(1);
18311553Srgrimes	}
183231492Swollman	if (pp->baud_rate > 0)
183331492Swollman		cfsetspeed(&ttybuf, pp->baud_rate);
183431492Swollman	if (pp->mode_set) {
183531492Swollman		char *s = strdup(pp->mode_set), *tmp;
183615032Ssef
183731492Swollman		while ((tmp = strsep(&s, ",")) != NULL) {
183839084Swollman			(void) msearch(tmp, &ttybuf);
18391553Srgrimes		}
18401553Srgrimes	}
184131492Swollman	if (pp->mode_set != 0 || pp->baud_rate > 0) {
184215032Ssef		if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
184331492Swollman			syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
18441553Srgrimes		}
18451553Srgrimes	}
18461553Srgrimes}
18471553Srgrimes
18481553Srgrimes#include <stdarg.h>
18491553Srgrimes
185015648Sjoergstatic void
185131492Swollmanpstatus(const struct printer *pp, const char *msg, ...)
18521553Srgrimes{
185331492Swollman	int fd;
185431492Swollman	char *buf;
18551553Srgrimes	va_list ap;
18561553Srgrimes	va_start(ap, msg);
18571553Srgrimes
18581553Srgrimes	umask(0);
185931492Swollman	fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
186031492Swollman	if (fd < 0) {
186131492Swollman		syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file);
18621553Srgrimes		exit(1);
18631553Srgrimes	}
18641553Srgrimes	ftruncate(fd, 0);
186531492Swollman	vasprintf(&buf, msg, ap);
18661553Srgrimes	va_end(ap);
186731492Swollman	writel(fd, buf, "\n", (char *)0);
186831492Swollman	close(fd);
186931492Swollman	free(buf);
18701553Srgrimes}
187130407Sjoerg
187230407Sjoergvoid
187378146Sgadalarmhandler(int signo __unused)
187430407Sjoerg{
187578146Sgad	/* the signal is ignored */
187678146Sgad	/* (the '__unused' is just to avoid a compile-time warning) */
187730407Sjoerg}
1878