printjob.c revision 68664
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 68664 2000-11-13 05:44:50Z gad $"; 471553Srgrimes#endif /* not lint */ 481553Srgrimes 491553Srgrimes 501553Srgrimes/* 511553Srgrimes * printjob -- print jobs in the queue. 521553Srgrimes * 531553Srgrimes * NOTE: the lock file is used to pass information to lpq and lprm. 541553Srgrimes * it does not need to be removed because file locks are dynamic. 551553Srgrimes */ 561553Srgrimes 571553Srgrimes#include <sys/param.h> 581553Srgrimes#include <sys/wait.h> 591553Srgrimes#include <sys/stat.h> 601553Srgrimes#include <sys/types.h> 611553Srgrimes 621553Srgrimes#include <pwd.h> 631553Srgrimes#include <unistd.h> 641553Srgrimes#include <signal.h> 651553Srgrimes#include <syslog.h> 661553Srgrimes#include <fcntl.h> 671553Srgrimes#include <dirent.h> 681553Srgrimes#include <errno.h> 691553Srgrimes#include <stdio.h> 701553Srgrimes#include <string.h> 711553Srgrimes#include <stdlib.h> 7215032Ssef#include <sys/ioctl.h> 7315032Ssef#include <termios.h> 7415703Sjoerg#include <time.h> 751553Srgrimes#include "lp.h" 761553Srgrimes#include "lp.local.h" 771553Srgrimes#include "pathnames.h" 781553Srgrimes#include "extern.h" 791553Srgrimes 801553Srgrimes#define DORETURN 0 /* absorb fork error */ 811553Srgrimes#define DOABORT 1 /* abort if dofork fails */ 821553Srgrimes 831553Srgrimes/* 841553Srgrimes * Error tokens 851553Srgrimes */ 861553Srgrimes#define REPRINT -2 871553Srgrimes#define ERROR -1 881553Srgrimes#define OK 0 891553Srgrimes#define FATALERR 1 901553Srgrimes#define NOACCT 2 911553Srgrimes#define FILTERERR 3 921553Srgrimes#define ACCESS 4 931553Srgrimes 941553Srgrimesstatic dev_t fdev; /* device of file pointed to by symlink */ 951553Srgrimesstatic ino_t fino; /* inode of file pointed to by symlink */ 961553Srgrimesstatic FILE *cfp; /* control file */ 971553Srgrimesstatic int child; /* id of any filters */ 9868253Sgadstatic int job_dfcnt; /* count of datafiles in current user job */ 991553Srgrimesstatic int lfd; /* lock file descriptor */ 1001553Srgrimesstatic int ofd; /* output filter file descriptor */ 1011553Srgrimesstatic int ofilter; /* id of output filter, if any */ 10224831Sbrianstatic int tfd = -1; /* output filter temp file output */ 1031553Srgrimesstatic int pfd; /* prstatic inter file descriptor */ 1041553Srgrimesstatic int pid; /* pid of lpd process */ 1051553Srgrimesstatic int prchild; /* id of pr process */ 1061553Srgrimesstatic char title[80]; /* ``pr'' title */ 10753956Sachestatic char locale[80]; /* ``pr'' locale */ 1081553Srgrimes 1091553Srgrimesstatic char class[32]; /* classification field */ 11068343Sgadstatic char fromhost[MAXHOSTNAMELEN]; /* user's host machine */ 1111553Srgrimes /* indentation size in static characters */ 1128857Srgrimesstatic char indent[10] = "-i0"; 1131553Srgrimesstatic char jobname[100]; /* job or file name */ 1141553Srgrimesstatic char length[10] = "-l"; /* page length in lines */ 1151553Srgrimesstatic char logname[32]; /* user's login name */ 1161553Srgrimesstatic char pxlength[10] = "-y"; /* page length in pixels */ 1171553Srgrimesstatic char pxwidth[10] = "-x"; /* page width in pixels */ 11868664Sgad/* tempstderr is the filename used to catch stderr from exec-ing filters */ 11968664Sgadstatic char tempstderr[] = "errs.XXXXXXX"; 1201553Srgrimesstatic char width[10] = "-w"; /* page width in static characters */ 12124831Sbrian#define TFILENAME "fltXXXXXX" 12224831Sbrianstatic char tfile[] = TFILENAME; /* file name for filter output */ 1231553Srgrimes 1241553Srgrimesstatic void abortpr __P((int)); 12530407Sjoergstatic void alarmhandler __P((int)); 12631492Swollmanstatic void banner __P((struct printer *pp, char *name1, char *name2)); 12731492Swollmanstatic int dofork __P((const struct printer *pp, int action)); 1281553Srgrimesstatic int dropit __P((int)); 12931492Swollmanstatic void init __P((struct printer *pp)); 13031492Swollmanstatic void openpr __P((const struct printer *pp)); 13131492Swollmanstatic void opennet __P((const struct printer *pp)); 13231492Swollmanstatic void opentty __P((const struct printer *pp)); 13331492Swollmanstatic void openrem __P((const struct printer *pp)); 13431492Swollmanstatic int print __P((struct printer *pp, int format, char *file)); 13531492Swollmanstatic int printit __P((struct printer *pp, char *file)); 13631492Swollmanstatic void pstatus __P((const struct printer *, const char *, ...)); 13731492Swollmanstatic char response __P((const struct printer *pp)); 13831492Swollmanstatic void scan_out __P((struct printer *pp, int scfd, char *scsp, 13931492Swollman int dlm)); 1401553Srgrimesstatic char *scnline __P((int, char *, int)); 14131492Swollmanstatic int sendfile __P((struct printer *pp, int type, char *file, 14231492Swollman int format)); 14331492Swollmanstatic int sendit __P((struct printer *pp, char *file)); 14431492Swollmanstatic void sendmail __P((struct printer *pp, char *user, int bombed)); 14531492Swollmanstatic void setty __P((const struct printer *pp)); 1461553Srgrimes 1471553Srgrimesvoid 14831492Swollmanprintjob(pp) 14931492Swollman struct printer *pp; 1501553Srgrimes{ 1511553Srgrimes struct stat stb; 15268401Sgad register struct jobqueue *q, **qp; 15368401Sgad struct jobqueue **queue; 1541553Srgrimes register int i, nitems; 15568664Sgad off_t pidoff; 15668664Sgad int errcnt, count = 0; 15768664Sgad int tempfd; 1581553Srgrimes 15931492Swollman init(pp); /* set up capabilities */ 16031492Swollman (void) write(1, "", 1); /* ack that daemon is started */ 1611553Srgrimes (void) close(2); /* set up log file */ 16231492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { 16331492Swollman syslog(LOG_ERR, "%s: %m", pp->log_file); 1641553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1651553Srgrimes } 1661553Srgrimes setgid(getegid()); 1671553Srgrimes pid = getpid(); /* for use with lprm */ 1681553Srgrimes setpgrp(0, pid); 1691553Srgrimes signal(SIGHUP, abortpr); 1701553Srgrimes signal(SIGINT, abortpr); 1711553Srgrimes signal(SIGQUIT, abortpr); 1721553Srgrimes signal(SIGTERM, abortpr); 1731553Srgrimes 1741553Srgrimes /* 1751553Srgrimes * uses short form file names 1761553Srgrimes */ 17731492Swollman if (chdir(pp->spool_dir) < 0) { 17831492Swollman syslog(LOG_ERR, "%s: %m", pp->spool_dir); 1791553Srgrimes exit(1); 1801553Srgrimes } 18131492Swollman if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS)) 1821553Srgrimes exit(0); /* printing disabled */ 18331492Swollman lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 18431492Swollman LOCK_FILE_MODE); 1851553Srgrimes if (lfd < 0) { 18631492Swollman if (errno == EWOULDBLOCK) /* active daemon present */ 18731492Swollman exit(0); 18831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 1891553Srgrimes exit(1); 1901553Srgrimes } 19131492Swollman /* turn off non-blocking mode (was turned on for lock effects only) */ 19231492Swollman if (fcntl(lfd, F_SETFL, 0) < 0) { 19331492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 1941553Srgrimes exit(1); 1951553Srgrimes } 1961553Srgrimes ftruncate(lfd, 0); 1971553Srgrimes /* 1981553Srgrimes * write process id for others to know 1991553Srgrimes */ 2001553Srgrimes sprintf(line, "%u\n", pid); 2011553Srgrimes pidoff = i = strlen(line); 2021553Srgrimes if (write(lfd, line, i) != i) { 20331492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file); 2041553Srgrimes exit(1); 2051553Srgrimes } 2061553Srgrimes /* 2071553Srgrimes * search the spool directory for work and sort by queue order. 2081553Srgrimes */ 20931492Swollman if ((nitems = getq(pp, &queue)) < 0) { 21031492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 21131492Swollman pp->spool_dir); 2121553Srgrimes exit(1); 2131553Srgrimes } 2141553Srgrimes if (nitems == 0) /* no work to do */ 2151553Srgrimes exit(0); 21631492Swollman if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ 21731492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) 21831492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 21931492Swollman pp->lock_file); 2201553Srgrimes } 22168664Sgad 22268664Sgad /* create a file which will be used to hold stderr from filters */ 22368664Sgad if ((tempfd = mkstemp(tempstderr)) == -1) { 22468664Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 22568664Sgad tempstderr); 22668664Sgad exit(-1); 22768664Sgad } 22868664Sgad if ((i = fchmod(tempfd, 0664)) == -1) { 22968664Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 23068664Sgad tempstderr); 23168664Sgad exit(-1); 23268664Sgad } 23368664Sgad /* lpd doesn't need it to be open, it just needs it to exist */ 23468664Sgad close(tempfd); 23568664Sgad 23631492Swollman openpr(pp); /* open printer or remote */ 2371553Srgrimesagain: 2381553Srgrimes /* 2391553Srgrimes * we found something to do now do it -- 2401553Srgrimes * write the name of the current control file into the lock file 2411553Srgrimes * so the spool queue program can tell what we're working on 2421553Srgrimes */ 2431553Srgrimes for (qp = queue; nitems--; free((char *) q)) { 2441553Srgrimes q = *qp++; 24568401Sgad if (stat(q->job_cfname, &stb) < 0) 2461553Srgrimes continue; 24715648Sjoerg errcnt = 0; 2481553Srgrimes restart: 24915648Sjoerg (void) lseek(lfd, pidoff, 0); 25068401Sgad (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname); 2511553Srgrimes i = strlen(line); 2521553Srgrimes if (write(lfd, line, i) != i) 25331492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 25431492Swollman pp->lock_file); 25531492Swollman if (!pp->remote) 25668401Sgad i = printit(pp, q->job_cfname); 2571553Srgrimes else 25868401Sgad i = sendit(pp, q->job_cfname); 2591553Srgrimes /* 2601553Srgrimes * Check to see if we are supposed to stop printing or 2611553Srgrimes * if we are to rebuild the queue. 2621553Srgrimes */ 2631553Srgrimes if (fstat(lfd, &stb) == 0) { 2641553Srgrimes /* stop printing before starting next job? */ 26531492Swollman if (stb.st_mode & LFM_PRINT_DIS) 2661553Srgrimes goto done; 2671553Srgrimes /* rebuild queue (after lpc topq) */ 26831492Swollman if (stb.st_mode & LFM_RESET_QUE) { 26931492Swollman for (free(q); nitems--; free(q)) 2701553Srgrimes q = *qp++; 27131492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) 27231492Swollman < 0) 2731553Srgrimes syslog(LOG_WARNING, "%s: %s: %m", 27431492Swollman pp->printer, pp->lock_file); 2751553Srgrimes break; 2761553Srgrimes } 2771553Srgrimes } 2781553Srgrimes if (i == OK) /* file ok and printed */ 2791553Srgrimes count++; 28015648Sjoerg else if (i == REPRINT && ++errcnt < 5) { 28115648Sjoerg /* try reprinting the job */ 28231492Swollman syslog(LOG_INFO, "restarting %s", pp->printer); 2831553Srgrimes if (ofilter > 0) { 2841553Srgrimes kill(ofilter, SIGCONT); /* to be sure */ 2851553Srgrimes (void) close(ofd); 28615648Sjoerg while ((i = wait(NULL)) > 0 && i != ofilter) 2871553Srgrimes ; 2881553Srgrimes ofilter = 0; 2891553Srgrimes } 2901553Srgrimes (void) close(pfd); /* close printer */ 2911553Srgrimes if (ftruncate(lfd, pidoff) < 0) 29231492Swollman syslog(LOG_WARNING, "%s: %s: %m", 29331492Swollman pp->printer, pp->lock_file); 29431492Swollman openpr(pp); /* try to reopen printer */ 2951553Srgrimes goto restart; 29615648Sjoerg } else { 29731492Swollman syslog(LOG_WARNING, "%s: job could not be %s (%s)", 29831492Swollman pp->printer, 29931492Swollman pp->remote ? "sent to remote host" : "printed", 30068401Sgad q->job_cfname); 30115648Sjoerg if (i == REPRINT) { 30227748Simp /* ensure we don't attempt this job again */ 30368401Sgad (void) unlink(q->job_cfname); 30468401Sgad q->job_cfname[0] = 'd'; 30568401Sgad (void) unlink(q->job_cfname); 30615648Sjoerg if (logname[0]) 30731492Swollman sendmail(pp, logname, FATALERR); 30815648Sjoerg } 3091553Srgrimes } 3101553Srgrimes } 31131492Swollman free(queue); 3121553Srgrimes /* 3131553Srgrimes * search the spool directory for more work. 3141553Srgrimes */ 31531492Swollman if ((nitems = getq(pp, &queue)) < 0) { 31631492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 31731492Swollman pp->spool_dir); 3181553Srgrimes exit(1); 3191553Srgrimes } 3201553Srgrimes if (nitems == 0) { /* no more work to do */ 3211553Srgrimes done: 3221553Srgrimes if (count > 0) { /* Files actually printed */ 32331492Swollman if (!pp->no_formfeed && !pp->tof) 32431492Swollman (void) write(ofd, pp->form_feed, 32531492Swollman strlen(pp->form_feed)); 32631492Swollman if (pp->trailer != NULL) /* output trailer */ 32731492Swollman (void) write(ofd, pp->trailer, 32831492Swollman strlen(pp->trailer)); 3291553Srgrimes } 33019202Simp (void) close(ofd); 33119202Simp (void) wait(NULL); 33268664Sgad (void) unlink(tempstderr); 3331553Srgrimes exit(0); 3341553Srgrimes } 3351553Srgrimes goto again; 3361553Srgrimes} 3371553Srgrimes 3381553Srgrimeschar fonts[4][50]; /* fonts for troff */ 3391553Srgrimes 3401553Srgrimeschar ifonts[4][40] = { 3411553Srgrimes _PATH_VFONTR, 3421553Srgrimes _PATH_VFONTI, 3431553Srgrimes _PATH_VFONTB, 3441553Srgrimes _PATH_VFONTS, 3451553Srgrimes}; 3461553Srgrimes 3471553Srgrimes/* 3481553Srgrimes * The remaining part is the reading of the control file (cf) 3491553Srgrimes * and performing the various actions. 3501553Srgrimes */ 3511553Srgrimesstatic int 35231492Swollmanprintit(pp, file) 35331492Swollman struct printer *pp; 3541553Srgrimes char *file; 3551553Srgrimes{ 3561553Srgrimes register int i; 35768664Sgad char *cp; 35868664Sgad int bombed = OK; 3591553Srgrimes 3601553Srgrimes /* 3611553Srgrimes * open control file; ignore if no longer there. 3621553Srgrimes */ 3631553Srgrimes if ((cfp = fopen(file, "r")) == NULL) { 36431492Swollman syslog(LOG_INFO, "%s: %s: %m", pp->printer, file); 3651553Srgrimes return(OK); 3661553Srgrimes } 3671553Srgrimes /* 3681553Srgrimes * Reset troff fonts. 3691553Srgrimes */ 3701553Srgrimes for (i = 0; i < 4; i++) 3711553Srgrimes strcpy(fonts[i], ifonts[i]); 37231492Swollman sprintf(&width[2], "%ld", pp->page_width); 3731553Srgrimes strcpy(indent+2, "0"); 3741553Srgrimes 37568253Sgad /* initialize job-specific count of datafiles processed */ 37668253Sgad job_dfcnt = 0; 37768253Sgad 3781553Srgrimes /* 3791553Srgrimes * read the control file for work to do 3801553Srgrimes * 3811553Srgrimes * file format -- first character in the line is a command 3821553Srgrimes * rest of the line is the argument. 3831553Srgrimes * valid commands are: 3841553Srgrimes * 3851553Srgrimes * S -- "stat info" for symbolic link protection 3861553Srgrimes * J -- "job name" on banner page 3871553Srgrimes * C -- "class name" on banner page 3881553Srgrimes * L -- "literal" user's name to print on banner 3891553Srgrimes * T -- "title" for pr 3901553Srgrimes * H -- "host name" of machine where lpr was done 3911553Srgrimes * P -- "person" user's login name 3921553Srgrimes * I -- "indent" amount to indent output 39315648Sjoerg * R -- laser dpi "resolution" 3941553Srgrimes * f -- "file name" name of text file to print 3951553Srgrimes * l -- "file name" text file with control chars 3961553Srgrimes * p -- "file name" text file to print with pr(1) 3971553Srgrimes * t -- "file name" troff(1) file to print 3981553Srgrimes * n -- "file name" ditroff(1) file to print 3991553Srgrimes * d -- "file name" dvi file to print 4001553Srgrimes * g -- "file name" plot(1G) file to print 4011553Srgrimes * v -- "file name" plain raster file to print 4021553Srgrimes * c -- "file name" cifplot file to print 4031553Srgrimes * 1 -- "R font file" for troff 4041553Srgrimes * 2 -- "I font file" for troff 4051553Srgrimes * 3 -- "B font file" for troff 4061553Srgrimes * 4 -- "S font file" for troff 4071553Srgrimes * N -- "name" of file (used by lpq) 4081553Srgrimes * U -- "unlink" name of file to remove 4091553Srgrimes * (after we print it. (Pass 2 only)). 4101553Srgrimes * M -- "mail" to user when done printing 41153956Sache * Z -- "locale" for pr 4121553Srgrimes * 4131553Srgrimes * getline reads a line and expands tabs to blanks 4141553Srgrimes */ 4151553Srgrimes 4161553Srgrimes /* pass 1 */ 4171553Srgrimes 4181553Srgrimes while (getline(cfp)) 4191553Srgrimes switch (line[0]) { 4201553Srgrimes case 'H': 42127748Simp strncpy(fromhost, line+1, sizeof(fromhost) - 1); 42227748Simp fromhost[sizeof(fromhost) - 1] = '\0'; 42327748Simp if (class[0] == '\0') { 42427748Simp strncpy(class, line+1, sizeof(class) - 1); 42527748Simp class[sizeof(class) - 1] = '\0'; 42627748Simp } 4271553Srgrimes continue; 4281553Srgrimes 4291553Srgrimes case 'P': 43027748Simp strncpy(logname, line+1, sizeof(logname) - 1); 43127748Simp logname[sizeof(logname) - 1] = '\0'; 43231492Swollman if (pp->restricted) { /* restricted */ 4331553Srgrimes if (getpwnam(logname) == NULL) { 4341553Srgrimes bombed = NOACCT; 43531492Swollman sendmail(pp, line+1, bombed); 4361553Srgrimes goto pass2; 4371553Srgrimes } 4381553Srgrimes } 4391553Srgrimes continue; 4401553Srgrimes 4411553Srgrimes case 'S': 4421553Srgrimes cp = line+1; 4431553Srgrimes i = 0; 4441553Srgrimes while (*cp >= '0' && *cp <= '9') 4451553Srgrimes i = i * 10 + (*cp++ - '0'); 4461553Srgrimes fdev = i; 4471553Srgrimes cp++; 4481553Srgrimes i = 0; 4491553Srgrimes while (*cp >= '0' && *cp <= '9') 4501553Srgrimes i = i * 10 + (*cp++ - '0'); 4511553Srgrimes fino = i; 4521553Srgrimes continue; 4531553Srgrimes 4541553Srgrimes case 'J': 45527748Simp if (line[1] != '\0') { 45627748Simp strncpy(jobname, line+1, sizeof(jobname) - 1); 45727748Simp jobname[sizeof(jobname) - 1] = '\0'; 45827748Simp } else 4591553Srgrimes strcpy(jobname, " "); 4601553Srgrimes continue; 4611553Srgrimes 4621553Srgrimes case 'C': 4631553Srgrimes if (line[1] != '\0') 46427748Simp strncpy(class, line+1, sizeof(class) - 1); 4651553Srgrimes else if (class[0] == '\0') 4661553Srgrimes gethostname(class, sizeof(class)); 46727748Simp class[sizeof(class) - 1] = '\0'; 4681553Srgrimes continue; 4691553Srgrimes 4701553Srgrimes case 'T': /* header title for pr */ 47127748Simp strncpy(title, line+1, sizeof(title) - 1); 47227748Simp title[sizeof(title) - 1] = '\0'; 4731553Srgrimes continue; 4741553Srgrimes 4751553Srgrimes case 'L': /* identification line */ 47631492Swollman if (!pp->no_header && !pp->header_last) 47731492Swollman banner(pp, line+1, jobname); 4781553Srgrimes continue; 4791553Srgrimes 4801553Srgrimes case '1': /* troff fonts */ 4811553Srgrimes case '2': 4821553Srgrimes case '3': 4831553Srgrimes case '4': 48427748Simp if (line[1] != '\0') { 48527748Simp strncpy(fonts[line[0]-'1'], line+1, 48627748Simp 50-1); 48727748Simp fonts[line[0]-'1'][50-1] = '\0'; 48827748Simp } 4891553Srgrimes continue; 4901553Srgrimes 4911553Srgrimes case 'W': /* page width */ 49227748Simp strncpy(width+2, line+1, sizeof(width) - 3); 49327748Simp width[2+sizeof(width) - 3] = '\0'; 4941553Srgrimes continue; 4951553Srgrimes 4961553Srgrimes case 'I': /* indent amount */ 49727748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 49827748Simp indent[2+sizeof(indent) - 3] = '\0'; 4991553Srgrimes continue; 5001553Srgrimes 50153956Sache case 'Z': /* locale for pr */ 50253956Sache strncpy(locale, line+1, sizeof(locale) - 1); 50353956Sache locale[sizeof(locale) - 1] = '\0'; 50453956Sache continue; 50553956Sache 5061553Srgrimes default: /* some file to print */ 50768467Sgad /* only lowercase cmd-codes include a file-to-print */ 50868467Sgad if ((line[0] < 'a') || (line[0] > 'z')) { 50968467Sgad /* ignore any other lines */ 51068467Sgad if (lflag <= 1) 51168467Sgad continue; 51268467Sgad if (!didignorehdr) { 51368467Sgad syslog(LOG_INFO, "%s: in %s :", 51468467Sgad pp->printer, file); 51568467Sgad didignorehdr = 1; 51668467Sgad } 51768467Sgad syslog(LOG_INFO, "%s: ignoring line: '%c' %s", 51868467Sgad pp->printer, line[0], &line[1]); 51968467Sgad continue; 52068467Sgad } 52168467Sgad i = print(pp, line[0], line+1); 52268467Sgad switch (i) { 5231553Srgrimes case ERROR: 5241553Srgrimes if (bombed == OK) 5251553Srgrimes bombed = FATALERR; 5261553Srgrimes break; 5271553Srgrimes case REPRINT: 5281553Srgrimes (void) fclose(cfp); 5291553Srgrimes return(REPRINT); 5301553Srgrimes case FILTERERR: 5311553Srgrimes case ACCESS: 5321553Srgrimes bombed = i; 53331492Swollman sendmail(pp, logname, bombed); 5341553Srgrimes } 5351553Srgrimes title[0] = '\0'; 5361553Srgrimes continue; 5371553Srgrimes 5381553Srgrimes case 'N': 5391553Srgrimes case 'U': 5401553Srgrimes case 'M': 54115648Sjoerg case 'R': 5421553Srgrimes continue; 5431553Srgrimes } 5441553Srgrimes 5451553Srgrimes /* pass 2 */ 5461553Srgrimes 5471553Srgrimespass2: 5481553Srgrimes fseek(cfp, 0L, 0); 5491553Srgrimes while (getline(cfp)) 5501553Srgrimes switch (line[0]) { 5511553Srgrimes case 'L': /* identification line */ 55231492Swollman if (!pp->no_header && pp->header_last) 55331492Swollman banner(pp, line+1, jobname); 5541553Srgrimes continue; 5551553Srgrimes 5561553Srgrimes case 'M': 5571553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 55831492Swollman sendmail(pp, line+1, bombed); 5591553Srgrimes continue; 5601553Srgrimes 5611553Srgrimes case 'U': 56227748Simp if (strchr(line+1, '/')) 56327748Simp continue; 5641553Srgrimes (void) unlink(line+1); 5651553Srgrimes } 5661553Srgrimes /* 5671553Srgrimes * clean-up in case another control file exists 5681553Srgrimes */ 5691553Srgrimes (void) fclose(cfp); 5701553Srgrimes (void) unlink(file); 5711553Srgrimes return(bombed == OK ? OK : ERROR); 5721553Srgrimes} 5731553Srgrimes 5741553Srgrimes/* 5751553Srgrimes * Print a file. 5761553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 5771553Srgrimes * Return -1 if a non-recoverable error occured, 5781553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 5791553Srgrimes * 1 if we should try to reprint this job and 5801553Srgrimes * 0 if all is well. 5811553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 5821553Srgrimes * stderr as the log file, and must not ignore SIGINT. 5831553Srgrimes */ 5841553Srgrimesstatic int 58531492Swollmanprint(pp, format, file) 58631492Swollman struct printer *pp; 5871553Srgrimes int format; 5881553Srgrimes char *file; 5891553Srgrimes{ 59053956Sache register int n, i; 5911553Srgrimes register char *prog; 59231492Swollman int fi, fo; 5931553Srgrimes FILE *fp; 5941553Srgrimes char *av[15], buf[BUFSIZ]; 5951553Srgrimes int pid, p[2], stopped = 0; 5961553Srgrimes union wait status; 5971553Srgrimes struct stat stb; 5981553Srgrimes 59968467Sgad if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { 60068467Sgad syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", 60168467Sgad pp->printer, file, format); 6021553Srgrimes return(ERROR); 60368467Sgad } 6041553Srgrimes /* 6051553Srgrimes * Check to see if data file is a symbolic link. If so, it should 6061553Srgrimes * still point to the same file or someone is trying to print 6071553Srgrimes * something he shouldn't. 6081553Srgrimes */ 6091553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 6101553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 6111553Srgrimes return(ACCESS); 61268253Sgad 61368253Sgad job_dfcnt++; /* increment datafile counter for this job */ 61468253Sgad 61568253Sgad /* everything seems OK, start it up */ 61631492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 61731492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 61831492Swollman pp->tof = 1; 6191553Srgrimes } 62031492Swollman if (pp->filters[LPF_INPUT] == NULL 62131492Swollman && (format == 'f' || format == 'l')) { 62231492Swollman pp->tof = 0; 6231553Srgrimes while ((n = read(fi, buf, BUFSIZ)) > 0) 6241553Srgrimes if (write(ofd, buf, n) != n) { 6251553Srgrimes (void) close(fi); 6261553Srgrimes return(REPRINT); 6271553Srgrimes } 6281553Srgrimes (void) close(fi); 6291553Srgrimes return(OK); 6301553Srgrimes } 6311553Srgrimes switch (format) { 6321553Srgrimes case 'p': /* print file using 'pr' */ 63331492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 6341553Srgrimes prog = _PATH_PR; 63553956Sache i = 0; 63653956Sache av[i++] = "pr"; 63753956Sache av[i++] = width; 63853956Sache av[i++] = length; 63953956Sache av[i++] = "-h"; 64053956Sache av[i++] = *title ? title : " "; 64153956Sache av[i++] = "-L"; 64253956Sache av[i++] = *locale ? locale : "C"; 64353956Sache av[i++] = "-F"; 64453956Sache av[i] = 0; 6451553Srgrimes fo = ofd; 6461553Srgrimes goto start; 6471553Srgrimes } 6481553Srgrimes pipe(p); 64931492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 6501553Srgrimes dup2(fi, 0); /* file is stdin */ 6511553Srgrimes dup2(p[1], 1); /* pipe is stdout */ 6528094Sjkh closelog(); 65331492Swollman closeallfds(3); 6541553Srgrimes execl(_PATH_PR, "pr", width, length, 65553956Sache "-h", *title ? title : " ", 65653956Sache "-L", *locale ? locale : "C", 65753956Sache "-F", 0); 6581553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6591553Srgrimes exit(2); 6601553Srgrimes } 6611553Srgrimes (void) close(p[1]); /* close output side */ 6621553Srgrimes (void) close(fi); 6631553Srgrimes if (prchild < 0) { 6641553Srgrimes prchild = 0; 6651553Srgrimes (void) close(p[0]); 6661553Srgrimes return(ERROR); 6671553Srgrimes } 6681553Srgrimes fi = p[0]; /* use pipe for input */ 6691553Srgrimes case 'f': /* print plain text file */ 67031492Swollman prog = pp->filters[LPF_INPUT]; 6711553Srgrimes av[1] = width; 6721553Srgrimes av[2] = length; 6731553Srgrimes av[3] = indent; 6741553Srgrimes n = 4; 6751553Srgrimes break; 6761553Srgrimes case 'l': /* like 'f' but pass control characters */ 67731492Swollman prog = pp->filters[LPF_INPUT]; 6781553Srgrimes av[1] = "-c"; 6791553Srgrimes av[2] = width; 6801553Srgrimes av[3] = length; 6811553Srgrimes av[4] = indent; 6821553Srgrimes n = 5; 6831553Srgrimes break; 6841553Srgrimes case 'r': /* print a fortran text file */ 68531492Swollman prog = pp->filters[LPF_FORTRAN]; 6861553Srgrimes av[1] = width; 6871553Srgrimes av[2] = length; 6881553Srgrimes n = 3; 6891553Srgrimes break; 6901553Srgrimes case 't': /* print troff output */ 6911553Srgrimes case 'n': /* print ditroff output */ 6921553Srgrimes case 'd': /* print tex output */ 6931553Srgrimes (void) unlink(".railmag"); 6941553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 69531492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 69631492Swollman pp->printer); 6971553Srgrimes (void) unlink(".railmag"); 6981553Srgrimes } else { 6991553Srgrimes for (n = 0; n < 4; n++) { 7001553Srgrimes if (fonts[n][0] != '/') 7011553Srgrimes (void) write(fo, _PATH_VFONT, 7021553Srgrimes sizeof(_PATH_VFONT) - 1); 7031553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 7041553Srgrimes (void) write(fo, "\n", 1); 7051553Srgrimes } 7061553Srgrimes (void) close(fo); 7071553Srgrimes } 70831492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 70931492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 71031492Swollman : pp->filters[LPF_DVI]); 7111553Srgrimes av[1] = pxwidth; 7121553Srgrimes av[2] = pxlength; 7131553Srgrimes n = 3; 7141553Srgrimes break; 7151553Srgrimes case 'c': /* print cifplot output */ 71631492Swollman prog = pp->filters[LPF_CIFPLOT]; 7171553Srgrimes av[1] = pxwidth; 7181553Srgrimes av[2] = pxlength; 7191553Srgrimes n = 3; 7201553Srgrimes break; 7211553Srgrimes case 'g': /* print plot(1G) output */ 72231492Swollman prog = pp->filters[LPF_GRAPH]; 7231553Srgrimes av[1] = pxwidth; 7241553Srgrimes av[2] = pxlength; 7251553Srgrimes n = 3; 7261553Srgrimes break; 7271553Srgrimes case 'v': /* print raster output */ 72831492Swollman prog = pp->filters[LPF_RASTER]; 7291553Srgrimes av[1] = pxwidth; 7301553Srgrimes av[2] = pxlength; 7311553Srgrimes n = 3; 7321553Srgrimes break; 7331553Srgrimes default: 7341553Srgrimes (void) close(fi); 7351553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 73631492Swollman pp->printer, format); 7371553Srgrimes return(ERROR); 7381553Srgrimes } 73915648Sjoerg if (prog == NULL) { 74015648Sjoerg (void) close(fi); 74115648Sjoerg syslog(LOG_ERR, 74215648Sjoerg "%s: no filter found in printcap for format character '%c'", 74331492Swollman pp->printer, format); 74415648Sjoerg return(ERROR); 74515648Sjoerg } 74627635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 7471553Srgrimes av[0]++; 7481553Srgrimes else 7491553Srgrimes av[0] = prog; 7501553Srgrimes av[n++] = "-n"; 7511553Srgrimes av[n++] = logname; 7521553Srgrimes av[n++] = "-h"; 7531553Srgrimes av[n++] = fromhost; 75431492Swollman av[n++] = pp->acct_file; 7551553Srgrimes av[n] = 0; 7561553Srgrimes fo = pfd; 7571553Srgrimes if (ofilter > 0) { /* stop output filter */ 7581553Srgrimes write(ofd, "\031\1", 2); 7591553Srgrimes while ((pid = 7601553Srgrimes wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter) 7611553Srgrimes ; 7621553Srgrimes if (status.w_stopval != WSTOPPED) { 7631553Srgrimes (void) close(fi); 76415648Sjoerg syslog(LOG_WARNING, 76531492Swollman "%s: output filter died " 76631492Swollman "(retcode=%d termsig=%d)", 76731492Swollman pp->printer, status.w_retcode, 76831492Swollman status.w_termsig); 7691553Srgrimes return(REPRINT); 7701553Srgrimes } 7711553Srgrimes stopped++; 7721553Srgrimes } 7731553Srgrimesstart: 77431492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 7751553Srgrimes dup2(fi, 0); 7761553Srgrimes dup2(fo, 1); 77768664Sgad /* setup stderr for the filter (child process) 77868664Sgad * so it goes to our temporary errors file */ 77968664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 7801553Srgrimes if (n >= 0) 7811553Srgrimes dup2(n, 2); 7828094Sjkh closelog(); 78331492Swollman closeallfds(3); 7841553Srgrimes execv(prog, av); 7851553Srgrimes syslog(LOG_ERR, "cannot execv %s", prog); 7861553Srgrimes exit(2); 7871553Srgrimes } 7881553Srgrimes (void) close(fi); 7891553Srgrimes if (child < 0) 7901553Srgrimes status.w_retcode = 100; 7911553Srgrimes else 7921553Srgrimes while ((pid = wait((int *)&status)) > 0 && pid != child) 7931553Srgrimes ; 7941553Srgrimes child = 0; 7951553Srgrimes prchild = 0; 7961553Srgrimes if (stopped) { /* restart output filter */ 7971553Srgrimes if (kill(ofilter, SIGCONT) < 0) { 7981553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 7991553Srgrimes exit(1); 8001553Srgrimes } 8011553Srgrimes } 80231492Swollman pp->tof = 0; 8031553Srgrimes 80468664Sgad /* Copy the filter's output to "lf" logfile */ 80568664Sgad if ((fp = fopen(tempstderr, "r"))) { 8061553Srgrimes while (fgets(buf, sizeof(buf), fp)) 8071553Srgrimes fputs(buf, stderr); 8081553Srgrimes fclose(fp); 8091553Srgrimes } 8101553Srgrimes 8111553Srgrimes if (!WIFEXITED(status)) { 81215648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 81331492Swollman pp->printer, format, status.w_termsig); 8141553Srgrimes return(ERROR); 8151553Srgrimes } 8161553Srgrimes switch (status.w_retcode) { 8171553Srgrimes case 0: 81831492Swollman pp->tof = 1; 8191553Srgrimes return(OK); 8201553Srgrimes case 1: 8211553Srgrimes return(REPRINT); 82215648Sjoerg case 2: 82315648Sjoerg return(ERROR); 8241553Srgrimes default: 82515648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 82631492Swollman pp->printer, format, status.w_retcode); 82715648Sjoerg return(FILTERERR); 8281553Srgrimes } 8291553Srgrimes} 8301553Srgrimes 8311553Srgrimes/* 8321553Srgrimes * Send the daemon control file (cf) and any data files. 8331553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 8341553Srgrimes * 0 if all is well. 8351553Srgrimes */ 8361553Srgrimesstatic int 83731492Swollmansendit(pp, file) 83831492Swollman struct printer *pp; 8391553Srgrimes char *file; 8401553Srgrimes{ 8411553Srgrimes register int i, err = OK; 8421553Srgrimes char *cp, last[BUFSIZ]; 8431553Srgrimes 8441553Srgrimes /* 8451553Srgrimes * open control file 8461553Srgrimes */ 8471553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 8481553Srgrimes return(OK); 84968253Sgad 85068253Sgad /* initialize job-specific count of datafiles processed */ 85168253Sgad job_dfcnt = 0; 85268253Sgad 8531553Srgrimes /* 8541553Srgrimes * read the control file for work to do 8551553Srgrimes * 8561553Srgrimes * file format -- first character in the line is a command 8571553Srgrimes * rest of the line is the argument. 8581553Srgrimes * commands of interest are: 8591553Srgrimes * 8601553Srgrimes * a-z -- "file name" name of file to print 8611553Srgrimes * U -- "unlink" name of file to remove 8621553Srgrimes * (after we print it. (Pass 2 only)). 8631553Srgrimes */ 8641553Srgrimes 8651553Srgrimes /* 8661553Srgrimes * pass 1 8671553Srgrimes */ 8681553Srgrimes while (getline(cfp)) { 8691553Srgrimes again: 8701553Srgrimes if (line[0] == 'S') { 8711553Srgrimes cp = line+1; 8721553Srgrimes i = 0; 8731553Srgrimes while (*cp >= '0' && *cp <= '9') 8741553Srgrimes i = i * 10 + (*cp++ - '0'); 8751553Srgrimes fdev = i; 8761553Srgrimes cp++; 8771553Srgrimes i = 0; 8781553Srgrimes while (*cp >= '0' && *cp <= '9') 8791553Srgrimes i = i * 10 + (*cp++ - '0'); 8801553Srgrimes fino = i; 88124831Sbrian } else if (line[0] == 'H') { 88268343Sgad strncpy(fromhost, line+1, sizeof(fromhost) - 1); 88368343Sgad fromhost[sizeof(fromhost) - 1] = '\0'; 88468343Sgad if (class[0] == '\0') { 88527748Simp strncpy(class, line+1, sizeof(class) - 1); 88668343Sgad class[sizeof(class) - 1] = '\0'; 88768343Sgad } 88824831Sbrian } else if (line[0] == 'P') { 88927748Simp strncpy(logname, line+1, sizeof(logname) - 1); 89031492Swollman if (pp->restricted) { /* restricted */ 89124831Sbrian if (getpwnam(logname) == NULL) { 89231492Swollman sendmail(pp, line+1, NOACCT); 89324831Sbrian err = ERROR; 89424831Sbrian break; 89524831Sbrian } 89624831Sbrian } 89724831Sbrian } else if (line[0] == 'I') { 89827748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 89924831Sbrian } else if (line[0] >= 'a' && line[0] <= 'z') { 9001553Srgrimes strcpy(last, line); 90131492Swollman while ((i = getline(cfp)) != 0) 9021553Srgrimes if (strcmp(last, line)) 9031553Srgrimes break; 90431492Swollman switch (sendfile(pp, '\3', last+1, *last)) { 9051553Srgrimes case OK: 9061553Srgrimes if (i) 9071553Srgrimes goto again; 9081553Srgrimes break; 9091553Srgrimes case REPRINT: 9101553Srgrimes (void) fclose(cfp); 9111553Srgrimes return(REPRINT); 9121553Srgrimes case ACCESS: 91331492Swollman sendmail(pp, logname, ACCESS); 9141553Srgrimes case ERROR: 9151553Srgrimes err = ERROR; 9161553Srgrimes } 9171553Srgrimes break; 9181553Srgrimes } 9191553Srgrimes } 92031492Swollman if (err == OK && sendfile(pp, '\2', file, '\0') > 0) { 9211553Srgrimes (void) fclose(cfp); 9221553Srgrimes return(REPRINT); 9231553Srgrimes } 9241553Srgrimes /* 9251553Srgrimes * pass 2 9261553Srgrimes */ 9271553Srgrimes fseek(cfp, 0L, 0); 9281553Srgrimes while (getline(cfp)) 92927748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 9301553Srgrimes (void) unlink(line+1); 9311553Srgrimes /* 9321553Srgrimes * clean-up in case another control file exists 9331553Srgrimes */ 9341553Srgrimes (void) fclose(cfp); 9351553Srgrimes (void) unlink(file); 9361553Srgrimes return(err); 9371553Srgrimes} 9381553Srgrimes 9391553Srgrimes/* 9401553Srgrimes * Send a data file to the remote machine and spool it. 9411553Srgrimes * Return positive if we should try resending. 9421553Srgrimes */ 9431553Srgrimesstatic int 94431492Swollmansendfile(pp, type, file, format) 94531492Swollman struct printer *pp; 9461553Srgrimes int type; 9471553Srgrimes char *file; 94824831Sbrian char format; 9491553Srgrimes{ 9501553Srgrimes register int f, i, amt; 9511553Srgrimes struct stat stb; 95268664Sgad FILE *fp; 9531553Srgrimes char buf[BUFSIZ]; 95424831Sbrian int sizerr, resp, closedpr; 9551553Srgrimes 9561553Srgrimes if (lstat(file, &stb) < 0 || (f = open(file, O_RDONLY)) < 0) 9571553Srgrimes return(ERROR); 9581553Srgrimes /* 9591553Srgrimes * Check to see if data file is a symbolic link. If so, it should 9601553Srgrimes * still point to the same file or someone is trying to print something 9611553Srgrimes * he shouldn't. 9621553Srgrimes */ 9631553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 && 9641553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 9651553Srgrimes return(ACCESS); 96624831Sbrian 96768253Sgad job_dfcnt++; /* increment datafile counter for this job */ 96868253Sgad 96968253Sgad /* everything seems OK, start it up */ 97024831Sbrian sizerr = 0; 97124831Sbrian closedpr = 0; 97224831Sbrian if (type == '\3') { 97331492Swollman if (pp->filters[LPF_INPUT]) { 97424831Sbrian /* 97524831Sbrian * We're sending something with an ifilter, we have to 97624831Sbrian * run the ifilter and store the output as a 97724831Sbrian * temporary file (tfile)... the protocol requires us 97824831Sbrian * to send the file size 97924831Sbrian */ 98024831Sbrian char *av[15]; 98124831Sbrian int n; 98224831Sbrian int ifilter; 98331492Swollman union wait status; /* XXX */ 98424831Sbrian 98524831Sbrian strcpy(tfile,TFILENAME); 98624831Sbrian if ((tfd = mkstemp(tfile)) == -1) { 98724831Sbrian syslog(LOG_ERR, "mkstemp: %m"); 98824831Sbrian return(ERROR); 98924831Sbrian } 99031492Swollman if ((av[0] = strrchr(pp->filters[LPF_INPUT], '/')) == NULL) 99131492Swollman av[0] = pp->filters[LPF_INPUT]; 99224831Sbrian else 99324831Sbrian av[0]++; 99424831Sbrian if (format == 'l') 99524831Sbrian av[n=1] = "-c"; 99624831Sbrian else 99724831Sbrian n = 0; 99824831Sbrian av[++n] = width; 99924831Sbrian av[++n] = length; 100024831Sbrian av[++n] = indent; 100124831Sbrian av[++n] = "-n"; 100224831Sbrian av[++n] = logname; 100324831Sbrian av[++n] = "-h"; 100424831Sbrian av[++n] = fromhost; 100531492Swollman av[++n] = pp->acct_file; 100624831Sbrian av[++n] = 0; 100731492Swollman if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */ 100824831Sbrian dup2(f, 0); 100924831Sbrian dup2(tfd, 1); 101068664Sgad /* setup stderr for the filter (child process) 101168664Sgad * so it goes to our temporary errors file */ 101268664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 101324831Sbrian if (n >= 0) 101424831Sbrian dup2(n, 2); 101524831Sbrian closelog(); 101631492Swollman closeallfds(3); 101731492Swollman execv(pp->filters[LPF_INPUT], av); 101831492Swollman syslog(LOG_ERR, "cannot execv %s", 101931492Swollman pp->filters[LPF_INPUT]); 102024831Sbrian exit(2); 102124831Sbrian } 102224831Sbrian (void) close(f); 102324831Sbrian if (ifilter < 0) 102424831Sbrian status.w_retcode = 100; 102524831Sbrian else 102624831Sbrian while ((pid = wait((int *)&status)) > 0 && 102724831Sbrian pid != ifilter) 102824831Sbrian ; 102968664Sgad /* Copy the filter's output to "lf" logfile */ 103068664Sgad if ((fp = fopen(tempstderr, "r"))) { 103168664Sgad while (fgets(buf, sizeof(buf), fp)) 103268664Sgad fputs(buf, stderr); 103368664Sgad fclose(fp); 103468664Sgad } 103568664Sgad /* process the return-code from the filter */ 103624831Sbrian switch (status.w_retcode) { 103724831Sbrian case 0: 103824831Sbrian break; 103924831Sbrian case 1: 104024831Sbrian unlink(tfile); 104124831Sbrian return(REPRINT); 104224831Sbrian case 2: 104324831Sbrian unlink(tfile); 104424831Sbrian return(ERROR); 104524831Sbrian default: 104624831Sbrian syslog(LOG_WARNING, "%s: filter '%c' exited" 104724831Sbrian " (retcode=%d)", 104831492Swollman pp->printer, format, status.w_retcode); 104924831Sbrian unlink(tfile); 105024831Sbrian return(FILTERERR); 105124831Sbrian } 105224831Sbrian if (fstat(tfd, &stb) < 0) /* the size of tfile */ 105324831Sbrian return(ERROR); 105424831Sbrian f = tfd; 105524831Sbrian lseek(f,0,SEEK_SET); 105624831Sbrian } else if (ofilter) { 105724831Sbrian /* 105824831Sbrian * We're sending something with an ofilter, we have to 105924831Sbrian * store the output as a temporary file (tfile)... the 106024831Sbrian * protocol requires us to send the file size 106124831Sbrian */ 106224831Sbrian int i; 106324831Sbrian for (i = 0; i < stb.st_size; i += BUFSIZ) { 106424831Sbrian amt = BUFSIZ; 106524831Sbrian if (i + amt > stb.st_size) 106624831Sbrian amt = stb.st_size - i; 106724831Sbrian if (sizerr == 0 && read(f, buf, amt) != amt) { 106824831Sbrian sizerr = 1; 106924831Sbrian break; 107024831Sbrian } 107124831Sbrian if (write(ofd, buf, amt) != amt) { 107224831Sbrian (void) close(f); 107324831Sbrian return(REPRINT); 107424831Sbrian } 107524831Sbrian } 107624831Sbrian close(ofd); 107724831Sbrian close(f); 107824831Sbrian while ((i = wait(NULL)) > 0 && i != ofilter) 107924831Sbrian ; 108024831Sbrian ofilter = 0; 108124831Sbrian if (fstat(tfd, &stb) < 0) { /* the size of tfile */ 108231492Swollman openpr(pp); 108324831Sbrian return(ERROR); 108424831Sbrian } 108524831Sbrian f = tfd; 108624831Sbrian lseek(f,0,SEEK_SET); 108724831Sbrian closedpr = 1; 108824831Sbrian } 108924831Sbrian } 109024831Sbrian 10911553Srgrimes (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 10921553Srgrimes amt = strlen(buf); 10931553Srgrimes for (i = 0; ; i++) { 10941553Srgrimes if (write(pfd, buf, amt) != amt || 109531492Swollman (resp = response(pp)) < 0 || resp == '\1') { 10961553Srgrimes (void) close(f); 109724831Sbrian if (tfd != -1 && type == '\3') { 109824831Sbrian tfd = -1; 109924831Sbrian unlink(tfile); 110024831Sbrian if (closedpr) 110131492Swollman openpr(pp); 110224831Sbrian } 11031553Srgrimes return(REPRINT); 11041553Srgrimes } else if (resp == '\0') 11051553Srgrimes break; 11061553Srgrimes if (i == 0) 110731492Swollman pstatus(pp, 110831492Swollman "no space on remote; waiting for queue to drain"); 11091553Srgrimes if (i == 10) 11101553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 111131492Swollman pp->printer, pp->remote_host); 11121553Srgrimes sleep(5 * 60); 11131553Srgrimes } 11141553Srgrimes if (i) 111531492Swollman pstatus(pp, "sending to %s", pp->remote_host); 111668253Sgad if (type == '\3') 111768253Sgad trstat_init(pp, file, job_dfcnt); 11181553Srgrimes for (i = 0; i < stb.st_size; i += BUFSIZ) { 11191553Srgrimes amt = BUFSIZ; 11201553Srgrimes if (i + amt > stb.st_size) 11211553Srgrimes amt = stb.st_size - i; 11221553Srgrimes if (sizerr == 0 && read(f, buf, amt) != amt) 11231553Srgrimes sizerr = 1; 11241553Srgrimes if (write(pfd, buf, amt) != amt) { 11251553Srgrimes (void) close(f); 112624831Sbrian if (tfd != -1 && type == '\3') { 112724831Sbrian tfd = -1; 112824831Sbrian unlink(tfile); 112924831Sbrian if (closedpr) 113031492Swollman openpr(pp); 113124831Sbrian } 11321553Srgrimes return(REPRINT); 11331553Srgrimes } 11341553Srgrimes } 11351553Srgrimes 11361553Srgrimes (void) close(f); 113724831Sbrian if (tfd != -1 && type == '\3') { 113824831Sbrian tfd = -1; 113924831Sbrian unlink(tfile); 114024831Sbrian } 11411553Srgrimes if (sizerr) { 114231492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 11431553Srgrimes /* tell recvjob to ignore this file */ 11441553Srgrimes (void) write(pfd, "\1", 1); 114524831Sbrian if (closedpr) 114631492Swollman openpr(pp); 11471553Srgrimes return(ERROR); 11481553Srgrimes } 114931492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 115024831Sbrian if (closedpr) 115131492Swollman openpr(pp); 11521553Srgrimes return(REPRINT); 115324831Sbrian } 115424831Sbrian if (closedpr) 115531492Swollman openpr(pp); 115668253Sgad if (type == '\3') 115768253Sgad trstat_write(pp, TR_SENDING, stb.st_size, logname, 115868253Sgad pp->remote_host, fromhost); 11591553Srgrimes return(OK); 11601553Srgrimes} 11611553Srgrimes 11621553Srgrimes/* 11631553Srgrimes * Check to make sure there have been no errors and that both programs 11641553Srgrimes * are in sync with eachother. 11651553Srgrimes * Return non-zero if the connection was lost. 11661553Srgrimes */ 11671553Srgrimesstatic char 116831492Swollmanresponse(pp) 116931492Swollman const struct printer *pp; 11701553Srgrimes{ 11711553Srgrimes char resp; 11721553Srgrimes 11731553Srgrimes if (read(pfd, &resp, 1) != 1) { 117431492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 11751553Srgrimes return(-1); 11761553Srgrimes } 11771553Srgrimes return(resp); 11781553Srgrimes} 11791553Srgrimes 11801553Srgrimes/* 11811553Srgrimes * Banner printing stuff 11821553Srgrimes */ 11831553Srgrimesstatic void 118431492Swollmanbanner(pp, name1, name2) 118531492Swollman struct printer *pp; 11861553Srgrimes char *name1, *name2; 11871553Srgrimes{ 11881553Srgrimes time_t tvec; 11891553Srgrimes 11901553Srgrimes time(&tvec); 119131492Swollman if (!pp->no_formfeed && !pp->tof) 119231492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 119331492Swollman if (pp->short_banner) { /* short banner only */ 11941553Srgrimes if (class[0]) { 11951553Srgrimes (void) write(ofd, class, strlen(class)); 11961553Srgrimes (void) write(ofd, ":", 1); 11971553Srgrimes } 11981553Srgrimes (void) write(ofd, name1, strlen(name1)); 11991553Srgrimes (void) write(ofd, " Job: ", 7); 12001553Srgrimes (void) write(ofd, name2, strlen(name2)); 12011553Srgrimes (void) write(ofd, " Date: ", 8); 12021553Srgrimes (void) write(ofd, ctime(&tvec), 24); 12031553Srgrimes (void) write(ofd, "\n", 1); 12041553Srgrimes } else { /* normal banner */ 12051553Srgrimes (void) write(ofd, "\n\n\n", 3); 120631492Swollman scan_out(pp, ofd, name1, '\0'); 12071553Srgrimes (void) write(ofd, "\n\n", 2); 120831492Swollman scan_out(pp, ofd, name2, '\0'); 12091553Srgrimes if (class[0]) { 12101553Srgrimes (void) write(ofd,"\n\n\n",3); 121131492Swollman scan_out(pp, ofd, class, '\0'); 12121553Srgrimes } 12131553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 12141553Srgrimes (void) write(ofd, name2, strlen(name2)); 12151553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 12161553Srgrimes (void) write(ofd, ctime(&tvec), 24); 12171553Srgrimes (void) write(ofd, "\n", 1); 12181553Srgrimes } 121931492Swollman if (!pp->no_formfeed) 122031492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 122131492Swollman pp->tof = 1; 12221553Srgrimes} 12231553Srgrimes 12241553Srgrimesstatic char * 12251553Srgrimesscnline(key, p, c) 12261553Srgrimes register int key; 12271553Srgrimes register char *p; 12281553Srgrimes int c; 12291553Srgrimes{ 123039084Swollman register int scnwidth; 12311553Srgrimes 12321553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 12331553Srgrimes key <<= 1; 12341553Srgrimes *p++ = key & 0200 ? c : BACKGND; 12351553Srgrimes } 12361553Srgrimes return (p); 12371553Srgrimes} 12381553Srgrimes 12391553Srgrimes#define TRC(q) (((q)-' ')&0177) 12401553Srgrimes 12411553Srgrimesstatic void 124231492Swollmanscan_out(pp, scfd, scsp, dlm) 124331492Swollman struct printer *pp; 12441553Srgrimes int scfd, dlm; 12451553Srgrimes char *scsp; 12461553Srgrimes{ 12471553Srgrimes register char *strp; 124839084Swollman register int nchrs, j; 12491553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 12501553Srgrimes int d, scnhgt; 12511553Srgrimes 12521553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 12531553Srgrimes strp = &outbuf[0]; 12541553Srgrimes sp = scsp; 12551553Srgrimes for (nchrs = 0; ; ) { 12561553Srgrimes d = dropit(c = TRC(cc = *sp++)); 12571553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 12581553Srgrimes for (j = WIDTH; --j;) 12591553Srgrimes *strp++ = BACKGND; 12601553Srgrimes else 126131492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 126231492Swollman if (*sp == dlm || *sp == '\0' || 126331492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 12641553Srgrimes break; 12651553Srgrimes *strp++ = BACKGND; 12661553Srgrimes *strp++ = BACKGND; 12671553Srgrimes } 12681553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 12691553Srgrimes ; 12701553Srgrimes strp++; 12718857Srgrimes *strp++ = '\n'; 12721553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 12731553Srgrimes } 12741553Srgrimes} 12751553Srgrimes 12761553Srgrimesstatic int 12771553Srgrimesdropit(c) 12781553Srgrimes int c; 12791553Srgrimes{ 12801553Srgrimes switch(c) { 12811553Srgrimes 12821553Srgrimes case TRC('_'): 12831553Srgrimes case TRC(';'): 12841553Srgrimes case TRC(','): 12851553Srgrimes case TRC('g'): 12861553Srgrimes case TRC('j'): 12871553Srgrimes case TRC('p'): 12881553Srgrimes case TRC('q'): 12891553Srgrimes case TRC('y'): 12901553Srgrimes return (DROP); 12911553Srgrimes 12921553Srgrimes default: 12931553Srgrimes return (0); 12941553Srgrimes } 12951553Srgrimes} 12961553Srgrimes 12971553Srgrimes/* 12981553Srgrimes * sendmail --- 12991553Srgrimes * tell people about job completion 13001553Srgrimes */ 13011553Srgrimesstatic void 130231492Swollmansendmail(pp, user, bombed) 130331492Swollman struct printer *pp; 13041553Srgrimes char *user; 13051553Srgrimes int bombed; 13061553Srgrimes{ 13071553Srgrimes register int i; 13081553Srgrimes int p[2], s; 13091553Srgrimes register char *cp; 13101553Srgrimes struct stat stb; 13111553Srgrimes FILE *fp; 13121553Srgrimes 13131553Srgrimes pipe(p); 131431492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 13151553Srgrimes dup2(p[0], 0); 13168094Sjkh closelog(); 131731492Swollman closeallfds(3); 131827635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 13191553Srgrimes cp++; 132031492Swollman else 13211553Srgrimes cp = _PATH_SENDMAIL; 132227757Simp execl(_PATH_SENDMAIL, cp, "-t", 0); 132331492Swollman _exit(0); 13241553Srgrimes } else if (s > 0) { /* parent */ 13251553Srgrimes dup2(p[1], 1); 13261553Srgrimes printf("To: %s@%s\n", user, fromhost); 132731492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 132815648Sjoerg *jobname ? jobname : "<unknown>"); 132915648Sjoerg printf("Reply-To: root@%s\n\n", host); 13301553Srgrimes printf("Your printer job "); 13311553Srgrimes if (*jobname) 13321553Srgrimes printf("(%s) ", jobname); 133331492Swollman 133431492Swollman cp = "XXX compiler confusion"; /* XXX shut GCC up */ 13351553Srgrimes switch (bombed) { 13361553Srgrimes case OK: 13371553Srgrimes printf("\ncompleted successfully\n"); 133815648Sjoerg cp = "OK"; 13391553Srgrimes break; 13401553Srgrimes default: 13411553Srgrimes case FATALERR: 13421553Srgrimes printf("\ncould not be printed\n"); 134315648Sjoerg cp = "FATALERR"; 13441553Srgrimes break; 13451553Srgrimes case NOACCT: 13461553Srgrimes printf("\ncould not be printed without an account on %s\n", host); 134715648Sjoerg cp = "NOACCT"; 13481553Srgrimes break; 13491553Srgrimes case FILTERERR: 135068664Sgad if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 135168664Sgad || (fp = fopen(tempstderr, "r")) == NULL) { 135215648Sjoerg printf("\nhad some errors and may not have printed\n"); 13531553Srgrimes break; 13541553Srgrimes } 135515648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 13561553Srgrimes while ((i = getc(fp)) != EOF) 13571553Srgrimes putchar(i); 13581553Srgrimes (void) fclose(fp); 135915648Sjoerg cp = "FILTERERR"; 13601553Srgrimes break; 13611553Srgrimes case ACCESS: 13621553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 136315648Sjoerg cp = "ACCESS"; 13641553Srgrimes } 13651553Srgrimes fflush(stdout); 13661553Srgrimes (void) close(1); 136731492Swollman } else { 136831492Swollman syslog(LOG_WARNING, "unable to send mail to %s: %m", user); 136931492Swollman return; 13701553Srgrimes } 13711553Srgrimes (void) close(p[0]); 13721553Srgrimes (void) close(p[1]); 137315648Sjoerg wait(NULL); 137415648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 137531492Swollman user, *jobname ? jobname : "<unknown>", pp->printer, cp); 13761553Srgrimes} 13771553Srgrimes 13781553Srgrimes/* 13791553Srgrimes * dofork - fork with retries on failure 13801553Srgrimes */ 13811553Srgrimesstatic int 138231492Swollmandofork(pp, action) 138331492Swollman const struct printer *pp; 13841553Srgrimes int action; 13851553Srgrimes{ 13861553Srgrimes register int i, pid; 138760871Smpp struct passwd *pwd; 13881553Srgrimes 13891553Srgrimes for (i = 0; i < 20; i++) { 13901553Srgrimes if ((pid = fork()) < 0) { 13911553Srgrimes sleep((unsigned)(i*i)); 13921553Srgrimes continue; 13931553Srgrimes } 13941553Srgrimes /* 13951553Srgrimes * Child should run as daemon instead of root 13961553Srgrimes */ 139760871Smpp if (pid == 0) { 139860871Smpp if ((pwd = getpwuid(pp->daemon_user)) == NULL) { 139968379Sgad syslog(LOG_ERR, "Can't lookup default daemon uid (%ld) in password file", 140060871Smpp pp->daemon_user); 140160871Smpp break; 140260871Smpp } 140360871Smpp initgroups(pwd->pw_name, pwd->pw_gid); 140460871Smpp setgid(pwd->pw_gid); 140531492Swollman setuid(pp->daemon_user); 140660871Smpp } 14071553Srgrimes return(pid); 14081553Srgrimes } 14091553Srgrimes syslog(LOG_ERR, "can't fork"); 14101553Srgrimes 14111553Srgrimes switch (action) { 14121553Srgrimes case DORETURN: 14131553Srgrimes return (-1); 14141553Srgrimes default: 14151553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 14161553Srgrimes /*FALL THRU*/ 14171553Srgrimes case DOABORT: 14181553Srgrimes exit(1); 14191553Srgrimes } 14201553Srgrimes /*NOTREACHED*/ 14211553Srgrimes} 14221553Srgrimes 14231553Srgrimes/* 14241553Srgrimes * Kill child processes to abort current job. 14251553Srgrimes */ 14261553Srgrimesstatic void 14271553Srgrimesabortpr(signo) 14281553Srgrimes int signo; 14291553Srgrimes{ 143068664Sgad 143168664Sgad (void) unlink(tempstderr); 14321553Srgrimes kill(0, SIGINT); 14331553Srgrimes if (ofilter > 0) 14341553Srgrimes kill(ofilter, SIGCONT); 14351553Srgrimes while (wait(NULL) > 0) 14361553Srgrimes ; 143724831Sbrian if (ofilter > 0 && tfd != -1) 143824831Sbrian unlink(tfile); 14391553Srgrimes exit(0); 14401553Srgrimes} 14411553Srgrimes 14421553Srgrimesstatic void 144331492Swollmaninit(pp) 144431492Swollman struct printer *pp; 14451553Srgrimes{ 14461553Srgrimes char *s; 14471553Srgrimes 144831492Swollman sprintf(&width[2], "%ld", pp->page_width); 144931492Swollman sprintf(&length[2], "%ld", pp->page_length); 145031492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 145131492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 145231492Swollman if ((s = checkremote(pp)) != 0) { 145331492Swollman syslog(LOG_WARNING, "%s", s); 145431492Swollman free(s); 145531492Swollman } 145631492Swollman} 145731492Swollman 145831492Swollmanvoid 145931492Swollmanstartprinting(printer) 146031492Swollman const char *printer; 146131492Swollman{ 146231492Swollman struct printer myprinter, *pp = &myprinter; 146331492Swollman int status; 146431492Swollman 146531492Swollman init_printer(pp); 146631492Swollman status = getprintcap(printer, pp); 146731492Swollman switch(status) { 146831492Swollman case PCAPERR_OSERR: 146931492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 14701553Srgrimes exit(1); 147131492Swollman case PCAPERR_NOTFOUND: 14721553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 14731553Srgrimes exit(1); 147431492Swollman case PCAPERR_TCLOOP: 147531492Swollman fatal(pp, "potential reference loop detected in printcap file"); 147631492Swollman default: 147731492Swollman break; 147831492Swollman } 147931492Swollman printjob(pp); 14801553Srgrimes} 14811553Srgrimes 14821553Srgrimes/* 14831553Srgrimes * Acquire line printer or remote connection. 14841553Srgrimes */ 14851553Srgrimesstatic void 148631492Swollmanopenpr(pp) 148731492Swollman const struct printer *pp; 14881553Srgrimes{ 148931492Swollman int p[2]; 149015648Sjoerg char *cp; 14911553Srgrimes 149231492Swollman if (pp->remote) { 149331492Swollman openrem(pp); 149431492Swollman } else if (*pp->lp) { 149531492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 149631492Swollman opennet(pp); 149715648Sjoerg else 149831492Swollman opentty(pp); 14991553Srgrimes } else { 15001553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 150131492Swollman pp->printer); 15021553Srgrimes exit(1); 15031553Srgrimes } 150415648Sjoerg 15051553Srgrimes /* 15061553Srgrimes * Start up an output filter, if needed. 15071553Srgrimes */ 150831492Swollman if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) { 15091553Srgrimes pipe(p); 151031492Swollman if (pp->remote) { 151131492Swollman strcpy(tfile, TFILENAME); 151224831Sbrian tfd = mkstemp(tfile); 151324831Sbrian } 151431492Swollman if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */ 15151553Srgrimes dup2(p[0], 0); /* pipe is std in */ 151624831Sbrian /* tfile/printer is stdout */ 151731492Swollman dup2(pp->remote ? tfd : pfd, 1); 15188094Sjkh closelog(); 151931492Swollman closeallfds(3); 152031492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 152131492Swollman cp = pp->filters[LPF_OUTPUT]; 15221553Srgrimes else 15231553Srgrimes cp++; 152431492Swollman execl(pp->filters[LPF_OUTPUT], cp, width, length, 0); 152531492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 152631492Swollman pp->filters[LPF_OUTPUT]); 15271553Srgrimes exit(1); 15281553Srgrimes } 15291553Srgrimes (void) close(p[0]); /* close input side */ 15301553Srgrimes ofd = p[1]; /* use pipe for output */ 15311553Srgrimes } else { 15321553Srgrimes ofd = pfd; 15331553Srgrimes ofilter = 0; 15341553Srgrimes } 15351553Srgrimes} 15361553Srgrimes 153715648Sjoerg/* 153815648Sjoerg * Printer connected directly to the network 153915648Sjoerg * or to a terminal server on the net 154015648Sjoerg */ 154115648Sjoergstatic void 154231492Swollmanopennet(pp) 154331492Swollman const struct printer *pp; 154415648Sjoerg{ 154515648Sjoerg register int i; 154631492Swollman int resp; 154731492Swollman u_long port; 154831492Swollman char *ep; 154930407Sjoerg void (*savealrm)(int); 155015648Sjoerg 155131492Swollman port = strtoul(pp->lp, &ep, 0); 155238470Sbrian if (*ep != '@' || port > 65535) { 155331492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 155431492Swollman pp->lp); 155515648Sjoerg exit(1); 155615648Sjoerg } 155731492Swollman ep++; 155815648Sjoerg 155915648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 156015648Sjoerg resp = -1; 156130407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 156231492Swollman alarm(pp->conn_timeout); 156331492Swollman pfd = getport(pp, ep, port); 156431020Sjoerg alarm(0); 156530407Sjoerg (void)signal(SIGALRM, savealrm); 156615648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 156715648Sjoerg resp = 1; 156815648Sjoerg else if (pfd >= 0) { 156915648Sjoerg /* 157015648Sjoerg * need to delay a bit for rs232 lines 157115648Sjoerg * to stabilize in case printer is 157215648Sjoerg * connected via a terminal server 157315648Sjoerg */ 157415648Sjoerg delay(500); 157515648Sjoerg break; 157615648Sjoerg } 157715648Sjoerg if (i == 1) { 157831492Swollman if (resp < 0) 157931492Swollman pstatus(pp, "waiting for %s to come up", 158031492Swollman pp->lp); 158131492Swollman else 158231492Swollman pstatus(pp, 158331492Swollman "waiting for access to printer on %s", 158431492Swollman pp->lp); 158515648Sjoerg } 158615648Sjoerg sleep(i); 158715648Sjoerg } 158831492Swollman pstatus(pp, "sending to %s port %d", ep, port); 158915648Sjoerg} 159015648Sjoerg 159115648Sjoerg/* 159215648Sjoerg * Printer is connected to an RS232 port on this host 159315648Sjoerg */ 159415648Sjoergstatic void 159531492Swollmanopentty(pp) 159631492Swollman const struct printer *pp; 159715648Sjoerg{ 159815648Sjoerg register int i; 159915648Sjoerg 160015648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 160131492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 160215648Sjoerg if (pfd >= 0) { 160315648Sjoerg delay(500); 160415648Sjoerg break; 160515648Sjoerg } 160615648Sjoerg if (errno == ENOENT) { 160731492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 160815648Sjoerg exit(1); 160915648Sjoerg } 161015648Sjoerg if (i == 1) 161131492Swollman pstatus(pp, 161231492Swollman "waiting for %s to become ready (offline?)", 161331492Swollman pp->printer); 161415648Sjoerg sleep(i); 161515648Sjoerg } 161615648Sjoerg if (isatty(pfd)) 161731492Swollman setty(pp); 161831492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 161915648Sjoerg} 162015648Sjoerg 162115648Sjoerg/* 162215648Sjoerg * Printer is on a remote host 162315648Sjoerg */ 162415648Sjoergstatic void 162531492Swollmanopenrem(pp) 162631492Swollman const struct printer *pp; 162715648Sjoerg{ 162831492Swollman register int i; 162927748Simp int resp; 163030407Sjoerg void (*savealrm)(int); 163115648Sjoerg 163215648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 163315648Sjoerg resp = -1; 163430407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 163531492Swollman alarm(pp->conn_timeout); 163631492Swollman pfd = getport(pp, pp->remote_host, 0); 163731020Sjoerg alarm(0); 163830407Sjoerg (void)signal(SIGALRM, savealrm); 163915648Sjoerg if (pfd >= 0) { 164031492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 164131492Swollman (char *)0) 164231492Swollman == 2 + strlen(pp->remote_queue)) 164331492Swollman && (resp = response(pp)) == 0) 164415648Sjoerg break; 164515648Sjoerg (void) close(pfd); 164615648Sjoerg } 164715648Sjoerg if (i == 1) { 164815648Sjoerg if (resp < 0) 164931492Swollman pstatus(pp, "waiting for %s to come up", 165031492Swollman pp->remote_host); 165115648Sjoerg else { 165231492Swollman pstatus(pp, 165331492Swollman "waiting for queue to be enabled on %s", 165431492Swollman pp->remote_host); 165515648Sjoerg i = 256; 165615648Sjoerg } 165715648Sjoerg } 165815648Sjoerg sleep(i); 165915648Sjoerg } 166031492Swollman pstatus(pp, "sending to %s", pp->remote_host); 166115648Sjoerg} 166215648Sjoerg 16631553Srgrimes/* 16641553Srgrimes * setup tty lines. 16651553Srgrimes */ 16661553Srgrimesstatic void 166731492Swollmansetty(pp) 166831492Swollman const struct printer *pp; 16691553Srgrimes{ 167015032Ssef struct termios ttybuf; 16711553Srgrimes 16721553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 167331492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 16741553Srgrimes exit(1); 16751553Srgrimes } 167615032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 167731492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 16781553Srgrimes exit(1); 16791553Srgrimes } 168031492Swollman if (pp->baud_rate > 0) 168131492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 168231492Swollman if (pp->mode_set) { 168331492Swollman char *s = strdup(pp->mode_set), *tmp; 168415032Ssef 168531492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 168639084Swollman (void) msearch(tmp, &ttybuf); 16871553Srgrimes } 16881553Srgrimes } 168931492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 169015032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 169131492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 16921553Srgrimes } 16931553Srgrimes } 16941553Srgrimes} 16951553Srgrimes 169627757Simp#ifdef __STDC__ 16971553Srgrimes#include <stdarg.h> 16981553Srgrimes#else 16991553Srgrimes#include <varargs.h> 17001553Srgrimes#endif 17011553Srgrimes 170215648Sjoergstatic void 170327757Simp#ifdef __STDC__ 170431492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 17051553Srgrimes#else 170631492Swollmanpstatus(pp, msg, va_alist) 170731492Swollman const struct printer *pp; 17081553Srgrimes char *msg; 17091553Srgrimes va_dcl 17101553Srgrimes#endif 17111553Srgrimes{ 171231492Swollman int fd; 171331492Swollman char *buf; 17141553Srgrimes va_list ap; 171527757Simp#ifdef __STDC__ 17161553Srgrimes va_start(ap, msg); 17171553Srgrimes#else 17181553Srgrimes va_start(ap); 17191553Srgrimes#endif 17201553Srgrimes 17211553Srgrimes umask(0); 172231492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 172331492Swollman if (fd < 0) { 172431492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file); 17251553Srgrimes exit(1); 17261553Srgrimes } 17271553Srgrimes ftruncate(fd, 0); 172831492Swollman vasprintf(&buf, msg, ap); 17291553Srgrimes va_end(ap); 173031492Swollman writel(fd, buf, "\n", (char *)0); 173131492Swollman close(fd); 173231492Swollman free(buf); 17331553Srgrimes} 173430407Sjoerg 173530407Sjoergvoid 173630407Sjoergalarmhandler(signo) 173730407Sjoerg{ 173830407Sjoerg /* ignored */ 173930407Sjoerg} 1740