printjob.c revision 68733
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 68733 2000-11-14 21:44:47Z 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; 15568733Sgad off_t pidoff; 15668733Sgad int errcnt, jobcount, tempfd; 1571553Srgrimes 15868733Sgad jobcount = 0; 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); 22668732Sgad exit(1); 22768664Sgad } 22868664Sgad if ((i = fchmod(tempfd, 0664)) == -1) { 22968664Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 23068664Sgad tempstderr); 23168732Sgad 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 } 27868733Sgad if (i == OK) /* all files of this job printed */ 27968733Sgad jobcount++; 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: 32268733Sgad if (jobcount > 0) { /* jobs 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; 35768682Sgad char *cp; 35868682Sgad int bombed = OK; 35968682Sgad int didignorehdr = 0; 3601553Srgrimes 3611553Srgrimes /* 3621553Srgrimes * open control file; ignore if no longer there. 3631553Srgrimes */ 3641553Srgrimes if ((cfp = fopen(file, "r")) == NULL) { 36531492Swollman syslog(LOG_INFO, "%s: %s: %m", pp->printer, file); 3661553Srgrimes return(OK); 3671553Srgrimes } 3681553Srgrimes /* 3691553Srgrimes * Reset troff fonts. 3701553Srgrimes */ 3711553Srgrimes for (i = 0; i < 4; i++) 3721553Srgrimes strcpy(fonts[i], ifonts[i]); 37331492Swollman sprintf(&width[2], "%ld", pp->page_width); 3741553Srgrimes strcpy(indent+2, "0"); 3751553Srgrimes 37668253Sgad /* initialize job-specific count of datafiles processed */ 37768253Sgad job_dfcnt = 0; 37868253Sgad 3791553Srgrimes /* 3801553Srgrimes * read the control file for work to do 3811553Srgrimes * 3821553Srgrimes * file format -- first character in the line is a command 3831553Srgrimes * rest of the line is the argument. 3841553Srgrimes * valid commands are: 3851553Srgrimes * 3861553Srgrimes * S -- "stat info" for symbolic link protection 3871553Srgrimes * J -- "job name" on banner page 3881553Srgrimes * C -- "class name" on banner page 3891553Srgrimes * L -- "literal" user's name to print on banner 3901553Srgrimes * T -- "title" for pr 3911553Srgrimes * H -- "host name" of machine where lpr was done 3921553Srgrimes * P -- "person" user's login name 3931553Srgrimes * I -- "indent" amount to indent output 39415648Sjoerg * R -- laser dpi "resolution" 3951553Srgrimes * f -- "file name" name of text file to print 3961553Srgrimes * l -- "file name" text file with control chars 3971553Srgrimes * p -- "file name" text file to print with pr(1) 3981553Srgrimes * t -- "file name" troff(1) file to print 3991553Srgrimes * n -- "file name" ditroff(1) file to print 4001553Srgrimes * d -- "file name" dvi file to print 4011553Srgrimes * g -- "file name" plot(1G) file to print 4021553Srgrimes * v -- "file name" plain raster file to print 4031553Srgrimes * c -- "file name" cifplot file to print 4041553Srgrimes * 1 -- "R font file" for troff 4051553Srgrimes * 2 -- "I font file" for troff 4061553Srgrimes * 3 -- "B font file" for troff 4071553Srgrimes * 4 -- "S font file" for troff 4081553Srgrimes * N -- "name" of file (used by lpq) 4091553Srgrimes * U -- "unlink" name of file to remove 4101553Srgrimes * (after we print it. (Pass 2 only)). 4111553Srgrimes * M -- "mail" to user when done printing 41253956Sache * Z -- "locale" for pr 4131553Srgrimes * 4141553Srgrimes * getline reads a line and expands tabs to blanks 4151553Srgrimes */ 4161553Srgrimes 4171553Srgrimes /* pass 1 */ 4181553Srgrimes 4191553Srgrimes while (getline(cfp)) 4201553Srgrimes switch (line[0]) { 4211553Srgrimes case 'H': 42227748Simp strncpy(fromhost, line+1, sizeof(fromhost) - 1); 42327748Simp fromhost[sizeof(fromhost) - 1] = '\0'; 42427748Simp if (class[0] == '\0') { 42527748Simp strncpy(class, line+1, sizeof(class) - 1); 42627748Simp class[sizeof(class) - 1] = '\0'; 42727748Simp } 4281553Srgrimes continue; 4291553Srgrimes 4301553Srgrimes case 'P': 43127748Simp strncpy(logname, line+1, sizeof(logname) - 1); 43227748Simp logname[sizeof(logname) - 1] = '\0'; 43331492Swollman if (pp->restricted) { /* restricted */ 4341553Srgrimes if (getpwnam(logname) == NULL) { 4351553Srgrimes bombed = NOACCT; 43631492Swollman sendmail(pp, line+1, bombed); 4371553Srgrimes goto pass2; 4381553Srgrimes } 4391553Srgrimes } 4401553Srgrimes continue; 4411553Srgrimes 4421553Srgrimes case 'S': 4431553Srgrimes cp = line+1; 4441553Srgrimes i = 0; 4451553Srgrimes while (*cp >= '0' && *cp <= '9') 4461553Srgrimes i = i * 10 + (*cp++ - '0'); 4471553Srgrimes fdev = i; 4481553Srgrimes cp++; 4491553Srgrimes i = 0; 4501553Srgrimes while (*cp >= '0' && *cp <= '9') 4511553Srgrimes i = i * 10 + (*cp++ - '0'); 4521553Srgrimes fino = i; 4531553Srgrimes continue; 4541553Srgrimes 4551553Srgrimes case 'J': 45627748Simp if (line[1] != '\0') { 45727748Simp strncpy(jobname, line+1, sizeof(jobname) - 1); 45827748Simp jobname[sizeof(jobname) - 1] = '\0'; 45927748Simp } else 4601553Srgrimes strcpy(jobname, " "); 4611553Srgrimes continue; 4621553Srgrimes 4631553Srgrimes case 'C': 4641553Srgrimes if (line[1] != '\0') 46527748Simp strncpy(class, line+1, sizeof(class) - 1); 4661553Srgrimes else if (class[0] == '\0') 4671553Srgrimes gethostname(class, sizeof(class)); 46827748Simp class[sizeof(class) - 1] = '\0'; 4691553Srgrimes continue; 4701553Srgrimes 4711553Srgrimes case 'T': /* header title for pr */ 47227748Simp strncpy(title, line+1, sizeof(title) - 1); 47327748Simp title[sizeof(title) - 1] = '\0'; 4741553Srgrimes continue; 4751553Srgrimes 4761553Srgrimes case 'L': /* identification line */ 47731492Swollman if (!pp->no_header && !pp->header_last) 47831492Swollman banner(pp, line+1, jobname); 4791553Srgrimes continue; 4801553Srgrimes 4811553Srgrimes case '1': /* troff fonts */ 4821553Srgrimes case '2': 4831553Srgrimes case '3': 4841553Srgrimes case '4': 48527748Simp if (line[1] != '\0') { 48627748Simp strncpy(fonts[line[0]-'1'], line+1, 48727748Simp 50-1); 48827748Simp fonts[line[0]-'1'][50-1] = '\0'; 48927748Simp } 4901553Srgrimes continue; 4911553Srgrimes 4921553Srgrimes case 'W': /* page width */ 49327748Simp strncpy(width+2, line+1, sizeof(width) - 3); 49427748Simp width[2+sizeof(width) - 3] = '\0'; 4951553Srgrimes continue; 4961553Srgrimes 4971553Srgrimes case 'I': /* indent amount */ 49827748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 49927748Simp indent[2+sizeof(indent) - 3] = '\0'; 5001553Srgrimes continue; 5011553Srgrimes 50253956Sache case 'Z': /* locale for pr */ 50353956Sache strncpy(locale, line+1, sizeof(locale) - 1); 50453956Sache locale[sizeof(locale) - 1] = '\0'; 50553956Sache continue; 50653956Sache 5071553Srgrimes default: /* some file to print */ 50868467Sgad /* only lowercase cmd-codes include a file-to-print */ 50968467Sgad if ((line[0] < 'a') || (line[0] > 'z')) { 51068467Sgad /* ignore any other lines */ 51168467Sgad if (lflag <= 1) 51268467Sgad continue; 51368467Sgad if (!didignorehdr) { 51468467Sgad syslog(LOG_INFO, "%s: in %s :", 51568467Sgad pp->printer, file); 51668467Sgad didignorehdr = 1; 51768467Sgad } 51868467Sgad syslog(LOG_INFO, "%s: ignoring line: '%c' %s", 51968467Sgad pp->printer, line[0], &line[1]); 52068467Sgad continue; 52168467Sgad } 52268467Sgad i = print(pp, line[0], line+1); 52368467Sgad switch (i) { 5241553Srgrimes case ERROR: 5251553Srgrimes if (bombed == OK) 5261553Srgrimes bombed = FATALERR; 5271553Srgrimes break; 5281553Srgrimes case REPRINT: 5291553Srgrimes (void) fclose(cfp); 5301553Srgrimes return(REPRINT); 5311553Srgrimes case FILTERERR: 5321553Srgrimes case ACCESS: 5331553Srgrimes bombed = i; 53431492Swollman sendmail(pp, logname, bombed); 5351553Srgrimes } 5361553Srgrimes title[0] = '\0'; 5371553Srgrimes continue; 5381553Srgrimes 5391553Srgrimes case 'N': 5401553Srgrimes case 'U': 5411553Srgrimes case 'M': 54215648Sjoerg case 'R': 5431553Srgrimes continue; 5441553Srgrimes } 5451553Srgrimes 5461553Srgrimes /* pass 2 */ 5471553Srgrimes 5481553Srgrimespass2: 5491553Srgrimes fseek(cfp, 0L, 0); 5501553Srgrimes while (getline(cfp)) 5511553Srgrimes switch (line[0]) { 5521553Srgrimes case 'L': /* identification line */ 55331492Swollman if (!pp->no_header && pp->header_last) 55431492Swollman banner(pp, line+1, jobname); 5551553Srgrimes continue; 5561553Srgrimes 5571553Srgrimes case 'M': 5581553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 55931492Swollman sendmail(pp, line+1, bombed); 5601553Srgrimes continue; 5611553Srgrimes 5621553Srgrimes case 'U': 56327748Simp if (strchr(line+1, '/')) 56427748Simp continue; 5651553Srgrimes (void) unlink(line+1); 5661553Srgrimes } 5671553Srgrimes /* 5681553Srgrimes * clean-up in case another control file exists 5691553Srgrimes */ 5701553Srgrimes (void) fclose(cfp); 5711553Srgrimes (void) unlink(file); 5721553Srgrimes return(bombed == OK ? OK : ERROR); 5731553Srgrimes} 5741553Srgrimes 5751553Srgrimes/* 5761553Srgrimes * Print a file. 5771553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 5781553Srgrimes * Return -1 if a non-recoverable error occured, 5791553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 5801553Srgrimes * 1 if we should try to reprint this job and 5811553Srgrimes * 0 if all is well. 5821553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 5831553Srgrimes * stderr as the log file, and must not ignore SIGINT. 5841553Srgrimes */ 5851553Srgrimesstatic int 58631492Swollmanprint(pp, format, file) 58731492Swollman struct printer *pp; 5881553Srgrimes int format; 5891553Srgrimes char *file; 5901553Srgrimes{ 59153956Sache register int n, i; 5921553Srgrimes register char *prog; 59331492Swollman int fi, fo; 5941553Srgrimes FILE *fp; 5951553Srgrimes char *av[15], buf[BUFSIZ]; 5961553Srgrimes int pid, p[2], stopped = 0; 5971553Srgrimes union wait status; 5981553Srgrimes struct stat stb; 5991553Srgrimes 60068467Sgad if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { 60168467Sgad syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", 60268467Sgad pp->printer, file, format); 6031553Srgrimes return(ERROR); 60468467Sgad } 6051553Srgrimes /* 6061553Srgrimes * Check to see if data file is a symbolic link. If so, it should 6071553Srgrimes * still point to the same file or someone is trying to print 6081553Srgrimes * something he shouldn't. 6091553Srgrimes */ 6101553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 6111553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 6121553Srgrimes return(ACCESS); 61368253Sgad 61468253Sgad job_dfcnt++; /* increment datafile counter for this job */ 61568253Sgad 61668253Sgad /* everything seems OK, start it up */ 61731492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 61831492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 61931492Swollman pp->tof = 1; 6201553Srgrimes } 62131492Swollman if (pp->filters[LPF_INPUT] == NULL 62231492Swollman && (format == 'f' || format == 'l')) { 62331492Swollman pp->tof = 0; 6241553Srgrimes while ((n = read(fi, buf, BUFSIZ)) > 0) 6251553Srgrimes if (write(ofd, buf, n) != n) { 6261553Srgrimes (void) close(fi); 6271553Srgrimes return(REPRINT); 6281553Srgrimes } 6291553Srgrimes (void) close(fi); 6301553Srgrimes return(OK); 6311553Srgrimes } 6321553Srgrimes switch (format) { 6331553Srgrimes case 'p': /* print file using 'pr' */ 63431492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 6351553Srgrimes prog = _PATH_PR; 63653956Sache i = 0; 63753956Sache av[i++] = "pr"; 63853956Sache av[i++] = width; 63953956Sache av[i++] = length; 64053956Sache av[i++] = "-h"; 64153956Sache av[i++] = *title ? title : " "; 64253956Sache av[i++] = "-L"; 64353956Sache av[i++] = *locale ? locale : "C"; 64453956Sache av[i++] = "-F"; 64553956Sache av[i] = 0; 6461553Srgrimes fo = ofd; 6471553Srgrimes goto start; 6481553Srgrimes } 6491553Srgrimes pipe(p); 65031492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 6511553Srgrimes dup2(fi, 0); /* file is stdin */ 6521553Srgrimes dup2(p[1], 1); /* pipe is stdout */ 6538094Sjkh closelog(); 65431492Swollman closeallfds(3); 6551553Srgrimes execl(_PATH_PR, "pr", width, length, 65653956Sache "-h", *title ? title : " ", 65753956Sache "-L", *locale ? locale : "C", 65853956Sache "-F", 0); 6591553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6601553Srgrimes exit(2); 6611553Srgrimes } 6621553Srgrimes (void) close(p[1]); /* close output side */ 6631553Srgrimes (void) close(fi); 6641553Srgrimes if (prchild < 0) { 6651553Srgrimes prchild = 0; 6661553Srgrimes (void) close(p[0]); 6671553Srgrimes return(ERROR); 6681553Srgrimes } 6691553Srgrimes fi = p[0]; /* use pipe for input */ 6701553Srgrimes case 'f': /* print plain text file */ 67131492Swollman prog = pp->filters[LPF_INPUT]; 6721553Srgrimes av[1] = width; 6731553Srgrimes av[2] = length; 6741553Srgrimes av[3] = indent; 6751553Srgrimes n = 4; 6761553Srgrimes break; 6771553Srgrimes case 'l': /* like 'f' but pass control characters */ 67831492Swollman prog = pp->filters[LPF_INPUT]; 6791553Srgrimes av[1] = "-c"; 6801553Srgrimes av[2] = width; 6811553Srgrimes av[3] = length; 6821553Srgrimes av[4] = indent; 6831553Srgrimes n = 5; 6841553Srgrimes break; 6851553Srgrimes case 'r': /* print a fortran text file */ 68631492Swollman prog = pp->filters[LPF_FORTRAN]; 6871553Srgrimes av[1] = width; 6881553Srgrimes av[2] = length; 6891553Srgrimes n = 3; 6901553Srgrimes break; 6911553Srgrimes case 't': /* print troff output */ 6921553Srgrimes case 'n': /* print ditroff output */ 6931553Srgrimes case 'd': /* print tex output */ 6941553Srgrimes (void) unlink(".railmag"); 6951553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 69631492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 69731492Swollman pp->printer); 6981553Srgrimes (void) unlink(".railmag"); 6991553Srgrimes } else { 7001553Srgrimes for (n = 0; n < 4; n++) { 7011553Srgrimes if (fonts[n][0] != '/') 7021553Srgrimes (void) write(fo, _PATH_VFONT, 7031553Srgrimes sizeof(_PATH_VFONT) - 1); 7041553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 7051553Srgrimes (void) write(fo, "\n", 1); 7061553Srgrimes } 7071553Srgrimes (void) close(fo); 7081553Srgrimes } 70931492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 71031492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 71131492Swollman : pp->filters[LPF_DVI]); 7121553Srgrimes av[1] = pxwidth; 7131553Srgrimes av[2] = pxlength; 7141553Srgrimes n = 3; 7151553Srgrimes break; 7161553Srgrimes case 'c': /* print cifplot output */ 71731492Swollman prog = pp->filters[LPF_CIFPLOT]; 7181553Srgrimes av[1] = pxwidth; 7191553Srgrimes av[2] = pxlength; 7201553Srgrimes n = 3; 7211553Srgrimes break; 7221553Srgrimes case 'g': /* print plot(1G) output */ 72331492Swollman prog = pp->filters[LPF_GRAPH]; 7241553Srgrimes av[1] = pxwidth; 7251553Srgrimes av[2] = pxlength; 7261553Srgrimes n = 3; 7271553Srgrimes break; 7281553Srgrimes case 'v': /* print raster output */ 72931492Swollman prog = pp->filters[LPF_RASTER]; 7301553Srgrimes av[1] = pxwidth; 7311553Srgrimes av[2] = pxlength; 7321553Srgrimes n = 3; 7331553Srgrimes break; 7341553Srgrimes default: 7351553Srgrimes (void) close(fi); 7361553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 73731492Swollman pp->printer, format); 7381553Srgrimes return(ERROR); 7391553Srgrimes } 74015648Sjoerg if (prog == NULL) { 74115648Sjoerg (void) close(fi); 74215648Sjoerg syslog(LOG_ERR, 74315648Sjoerg "%s: no filter found in printcap for format character '%c'", 74431492Swollman pp->printer, format); 74515648Sjoerg return(ERROR); 74615648Sjoerg } 74727635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 7481553Srgrimes av[0]++; 7491553Srgrimes else 7501553Srgrimes av[0] = prog; 7511553Srgrimes av[n++] = "-n"; 7521553Srgrimes av[n++] = logname; 7531553Srgrimes av[n++] = "-h"; 7541553Srgrimes av[n++] = fromhost; 75531492Swollman av[n++] = pp->acct_file; 7561553Srgrimes av[n] = 0; 7571553Srgrimes fo = pfd; 7581553Srgrimes if (ofilter > 0) { /* stop output filter */ 7591553Srgrimes write(ofd, "\031\1", 2); 7601553Srgrimes while ((pid = 7611553Srgrimes wait3((int *)&status, WUNTRACED, 0)) > 0 && pid != ofilter) 7621553Srgrimes ; 7631553Srgrimes if (status.w_stopval != WSTOPPED) { 7641553Srgrimes (void) close(fi); 76515648Sjoerg syslog(LOG_WARNING, 76631492Swollman "%s: output filter died " 76731492Swollman "(retcode=%d termsig=%d)", 76831492Swollman pp->printer, status.w_retcode, 76931492Swollman status.w_termsig); 7701553Srgrimes return(REPRINT); 7711553Srgrimes } 7721553Srgrimes stopped++; 7731553Srgrimes } 7741553Srgrimesstart: 77531492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 7761553Srgrimes dup2(fi, 0); 7771553Srgrimes dup2(fo, 1); 77868664Sgad /* setup stderr for the filter (child process) 77968664Sgad * so it goes to our temporary errors file */ 78068664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 7811553Srgrimes if (n >= 0) 7821553Srgrimes dup2(n, 2); 7838094Sjkh closelog(); 78431492Swollman closeallfds(3); 7851553Srgrimes execv(prog, av); 7861553Srgrimes syslog(LOG_ERR, "cannot execv %s", prog); 7871553Srgrimes exit(2); 7881553Srgrimes } 7891553Srgrimes (void) close(fi); 7901553Srgrimes if (child < 0) 7911553Srgrimes status.w_retcode = 100; 7921553Srgrimes else 7931553Srgrimes while ((pid = wait((int *)&status)) > 0 && pid != child) 7941553Srgrimes ; 7951553Srgrimes child = 0; 7961553Srgrimes prchild = 0; 7971553Srgrimes if (stopped) { /* restart output filter */ 7981553Srgrimes if (kill(ofilter, SIGCONT) < 0) { 7991553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 8001553Srgrimes exit(1); 8011553Srgrimes } 8021553Srgrimes } 80331492Swollman pp->tof = 0; 8041553Srgrimes 80568664Sgad /* Copy the filter's output to "lf" logfile */ 80668664Sgad if ((fp = fopen(tempstderr, "r"))) { 8071553Srgrimes while (fgets(buf, sizeof(buf), fp)) 8081553Srgrimes fputs(buf, stderr); 8091553Srgrimes fclose(fp); 8101553Srgrimes } 8111553Srgrimes 8121553Srgrimes if (!WIFEXITED(status)) { 81315648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 81431492Swollman pp->printer, format, status.w_termsig); 8151553Srgrimes return(ERROR); 8161553Srgrimes } 8171553Srgrimes switch (status.w_retcode) { 8181553Srgrimes case 0: 81931492Swollman pp->tof = 1; 8201553Srgrimes return(OK); 8211553Srgrimes case 1: 8221553Srgrimes return(REPRINT); 82315648Sjoerg case 2: 82415648Sjoerg return(ERROR); 8251553Srgrimes default: 82615648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 82731492Swollman pp->printer, format, status.w_retcode); 82815648Sjoerg return(FILTERERR); 8291553Srgrimes } 8301553Srgrimes} 8311553Srgrimes 8321553Srgrimes/* 8331553Srgrimes * Send the daemon control file (cf) and any data files. 8341553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 8351553Srgrimes * 0 if all is well. 8361553Srgrimes */ 8371553Srgrimesstatic int 83831492Swollmansendit(pp, file) 83931492Swollman struct printer *pp; 8401553Srgrimes char *file; 8411553Srgrimes{ 8421553Srgrimes register int i, err = OK; 8431553Srgrimes char *cp, last[BUFSIZ]; 8441553Srgrimes 8451553Srgrimes /* 8461553Srgrimes * open control file 8471553Srgrimes */ 8481553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 8491553Srgrimes return(OK); 85068253Sgad 85168253Sgad /* initialize job-specific count of datafiles processed */ 85268253Sgad job_dfcnt = 0; 85368253Sgad 8541553Srgrimes /* 8551553Srgrimes * read the control file for work to do 8561553Srgrimes * 8571553Srgrimes * file format -- first character in the line is a command 8581553Srgrimes * rest of the line is the argument. 8591553Srgrimes * commands of interest are: 8601553Srgrimes * 8611553Srgrimes * a-z -- "file name" name of file to print 8621553Srgrimes * U -- "unlink" name of file to remove 8631553Srgrimes * (after we print it. (Pass 2 only)). 8641553Srgrimes */ 8651553Srgrimes 8661553Srgrimes /* 8671553Srgrimes * pass 1 8681553Srgrimes */ 8691553Srgrimes while (getline(cfp)) { 8701553Srgrimes again: 8711553Srgrimes if (line[0] == 'S') { 8721553Srgrimes cp = line+1; 8731553Srgrimes i = 0; 8741553Srgrimes while (*cp >= '0' && *cp <= '9') 8751553Srgrimes i = i * 10 + (*cp++ - '0'); 8761553Srgrimes fdev = i; 8771553Srgrimes cp++; 8781553Srgrimes i = 0; 8791553Srgrimes while (*cp >= '0' && *cp <= '9') 8801553Srgrimes i = i * 10 + (*cp++ - '0'); 8811553Srgrimes fino = i; 88224831Sbrian } else if (line[0] == 'H') { 88368343Sgad strncpy(fromhost, line+1, sizeof(fromhost) - 1); 88468343Sgad fromhost[sizeof(fromhost) - 1] = '\0'; 88568343Sgad if (class[0] == '\0') { 88627748Simp strncpy(class, line+1, sizeof(class) - 1); 88768343Sgad class[sizeof(class) - 1] = '\0'; 88868343Sgad } 88924831Sbrian } else if (line[0] == 'P') { 89027748Simp strncpy(logname, line+1, sizeof(logname) - 1); 89131492Swollman if (pp->restricted) { /* restricted */ 89224831Sbrian if (getpwnam(logname) == NULL) { 89331492Swollman sendmail(pp, line+1, NOACCT); 89424831Sbrian err = ERROR; 89524831Sbrian break; 89624831Sbrian } 89724831Sbrian } 89824831Sbrian } else if (line[0] == 'I') { 89927748Simp strncpy(indent+2, line+1, sizeof(indent) - 3); 90024831Sbrian } else if (line[0] >= 'a' && line[0] <= 'z') { 9011553Srgrimes strcpy(last, line); 90231492Swollman while ((i = getline(cfp)) != 0) 9031553Srgrimes if (strcmp(last, line)) 9041553Srgrimes break; 90531492Swollman switch (sendfile(pp, '\3', last+1, *last)) { 9061553Srgrimes case OK: 9071553Srgrimes if (i) 9081553Srgrimes goto again; 9091553Srgrimes break; 9101553Srgrimes case REPRINT: 9111553Srgrimes (void) fclose(cfp); 9121553Srgrimes return(REPRINT); 9131553Srgrimes case ACCESS: 91431492Swollman sendmail(pp, logname, ACCESS); 9151553Srgrimes case ERROR: 9161553Srgrimes err = ERROR; 9171553Srgrimes } 9181553Srgrimes break; 9191553Srgrimes } 9201553Srgrimes } 92131492Swollman if (err == OK && sendfile(pp, '\2', file, '\0') > 0) { 9221553Srgrimes (void) fclose(cfp); 9231553Srgrimes return(REPRINT); 9241553Srgrimes } 9251553Srgrimes /* 9261553Srgrimes * pass 2 9271553Srgrimes */ 9281553Srgrimes fseek(cfp, 0L, 0); 9291553Srgrimes while (getline(cfp)) 93027748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 9311553Srgrimes (void) unlink(line+1); 9321553Srgrimes /* 9331553Srgrimes * clean-up in case another control file exists 9341553Srgrimes */ 9351553Srgrimes (void) fclose(cfp); 9361553Srgrimes (void) unlink(file); 9371553Srgrimes return(err); 9381553Srgrimes} 9391553Srgrimes 9401553Srgrimes/* 9411553Srgrimes * Send a data file to the remote machine and spool it. 9421553Srgrimes * Return positive if we should try resending. 9431553Srgrimes */ 9441553Srgrimesstatic int 94531492Swollmansendfile(pp, type, file, format) 94631492Swollman struct printer *pp; 9471553Srgrimes int type; 9481553Srgrimes char *file; 94924831Sbrian char format; 9501553Srgrimes{ 9511553Srgrimes register int f, i, amt; 9521553Srgrimes struct stat stb; 95368664Sgad FILE *fp; 9541553Srgrimes char buf[BUFSIZ]; 95524831Sbrian int sizerr, resp, closedpr; 9561553Srgrimes 9571553Srgrimes if (lstat(file, &stb) < 0 || (f = open(file, O_RDONLY)) < 0) 9581553Srgrimes return(ERROR); 9591553Srgrimes /* 9601553Srgrimes * Check to see if data file is a symbolic link. If so, it should 9611553Srgrimes * still point to the same file or someone is trying to print something 9621553Srgrimes * he shouldn't. 9631553Srgrimes */ 9641553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(f, &stb) == 0 && 9651553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 9661553Srgrimes return(ACCESS); 96724831Sbrian 96868253Sgad job_dfcnt++; /* increment datafile counter for this job */ 96968253Sgad 97068253Sgad /* everything seems OK, start it up */ 97124831Sbrian sizerr = 0; 97224831Sbrian closedpr = 0; 97324831Sbrian if (type == '\3') { 97431492Swollman if (pp->filters[LPF_INPUT]) { 97524831Sbrian /* 97624831Sbrian * We're sending something with an ifilter, we have to 97724831Sbrian * run the ifilter and store the output as a 97824831Sbrian * temporary file (tfile)... the protocol requires us 97924831Sbrian * to send the file size 98024831Sbrian */ 98124831Sbrian char *av[15]; 98224831Sbrian int n; 98324831Sbrian int ifilter; 98431492Swollman union wait status; /* XXX */ 98524831Sbrian 98624831Sbrian strcpy(tfile,TFILENAME); 98724831Sbrian if ((tfd = mkstemp(tfile)) == -1) { 98824831Sbrian syslog(LOG_ERR, "mkstemp: %m"); 98924831Sbrian return(ERROR); 99024831Sbrian } 99131492Swollman if ((av[0] = strrchr(pp->filters[LPF_INPUT], '/')) == NULL) 99231492Swollman av[0] = pp->filters[LPF_INPUT]; 99324831Sbrian else 99424831Sbrian av[0]++; 99524831Sbrian if (format == 'l') 99624831Sbrian av[n=1] = "-c"; 99724831Sbrian else 99824831Sbrian n = 0; 99924831Sbrian av[++n] = width; 100024831Sbrian av[++n] = length; 100124831Sbrian av[++n] = indent; 100224831Sbrian av[++n] = "-n"; 100324831Sbrian av[++n] = logname; 100424831Sbrian av[++n] = "-h"; 100524831Sbrian av[++n] = fromhost; 100631492Swollman av[++n] = pp->acct_file; 100724831Sbrian av[++n] = 0; 100831492Swollman if ((ifilter = dofork(pp, DORETURN)) == 0) { /* child */ 100924831Sbrian dup2(f, 0); 101024831Sbrian dup2(tfd, 1); 101168664Sgad /* setup stderr for the filter (child process) 101268664Sgad * so it goes to our temporary errors file */ 101368664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 101424831Sbrian if (n >= 0) 101524831Sbrian dup2(n, 2); 101624831Sbrian closelog(); 101731492Swollman closeallfds(3); 101831492Swollman execv(pp->filters[LPF_INPUT], av); 101931492Swollman syslog(LOG_ERR, "cannot execv %s", 102031492Swollman pp->filters[LPF_INPUT]); 102124831Sbrian exit(2); 102224831Sbrian } 102324831Sbrian (void) close(f); 102424831Sbrian if (ifilter < 0) 102524831Sbrian status.w_retcode = 100; 102624831Sbrian else 102724831Sbrian while ((pid = wait((int *)&status)) > 0 && 102824831Sbrian pid != ifilter) 102924831Sbrian ; 103068664Sgad /* Copy the filter's output to "lf" logfile */ 103168664Sgad if ((fp = fopen(tempstderr, "r"))) { 103268664Sgad while (fgets(buf, sizeof(buf), fp)) 103368664Sgad fputs(buf, stderr); 103468664Sgad fclose(fp); 103568664Sgad } 103668664Sgad /* process the return-code from the filter */ 103724831Sbrian switch (status.w_retcode) { 103824831Sbrian case 0: 103924831Sbrian break; 104024831Sbrian case 1: 104124831Sbrian unlink(tfile); 104224831Sbrian return(REPRINT); 104324831Sbrian case 2: 104424831Sbrian unlink(tfile); 104524831Sbrian return(ERROR); 104624831Sbrian default: 104724831Sbrian syslog(LOG_WARNING, "%s: filter '%c' exited" 104824831Sbrian " (retcode=%d)", 104931492Swollman pp->printer, format, status.w_retcode); 105024831Sbrian unlink(tfile); 105124831Sbrian return(FILTERERR); 105224831Sbrian } 105324831Sbrian if (fstat(tfd, &stb) < 0) /* the size of tfile */ 105424831Sbrian return(ERROR); 105524831Sbrian f = tfd; 105624831Sbrian lseek(f,0,SEEK_SET); 105724831Sbrian } else if (ofilter) { 105824831Sbrian /* 105924831Sbrian * We're sending something with an ofilter, we have to 106024831Sbrian * store the output as a temporary file (tfile)... the 106124831Sbrian * protocol requires us to send the file size 106224831Sbrian */ 106324831Sbrian int i; 106424831Sbrian for (i = 0; i < stb.st_size; i += BUFSIZ) { 106524831Sbrian amt = BUFSIZ; 106624831Sbrian if (i + amt > stb.st_size) 106724831Sbrian amt = stb.st_size - i; 106824831Sbrian if (sizerr == 0 && read(f, buf, amt) != amt) { 106924831Sbrian sizerr = 1; 107024831Sbrian break; 107124831Sbrian } 107224831Sbrian if (write(ofd, buf, amt) != amt) { 107324831Sbrian (void) close(f); 107424831Sbrian return(REPRINT); 107524831Sbrian } 107624831Sbrian } 107724831Sbrian close(ofd); 107824831Sbrian close(f); 107924831Sbrian while ((i = wait(NULL)) > 0 && i != ofilter) 108024831Sbrian ; 108124831Sbrian ofilter = 0; 108224831Sbrian if (fstat(tfd, &stb) < 0) { /* the size of tfile */ 108331492Swollman openpr(pp); 108424831Sbrian return(ERROR); 108524831Sbrian } 108624831Sbrian f = tfd; 108724831Sbrian lseek(f,0,SEEK_SET); 108824831Sbrian closedpr = 1; 108924831Sbrian } 109024831Sbrian } 109124831Sbrian 10921553Srgrimes (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 10931553Srgrimes amt = strlen(buf); 10941553Srgrimes for (i = 0; ; i++) { 10951553Srgrimes if (write(pfd, buf, amt) != amt || 109631492Swollman (resp = response(pp)) < 0 || resp == '\1') { 10971553Srgrimes (void) close(f); 109824831Sbrian if (tfd != -1 && type == '\3') { 109924831Sbrian tfd = -1; 110024831Sbrian unlink(tfile); 110124831Sbrian if (closedpr) 110231492Swollman openpr(pp); 110324831Sbrian } 11041553Srgrimes return(REPRINT); 11051553Srgrimes } else if (resp == '\0') 11061553Srgrimes break; 11071553Srgrimes if (i == 0) 110831492Swollman pstatus(pp, 110931492Swollman "no space on remote; waiting for queue to drain"); 11101553Srgrimes if (i == 10) 11111553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 111231492Swollman pp->printer, pp->remote_host); 11131553Srgrimes sleep(5 * 60); 11141553Srgrimes } 11151553Srgrimes if (i) 111631492Swollman pstatus(pp, "sending to %s", pp->remote_host); 111768253Sgad if (type == '\3') 111868253Sgad trstat_init(pp, file, job_dfcnt); 11191553Srgrimes for (i = 0; i < stb.st_size; i += BUFSIZ) { 11201553Srgrimes amt = BUFSIZ; 11211553Srgrimes if (i + amt > stb.st_size) 11221553Srgrimes amt = stb.st_size - i; 11231553Srgrimes if (sizerr == 0 && read(f, buf, amt) != amt) 11241553Srgrimes sizerr = 1; 11251553Srgrimes if (write(pfd, buf, amt) != amt) { 11261553Srgrimes (void) close(f); 112724831Sbrian if (tfd != -1 && type == '\3') { 112824831Sbrian tfd = -1; 112924831Sbrian unlink(tfile); 113024831Sbrian if (closedpr) 113131492Swollman openpr(pp); 113224831Sbrian } 11331553Srgrimes return(REPRINT); 11341553Srgrimes } 11351553Srgrimes } 11361553Srgrimes 11371553Srgrimes (void) close(f); 113824831Sbrian if (tfd != -1 && type == '\3') { 113924831Sbrian tfd = -1; 114024831Sbrian unlink(tfile); 114124831Sbrian } 11421553Srgrimes if (sizerr) { 114331492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 11441553Srgrimes /* tell recvjob to ignore this file */ 11451553Srgrimes (void) write(pfd, "\1", 1); 114624831Sbrian if (closedpr) 114731492Swollman openpr(pp); 11481553Srgrimes return(ERROR); 11491553Srgrimes } 115031492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 115124831Sbrian if (closedpr) 115231492Swollman openpr(pp); 11531553Srgrimes return(REPRINT); 115424831Sbrian } 115524831Sbrian if (closedpr) 115631492Swollman openpr(pp); 115768253Sgad if (type == '\3') 115868253Sgad trstat_write(pp, TR_SENDING, stb.st_size, logname, 115968253Sgad pp->remote_host, fromhost); 11601553Srgrimes return(OK); 11611553Srgrimes} 11621553Srgrimes 11631553Srgrimes/* 11641553Srgrimes * Check to make sure there have been no errors and that both programs 11651553Srgrimes * are in sync with eachother. 11661553Srgrimes * Return non-zero if the connection was lost. 11671553Srgrimes */ 11681553Srgrimesstatic char 116931492Swollmanresponse(pp) 117031492Swollman const struct printer *pp; 11711553Srgrimes{ 11721553Srgrimes char resp; 11731553Srgrimes 11741553Srgrimes if (read(pfd, &resp, 1) != 1) { 117531492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 11761553Srgrimes return(-1); 11771553Srgrimes } 11781553Srgrimes return(resp); 11791553Srgrimes} 11801553Srgrimes 11811553Srgrimes/* 11821553Srgrimes * Banner printing stuff 11831553Srgrimes */ 11841553Srgrimesstatic void 118531492Swollmanbanner(pp, name1, name2) 118631492Swollman struct printer *pp; 11871553Srgrimes char *name1, *name2; 11881553Srgrimes{ 11891553Srgrimes time_t tvec; 11901553Srgrimes 11911553Srgrimes time(&tvec); 119231492Swollman if (!pp->no_formfeed && !pp->tof) 119331492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 119431492Swollman if (pp->short_banner) { /* short banner only */ 11951553Srgrimes if (class[0]) { 11961553Srgrimes (void) write(ofd, class, strlen(class)); 11971553Srgrimes (void) write(ofd, ":", 1); 11981553Srgrimes } 11991553Srgrimes (void) write(ofd, name1, strlen(name1)); 12001553Srgrimes (void) write(ofd, " Job: ", 7); 12011553Srgrimes (void) write(ofd, name2, strlen(name2)); 12021553Srgrimes (void) write(ofd, " Date: ", 8); 12031553Srgrimes (void) write(ofd, ctime(&tvec), 24); 12041553Srgrimes (void) write(ofd, "\n", 1); 12051553Srgrimes } else { /* normal banner */ 12061553Srgrimes (void) write(ofd, "\n\n\n", 3); 120731492Swollman scan_out(pp, ofd, name1, '\0'); 12081553Srgrimes (void) write(ofd, "\n\n", 2); 120931492Swollman scan_out(pp, ofd, name2, '\0'); 12101553Srgrimes if (class[0]) { 12111553Srgrimes (void) write(ofd,"\n\n\n",3); 121231492Swollman scan_out(pp, ofd, class, '\0'); 12131553Srgrimes } 12141553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 12151553Srgrimes (void) write(ofd, name2, strlen(name2)); 12161553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 12171553Srgrimes (void) write(ofd, ctime(&tvec), 24); 12181553Srgrimes (void) write(ofd, "\n", 1); 12191553Srgrimes } 122031492Swollman if (!pp->no_formfeed) 122131492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 122231492Swollman pp->tof = 1; 12231553Srgrimes} 12241553Srgrimes 12251553Srgrimesstatic char * 12261553Srgrimesscnline(key, p, c) 12271553Srgrimes register int key; 12281553Srgrimes register char *p; 12291553Srgrimes int c; 12301553Srgrimes{ 123139084Swollman register int scnwidth; 12321553Srgrimes 12331553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 12341553Srgrimes key <<= 1; 12351553Srgrimes *p++ = key & 0200 ? c : BACKGND; 12361553Srgrimes } 12371553Srgrimes return (p); 12381553Srgrimes} 12391553Srgrimes 12401553Srgrimes#define TRC(q) (((q)-' ')&0177) 12411553Srgrimes 12421553Srgrimesstatic void 124331492Swollmanscan_out(pp, scfd, scsp, dlm) 124431492Swollman struct printer *pp; 12451553Srgrimes int scfd, dlm; 12461553Srgrimes char *scsp; 12471553Srgrimes{ 12481553Srgrimes register char *strp; 124939084Swollman register int nchrs, j; 12501553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 12511553Srgrimes int d, scnhgt; 12521553Srgrimes 12531553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 12541553Srgrimes strp = &outbuf[0]; 12551553Srgrimes sp = scsp; 12561553Srgrimes for (nchrs = 0; ; ) { 12571553Srgrimes d = dropit(c = TRC(cc = *sp++)); 12581553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 12591553Srgrimes for (j = WIDTH; --j;) 12601553Srgrimes *strp++ = BACKGND; 12611553Srgrimes else 126231492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 126331492Swollman if (*sp == dlm || *sp == '\0' || 126431492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 12651553Srgrimes break; 12661553Srgrimes *strp++ = BACKGND; 12671553Srgrimes *strp++ = BACKGND; 12681553Srgrimes } 12691553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 12701553Srgrimes ; 12711553Srgrimes strp++; 12728857Srgrimes *strp++ = '\n'; 12731553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 12741553Srgrimes } 12751553Srgrimes} 12761553Srgrimes 12771553Srgrimesstatic int 12781553Srgrimesdropit(c) 12791553Srgrimes int c; 12801553Srgrimes{ 12811553Srgrimes switch(c) { 12821553Srgrimes 12831553Srgrimes case TRC('_'): 12841553Srgrimes case TRC(';'): 12851553Srgrimes case TRC(','): 12861553Srgrimes case TRC('g'): 12871553Srgrimes case TRC('j'): 12881553Srgrimes case TRC('p'): 12891553Srgrimes case TRC('q'): 12901553Srgrimes case TRC('y'): 12911553Srgrimes return (DROP); 12921553Srgrimes 12931553Srgrimes default: 12941553Srgrimes return (0); 12951553Srgrimes } 12961553Srgrimes} 12971553Srgrimes 12981553Srgrimes/* 12991553Srgrimes * sendmail --- 13001553Srgrimes * tell people about job completion 13011553Srgrimes */ 13021553Srgrimesstatic void 130331492Swollmansendmail(pp, user, bombed) 130431492Swollman struct printer *pp; 13051553Srgrimes char *user; 13061553Srgrimes int bombed; 13071553Srgrimes{ 13081553Srgrimes register int i; 13091553Srgrimes int p[2], s; 13101553Srgrimes register char *cp; 13111553Srgrimes struct stat stb; 13121553Srgrimes FILE *fp; 13131553Srgrimes 13141553Srgrimes pipe(p); 131531492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 13161553Srgrimes dup2(p[0], 0); 13178094Sjkh closelog(); 131831492Swollman closeallfds(3); 131927635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 13201553Srgrimes cp++; 132131492Swollman else 13221553Srgrimes cp = _PATH_SENDMAIL; 132327757Simp execl(_PATH_SENDMAIL, cp, "-t", 0); 132431492Swollman _exit(0); 13251553Srgrimes } else if (s > 0) { /* parent */ 13261553Srgrimes dup2(p[1], 1); 13271553Srgrimes printf("To: %s@%s\n", user, fromhost); 132831492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 132915648Sjoerg *jobname ? jobname : "<unknown>"); 133015648Sjoerg printf("Reply-To: root@%s\n\n", host); 13311553Srgrimes printf("Your printer job "); 13321553Srgrimes if (*jobname) 13331553Srgrimes printf("(%s) ", jobname); 133431492Swollman 133531492Swollman cp = "XXX compiler confusion"; /* XXX shut GCC up */ 13361553Srgrimes switch (bombed) { 13371553Srgrimes case OK: 13381553Srgrimes printf("\ncompleted successfully\n"); 133915648Sjoerg cp = "OK"; 13401553Srgrimes break; 13411553Srgrimes default: 13421553Srgrimes case FATALERR: 13431553Srgrimes printf("\ncould not be printed\n"); 134415648Sjoerg cp = "FATALERR"; 13451553Srgrimes break; 13461553Srgrimes case NOACCT: 13471553Srgrimes printf("\ncould not be printed without an account on %s\n", host); 134815648Sjoerg cp = "NOACCT"; 13491553Srgrimes break; 13501553Srgrimes case FILTERERR: 135168664Sgad if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 135268664Sgad || (fp = fopen(tempstderr, "r")) == NULL) { 135315648Sjoerg printf("\nhad some errors and may not have printed\n"); 13541553Srgrimes break; 13551553Srgrimes } 135615648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 13571553Srgrimes while ((i = getc(fp)) != EOF) 13581553Srgrimes putchar(i); 13591553Srgrimes (void) fclose(fp); 136015648Sjoerg cp = "FILTERERR"; 13611553Srgrimes break; 13621553Srgrimes case ACCESS: 13631553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 136415648Sjoerg cp = "ACCESS"; 13651553Srgrimes } 13661553Srgrimes fflush(stdout); 13671553Srgrimes (void) close(1); 136831492Swollman } else { 136931492Swollman syslog(LOG_WARNING, "unable to send mail to %s: %m", user); 137031492Swollman return; 13711553Srgrimes } 13721553Srgrimes (void) close(p[0]); 13731553Srgrimes (void) close(p[1]); 137415648Sjoerg wait(NULL); 137515648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 137631492Swollman user, *jobname ? jobname : "<unknown>", pp->printer, cp); 13771553Srgrimes} 13781553Srgrimes 13791553Srgrimes/* 13801553Srgrimes * dofork - fork with retries on failure 13811553Srgrimes */ 13821553Srgrimesstatic int 138331492Swollmandofork(pp, action) 138431492Swollman const struct printer *pp; 13851553Srgrimes int action; 13861553Srgrimes{ 13871553Srgrimes register int i, pid; 138860871Smpp struct passwd *pwd; 13891553Srgrimes 13901553Srgrimes for (i = 0; i < 20; i++) { 13911553Srgrimes if ((pid = fork()) < 0) { 13921553Srgrimes sleep((unsigned)(i*i)); 13931553Srgrimes continue; 13941553Srgrimes } 13951553Srgrimes /* 13961553Srgrimes * Child should run as daemon instead of root 13971553Srgrimes */ 139860871Smpp if (pid == 0) { 139960871Smpp if ((pwd = getpwuid(pp->daemon_user)) == NULL) { 140068379Sgad syslog(LOG_ERR, "Can't lookup default daemon uid (%ld) in password file", 140160871Smpp pp->daemon_user); 140260871Smpp break; 140360871Smpp } 140460871Smpp initgroups(pwd->pw_name, pwd->pw_gid); 140560871Smpp setgid(pwd->pw_gid); 140631492Swollman setuid(pp->daemon_user); 140760871Smpp } 14081553Srgrimes return(pid); 14091553Srgrimes } 14101553Srgrimes syslog(LOG_ERR, "can't fork"); 14111553Srgrimes 14121553Srgrimes switch (action) { 14131553Srgrimes case DORETURN: 14141553Srgrimes return (-1); 14151553Srgrimes default: 14161553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 14171553Srgrimes /*FALL THRU*/ 14181553Srgrimes case DOABORT: 14191553Srgrimes exit(1); 14201553Srgrimes } 14211553Srgrimes /*NOTREACHED*/ 14221553Srgrimes} 14231553Srgrimes 14241553Srgrimes/* 14251553Srgrimes * Kill child processes to abort current job. 14261553Srgrimes */ 14271553Srgrimesstatic void 14281553Srgrimesabortpr(signo) 14291553Srgrimes int signo; 14301553Srgrimes{ 143168664Sgad 143268664Sgad (void) unlink(tempstderr); 14331553Srgrimes kill(0, SIGINT); 14341553Srgrimes if (ofilter > 0) 14351553Srgrimes kill(ofilter, SIGCONT); 14361553Srgrimes while (wait(NULL) > 0) 14371553Srgrimes ; 143824831Sbrian if (ofilter > 0 && tfd != -1) 143924831Sbrian unlink(tfile); 14401553Srgrimes exit(0); 14411553Srgrimes} 14421553Srgrimes 14431553Srgrimesstatic void 144431492Swollmaninit(pp) 144531492Swollman struct printer *pp; 14461553Srgrimes{ 14471553Srgrimes char *s; 14481553Srgrimes 144931492Swollman sprintf(&width[2], "%ld", pp->page_width); 145031492Swollman sprintf(&length[2], "%ld", pp->page_length); 145131492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 145231492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 145331492Swollman if ((s = checkremote(pp)) != 0) { 145431492Swollman syslog(LOG_WARNING, "%s", s); 145531492Swollman free(s); 145631492Swollman } 145731492Swollman} 145831492Swollman 145931492Swollmanvoid 146031492Swollmanstartprinting(printer) 146131492Swollman const char *printer; 146231492Swollman{ 146331492Swollman struct printer myprinter, *pp = &myprinter; 146431492Swollman int status; 146531492Swollman 146631492Swollman init_printer(pp); 146731492Swollman status = getprintcap(printer, pp); 146831492Swollman switch(status) { 146931492Swollman case PCAPERR_OSERR: 147031492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 14711553Srgrimes exit(1); 147231492Swollman case PCAPERR_NOTFOUND: 14731553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 14741553Srgrimes exit(1); 147531492Swollman case PCAPERR_TCLOOP: 147631492Swollman fatal(pp, "potential reference loop detected in printcap file"); 147731492Swollman default: 147831492Swollman break; 147931492Swollman } 148031492Swollman printjob(pp); 14811553Srgrimes} 14821553Srgrimes 14831553Srgrimes/* 14841553Srgrimes * Acquire line printer or remote connection. 14851553Srgrimes */ 14861553Srgrimesstatic void 148731492Swollmanopenpr(pp) 148831492Swollman const struct printer *pp; 14891553Srgrimes{ 149031492Swollman int p[2]; 149115648Sjoerg char *cp; 14921553Srgrimes 149331492Swollman if (pp->remote) { 149431492Swollman openrem(pp); 149531492Swollman } else if (*pp->lp) { 149631492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 149731492Swollman opennet(pp); 149815648Sjoerg else 149931492Swollman opentty(pp); 15001553Srgrimes } else { 15011553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 150231492Swollman pp->printer); 15031553Srgrimes exit(1); 15041553Srgrimes } 150515648Sjoerg 15061553Srgrimes /* 15071553Srgrimes * Start up an output filter, if needed. 15081553Srgrimes */ 150931492Swollman if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) { 15101553Srgrimes pipe(p); 151131492Swollman if (pp->remote) { 151231492Swollman strcpy(tfile, TFILENAME); 151324831Sbrian tfd = mkstemp(tfile); 151424831Sbrian } 151531492Swollman if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */ 15161553Srgrimes dup2(p[0], 0); /* pipe is std in */ 151724831Sbrian /* tfile/printer is stdout */ 151831492Swollman dup2(pp->remote ? tfd : pfd, 1); 15198094Sjkh closelog(); 152031492Swollman closeallfds(3); 152131492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 152231492Swollman cp = pp->filters[LPF_OUTPUT]; 15231553Srgrimes else 15241553Srgrimes cp++; 152531492Swollman execl(pp->filters[LPF_OUTPUT], cp, width, length, 0); 152631492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, 152731492Swollman pp->filters[LPF_OUTPUT]); 15281553Srgrimes exit(1); 15291553Srgrimes } 15301553Srgrimes (void) close(p[0]); /* close input side */ 15311553Srgrimes ofd = p[1]; /* use pipe for output */ 15321553Srgrimes } else { 15331553Srgrimes ofd = pfd; 15341553Srgrimes ofilter = 0; 15351553Srgrimes } 15361553Srgrimes} 15371553Srgrimes 153815648Sjoerg/* 153915648Sjoerg * Printer connected directly to the network 154015648Sjoerg * or to a terminal server on the net 154115648Sjoerg */ 154215648Sjoergstatic void 154331492Swollmanopennet(pp) 154431492Swollman const struct printer *pp; 154515648Sjoerg{ 154615648Sjoerg register int i; 154731492Swollman int resp; 154831492Swollman u_long port; 154931492Swollman char *ep; 155030407Sjoerg void (*savealrm)(int); 155115648Sjoerg 155231492Swollman port = strtoul(pp->lp, &ep, 0); 155338470Sbrian if (*ep != '@' || port > 65535) { 155431492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 155531492Swollman pp->lp); 155615648Sjoerg exit(1); 155715648Sjoerg } 155831492Swollman ep++; 155915648Sjoerg 156015648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 156115648Sjoerg resp = -1; 156230407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 156331492Swollman alarm(pp->conn_timeout); 156431492Swollman pfd = getport(pp, ep, port); 156531020Sjoerg alarm(0); 156630407Sjoerg (void)signal(SIGALRM, savealrm); 156715648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 156815648Sjoerg resp = 1; 156915648Sjoerg else if (pfd >= 0) { 157015648Sjoerg /* 157115648Sjoerg * need to delay a bit for rs232 lines 157215648Sjoerg * to stabilize in case printer is 157315648Sjoerg * connected via a terminal server 157415648Sjoerg */ 157515648Sjoerg delay(500); 157615648Sjoerg break; 157715648Sjoerg } 157815648Sjoerg if (i == 1) { 157931492Swollman if (resp < 0) 158031492Swollman pstatus(pp, "waiting for %s to come up", 158131492Swollman pp->lp); 158231492Swollman else 158331492Swollman pstatus(pp, 158431492Swollman "waiting for access to printer on %s", 158531492Swollman pp->lp); 158615648Sjoerg } 158715648Sjoerg sleep(i); 158815648Sjoerg } 158931492Swollman pstatus(pp, "sending to %s port %d", ep, port); 159015648Sjoerg} 159115648Sjoerg 159215648Sjoerg/* 159315648Sjoerg * Printer is connected to an RS232 port on this host 159415648Sjoerg */ 159515648Sjoergstatic void 159631492Swollmanopentty(pp) 159731492Swollman const struct printer *pp; 159815648Sjoerg{ 159915648Sjoerg register int i; 160015648Sjoerg 160115648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 160231492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 160315648Sjoerg if (pfd >= 0) { 160415648Sjoerg delay(500); 160515648Sjoerg break; 160615648Sjoerg } 160715648Sjoerg if (errno == ENOENT) { 160831492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 160915648Sjoerg exit(1); 161015648Sjoerg } 161115648Sjoerg if (i == 1) 161231492Swollman pstatus(pp, 161331492Swollman "waiting for %s to become ready (offline?)", 161431492Swollman pp->printer); 161515648Sjoerg sleep(i); 161615648Sjoerg } 161715648Sjoerg if (isatty(pfd)) 161831492Swollman setty(pp); 161931492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 162015648Sjoerg} 162115648Sjoerg 162215648Sjoerg/* 162315648Sjoerg * Printer is on a remote host 162415648Sjoerg */ 162515648Sjoergstatic void 162631492Swollmanopenrem(pp) 162731492Swollman const struct printer *pp; 162815648Sjoerg{ 162931492Swollman register int i; 163027748Simp int resp; 163130407Sjoerg void (*savealrm)(int); 163215648Sjoerg 163315648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 163415648Sjoerg resp = -1; 163530407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 163631492Swollman alarm(pp->conn_timeout); 163731492Swollman pfd = getport(pp, pp->remote_host, 0); 163831020Sjoerg alarm(0); 163930407Sjoerg (void)signal(SIGALRM, savealrm); 164015648Sjoerg if (pfd >= 0) { 164131492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 164231492Swollman (char *)0) 164331492Swollman == 2 + strlen(pp->remote_queue)) 164431492Swollman && (resp = response(pp)) == 0) 164515648Sjoerg break; 164615648Sjoerg (void) close(pfd); 164715648Sjoerg } 164815648Sjoerg if (i == 1) { 164915648Sjoerg if (resp < 0) 165031492Swollman pstatus(pp, "waiting for %s to come up", 165131492Swollman pp->remote_host); 165215648Sjoerg else { 165331492Swollman pstatus(pp, 165431492Swollman "waiting for queue to be enabled on %s", 165531492Swollman pp->remote_host); 165615648Sjoerg i = 256; 165715648Sjoerg } 165815648Sjoerg } 165915648Sjoerg sleep(i); 166015648Sjoerg } 166131492Swollman pstatus(pp, "sending to %s", pp->remote_host); 166215648Sjoerg} 166315648Sjoerg 16641553Srgrimes/* 16651553Srgrimes * setup tty lines. 16661553Srgrimes */ 16671553Srgrimesstatic void 166831492Swollmansetty(pp) 166931492Swollman const struct printer *pp; 16701553Srgrimes{ 167115032Ssef struct termios ttybuf; 16721553Srgrimes 16731553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 167431492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 16751553Srgrimes exit(1); 16761553Srgrimes } 167715032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 167831492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 16791553Srgrimes exit(1); 16801553Srgrimes } 168131492Swollman if (pp->baud_rate > 0) 168231492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 168331492Swollman if (pp->mode_set) { 168431492Swollman char *s = strdup(pp->mode_set), *tmp; 168515032Ssef 168631492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 168739084Swollman (void) msearch(tmp, &ttybuf); 16881553Srgrimes } 16891553Srgrimes } 169031492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 169115032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 169231492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 16931553Srgrimes } 16941553Srgrimes } 16951553Srgrimes} 16961553Srgrimes 169727757Simp#ifdef __STDC__ 16981553Srgrimes#include <stdarg.h> 16991553Srgrimes#else 17001553Srgrimes#include <varargs.h> 17011553Srgrimes#endif 17021553Srgrimes 170315648Sjoergstatic void 170427757Simp#ifdef __STDC__ 170531492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 17061553Srgrimes#else 170731492Swollmanpstatus(pp, msg, va_alist) 170831492Swollman const struct printer *pp; 17091553Srgrimes char *msg; 17101553Srgrimes va_dcl 17111553Srgrimes#endif 17121553Srgrimes{ 171331492Swollman int fd; 171431492Swollman char *buf; 17151553Srgrimes va_list ap; 171627757Simp#ifdef __STDC__ 17171553Srgrimes va_start(ap, msg); 17181553Srgrimes#else 17191553Srgrimes va_start(ap); 17201553Srgrimes#endif 17211553Srgrimes 17221553Srgrimes umask(0); 172331492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 172431492Swollman if (fd < 0) { 172531492Swollman syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file); 17261553Srgrimes exit(1); 17271553Srgrimes } 17281553Srgrimes ftruncate(fd, 0); 172931492Swollman vasprintf(&buf, msg, ap); 17301553Srgrimes va_end(ap); 173131492Swollman writel(fd, buf, "\n", (char *)0); 173231492Swollman close(fd); 173331492Swollman free(buf); 17341553Srgrimes} 173530407Sjoerg 173630407Sjoergvoid 173730407Sjoergalarmhandler(signo) 173830407Sjoerg{ 173930407Sjoerg /* ignored */ 174030407Sjoerg} 1741