printjob.c revision 138939
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 41117554Sgad#if 0 42117587Sgad#ifndef lint 4315648Sjoergstatic char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95"; 44117587Sgad#endif /* not lint */ 45117554Sgad#endif 461553Srgrimes 47117554Sgad#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 48117554Sgad__FBSDID("$FreeBSD: head/usr.sbin/lpr/lpd/printjob.c 138939 2004-12-17 01:54:50Z gad $"); 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/* 84119192Sgad * The buffer size to use when reading/writing spool files. 85119192Sgad */ 86119192Sgad#define SPL_BUFSIZ BUFSIZ 87119192Sgad 88119192Sgad/* 891553Srgrimes * Error tokens 901553Srgrimes */ 911553Srgrimes#define REPRINT -2 921553Srgrimes#define ERROR -1 931553Srgrimes#define OK 0 941553Srgrimes#define FATALERR 1 951553Srgrimes#define NOACCT 2 961553Srgrimes#define FILTERERR 3 971553Srgrimes#define ACCESS 4 981553Srgrimes 991553Srgrimesstatic dev_t fdev; /* device of file pointed to by symlink */ 1001553Srgrimesstatic ino_t fino; /* inode of file pointed to by symlink */ 1011553Srgrimesstatic FILE *cfp; /* control file */ 10297793Sgadstatic pid_t of_pid; /* process id of output filter, if any */ 1031553Srgrimesstatic int child; /* id of any filters */ 10468253Sgadstatic int job_dfcnt; /* count of datafiles in current user job */ 1051553Srgrimesstatic int lfd; /* lock file descriptor */ 1061553Srgrimesstatic int ofd; /* output filter file descriptor */ 10724831Sbrianstatic int tfd = -1; /* output filter temp file output */ 1081553Srgrimesstatic int pfd; /* prstatic inter file descriptor */ 1091553Srgrimesstatic int prchild; /* id of pr process */ 1101553Srgrimesstatic char title[80]; /* ``pr'' title */ 11153956Sachestatic char locale[80]; /* ``pr'' locale */ 1121553Srgrimes 11380230Sgad/* these two are set from pp->daemon_user, but only if they are needed */ 11480230Sgadstatic char *daemon_uname; /* set from pwd->pw_name */ 11580230Sgadstatic int daemon_defgid; 11680230Sgad 1171553Srgrimesstatic char class[32]; /* classification field */ 11878300Sgadstatic char origin_host[MAXHOSTNAMELEN]; /* user's host machine */ 1191553Srgrimes /* indentation size in static characters */ 1208857Srgrimesstatic char indent[10] = "-i0"; 1211553Srgrimesstatic char jobname[100]; /* job or file name */ 1221553Srgrimesstatic char length[10] = "-l"; /* page length in lines */ 1231553Srgrimesstatic char logname[32]; /* user's login name */ 1241553Srgrimesstatic char pxlength[10] = "-y"; /* page length in pixels */ 1251553Srgrimesstatic char pxwidth[10] = "-x"; /* page width in pixels */ 12668664Sgad/* tempstderr is the filename used to catch stderr from exec-ing filters */ 12768664Sgadstatic char tempstderr[] = "errs.XXXXXXX"; 1281553Srgrimesstatic char width[10] = "-w"; /* page width in static characters */ 12924831Sbrian#define TFILENAME "fltXXXXXX" 13024831Sbrianstatic char tfile[] = TFILENAME; /* file name for filter output */ 1311553Srgrimes 13278146Sgadstatic void abortpr(int _signo); 13378146Sgadstatic void alarmhandler(int _signo); 13478146Sgadstatic void banner(struct printer *_pp, char *_name1, char *_name2); 13578146Sgadstatic int dofork(const struct printer *_pp, int _action); 13678146Sgadstatic int dropit(int _c); 13794032Sgadstatic int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av, 13894032Sgad int _infd, int _outfd); 13978146Sgadstatic void init(struct printer *_pp); 14078146Sgadstatic void openpr(const struct printer *_pp); 14178146Sgadstatic void opennet(const struct printer *_pp); 14278146Sgadstatic void opentty(const struct printer *_pp); 14378146Sgadstatic void openrem(const struct printer *pp); 14478146Sgadstatic int print(struct printer *_pp, int _format, char *_file); 14578146Sgadstatic int printit(struct printer *_pp, char *_file); 14679739Sgadstatic void pstatus(const struct printer *_pp, const char *_msg, ...) 14779739Sgad __printflike(2, 3); 14878146Sgadstatic char response(const struct printer *_pp); 14978146Sgadstatic void scan_out(struct printer *_pp, int _scfd, char *_scsp, 15078146Sgad int _dlm); 15178146Sgadstatic char *scnline(int _key, char *_p, int _c); 15278146Sgadstatic int sendfile(struct printer *_pp, int _type, char *_file, 15395293Sgad char _format, int _copyreq); 15478146Sgadstatic int sendit(struct printer *_pp, char *_file); 15594038Sgadstatic void sendmail(struct printer *_pp, char *_userid, int _bombed); 15678146Sgadstatic void setty(const struct printer *_pp); 157138939Sgadstatic void wait4data(struct printer *_pp, const char *_dfile); 1581553Srgrimes 1591553Srgrimesvoid 16078146Sgadprintjob(struct printer *pp) 1611553Srgrimes{ 1621553Srgrimes struct stat stb; 16368401Sgad register struct jobqueue *q, **qp; 16468401Sgad struct jobqueue **queue; 1651553Srgrimes register int i, nitems; 16668733Sgad off_t pidoff; 16797793Sgad pid_t printpid; 16868733Sgad int errcnt, jobcount, tempfd; 1691553Srgrimes 17068733Sgad jobcount = 0; 17131492Swollman init(pp); /* set up capabilities */ 172118881Sgad (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */ 173118881Sgad (void) close(STDERR_FILENO); /* set up log file */ 17431492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { 17597792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 17697792Sgad pp->log_file); 1771553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1781553Srgrimes } 1791553Srgrimes setgid(getegid()); 18097793Sgad printpid = getpid(); /* for use with lprm */ 18197793Sgad setpgrp(0, printpid); 18279735Sgad 18379735Sgad /* 18479735Sgad * At initial lpd startup, printjob may be called with various 18579735Sgad * signal handlers in effect. After that initial startup, any 18679735Sgad * calls to printjob will have a *different* set of signal-handlers 18779735Sgad * in effect. Make sure all handlers are the ones we want. 18879735Sgad */ 18979735Sgad signal(SIGCHLD, SIG_DFL); 1901553Srgrimes signal(SIGHUP, abortpr); 1911553Srgrimes signal(SIGINT, abortpr); 1921553Srgrimes signal(SIGQUIT, abortpr); 1931553Srgrimes signal(SIGTERM, abortpr); 1941553Srgrimes 1951553Srgrimes /* 1961553Srgrimes * uses short form file names 1971553Srgrimes */ 19831492Swollman if (chdir(pp->spool_dir) < 0) { 19997792Sgad syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer, 20097792Sgad pp->spool_dir); 2011553Srgrimes exit(1); 2021553Srgrimes } 20331492Swollman if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS)) 2041553Srgrimes exit(0); /* printing disabled */ 20531492Swollman lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 20631492Swollman LOCK_FILE_MODE); 2071553Srgrimes if (lfd < 0) { 20831492Swollman if (errno == EWOULDBLOCK) /* active daemon present */ 20931492Swollman exit(0); 21097792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 21197792Sgad pp->lock_file); 2121553Srgrimes exit(1); 2131553Srgrimes } 21431492Swollman /* turn off non-blocking mode (was turned on for lock effects only) */ 21531492Swollman if (fcntl(lfd, F_SETFL, 0) < 0) { 21697792Sgad syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer, 21797792Sgad pp->lock_file); 2181553Srgrimes exit(1); 2191553Srgrimes } 2201553Srgrimes ftruncate(lfd, 0); 2211553Srgrimes /* 2221553Srgrimes * write process id for others to know 2231553Srgrimes */ 22497793Sgad sprintf(line, "%u\n", printpid); 2251553Srgrimes pidoff = i = strlen(line); 2261553Srgrimes if (write(lfd, line, i) != i) { 22797792Sgad syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, 22897792Sgad pp->lock_file); 2291553Srgrimes exit(1); 2301553Srgrimes } 2311553Srgrimes /* 2321553Srgrimes * search the spool directory for work and sort by queue order. 2331553Srgrimes */ 23431492Swollman if ((nitems = getq(pp, &queue)) < 0) { 23531492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 23697792Sgad pp->spool_dir); 2371553Srgrimes exit(1); 2381553Srgrimes } 2391553Srgrimes if (nitems == 0) /* no work to do */ 2401553Srgrimes exit(0); 24131492Swollman if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ 24231492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) 24397792Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 24497792Sgad pp->lock_file); 2451553Srgrimes } 24668664Sgad 24768664Sgad /* create a file which will be used to hold stderr from filters */ 24868664Sgad if ((tempfd = mkstemp(tempstderr)) == -1) { 24968664Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 25097792Sgad tempstderr); 25168732Sgad exit(1); 25268664Sgad } 25368664Sgad if ((i = fchmod(tempfd, 0664)) == -1) { 25468664Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 25597792Sgad tempstderr); 25668732Sgad exit(1); 25768664Sgad } 25868664Sgad /* lpd doesn't need it to be open, it just needs it to exist */ 25968664Sgad close(tempfd); 26068664Sgad 26131492Swollman openpr(pp); /* open printer or remote */ 2621553Srgrimesagain: 2631553Srgrimes /* 2641553Srgrimes * we found something to do now do it -- 2651553Srgrimes * write the name of the current control file into the lock file 2661553Srgrimes * so the spool queue program can tell what we're working on 2671553Srgrimes */ 2681553Srgrimes for (qp = queue; nitems--; free((char *) q)) { 2691553Srgrimes q = *qp++; 27068401Sgad if (stat(q->job_cfname, &stb) < 0) 2711553Srgrimes continue; 27215648Sjoerg errcnt = 0; 2731553Srgrimes restart: 27415648Sjoerg (void) lseek(lfd, pidoff, 0); 27568401Sgad (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname); 2761553Srgrimes i = strlen(line); 2771553Srgrimes if (write(lfd, line, i) != i) 27897792Sgad syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, 27997792Sgad pp->lock_file); 28031492Swollman if (!pp->remote) 28168401Sgad i = printit(pp, q->job_cfname); 2821553Srgrimes else 28368401Sgad i = sendit(pp, q->job_cfname); 2841553Srgrimes /* 2851553Srgrimes * Check to see if we are supposed to stop printing or 2861553Srgrimes * if we are to rebuild the queue. 2871553Srgrimes */ 2881553Srgrimes if (fstat(lfd, &stb) == 0) { 2891553Srgrimes /* stop printing before starting next job? */ 29031492Swollman if (stb.st_mode & LFM_PRINT_DIS) 2911553Srgrimes goto done; 2921553Srgrimes /* rebuild queue (after lpc topq) */ 29331492Swollman if (stb.st_mode & LFM_RESET_QUE) { 29431492Swollman for (free(q); nitems--; free(q)) 2951553Srgrimes q = *qp++; 29631492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) 29731492Swollman < 0) 29897792Sgad syslog(LOG_WARNING, 29997792Sgad "%s: fchmod(%s): %m", 30097792Sgad pp->printer, pp->lock_file); 3011553Srgrimes break; 3021553Srgrimes } 3031553Srgrimes } 30468733Sgad if (i == OK) /* all files of this job printed */ 30568733Sgad jobcount++; 30615648Sjoerg else if (i == REPRINT && ++errcnt < 5) { 30715648Sjoerg /* try reprinting the job */ 30831492Swollman syslog(LOG_INFO, "restarting %s", pp->printer); 30997793Sgad if (of_pid > 0) { 31097793Sgad kill(of_pid, SIGCONT); /* to be sure */ 3111553Srgrimes (void) close(ofd); 31297793Sgad while ((i = wait(NULL)) > 0 && i != of_pid) 3131553Srgrimes ; 31479735Sgad if (i < 0) 31579735Sgad syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m", 31697793Sgad pp->printer, of_pid); 31797793Sgad of_pid = 0; 3181553Srgrimes } 3191553Srgrimes (void) close(pfd); /* close printer */ 3201553Srgrimes if (ftruncate(lfd, pidoff) < 0) 32197792Sgad syslog(LOG_WARNING, "%s: ftruncate(%s): %m", 32297792Sgad pp->printer, pp->lock_file); 32331492Swollman openpr(pp); /* try to reopen printer */ 3241553Srgrimes goto restart; 32515648Sjoerg } else { 32631492Swollman syslog(LOG_WARNING, "%s: job could not be %s (%s)", 32797792Sgad pp->printer, 32897792Sgad pp->remote ? "sent to remote host" : "printed", 32997792Sgad q->job_cfname); 33015648Sjoerg if (i == REPRINT) { 33127748Simp /* ensure we don't attempt this job again */ 33268401Sgad (void) unlink(q->job_cfname); 33368401Sgad q->job_cfname[0] = 'd'; 33468401Sgad (void) unlink(q->job_cfname); 33515648Sjoerg if (logname[0]) 33631492Swollman sendmail(pp, logname, FATALERR); 33715648Sjoerg } 3381553Srgrimes } 3391553Srgrimes } 34031492Swollman free(queue); 3411553Srgrimes /* 3421553Srgrimes * search the spool directory for more work. 3431553Srgrimes */ 34431492Swollman if ((nitems = getq(pp, &queue)) < 0) { 34531492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 34697792Sgad pp->spool_dir); 3471553Srgrimes exit(1); 3481553Srgrimes } 3491553Srgrimes if (nitems == 0) { /* no more work to do */ 3501553Srgrimes done: 35168733Sgad if (jobcount > 0) { /* jobs actually printed */ 35231492Swollman if (!pp->no_formfeed && !pp->tof) 35331492Swollman (void) write(ofd, pp->form_feed, 35431492Swollman strlen(pp->form_feed)); 35531492Swollman if (pp->trailer != NULL) /* output trailer */ 35631492Swollman (void) write(ofd, pp->trailer, 35731492Swollman strlen(pp->trailer)); 3581553Srgrimes } 35919202Simp (void) close(ofd); 36019202Simp (void) wait(NULL); 36168664Sgad (void) unlink(tempstderr); 3621553Srgrimes exit(0); 3631553Srgrimes } 3641553Srgrimes goto again; 3651553Srgrimes} 3661553Srgrimes 3671553Srgrimeschar fonts[4][50]; /* fonts for troff */ 3681553Srgrimes 3691553Srgrimeschar ifonts[4][40] = { 3701553Srgrimes _PATH_VFONTR, 3711553Srgrimes _PATH_VFONTI, 3721553Srgrimes _PATH_VFONTB, 3731553Srgrimes _PATH_VFONTS, 3741553Srgrimes}; 3751553Srgrimes 3761553Srgrimes/* 3771553Srgrimes * The remaining part is the reading of the control file (cf) 3781553Srgrimes * and performing the various actions. 3791553Srgrimes */ 3801553Srgrimesstatic int 38178146Sgadprintit(struct printer *pp, char *file) 3821553Srgrimes{ 3831553Srgrimes register int i; 38468734Sgad char *cp; 38568734Sgad int bombed, didignorehdr; 3861553Srgrimes 38768734Sgad bombed = OK; 38868734Sgad didignorehdr = 0; 3891553Srgrimes /* 3901553Srgrimes * open control file; ignore if no longer there. 3911553Srgrimes */ 3921553Srgrimes if ((cfp = fopen(file, "r")) == NULL) { 39397792Sgad syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file); 39497791Sgad return (OK); 3951553Srgrimes } 3961553Srgrimes /* 3971553Srgrimes * Reset troff fonts. 3981553Srgrimes */ 3991553Srgrimes for (i = 0; i < 4; i++) 4001553Srgrimes strcpy(fonts[i], ifonts[i]); 40131492Swollman sprintf(&width[2], "%ld", pp->page_width); 4021553Srgrimes strcpy(indent+2, "0"); 4031553Srgrimes 40468253Sgad /* initialize job-specific count of datafiles processed */ 40568253Sgad job_dfcnt = 0; 40668253Sgad 4071553Srgrimes /* 4081553Srgrimes * read the control file for work to do 4091553Srgrimes * 4101553Srgrimes * file format -- first character in the line is a command 4111553Srgrimes * rest of the line is the argument. 4121553Srgrimes * valid commands are: 4131553Srgrimes * 4141553Srgrimes * S -- "stat info" for symbolic link protection 4151553Srgrimes * J -- "job name" on banner page 4161553Srgrimes * C -- "class name" on banner page 4171553Srgrimes * L -- "literal" user's name to print on banner 4181553Srgrimes * T -- "title" for pr 4191553Srgrimes * H -- "host name" of machine where lpr was done 4201553Srgrimes * P -- "person" user's login name 4211553Srgrimes * I -- "indent" amount to indent output 42215648Sjoerg * R -- laser dpi "resolution" 4231553Srgrimes * f -- "file name" name of text file to print 4241553Srgrimes * l -- "file name" text file with control chars 42583684Sgad * o -- "file name" postscript file, according to 42683684Sgad * the RFC. Here it is treated like an 'f'. 4271553Srgrimes * p -- "file name" text file to print with pr(1) 4281553Srgrimes * t -- "file name" troff(1) file to print 4291553Srgrimes * n -- "file name" ditroff(1) file to print 4301553Srgrimes * d -- "file name" dvi file to print 4311553Srgrimes * g -- "file name" plot(1G) file to print 4321553Srgrimes * v -- "file name" plain raster file to print 4331553Srgrimes * c -- "file name" cifplot file to print 4341553Srgrimes * 1 -- "R font file" for troff 4351553Srgrimes * 2 -- "I font file" for troff 4361553Srgrimes * 3 -- "B font file" for troff 4371553Srgrimes * 4 -- "S font file" for troff 4381553Srgrimes * N -- "name" of file (used by lpq) 4391553Srgrimes * U -- "unlink" name of file to remove 4401553Srgrimes * (after we print it. (Pass 2 only)). 4411553Srgrimes * M -- "mail" to user when done printing 44253956Sache * Z -- "locale" for pr 4431553Srgrimes * 4441553Srgrimes * getline reads a line and expands tabs to blanks 4451553Srgrimes */ 4461553Srgrimes 4471553Srgrimes /* pass 1 */ 4481553Srgrimes 4491553Srgrimes while (getline(cfp)) 4501553Srgrimes switch (line[0]) { 4511553Srgrimes case 'H': 45278300Sgad strlcpy(origin_host, line + 1, sizeof(origin_host)); 45327748Simp if (class[0] == '\0') { 45480133Sgad strlcpy(class, line+1, sizeof(class)); 45527748Simp } 4561553Srgrimes continue; 4571553Srgrimes 4581553Srgrimes case 'P': 45980133Sgad strlcpy(logname, line + 1, sizeof(logname)); 46031492Swollman if (pp->restricted) { /* restricted */ 4611553Srgrimes if (getpwnam(logname) == NULL) { 4621553Srgrimes bombed = NOACCT; 46331492Swollman sendmail(pp, line+1, bombed); 4641553Srgrimes goto pass2; 4651553Srgrimes } 4661553Srgrimes } 4671553Srgrimes continue; 4681553Srgrimes 4691553Srgrimes case 'S': 4701553Srgrimes cp = line+1; 4711553Srgrimes i = 0; 4721553Srgrimes while (*cp >= '0' && *cp <= '9') 4731553Srgrimes i = i * 10 + (*cp++ - '0'); 4741553Srgrimes fdev = i; 4751553Srgrimes cp++; 4761553Srgrimes i = 0; 4771553Srgrimes while (*cp >= '0' && *cp <= '9') 4781553Srgrimes i = i * 10 + (*cp++ - '0'); 4791553Srgrimes fino = i; 4801553Srgrimes continue; 4811553Srgrimes 4821553Srgrimes case 'J': 48327748Simp if (line[1] != '\0') { 48480133Sgad strlcpy(jobname, line + 1, sizeof(jobname)); 48527748Simp } else 4861553Srgrimes strcpy(jobname, " "); 4871553Srgrimes continue; 4881553Srgrimes 4891553Srgrimes case 'C': 4901553Srgrimes if (line[1] != '\0') 49180133Sgad strlcpy(class, line + 1, sizeof(class)); 49280133Sgad else if (class[0] == '\0') { 49380133Sgad /* XXX - why call gethostname instead of 49480133Sgad * just strlcpy'ing local_host? */ 4951553Srgrimes gethostname(class, sizeof(class)); 49680133Sgad class[sizeof(class) - 1] = '\0'; 49780133Sgad } 4981553Srgrimes continue; 4991553Srgrimes 5001553Srgrimes case 'T': /* header title for pr */ 50180133Sgad strlcpy(title, line + 1, sizeof(title)); 5021553Srgrimes continue; 5031553Srgrimes 5041553Srgrimes case 'L': /* identification line */ 50531492Swollman if (!pp->no_header && !pp->header_last) 50631492Swollman banner(pp, line+1, jobname); 5071553Srgrimes continue; 5081553Srgrimes 5091553Srgrimes case '1': /* troff fonts */ 5101553Srgrimes case '2': 5111553Srgrimes case '3': 5121553Srgrimes case '4': 51327748Simp if (line[1] != '\0') { 51480133Sgad strlcpy(fonts[line[0]-'1'], line + 1, 51580133Sgad (size_t)50); 51627748Simp } 5171553Srgrimes continue; 5181553Srgrimes 5191553Srgrimes case 'W': /* page width */ 52080133Sgad strlcpy(width+2, line + 1, sizeof(width) - 2); 5211553Srgrimes continue; 5221553Srgrimes 5231553Srgrimes case 'I': /* indent amount */ 52480133Sgad strlcpy(indent+2, line + 1, sizeof(indent) - 2); 5251553Srgrimes continue; 5261553Srgrimes 52753956Sache case 'Z': /* locale for pr */ 52880133Sgad strlcpy(locale, line + 1, sizeof(locale)); 52953956Sache continue; 53053956Sache 5311553Srgrimes default: /* some file to print */ 53268467Sgad /* only lowercase cmd-codes include a file-to-print */ 53368467Sgad if ((line[0] < 'a') || (line[0] > 'z')) { 53468467Sgad /* ignore any other lines */ 53568467Sgad if (lflag <= 1) 53668467Sgad continue; 53768467Sgad if (!didignorehdr) { 53868467Sgad syslog(LOG_INFO, "%s: in %s :", 53997792Sgad pp->printer, file); 54068467Sgad didignorehdr = 1; 54168467Sgad } 54268467Sgad syslog(LOG_INFO, "%s: ignoring line: '%c' %s", 54397792Sgad pp->printer, line[0], &line[1]); 54468467Sgad continue; 54568467Sgad } 54668467Sgad i = print(pp, line[0], line+1); 54768467Sgad switch (i) { 5481553Srgrimes case ERROR: 5491553Srgrimes if (bombed == OK) 5501553Srgrimes bombed = FATALERR; 5511553Srgrimes break; 5521553Srgrimes case REPRINT: 5531553Srgrimes (void) fclose(cfp); 55497791Sgad return (REPRINT); 5551553Srgrimes case FILTERERR: 5561553Srgrimes case ACCESS: 5571553Srgrimes bombed = i; 55831492Swollman sendmail(pp, logname, bombed); 5591553Srgrimes } 5601553Srgrimes title[0] = '\0'; 5611553Srgrimes continue; 5621553Srgrimes 5631553Srgrimes case 'N': 5641553Srgrimes case 'U': 5651553Srgrimes case 'M': 56615648Sjoerg case 'R': 5671553Srgrimes continue; 5681553Srgrimes } 5691553Srgrimes 5701553Srgrimes /* pass 2 */ 5711553Srgrimes 5721553Srgrimespass2: 5731553Srgrimes fseek(cfp, 0L, 0); 5741553Srgrimes while (getline(cfp)) 5751553Srgrimes switch (line[0]) { 5761553Srgrimes case 'L': /* identification line */ 57731492Swollman if (!pp->no_header && pp->header_last) 57831492Swollman banner(pp, line+1, jobname); 5791553Srgrimes continue; 5801553Srgrimes 5811553Srgrimes case 'M': 5821553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 58331492Swollman sendmail(pp, line+1, bombed); 5841553Srgrimes continue; 5851553Srgrimes 5861553Srgrimes case 'U': 58727748Simp if (strchr(line+1, '/')) 58827748Simp continue; 5891553Srgrimes (void) unlink(line+1); 5901553Srgrimes } 5911553Srgrimes /* 5921553Srgrimes * clean-up in case another control file exists 5931553Srgrimes */ 5941553Srgrimes (void) fclose(cfp); 5951553Srgrimes (void) unlink(file); 59697791Sgad return (bombed == OK ? OK : ERROR); 5971553Srgrimes} 5981553Srgrimes 5991553Srgrimes/* 6001553Srgrimes * Print a file. 6011553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 6021553Srgrimes * Return -1 if a non-recoverable error occured, 6031553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 6041553Srgrimes * 1 if we should try to reprint this job and 6051553Srgrimes * 0 if all is well. 6061553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 6071553Srgrimes * stderr as the log file, and must not ignore SIGINT. 6081553Srgrimes */ 6091553Srgrimesstatic int 61078146Sgadprint(struct printer *pp, int format, char *file) 6111553Srgrimes{ 61253956Sache register int n, i; 6131553Srgrimes register char *prog; 61431492Swollman int fi, fo; 6151553Srgrimes FILE *fp; 616119192Sgad char *av[15], buf[SPL_BUFSIZ]; 61797793Sgad pid_t wpid; 61897793Sgad int p[2], retcode, stopped, wstatus, wstatus_set; 6191553Srgrimes struct stat stb; 6201553Srgrimes 621138939Sgad /* Make sure the entire data file has arrived. */ 622138939Sgad wait4data(pp, file); 623138939Sgad 62468467Sgad if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { 62568467Sgad syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", 62697792Sgad pp->printer, file, format); 62797791Sgad return (ERROR); 62868467Sgad } 6291553Srgrimes /* 6301553Srgrimes * Check to see if data file is a symbolic link. If so, it should 6311553Srgrimes * still point to the same file or someone is trying to print 6321553Srgrimes * something he shouldn't. 6331553Srgrimes */ 6341553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 6351553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 63697791Sgad return (ACCESS); 63768253Sgad 63868253Sgad job_dfcnt++; /* increment datafile counter for this job */ 63968734Sgad stopped = 0; /* output filter is not stopped */ 64068253Sgad 64168253Sgad /* everything seems OK, start it up */ 64231492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 64331492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 64431492Swollman pp->tof = 1; 6451553Srgrimes } 64631492Swollman if (pp->filters[LPF_INPUT] == NULL 64783684Sgad && (format == 'f' || format == 'l' || format == 'o')) { 64831492Swollman pp->tof = 0; 649119192Sgad while ((n = read(fi, buf, SPL_BUFSIZ)) > 0) 6501553Srgrimes if (write(ofd, buf, n) != n) { 6511553Srgrimes (void) close(fi); 65297791Sgad return (REPRINT); 6531553Srgrimes } 6541553Srgrimes (void) close(fi); 65597791Sgad return (OK); 6561553Srgrimes } 6571553Srgrimes switch (format) { 6581553Srgrimes case 'p': /* print file using 'pr' */ 65931492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 6601553Srgrimes prog = _PATH_PR; 66153956Sache i = 0; 66253956Sache av[i++] = "pr"; 66353956Sache av[i++] = width; 66453956Sache av[i++] = length; 66553956Sache av[i++] = "-h"; 66653956Sache av[i++] = *title ? title : " "; 66753956Sache av[i++] = "-L"; 66853956Sache av[i++] = *locale ? locale : "C"; 66953956Sache av[i++] = "-F"; 67053956Sache av[i] = 0; 6711553Srgrimes fo = ofd; 6721553Srgrimes goto start; 6731553Srgrimes } 6741553Srgrimes pipe(p); 67531492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 676118881Sgad dup2(fi, STDIN_FILENO); /* file is stdin */ 677118881Sgad dup2(p[1], STDOUT_FILENO); /* pipe is stdout */ 6788094Sjkh closelog(); 67931492Swollman closeallfds(3); 6801553Srgrimes execl(_PATH_PR, "pr", width, length, 68153956Sache "-h", *title ? title : " ", 68253956Sache "-L", *locale ? locale : "C", 68379452Sbrian "-F", (char *)0); 6841553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6851553Srgrimes exit(2); 6861553Srgrimes } 6871553Srgrimes (void) close(p[1]); /* close output side */ 6881553Srgrimes (void) close(fi); 6891553Srgrimes if (prchild < 0) { 6901553Srgrimes prchild = 0; 6911553Srgrimes (void) close(p[0]); 69297791Sgad return (ERROR); 6931553Srgrimes } 6941553Srgrimes fi = p[0]; /* use pipe for input */ 6951553Srgrimes case 'f': /* print plain text file */ 69631492Swollman prog = pp->filters[LPF_INPUT]; 6971553Srgrimes av[1] = width; 6981553Srgrimes av[2] = length; 6991553Srgrimes av[3] = indent; 7001553Srgrimes n = 4; 7011553Srgrimes break; 70286935Sgad case 'o': /* print postscript file */ 70386935Sgad /* 70486935Sgad * Treat this as a "plain file with control characters", and 70586935Sgad * assume the standard LPF_INPUT filter will recognize that 70686935Sgad * the data is postscript and know what to do with it. These 70786935Sgad * 'o'-file requests could come from MacOS 10.1 systems. 70886935Sgad * (later versions of MacOS 10 will explicitly use 'l') 70986935Sgad * A postscript file can contain binary data, which is why 'l' 71086935Sgad * is somewhat more appropriate than 'f'. 71186935Sgad */ 71286935Sgad /* FALLTHROUGH */ 7131553Srgrimes case 'l': /* like 'f' but pass control characters */ 71431492Swollman prog = pp->filters[LPF_INPUT]; 7151553Srgrimes av[1] = "-c"; 7161553Srgrimes av[2] = width; 7171553Srgrimes av[3] = length; 7181553Srgrimes av[4] = indent; 7191553Srgrimes n = 5; 7201553Srgrimes break; 7211553Srgrimes case 'r': /* print a fortran text file */ 72231492Swollman prog = pp->filters[LPF_FORTRAN]; 7231553Srgrimes av[1] = width; 7241553Srgrimes av[2] = length; 7251553Srgrimes n = 3; 7261553Srgrimes break; 7271553Srgrimes case 't': /* print troff output */ 7281553Srgrimes case 'n': /* print ditroff output */ 7291553Srgrimes case 'd': /* print tex output */ 7301553Srgrimes (void) unlink(".railmag"); 7311553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 73231492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 73397792Sgad pp->printer); 7341553Srgrimes (void) unlink(".railmag"); 7351553Srgrimes } else { 7361553Srgrimes for (n = 0; n < 4; n++) { 7371553Srgrimes if (fonts[n][0] != '/') 7381553Srgrimes (void) write(fo, _PATH_VFONT, 7391553Srgrimes sizeof(_PATH_VFONT) - 1); 7401553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 7411553Srgrimes (void) write(fo, "\n", 1); 7421553Srgrimes } 7431553Srgrimes (void) close(fo); 7441553Srgrimes } 74531492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 74631492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 74731492Swollman : pp->filters[LPF_DVI]); 7481553Srgrimes av[1] = pxwidth; 7491553Srgrimes av[2] = pxlength; 7501553Srgrimes n = 3; 7511553Srgrimes break; 7521553Srgrimes case 'c': /* print cifplot output */ 75331492Swollman prog = pp->filters[LPF_CIFPLOT]; 7541553Srgrimes av[1] = pxwidth; 7551553Srgrimes av[2] = pxlength; 7561553Srgrimes n = 3; 7571553Srgrimes break; 7581553Srgrimes case 'g': /* print plot(1G) output */ 75931492Swollman prog = pp->filters[LPF_GRAPH]; 7601553Srgrimes av[1] = pxwidth; 7611553Srgrimes av[2] = pxlength; 7621553Srgrimes n = 3; 7631553Srgrimes break; 7641553Srgrimes case 'v': /* print raster output */ 76531492Swollman prog = pp->filters[LPF_RASTER]; 7661553Srgrimes av[1] = pxwidth; 7671553Srgrimes av[2] = pxlength; 7681553Srgrimes n = 3; 7691553Srgrimes break; 7701553Srgrimes default: 7711553Srgrimes (void) close(fi); 7721553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 77397792Sgad pp->printer, format); 77497791Sgad return (ERROR); 7751553Srgrimes } 77615648Sjoerg if (prog == NULL) { 77715648Sjoerg (void) close(fi); 77815648Sjoerg syslog(LOG_ERR, 77915648Sjoerg "%s: no filter found in printcap for format character '%c'", 78031492Swollman pp->printer, format); 78197791Sgad return (ERROR); 78215648Sjoerg } 78327635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 7841553Srgrimes av[0]++; 7851553Srgrimes else 7861553Srgrimes av[0] = prog; 7871553Srgrimes av[n++] = "-n"; 7881553Srgrimes av[n++] = logname; 7891553Srgrimes av[n++] = "-h"; 79078300Sgad av[n++] = origin_host; 79131492Swollman av[n++] = pp->acct_file; 7921553Srgrimes av[n] = 0; 7931553Srgrimes fo = pfd; 79497793Sgad if (of_pid > 0) { /* stop output filter */ 7951553Srgrimes write(ofd, "\031\1", 2); 79697793Sgad while ((wpid = 79797793Sgad wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid) 7981553Srgrimes ; 79997793Sgad if (wpid < 0) 80097792Sgad syslog(LOG_WARNING, 80197792Sgad "%s: after stopping 'of', wait3() returned: %m", 80279735Sgad pp->printer); 80397781Sgad else if (!WIFSTOPPED(wstatus)) { 8041553Srgrimes (void) close(fi); 80597781Sgad syslog(LOG_WARNING, "%s: output filter died " 80697781Sgad "(pid=%d retcode=%d termsig=%d)", 80797793Sgad pp->printer, of_pid, WEXITSTATUS(wstatus), 80897781Sgad WTERMSIG(wstatus)); 80997791Sgad return (REPRINT); 8101553Srgrimes } 8111553Srgrimes stopped++; 8121553Srgrimes } 8131553Srgrimesstart: 81431492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 815118881Sgad dup2(fi, STDIN_FILENO); 816118881Sgad dup2(fo, STDOUT_FILENO); 81768664Sgad /* setup stderr for the filter (child process) 81868664Sgad * so it goes to our temporary errors file */ 81968664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 8201553Srgrimes if (n >= 0) 821118881Sgad dup2(n, STDERR_FILENO); 8228094Sjkh closelog(); 82331492Swollman closeallfds(3); 8241553Srgrimes execv(prog, av); 82595067Sgad syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, 82695067Sgad prog); 8271553Srgrimes exit(2); 8281553Srgrimes } 8291553Srgrimes (void) close(fi); 83097789Sgad wstatus_set = 0; 8311553Srgrimes if (child < 0) 83297781Sgad retcode = 100; 83379735Sgad else { 83497793Sgad while ((wpid = wait(&wstatus)) > 0 && wpid != child) 8351553Srgrimes ; 83697793Sgad if (wpid < 0) { 83797781Sgad retcode = 100; 83897792Sgad syslog(LOG_WARNING, 83997792Sgad "%s: after execv(%s), wait() returned: %m", 84079735Sgad pp->printer, prog); 84197789Sgad } else { 84297789Sgad wstatus_set = 1; 84397781Sgad retcode = WEXITSTATUS(wstatus); 84497789Sgad } 84579735Sgad } 8461553Srgrimes child = 0; 8471553Srgrimes prchild = 0; 8481553Srgrimes if (stopped) { /* restart output filter */ 84997793Sgad if (kill(of_pid, SIGCONT) < 0) { 8501553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 8511553Srgrimes exit(1); 8521553Srgrimes } 8531553Srgrimes } 85431492Swollman pp->tof = 0; 8551553Srgrimes 85668664Sgad /* Copy the filter's output to "lf" logfile */ 85768664Sgad if ((fp = fopen(tempstderr, "r"))) { 8581553Srgrimes while (fgets(buf, sizeof(buf), fp)) 8591553Srgrimes fputs(buf, stderr); 8601553Srgrimes fclose(fp); 8611553Srgrimes } 8621553Srgrimes 86397789Sgad if (wstatus_set && !WIFEXITED(wstatus)) { 86415648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 86597781Sgad pp->printer, format, WTERMSIG(wstatus)); 86697791Sgad return (ERROR); 8671553Srgrimes } 86897781Sgad switch (retcode) { 8691553Srgrimes case 0: 87031492Swollman pp->tof = 1; 87197791Sgad return (OK); 8721553Srgrimes case 1: 87397791Sgad return (REPRINT); 87415648Sjoerg case 2: 87597791Sgad return (ERROR); 8761553Srgrimes default: 87715648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 87897792Sgad pp->printer, format, retcode); 87997791Sgad return (FILTERERR); 8801553Srgrimes } 8811553Srgrimes} 8821553Srgrimes 8831553Srgrimes/* 8841553Srgrimes * Send the daemon control file (cf) and any data files. 8851553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 8861553Srgrimes * 0 if all is well. 8871553Srgrimes */ 8881553Srgrimesstatic int 88978146Sgadsendit(struct printer *pp, char *file) 8901553Srgrimes{ 89195293Sgad int dfcopies, err, i; 892119192Sgad char *cp, last[sizeof(line)]; 8931553Srgrimes 8941553Srgrimes /* 8951553Srgrimes * open control file 8961553Srgrimes */ 8971553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 89897791Sgad return (OK); 89968253Sgad 90068253Sgad /* initialize job-specific count of datafiles processed */ 90168253Sgad job_dfcnt = 0; 90268253Sgad 9031553Srgrimes /* 9041553Srgrimes * read the control file for work to do 9051553Srgrimes * 9061553Srgrimes * file format -- first character in the line is a command 9071553Srgrimes * rest of the line is the argument. 9081553Srgrimes * commands of interest are: 9091553Srgrimes * 9101553Srgrimes * a-z -- "file name" name of file to print 9111553Srgrimes * U -- "unlink" name of file to remove 9121553Srgrimes * (after we print it. (Pass 2 only)). 9131553Srgrimes */ 9141553Srgrimes 9151553Srgrimes /* 9161553Srgrimes * pass 1 9171553Srgrimes */ 91895293Sgad err = OK; 9191553Srgrimes while (getline(cfp)) { 9201553Srgrimes again: 9211553Srgrimes if (line[0] == 'S') { 9221553Srgrimes cp = line+1; 9231553Srgrimes i = 0; 9241553Srgrimes while (*cp >= '0' && *cp <= '9') 9251553Srgrimes i = i * 10 + (*cp++ - '0'); 9261553Srgrimes fdev = i; 9271553Srgrimes cp++; 9281553Srgrimes i = 0; 9291553Srgrimes while (*cp >= '0' && *cp <= '9') 9301553Srgrimes i = i * 10 + (*cp++ - '0'); 9311553Srgrimes fino = i; 93224831Sbrian } else if (line[0] == 'H') { 93378300Sgad strlcpy(origin_host, line + 1, sizeof(origin_host)); 93468343Sgad if (class[0] == '\0') { 93580133Sgad strlcpy(class, line + 1, sizeof(class)); 93668343Sgad } 93724831Sbrian } else if (line[0] == 'P') { 93880133Sgad strlcpy(logname, line + 1, sizeof(logname)); 93931492Swollman if (pp->restricted) { /* restricted */ 94024831Sbrian if (getpwnam(logname) == NULL) { 94131492Swollman sendmail(pp, line+1, NOACCT); 94224831Sbrian err = ERROR; 94324831Sbrian break; 94424831Sbrian } 94524831Sbrian } 94624831Sbrian } else if (line[0] == 'I') { 94780133Sgad strlcpy(indent+2, line + 1, sizeof(indent) - 2); 94824831Sbrian } else if (line[0] >= 'a' && line[0] <= 'z') { 94995293Sgad dfcopies = 1; 9501553Srgrimes strcpy(last, line); 95195293Sgad while ((i = getline(cfp)) != 0) { 95295293Sgad if (strcmp(last, line) != 0) 9531553Srgrimes break; 95495293Sgad dfcopies++; 95595293Sgad } 95695293Sgad switch (sendfile(pp, '\3', last+1, *last, dfcopies)) { 9571553Srgrimes case OK: 9581553Srgrimes if (i) 9591553Srgrimes goto again; 9601553Srgrimes break; 9611553Srgrimes case REPRINT: 9621553Srgrimes (void) fclose(cfp); 96397791Sgad return (REPRINT); 9641553Srgrimes case ACCESS: 96531492Swollman sendmail(pp, logname, ACCESS); 9661553Srgrimes case ERROR: 9671553Srgrimes err = ERROR; 9681553Srgrimes } 9691553Srgrimes break; 9701553Srgrimes } 9711553Srgrimes } 97295293Sgad if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) { 9731553Srgrimes (void) fclose(cfp); 97497791Sgad return (REPRINT); 9751553Srgrimes } 9761553Srgrimes /* 9771553Srgrimes * pass 2 9781553Srgrimes */ 9791553Srgrimes fseek(cfp, 0L, 0); 9801553Srgrimes while (getline(cfp)) 98127748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 9821553Srgrimes (void) unlink(line+1); 9831553Srgrimes /* 9841553Srgrimes * clean-up in case another control file exists 9851553Srgrimes */ 9861553Srgrimes (void) fclose(cfp); 9871553Srgrimes (void) unlink(file); 98897791Sgad return (err); 9891553Srgrimes} 9901553Srgrimes 9911553Srgrimes/* 9921553Srgrimes * Send a data file to the remote machine and spool it. 9931553Srgrimes * Return positive if we should try resending. 9941553Srgrimes */ 9951553Srgrimesstatic int 99695293Sgadsendfile(struct printer *pp, int type, char *file, char format, int copyreq) 9971553Srgrimes{ 99894036Sgad int i, amt; 9991553Srgrimes struct stat stb; 100094032Sgad char *av[15], *filtcmd; 1001119192Sgad char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4]; 100295293Sgad int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc; 10031553Srgrimes 1004138939Sgad /* Make sure the entire data file has arrived. */ 1005138939Sgad wait4data(pp, file); 1006138939Sgad 100774124Sgad statrc = lstat(file, &stb); 100874124Sgad if (statrc < 0) { 100974124Sgad syslog(LOG_ERR, "%s: error from lstat(%s): %m", 101074124Sgad pp->printer, file); 101194036Sgad return (ERROR); 101274124Sgad } 101394036Sgad sfd = open(file, O_RDONLY); 101494036Sgad if (sfd < 0) { 101574124Sgad syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m", 101674124Sgad pp->printer, file); 101794036Sgad return (ERROR); 101874124Sgad } 10191553Srgrimes /* 10201553Srgrimes * Check to see if data file is a symbolic link. If so, it should 10211553Srgrimes * still point to the same file or someone is trying to print something 10221553Srgrimes * he shouldn't. 10231553Srgrimes */ 102494036Sgad if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 && 102594036Sgad (stb.st_dev != fdev || stb.st_ino != fino)) { 102694036Sgad close(sfd); 102794036Sgad return (ACCESS); 102894036Sgad } 102924831Sbrian 103094032Sgad /* Everything seems OK for reading the file, now to send it */ 103194032Sgad filtcmd = NULL; 103224831Sbrian sizerr = 0; 103394032Sgad tfd = -1; 103424831Sbrian if (type == '\3') { 103594032Sgad /* 103694032Sgad * Type == 3 means this is a datafile, not a control file. 103794032Sgad * Increment the counter of data-files in this job, and 103894032Sgad * then check for input or output filters (which are only 103994032Sgad * applied to datafiles, not control files). 104094032Sgad */ 104194032Sgad job_dfcnt++; 104294032Sgad 104394032Sgad /* 104494032Sgad * Note that here we are filtering datafiles, one at a time, 104594032Sgad * as they are sent to the remote machine. Here, the *only* 104694032Sgad * difference between an input filter (`if=') and an output 104794032Sgad * filter (`of=') is the argument list that the filter is 104894032Sgad * started up with. Here, the output filter is executed 104994032Sgad * for each individual file as it is sent. This is not the 105094032Sgad * same as local print queues, where the output filter is 105194032Sgad * started up once, and then all jobs are passed thru that 105294032Sgad * single invocation of the output filter. 105394032Sgad * 105494032Sgad * Also note that a queue for a remote-machine can have an 105594032Sgad * input filter or an output filter, but not both. 105694032Sgad */ 105731492Swollman if (pp->filters[LPF_INPUT]) { 105894032Sgad filtcmd = pp->filters[LPF_INPUT]; 105994032Sgad av[0] = filtcmd; 106094032Sgad narg = 0; 106194032Sgad strcpy(opt_c, "-c"); 106294032Sgad strcpy(opt_h, "-h"); 106394032Sgad strcpy(opt_n, "-n"); 106424831Sbrian if (format == 'l') 106594032Sgad av[++narg] = opt_c; 106694032Sgad av[++narg] = width; 106794032Sgad av[++narg] = length; 106894032Sgad av[++narg] = indent; 106994032Sgad av[++narg] = opt_n; 107094032Sgad av[++narg] = logname; 107194032Sgad av[++narg] = opt_h; 107294032Sgad av[++narg] = origin_host; 107394032Sgad av[++narg] = pp->acct_file; 107494032Sgad av[++narg] = NULL; 107594032Sgad } else if (pp->filters[LPF_OUTPUT]) { 107694032Sgad filtcmd = pp->filters[LPF_OUTPUT]; 107794032Sgad av[0] = filtcmd; 107894032Sgad narg = 0; 107994032Sgad av[++narg] = width; 108094032Sgad av[++narg] = length; 108194032Sgad av[++narg] = NULL; 108224831Sbrian } 108324831Sbrian } 108494032Sgad if (filtcmd) { 108594032Sgad /* 108694032Sgad * If there is an input or output filter, we have to run 108794032Sgad * the datafile thru that filter and store the result as 108894032Sgad * a temporary spool file, because the protocol requires 108994032Sgad * that we send the remote host the file-size before we 109094032Sgad * start to send any of the data. 109194032Sgad */ 109294032Sgad strcpy(tfile, TFILENAME); 109394032Sgad tfd = mkstemp(tfile); 109494032Sgad if (tfd == -1) { 109594032Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 109694032Sgad TFILENAME); 109794036Sgad sfres = ERROR; 109894036Sgad goto return_sfres; 109994032Sgad } 110094036Sgad filtstat = execfilter(pp, filtcmd, av, sfd, tfd); 110124831Sbrian 110294032Sgad /* process the return-code from the filter */ 110394032Sgad switch (filtstat) { 110494032Sgad case 0: 110594032Sgad break; 110694032Sgad case 1: 110794036Sgad sfres = REPRINT; 110894036Sgad goto return_sfres; 110994032Sgad case 2: 111094036Sgad sfres = ERROR; 111194036Sgad goto return_sfres; 111294032Sgad default: 111394032Sgad syslog(LOG_WARNING, 111494032Sgad "%s: filter '%c' exited (retcode=%d)", 111594032Sgad pp->printer, format, filtstat); 111694036Sgad sfres = FILTERERR; 111794036Sgad goto return_sfres; 111894032Sgad } 111994032Sgad statrc = fstat(tfd, &stb); /* to find size of tfile */ 112094032Sgad if (statrc < 0) { 112194032Sgad syslog(LOG_ERR, 112294032Sgad "%s: error processing 'if', fstat(%s): %m", 112394032Sgad pp->printer, tfile); 112494036Sgad sfres = ERROR; 112594036Sgad goto return_sfres; 112694032Sgad } 112794036Sgad close(sfd); 112894036Sgad sfd = tfd; 112994036Sgad lseek(sfd, 0, SEEK_SET); 113094032Sgad } 113194032Sgad 113295293Sgad copycnt = 0; 113395293Sgadsendagain: 113495293Sgad copycnt++; 113595293Sgad 113695293Sgad if (copycnt < 2) 113795293Sgad (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 113895293Sgad else 113995293Sgad (void) sprintf(buf, "%c%qd %s_c%d\n", type, stb.st_size, 114095293Sgad file, copycnt); 11411553Srgrimes amt = strlen(buf); 11421553Srgrimes for (i = 0; ; i++) { 11431553Srgrimes if (write(pfd, buf, amt) != amt || 114431492Swollman (resp = response(pp)) < 0 || resp == '\1') { 114594036Sgad sfres = REPRINT; 114694036Sgad goto return_sfres; 11471553Srgrimes } else if (resp == '\0') 11481553Srgrimes break; 11491553Srgrimes if (i == 0) 115031492Swollman pstatus(pp, 115131492Swollman "no space on remote; waiting for queue to drain"); 11521553Srgrimes if (i == 10) 11531553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 115497792Sgad pp->printer, pp->remote_host); 11551553Srgrimes sleep(5 * 60); 11561553Srgrimes } 11571553Srgrimes if (i) 115831492Swollman pstatus(pp, "sending to %s", pp->remote_host); 115995293Sgad /* 116095293Sgad * XXX - we should change trstat_init()/trstat_write() to include 116195293Sgad * the copycnt in the statistics record it may write. 116295293Sgad */ 116368253Sgad if (type == '\3') 116468253Sgad trstat_init(pp, file, job_dfcnt); 1165119192Sgad for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) { 1166119192Sgad amt = SPL_BUFSIZ; 11671553Srgrimes if (i + amt > stb.st_size) 11681553Srgrimes amt = stb.st_size - i; 116994036Sgad if (sizerr == 0 && read(sfd, buf, amt) != amt) 11701553Srgrimes sizerr = 1; 11711553Srgrimes if (write(pfd, buf, amt) != amt) { 117294036Sgad sfres = REPRINT; 117394036Sgad goto return_sfres; 11741553Srgrimes } 11751553Srgrimes } 11761553Srgrimes 11771553Srgrimes if (sizerr) { 117831492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 11791553Srgrimes /* tell recvjob to ignore this file */ 11801553Srgrimes (void) write(pfd, "\1", 1); 118194036Sgad sfres = ERROR; 118294036Sgad goto return_sfres; 11831553Srgrimes } 118431492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 118594036Sgad sfres = REPRINT; 118694036Sgad goto return_sfres; 118724831Sbrian } 118895293Sgad if (type == '\3') { 118968253Sgad trstat_write(pp, TR_SENDING, stb.st_size, logname, 119095293Sgad pp->remote_host, origin_host); 119195293Sgad /* 119295293Sgad * Usually we only need to send one copy of a datafile, 119395293Sgad * because the control-file will simply print the same 119495293Sgad * file multiple times. However, some printers ignore 119595293Sgad * the control file, and simply print each data file as 119695293Sgad * it arrives. For such "remote hosts", we need to 119795293Sgad * transfer the same data file multiple times. Such a 119895293Sgad * a host is indicated by adding 'rc' to the printcap 119995293Sgad * entry. 120095293Sgad * XXX - Right now this ONLY works for remote hosts which 120195293Sgad * do ignore the name of the data file, because 120295293Sgad * this sends the file multiple times with slight 120395293Sgad * changes to the filename. To do this right would 120495293Sgad * require that we also rewrite the control file 120595293Sgad * to match those filenames. 120695293Sgad */ 120795293Sgad if (pp->resend_copies && (copycnt < copyreq)) { 120895293Sgad lseek(sfd, 0, SEEK_SET); 120995293Sgad goto sendagain; 121095293Sgad } 121195293Sgad } 121294036Sgad sfres = OK; 121394036Sgad 121494036Sgadreturn_sfres: 121594036Sgad (void)close(sfd); 121694036Sgad if (tfd != -1) { 121794036Sgad /* 121894036Sgad * If tfd is set, then it is the same value as sfd, and 121994036Sgad * therefore it is already closed at this point. All 122094036Sgad * we need to do is remove the temporary file. 122194036Sgad */ 122294036Sgad tfd = -1; 122394036Sgad unlink(tfile); 122494036Sgad } 122594036Sgad return (sfres); 12261553Srgrimes} 12271553Srgrimes 12281553Srgrimes/* 1229138939Sgad * Some print servers send the control-file first, and then start sending the 1230138939Sgad * matching data file(s). That is not the correct order. If some queue is 1231138939Sgad * already printing an active job, then when that job is finished the queue 1232138939Sgad * may proceed to the control file of any incoming print job. This turns 1233138939Sgad * into a race between the process which is receiving the data file, and the 1234138939Sgad * process which is actively printing the very same file. When the remote 1235138939Sgad * server sends files in the wrong order, it is even possible that a queue 1236138939Sgad * will start to print a data file before the file has been created! 1237138939Sgad * 1238138939Sgad * So before we start to print() or send() a data file, we call this routine 1239138939Sgad * to make sure the data file is not still changing in size. Note that this 1240138939Sgad * problem will only happen for jobs arriving from a remote host, and that 1241138939Sgad * the process which has decided to print this job (and is thus making this 1242138939Sgad * check) is *not* the process which is receiving the job. 1243138939Sgad * 1244138939Sgad * A second benefit of this is that any incoming job is guaranteed to appear 1245138939Sgad * in a queue listing for at least a few seconds after it has arrived. Some 1246138939Sgad * lpr implementations get confused if they send a job and it disappears 1247138939Sgad * from the queue before they can check on it. 1248138939Sgad */ 1249138939Sgad#define MAXWAIT_ARRIVE 16 /* max to wait for the file to *exist* */ 1250138939Sgad#define MAXWAIT_4DATA (20*60) /* max to wait for it to stop changing */ 1251138939Sgad#define MINWAIT_4DATA 4 /* This value must be >= 1 */ 1252138939Sgad#define DEBUG_MINWAIT 1 1253138939Sgadstatic void 1254138939Sgadwait4data(struct printer *pp, const char *dfile) 1255138939Sgad{ 1256138939Sgad const char *cp; 1257138939Sgad int statres; 1258138939Sgad size_t dlen, hlen; 1259138939Sgad time_t amtslept, checktime; 1260138939Sgad struct stat statdf; 1261138939Sgad 1262138939Sgad /* Skip these checks if the print job is from the local host. */ 1263138939Sgad dlen = strlen(dfile); 1264138939Sgad hlen = strlen(local_host); 1265138939Sgad if (dlen > hlen) { 1266138939Sgad cp = dfile + dlen - hlen; 1267138939Sgad if (strcmp(cp, local_host) == 0) 1268138939Sgad return; 1269138939Sgad } 1270138939Sgad 1271138939Sgad /* 1272138939Sgad * If this data file does not exist, then wait up to MAXWAIT_ARRIVE 1273138939Sgad * seconds for it to arrive. 1274138939Sgad */ 1275138939Sgad amtslept = 0; 1276138939Sgad statres = stat(dfile, &statdf); 1277138939Sgad while (statres < 0 && amtslept < MAXWAIT_ARRIVE) { 1278138939Sgad if (amtslept == 0) 1279138939Sgad pstatus(pp, "Waiting for data file from remote host"); 1280138939Sgad amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA); 1281138939Sgad statres = stat(dfile, &statdf); 1282138939Sgad } 1283138939Sgad if (statres < 0) { 1284138939Sgad /* The file still does not exist, so just give up on it. */ 1285138939Sgad syslog(LOG_WARNING, "%s: wait4data() abandoned wait for %s", 1286138939Sgad pp->printer, dfile); 1287138939Sgad return; 1288138939Sgad } 1289138939Sgad 1290138939Sgad /* 1291138939Sgad * The file exists, so keep waiting until the data file has not 1292138939Sgad * changed for some reasonable amount of time. 1293138939Sgad */ 1294138939Sgad while (statres == 0 && amtslept < MAXWAIT_4DATA) { 1295138939Sgad checktime = time(NULL) - MINWAIT_4DATA; 1296138939Sgad if (statdf.st_mtime <= checktime) 1297138939Sgad break; 1298138939Sgad if (amtslept == 0) 1299138939Sgad pstatus(pp, "Waiting for data file from remote host"); 1300138939Sgad amtslept += MINWAIT_4DATA - sleep(MINWAIT_4DATA); 1301138939Sgad statres = stat(dfile, &statdf); 1302138939Sgad } 1303138939Sgad 1304138939Sgad if (statres != 0) 1305138939Sgad syslog(LOG_WARNING, "%s: %s disappeared during wait4data()", 1306138939Sgad pp->printer, dfile); 1307138939Sgad else if (amtslept > MAXWAIT_4DATA) 1308138939Sgad syslog(LOG_WARNING, 1309138939Sgad "%s: %s still changing after %lu secs in wait4data()", 1310138939Sgad pp->printer, dfile, (unsigned long)amtslept); 1311138939Sgad#if DEBUG_MINWAIT 1312138939Sgad else if (amtslept > MINWAIT_4DATA) 1313138939Sgad syslog(LOG_INFO, "%s: slept %lu secs in wait4data(%s)", 1314138939Sgad pp->printer, (unsigned long)amtslept, dfile); 1315138939Sgad#endif 1316138939Sgad} 1317138939Sgad#undef MAXWAIT_ARRIVE 1318138939Sgad#undef MAXWAIT_4DATA 1319138939Sgad#undef MINWAIT_4DATA 1320138939Sgad 1321138939Sgad/* 132294032Sgad * This routine is called to execute one of the filters as was 132394036Sgad * specified in a printcap entry. While the child-process will read 132494036Sgad * all of 'infd', it is up to the caller to close that file descriptor 132594036Sgad * in the parent process. 132694032Sgad */ 132794032Sgadstatic int 132894032Sgadexecfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd) 132994032Sgad{ 133097793Sgad pid_t fpid, wpid; 133197793Sgad int errfd, retcode, wstatus; 133294032Sgad FILE *errfp; 133394032Sgad char buf[BUFSIZ], *slash; 133494032Sgad 133594032Sgad fpid = dofork(pp, DORETURN); 133694032Sgad if (fpid != 0) { 133794032Sgad /* 133894032Sgad * This is the parent process, which just waits for the child 133994032Sgad * to complete and then returns the result. Note that it is 134094032Sgad * the child process which reads the input stream. 134194032Sgad */ 134294032Sgad if (fpid < 0) 134397781Sgad retcode = 100; 134494032Sgad else { 134597781Sgad while ((wpid = wait(&wstatus)) > 0 && 134694032Sgad wpid != fpid) 134794032Sgad ; 134894032Sgad if (wpid < 0) { 134997781Sgad retcode = 100; 135094032Sgad syslog(LOG_WARNING, 135194032Sgad "%s: after execv(%s), wait() returned: %m", 135294032Sgad pp->printer, f_cmd); 135397781Sgad } else 135497781Sgad retcode = WEXITSTATUS(wstatus); 135594032Sgad } 135694032Sgad 135794032Sgad /* 135894032Sgad * Copy everything the filter wrote to stderr from our 135994032Sgad * temporary errors file to the "lf=" logfile. 136094032Sgad */ 136194032Sgad errfp = fopen(tempstderr, "r"); 136294032Sgad if (errfp) { 136394032Sgad while (fgets(buf, sizeof(buf), errfp)) 136494032Sgad fputs(buf, stderr); 136594032Sgad fclose(errfp); 136694032Sgad } 136794032Sgad 136897781Sgad return (retcode); 136994032Sgad } 137094032Sgad 137194032Sgad /* 137294032Sgad * This is the child process, which is the one that executes the 137394032Sgad * given filter. 137494032Sgad */ 137594032Sgad /* 137694032Sgad * If the first parameter has any slashes in it, then change it 137794032Sgad * to point to the first character after the last slash. 137894032Sgad */ 137994032Sgad slash = strrchr(f_av[0], '/'); 138094032Sgad if (slash != NULL) 138194032Sgad f_av[0] = slash + 1; 138294032Sgad /* 138394032Sgad * XXX - in the future, this should setup an explicit list of 138494032Sgad * environment variables and use execve()! 138594032Sgad */ 138694032Sgad 138794032Sgad /* 138894032Sgad * Setup stdin, stdout, and stderr as we want them when the filter 138994032Sgad * is running. Stderr is setup so it points to a temporary errors 139094032Sgad * file, and the parent process will copy that temporary file to 139194032Sgad * the real logfile after the filter completes. 139294032Sgad */ 1393118881Sgad dup2(infd, STDIN_FILENO); 1394118881Sgad dup2(outfd, STDOUT_FILENO); 139594032Sgad errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 139694032Sgad if (errfd >= 0) 1397118881Sgad dup2(errfd, STDERR_FILENO); 139894032Sgad closelog(); 139994032Sgad closeallfds(3); 140094032Sgad execv(f_cmd, f_av); 140195067Sgad syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd); 140294032Sgad exit(2); 140394032Sgad /* NOTREACHED */ 140494032Sgad} 140594032Sgad 140694032Sgad/* 14071553Srgrimes * Check to make sure there have been no errors and that both programs 14081553Srgrimes * are in sync with eachother. 14091553Srgrimes * Return non-zero if the connection was lost. 14101553Srgrimes */ 14111553Srgrimesstatic char 141278146Sgadresponse(const struct printer *pp) 14131553Srgrimes{ 14141553Srgrimes char resp; 14151553Srgrimes 14161553Srgrimes if (read(pfd, &resp, 1) != 1) { 141731492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 141897791Sgad return (-1); 14191553Srgrimes } 142097791Sgad return (resp); 14211553Srgrimes} 14221553Srgrimes 14231553Srgrimes/* 14241553Srgrimes * Banner printing stuff 14251553Srgrimes */ 14261553Srgrimesstatic void 142778146Sgadbanner(struct printer *pp, char *name1, char *name2) 14281553Srgrimes{ 14291553Srgrimes time_t tvec; 14301553Srgrimes 14311553Srgrimes time(&tvec); 143231492Swollman if (!pp->no_formfeed && !pp->tof) 143331492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 143431492Swollman if (pp->short_banner) { /* short banner only */ 14351553Srgrimes if (class[0]) { 14361553Srgrimes (void) write(ofd, class, strlen(class)); 14371553Srgrimes (void) write(ofd, ":", 1); 14381553Srgrimes } 14391553Srgrimes (void) write(ofd, name1, strlen(name1)); 14401553Srgrimes (void) write(ofd, " Job: ", 7); 14411553Srgrimes (void) write(ofd, name2, strlen(name2)); 14421553Srgrimes (void) write(ofd, " Date: ", 8); 14431553Srgrimes (void) write(ofd, ctime(&tvec), 24); 14441553Srgrimes (void) write(ofd, "\n", 1); 14451553Srgrimes } else { /* normal banner */ 14461553Srgrimes (void) write(ofd, "\n\n\n", 3); 144731492Swollman scan_out(pp, ofd, name1, '\0'); 14481553Srgrimes (void) write(ofd, "\n\n", 2); 144931492Swollman scan_out(pp, ofd, name2, '\0'); 14501553Srgrimes if (class[0]) { 14511553Srgrimes (void) write(ofd,"\n\n\n",3); 145231492Swollman scan_out(pp, ofd, class, '\0'); 14531553Srgrimes } 14541553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 14551553Srgrimes (void) write(ofd, name2, strlen(name2)); 14561553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 14571553Srgrimes (void) write(ofd, ctime(&tvec), 24); 14581553Srgrimes (void) write(ofd, "\n", 1); 14591553Srgrimes } 146031492Swollman if (!pp->no_formfeed) 146131492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 146231492Swollman pp->tof = 1; 14631553Srgrimes} 14641553Srgrimes 14651553Srgrimesstatic char * 146678146Sgadscnline(int key, char *p, int c) 14671553Srgrimes{ 146839084Swollman register int scnwidth; 14691553Srgrimes 14701553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 14711553Srgrimes key <<= 1; 14721553Srgrimes *p++ = key & 0200 ? c : BACKGND; 14731553Srgrimes } 14741553Srgrimes return (p); 14751553Srgrimes} 14761553Srgrimes 14771553Srgrimes#define TRC(q) (((q)-' ')&0177) 14781553Srgrimes 14791553Srgrimesstatic void 148078146Sgadscan_out(struct printer *pp, int scfd, char *scsp, int dlm) 14811553Srgrimes{ 14821553Srgrimes register char *strp; 148339084Swollman register int nchrs, j; 14841553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 14851553Srgrimes int d, scnhgt; 14861553Srgrimes 14871553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 14881553Srgrimes strp = &outbuf[0]; 14891553Srgrimes sp = scsp; 14901553Srgrimes for (nchrs = 0; ; ) { 14911553Srgrimes d = dropit(c = TRC(cc = *sp++)); 14921553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 14931553Srgrimes for (j = WIDTH; --j;) 14941553Srgrimes *strp++ = BACKGND; 14951553Srgrimes else 149631492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 149731492Swollman if (*sp == dlm || *sp == '\0' || 149831492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 14991553Srgrimes break; 15001553Srgrimes *strp++ = BACKGND; 15011553Srgrimes *strp++ = BACKGND; 15021553Srgrimes } 15031553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 15041553Srgrimes ; 15051553Srgrimes strp++; 15068857Srgrimes *strp++ = '\n'; 15071553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 15081553Srgrimes } 15091553Srgrimes} 15101553Srgrimes 15111553Srgrimesstatic int 151278146Sgaddropit(int c) 15131553Srgrimes{ 15141553Srgrimes switch(c) { 15151553Srgrimes 15161553Srgrimes case TRC('_'): 15171553Srgrimes case TRC(';'): 15181553Srgrimes case TRC(','): 15191553Srgrimes case TRC('g'): 15201553Srgrimes case TRC('j'): 15211553Srgrimes case TRC('p'): 15221553Srgrimes case TRC('q'): 15231553Srgrimes case TRC('y'): 15241553Srgrimes return (DROP); 15251553Srgrimes 15261553Srgrimes default: 15271553Srgrimes return (0); 15281553Srgrimes } 15291553Srgrimes} 15301553Srgrimes 15311553Srgrimes/* 15321553Srgrimes * sendmail --- 15331553Srgrimes * tell people about job completion 15341553Srgrimes */ 15351553Srgrimesstatic void 153694038Sgadsendmail(struct printer *pp, char *userid, int bombed) 15371553Srgrimes{ 15381553Srgrimes register int i; 15391553Srgrimes int p[2], s; 154078146Sgad register const char *cp; 15411553Srgrimes struct stat stb; 15421553Srgrimes FILE *fp; 15431553Srgrimes 15441553Srgrimes pipe(p); 154531492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 1546118881Sgad dup2(p[0], STDIN_FILENO); 15478094Sjkh closelog(); 154831492Swollman closeallfds(3); 154927635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 15501553Srgrimes cp++; 155131492Swollman else 15521553Srgrimes cp = _PATH_SENDMAIL; 155379452Sbrian execl(_PATH_SENDMAIL, cp, "-t", (char *)0); 155431492Swollman _exit(0); 15551553Srgrimes } else if (s > 0) { /* parent */ 1556118881Sgad dup2(p[1], STDOUT_FILENO); 155794038Sgad printf("To: %s@%s\n", userid, origin_host); 155831492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 155915648Sjoerg *jobname ? jobname : "<unknown>"); 156078300Sgad printf("Reply-To: root@%s\n\n", local_host); 15611553Srgrimes printf("Your printer job "); 15621553Srgrimes if (*jobname) 15631553Srgrimes printf("(%s) ", jobname); 156494040Sgad 15651553Srgrimes switch (bombed) { 15661553Srgrimes case OK: 156794040Sgad cp = "OK"; 15681553Srgrimes printf("\ncompleted successfully\n"); 15691553Srgrimes break; 15701553Srgrimes default: 15711553Srgrimes case FATALERR: 157294040Sgad cp = "FATALERR"; 15731553Srgrimes printf("\ncould not be printed\n"); 15741553Srgrimes break; 15751553Srgrimes case NOACCT: 157694040Sgad cp = "NOACCT"; 157778300Sgad printf("\ncould not be printed without an account on %s\n", 157878300Sgad local_host); 15791553Srgrimes break; 15801553Srgrimes case FILTERERR: 158194040Sgad cp = "FILTERERR"; 158268664Sgad if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 158368664Sgad || (fp = fopen(tempstderr, "r")) == NULL) { 158415648Sjoerg printf("\nhad some errors and may not have printed\n"); 15851553Srgrimes break; 15861553Srgrimes } 158715648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 15881553Srgrimes while ((i = getc(fp)) != EOF) 15891553Srgrimes putchar(i); 15901553Srgrimes (void) fclose(fp); 15911553Srgrimes break; 15921553Srgrimes case ACCESS: 159394040Sgad cp = "ACCESS"; 15941553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 15951553Srgrimes } 15961553Srgrimes fflush(stdout); 1597118881Sgad (void) close(STDOUT_FILENO); 159831492Swollman } else { 159994038Sgad syslog(LOG_WARNING, "unable to send mail to %s: %m", userid); 160031492Swollman return; 16011553Srgrimes } 16021553Srgrimes (void) close(p[0]); 16031553Srgrimes (void) close(p[1]); 160415648Sjoerg wait(NULL); 160515648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 160694038Sgad userid, *jobname ? jobname : "<unknown>", pp->printer, cp); 16071553Srgrimes} 16081553Srgrimes 16091553Srgrimes/* 16101553Srgrimes * dofork - fork with retries on failure 16111553Srgrimes */ 16121553Srgrimesstatic int 161378146Sgaddofork(const struct printer *pp, int action) 16141553Srgrimes{ 161597793Sgad pid_t forkpid; 161697793Sgad int i, fail; 161760871Smpp struct passwd *pwd; 16181553Srgrimes 161980230Sgad forkpid = -1; 162080230Sgad if (daemon_uname == NULL) { 162180230Sgad pwd = getpwuid(pp->daemon_user); 162280230Sgad if (pwd == NULL) { 162380230Sgad syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file", 162480230Sgad pp->printer, pp->daemon_user); 162580230Sgad goto error_ret; 162680230Sgad } 162780230Sgad daemon_uname = strdup(pwd->pw_name); 162880230Sgad daemon_defgid = pwd->pw_gid; 162980230Sgad } 163080230Sgad 16311553Srgrimes for (i = 0; i < 20; i++) { 163280230Sgad forkpid = fork(); 163380230Sgad if (forkpid < 0) { 16341553Srgrimes sleep((unsigned)(i*i)); 16351553Srgrimes continue; 16361553Srgrimes } 16371553Srgrimes /* 16381553Srgrimes * Child should run as daemon instead of root 16391553Srgrimes */ 164078146Sgad if (forkpid == 0) { 164180230Sgad errno = 0; 164280230Sgad fail = initgroups(daemon_uname, daemon_defgid); 164380230Sgad if (fail) { 164480230Sgad syslog(LOG_ERR, "%s: initgroups(%s,%u): %m", 164580230Sgad pp->printer, daemon_uname, daemon_defgid); 164660871Smpp break; 164760871Smpp } 164880230Sgad fail = setgid(daemon_defgid); 164980230Sgad if (fail) { 165080230Sgad syslog(LOG_ERR, "%s: setgid(%u): %m", 165180230Sgad pp->printer, daemon_defgid); 165280230Sgad break; 165380230Sgad } 165480230Sgad fail = setuid(pp->daemon_user); 165580230Sgad if (fail) { 165680230Sgad syslog(LOG_ERR, "%s: setuid(%ld): %m", 165780230Sgad pp->printer, pp->daemon_user); 165880230Sgad break; 165980230Sgad } 166060871Smpp } 166197791Sgad return (forkpid); 16621553Srgrimes } 16631553Srgrimes 166480230Sgad /* 166580230Sgad * An error occurred. If the error is in the child process, then 166680230Sgad * this routine MUST always exit(). DORETURN only effects how 166780230Sgad * errors should be handled in the parent process. 166880230Sgad */ 166980230Sgaderror_ret: 167080230Sgad if (forkpid == 0) { 167180230Sgad syslog(LOG_ERR, "%s: dofork(): aborting child process...", 167280230Sgad pp->printer); 167380230Sgad exit(1); 167480230Sgad } 167580230Sgad syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer); 167680230Sgad 167780230Sgad sleep(1); /* throttle errors, as a safety measure */ 16781553Srgrimes switch (action) { 16791553Srgrimes case DORETURN: 168097791Sgad return (-1); 16811553Srgrimes default: 16821553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 168380230Sgad /* FALLTHROUGH */ 16841553Srgrimes case DOABORT: 16851553Srgrimes exit(1); 16861553Srgrimes } 16871553Srgrimes /*NOTREACHED*/ 16881553Srgrimes} 16891553Srgrimes 16901553Srgrimes/* 16911553Srgrimes * Kill child processes to abort current job. 16921553Srgrimes */ 16931553Srgrimesstatic void 169478146Sgadabortpr(int signo __unused) 16951553Srgrimes{ 169668664Sgad 169768664Sgad (void) unlink(tempstderr); 16981553Srgrimes kill(0, SIGINT); 169997793Sgad if (of_pid > 0) 170097793Sgad kill(of_pid, SIGCONT); 17011553Srgrimes while (wait(NULL) > 0) 17021553Srgrimes ; 170397793Sgad if (of_pid > 0 && tfd != -1) 170424831Sbrian unlink(tfile); 17051553Srgrimes exit(0); 17061553Srgrimes} 17071553Srgrimes 17081553Srgrimesstatic void 170978146Sgadinit(struct printer *pp) 17101553Srgrimes{ 17111553Srgrimes char *s; 17121553Srgrimes 171331492Swollman sprintf(&width[2], "%ld", pp->page_width); 171431492Swollman sprintf(&length[2], "%ld", pp->page_length); 171531492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 171631492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 171731492Swollman if ((s = checkremote(pp)) != 0) { 171831492Swollman syslog(LOG_WARNING, "%s", s); 171931492Swollman free(s); 172031492Swollman } 172131492Swollman} 172231492Swollman 172331492Swollmanvoid 172478146Sgadstartprinting(const char *printer) 172531492Swollman{ 172631492Swollman struct printer myprinter, *pp = &myprinter; 172731492Swollman int status; 172831492Swollman 172931492Swollman init_printer(pp); 173031492Swollman status = getprintcap(printer, pp); 173131492Swollman switch(status) { 173231492Swollman case PCAPERR_OSERR: 173331492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 17341553Srgrimes exit(1); 173531492Swollman case PCAPERR_NOTFOUND: 17361553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 17371553Srgrimes exit(1); 173831492Swollman case PCAPERR_TCLOOP: 173931492Swollman fatal(pp, "potential reference loop detected in printcap file"); 174031492Swollman default: 174131492Swollman break; 174231492Swollman } 174331492Swollman printjob(pp); 17441553Srgrimes} 17451553Srgrimes 17461553Srgrimes/* 17471553Srgrimes * Acquire line printer or remote connection. 17481553Srgrimes */ 17491553Srgrimesstatic void 175078146Sgadopenpr(const struct printer *pp) 17511553Srgrimes{ 175231492Swollman int p[2]; 175315648Sjoerg char *cp; 17541553Srgrimes 175531492Swollman if (pp->remote) { 175631492Swollman openrem(pp); 175794032Sgad /* 175894032Sgad * Lpd does support the setting of 'of=' filters for 175994032Sgad * jobs going to remote machines, but that does not 176094032Sgad * have the same meaning as 'of=' does when handling 176194032Sgad * local print queues. For remote machines, all 'of=' 176294032Sgad * filter processing is handled in sendfile(), and that 176394032Sgad * does not use these global "output filter" variables. 176494032Sgad */ 176594032Sgad ofd = -1; 176697793Sgad of_pid = 0; 176794032Sgad return; 176831492Swollman } else if (*pp->lp) { 176931492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 177031492Swollman opennet(pp); 177115648Sjoerg else 177231492Swollman opentty(pp); 17731553Srgrimes } else { 17741553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 177597792Sgad pp->printer); 17761553Srgrimes exit(1); 17771553Srgrimes } 177815648Sjoerg 17791553Srgrimes /* 17801553Srgrimes * Start up an output filter, if needed. 17811553Srgrimes */ 178297793Sgad if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) { 17831553Srgrimes pipe(p); 178431492Swollman if (pp->remote) { 178531492Swollman strcpy(tfile, TFILENAME); 178624831Sbrian tfd = mkstemp(tfile); 178724831Sbrian } 178897793Sgad if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */ 1789118881Sgad dup2(p[0], STDIN_FILENO); /* pipe is std in */ 179024831Sbrian /* tfile/printer is stdout */ 1791118881Sgad dup2(pp->remote ? tfd : pfd, STDOUT_FILENO); 17928094Sjkh closelog(); 179331492Swollman closeallfds(3); 179431492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 179531492Swollman cp = pp->filters[LPF_OUTPUT]; 17961553Srgrimes else 17971553Srgrimes cp++; 179879452Sbrian execl(pp->filters[LPF_OUTPUT], cp, width, length, 179979452Sbrian (char *)0); 180097792Sgad syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer, 180197792Sgad pp->filters[LPF_OUTPUT]); 18021553Srgrimes exit(1); 18031553Srgrimes } 18041553Srgrimes (void) close(p[0]); /* close input side */ 18051553Srgrimes ofd = p[1]; /* use pipe for output */ 18061553Srgrimes } else { 18071553Srgrimes ofd = pfd; 180897793Sgad of_pid = 0; 18091553Srgrimes } 18101553Srgrimes} 18111553Srgrimes 181215648Sjoerg/* 181315648Sjoerg * Printer connected directly to the network 181415648Sjoerg * or to a terminal server on the net 181515648Sjoerg */ 181615648Sjoergstatic void 181778146Sgadopennet(const struct printer *pp) 181815648Sjoerg{ 181915648Sjoerg register int i; 182031492Swollman int resp; 182131492Swollman u_long port; 182231492Swollman char *ep; 182330407Sjoerg void (*savealrm)(int); 182415648Sjoerg 182531492Swollman port = strtoul(pp->lp, &ep, 0); 182638470Sbrian if (*ep != '@' || port > 65535) { 182731492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 182897792Sgad pp->lp); 182915648Sjoerg exit(1); 183015648Sjoerg } 183131492Swollman ep++; 183215648Sjoerg 183315648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 183415648Sjoerg resp = -1; 183530407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 183631492Swollman alarm(pp->conn_timeout); 183731492Swollman pfd = getport(pp, ep, port); 183831020Sjoerg alarm(0); 183930407Sjoerg (void)signal(SIGALRM, savealrm); 184015648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 184115648Sjoerg resp = 1; 184215648Sjoerg else if (pfd >= 0) { 184315648Sjoerg /* 184415648Sjoerg * need to delay a bit for rs232 lines 184515648Sjoerg * to stabilize in case printer is 184615648Sjoerg * connected via a terminal server 184715648Sjoerg */ 184815648Sjoerg delay(500); 184915648Sjoerg break; 185015648Sjoerg } 185115648Sjoerg if (i == 1) { 185231492Swollman if (resp < 0) 185331492Swollman pstatus(pp, "waiting for %s to come up", 185431492Swollman pp->lp); 185531492Swollman else 185631492Swollman pstatus(pp, 185731492Swollman "waiting for access to printer on %s", 185831492Swollman pp->lp); 185915648Sjoerg } 186015648Sjoerg sleep(i); 186115648Sjoerg } 186279739Sgad pstatus(pp, "sending to %s port %lu", ep, port); 186315648Sjoerg} 186415648Sjoerg 186515648Sjoerg/* 186615648Sjoerg * Printer is connected to an RS232 port on this host 186715648Sjoerg */ 186815648Sjoergstatic void 186978146Sgadopentty(const struct printer *pp) 187015648Sjoerg{ 187115648Sjoerg register int i; 187215648Sjoerg 187315648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 187431492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 187515648Sjoerg if (pfd >= 0) { 187615648Sjoerg delay(500); 187715648Sjoerg break; 187815648Sjoerg } 187915648Sjoerg if (errno == ENOENT) { 188031492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 188115648Sjoerg exit(1); 188215648Sjoerg } 188315648Sjoerg if (i == 1) 188431492Swollman pstatus(pp, 188531492Swollman "waiting for %s to become ready (offline?)", 188631492Swollman pp->printer); 188715648Sjoerg sleep(i); 188815648Sjoerg } 188915648Sjoerg if (isatty(pfd)) 189031492Swollman setty(pp); 189131492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 189215648Sjoerg} 189315648Sjoerg 189415648Sjoerg/* 189515648Sjoerg * Printer is on a remote host 189615648Sjoerg */ 189715648Sjoergstatic void 189878146Sgadopenrem(const struct printer *pp) 189915648Sjoerg{ 190031492Swollman register int i; 190127748Simp int resp; 190230407Sjoerg void (*savealrm)(int); 190315648Sjoerg 190415648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 190515648Sjoerg resp = -1; 190630407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 190731492Swollman alarm(pp->conn_timeout); 190831492Swollman pfd = getport(pp, pp->remote_host, 0); 190931020Sjoerg alarm(0); 191030407Sjoerg (void)signal(SIGALRM, savealrm); 191115648Sjoerg if (pfd >= 0) { 191231492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 191331492Swollman (char *)0) 191431492Swollman == 2 + strlen(pp->remote_queue)) 191531492Swollman && (resp = response(pp)) == 0) 191615648Sjoerg break; 191715648Sjoerg (void) close(pfd); 191815648Sjoerg } 191915648Sjoerg if (i == 1) { 192015648Sjoerg if (resp < 0) 192131492Swollman pstatus(pp, "waiting for %s to come up", 192231492Swollman pp->remote_host); 192315648Sjoerg else { 192431492Swollman pstatus(pp, 192531492Swollman "waiting for queue to be enabled on %s", 192631492Swollman pp->remote_host); 192715648Sjoerg i = 256; 192815648Sjoerg } 192915648Sjoerg } 193015648Sjoerg sleep(i); 193115648Sjoerg } 193231492Swollman pstatus(pp, "sending to %s", pp->remote_host); 193315648Sjoerg} 193415648Sjoerg 19351553Srgrimes/* 19361553Srgrimes * setup tty lines. 19371553Srgrimes */ 19381553Srgrimesstatic void 193978146Sgadsetty(const struct printer *pp) 19401553Srgrimes{ 194115032Ssef struct termios ttybuf; 19421553Srgrimes 19431553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 194431492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 19451553Srgrimes exit(1); 19461553Srgrimes } 194715032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 194831492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 19491553Srgrimes exit(1); 19501553Srgrimes } 195131492Swollman if (pp->baud_rate > 0) 195231492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 195331492Swollman if (pp->mode_set) { 195431492Swollman char *s = strdup(pp->mode_set), *tmp; 195515032Ssef 195631492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 195739084Swollman (void) msearch(tmp, &ttybuf); 19581553Srgrimes } 19591553Srgrimes } 196031492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 196115032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 196231492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 19631553Srgrimes } 19641553Srgrimes } 19651553Srgrimes} 19661553Srgrimes 19671553Srgrimes#include <stdarg.h> 19681553Srgrimes 196915648Sjoergstatic void 197031492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 19711553Srgrimes{ 197231492Swollman int fd; 197331492Swollman char *buf; 19741553Srgrimes va_list ap; 19751553Srgrimes va_start(ap, msg); 19761553Srgrimes 19771553Srgrimes umask(0); 197831492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 197931492Swollman if (fd < 0) { 198097792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 198197792Sgad pp->status_file); 19821553Srgrimes exit(1); 19831553Srgrimes } 19841553Srgrimes ftruncate(fd, 0); 198531492Swollman vasprintf(&buf, msg, ap); 19861553Srgrimes va_end(ap); 198731492Swollman writel(fd, buf, "\n", (char *)0); 198831492Swollman close(fd); 198931492Swollman free(buf); 19901553Srgrimes} 199130407Sjoerg 199230407Sjoergvoid 199378146Sgadalarmhandler(int signo __unused) 199430407Sjoerg{ 199578146Sgad /* the signal is ignored */ 199678146Sgad /* (the '__unused' is just to avoid a compile-time warning) */ 199730407Sjoerg} 1998