printjob.c revision 94040
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 94040 2002-04-07 08:12:39Z 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, 14978146Sgad char _format); 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 locale[sizeof(locale) - 1] = '\0'; 51853956Sache continue; 51953956Sache 5201553Srgrimes default: /* some file to print */ 52168467Sgad /* only lowercase cmd-codes include a file-to-print */ 52268467Sgad if ((line[0] < 'a') || (line[0] > 'z')) { 52368467Sgad /* ignore any other lines */ 52468467Sgad if (lflag <= 1) 52568467Sgad continue; 52668467Sgad if (!didignorehdr) { 52768467Sgad syslog(LOG_INFO, "%s: in %s :", 52868467Sgad pp->printer, file); 52968467Sgad didignorehdr = 1; 53068467Sgad } 53168467Sgad syslog(LOG_INFO, "%s: ignoring line: '%c' %s", 53268467Sgad pp->printer, line[0], &line[1]); 53368467Sgad continue; 53468467Sgad } 53568467Sgad i = print(pp, line[0], line+1); 53668467Sgad switch (i) { 5371553Srgrimes case ERROR: 5381553Srgrimes if (bombed == OK) 5391553Srgrimes bombed = FATALERR; 5401553Srgrimes break; 5411553Srgrimes case REPRINT: 5421553Srgrimes (void) fclose(cfp); 5431553Srgrimes return(REPRINT); 5441553Srgrimes case FILTERERR: 5451553Srgrimes case ACCESS: 5461553Srgrimes bombed = i; 54731492Swollman sendmail(pp, logname, bombed); 5481553Srgrimes } 5491553Srgrimes title[0] = '\0'; 5501553Srgrimes continue; 5511553Srgrimes 5521553Srgrimes case 'N': 5531553Srgrimes case 'U': 5541553Srgrimes case 'M': 55515648Sjoerg case 'R': 5561553Srgrimes continue; 5571553Srgrimes } 5581553Srgrimes 5591553Srgrimes /* pass 2 */ 5601553Srgrimes 5611553Srgrimespass2: 5621553Srgrimes fseek(cfp, 0L, 0); 5631553Srgrimes while (getline(cfp)) 5641553Srgrimes switch (line[0]) { 5651553Srgrimes case 'L': /* identification line */ 56631492Swollman if (!pp->no_header && pp->header_last) 56731492Swollman banner(pp, line+1, jobname); 5681553Srgrimes continue; 5691553Srgrimes 5701553Srgrimes case 'M': 5711553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 57231492Swollman sendmail(pp, line+1, bombed); 5731553Srgrimes continue; 5741553Srgrimes 5751553Srgrimes case 'U': 57627748Simp if (strchr(line+1, '/')) 57727748Simp continue; 5781553Srgrimes (void) unlink(line+1); 5791553Srgrimes } 5801553Srgrimes /* 5811553Srgrimes * clean-up in case another control file exists 5821553Srgrimes */ 5831553Srgrimes (void) fclose(cfp); 5841553Srgrimes (void) unlink(file); 5851553Srgrimes return(bombed == OK ? OK : ERROR); 5861553Srgrimes} 5871553Srgrimes 5881553Srgrimes/* 5891553Srgrimes * Print a file. 5901553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 5911553Srgrimes * Return -1 if a non-recoverable error occured, 5921553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 5931553Srgrimes * 1 if we should try to reprint this job and 5941553Srgrimes * 0 if all is well. 5951553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 5961553Srgrimes * stderr as the log file, and must not ignore SIGINT. 5971553Srgrimes */ 5981553Srgrimesstatic int 59978146Sgadprint(struct printer *pp, int format, char *file) 6001553Srgrimes{ 60153956Sache register int n, i; 6021553Srgrimes register char *prog; 60331492Swollman int fi, fo; 6041553Srgrimes FILE *fp; 6051553Srgrimes char *av[15], buf[BUFSIZ]; 60668734Sgad int pid, p[2], stopped; 6071553Srgrimes union wait status; 6081553Srgrimes struct stat stb; 6091553Srgrimes 61068467Sgad if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { 61168467Sgad syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", 61268467Sgad pp->printer, file, format); 6131553Srgrimes return(ERROR); 61468467Sgad } 6151553Srgrimes /* 6161553Srgrimes * Check to see if data file is a symbolic link. If so, it should 6171553Srgrimes * still point to the same file or someone is trying to print 6181553Srgrimes * something he shouldn't. 6191553Srgrimes */ 6201553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 6211553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 6221553Srgrimes return(ACCESS); 62368253Sgad 62468253Sgad job_dfcnt++; /* increment datafile counter for this job */ 62568734Sgad stopped = 0; /* output filter is not stopped */ 62668253Sgad 62768253Sgad /* everything seems OK, start it up */ 62831492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 62931492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 63031492Swollman pp->tof = 1; 6311553Srgrimes } 63231492Swollman if (pp->filters[LPF_INPUT] == NULL 63383684Sgad && (format == 'f' || format == 'l' || format == 'o')) { 63431492Swollman pp->tof = 0; 6351553Srgrimes while ((n = read(fi, buf, BUFSIZ)) > 0) 6361553Srgrimes if (write(ofd, buf, n) != n) { 6371553Srgrimes (void) close(fi); 6381553Srgrimes return(REPRINT); 6391553Srgrimes } 6401553Srgrimes (void) close(fi); 6411553Srgrimes return(OK); 6421553Srgrimes } 6431553Srgrimes switch (format) { 6441553Srgrimes case 'p': /* print file using 'pr' */ 64531492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 6461553Srgrimes prog = _PATH_PR; 64753956Sache i = 0; 64853956Sache av[i++] = "pr"; 64953956Sache av[i++] = width; 65053956Sache av[i++] = length; 65153956Sache av[i++] = "-h"; 65253956Sache av[i++] = *title ? title : " "; 65353956Sache av[i++] = "-L"; 65453956Sache av[i++] = *locale ? locale : "C"; 65553956Sache av[i++] = "-F"; 65653956Sache av[i] = 0; 6571553Srgrimes fo = ofd; 6581553Srgrimes goto start; 6591553Srgrimes } 6601553Srgrimes pipe(p); 66131492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 6621553Srgrimes dup2(fi, 0); /* file is stdin */ 6631553Srgrimes dup2(p[1], 1); /* pipe is stdout */ 6648094Sjkh closelog(); 66531492Swollman closeallfds(3); 6661553Srgrimes execl(_PATH_PR, "pr", width, length, 66753956Sache "-h", *title ? title : " ", 66853956Sache "-L", *locale ? locale : "C", 66979452Sbrian "-F", (char *)0); 6701553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6711553Srgrimes exit(2); 6721553Srgrimes } 6731553Srgrimes (void) close(p[1]); /* close output side */ 6741553Srgrimes (void) close(fi); 6751553Srgrimes if (prchild < 0) { 6761553Srgrimes prchild = 0; 6771553Srgrimes (void) close(p[0]); 6781553Srgrimes return(ERROR); 6791553Srgrimes } 6801553Srgrimes fi = p[0]; /* use pipe for input */ 6811553Srgrimes case 'f': /* print plain text file */ 68231492Swollman prog = pp->filters[LPF_INPUT]; 6831553Srgrimes av[1] = width; 6841553Srgrimes av[2] = length; 6851553Srgrimes av[3] = indent; 6861553Srgrimes n = 4; 6871553Srgrimes break; 68886935Sgad case 'o': /* print postscript file */ 68986935Sgad /* 69086935Sgad * Treat this as a "plain file with control characters", and 69186935Sgad * assume the standard LPF_INPUT filter will recognize that 69286935Sgad * the data is postscript and know what to do with it. These 69386935Sgad * 'o'-file requests could come from MacOS 10.1 systems. 69486935Sgad * (later versions of MacOS 10 will explicitly use 'l') 69586935Sgad * A postscript file can contain binary data, which is why 'l' 69686935Sgad * is somewhat more appropriate than 'f'. 69786935Sgad */ 69886935Sgad /* FALLTHROUGH */ 6991553Srgrimes case 'l': /* like 'f' but pass control characters */ 70031492Swollman prog = pp->filters[LPF_INPUT]; 7011553Srgrimes av[1] = "-c"; 7021553Srgrimes av[2] = width; 7031553Srgrimes av[3] = length; 7041553Srgrimes av[4] = indent; 7051553Srgrimes n = 5; 7061553Srgrimes break; 7071553Srgrimes case 'r': /* print a fortran text file */ 70831492Swollman prog = pp->filters[LPF_FORTRAN]; 7091553Srgrimes av[1] = width; 7101553Srgrimes av[2] = length; 7111553Srgrimes n = 3; 7121553Srgrimes break; 7131553Srgrimes case 't': /* print troff output */ 7141553Srgrimes case 'n': /* print ditroff output */ 7151553Srgrimes case 'd': /* print tex output */ 7161553Srgrimes (void) unlink(".railmag"); 7171553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 71831492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 71931492Swollman pp->printer); 7201553Srgrimes (void) unlink(".railmag"); 7211553Srgrimes } else { 7221553Srgrimes for (n = 0; n < 4; n++) { 7231553Srgrimes if (fonts[n][0] != '/') 7241553Srgrimes (void) write(fo, _PATH_VFONT, 7251553Srgrimes sizeof(_PATH_VFONT) - 1); 7261553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 7271553Srgrimes (void) write(fo, "\n", 1); 7281553Srgrimes } 7291553Srgrimes (void) close(fo); 7301553Srgrimes } 73131492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 73231492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 73331492Swollman : pp->filters[LPF_DVI]); 7341553Srgrimes av[1] = pxwidth; 7351553Srgrimes av[2] = pxlength; 7361553Srgrimes n = 3; 7371553Srgrimes break; 7381553Srgrimes case 'c': /* print cifplot output */ 73931492Swollman prog = pp->filters[LPF_CIFPLOT]; 7401553Srgrimes av[1] = pxwidth; 7411553Srgrimes av[2] = pxlength; 7421553Srgrimes n = 3; 7431553Srgrimes break; 7441553Srgrimes case 'g': /* print plot(1G) output */ 74531492Swollman prog = pp->filters[LPF_GRAPH]; 7461553Srgrimes av[1] = pxwidth; 7471553Srgrimes av[2] = pxlength; 7481553Srgrimes n = 3; 7491553Srgrimes break; 7501553Srgrimes case 'v': /* print raster output */ 75131492Swollman prog = pp->filters[LPF_RASTER]; 7521553Srgrimes av[1] = pxwidth; 7531553Srgrimes av[2] = pxlength; 7541553Srgrimes n = 3; 7551553Srgrimes break; 7561553Srgrimes default: 7571553Srgrimes (void) close(fi); 7581553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 75931492Swollman pp->printer, format); 7601553Srgrimes return(ERROR); 7611553Srgrimes } 76215648Sjoerg if (prog == NULL) { 76315648Sjoerg (void) close(fi); 76415648Sjoerg syslog(LOG_ERR, 76515648Sjoerg "%s: no filter found in printcap for format character '%c'", 76631492Swollman pp->printer, format); 76715648Sjoerg return(ERROR); 76815648Sjoerg } 76927635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 7701553Srgrimes av[0]++; 7711553Srgrimes else 7721553Srgrimes av[0] = prog; 7731553Srgrimes av[n++] = "-n"; 7741553Srgrimes av[n++] = logname; 7751553Srgrimes av[n++] = "-h"; 77678300Sgad av[n++] = origin_host; 77731492Swollman av[n++] = pp->acct_file; 7781553Srgrimes av[n] = 0; 7791553Srgrimes fo = pfd; 7801553Srgrimes if (ofilter > 0) { /* stop output filter */ 7811553Srgrimes write(ofd, "\031\1", 2); 7821553Srgrimes while ((pid = 7831553Srgrimes wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter) 7841553Srgrimes ; 78579735Sgad if (pid < 0) 78679735Sgad syslog(LOG_WARNING, "%s: after stopping 'of', wait3() returned: %m", 78779735Sgad pp->printer); 78879735Sgad else if (status.w_stopval != WSTOPPED) { 7891553Srgrimes (void) close(fi); 79015648Sjoerg syslog(LOG_WARNING, 79131492Swollman "%s: output filter died " 79279735Sgad "(pid=%d retcode=%d termsig=%d)", 79379735Sgad pp->printer, ofilter, status.w_retcode, 79431492Swollman status.w_termsig); 7951553Srgrimes return(REPRINT); 7961553Srgrimes } 7971553Srgrimes stopped++; 7981553Srgrimes } 7991553Srgrimesstart: 80031492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 8011553Srgrimes dup2(fi, 0); 8021553Srgrimes dup2(fo, 1); 80368664Sgad /* setup stderr for the filter (child process) 80468664Sgad * so it goes to our temporary errors file */ 80568664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 8061553Srgrimes if (n >= 0) 8071553Srgrimes dup2(n, 2); 8088094Sjkh closelog(); 80931492Swollman closeallfds(3); 8101553Srgrimes execv(prog, av); 8111553Srgrimes syslog(LOG_ERR, "cannot execv %s", prog); 8121553Srgrimes exit(2); 8131553Srgrimes } 8141553Srgrimes (void) close(fi); 8151553Srgrimes if (child < 0) 8161553Srgrimes status.w_retcode = 100; 81779735Sgad else { 8181553Srgrimes while ((pid = wait((int *)&status)) > 0 && pid != child) 8191553Srgrimes ; 82079735Sgad if (pid < 0) { 82179735Sgad status.w_retcode = 100; 82279735Sgad syslog(LOG_WARNING, "%s: after execv(%s), wait() returned: %m", 82379735Sgad pp->printer, prog); 82479735Sgad } 82579735Sgad } 8261553Srgrimes child = 0; 8271553Srgrimes prchild = 0; 8281553Srgrimes if (stopped) { /* restart output filter */ 8291553Srgrimes if (kill(ofilter, SIGCONT) < 0) { 8301553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 8311553Srgrimes exit(1); 8321553Srgrimes } 8331553Srgrimes } 83431492Swollman pp->tof = 0; 8351553Srgrimes 83668664Sgad /* Copy the filter's output to "lf" logfile */ 83768664Sgad if ((fp = fopen(tempstderr, "r"))) { 8381553Srgrimes while (fgets(buf, sizeof(buf), fp)) 8391553Srgrimes fputs(buf, stderr); 8401553Srgrimes fclose(fp); 8411553Srgrimes } 8421553Srgrimes 8431553Srgrimes if (!WIFEXITED(status)) { 84415648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 84531492Swollman pp->printer, format, status.w_termsig); 8461553Srgrimes return(ERROR); 8471553Srgrimes } 8481553Srgrimes switch (status.w_retcode) { 8491553Srgrimes case 0: 85031492Swollman pp->tof = 1; 8511553Srgrimes return(OK); 8521553Srgrimes case 1: 8531553Srgrimes return(REPRINT); 85415648Sjoerg case 2: 85515648Sjoerg return(ERROR); 8561553Srgrimes default: 85715648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 85831492Swollman pp->printer, format, status.w_retcode); 85915648Sjoerg return(FILTERERR); 8601553Srgrimes } 8611553Srgrimes} 8621553Srgrimes 8631553Srgrimes/* 8641553Srgrimes * Send the daemon control file (cf) and any data files. 8651553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 8661553Srgrimes * 0 if all is well. 8671553Srgrimes */ 8681553Srgrimesstatic int 86978146Sgadsendit(struct printer *pp, char *file) 8701553Srgrimes{ 8711553Srgrimes register int i, err = OK; 8721553Srgrimes char *cp, last[BUFSIZ]; 8731553Srgrimes 8741553Srgrimes /* 8751553Srgrimes * open control file 8761553Srgrimes */ 8771553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 8781553Srgrimes return(OK); 87968253Sgad 88068253Sgad /* initialize job-specific count of datafiles processed */ 88168253Sgad job_dfcnt = 0; 88268253Sgad 8831553Srgrimes /* 8841553Srgrimes * read the control file for work to do 8851553Srgrimes * 8861553Srgrimes * file format -- first character in the line is a command 8871553Srgrimes * rest of the line is the argument. 8881553Srgrimes * commands of interest are: 8891553Srgrimes * 8901553Srgrimes * a-z -- "file name" name of file to print 8911553Srgrimes * U -- "unlink" name of file to remove 8921553Srgrimes * (after we print it. (Pass 2 only)). 8931553Srgrimes */ 8941553Srgrimes 8951553Srgrimes /* 8961553Srgrimes * pass 1 8971553Srgrimes */ 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') { 9281553Srgrimes strcpy(last, line); 92931492Swollman while ((i = getline(cfp)) != 0) 9301553Srgrimes if (strcmp(last, line)) 9311553Srgrimes break; 93231492Swollman switch (sendfile(pp, '\3', last+1, *last)) { 9331553Srgrimes case OK: 9341553Srgrimes if (i) 9351553Srgrimes goto again; 9361553Srgrimes break; 9371553Srgrimes case REPRINT: 9381553Srgrimes (void) fclose(cfp); 9391553Srgrimes return(REPRINT); 9401553Srgrimes case ACCESS: 94131492Swollman sendmail(pp, logname, ACCESS); 9421553Srgrimes case ERROR: 9431553Srgrimes err = ERROR; 9441553Srgrimes } 9451553Srgrimes break; 9461553Srgrimes } 9471553Srgrimes } 94831492Swollman if (err == OK && sendfile(pp, '\2', file, '\0') > 0) { 9491553Srgrimes (void) fclose(cfp); 9501553Srgrimes return(REPRINT); 9511553Srgrimes } 9521553Srgrimes /* 9531553Srgrimes * pass 2 9541553Srgrimes */ 9551553Srgrimes fseek(cfp, 0L, 0); 9561553Srgrimes while (getline(cfp)) 95727748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 9581553Srgrimes (void) unlink(line+1); 9591553Srgrimes /* 9601553Srgrimes * clean-up in case another control file exists 9611553Srgrimes */ 9621553Srgrimes (void) fclose(cfp); 9631553Srgrimes (void) unlink(file); 9641553Srgrimes return(err); 9651553Srgrimes} 9661553Srgrimes 9671553Srgrimes/* 9681553Srgrimes * Send a data file to the remote machine and spool it. 9691553Srgrimes * Return positive if we should try resending. 9701553Srgrimes */ 9711553Srgrimesstatic int 97278146Sgadsendfile(struct printer *pp, int type, char *file, char format) 9731553Srgrimes{ 97494036Sgad int i, amt; 9751553Srgrimes struct stat stb; 97694032Sgad char *av[15], *filtcmd; 97794032Sgad char buf[BUFSIZ], opt_c[4], opt_h[4], opt_n[4]; 97894036Sgad int filtstat, narg, resp, sfd, sfres, sizerr, statrc; 9791553Srgrimes 98074124Sgad statrc = lstat(file, &stb); 98174124Sgad if (statrc < 0) { 98274124Sgad syslog(LOG_ERR, "%s: error from lstat(%s): %m", 98374124Sgad pp->printer, file); 98494036Sgad return (ERROR); 98574124Sgad } 98694036Sgad sfd = open(file, O_RDONLY); 98794036Sgad if (sfd < 0) { 98874124Sgad syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m", 98974124Sgad pp->printer, file); 99094036Sgad return (ERROR); 99174124Sgad } 9921553Srgrimes /* 9931553Srgrimes * Check to see if data file is a symbolic link. If so, it should 9941553Srgrimes * still point to the same file or someone is trying to print something 9951553Srgrimes * he shouldn't. 9961553Srgrimes */ 99794036Sgad if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 && 99894036Sgad (stb.st_dev != fdev || stb.st_ino != fino)) { 99994036Sgad close(sfd); 100094036Sgad return (ACCESS); 100194036Sgad } 100224831Sbrian 100394032Sgad /* Everything seems OK for reading the file, now to send it */ 100494032Sgad filtcmd = NULL; 100524831Sbrian sizerr = 0; 100694032Sgad tfd = -1; 100724831Sbrian if (type == '\3') { 100894032Sgad /* 100994032Sgad * Type == 3 means this is a datafile, not a control file. 101094032Sgad * Increment the counter of data-files in this job, and 101194032Sgad * then check for input or output filters (which are only 101294032Sgad * applied to datafiles, not control files). 101394032Sgad */ 101494032Sgad job_dfcnt++; 101594032Sgad 101694032Sgad /* 101794032Sgad * Note that here we are filtering datafiles, one at a time, 101894032Sgad * as they are sent to the remote machine. Here, the *only* 101994032Sgad * difference between an input filter (`if=') and an output 102094032Sgad * filter (`of=') is the argument list that the filter is 102194032Sgad * started up with. Here, the output filter is executed 102294032Sgad * for each individual file as it is sent. This is not the 102394032Sgad * same as local print queues, where the output filter is 102494032Sgad * started up once, and then all jobs are passed thru that 102594032Sgad * single invocation of the output filter. 102694032Sgad * 102794032Sgad * Also note that a queue for a remote-machine can have an 102894032Sgad * input filter or an output filter, but not both. 102994032Sgad */ 103031492Swollman if (pp->filters[LPF_INPUT]) { 103194032Sgad filtcmd = pp->filters[LPF_INPUT]; 103294032Sgad av[0] = filtcmd; 103394032Sgad narg = 0; 103494032Sgad strcpy(opt_c, "-c"); 103594032Sgad strcpy(opt_h, "-h"); 103694032Sgad strcpy(opt_n, "-n"); 103724831Sbrian if (format == 'l') 103894032Sgad av[++narg] = opt_c; 103994032Sgad av[++narg] = width; 104094032Sgad av[++narg] = length; 104194032Sgad av[++narg] = indent; 104294032Sgad av[++narg] = opt_n; 104394032Sgad av[++narg] = logname; 104494032Sgad av[++narg] = opt_h; 104594032Sgad av[++narg] = origin_host; 104694032Sgad av[++narg] = pp->acct_file; 104794032Sgad av[++narg] = NULL; 104894032Sgad } else if (pp->filters[LPF_OUTPUT]) { 104994032Sgad filtcmd = pp->filters[LPF_OUTPUT]; 105094032Sgad av[0] = filtcmd; 105194032Sgad narg = 0; 105294032Sgad av[++narg] = width; 105394032Sgad av[++narg] = length; 105494032Sgad av[++narg] = NULL; 105524831Sbrian } 105624831Sbrian } 105794032Sgad if (filtcmd) { 105894032Sgad /* 105994032Sgad * If there is an input or output filter, we have to run 106094032Sgad * the datafile thru that filter and store the result as 106194032Sgad * a temporary spool file, because the protocol requires 106294032Sgad * that we send the remote host the file-size before we 106394032Sgad * start to send any of the data. 106494032Sgad */ 106594032Sgad strcpy(tfile, TFILENAME); 106694032Sgad tfd = mkstemp(tfile); 106794032Sgad if (tfd == -1) { 106894032Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 106994032Sgad TFILENAME); 107094036Sgad sfres = ERROR; 107194036Sgad goto return_sfres; 107294032Sgad } 107394036Sgad filtstat = execfilter(pp, filtcmd, av, sfd, tfd); 107424831Sbrian 107594032Sgad /* process the return-code from the filter */ 107694032Sgad switch (filtstat) { 107794032Sgad case 0: 107894032Sgad break; 107994032Sgad case 1: 108094036Sgad sfres = REPRINT; 108194036Sgad goto return_sfres; 108294032Sgad case 2: 108394036Sgad sfres = ERROR; 108494036Sgad goto return_sfres; 108594032Sgad default: 108694032Sgad syslog(LOG_WARNING, 108794032Sgad "%s: filter '%c' exited (retcode=%d)", 108894032Sgad pp->printer, format, filtstat); 108994036Sgad sfres = FILTERERR; 109094036Sgad goto return_sfres; 109194032Sgad } 109294032Sgad statrc = fstat(tfd, &stb); /* to find size of tfile */ 109394032Sgad if (statrc < 0) { 109494032Sgad syslog(LOG_ERR, 109594032Sgad "%s: error processing 'if', fstat(%s): %m", 109694032Sgad pp->printer, tfile); 109794036Sgad sfres = ERROR; 109894036Sgad goto return_sfres; 109994032Sgad } 110094036Sgad close(sfd); 110194036Sgad sfd = tfd; 110294036Sgad lseek(sfd, 0, SEEK_SET); 110394032Sgad } 110494032Sgad 11051553Srgrimes (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 11061553Srgrimes amt = strlen(buf); 11071553Srgrimes for (i = 0; ; i++) { 11081553Srgrimes if (write(pfd, buf, amt) != amt || 110931492Swollman (resp = response(pp)) < 0 || resp == '\1') { 111094036Sgad sfres = REPRINT; 111194036Sgad goto return_sfres; 11121553Srgrimes } else if (resp == '\0') 11131553Srgrimes break; 11141553Srgrimes if (i == 0) 111531492Swollman pstatus(pp, 111631492Swollman "no space on remote; waiting for queue to drain"); 11171553Srgrimes if (i == 10) 11181553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 111931492Swollman pp->printer, pp->remote_host); 11201553Srgrimes sleep(5 * 60); 11211553Srgrimes } 11221553Srgrimes if (i) 112331492Swollman pstatus(pp, "sending to %s", pp->remote_host); 112468253Sgad if (type == '\3') 112568253Sgad trstat_init(pp, file, job_dfcnt); 11261553Srgrimes for (i = 0; i < stb.st_size; i += BUFSIZ) { 11271553Srgrimes amt = BUFSIZ; 11281553Srgrimes if (i + amt > stb.st_size) 11291553Srgrimes amt = stb.st_size - i; 113094036Sgad if (sizerr == 0 && read(sfd, buf, amt) != amt) 11311553Srgrimes sizerr = 1; 11321553Srgrimes if (write(pfd, buf, amt) != amt) { 113394036Sgad sfres = REPRINT; 113494036Sgad goto return_sfres; 11351553Srgrimes } 11361553Srgrimes } 11371553Srgrimes 11381553Srgrimes if (sizerr) { 113931492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 11401553Srgrimes /* tell recvjob to ignore this file */ 11411553Srgrimes (void) write(pfd, "\1", 1); 114294036Sgad sfres = ERROR; 114394036Sgad goto return_sfres; 11441553Srgrimes } 114531492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 114694036Sgad sfres = REPRINT; 114794036Sgad goto return_sfres; 114824831Sbrian } 114968253Sgad if (type == '\3') 115068253Sgad trstat_write(pp, TR_SENDING, stb.st_size, logname, 115178300Sgad pp->remote_host, origin_host); 115294036Sgad sfres = OK; 115394036Sgad 115494036Sgadreturn_sfres: 115594036Sgad (void)close(sfd); 115694036Sgad if (tfd != -1) { 115794036Sgad /* 115894036Sgad * If tfd is set, then it is the same value as sfd, and 115994036Sgad * therefore it is already closed at this point. All 116094036Sgad * we need to do is remove the temporary file. 116194036Sgad */ 116294036Sgad tfd = -1; 116394036Sgad unlink(tfile); 116494036Sgad } 116594036Sgad return (sfres); 11661553Srgrimes} 11671553Srgrimes 11681553Srgrimes/* 116994032Sgad * This routine is called to execute one of the filters as was 117094036Sgad * specified in a printcap entry. While the child-process will read 117194036Sgad * all of 'infd', it is up to the caller to close that file descriptor 117294036Sgad * in the parent process. 117394032Sgad */ 117494032Sgadstatic int 117594032Sgadexecfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd) 117694032Sgad{ 117794032Sgad int errfd, fpid, wpid; 117894032Sgad FILE *errfp; 117994032Sgad union wait status; /* XXX */ 118094032Sgad char buf[BUFSIZ], *slash; 118194032Sgad 118294032Sgad fpid = dofork(pp, DORETURN); 118394032Sgad if (fpid != 0) { 118494032Sgad /* 118594032Sgad * This is the parent process, which just waits for the child 118694032Sgad * to complete and then returns the result. Note that it is 118794032Sgad * the child process which reads the input stream. 118894032Sgad */ 118994032Sgad if (fpid < 0) 119094032Sgad status.w_retcode = 100; 119194032Sgad else { 119294032Sgad while ((wpid = wait((int *)&status)) > 0 && 119394032Sgad wpid != fpid) 119494032Sgad ; 119594032Sgad if (wpid < 0) { 119694032Sgad status.w_retcode = 100; 119794032Sgad syslog(LOG_WARNING, 119894032Sgad "%s: after execv(%s), wait() returned: %m", 119994032Sgad pp->printer, f_cmd); 120094032Sgad } 120194032Sgad } 120294032Sgad 120394032Sgad /* 120494032Sgad * Copy everything the filter wrote to stderr from our 120594032Sgad * temporary errors file to the "lf=" logfile. 120694032Sgad */ 120794032Sgad errfp = fopen(tempstderr, "r"); 120894032Sgad if (errfp) { 120994032Sgad while (fgets(buf, sizeof(buf), errfp)) 121094032Sgad fputs(buf, stderr); 121194032Sgad fclose(errfp); 121294032Sgad } 121394032Sgad 121494032Sgad return (status.w_retcode); 121594032Sgad } 121694032Sgad 121794032Sgad /* 121894032Sgad * This is the child process, which is the one that executes the 121994032Sgad * given filter. 122094032Sgad */ 122194032Sgad /* 122294032Sgad * If the first parameter has any slashes in it, then change it 122394032Sgad * to point to the first character after the last slash. 122494032Sgad */ 122594032Sgad slash = strrchr(f_av[0], '/'); 122694032Sgad if (slash != NULL) 122794032Sgad f_av[0] = slash + 1; 122894032Sgad /* 122994032Sgad * XXX - in the future, this should setup an explicit list of 123094032Sgad * environment variables and use execve()! 123194032Sgad */ 123294032Sgad 123394032Sgad /* 123494032Sgad * Setup stdin, stdout, and stderr as we want them when the filter 123594032Sgad * is running. Stderr is setup so it points to a temporary errors 123694032Sgad * file, and the parent process will copy that temporary file to 123794032Sgad * the real logfile after the filter completes. 123894032Sgad */ 123994032Sgad dup2(infd, 0); 124094032Sgad dup2(outfd, 1); 124194032Sgad errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 124294032Sgad if (errfd >= 0) 124394032Sgad dup2(errfd, 2); 124494032Sgad closelog(); 124594032Sgad closeallfds(3); 124694032Sgad execv(f_cmd, f_av); 124794032Sgad syslog(LOG_ERR, "%s: cannot execv %s", pp->printer, f_cmd); 124894032Sgad exit(2); 124994032Sgad /* NOTREACHED */ 125094032Sgad} 125194032Sgad 125294032Sgad/* 12531553Srgrimes * Check to make sure there have been no errors and that both programs 12541553Srgrimes * are in sync with eachother. 12551553Srgrimes * Return non-zero if the connection was lost. 12561553Srgrimes */ 12571553Srgrimesstatic char 125878146Sgadresponse(const struct printer *pp) 12591553Srgrimes{ 12601553Srgrimes char resp; 12611553Srgrimes 12621553Srgrimes if (read(pfd, &resp, 1) != 1) { 126331492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 12641553Srgrimes return(-1); 12651553Srgrimes } 12661553Srgrimes return(resp); 12671553Srgrimes} 12681553Srgrimes 12691553Srgrimes/* 12701553Srgrimes * Banner printing stuff 12711553Srgrimes */ 12721553Srgrimesstatic void 127378146Sgadbanner(struct printer *pp, char *name1, char *name2) 12741553Srgrimes{ 12751553Srgrimes time_t tvec; 12761553Srgrimes 12771553Srgrimes time(&tvec); 127831492Swollman if (!pp->no_formfeed && !pp->tof) 127931492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 128031492Swollman if (pp->short_banner) { /* short banner only */ 12811553Srgrimes if (class[0]) { 12821553Srgrimes (void) write(ofd, class, strlen(class)); 12831553Srgrimes (void) write(ofd, ":", 1); 12841553Srgrimes } 12851553Srgrimes (void) write(ofd, name1, strlen(name1)); 12861553Srgrimes (void) write(ofd, " Job: ", 7); 12871553Srgrimes (void) write(ofd, name2, strlen(name2)); 12881553Srgrimes (void) write(ofd, " Date: ", 8); 12891553Srgrimes (void) write(ofd, ctime(&tvec), 24); 12901553Srgrimes (void) write(ofd, "\n", 1); 12911553Srgrimes } else { /* normal banner */ 12921553Srgrimes (void) write(ofd, "\n\n\n", 3); 129331492Swollman scan_out(pp, ofd, name1, '\0'); 12941553Srgrimes (void) write(ofd, "\n\n", 2); 129531492Swollman scan_out(pp, ofd, name2, '\0'); 12961553Srgrimes if (class[0]) { 12971553Srgrimes (void) write(ofd,"\n\n\n",3); 129831492Swollman scan_out(pp, ofd, class, '\0'); 12991553Srgrimes } 13001553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 13011553Srgrimes (void) write(ofd, name2, strlen(name2)); 13021553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 13031553Srgrimes (void) write(ofd, ctime(&tvec), 24); 13041553Srgrimes (void) write(ofd, "\n", 1); 13051553Srgrimes } 130631492Swollman if (!pp->no_formfeed) 130731492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 130831492Swollman pp->tof = 1; 13091553Srgrimes} 13101553Srgrimes 13111553Srgrimesstatic char * 131278146Sgadscnline(int key, char *p, int c) 13131553Srgrimes{ 131439084Swollman register int scnwidth; 13151553Srgrimes 13161553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 13171553Srgrimes key <<= 1; 13181553Srgrimes *p++ = key & 0200 ? c : BACKGND; 13191553Srgrimes } 13201553Srgrimes return (p); 13211553Srgrimes} 13221553Srgrimes 13231553Srgrimes#define TRC(q) (((q)-' ')&0177) 13241553Srgrimes 13251553Srgrimesstatic void 132678146Sgadscan_out(struct printer *pp, int scfd, char *scsp, int dlm) 13271553Srgrimes{ 13281553Srgrimes register char *strp; 132939084Swollman register int nchrs, j; 13301553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 13311553Srgrimes int d, scnhgt; 13321553Srgrimes 13331553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 13341553Srgrimes strp = &outbuf[0]; 13351553Srgrimes sp = scsp; 13361553Srgrimes for (nchrs = 0; ; ) { 13371553Srgrimes d = dropit(c = TRC(cc = *sp++)); 13381553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 13391553Srgrimes for (j = WIDTH; --j;) 13401553Srgrimes *strp++ = BACKGND; 13411553Srgrimes else 134231492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 134331492Swollman if (*sp == dlm || *sp == '\0' || 134431492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 13451553Srgrimes break; 13461553Srgrimes *strp++ = BACKGND; 13471553Srgrimes *strp++ = BACKGND; 13481553Srgrimes } 13491553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 13501553Srgrimes ; 13511553Srgrimes strp++; 13528857Srgrimes *strp++ = '\n'; 13531553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 13541553Srgrimes } 13551553Srgrimes} 13561553Srgrimes 13571553Srgrimesstatic int 135878146Sgaddropit(int c) 13591553Srgrimes{ 13601553Srgrimes switch(c) { 13611553Srgrimes 13621553Srgrimes case TRC('_'): 13631553Srgrimes case TRC(';'): 13641553Srgrimes case TRC(','): 13651553Srgrimes case TRC('g'): 13661553Srgrimes case TRC('j'): 13671553Srgrimes case TRC('p'): 13681553Srgrimes case TRC('q'): 13691553Srgrimes case TRC('y'): 13701553Srgrimes return (DROP); 13711553Srgrimes 13721553Srgrimes default: 13731553Srgrimes return (0); 13741553Srgrimes } 13751553Srgrimes} 13761553Srgrimes 13771553Srgrimes/* 13781553Srgrimes * sendmail --- 13791553Srgrimes * tell people about job completion 13801553Srgrimes */ 13811553Srgrimesstatic void 138294038Sgadsendmail(struct printer *pp, char *userid, int bombed) 13831553Srgrimes{ 13841553Srgrimes register int i; 13851553Srgrimes int p[2], s; 138678146Sgad register const char *cp; 13871553Srgrimes struct stat stb; 13881553Srgrimes FILE *fp; 13891553Srgrimes 13901553Srgrimes pipe(p); 139131492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 13921553Srgrimes dup2(p[0], 0); 13938094Sjkh closelog(); 139431492Swollman closeallfds(3); 139527635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 13961553Srgrimes cp++; 139731492Swollman else 13981553Srgrimes cp = _PATH_SENDMAIL; 139979452Sbrian execl(_PATH_SENDMAIL, cp, "-t", (char *)0); 140031492Swollman _exit(0); 14011553Srgrimes } else if (s > 0) { /* parent */ 14021553Srgrimes dup2(p[1], 1); 140394038Sgad printf("To: %s@%s\n", userid, origin_host); 140431492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 140515648Sjoerg *jobname ? jobname : "<unknown>"); 140678300Sgad printf("Reply-To: root@%s\n\n", local_host); 14071553Srgrimes printf("Your printer job "); 14081553Srgrimes if (*jobname) 14091553Srgrimes printf("(%s) ", jobname); 141094040Sgad 14111553Srgrimes switch (bombed) { 14121553Srgrimes case OK: 141394040Sgad cp = "OK"; 14141553Srgrimes printf("\ncompleted successfully\n"); 14151553Srgrimes break; 14161553Srgrimes default: 14171553Srgrimes case FATALERR: 141894040Sgad cp = "FATALERR"; 14191553Srgrimes printf("\ncould not be printed\n"); 14201553Srgrimes break; 14211553Srgrimes case NOACCT: 142294040Sgad cp = "NOACCT"; 142378300Sgad printf("\ncould not be printed without an account on %s\n", 142478300Sgad local_host); 14251553Srgrimes break; 14261553Srgrimes case FILTERERR: 142794040Sgad cp = "FILTERERR"; 142868664Sgad if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 142968664Sgad || (fp = fopen(tempstderr, "r")) == NULL) { 143015648Sjoerg printf("\nhad some errors and may not have printed\n"); 14311553Srgrimes break; 14321553Srgrimes } 143315648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 14341553Srgrimes while ((i = getc(fp)) != EOF) 14351553Srgrimes putchar(i); 14361553Srgrimes (void) fclose(fp); 14371553Srgrimes break; 14381553Srgrimes case ACCESS: 143994040Sgad cp = "ACCESS"; 14401553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 14411553Srgrimes } 14421553Srgrimes fflush(stdout); 14431553Srgrimes (void) close(1); 144431492Swollman } else { 144594038Sgad syslog(LOG_WARNING, "unable to send mail to %s: %m", userid); 144631492Swollman return; 14471553Srgrimes } 14481553Srgrimes (void) close(p[0]); 14491553Srgrimes (void) close(p[1]); 145015648Sjoerg wait(NULL); 145115648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 145294038Sgad userid, *jobname ? jobname : "<unknown>", pp->printer, cp); 14531553Srgrimes} 14541553Srgrimes 14551553Srgrimes/* 14561553Srgrimes * dofork - fork with retries on failure 14571553Srgrimes */ 14581553Srgrimesstatic int 145978146Sgaddofork(const struct printer *pp, int action) 14601553Srgrimes{ 146180230Sgad int i, fail, forkpid; 146260871Smpp struct passwd *pwd; 14631553Srgrimes 146480230Sgad forkpid = -1; 146580230Sgad if (daemon_uname == NULL) { 146680230Sgad pwd = getpwuid(pp->daemon_user); 146780230Sgad if (pwd == NULL) { 146880230Sgad syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file", 146980230Sgad pp->printer, pp->daemon_user); 147080230Sgad goto error_ret; 147180230Sgad } 147280230Sgad daemon_uname = strdup(pwd->pw_name); 147380230Sgad daemon_defgid = pwd->pw_gid; 147480230Sgad } 147580230Sgad 14761553Srgrimes for (i = 0; i < 20; i++) { 147780230Sgad forkpid = fork(); 147880230Sgad if (forkpid < 0) { 14791553Srgrimes sleep((unsigned)(i*i)); 14801553Srgrimes continue; 14811553Srgrimes } 14821553Srgrimes /* 14831553Srgrimes * Child should run as daemon instead of root 14841553Srgrimes */ 148578146Sgad if (forkpid == 0) { 148680230Sgad errno = 0; 148780230Sgad fail = initgroups(daemon_uname, daemon_defgid); 148880230Sgad if (fail) { 148980230Sgad syslog(LOG_ERR, "%s: initgroups(%s,%u): %m", 149080230Sgad pp->printer, daemon_uname, daemon_defgid); 149160871Smpp break; 149260871Smpp } 149380230Sgad fail = setgid(daemon_defgid); 149480230Sgad if (fail) { 149580230Sgad syslog(LOG_ERR, "%s: setgid(%u): %m", 149680230Sgad pp->printer, daemon_defgid); 149780230Sgad break; 149880230Sgad } 149980230Sgad fail = setuid(pp->daemon_user); 150080230Sgad if (fail) { 150180230Sgad syslog(LOG_ERR, "%s: setuid(%ld): %m", 150280230Sgad pp->printer, pp->daemon_user); 150380230Sgad break; 150480230Sgad } 150560871Smpp } 150680230Sgad return forkpid; 15071553Srgrimes } 15081553Srgrimes 150980230Sgad /* 151080230Sgad * An error occurred. If the error is in the child process, then 151180230Sgad * this routine MUST always exit(). DORETURN only effects how 151280230Sgad * errors should be handled in the parent process. 151380230Sgad */ 151480230Sgaderror_ret: 151580230Sgad if (forkpid == 0) { 151680230Sgad syslog(LOG_ERR, "%s: dofork(): aborting child process...", 151780230Sgad pp->printer); 151880230Sgad exit(1); 151980230Sgad } 152080230Sgad syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer); 152180230Sgad 152280230Sgad sleep(1); /* throttle errors, as a safety measure */ 15231553Srgrimes switch (action) { 15241553Srgrimes case DORETURN: 152580230Sgad return -1; 15261553Srgrimes default: 15271553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 152880230Sgad /* FALLTHROUGH */ 15291553Srgrimes case DOABORT: 15301553Srgrimes exit(1); 15311553Srgrimes } 15321553Srgrimes /*NOTREACHED*/ 15331553Srgrimes} 15341553Srgrimes 15351553Srgrimes/* 15361553Srgrimes * Kill child processes to abort current job. 15371553Srgrimes */ 15381553Srgrimesstatic void 153978146Sgadabortpr(int signo __unused) 15401553Srgrimes{ 154168664Sgad 154268664Sgad (void) unlink(tempstderr); 15431553Srgrimes kill(0, SIGINT); 15441553Srgrimes if (ofilter > 0) 15451553Srgrimes kill(ofilter, SIGCONT); 15461553Srgrimes while (wait(NULL) > 0) 15471553Srgrimes ; 154824831Sbrian if (ofilter > 0 && tfd != -1) 154924831Sbrian unlink(tfile); 15501553Srgrimes exit(0); 15511553Srgrimes} 15521553Srgrimes 15531553Srgrimesstatic void 155478146Sgadinit(struct printer *pp) 15551553Srgrimes{ 15561553Srgrimes char *s; 15571553Srgrimes 155831492Swollman sprintf(&width[2], "%ld", pp->page_width); 155931492Swollman sprintf(&length[2], "%ld", pp->page_length); 156031492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 156131492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 156231492Swollman if ((s = checkremote(pp)) != 0) { 156331492Swollman syslog(LOG_WARNING, "%s", s); 156431492Swollman free(s); 156531492Swollman } 156631492Swollman} 156731492Swollman 156831492Swollmanvoid 156978146Sgadstartprinting(const char *printer) 157031492Swollman{ 157131492Swollman struct printer myprinter, *pp = &myprinter; 157231492Swollman int status; 157331492Swollman 157431492Swollman init_printer(pp); 157531492Swollman status = getprintcap(printer, pp); 157631492Swollman switch(status) { 157731492Swollman case PCAPERR_OSERR: 157831492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 15791553Srgrimes exit(1); 158031492Swollman case PCAPERR_NOTFOUND: 15811553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 15821553Srgrimes exit(1); 158331492Swollman case PCAPERR_TCLOOP: 158431492Swollman fatal(pp, "potential reference loop detected in printcap file"); 158531492Swollman default: 158631492Swollman break; 158731492Swollman } 158831492Swollman printjob(pp); 15891553Srgrimes} 15901553Srgrimes 15911553Srgrimes/* 15921553Srgrimes * Acquire line printer or remote connection. 15931553Srgrimes */ 15941553Srgrimesstatic void 159578146Sgadopenpr(const struct printer *pp) 15961553Srgrimes{ 159731492Swollman int p[2]; 159815648Sjoerg char *cp; 15991553Srgrimes 160031492Swollman if (pp->remote) { 160131492Swollman openrem(pp); 160294032Sgad /* 160394032Sgad * Lpd does support the setting of 'of=' filters for 160494032Sgad * jobs going to remote machines, but that does not 160594032Sgad * have the same meaning as 'of=' does when handling 160694032Sgad * local print queues. For remote machines, all 'of=' 160794032Sgad * filter processing is handled in sendfile(), and that 160894032Sgad * does not use these global "output filter" variables. 160994032Sgad */ 161094032Sgad ofd = -1; 161194032Sgad ofilter = 0; 161294032Sgad return; 161331492Swollman } else if (*pp->lp) { 161431492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 161531492Swollman opennet(pp); 161615648Sjoerg else 161731492Swollman opentty(pp); 16181553Srgrimes } else { 16191553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 162031492Swollman pp->printer); 16211553Srgrimes exit(1); 16221553Srgrimes } 162315648Sjoerg 16241553Srgrimes /* 16251553Srgrimes * Start up an output filter, if needed. 16261553Srgrimes */ 162731492Swollman if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) { 16281553Srgrimes pipe(p); 162931492Swollman if (pp->remote) { 163031492Swollman strcpy(tfile, TFILENAME); 163124831Sbrian tfd = mkstemp(tfile); 163224831Sbrian } 163331492Swollman if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */ 16341553Srgrimes dup2(p[0], 0); /* pipe is std in */ 163524831Sbrian /* tfile/printer is stdout */ 163631492Swollman dup2(pp->remote ? tfd : pfd, 1); 16378094Sjkh closelog(); 163831492Swollman closeallfds(3); 163931492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 164031492Swollman cp = pp->filters[LPF_OUTPUT]; 16411553Srgrimes else 16421553Srgrimes cp++; 164379452Sbrian execl(pp->filters[LPF_OUTPUT], cp, width, length, 164479452Sbrian (char *)0); 164531492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 164631492Swollman pp->filters[LPF_OUTPUT]); 16471553Srgrimes exit(1); 16481553Srgrimes } 16491553Srgrimes (void) close(p[0]); /* close input side */ 16501553Srgrimes ofd = p[1]; /* use pipe for output */ 16511553Srgrimes } else { 16521553Srgrimes ofd = pfd; 16531553Srgrimes ofilter = 0; 16541553Srgrimes } 16551553Srgrimes} 16561553Srgrimes 165715648Sjoerg/* 165815648Sjoerg * Printer connected directly to the network 165915648Sjoerg * or to a terminal server on the net 166015648Sjoerg */ 166115648Sjoergstatic void 166278146Sgadopennet(const struct printer *pp) 166315648Sjoerg{ 166415648Sjoerg register int i; 166531492Swollman int resp; 166631492Swollman u_long port; 166731492Swollman char *ep; 166830407Sjoerg void (*savealrm)(int); 166915648Sjoerg 167031492Swollman port = strtoul(pp->lp, &ep, 0); 167138470Sbrian if (*ep != '@' || port > 65535) { 167231492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 167331492Swollman pp->lp); 167415648Sjoerg exit(1); 167515648Sjoerg } 167631492Swollman ep++; 167715648Sjoerg 167815648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 167915648Sjoerg resp = -1; 168030407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 168131492Swollman alarm(pp->conn_timeout); 168231492Swollman pfd = getport(pp, ep, port); 168331020Sjoerg alarm(0); 168430407Sjoerg (void)signal(SIGALRM, savealrm); 168515648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 168615648Sjoerg resp = 1; 168715648Sjoerg else if (pfd >= 0) { 168815648Sjoerg /* 168915648Sjoerg * need to delay a bit for rs232 lines 169015648Sjoerg * to stabilize in case printer is 169115648Sjoerg * connected via a terminal server 169215648Sjoerg */ 169315648Sjoerg delay(500); 169415648Sjoerg break; 169515648Sjoerg } 169615648Sjoerg if (i == 1) { 169731492Swollman if (resp < 0) 169831492Swollman pstatus(pp, "waiting for %s to come up", 169931492Swollman pp->lp); 170031492Swollman else 170131492Swollman pstatus(pp, 170231492Swollman "waiting for access to printer on %s", 170331492Swollman pp->lp); 170415648Sjoerg } 170515648Sjoerg sleep(i); 170615648Sjoerg } 170779739Sgad pstatus(pp, "sending to %s port %lu", ep, port); 170815648Sjoerg} 170915648Sjoerg 171015648Sjoerg/* 171115648Sjoerg * Printer is connected to an RS232 port on this host 171215648Sjoerg */ 171315648Sjoergstatic void 171478146Sgadopentty(const struct printer *pp) 171515648Sjoerg{ 171615648Sjoerg register int i; 171715648Sjoerg 171815648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 171931492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 172015648Sjoerg if (pfd >= 0) { 172115648Sjoerg delay(500); 172215648Sjoerg break; 172315648Sjoerg } 172415648Sjoerg if (errno == ENOENT) { 172531492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 172615648Sjoerg exit(1); 172715648Sjoerg } 172815648Sjoerg if (i == 1) 172931492Swollman pstatus(pp, 173031492Swollman "waiting for %s to become ready (offline?)", 173131492Swollman pp->printer); 173215648Sjoerg sleep(i); 173315648Sjoerg } 173415648Sjoerg if (isatty(pfd)) 173531492Swollman setty(pp); 173631492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 173715648Sjoerg} 173815648Sjoerg 173915648Sjoerg/* 174015648Sjoerg * Printer is on a remote host 174115648Sjoerg */ 174215648Sjoergstatic void 174378146Sgadopenrem(const struct printer *pp) 174415648Sjoerg{ 174531492Swollman register int i; 174627748Simp int resp; 174730407Sjoerg void (*savealrm)(int); 174815648Sjoerg 174915648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 175015648Sjoerg resp = -1; 175130407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 175231492Swollman alarm(pp->conn_timeout); 175331492Swollman pfd = getport(pp, pp->remote_host, 0); 175431020Sjoerg alarm(0); 175530407Sjoerg (void)signal(SIGALRM, savealrm); 175615648Sjoerg if (pfd >= 0) { 175731492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 175831492Swollman (char *)0) 175931492Swollman == 2 + strlen(pp->remote_queue)) 176031492Swollman && (resp = response(pp)) == 0) 176115648Sjoerg break; 176215648Sjoerg (void) close(pfd); 176315648Sjoerg } 176415648Sjoerg if (i == 1) { 176515648Sjoerg if (resp < 0) 176631492Swollman pstatus(pp, "waiting for %s to come up", 176731492Swollman pp->remote_host); 176815648Sjoerg else { 176931492Swollman pstatus(pp, 177031492Swollman "waiting for queue to be enabled on %s", 177131492Swollman pp->remote_host); 177215648Sjoerg i = 256; 177315648Sjoerg } 177415648Sjoerg } 177515648Sjoerg sleep(i); 177615648Sjoerg } 177731492Swollman pstatus(pp, "sending to %s", pp->remote_host); 177815648Sjoerg} 177915648Sjoerg 17801553Srgrimes/* 17811553Srgrimes * setup tty lines. 17821553Srgrimes */ 17831553Srgrimesstatic void 178478146Sgadsetty(const struct printer *pp) 17851553Srgrimes{ 178615032Ssef struct termios ttybuf; 17871553Srgrimes 17881553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 178931492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 17901553Srgrimes exit(1); 17911553Srgrimes } 179215032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 179331492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 17941553Srgrimes exit(1); 17951553Srgrimes } 179631492Swollman if (pp->baud_rate > 0) 179731492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 179831492Swollman if (pp->mode_set) { 179931492Swollman char *s = strdup(pp->mode_set), *tmp; 180015032Ssef 180131492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 180239084Swollman (void) msearch(tmp, &ttybuf); 18031553Srgrimes } 18041553Srgrimes } 180531492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 180615032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 180731492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 18081553Srgrimes } 18091553Srgrimes } 18101553Srgrimes} 18111553Srgrimes 181227757Simp#ifdef __STDC__ 18131553Srgrimes#include <stdarg.h> 18141553Srgrimes#else 18151553Srgrimes#include <varargs.h> 18161553Srgrimes#endif 18171553Srgrimes 181815648Sjoergstatic void 181927757Simp#ifdef __STDC__ 182031492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 18211553Srgrimes#else 182231492Swollmanpstatus(pp, msg, va_alist) 182331492Swollman const struct printer *pp; 18241553Srgrimes char *msg; 18251553Srgrimes va_dcl 18261553Srgrimes#endif 18271553Srgrimes{ 182831492Swollman int fd; 182931492Swollman char *buf; 18301553Srgrimes va_list ap; 183127757Simp#ifdef __STDC__ 18321553Srgrimes va_start(ap, msg); 18331553Srgrimes#else 18341553Srgrimes va_start(ap); 18351553Srgrimes#endif 18361553Srgrimes 18371553Srgrimes umask(0); 183831492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 183931492Swollman if (fd < 0) { 184031492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file); 18411553Srgrimes exit(1); 18421553Srgrimes } 18431553Srgrimes ftruncate(fd, 0); 184431492Swollman vasprintf(&buf, msg, ap); 18451553Srgrimes va_end(ap); 184631492Swollman writel(fd, buf, "\n", (char *)0); 184731492Swollman close(fd); 184831492Swollman free(buf); 18491553Srgrimes} 185030407Sjoerg 185130407Sjoergvoid 185278146Sgadalarmhandler(int signo __unused) 185330407Sjoerg{ 185478146Sgad /* the signal is ignored */ 185578146Sgad /* (the '__unused' is just to avoid a compile-time warning) */ 185630407Sjoerg} 1857