printjob.c revision 119192
11553Srgrimes/* 21553Srgrimes * Copyright (c) 1983, 1993 31553Srgrimes * The Regents of the University of California. All rights reserved. 41553Srgrimes * 51553Srgrimes * 61553Srgrimes * Redistribution and use in source and binary forms, with or without 71553Srgrimes * modification, are permitted provided that the following conditions 81553Srgrimes * are met: 91553Srgrimes * 1. Redistributions of source code must retain the above copyright 101553Srgrimes * notice, this list of conditions and the following disclaimer. 111553Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 121553Srgrimes * notice, this list of conditions and the following disclaimer in the 131553Srgrimes * documentation and/or other materials provided with the distribution. 141553Srgrimes * 3. All advertising materials mentioning features or use of this software 151553Srgrimes * must display the following acknowledgement: 161553Srgrimes * This product includes software developed by the University of 171553Srgrimes * California, Berkeley and its contributors. 181553Srgrimes * 4. Neither the name of the University nor the names of its contributors 191553Srgrimes * may be used to endorse or promote products derived from this software 201553Srgrimes * without specific prior written permission. 211553Srgrimes * 221553Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 231553Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 241553Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 251553Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 261553Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 271553Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 281553Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 291553Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 301553Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 311553Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 321553Srgrimes * SUCH DAMAGE. 331553Srgrimes */ 341553Srgrimes 351553Srgrimes#ifndef lint 3631492Swollmanstatic const char copyright[] = 371553Srgrimes"@(#) Copyright (c) 1983, 1993\n\ 381553Srgrimes The Regents of the University of California. All rights reserved.\n"; 391553Srgrimes#endif /* not lint */ 401553Srgrimes 41117554Sgad#if 0 42117587Sgad#ifndef lint 4315648Sjoergstatic char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95"; 44117587Sgad#endif /* not lint */ 45117554Sgad#endif 461553Srgrimes 47117554Sgad#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */ 48117554Sgad__FBSDID("$FreeBSD: head/usr.sbin/lpr/lpd/printjob.c 119192 2003-08-21 03:43:48Z gad $"); 491553Srgrimes 501553Srgrimes/* 511553Srgrimes * printjob -- print jobs in the queue. 521553Srgrimes * 531553Srgrimes * NOTE: the lock file is used to pass information to lpq and lprm. 541553Srgrimes * it does not need to be removed because file locks are dynamic. 551553Srgrimes */ 561553Srgrimes 571553Srgrimes#include <sys/param.h> 581553Srgrimes#include <sys/wait.h> 591553Srgrimes#include <sys/stat.h> 601553Srgrimes#include <sys/types.h> 611553Srgrimes 621553Srgrimes#include <pwd.h> 631553Srgrimes#include <unistd.h> 641553Srgrimes#include <signal.h> 651553Srgrimes#include <syslog.h> 661553Srgrimes#include <fcntl.h> 671553Srgrimes#include <dirent.h> 681553Srgrimes#include <errno.h> 691553Srgrimes#include <stdio.h> 701553Srgrimes#include <string.h> 711553Srgrimes#include <stdlib.h> 7215032Ssef#include <sys/ioctl.h> 7315032Ssef#include <termios.h> 7415703Sjoerg#include <time.h> 751553Srgrimes#include "lp.h" 761553Srgrimes#include "lp.local.h" 771553Srgrimes#include "pathnames.h" 781553Srgrimes#include "extern.h" 791553Srgrimes 8080230Sgad#define DORETURN 0 /* dofork should return "can't fork" error */ 8180230Sgad#define DOABORT 1 /* dofork should just die if fork() fails */ 821553Srgrimes 831553Srgrimes/* 84119192Sgad * The buffer size to use when reading/writing spool files. 85119192Sgad */ 86119192Sgad#define SPL_BUFSIZ BUFSIZ 87119192Sgad 88119192Sgad/* 891553Srgrimes * Error tokens 901553Srgrimes */ 911553Srgrimes#define REPRINT -2 921553Srgrimes#define ERROR -1 931553Srgrimes#define OK 0 941553Srgrimes#define FATALERR 1 951553Srgrimes#define NOACCT 2 961553Srgrimes#define FILTERERR 3 971553Srgrimes#define ACCESS 4 981553Srgrimes 991553Srgrimesstatic dev_t fdev; /* device of file pointed to by symlink */ 1001553Srgrimesstatic ino_t fino; /* inode of file pointed to by symlink */ 1011553Srgrimesstatic FILE *cfp; /* control file */ 10297793Sgadstatic pid_t of_pid; /* process id of output filter, if any */ 1031553Srgrimesstatic int child; /* id of any filters */ 10468253Sgadstatic int job_dfcnt; /* count of datafiles in current user job */ 1051553Srgrimesstatic int lfd; /* lock file descriptor */ 1061553Srgrimesstatic int ofd; /* output filter file descriptor */ 10724831Sbrianstatic int tfd = -1; /* output filter temp file output */ 1081553Srgrimesstatic int pfd; /* prstatic inter file descriptor */ 1091553Srgrimesstatic int prchild; /* id of pr process */ 1101553Srgrimesstatic char title[80]; /* ``pr'' title */ 11153956Sachestatic char locale[80]; /* ``pr'' locale */ 1121553Srgrimes 11380230Sgad/* these two are set from pp->daemon_user, but only if they are needed */ 11480230Sgadstatic char *daemon_uname; /* set from pwd->pw_name */ 11580230Sgadstatic int daemon_defgid; 11680230Sgad 1171553Srgrimesstatic char class[32]; /* classification field */ 11878300Sgadstatic char origin_host[MAXHOSTNAMELEN]; /* user's host machine */ 1191553Srgrimes /* indentation size in static characters */ 1208857Srgrimesstatic char indent[10] = "-i0"; 1211553Srgrimesstatic char jobname[100]; /* job or file name */ 1221553Srgrimesstatic char length[10] = "-l"; /* page length in lines */ 1231553Srgrimesstatic char logname[32]; /* user's login name */ 1241553Srgrimesstatic char pxlength[10] = "-y"; /* page length in pixels */ 1251553Srgrimesstatic char pxwidth[10] = "-x"; /* page width in pixels */ 12668664Sgad/* tempstderr is the filename used to catch stderr from exec-ing filters */ 12768664Sgadstatic char tempstderr[] = "errs.XXXXXXX"; 1281553Srgrimesstatic char width[10] = "-w"; /* page width in static characters */ 12924831Sbrian#define TFILENAME "fltXXXXXX" 13024831Sbrianstatic char tfile[] = TFILENAME; /* file name for filter output */ 1311553Srgrimes 13278146Sgadstatic void abortpr(int _signo); 13378146Sgadstatic void alarmhandler(int _signo); 13478146Sgadstatic void banner(struct printer *_pp, char *_name1, char *_name2); 13578146Sgadstatic int dofork(const struct printer *_pp, int _action); 13678146Sgadstatic int dropit(int _c); 13794032Sgadstatic int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av, 13894032Sgad int _infd, int _outfd); 13978146Sgadstatic void init(struct printer *_pp); 14078146Sgadstatic void openpr(const struct printer *_pp); 14178146Sgadstatic void opennet(const struct printer *_pp); 14278146Sgadstatic void opentty(const struct printer *_pp); 14378146Sgadstatic void openrem(const struct printer *pp); 14478146Sgadstatic int print(struct printer *_pp, int _format, char *_file); 14578146Sgadstatic int printit(struct printer *_pp, char *_file); 14679739Sgadstatic void pstatus(const struct printer *_pp, const char *_msg, ...) 14779739Sgad __printflike(2, 3); 14878146Sgadstatic char response(const struct printer *_pp); 14978146Sgadstatic void scan_out(struct printer *_pp, int _scfd, char *_scsp, 15078146Sgad int _dlm); 15178146Sgadstatic char *scnline(int _key, char *_p, int _c); 15278146Sgadstatic int sendfile(struct printer *_pp, int _type, char *_file, 15395293Sgad char _format, int _copyreq); 15478146Sgadstatic int sendit(struct printer *_pp, char *_file); 15594038Sgadstatic void sendmail(struct printer *_pp, char *_userid, int _bombed); 15678146Sgadstatic void setty(const struct printer *_pp); 1571553Srgrimes 1581553Srgrimesvoid 15978146Sgadprintjob(struct printer *pp) 1601553Srgrimes{ 1611553Srgrimes struct stat stb; 16268401Sgad register struct jobqueue *q, **qp; 16368401Sgad struct jobqueue **queue; 1641553Srgrimes register int i, nitems; 16568733Sgad off_t pidoff; 16697793Sgad pid_t printpid; 16768733Sgad int errcnt, jobcount, tempfd; 1681553Srgrimes 16968733Sgad jobcount = 0; 17031492Swollman init(pp); /* set up capabilities */ 171118881Sgad (void) write(STDOUT_FILENO, "", 1); /* ack that daemon is started */ 172118881Sgad (void) close(STDERR_FILENO); /* set up log file */ 17331492Swollman if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) { 17497792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 17597792Sgad pp->log_file); 1761553Srgrimes (void) open(_PATH_DEVNULL, O_WRONLY); 1771553Srgrimes } 1781553Srgrimes setgid(getegid()); 17997793Sgad printpid = getpid(); /* for use with lprm */ 18097793Sgad setpgrp(0, printpid); 18179735Sgad 18279735Sgad /* 18379735Sgad * At initial lpd startup, printjob may be called with various 18479735Sgad * signal handlers in effect. After that initial startup, any 18579735Sgad * calls to printjob will have a *different* set of signal-handlers 18679735Sgad * in effect. Make sure all handlers are the ones we want. 18779735Sgad */ 18879735Sgad signal(SIGCHLD, SIG_DFL); 1891553Srgrimes signal(SIGHUP, abortpr); 1901553Srgrimes signal(SIGINT, abortpr); 1911553Srgrimes signal(SIGQUIT, abortpr); 1921553Srgrimes signal(SIGTERM, abortpr); 1931553Srgrimes 1941553Srgrimes /* 1951553Srgrimes * uses short form file names 1961553Srgrimes */ 19731492Swollman if (chdir(pp->spool_dir) < 0) { 19897792Sgad syslog(LOG_ERR, "%s: chdir(%s): %m", pp->printer, 19997792Sgad pp->spool_dir); 2001553Srgrimes exit(1); 2011553Srgrimes } 20231492Swollman if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS)) 2031553Srgrimes exit(0); /* printing disabled */ 20431492Swollman lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK, 20531492Swollman LOCK_FILE_MODE); 2061553Srgrimes if (lfd < 0) { 20731492Swollman if (errno == EWOULDBLOCK) /* active daemon present */ 20831492Swollman exit(0); 20997792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 21097792Sgad pp->lock_file); 2111553Srgrimes exit(1); 2121553Srgrimes } 21331492Swollman /* turn off non-blocking mode (was turned on for lock effects only) */ 21431492Swollman if (fcntl(lfd, F_SETFL, 0) < 0) { 21597792Sgad syslog(LOG_ERR, "%s: fcntl(%s): %m", pp->printer, 21697792Sgad pp->lock_file); 2171553Srgrimes exit(1); 2181553Srgrimes } 2191553Srgrimes ftruncate(lfd, 0); 2201553Srgrimes /* 2211553Srgrimes * write process id for others to know 2221553Srgrimes */ 22397793Sgad sprintf(line, "%u\n", printpid); 2241553Srgrimes pidoff = i = strlen(line); 2251553Srgrimes if (write(lfd, line, i) != i) { 22697792Sgad syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, 22797792Sgad pp->lock_file); 2281553Srgrimes exit(1); 2291553Srgrimes } 2301553Srgrimes /* 2311553Srgrimes * search the spool directory for work and sort by queue order. 2321553Srgrimes */ 23331492Swollman if ((nitems = getq(pp, &queue)) < 0) { 23431492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 23597792Sgad pp->spool_dir); 2361553Srgrimes exit(1); 2371553Srgrimes } 2381553Srgrimes if (nitems == 0) /* no work to do */ 2391553Srgrimes exit(0); 24031492Swollman if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */ 24131492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0) 24297792Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 24397792Sgad pp->lock_file); 2441553Srgrimes } 24568664Sgad 24668664Sgad /* create a file which will be used to hold stderr from filters */ 24768664Sgad if ((tempfd = mkstemp(tempstderr)) == -1) { 24868664Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 24997792Sgad tempstderr); 25068732Sgad exit(1); 25168664Sgad } 25268664Sgad if ((i = fchmod(tempfd, 0664)) == -1) { 25368664Sgad syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer, 25497792Sgad tempstderr); 25568732Sgad exit(1); 25668664Sgad } 25768664Sgad /* lpd doesn't need it to be open, it just needs it to exist */ 25868664Sgad close(tempfd); 25968664Sgad 26031492Swollman openpr(pp); /* open printer or remote */ 2611553Srgrimesagain: 2621553Srgrimes /* 2631553Srgrimes * we found something to do now do it -- 2641553Srgrimes * write the name of the current control file into the lock file 2651553Srgrimes * so the spool queue program can tell what we're working on 2661553Srgrimes */ 2671553Srgrimes for (qp = queue; nitems--; free((char *) q)) { 2681553Srgrimes q = *qp++; 26968401Sgad if (stat(q->job_cfname, &stb) < 0) 2701553Srgrimes continue; 27115648Sjoerg errcnt = 0; 2721553Srgrimes restart: 27315648Sjoerg (void) lseek(lfd, pidoff, 0); 27468401Sgad (void) snprintf(line, sizeof(line), "%s\n", q->job_cfname); 2751553Srgrimes i = strlen(line); 2761553Srgrimes if (write(lfd, line, i) != i) 27797792Sgad syslog(LOG_ERR, "%s: write(%s): %m", pp->printer, 27897792Sgad pp->lock_file); 27931492Swollman if (!pp->remote) 28068401Sgad i = printit(pp, q->job_cfname); 2811553Srgrimes else 28268401Sgad i = sendit(pp, q->job_cfname); 2831553Srgrimes /* 2841553Srgrimes * Check to see if we are supposed to stop printing or 2851553Srgrimes * if we are to rebuild the queue. 2861553Srgrimes */ 2871553Srgrimes if (fstat(lfd, &stb) == 0) { 2881553Srgrimes /* stop printing before starting next job? */ 28931492Swollman if (stb.st_mode & LFM_PRINT_DIS) 2901553Srgrimes goto done; 2911553Srgrimes /* rebuild queue (after lpc topq) */ 29231492Swollman if (stb.st_mode & LFM_RESET_QUE) { 29331492Swollman for (free(q); nitems--; free(q)) 2941553Srgrimes q = *qp++; 29531492Swollman if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) 29631492Swollman < 0) 29797792Sgad syslog(LOG_WARNING, 29897792Sgad "%s: fchmod(%s): %m", 29997792Sgad pp->printer, pp->lock_file); 3001553Srgrimes break; 3011553Srgrimes } 3021553Srgrimes } 30368733Sgad if (i == OK) /* all files of this job printed */ 30468733Sgad jobcount++; 30515648Sjoerg else if (i == REPRINT && ++errcnt < 5) { 30615648Sjoerg /* try reprinting the job */ 30731492Swollman syslog(LOG_INFO, "restarting %s", pp->printer); 30897793Sgad if (of_pid > 0) { 30997793Sgad kill(of_pid, SIGCONT); /* to be sure */ 3101553Srgrimes (void) close(ofd); 31197793Sgad while ((i = wait(NULL)) > 0 && i != of_pid) 3121553Srgrimes ; 31379735Sgad if (i < 0) 31479735Sgad syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m", 31597793Sgad pp->printer, of_pid); 31697793Sgad of_pid = 0; 3171553Srgrimes } 3181553Srgrimes (void) close(pfd); /* close printer */ 3191553Srgrimes if (ftruncate(lfd, pidoff) < 0) 32097792Sgad syslog(LOG_WARNING, "%s: ftruncate(%s): %m", 32197792Sgad pp->printer, pp->lock_file); 32231492Swollman openpr(pp); /* try to reopen printer */ 3231553Srgrimes goto restart; 32415648Sjoerg } else { 32531492Swollman syslog(LOG_WARNING, "%s: job could not be %s (%s)", 32697792Sgad pp->printer, 32797792Sgad pp->remote ? "sent to remote host" : "printed", 32897792Sgad q->job_cfname); 32915648Sjoerg if (i == REPRINT) { 33027748Simp /* ensure we don't attempt this job again */ 33168401Sgad (void) unlink(q->job_cfname); 33268401Sgad q->job_cfname[0] = 'd'; 33368401Sgad (void) unlink(q->job_cfname); 33415648Sjoerg if (logname[0]) 33531492Swollman sendmail(pp, logname, FATALERR); 33615648Sjoerg } 3371553Srgrimes } 3381553Srgrimes } 33931492Swollman free(queue); 3401553Srgrimes /* 3411553Srgrimes * search the spool directory for more work. 3421553Srgrimes */ 34331492Swollman if ((nitems = getq(pp, &queue)) < 0) { 34431492Swollman syslog(LOG_ERR, "%s: can't scan %s", pp->printer, 34597792Sgad pp->spool_dir); 3461553Srgrimes exit(1); 3471553Srgrimes } 3481553Srgrimes if (nitems == 0) { /* no more work to do */ 3491553Srgrimes done: 35068733Sgad if (jobcount > 0) { /* jobs actually printed */ 35131492Swollman if (!pp->no_formfeed && !pp->tof) 35231492Swollman (void) write(ofd, pp->form_feed, 35331492Swollman strlen(pp->form_feed)); 35431492Swollman if (pp->trailer != NULL) /* output trailer */ 35531492Swollman (void) write(ofd, pp->trailer, 35631492Swollman strlen(pp->trailer)); 3571553Srgrimes } 35819202Simp (void) close(ofd); 35919202Simp (void) wait(NULL); 36068664Sgad (void) unlink(tempstderr); 3611553Srgrimes exit(0); 3621553Srgrimes } 3631553Srgrimes goto again; 3641553Srgrimes} 3651553Srgrimes 3661553Srgrimeschar fonts[4][50]; /* fonts for troff */ 3671553Srgrimes 3681553Srgrimeschar ifonts[4][40] = { 3691553Srgrimes _PATH_VFONTR, 3701553Srgrimes _PATH_VFONTI, 3711553Srgrimes _PATH_VFONTB, 3721553Srgrimes _PATH_VFONTS, 3731553Srgrimes}; 3741553Srgrimes 3751553Srgrimes/* 3761553Srgrimes * The remaining part is the reading of the control file (cf) 3771553Srgrimes * and performing the various actions. 3781553Srgrimes */ 3791553Srgrimesstatic int 38078146Sgadprintit(struct printer *pp, char *file) 3811553Srgrimes{ 3821553Srgrimes register int i; 38368734Sgad char *cp; 38468734Sgad int bombed, didignorehdr; 3851553Srgrimes 38668734Sgad bombed = OK; 38768734Sgad didignorehdr = 0; 3881553Srgrimes /* 3891553Srgrimes * open control file; ignore if no longer there. 3901553Srgrimes */ 3911553Srgrimes if ((cfp = fopen(file, "r")) == NULL) { 39297792Sgad syslog(LOG_INFO, "%s: fopen(%s): %m", pp->printer, file); 39397791Sgad return (OK); 3941553Srgrimes } 3951553Srgrimes /* 3961553Srgrimes * Reset troff fonts. 3971553Srgrimes */ 3981553Srgrimes for (i = 0; i < 4; i++) 3991553Srgrimes strcpy(fonts[i], ifonts[i]); 40031492Swollman sprintf(&width[2], "%ld", pp->page_width); 4011553Srgrimes strcpy(indent+2, "0"); 4021553Srgrimes 40368253Sgad /* initialize job-specific count of datafiles processed */ 40468253Sgad job_dfcnt = 0; 40568253Sgad 4061553Srgrimes /* 4071553Srgrimes * read the control file for work to do 4081553Srgrimes * 4091553Srgrimes * file format -- first character in the line is a command 4101553Srgrimes * rest of the line is the argument. 4111553Srgrimes * valid commands are: 4121553Srgrimes * 4131553Srgrimes * S -- "stat info" for symbolic link protection 4141553Srgrimes * J -- "job name" on banner page 4151553Srgrimes * C -- "class name" on banner page 4161553Srgrimes * L -- "literal" user's name to print on banner 4171553Srgrimes * T -- "title" for pr 4181553Srgrimes * H -- "host name" of machine where lpr was done 4191553Srgrimes * P -- "person" user's login name 4201553Srgrimes * I -- "indent" amount to indent output 42115648Sjoerg * R -- laser dpi "resolution" 4221553Srgrimes * f -- "file name" name of text file to print 4231553Srgrimes * l -- "file name" text file with control chars 42483684Sgad * o -- "file name" postscript file, according to 42583684Sgad * the RFC. Here it is treated like an 'f'. 4261553Srgrimes * p -- "file name" text file to print with pr(1) 4271553Srgrimes * t -- "file name" troff(1) file to print 4281553Srgrimes * n -- "file name" ditroff(1) file to print 4291553Srgrimes * d -- "file name" dvi file to print 4301553Srgrimes * g -- "file name" plot(1G) file to print 4311553Srgrimes * v -- "file name" plain raster file to print 4321553Srgrimes * c -- "file name" cifplot file to print 4331553Srgrimes * 1 -- "R font file" for troff 4341553Srgrimes * 2 -- "I font file" for troff 4351553Srgrimes * 3 -- "B font file" for troff 4361553Srgrimes * 4 -- "S font file" for troff 4371553Srgrimes * N -- "name" of file (used by lpq) 4381553Srgrimes * U -- "unlink" name of file to remove 4391553Srgrimes * (after we print it. (Pass 2 only)). 4401553Srgrimes * M -- "mail" to user when done printing 44153956Sache * Z -- "locale" for pr 4421553Srgrimes * 4431553Srgrimes * getline reads a line and expands tabs to blanks 4441553Srgrimes */ 4451553Srgrimes 4461553Srgrimes /* pass 1 */ 4471553Srgrimes 4481553Srgrimes while (getline(cfp)) 4491553Srgrimes switch (line[0]) { 4501553Srgrimes case 'H': 45178300Sgad strlcpy(origin_host, line + 1, sizeof(origin_host)); 45227748Simp if (class[0] == '\0') { 45380133Sgad strlcpy(class, line+1, sizeof(class)); 45427748Simp } 4551553Srgrimes continue; 4561553Srgrimes 4571553Srgrimes case 'P': 45880133Sgad strlcpy(logname, line + 1, sizeof(logname)); 45931492Swollman if (pp->restricted) { /* restricted */ 4601553Srgrimes if (getpwnam(logname) == NULL) { 4611553Srgrimes bombed = NOACCT; 46231492Swollman sendmail(pp, line+1, bombed); 4631553Srgrimes goto pass2; 4641553Srgrimes } 4651553Srgrimes } 4661553Srgrimes continue; 4671553Srgrimes 4681553Srgrimes case 'S': 4691553Srgrimes cp = line+1; 4701553Srgrimes i = 0; 4711553Srgrimes while (*cp >= '0' && *cp <= '9') 4721553Srgrimes i = i * 10 + (*cp++ - '0'); 4731553Srgrimes fdev = i; 4741553Srgrimes cp++; 4751553Srgrimes i = 0; 4761553Srgrimes while (*cp >= '0' && *cp <= '9') 4771553Srgrimes i = i * 10 + (*cp++ - '0'); 4781553Srgrimes fino = i; 4791553Srgrimes continue; 4801553Srgrimes 4811553Srgrimes case 'J': 48227748Simp if (line[1] != '\0') { 48380133Sgad strlcpy(jobname, line + 1, sizeof(jobname)); 48427748Simp } else 4851553Srgrimes strcpy(jobname, " "); 4861553Srgrimes continue; 4871553Srgrimes 4881553Srgrimes case 'C': 4891553Srgrimes if (line[1] != '\0') 49080133Sgad strlcpy(class, line + 1, sizeof(class)); 49180133Sgad else if (class[0] == '\0') { 49280133Sgad /* XXX - why call gethostname instead of 49380133Sgad * just strlcpy'ing local_host? */ 4941553Srgrimes gethostname(class, sizeof(class)); 49580133Sgad class[sizeof(class) - 1] = '\0'; 49680133Sgad } 4971553Srgrimes continue; 4981553Srgrimes 4991553Srgrimes case 'T': /* header title for pr */ 50080133Sgad strlcpy(title, line + 1, sizeof(title)); 5011553Srgrimes continue; 5021553Srgrimes 5031553Srgrimes case 'L': /* identification line */ 50431492Swollman if (!pp->no_header && !pp->header_last) 50531492Swollman banner(pp, line+1, jobname); 5061553Srgrimes continue; 5071553Srgrimes 5081553Srgrimes case '1': /* troff fonts */ 5091553Srgrimes case '2': 5101553Srgrimes case '3': 5111553Srgrimes case '4': 51227748Simp if (line[1] != '\0') { 51380133Sgad strlcpy(fonts[line[0]-'1'], line + 1, 51480133Sgad (size_t)50); 51527748Simp } 5161553Srgrimes continue; 5171553Srgrimes 5181553Srgrimes case 'W': /* page width */ 51980133Sgad strlcpy(width+2, line + 1, sizeof(width) - 2); 5201553Srgrimes continue; 5211553Srgrimes 5221553Srgrimes case 'I': /* indent amount */ 52380133Sgad strlcpy(indent+2, line + 1, sizeof(indent) - 2); 5241553Srgrimes continue; 5251553Srgrimes 52653956Sache case 'Z': /* locale for pr */ 52780133Sgad strlcpy(locale, line + 1, sizeof(locale)); 52853956Sache continue; 52953956Sache 5301553Srgrimes default: /* some file to print */ 53168467Sgad /* only lowercase cmd-codes include a file-to-print */ 53268467Sgad if ((line[0] < 'a') || (line[0] > 'z')) { 53368467Sgad /* ignore any other lines */ 53468467Sgad if (lflag <= 1) 53568467Sgad continue; 53668467Sgad if (!didignorehdr) { 53768467Sgad syslog(LOG_INFO, "%s: in %s :", 53897792Sgad pp->printer, file); 53968467Sgad didignorehdr = 1; 54068467Sgad } 54168467Sgad syslog(LOG_INFO, "%s: ignoring line: '%c' %s", 54297792Sgad pp->printer, line[0], &line[1]); 54368467Sgad continue; 54468467Sgad } 54568467Sgad i = print(pp, line[0], line+1); 54668467Sgad switch (i) { 5471553Srgrimes case ERROR: 5481553Srgrimes if (bombed == OK) 5491553Srgrimes bombed = FATALERR; 5501553Srgrimes break; 5511553Srgrimes case REPRINT: 5521553Srgrimes (void) fclose(cfp); 55397791Sgad return (REPRINT); 5541553Srgrimes case FILTERERR: 5551553Srgrimes case ACCESS: 5561553Srgrimes bombed = i; 55731492Swollman sendmail(pp, logname, bombed); 5581553Srgrimes } 5591553Srgrimes title[0] = '\0'; 5601553Srgrimes continue; 5611553Srgrimes 5621553Srgrimes case 'N': 5631553Srgrimes case 'U': 5641553Srgrimes case 'M': 56515648Sjoerg case 'R': 5661553Srgrimes continue; 5671553Srgrimes } 5681553Srgrimes 5691553Srgrimes /* pass 2 */ 5701553Srgrimes 5711553Srgrimespass2: 5721553Srgrimes fseek(cfp, 0L, 0); 5731553Srgrimes while (getline(cfp)) 5741553Srgrimes switch (line[0]) { 5751553Srgrimes case 'L': /* identification line */ 57631492Swollman if (!pp->no_header && pp->header_last) 57731492Swollman banner(pp, line+1, jobname); 5781553Srgrimes continue; 5791553Srgrimes 5801553Srgrimes case 'M': 5811553Srgrimes if (bombed < NOACCT) /* already sent if >= NOACCT */ 58231492Swollman sendmail(pp, line+1, bombed); 5831553Srgrimes continue; 5841553Srgrimes 5851553Srgrimes case 'U': 58627748Simp if (strchr(line+1, '/')) 58727748Simp continue; 5881553Srgrimes (void) unlink(line+1); 5891553Srgrimes } 5901553Srgrimes /* 5911553Srgrimes * clean-up in case another control file exists 5921553Srgrimes */ 5931553Srgrimes (void) fclose(cfp); 5941553Srgrimes (void) unlink(file); 59597791Sgad return (bombed == OK ? OK : ERROR); 5961553Srgrimes} 5971553Srgrimes 5981553Srgrimes/* 5991553Srgrimes * Print a file. 6001553Srgrimes * Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}. 6011553Srgrimes * Return -1 if a non-recoverable error occured, 6021553Srgrimes * 2 if the filter detected some errors (but printed the job anyway), 6031553Srgrimes * 1 if we should try to reprint this job and 6041553Srgrimes * 0 if all is well. 6051553Srgrimes * Note: all filters take stdin as the file, stdout as the printer, 6061553Srgrimes * stderr as the log file, and must not ignore SIGINT. 6071553Srgrimes */ 6081553Srgrimesstatic int 60978146Sgadprint(struct printer *pp, int format, char *file) 6101553Srgrimes{ 61153956Sache register int n, i; 6121553Srgrimes register char *prog; 61331492Swollman int fi, fo; 6141553Srgrimes FILE *fp; 615119192Sgad char *av[15], buf[SPL_BUFSIZ]; 61697793Sgad pid_t wpid; 61797793Sgad int p[2], retcode, stopped, wstatus, wstatus_set; 6181553Srgrimes struct stat stb; 6191553Srgrimes 62068467Sgad if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) { 62168467Sgad syslog(LOG_INFO, "%s: unable to open %s ('%c' line)", 62297792Sgad pp->printer, file, format); 62397791Sgad return (ERROR); 62468467Sgad } 6251553Srgrimes /* 6261553Srgrimes * Check to see if data file is a symbolic link. If so, it should 6271553Srgrimes * still point to the same file or someone is trying to print 6281553Srgrimes * something he shouldn't. 6291553Srgrimes */ 6301553Srgrimes if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 && 6311553Srgrimes (stb.st_dev != fdev || stb.st_ino != fino)) 63297791Sgad return (ACCESS); 63368253Sgad 63468253Sgad job_dfcnt++; /* increment datafile counter for this job */ 63568734Sgad stopped = 0; /* output filter is not stopped */ 63668253Sgad 63768253Sgad /* everything seems OK, start it up */ 63831492Swollman if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */ 63931492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 64031492Swollman pp->tof = 1; 6411553Srgrimes } 64231492Swollman if (pp->filters[LPF_INPUT] == NULL 64383684Sgad && (format == 'f' || format == 'l' || format == 'o')) { 64431492Swollman pp->tof = 0; 645119192Sgad while ((n = read(fi, buf, SPL_BUFSIZ)) > 0) 6461553Srgrimes if (write(ofd, buf, n) != n) { 6471553Srgrimes (void) close(fi); 64897791Sgad return (REPRINT); 6491553Srgrimes } 6501553Srgrimes (void) close(fi); 65197791Sgad return (OK); 6521553Srgrimes } 6531553Srgrimes switch (format) { 6541553Srgrimes case 'p': /* print file using 'pr' */ 65531492Swollman if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */ 6561553Srgrimes prog = _PATH_PR; 65753956Sache i = 0; 65853956Sache av[i++] = "pr"; 65953956Sache av[i++] = width; 66053956Sache av[i++] = length; 66153956Sache av[i++] = "-h"; 66253956Sache av[i++] = *title ? title : " "; 66353956Sache av[i++] = "-L"; 66453956Sache av[i++] = *locale ? locale : "C"; 66553956Sache av[i++] = "-F"; 66653956Sache av[i] = 0; 6671553Srgrimes fo = ofd; 6681553Srgrimes goto start; 6691553Srgrimes } 6701553Srgrimes pipe(p); 67131492Swollman if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */ 672118881Sgad dup2(fi, STDIN_FILENO); /* file is stdin */ 673118881Sgad dup2(p[1], STDOUT_FILENO); /* pipe is stdout */ 6748094Sjkh closelog(); 67531492Swollman closeallfds(3); 6761553Srgrimes execl(_PATH_PR, "pr", width, length, 67753956Sache "-h", *title ? title : " ", 67853956Sache "-L", *locale ? locale : "C", 67979452Sbrian "-F", (char *)0); 6801553Srgrimes syslog(LOG_ERR, "cannot execl %s", _PATH_PR); 6811553Srgrimes exit(2); 6821553Srgrimes } 6831553Srgrimes (void) close(p[1]); /* close output side */ 6841553Srgrimes (void) close(fi); 6851553Srgrimes if (prchild < 0) { 6861553Srgrimes prchild = 0; 6871553Srgrimes (void) close(p[0]); 68897791Sgad return (ERROR); 6891553Srgrimes } 6901553Srgrimes fi = p[0]; /* use pipe for input */ 6911553Srgrimes case 'f': /* print plain text file */ 69231492Swollman prog = pp->filters[LPF_INPUT]; 6931553Srgrimes av[1] = width; 6941553Srgrimes av[2] = length; 6951553Srgrimes av[3] = indent; 6961553Srgrimes n = 4; 6971553Srgrimes break; 69886935Sgad case 'o': /* print postscript file */ 69986935Sgad /* 70086935Sgad * Treat this as a "plain file with control characters", and 70186935Sgad * assume the standard LPF_INPUT filter will recognize that 70286935Sgad * the data is postscript and know what to do with it. These 70386935Sgad * 'o'-file requests could come from MacOS 10.1 systems. 70486935Sgad * (later versions of MacOS 10 will explicitly use 'l') 70586935Sgad * A postscript file can contain binary data, which is why 'l' 70686935Sgad * is somewhat more appropriate than 'f'. 70786935Sgad */ 70886935Sgad /* FALLTHROUGH */ 7091553Srgrimes case 'l': /* like 'f' but pass control characters */ 71031492Swollman prog = pp->filters[LPF_INPUT]; 7111553Srgrimes av[1] = "-c"; 7121553Srgrimes av[2] = width; 7131553Srgrimes av[3] = length; 7141553Srgrimes av[4] = indent; 7151553Srgrimes n = 5; 7161553Srgrimes break; 7171553Srgrimes case 'r': /* print a fortran text file */ 71831492Swollman prog = pp->filters[LPF_FORTRAN]; 7191553Srgrimes av[1] = width; 7201553Srgrimes av[2] = length; 7211553Srgrimes n = 3; 7221553Srgrimes break; 7231553Srgrimes case 't': /* print troff output */ 7241553Srgrimes case 'n': /* print ditroff output */ 7251553Srgrimes case 'd': /* print tex output */ 7261553Srgrimes (void) unlink(".railmag"); 7271553Srgrimes if ((fo = creat(".railmag", FILMOD)) < 0) { 72831492Swollman syslog(LOG_ERR, "%s: cannot create .railmag", 72997792Sgad pp->printer); 7301553Srgrimes (void) unlink(".railmag"); 7311553Srgrimes } else { 7321553Srgrimes for (n = 0; n < 4; n++) { 7331553Srgrimes if (fonts[n][0] != '/') 7341553Srgrimes (void) write(fo, _PATH_VFONT, 7351553Srgrimes sizeof(_PATH_VFONT) - 1); 7361553Srgrimes (void) write(fo, fonts[n], strlen(fonts[n])); 7371553Srgrimes (void) write(fo, "\n", 1); 7381553Srgrimes } 7391553Srgrimes (void) close(fo); 7401553Srgrimes } 74131492Swollman prog = (format == 't') ? pp->filters[LPF_TROFF] 74231492Swollman : ((format == 'n') ? pp->filters[LPF_DITROFF] 74331492Swollman : pp->filters[LPF_DVI]); 7441553Srgrimes av[1] = pxwidth; 7451553Srgrimes av[2] = pxlength; 7461553Srgrimes n = 3; 7471553Srgrimes break; 7481553Srgrimes case 'c': /* print cifplot output */ 74931492Swollman prog = pp->filters[LPF_CIFPLOT]; 7501553Srgrimes av[1] = pxwidth; 7511553Srgrimes av[2] = pxlength; 7521553Srgrimes n = 3; 7531553Srgrimes break; 7541553Srgrimes case 'g': /* print plot(1G) output */ 75531492Swollman prog = pp->filters[LPF_GRAPH]; 7561553Srgrimes av[1] = pxwidth; 7571553Srgrimes av[2] = pxlength; 7581553Srgrimes n = 3; 7591553Srgrimes break; 7601553Srgrimes case 'v': /* print raster output */ 76131492Swollman prog = pp->filters[LPF_RASTER]; 7621553Srgrimes av[1] = pxwidth; 7631553Srgrimes av[2] = pxlength; 7641553Srgrimes n = 3; 7651553Srgrimes break; 7661553Srgrimes default: 7671553Srgrimes (void) close(fi); 7681553Srgrimes syslog(LOG_ERR, "%s: illegal format character '%c'", 76997792Sgad pp->printer, format); 77097791Sgad return (ERROR); 7711553Srgrimes } 77215648Sjoerg if (prog == NULL) { 77315648Sjoerg (void) close(fi); 77415648Sjoerg syslog(LOG_ERR, 77515648Sjoerg "%s: no filter found in printcap for format character '%c'", 77631492Swollman pp->printer, format); 77797791Sgad return (ERROR); 77815648Sjoerg } 77927635Simp if ((av[0] = strrchr(prog, '/')) != NULL) 7801553Srgrimes av[0]++; 7811553Srgrimes else 7821553Srgrimes av[0] = prog; 7831553Srgrimes av[n++] = "-n"; 7841553Srgrimes av[n++] = logname; 7851553Srgrimes av[n++] = "-h"; 78678300Sgad av[n++] = origin_host; 78731492Swollman av[n++] = pp->acct_file; 7881553Srgrimes av[n] = 0; 7891553Srgrimes fo = pfd; 79097793Sgad if (of_pid > 0) { /* stop output filter */ 7911553Srgrimes write(ofd, "\031\1", 2); 79297793Sgad while ((wpid = 79397793Sgad wait3(&wstatus, WUNTRACED, 0)) > 0 && wpid != of_pid) 7941553Srgrimes ; 79597793Sgad if (wpid < 0) 79697792Sgad syslog(LOG_WARNING, 79797792Sgad "%s: after stopping 'of', wait3() returned: %m", 79879735Sgad pp->printer); 79997781Sgad else if (!WIFSTOPPED(wstatus)) { 8001553Srgrimes (void) close(fi); 80197781Sgad syslog(LOG_WARNING, "%s: output filter died " 80297781Sgad "(pid=%d retcode=%d termsig=%d)", 80397793Sgad pp->printer, of_pid, WEXITSTATUS(wstatus), 80497781Sgad WTERMSIG(wstatus)); 80597791Sgad return (REPRINT); 8061553Srgrimes } 8071553Srgrimes stopped++; 8081553Srgrimes } 8091553Srgrimesstart: 81031492Swollman if ((child = dofork(pp, DORETURN)) == 0) { /* child */ 811118881Sgad dup2(fi, STDIN_FILENO); 812118881Sgad dup2(fo, STDOUT_FILENO); 81368664Sgad /* setup stderr for the filter (child process) 81468664Sgad * so it goes to our temporary errors file */ 81568664Sgad n = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 8161553Srgrimes if (n >= 0) 817118881Sgad dup2(n, STDERR_FILENO); 8188094Sjkh closelog(); 81931492Swollman closeallfds(3); 8201553Srgrimes execv(prog, av); 82195067Sgad syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, 82295067Sgad prog); 8231553Srgrimes exit(2); 8241553Srgrimes } 8251553Srgrimes (void) close(fi); 82697789Sgad wstatus_set = 0; 8271553Srgrimes if (child < 0) 82897781Sgad retcode = 100; 82979735Sgad else { 83097793Sgad while ((wpid = wait(&wstatus)) > 0 && wpid != child) 8311553Srgrimes ; 83297793Sgad if (wpid < 0) { 83397781Sgad retcode = 100; 83497792Sgad syslog(LOG_WARNING, 83597792Sgad "%s: after execv(%s), wait() returned: %m", 83679735Sgad pp->printer, prog); 83797789Sgad } else { 83897789Sgad wstatus_set = 1; 83997781Sgad retcode = WEXITSTATUS(wstatus); 84097789Sgad } 84179735Sgad } 8421553Srgrimes child = 0; 8431553Srgrimes prchild = 0; 8441553Srgrimes if (stopped) { /* restart output filter */ 84597793Sgad if (kill(of_pid, SIGCONT) < 0) { 8461553Srgrimes syslog(LOG_ERR, "cannot restart output filter"); 8471553Srgrimes exit(1); 8481553Srgrimes } 8491553Srgrimes } 85031492Swollman pp->tof = 0; 8511553Srgrimes 85268664Sgad /* Copy the filter's output to "lf" logfile */ 85368664Sgad if ((fp = fopen(tempstderr, "r"))) { 8541553Srgrimes while (fgets(buf, sizeof(buf), fp)) 8551553Srgrimes fputs(buf, stderr); 8561553Srgrimes fclose(fp); 8571553Srgrimes } 8581553Srgrimes 85997789Sgad if (wstatus_set && !WIFEXITED(wstatus)) { 86015648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)", 86197781Sgad pp->printer, format, WTERMSIG(wstatus)); 86297791Sgad return (ERROR); 8631553Srgrimes } 86497781Sgad switch (retcode) { 8651553Srgrimes case 0: 86631492Swollman pp->tof = 1; 86797791Sgad return (OK); 8681553Srgrimes case 1: 86997791Sgad return (REPRINT); 87015648Sjoerg case 2: 87197791Sgad return (ERROR); 8721553Srgrimes default: 87315648Sjoerg syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)", 87497792Sgad pp->printer, format, retcode); 87597791Sgad return (FILTERERR); 8761553Srgrimes } 8771553Srgrimes} 8781553Srgrimes 8791553Srgrimes/* 8801553Srgrimes * Send the daemon control file (cf) and any data files. 8811553Srgrimes * Return -1 if a non-recoverable error occured, 1 if a recoverable error and 8821553Srgrimes * 0 if all is well. 8831553Srgrimes */ 8841553Srgrimesstatic int 88578146Sgadsendit(struct printer *pp, char *file) 8861553Srgrimes{ 88795293Sgad int dfcopies, err, i; 888119192Sgad char *cp, last[sizeof(line)]; 8891553Srgrimes 8901553Srgrimes /* 8911553Srgrimes * open control file 8921553Srgrimes */ 8931553Srgrimes if ((cfp = fopen(file, "r")) == NULL) 89497791Sgad return (OK); 89568253Sgad 89668253Sgad /* initialize job-specific count of datafiles processed */ 89768253Sgad job_dfcnt = 0; 89868253Sgad 8991553Srgrimes /* 9001553Srgrimes * read the control file for work to do 9011553Srgrimes * 9021553Srgrimes * file format -- first character in the line is a command 9031553Srgrimes * rest of the line is the argument. 9041553Srgrimes * commands of interest are: 9051553Srgrimes * 9061553Srgrimes * a-z -- "file name" name of file to print 9071553Srgrimes * U -- "unlink" name of file to remove 9081553Srgrimes * (after we print it. (Pass 2 only)). 9091553Srgrimes */ 9101553Srgrimes 9111553Srgrimes /* 9121553Srgrimes * pass 1 9131553Srgrimes */ 91495293Sgad err = OK; 9151553Srgrimes while (getline(cfp)) { 9161553Srgrimes again: 9171553Srgrimes if (line[0] == 'S') { 9181553Srgrimes cp = line+1; 9191553Srgrimes i = 0; 9201553Srgrimes while (*cp >= '0' && *cp <= '9') 9211553Srgrimes i = i * 10 + (*cp++ - '0'); 9221553Srgrimes fdev = i; 9231553Srgrimes cp++; 9241553Srgrimes i = 0; 9251553Srgrimes while (*cp >= '0' && *cp <= '9') 9261553Srgrimes i = i * 10 + (*cp++ - '0'); 9271553Srgrimes fino = i; 92824831Sbrian } else if (line[0] == 'H') { 92978300Sgad strlcpy(origin_host, line + 1, sizeof(origin_host)); 93068343Sgad if (class[0] == '\0') { 93180133Sgad strlcpy(class, line + 1, sizeof(class)); 93268343Sgad } 93324831Sbrian } else if (line[0] == 'P') { 93480133Sgad strlcpy(logname, line + 1, sizeof(logname)); 93531492Swollman if (pp->restricted) { /* restricted */ 93624831Sbrian if (getpwnam(logname) == NULL) { 93731492Swollman sendmail(pp, line+1, NOACCT); 93824831Sbrian err = ERROR; 93924831Sbrian break; 94024831Sbrian } 94124831Sbrian } 94224831Sbrian } else if (line[0] == 'I') { 94380133Sgad strlcpy(indent+2, line + 1, sizeof(indent) - 2); 94424831Sbrian } else if (line[0] >= 'a' && line[0] <= 'z') { 94595293Sgad dfcopies = 1; 9461553Srgrimes strcpy(last, line); 94795293Sgad while ((i = getline(cfp)) != 0) { 94895293Sgad if (strcmp(last, line) != 0) 9491553Srgrimes break; 95095293Sgad dfcopies++; 95195293Sgad } 95295293Sgad switch (sendfile(pp, '\3', last+1, *last, dfcopies)) { 9531553Srgrimes case OK: 9541553Srgrimes if (i) 9551553Srgrimes goto again; 9561553Srgrimes break; 9571553Srgrimes case REPRINT: 9581553Srgrimes (void) fclose(cfp); 95997791Sgad return (REPRINT); 9601553Srgrimes case ACCESS: 96131492Swollman sendmail(pp, logname, ACCESS); 9621553Srgrimes case ERROR: 9631553Srgrimes err = ERROR; 9641553Srgrimes } 9651553Srgrimes break; 9661553Srgrimes } 9671553Srgrimes } 96895293Sgad if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) { 9691553Srgrimes (void) fclose(cfp); 97097791Sgad return (REPRINT); 9711553Srgrimes } 9721553Srgrimes /* 9731553Srgrimes * pass 2 9741553Srgrimes */ 9751553Srgrimes fseek(cfp, 0L, 0); 9761553Srgrimes while (getline(cfp)) 97727748Simp if (line[0] == 'U' && !strchr(line+1, '/')) 9781553Srgrimes (void) unlink(line+1); 9791553Srgrimes /* 9801553Srgrimes * clean-up in case another control file exists 9811553Srgrimes */ 9821553Srgrimes (void) fclose(cfp); 9831553Srgrimes (void) unlink(file); 98497791Sgad return (err); 9851553Srgrimes} 9861553Srgrimes 9871553Srgrimes/* 9881553Srgrimes * Send a data file to the remote machine and spool it. 9891553Srgrimes * Return positive if we should try resending. 9901553Srgrimes */ 9911553Srgrimesstatic int 99295293Sgadsendfile(struct printer *pp, int type, char *file, char format, int copyreq) 9931553Srgrimes{ 99494036Sgad int i, amt; 9951553Srgrimes struct stat stb; 99694032Sgad char *av[15], *filtcmd; 997119192Sgad char buf[SPL_BUFSIZ], opt_c[4], opt_h[4], opt_n[4]; 99895293Sgad int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc; 9991553Srgrimes 100074124Sgad statrc = lstat(file, &stb); 100174124Sgad if (statrc < 0) { 100274124Sgad syslog(LOG_ERR, "%s: error from lstat(%s): %m", 100374124Sgad pp->printer, file); 100494036Sgad return (ERROR); 100574124Sgad } 100694036Sgad sfd = open(file, O_RDONLY); 100794036Sgad if (sfd < 0) { 100874124Sgad syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m", 100974124Sgad pp->printer, file); 101094036Sgad return (ERROR); 101174124Sgad } 10121553Srgrimes /* 10131553Srgrimes * Check to see if data file is a symbolic link. If so, it should 10141553Srgrimes * still point to the same file or someone is trying to print something 10151553Srgrimes * he shouldn't. 10161553Srgrimes */ 101794036Sgad if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 && 101894036Sgad (stb.st_dev != fdev || stb.st_ino != fino)) { 101994036Sgad close(sfd); 102094036Sgad return (ACCESS); 102194036Sgad } 102224831Sbrian 102394032Sgad /* Everything seems OK for reading the file, now to send it */ 102494032Sgad filtcmd = NULL; 102524831Sbrian sizerr = 0; 102694032Sgad tfd = -1; 102724831Sbrian if (type == '\3') { 102894032Sgad /* 102994032Sgad * Type == 3 means this is a datafile, not a control file. 103094032Sgad * Increment the counter of data-files in this job, and 103194032Sgad * then check for input or output filters (which are only 103294032Sgad * applied to datafiles, not control files). 103394032Sgad */ 103494032Sgad job_dfcnt++; 103594032Sgad 103694032Sgad /* 103794032Sgad * Note that here we are filtering datafiles, one at a time, 103894032Sgad * as they are sent to the remote machine. Here, the *only* 103994032Sgad * difference between an input filter (`if=') and an output 104094032Sgad * filter (`of=') is the argument list that the filter is 104194032Sgad * started up with. Here, the output filter is executed 104294032Sgad * for each individual file as it is sent. This is not the 104394032Sgad * same as local print queues, where the output filter is 104494032Sgad * started up once, and then all jobs are passed thru that 104594032Sgad * single invocation of the output filter. 104694032Sgad * 104794032Sgad * Also note that a queue for a remote-machine can have an 104894032Sgad * input filter or an output filter, but not both. 104994032Sgad */ 105031492Swollman if (pp->filters[LPF_INPUT]) { 105194032Sgad filtcmd = pp->filters[LPF_INPUT]; 105294032Sgad av[0] = filtcmd; 105394032Sgad narg = 0; 105494032Sgad strcpy(opt_c, "-c"); 105594032Sgad strcpy(opt_h, "-h"); 105694032Sgad strcpy(opt_n, "-n"); 105724831Sbrian if (format == 'l') 105894032Sgad av[++narg] = opt_c; 105994032Sgad av[++narg] = width; 106094032Sgad av[++narg] = length; 106194032Sgad av[++narg] = indent; 106294032Sgad av[++narg] = opt_n; 106394032Sgad av[++narg] = logname; 106494032Sgad av[++narg] = opt_h; 106594032Sgad av[++narg] = origin_host; 106694032Sgad av[++narg] = pp->acct_file; 106794032Sgad av[++narg] = NULL; 106894032Sgad } else if (pp->filters[LPF_OUTPUT]) { 106994032Sgad filtcmd = pp->filters[LPF_OUTPUT]; 107094032Sgad av[0] = filtcmd; 107194032Sgad narg = 0; 107294032Sgad av[++narg] = width; 107394032Sgad av[++narg] = length; 107494032Sgad av[++narg] = NULL; 107524831Sbrian } 107624831Sbrian } 107794032Sgad if (filtcmd) { 107894032Sgad /* 107994032Sgad * If there is an input or output filter, we have to run 108094032Sgad * the datafile thru that filter and store the result as 108194032Sgad * a temporary spool file, because the protocol requires 108294032Sgad * that we send the remote host the file-size before we 108394032Sgad * start to send any of the data. 108494032Sgad */ 108594032Sgad strcpy(tfile, TFILENAME); 108694032Sgad tfd = mkstemp(tfile); 108794032Sgad if (tfd == -1) { 108894032Sgad syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer, 108994032Sgad TFILENAME); 109094036Sgad sfres = ERROR; 109194036Sgad goto return_sfres; 109294032Sgad } 109394036Sgad filtstat = execfilter(pp, filtcmd, av, sfd, tfd); 109424831Sbrian 109594032Sgad /* process the return-code from the filter */ 109694032Sgad switch (filtstat) { 109794032Sgad case 0: 109894032Sgad break; 109994032Sgad case 1: 110094036Sgad sfres = REPRINT; 110194036Sgad goto return_sfres; 110294032Sgad case 2: 110394036Sgad sfres = ERROR; 110494036Sgad goto return_sfres; 110594032Sgad default: 110694032Sgad syslog(LOG_WARNING, 110794032Sgad "%s: filter '%c' exited (retcode=%d)", 110894032Sgad pp->printer, format, filtstat); 110994036Sgad sfres = FILTERERR; 111094036Sgad goto return_sfres; 111194032Sgad } 111294032Sgad statrc = fstat(tfd, &stb); /* to find size of tfile */ 111394032Sgad if (statrc < 0) { 111494032Sgad syslog(LOG_ERR, 111594032Sgad "%s: error processing 'if', fstat(%s): %m", 111694032Sgad pp->printer, tfile); 111794036Sgad sfres = ERROR; 111894036Sgad goto return_sfres; 111994032Sgad } 112094036Sgad close(sfd); 112194036Sgad sfd = tfd; 112294036Sgad lseek(sfd, 0, SEEK_SET); 112394032Sgad } 112494032Sgad 112595293Sgad copycnt = 0; 112695293Sgadsendagain: 112795293Sgad copycnt++; 112895293Sgad 112995293Sgad if (copycnt < 2) 113095293Sgad (void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file); 113195293Sgad else 113295293Sgad (void) sprintf(buf, "%c%qd %s_c%d\n", type, stb.st_size, 113395293Sgad file, copycnt); 11341553Srgrimes amt = strlen(buf); 11351553Srgrimes for (i = 0; ; i++) { 11361553Srgrimes if (write(pfd, buf, amt) != amt || 113731492Swollman (resp = response(pp)) < 0 || resp == '\1') { 113894036Sgad sfres = REPRINT; 113994036Sgad goto return_sfres; 11401553Srgrimes } else if (resp == '\0') 11411553Srgrimes break; 11421553Srgrimes if (i == 0) 114331492Swollman pstatus(pp, 114431492Swollman "no space on remote; waiting for queue to drain"); 11451553Srgrimes if (i == 10) 11461553Srgrimes syslog(LOG_ALERT, "%s: can't send to %s; queue full", 114797792Sgad pp->printer, pp->remote_host); 11481553Srgrimes sleep(5 * 60); 11491553Srgrimes } 11501553Srgrimes if (i) 115131492Swollman pstatus(pp, "sending to %s", pp->remote_host); 115295293Sgad /* 115395293Sgad * XXX - we should change trstat_init()/trstat_write() to include 115495293Sgad * the copycnt in the statistics record it may write. 115595293Sgad */ 115668253Sgad if (type == '\3') 115768253Sgad trstat_init(pp, file, job_dfcnt); 1158119192Sgad for (i = 0; i < stb.st_size; i += SPL_BUFSIZ) { 1159119192Sgad amt = SPL_BUFSIZ; 11601553Srgrimes if (i + amt > stb.st_size) 11611553Srgrimes amt = stb.st_size - i; 116294036Sgad if (sizerr == 0 && read(sfd, buf, amt) != amt) 11631553Srgrimes sizerr = 1; 11641553Srgrimes if (write(pfd, buf, amt) != amt) { 116594036Sgad sfres = REPRINT; 116694036Sgad goto return_sfres; 11671553Srgrimes } 11681553Srgrimes } 11691553Srgrimes 11701553Srgrimes if (sizerr) { 117131492Swollman syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file); 11721553Srgrimes /* tell recvjob to ignore this file */ 11731553Srgrimes (void) write(pfd, "\1", 1); 117494036Sgad sfres = ERROR; 117594036Sgad goto return_sfres; 11761553Srgrimes } 117731492Swollman if (write(pfd, "", 1) != 1 || response(pp)) { 117894036Sgad sfres = REPRINT; 117994036Sgad goto return_sfres; 118024831Sbrian } 118195293Sgad if (type == '\3') { 118268253Sgad trstat_write(pp, TR_SENDING, stb.st_size, logname, 118395293Sgad pp->remote_host, origin_host); 118495293Sgad /* 118595293Sgad * Usually we only need to send one copy of a datafile, 118695293Sgad * because the control-file will simply print the same 118795293Sgad * file multiple times. However, some printers ignore 118895293Sgad * the control file, and simply print each data file as 118995293Sgad * it arrives. For such "remote hosts", we need to 119095293Sgad * transfer the same data file multiple times. Such a 119195293Sgad * a host is indicated by adding 'rc' to the printcap 119295293Sgad * entry. 119395293Sgad * XXX - Right now this ONLY works for remote hosts which 119495293Sgad * do ignore the name of the data file, because 119595293Sgad * this sends the file multiple times with slight 119695293Sgad * changes to the filename. To do this right would 119795293Sgad * require that we also rewrite the control file 119895293Sgad * to match those filenames. 119995293Sgad */ 120095293Sgad if (pp->resend_copies && (copycnt < copyreq)) { 120195293Sgad lseek(sfd, 0, SEEK_SET); 120295293Sgad goto sendagain; 120395293Sgad } 120495293Sgad } 120594036Sgad sfres = OK; 120694036Sgad 120794036Sgadreturn_sfres: 120894036Sgad (void)close(sfd); 120994036Sgad if (tfd != -1) { 121094036Sgad /* 121194036Sgad * If tfd is set, then it is the same value as sfd, and 121294036Sgad * therefore it is already closed at this point. All 121394036Sgad * we need to do is remove the temporary file. 121494036Sgad */ 121594036Sgad tfd = -1; 121694036Sgad unlink(tfile); 121794036Sgad } 121894036Sgad return (sfres); 12191553Srgrimes} 12201553Srgrimes 12211553Srgrimes/* 122294032Sgad * This routine is called to execute one of the filters as was 122394036Sgad * specified in a printcap entry. While the child-process will read 122494036Sgad * all of 'infd', it is up to the caller to close that file descriptor 122594036Sgad * in the parent process. 122694032Sgad */ 122794032Sgadstatic int 122894032Sgadexecfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd) 122994032Sgad{ 123097793Sgad pid_t fpid, wpid; 123197793Sgad int errfd, retcode, wstatus; 123294032Sgad FILE *errfp; 123394032Sgad char buf[BUFSIZ], *slash; 123494032Sgad 123594032Sgad fpid = dofork(pp, DORETURN); 123694032Sgad if (fpid != 0) { 123794032Sgad /* 123894032Sgad * This is the parent process, which just waits for the child 123994032Sgad * to complete and then returns the result. Note that it is 124094032Sgad * the child process which reads the input stream. 124194032Sgad */ 124294032Sgad if (fpid < 0) 124397781Sgad retcode = 100; 124494032Sgad else { 124597781Sgad while ((wpid = wait(&wstatus)) > 0 && 124694032Sgad wpid != fpid) 124794032Sgad ; 124894032Sgad if (wpid < 0) { 124997781Sgad retcode = 100; 125094032Sgad syslog(LOG_WARNING, 125194032Sgad "%s: after execv(%s), wait() returned: %m", 125294032Sgad pp->printer, f_cmd); 125397781Sgad } else 125497781Sgad retcode = WEXITSTATUS(wstatus); 125594032Sgad } 125694032Sgad 125794032Sgad /* 125894032Sgad * Copy everything the filter wrote to stderr from our 125994032Sgad * temporary errors file to the "lf=" logfile. 126094032Sgad */ 126194032Sgad errfp = fopen(tempstderr, "r"); 126294032Sgad if (errfp) { 126394032Sgad while (fgets(buf, sizeof(buf), errfp)) 126494032Sgad fputs(buf, stderr); 126594032Sgad fclose(errfp); 126694032Sgad } 126794032Sgad 126897781Sgad return (retcode); 126994032Sgad } 127094032Sgad 127194032Sgad /* 127294032Sgad * This is the child process, which is the one that executes the 127394032Sgad * given filter. 127494032Sgad */ 127594032Sgad /* 127694032Sgad * If the first parameter has any slashes in it, then change it 127794032Sgad * to point to the first character after the last slash. 127894032Sgad */ 127994032Sgad slash = strrchr(f_av[0], '/'); 128094032Sgad if (slash != NULL) 128194032Sgad f_av[0] = slash + 1; 128294032Sgad /* 128394032Sgad * XXX - in the future, this should setup an explicit list of 128494032Sgad * environment variables and use execve()! 128594032Sgad */ 128694032Sgad 128794032Sgad /* 128894032Sgad * Setup stdin, stdout, and stderr as we want them when the filter 128994032Sgad * is running. Stderr is setup so it points to a temporary errors 129094032Sgad * file, and the parent process will copy that temporary file to 129194032Sgad * the real logfile after the filter completes. 129294032Sgad */ 1293118881Sgad dup2(infd, STDIN_FILENO); 1294118881Sgad dup2(outfd, STDOUT_FILENO); 129594032Sgad errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664); 129694032Sgad if (errfd >= 0) 1297118881Sgad dup2(errfd, STDERR_FILENO); 129894032Sgad closelog(); 129994032Sgad closeallfds(3); 130094032Sgad execv(f_cmd, f_av); 130195067Sgad syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd); 130294032Sgad exit(2); 130394032Sgad /* NOTREACHED */ 130494032Sgad} 130594032Sgad 130694032Sgad/* 13071553Srgrimes * Check to make sure there have been no errors and that both programs 13081553Srgrimes * are in sync with eachother. 13091553Srgrimes * Return non-zero if the connection was lost. 13101553Srgrimes */ 13111553Srgrimesstatic char 131278146Sgadresponse(const struct printer *pp) 13131553Srgrimes{ 13141553Srgrimes char resp; 13151553Srgrimes 13161553Srgrimes if (read(pfd, &resp, 1) != 1) { 131731492Swollman syslog(LOG_INFO, "%s: lost connection", pp->printer); 131897791Sgad return (-1); 13191553Srgrimes } 132097791Sgad return (resp); 13211553Srgrimes} 13221553Srgrimes 13231553Srgrimes/* 13241553Srgrimes * Banner printing stuff 13251553Srgrimes */ 13261553Srgrimesstatic void 132778146Sgadbanner(struct printer *pp, char *name1, char *name2) 13281553Srgrimes{ 13291553Srgrimes time_t tvec; 13301553Srgrimes 13311553Srgrimes time(&tvec); 133231492Swollman if (!pp->no_formfeed && !pp->tof) 133331492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 133431492Swollman if (pp->short_banner) { /* short banner only */ 13351553Srgrimes if (class[0]) { 13361553Srgrimes (void) write(ofd, class, strlen(class)); 13371553Srgrimes (void) write(ofd, ":", 1); 13381553Srgrimes } 13391553Srgrimes (void) write(ofd, name1, strlen(name1)); 13401553Srgrimes (void) write(ofd, " Job: ", 7); 13411553Srgrimes (void) write(ofd, name2, strlen(name2)); 13421553Srgrimes (void) write(ofd, " Date: ", 8); 13431553Srgrimes (void) write(ofd, ctime(&tvec), 24); 13441553Srgrimes (void) write(ofd, "\n", 1); 13451553Srgrimes } else { /* normal banner */ 13461553Srgrimes (void) write(ofd, "\n\n\n", 3); 134731492Swollman scan_out(pp, ofd, name1, '\0'); 13481553Srgrimes (void) write(ofd, "\n\n", 2); 134931492Swollman scan_out(pp, ofd, name2, '\0'); 13501553Srgrimes if (class[0]) { 13511553Srgrimes (void) write(ofd,"\n\n\n",3); 135231492Swollman scan_out(pp, ofd, class, '\0'); 13531553Srgrimes } 13541553Srgrimes (void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15); 13551553Srgrimes (void) write(ofd, name2, strlen(name2)); 13561553Srgrimes (void) write(ofd, "\n\t\t\t\t\tDate: ", 12); 13571553Srgrimes (void) write(ofd, ctime(&tvec), 24); 13581553Srgrimes (void) write(ofd, "\n", 1); 13591553Srgrimes } 136031492Swollman if (!pp->no_formfeed) 136131492Swollman (void) write(ofd, pp->form_feed, strlen(pp->form_feed)); 136231492Swollman pp->tof = 1; 13631553Srgrimes} 13641553Srgrimes 13651553Srgrimesstatic char * 136678146Sgadscnline(int key, char *p, int c) 13671553Srgrimes{ 136839084Swollman register int scnwidth; 13691553Srgrimes 13701553Srgrimes for (scnwidth = WIDTH; --scnwidth;) { 13711553Srgrimes key <<= 1; 13721553Srgrimes *p++ = key & 0200 ? c : BACKGND; 13731553Srgrimes } 13741553Srgrimes return (p); 13751553Srgrimes} 13761553Srgrimes 13771553Srgrimes#define TRC(q) (((q)-' ')&0177) 13781553Srgrimes 13791553Srgrimesstatic void 138078146Sgadscan_out(struct printer *pp, int scfd, char *scsp, int dlm) 13811553Srgrimes{ 13821553Srgrimes register char *strp; 138339084Swollman register int nchrs, j; 13841553Srgrimes char outbuf[LINELEN+1], *sp, c, cc; 13851553Srgrimes int d, scnhgt; 13861553Srgrimes 13871553Srgrimes for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) { 13881553Srgrimes strp = &outbuf[0]; 13891553Srgrimes sp = scsp; 13901553Srgrimes for (nchrs = 0; ; ) { 13911553Srgrimes d = dropit(c = TRC(cc = *sp++)); 13921553Srgrimes if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d)) 13931553Srgrimes for (j = WIDTH; --j;) 13941553Srgrimes *strp++ = BACKGND; 13951553Srgrimes else 139631492Swollman strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc); 139731492Swollman if (*sp == dlm || *sp == '\0' || 139831492Swollman nchrs++ >= pp->page_width/(WIDTH+1)-1) 13991553Srgrimes break; 14001553Srgrimes *strp++ = BACKGND; 14011553Srgrimes *strp++ = BACKGND; 14021553Srgrimes } 14031553Srgrimes while (*--strp == BACKGND && strp >= outbuf) 14041553Srgrimes ; 14051553Srgrimes strp++; 14068857Srgrimes *strp++ = '\n'; 14071553Srgrimes (void) write(scfd, outbuf, strp-outbuf); 14081553Srgrimes } 14091553Srgrimes} 14101553Srgrimes 14111553Srgrimesstatic int 141278146Sgaddropit(int c) 14131553Srgrimes{ 14141553Srgrimes switch(c) { 14151553Srgrimes 14161553Srgrimes case TRC('_'): 14171553Srgrimes case TRC(';'): 14181553Srgrimes case TRC(','): 14191553Srgrimes case TRC('g'): 14201553Srgrimes case TRC('j'): 14211553Srgrimes case TRC('p'): 14221553Srgrimes case TRC('q'): 14231553Srgrimes case TRC('y'): 14241553Srgrimes return (DROP); 14251553Srgrimes 14261553Srgrimes default: 14271553Srgrimes return (0); 14281553Srgrimes } 14291553Srgrimes} 14301553Srgrimes 14311553Srgrimes/* 14321553Srgrimes * sendmail --- 14331553Srgrimes * tell people about job completion 14341553Srgrimes */ 14351553Srgrimesstatic void 143694038Sgadsendmail(struct printer *pp, char *userid, int bombed) 14371553Srgrimes{ 14381553Srgrimes register int i; 14391553Srgrimes int p[2], s; 144078146Sgad register const char *cp; 14411553Srgrimes struct stat stb; 14421553Srgrimes FILE *fp; 14431553Srgrimes 14441553Srgrimes pipe(p); 144531492Swollman if ((s = dofork(pp, DORETURN)) == 0) { /* child */ 1446118881Sgad dup2(p[0], STDIN_FILENO); 14478094Sjkh closelog(); 144831492Swollman closeallfds(3); 144927635Simp if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL) 14501553Srgrimes cp++; 145131492Swollman else 14521553Srgrimes cp = _PATH_SENDMAIL; 145379452Sbrian execl(_PATH_SENDMAIL, cp, "-t", (char *)0); 145431492Swollman _exit(0); 14551553Srgrimes } else if (s > 0) { /* parent */ 1456118881Sgad dup2(p[1], STDOUT_FILENO); 145794038Sgad printf("To: %s@%s\n", userid, origin_host); 145831492Swollman printf("Subject: %s printer job \"%s\"\n", pp->printer, 145915648Sjoerg *jobname ? jobname : "<unknown>"); 146078300Sgad printf("Reply-To: root@%s\n\n", local_host); 14611553Srgrimes printf("Your printer job "); 14621553Srgrimes if (*jobname) 14631553Srgrimes printf("(%s) ", jobname); 146494040Sgad 14651553Srgrimes switch (bombed) { 14661553Srgrimes case OK: 146794040Sgad cp = "OK"; 14681553Srgrimes printf("\ncompleted successfully\n"); 14691553Srgrimes break; 14701553Srgrimes default: 14711553Srgrimes case FATALERR: 147294040Sgad cp = "FATALERR"; 14731553Srgrimes printf("\ncould not be printed\n"); 14741553Srgrimes break; 14751553Srgrimes case NOACCT: 147694040Sgad cp = "NOACCT"; 147778300Sgad printf("\ncould not be printed without an account on %s\n", 147878300Sgad local_host); 14791553Srgrimes break; 14801553Srgrimes case FILTERERR: 148194040Sgad cp = "FILTERERR"; 148268664Sgad if (stat(tempstderr, &stb) < 0 || stb.st_size == 0 148368664Sgad || (fp = fopen(tempstderr, "r")) == NULL) { 148415648Sjoerg printf("\nhad some errors and may not have printed\n"); 14851553Srgrimes break; 14861553Srgrimes } 148715648Sjoerg printf("\nhad the following errors and may not have printed:\n"); 14881553Srgrimes while ((i = getc(fp)) != EOF) 14891553Srgrimes putchar(i); 14901553Srgrimes (void) fclose(fp); 14911553Srgrimes break; 14921553Srgrimes case ACCESS: 149394040Sgad cp = "ACCESS"; 14941553Srgrimes printf("\nwas not printed because it was not linked to the original file\n"); 14951553Srgrimes } 14961553Srgrimes fflush(stdout); 1497118881Sgad (void) close(STDOUT_FILENO); 149831492Swollman } else { 149994038Sgad syslog(LOG_WARNING, "unable to send mail to %s: %m", userid); 150031492Swollman return; 15011553Srgrimes } 15021553Srgrimes (void) close(p[0]); 15031553Srgrimes (void) close(p[1]); 150415648Sjoerg wait(NULL); 150515648Sjoerg syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)", 150694038Sgad userid, *jobname ? jobname : "<unknown>", pp->printer, cp); 15071553Srgrimes} 15081553Srgrimes 15091553Srgrimes/* 15101553Srgrimes * dofork - fork with retries on failure 15111553Srgrimes */ 15121553Srgrimesstatic int 151378146Sgaddofork(const struct printer *pp, int action) 15141553Srgrimes{ 151597793Sgad pid_t forkpid; 151697793Sgad int i, fail; 151760871Smpp struct passwd *pwd; 15181553Srgrimes 151980230Sgad forkpid = -1; 152080230Sgad if (daemon_uname == NULL) { 152180230Sgad pwd = getpwuid(pp->daemon_user); 152280230Sgad if (pwd == NULL) { 152380230Sgad syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file", 152480230Sgad pp->printer, pp->daemon_user); 152580230Sgad goto error_ret; 152680230Sgad } 152780230Sgad daemon_uname = strdup(pwd->pw_name); 152880230Sgad daemon_defgid = pwd->pw_gid; 152980230Sgad } 153080230Sgad 15311553Srgrimes for (i = 0; i < 20; i++) { 153280230Sgad forkpid = fork(); 153380230Sgad if (forkpid < 0) { 15341553Srgrimes sleep((unsigned)(i*i)); 15351553Srgrimes continue; 15361553Srgrimes } 15371553Srgrimes /* 15381553Srgrimes * Child should run as daemon instead of root 15391553Srgrimes */ 154078146Sgad if (forkpid == 0) { 154180230Sgad errno = 0; 154280230Sgad fail = initgroups(daemon_uname, daemon_defgid); 154380230Sgad if (fail) { 154480230Sgad syslog(LOG_ERR, "%s: initgroups(%s,%u): %m", 154580230Sgad pp->printer, daemon_uname, daemon_defgid); 154660871Smpp break; 154760871Smpp } 154880230Sgad fail = setgid(daemon_defgid); 154980230Sgad if (fail) { 155080230Sgad syslog(LOG_ERR, "%s: setgid(%u): %m", 155180230Sgad pp->printer, daemon_defgid); 155280230Sgad break; 155380230Sgad } 155480230Sgad fail = setuid(pp->daemon_user); 155580230Sgad if (fail) { 155680230Sgad syslog(LOG_ERR, "%s: setuid(%ld): %m", 155780230Sgad pp->printer, pp->daemon_user); 155880230Sgad break; 155980230Sgad } 156060871Smpp } 156197791Sgad return (forkpid); 15621553Srgrimes } 15631553Srgrimes 156480230Sgad /* 156580230Sgad * An error occurred. If the error is in the child process, then 156680230Sgad * this routine MUST always exit(). DORETURN only effects how 156780230Sgad * errors should be handled in the parent process. 156880230Sgad */ 156980230Sgaderror_ret: 157080230Sgad if (forkpid == 0) { 157180230Sgad syslog(LOG_ERR, "%s: dofork(): aborting child process...", 157280230Sgad pp->printer); 157380230Sgad exit(1); 157480230Sgad } 157580230Sgad syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer); 157680230Sgad 157780230Sgad sleep(1); /* throttle errors, as a safety measure */ 15781553Srgrimes switch (action) { 15791553Srgrimes case DORETURN: 158097791Sgad return (-1); 15811553Srgrimes default: 15821553Srgrimes syslog(LOG_ERR, "bad action (%d) to dofork", action); 158380230Sgad /* FALLTHROUGH */ 15841553Srgrimes case DOABORT: 15851553Srgrimes exit(1); 15861553Srgrimes } 15871553Srgrimes /*NOTREACHED*/ 15881553Srgrimes} 15891553Srgrimes 15901553Srgrimes/* 15911553Srgrimes * Kill child processes to abort current job. 15921553Srgrimes */ 15931553Srgrimesstatic void 159478146Sgadabortpr(int signo __unused) 15951553Srgrimes{ 159668664Sgad 159768664Sgad (void) unlink(tempstderr); 15981553Srgrimes kill(0, SIGINT); 159997793Sgad if (of_pid > 0) 160097793Sgad kill(of_pid, SIGCONT); 16011553Srgrimes while (wait(NULL) > 0) 16021553Srgrimes ; 160397793Sgad if (of_pid > 0 && tfd != -1) 160424831Sbrian unlink(tfile); 16051553Srgrimes exit(0); 16061553Srgrimes} 16071553Srgrimes 16081553Srgrimesstatic void 160978146Sgadinit(struct printer *pp) 16101553Srgrimes{ 16111553Srgrimes char *s; 16121553Srgrimes 161331492Swollman sprintf(&width[2], "%ld", pp->page_width); 161431492Swollman sprintf(&length[2], "%ld", pp->page_length); 161531492Swollman sprintf(&pxwidth[2], "%ld", pp->page_pwidth); 161631492Swollman sprintf(&pxlength[2], "%ld", pp->page_plength); 161731492Swollman if ((s = checkremote(pp)) != 0) { 161831492Swollman syslog(LOG_WARNING, "%s", s); 161931492Swollman free(s); 162031492Swollman } 162131492Swollman} 162231492Swollman 162331492Swollmanvoid 162478146Sgadstartprinting(const char *printer) 162531492Swollman{ 162631492Swollman struct printer myprinter, *pp = &myprinter; 162731492Swollman int status; 162831492Swollman 162931492Swollman init_printer(pp); 163031492Swollman status = getprintcap(printer, pp); 163131492Swollman switch(status) { 163231492Swollman case PCAPERR_OSERR: 163331492Swollman syslog(LOG_ERR, "can't open printer description file: %m"); 16341553Srgrimes exit(1); 163531492Swollman case PCAPERR_NOTFOUND: 16361553Srgrimes syslog(LOG_ERR, "unknown printer: %s", printer); 16371553Srgrimes exit(1); 163831492Swollman case PCAPERR_TCLOOP: 163931492Swollman fatal(pp, "potential reference loop detected in printcap file"); 164031492Swollman default: 164131492Swollman break; 164231492Swollman } 164331492Swollman printjob(pp); 16441553Srgrimes} 16451553Srgrimes 16461553Srgrimes/* 16471553Srgrimes * Acquire line printer or remote connection. 16481553Srgrimes */ 16491553Srgrimesstatic void 165078146Sgadopenpr(const struct printer *pp) 16511553Srgrimes{ 165231492Swollman int p[2]; 165315648Sjoerg char *cp; 16541553Srgrimes 165531492Swollman if (pp->remote) { 165631492Swollman openrem(pp); 165794032Sgad /* 165894032Sgad * Lpd does support the setting of 'of=' filters for 165994032Sgad * jobs going to remote machines, but that does not 166094032Sgad * have the same meaning as 'of=' does when handling 166194032Sgad * local print queues. For remote machines, all 'of=' 166294032Sgad * filter processing is handled in sendfile(), and that 166394032Sgad * does not use these global "output filter" variables. 166494032Sgad */ 166594032Sgad ofd = -1; 166697793Sgad of_pid = 0; 166794032Sgad return; 166831492Swollman } else if (*pp->lp) { 166931492Swollman if ((cp = strchr(pp->lp, '@')) != NULL) 167031492Swollman opennet(pp); 167115648Sjoerg else 167231492Swollman opentty(pp); 16731553Srgrimes } else { 16741553Srgrimes syslog(LOG_ERR, "%s: no line printer device or host name", 167597792Sgad pp->printer); 16761553Srgrimes exit(1); 16771553Srgrimes } 167815648Sjoerg 16791553Srgrimes /* 16801553Srgrimes * Start up an output filter, if needed. 16811553Srgrimes */ 168297793Sgad if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !of_pid) { 16831553Srgrimes pipe(p); 168431492Swollman if (pp->remote) { 168531492Swollman strcpy(tfile, TFILENAME); 168624831Sbrian tfd = mkstemp(tfile); 168724831Sbrian } 168897793Sgad if ((of_pid = dofork(pp, DOABORT)) == 0) { /* child */ 1689118881Sgad dup2(p[0], STDIN_FILENO); /* pipe is std in */ 169024831Sbrian /* tfile/printer is stdout */ 1691118881Sgad dup2(pp->remote ? tfd : pfd, STDOUT_FILENO); 16928094Sjkh closelog(); 169331492Swollman closeallfds(3); 169431492Swollman if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL) 169531492Swollman cp = pp->filters[LPF_OUTPUT]; 16961553Srgrimes else 16971553Srgrimes cp++; 169879452Sbrian execl(pp->filters[LPF_OUTPUT], cp, width, length, 169979452Sbrian (char *)0); 170097792Sgad syslog(LOG_ERR, "%s: execl(%s): %m", pp->printer, 170197792Sgad pp->filters[LPF_OUTPUT]); 17021553Srgrimes exit(1); 17031553Srgrimes } 17041553Srgrimes (void) close(p[0]); /* close input side */ 17051553Srgrimes ofd = p[1]; /* use pipe for output */ 17061553Srgrimes } else { 17071553Srgrimes ofd = pfd; 170897793Sgad of_pid = 0; 17091553Srgrimes } 17101553Srgrimes} 17111553Srgrimes 171215648Sjoerg/* 171315648Sjoerg * Printer connected directly to the network 171415648Sjoerg * or to a terminal server on the net 171515648Sjoerg */ 171615648Sjoergstatic void 171778146Sgadopennet(const struct printer *pp) 171815648Sjoerg{ 171915648Sjoerg register int i; 172031492Swollman int resp; 172131492Swollman u_long port; 172231492Swollman char *ep; 172330407Sjoerg void (*savealrm)(int); 172415648Sjoerg 172531492Swollman port = strtoul(pp->lp, &ep, 0); 172638470Sbrian if (*ep != '@' || port > 65535) { 172731492Swollman syslog(LOG_ERR, "%s: bad port number: %s", pp->printer, 172897792Sgad pp->lp); 172915648Sjoerg exit(1); 173015648Sjoerg } 173131492Swollman ep++; 173215648Sjoerg 173315648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 173415648Sjoerg resp = -1; 173530407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 173631492Swollman alarm(pp->conn_timeout); 173731492Swollman pfd = getport(pp, ep, port); 173831020Sjoerg alarm(0); 173930407Sjoerg (void)signal(SIGALRM, savealrm); 174015648Sjoerg if (pfd < 0 && errno == ECONNREFUSED) 174115648Sjoerg resp = 1; 174215648Sjoerg else if (pfd >= 0) { 174315648Sjoerg /* 174415648Sjoerg * need to delay a bit for rs232 lines 174515648Sjoerg * to stabilize in case printer is 174615648Sjoerg * connected via a terminal server 174715648Sjoerg */ 174815648Sjoerg delay(500); 174915648Sjoerg break; 175015648Sjoerg } 175115648Sjoerg if (i == 1) { 175231492Swollman if (resp < 0) 175331492Swollman pstatus(pp, "waiting for %s to come up", 175431492Swollman pp->lp); 175531492Swollman else 175631492Swollman pstatus(pp, 175731492Swollman "waiting for access to printer on %s", 175831492Swollman pp->lp); 175915648Sjoerg } 176015648Sjoerg sleep(i); 176115648Sjoerg } 176279739Sgad pstatus(pp, "sending to %s port %lu", ep, port); 176315648Sjoerg} 176415648Sjoerg 176515648Sjoerg/* 176615648Sjoerg * Printer is connected to an RS232 port on this host 176715648Sjoerg */ 176815648Sjoergstatic void 176978146Sgadopentty(const struct printer *pp) 177015648Sjoerg{ 177115648Sjoerg register int i; 177215648Sjoerg 177315648Sjoerg for (i = 1; ; i = i < 32 ? i << 1 : i) { 177431492Swollman pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY); 177515648Sjoerg if (pfd >= 0) { 177615648Sjoerg delay(500); 177715648Sjoerg break; 177815648Sjoerg } 177915648Sjoerg if (errno == ENOENT) { 178031492Swollman syslog(LOG_ERR, "%s: %m", pp->lp); 178115648Sjoerg exit(1); 178215648Sjoerg } 178315648Sjoerg if (i == 1) 178431492Swollman pstatus(pp, 178531492Swollman "waiting for %s to become ready (offline?)", 178631492Swollman pp->printer); 178715648Sjoerg sleep(i); 178815648Sjoerg } 178915648Sjoerg if (isatty(pfd)) 179031492Swollman setty(pp); 179131492Swollman pstatus(pp, "%s is ready and printing", pp->printer); 179215648Sjoerg} 179315648Sjoerg 179415648Sjoerg/* 179515648Sjoerg * Printer is on a remote host 179615648Sjoerg */ 179715648Sjoergstatic void 179878146Sgadopenrem(const struct printer *pp) 179915648Sjoerg{ 180031492Swollman register int i; 180127748Simp int resp; 180230407Sjoerg void (*savealrm)(int); 180315648Sjoerg 180415648Sjoerg for (i = 1; ; i = i < 256 ? i << 1 : i) { 180515648Sjoerg resp = -1; 180630407Sjoerg savealrm = signal(SIGALRM, alarmhandler); 180731492Swollman alarm(pp->conn_timeout); 180831492Swollman pfd = getport(pp, pp->remote_host, 0); 180931020Sjoerg alarm(0); 181030407Sjoerg (void)signal(SIGALRM, savealrm); 181115648Sjoerg if (pfd >= 0) { 181231492Swollman if ((writel(pfd, "\2", pp->remote_queue, "\n", 181331492Swollman (char *)0) 181431492Swollman == 2 + strlen(pp->remote_queue)) 181531492Swollman && (resp = response(pp)) == 0) 181615648Sjoerg break; 181715648Sjoerg (void) close(pfd); 181815648Sjoerg } 181915648Sjoerg if (i == 1) { 182015648Sjoerg if (resp < 0) 182131492Swollman pstatus(pp, "waiting for %s to come up", 182231492Swollman pp->remote_host); 182315648Sjoerg else { 182431492Swollman pstatus(pp, 182531492Swollman "waiting for queue to be enabled on %s", 182631492Swollman pp->remote_host); 182715648Sjoerg i = 256; 182815648Sjoerg } 182915648Sjoerg } 183015648Sjoerg sleep(i); 183115648Sjoerg } 183231492Swollman pstatus(pp, "sending to %s", pp->remote_host); 183315648Sjoerg} 183415648Sjoerg 18351553Srgrimes/* 18361553Srgrimes * setup tty lines. 18371553Srgrimes */ 18381553Srgrimesstatic void 183978146Sgadsetty(const struct printer *pp) 18401553Srgrimes{ 184115032Ssef struct termios ttybuf; 18421553Srgrimes 18431553Srgrimes if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) { 184431492Swollman syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer); 18451553Srgrimes exit(1); 18461553Srgrimes } 184715032Ssef if (tcgetattr(pfd, &ttybuf) < 0) { 184831492Swollman syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer); 18491553Srgrimes exit(1); 18501553Srgrimes } 185131492Swollman if (pp->baud_rate > 0) 185231492Swollman cfsetspeed(&ttybuf, pp->baud_rate); 185331492Swollman if (pp->mode_set) { 185431492Swollman char *s = strdup(pp->mode_set), *tmp; 185515032Ssef 185631492Swollman while ((tmp = strsep(&s, ",")) != NULL) { 185739084Swollman (void) msearch(tmp, &ttybuf); 18581553Srgrimes } 18591553Srgrimes } 186031492Swollman if (pp->mode_set != 0 || pp->baud_rate > 0) { 186115032Ssef if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) { 186231492Swollman syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer); 18631553Srgrimes } 18641553Srgrimes } 18651553Srgrimes} 18661553Srgrimes 18671553Srgrimes#include <stdarg.h> 18681553Srgrimes 186915648Sjoergstatic void 187031492Swollmanpstatus(const struct printer *pp, const char *msg, ...) 18711553Srgrimes{ 187231492Swollman int fd; 187331492Swollman char *buf; 18741553Srgrimes va_list ap; 18751553Srgrimes va_start(ap, msg); 18761553Srgrimes 18771553Srgrimes umask(0); 187831492Swollman fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE); 187931492Swollman if (fd < 0) { 188097792Sgad syslog(LOG_ERR, "%s: open(%s): %m", pp->printer, 188197792Sgad pp->status_file); 18821553Srgrimes exit(1); 18831553Srgrimes } 18841553Srgrimes ftruncate(fd, 0); 188531492Swollman vasprintf(&buf, msg, ap); 18861553Srgrimes va_end(ap); 188731492Swollman writel(fd, buf, "\n", (char *)0); 188831492Swollman close(fd); 188931492Swollman free(buf); 18901553Srgrimes} 189130407Sjoerg 189230407Sjoergvoid 189378146Sgadalarmhandler(int signo __unused) 189430407Sjoerg{ 189578146Sgad /* the signal is ignored */ 189678146Sgad /* (the '__unused' is just to avoid a compile-time warning) */ 189730407Sjoerg} 1898