1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-12-02 20:46:22 +00:00
|
|
|
static const char copyright[] =
|
1994-05-26 05:23:31 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-12-02 20:46:22 +00:00
|
|
|
/*
|
1996-05-05 22:40:51 +00:00
|
|
|
static char sccsid[] = "@(#)printjob.c 8.7 (Berkeley) 5/10/95";
|
1997-12-02 20:46:22 +00:00
|
|
|
*/
|
|
|
|
static const char rcsid[] =
|
1999-08-28 01:35:59 +00:00
|
|
|
"$FreeBSD$";
|
1994-05-26 05:23:31 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* printjob -- print jobs in the queue.
|
|
|
|
*
|
|
|
|
* NOTE: the lock file is used to pass information to lpq and lprm.
|
|
|
|
* it does not need to be removed because file locks are dynamic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1996-04-03 22:49:15 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <termios.h>
|
1996-05-09 22:44:28 +00:00
|
|
|
#include <time.h>
|
1994-05-26 05:23:31 +00:00
|
|
|
#include "lp.h"
|
|
|
|
#include "lp.local.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2001-07-23 23:13:39 +00:00
|
|
|
#define DORETURN 0 /* dofork should return "can't fork" error */
|
|
|
|
#define DOABORT 1 /* dofork should just die if fork() fails */
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Error tokens
|
|
|
|
*/
|
|
|
|
#define REPRINT -2
|
|
|
|
#define ERROR -1
|
|
|
|
#define OK 0
|
|
|
|
#define FATALERR 1
|
|
|
|
#define NOACCT 2
|
|
|
|
#define FILTERERR 3
|
|
|
|
#define ACCESS 4
|
|
|
|
|
|
|
|
static dev_t fdev; /* device of file pointed to by symlink */
|
|
|
|
static ino_t fino; /* inode of file pointed to by symlink */
|
|
|
|
static FILE *cfp; /* control file */
|
|
|
|
static int child; /* id of any filters */
|
2000-11-02 19:22:06 +00:00
|
|
|
static int job_dfcnt; /* count of datafiles in current user job */
|
1994-05-26 05:23:31 +00:00
|
|
|
static int lfd; /* lock file descriptor */
|
|
|
|
static int ofd; /* output filter file descriptor */
|
|
|
|
static int ofilter; /* id of output filter, if any */
|
1997-04-12 04:23:13 +00:00
|
|
|
static int tfd = -1; /* output filter temp file output */
|
1994-05-26 05:23:31 +00:00
|
|
|
static int pfd; /* prstatic inter file descriptor */
|
|
|
|
static int pid; /* pid of lpd process */
|
|
|
|
static int prchild; /* id of pr process */
|
|
|
|
static char title[80]; /* ``pr'' title */
|
1999-11-30 16:15:22 +00:00
|
|
|
static char locale[80]; /* ``pr'' locale */
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2001-07-23 23:13:39 +00:00
|
|
|
/* these two are set from pp->daemon_user, but only if they are needed */
|
|
|
|
static char *daemon_uname; /* set from pwd->pw_name */
|
|
|
|
static int daemon_defgid;
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
static char class[32]; /* classification field */
|
2001-06-15 22:03:07 +00:00
|
|
|
static char origin_host[MAXHOSTNAMELEN]; /* user's host machine */
|
1994-05-26 05:23:31 +00:00
|
|
|
/* indentation size in static characters */
|
1995-05-30 03:57:47 +00:00
|
|
|
static char indent[10] = "-i0";
|
1994-05-26 05:23:31 +00:00
|
|
|
static char jobname[100]; /* job or file name */
|
|
|
|
static char length[10] = "-l"; /* page length in lines */
|
|
|
|
static char logname[32]; /* user's login name */
|
|
|
|
static char pxlength[10] = "-y"; /* page length in pixels */
|
|
|
|
static char pxwidth[10] = "-x"; /* page width in pixels */
|
2000-11-13 05:44:50 +00:00
|
|
|
/* tempstderr is the filename used to catch stderr from exec-ing filters */
|
|
|
|
static char tempstderr[] = "errs.XXXXXXX";
|
1994-05-26 05:23:31 +00:00
|
|
|
static char width[10] = "-w"; /* page width in static characters */
|
1997-04-12 04:23:13 +00:00
|
|
|
#define TFILENAME "fltXXXXXX"
|
|
|
|
static char tfile[] = TFILENAME; /* file name for filter output */
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2001-06-12 16:38:20 +00:00
|
|
|
static void abortpr(int _signo);
|
|
|
|
static void alarmhandler(int _signo);
|
|
|
|
static void banner(struct printer *_pp, char *_name1, char *_name2);
|
|
|
|
static int dofork(const struct printer *_pp, int _action);
|
|
|
|
static int dropit(int _c);
|
2002-04-07 05:37:27 +00:00
|
|
|
static int execfilter(struct printer *_pp, char *_f_cmd, char **_f_av,
|
|
|
|
int _infd, int _outfd);
|
2001-06-12 16:38:20 +00:00
|
|
|
static void init(struct printer *_pp);
|
|
|
|
static void openpr(const struct printer *_pp);
|
|
|
|
static void opennet(const struct printer *_pp);
|
|
|
|
static void opentty(const struct printer *_pp);
|
|
|
|
static void openrem(const struct printer *pp);
|
|
|
|
static int print(struct printer *_pp, int _format, char *_file);
|
|
|
|
static int printit(struct printer *_pp, char *_file);
|
2001-07-15 00:09:46 +00:00
|
|
|
static void pstatus(const struct printer *_pp, const char *_msg, ...)
|
|
|
|
__printflike(2, 3);
|
2001-06-12 16:38:20 +00:00
|
|
|
static char response(const struct printer *_pp);
|
|
|
|
static void scan_out(struct printer *_pp, int _scfd, char *_scsp,
|
|
|
|
int _dlm);
|
|
|
|
static char *scnline(int _key, char *_p, int _c);
|
|
|
|
static int sendfile(struct printer *_pp, int _type, char *_file,
|
2002-04-23 00:06:10 +00:00
|
|
|
char _format, int _copyreq);
|
2001-06-12 16:38:20 +00:00
|
|
|
static int sendit(struct printer *_pp, char *_file);
|
2002-04-07 08:01:42 +00:00
|
|
|
static void sendmail(struct printer *_pp, char *_userid, int _bombed);
|
2001-06-12 16:38:20 +00:00
|
|
|
static void setty(const struct printer *_pp);
|
1997-12-02 20:46:22 +00:00
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
void
|
2001-06-12 16:38:20 +00:00
|
|
|
printjob(struct printer *pp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
struct stat stb;
|
2000-11-06 19:36:38 +00:00
|
|
|
register struct jobqueue *q, **qp;
|
|
|
|
struct jobqueue **queue;
|
1994-05-26 05:23:31 +00:00
|
|
|
register int i, nitems;
|
2000-11-14 21:44:47 +00:00
|
|
|
off_t pidoff;
|
|
|
|
int errcnt, jobcount, tempfd;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2000-11-14 21:44:47 +00:00
|
|
|
jobcount = 0;
|
1997-12-02 20:46:22 +00:00
|
|
|
init(pp); /* set up capabilities */
|
|
|
|
(void) write(1, "", 1); /* ack that daemon is started */
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) close(2); /* set up log file */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (open(pp->log_file, O_WRONLY|O_APPEND, LOG_FILE_MODE) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", pp->log_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) open(_PATH_DEVNULL, O_WRONLY);
|
|
|
|
}
|
|
|
|
setgid(getegid());
|
|
|
|
pid = getpid(); /* for use with lprm */
|
|
|
|
setpgrp(0, pid);
|
2001-07-14 21:49:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* At initial lpd startup, printjob may be called with various
|
|
|
|
* signal handlers in effect. After that initial startup, any
|
|
|
|
* calls to printjob will have a *different* set of signal-handlers
|
|
|
|
* in effect. Make sure all handlers are the ones we want.
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
1994-05-26 05:23:31 +00:00
|
|
|
signal(SIGHUP, abortpr);
|
|
|
|
signal(SIGINT, abortpr);
|
|
|
|
signal(SIGQUIT, abortpr);
|
|
|
|
signal(SIGTERM, abortpr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* uses short form file names
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if (chdir(pp->spool_dir) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", pp->spool_dir);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (stat(pp->lock_file, &stb) == 0 && (stb.st_mode & LFM_PRINT_DIS))
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(0); /* printing disabled */
|
1997-12-02 20:46:22 +00:00
|
|
|
lfd = open(pp->lock_file, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
|
|
|
|
LOCK_FILE_MODE);
|
1994-05-26 05:23:31 +00:00
|
|
|
if (lfd < 0) {
|
1997-12-02 20:46:22 +00:00
|
|
|
if (errno == EWOULDBLOCK) /* active daemon present */
|
|
|
|
exit(0);
|
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
/* turn off non-blocking mode (was turned on for lock effects only) */
|
|
|
|
if (fcntl(lfd, F_SETFL, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ftruncate(lfd, 0);
|
|
|
|
/*
|
|
|
|
* write process id for others to know
|
|
|
|
*/
|
|
|
|
sprintf(line, "%u\n", pid);
|
|
|
|
pidoff = i = strlen(line);
|
|
|
|
if (write(lfd, line, i) != i) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->lock_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* search the spool directory for work and sort by queue order.
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((nitems = getq(pp, &queue)) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
|
|
|
|
pp->spool_dir);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (nitems == 0) /* no work to do */
|
|
|
|
exit(0);
|
1997-12-02 20:46:22 +00:00
|
|
|
if (stb.st_mode & LFM_RESET_QUE) { /* reset queue flag */
|
|
|
|
if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE) < 0)
|
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer,
|
|
|
|
pp->lock_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
2000-11-13 05:44:50 +00:00
|
|
|
|
|
|
|
/* create a file which will be used to hold stderr from filters */
|
|
|
|
if ((tempfd = mkstemp(tempstderr)) == -1) {
|
|
|
|
syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
|
|
|
|
tempstderr);
|
2000-11-14 21:32:52 +00:00
|
|
|
exit(1);
|
2000-11-13 05:44:50 +00:00
|
|
|
}
|
|
|
|
if ((i = fchmod(tempfd, 0664)) == -1) {
|
|
|
|
syslog(LOG_ERR, "%s: fchmod(%s): %m", pp->printer,
|
|
|
|
tempstderr);
|
2000-11-14 21:32:52 +00:00
|
|
|
exit(1);
|
2000-11-13 05:44:50 +00:00
|
|
|
}
|
|
|
|
/* lpd doesn't need it to be open, it just needs it to exist */
|
|
|
|
close(tempfd);
|
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
openpr(pp); /* open printer or remote */
|
1994-05-26 05:23:31 +00:00
|
|
|
again:
|
|
|
|
/*
|
|
|
|
* we found something to do now do it --
|
|
|
|
* write the name of the current control file into the lock file
|
|
|
|
* so the spool queue program can tell what we're working on
|
|
|
|
*/
|
|
|
|
for (qp = queue; nitems--; free((char *) q)) {
|
|
|
|
q = *qp++;
|
2000-11-06 19:36:38 +00:00
|
|
|
if (stat(q->job_cfname, &stb) < 0)
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
1996-05-05 22:40:51 +00:00
|
|
|
errcnt = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
restart:
|
1996-05-05 22:40:51 +00:00
|
|
|
(void) lseek(lfd, pidoff, 0);
|
2000-11-06 19:36:38 +00:00
|
|
|
(void) snprintf(line, sizeof(line), "%s\n", q->job_cfname);
|
1994-05-26 05:23:31 +00:00
|
|
|
i = strlen(line);
|
|
|
|
if (write(lfd, line, i) != i)
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer,
|
|
|
|
pp->lock_file);
|
|
|
|
if (!pp->remote)
|
2000-11-06 19:36:38 +00:00
|
|
|
i = printit(pp, q->job_cfname);
|
1994-05-26 05:23:31 +00:00
|
|
|
else
|
2000-11-06 19:36:38 +00:00
|
|
|
i = sendit(pp, q->job_cfname);
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Check to see if we are supposed to stop printing or
|
|
|
|
* if we are to rebuild the queue.
|
|
|
|
*/
|
|
|
|
if (fstat(lfd, &stb) == 0) {
|
|
|
|
/* stop printing before starting next job? */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (stb.st_mode & LFM_PRINT_DIS)
|
1994-05-26 05:23:31 +00:00
|
|
|
goto done;
|
|
|
|
/* rebuild queue (after lpc topq) */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (stb.st_mode & LFM_RESET_QUE) {
|
|
|
|
for (free(q); nitems--; free(q))
|
1994-05-26 05:23:31 +00:00
|
|
|
q = *qp++;
|
1997-12-02 20:46:22 +00:00
|
|
|
if (fchmod(lfd, stb.st_mode & ~LFM_RESET_QUE)
|
|
|
|
< 0)
|
1994-05-26 05:23:31 +00:00
|
|
|
syslog(LOG_WARNING, "%s: %s: %m",
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->printer, pp->lock_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2000-11-14 21:44:47 +00:00
|
|
|
if (i == OK) /* all files of this job printed */
|
|
|
|
jobcount++;
|
1996-05-05 22:40:51 +00:00
|
|
|
else if (i == REPRINT && ++errcnt < 5) {
|
|
|
|
/* try reprinting the job */
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_INFO, "restarting %s", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
if (ofilter > 0) {
|
|
|
|
kill(ofilter, SIGCONT); /* to be sure */
|
|
|
|
(void) close(ofd);
|
1996-05-05 22:40:51 +00:00
|
|
|
while ((i = wait(NULL)) > 0 && i != ofilter)
|
1994-05-26 05:23:31 +00:00
|
|
|
;
|
2001-07-14 21:49:17 +00:00
|
|
|
if (i < 0)
|
|
|
|
syslog(LOG_WARNING, "%s: after kill(of=%d), wait() returned: %m",
|
|
|
|
pp->printer, ofilter);
|
1994-05-26 05:23:31 +00:00
|
|
|
ofilter = 0;
|
|
|
|
}
|
|
|
|
(void) close(pfd); /* close printer */
|
|
|
|
if (ftruncate(lfd, pidoff) < 0)
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_WARNING, "%s: %s: %m",
|
|
|
|
pp->printer, pp->lock_file);
|
|
|
|
openpr(pp); /* try to reopen printer */
|
1994-05-26 05:23:31 +00:00
|
|
|
goto restart;
|
1996-05-05 22:40:51 +00:00
|
|
|
} else {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_WARNING, "%s: job could not be %s (%s)",
|
|
|
|
pp->printer,
|
|
|
|
pp->remote ? "sent to remote host" : "printed",
|
2000-11-06 19:36:38 +00:00
|
|
|
q->job_cfname);
|
1996-05-05 22:40:51 +00:00
|
|
|
if (i == REPRINT) {
|
1997-07-29 04:17:19 +00:00
|
|
|
/* ensure we don't attempt this job again */
|
2000-11-06 19:36:38 +00:00
|
|
|
(void) unlink(q->job_cfname);
|
|
|
|
q->job_cfname[0] = 'd';
|
|
|
|
(void) unlink(q->job_cfname);
|
1996-05-05 22:40:51 +00:00
|
|
|
if (logname[0])
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, logname, FATALERR);
|
1996-05-05 22:40:51 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
free(queue);
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* search the spool directory for more work.
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((nitems = getq(pp, &queue)) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: can't scan %s", pp->printer,
|
|
|
|
pp->spool_dir);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (nitems == 0) { /* no more work to do */
|
|
|
|
done:
|
2000-11-14 21:44:47 +00:00
|
|
|
if (jobcount > 0) { /* jobs actually printed */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_formfeed && !pp->tof)
|
|
|
|
(void) write(ofd, pp->form_feed,
|
|
|
|
strlen(pp->form_feed));
|
|
|
|
if (pp->trailer != NULL) /* output trailer */
|
|
|
|
(void) write(ofd, pp->trailer,
|
|
|
|
strlen(pp->trailer));
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
1996-10-27 03:06:52 +00:00
|
|
|
(void) close(ofd);
|
|
|
|
(void) wait(NULL);
|
2000-11-13 05:44:50 +00:00
|
|
|
(void) unlink(tempstderr);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
char fonts[4][50]; /* fonts for troff */
|
|
|
|
|
|
|
|
char ifonts[4][40] = {
|
|
|
|
_PATH_VFONTR,
|
|
|
|
_PATH_VFONTI,
|
|
|
|
_PATH_VFONTB,
|
|
|
|
_PATH_VFONTS,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The remaining part is the reading of the control file (cf)
|
|
|
|
* and performing the various actions.
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
printit(struct printer *pp, char *file)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register int i;
|
2000-11-14 22:03:59 +00:00
|
|
|
char *cp;
|
|
|
|
int bombed, didignorehdr;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2000-11-14 22:03:59 +00:00
|
|
|
bombed = OK;
|
|
|
|
didignorehdr = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* open control file; ignore if no longer there.
|
|
|
|
*/
|
|
|
|
if ((cfp = fopen(file, "r")) == NULL) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_INFO, "%s: %s: %m", pp->printer, file);
|
1994-05-26 05:23:31 +00:00
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Reset troff fonts.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 4; i++)
|
|
|
|
strcpy(fonts[i], ifonts[i]);
|
1997-12-02 20:46:22 +00:00
|
|
|
sprintf(&width[2], "%ld", pp->page_width);
|
1994-05-26 05:23:31 +00:00
|
|
|
strcpy(indent+2, "0");
|
|
|
|
|
2000-11-02 19:22:06 +00:00
|
|
|
/* initialize job-specific count of datafiles processed */
|
|
|
|
job_dfcnt = 0;
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* read the control file for work to do
|
|
|
|
*
|
|
|
|
* file format -- first character in the line is a command
|
|
|
|
* rest of the line is the argument.
|
|
|
|
* valid commands are:
|
|
|
|
*
|
|
|
|
* S -- "stat info" for symbolic link protection
|
|
|
|
* J -- "job name" on banner page
|
|
|
|
* C -- "class name" on banner page
|
|
|
|
* L -- "literal" user's name to print on banner
|
|
|
|
* T -- "title" for pr
|
|
|
|
* H -- "host name" of machine where lpr was done
|
|
|
|
* P -- "person" user's login name
|
|
|
|
* I -- "indent" amount to indent output
|
1996-05-05 22:40:51 +00:00
|
|
|
* R -- laser dpi "resolution"
|
1994-05-26 05:23:31 +00:00
|
|
|
* f -- "file name" name of text file to print
|
|
|
|
* l -- "file name" text file with control chars
|
2001-09-20 01:04:50 +00:00
|
|
|
* o -- "file name" postscript file, according to
|
|
|
|
* the RFC. Here it is treated like an 'f'.
|
1994-05-26 05:23:31 +00:00
|
|
|
* p -- "file name" text file to print with pr(1)
|
|
|
|
* t -- "file name" troff(1) file to print
|
|
|
|
* n -- "file name" ditroff(1) file to print
|
|
|
|
* d -- "file name" dvi file to print
|
|
|
|
* g -- "file name" plot(1G) file to print
|
|
|
|
* v -- "file name" plain raster file to print
|
|
|
|
* c -- "file name" cifplot file to print
|
|
|
|
* 1 -- "R font file" for troff
|
|
|
|
* 2 -- "I font file" for troff
|
|
|
|
* 3 -- "B font file" for troff
|
|
|
|
* 4 -- "S font file" for troff
|
|
|
|
* N -- "name" of file (used by lpq)
|
|
|
|
* U -- "unlink" name of file to remove
|
|
|
|
* (after we print it. (Pass 2 only)).
|
|
|
|
* M -- "mail" to user when done printing
|
1999-11-30 16:15:22 +00:00
|
|
|
* Z -- "locale" for pr
|
1994-05-26 05:23:31 +00:00
|
|
|
*
|
|
|
|
* getline reads a line and expands tabs to blanks
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* pass 1 */
|
|
|
|
|
|
|
|
while (getline(cfp))
|
|
|
|
switch (line[0]) {
|
|
|
|
case 'H':
|
2001-06-15 22:03:07 +00:00
|
|
|
strlcpy(origin_host, line + 1, sizeof(origin_host));
|
1997-07-29 04:17:19 +00:00
|
|
|
if (class[0] == '\0') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(class, line+1, sizeof(class));
|
1997-07-29 04:17:19 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'P':
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(logname, line + 1, sizeof(logname));
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->restricted) { /* restricted */
|
1994-05-26 05:23:31 +00:00
|
|
|
if (getpwnam(logname) == NULL) {
|
|
|
|
bombed = NOACCT;
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, line+1, bombed);
|
1994-05-26 05:23:31 +00:00
|
|
|
goto pass2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
cp = line+1;
|
|
|
|
i = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
i = i * 10 + (*cp++ - '0');
|
|
|
|
fdev = i;
|
|
|
|
cp++;
|
|
|
|
i = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
i = i * 10 + (*cp++ - '0');
|
|
|
|
fino = i;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'J':
|
1997-07-29 04:17:19 +00:00
|
|
|
if (line[1] != '\0') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(jobname, line + 1, sizeof(jobname));
|
1997-07-29 04:17:19 +00:00
|
|
|
} else
|
1994-05-26 05:23:31 +00:00
|
|
|
strcpy(jobname, " ");
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'C':
|
|
|
|
if (line[1] != '\0')
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(class, line + 1, sizeof(class));
|
|
|
|
else if (class[0] == '\0') {
|
|
|
|
/* XXX - why call gethostname instead of
|
|
|
|
* just strlcpy'ing local_host? */
|
1994-05-26 05:23:31 +00:00
|
|
|
gethostname(class, sizeof(class));
|
2001-07-22 07:25:27 +00:00
|
|
|
class[sizeof(class) - 1] = '\0';
|
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'T': /* header title for pr */
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(title, line + 1, sizeof(title));
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'L': /* identification line */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_header && !pp->header_last)
|
|
|
|
banner(pp, line+1, jobname);
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case '1': /* troff fonts */
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
1997-07-29 04:17:19 +00:00
|
|
|
if (line[1] != '\0') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(fonts[line[0]-'1'], line + 1,
|
|
|
|
(size_t)50);
|
1997-07-29 04:17:19 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'W': /* page width */
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(width+2, line + 1, sizeof(width) - 2);
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'I': /* indent amount */
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(indent+2, line + 1, sizeof(indent) - 2);
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
1999-11-30 16:15:22 +00:00
|
|
|
case 'Z': /* locale for pr */
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(locale, line + 1, sizeof(locale));
|
1999-11-30 16:15:22 +00:00
|
|
|
continue;
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
default: /* some file to print */
|
2000-11-08 00:52:11 +00:00
|
|
|
/* only lowercase cmd-codes include a file-to-print */
|
|
|
|
if ((line[0] < 'a') || (line[0] > 'z')) {
|
|
|
|
/* ignore any other lines */
|
|
|
|
if (lflag <= 1)
|
|
|
|
continue;
|
|
|
|
if (!didignorehdr) {
|
|
|
|
syslog(LOG_INFO, "%s: in %s :",
|
|
|
|
pp->printer, file);
|
|
|
|
didignorehdr = 1;
|
|
|
|
}
|
|
|
|
syslog(LOG_INFO, "%s: ignoring line: '%c' %s",
|
|
|
|
pp->printer, line[0], &line[1]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
i = print(pp, line[0], line+1);
|
|
|
|
switch (i) {
|
1994-05-26 05:23:31 +00:00
|
|
|
case ERROR:
|
|
|
|
if (bombed == OK)
|
|
|
|
bombed = FATALERR;
|
|
|
|
break;
|
|
|
|
case REPRINT:
|
|
|
|
(void) fclose(cfp);
|
|
|
|
return(REPRINT);
|
|
|
|
case FILTERERR:
|
|
|
|
case ACCESS:
|
|
|
|
bombed = i;
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, logname, bombed);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
title[0] = '\0';
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'N':
|
|
|
|
case 'U':
|
|
|
|
case 'M':
|
1996-05-05 22:40:51 +00:00
|
|
|
case 'R':
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pass 2 */
|
|
|
|
|
|
|
|
pass2:
|
|
|
|
fseek(cfp, 0L, 0);
|
|
|
|
while (getline(cfp))
|
|
|
|
switch (line[0]) {
|
|
|
|
case 'L': /* identification line */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_header && pp->header_last)
|
|
|
|
banner(pp, line+1, jobname);
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
if (bombed < NOACCT) /* already sent if >= NOACCT */
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, line+1, bombed);
|
1994-05-26 05:23:31 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'U':
|
1997-07-29 04:17:19 +00:00
|
|
|
if (strchr(line+1, '/'))
|
|
|
|
continue;
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) unlink(line+1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* clean-up in case another control file exists
|
|
|
|
*/
|
|
|
|
(void) fclose(cfp);
|
|
|
|
(void) unlink(file);
|
|
|
|
return(bombed == OK ? OK : ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print a file.
|
|
|
|
* Set up the chain [ PR [ | {IF, OF} ] ] or {IF, RF, TF, NF, DF, CF, VF}.
|
|
|
|
* Return -1 if a non-recoverable error occured,
|
|
|
|
* 2 if the filter detected some errors (but printed the job anyway),
|
|
|
|
* 1 if we should try to reprint this job and
|
|
|
|
* 0 if all is well.
|
|
|
|
* Note: all filters take stdin as the file, stdout as the printer,
|
|
|
|
* stderr as the log file, and must not ignore SIGINT.
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
print(struct printer *pp, int format, char *file)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
1999-11-30 16:15:22 +00:00
|
|
|
register int n, i;
|
1994-05-26 05:23:31 +00:00
|
|
|
register char *prog;
|
1997-12-02 20:46:22 +00:00
|
|
|
int fi, fo;
|
1994-05-26 05:23:31 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *av[15], buf[BUFSIZ];
|
2002-06-04 01:16:13 +00:00
|
|
|
int pid, p[2], retcode, stopped, wstatus, wstatus_set;
|
1994-05-26 05:23:31 +00:00
|
|
|
struct stat stb;
|
|
|
|
|
2000-11-08 00:52:11 +00:00
|
|
|
if (lstat(file, &stb) < 0 || (fi = open(file, O_RDONLY)) < 0) {
|
|
|
|
syslog(LOG_INFO, "%s: unable to open %s ('%c' line)",
|
|
|
|
pp->printer, file, format);
|
1994-05-26 05:23:31 +00:00
|
|
|
return(ERROR);
|
2000-11-08 00:52:11 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Check to see if data file is a symbolic link. If so, it should
|
|
|
|
* still point to the same file or someone is trying to print
|
|
|
|
* something he shouldn't.
|
|
|
|
*/
|
|
|
|
if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(fi, &stb) == 0 &&
|
|
|
|
(stb.st_dev != fdev || stb.st_ino != fino))
|
|
|
|
return(ACCESS);
|
2000-11-02 19:22:06 +00:00
|
|
|
|
|
|
|
job_dfcnt++; /* increment datafile counter for this job */
|
2000-11-14 22:03:59 +00:00
|
|
|
stopped = 0; /* output filter is not stopped */
|
2000-11-02 19:22:06 +00:00
|
|
|
|
|
|
|
/* everything seems OK, start it up */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_formfeed && !pp->tof) { /* start on a fresh page */
|
|
|
|
(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
|
|
|
|
pp->tof = 1;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->filters[LPF_INPUT] == NULL
|
2001-09-20 01:04:50 +00:00
|
|
|
&& (format == 'f' || format == 'l' || format == 'o')) {
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->tof = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
while ((n = read(fi, buf, BUFSIZ)) > 0)
|
|
|
|
if (write(ofd, buf, n) != n) {
|
|
|
|
(void) close(fi);
|
|
|
|
return(REPRINT);
|
|
|
|
}
|
|
|
|
(void) close(fi);
|
|
|
|
return(OK);
|
|
|
|
}
|
|
|
|
switch (format) {
|
|
|
|
case 'p': /* print file using 'pr' */
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->filters[LPF_INPUT] == NULL) { /* use output filter */
|
1994-05-26 05:23:31 +00:00
|
|
|
prog = _PATH_PR;
|
1999-11-30 16:15:22 +00:00
|
|
|
i = 0;
|
|
|
|
av[i++] = "pr";
|
|
|
|
av[i++] = width;
|
|
|
|
av[i++] = length;
|
|
|
|
av[i++] = "-h";
|
|
|
|
av[i++] = *title ? title : " ";
|
|
|
|
av[i++] = "-L";
|
|
|
|
av[i++] = *locale ? locale : "C";
|
|
|
|
av[i++] = "-F";
|
|
|
|
av[i] = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
fo = ofd;
|
|
|
|
goto start;
|
|
|
|
}
|
|
|
|
pipe(p);
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((prchild = dofork(pp, DORETURN)) == 0) { /* child */
|
1994-05-26 05:23:31 +00:00
|
|
|
dup2(fi, 0); /* file is stdin */
|
|
|
|
dup2(p[1], 1); /* pipe is stdout */
|
1995-04-27 04:56:10 +00:00
|
|
|
closelog();
|
1997-12-02 20:46:22 +00:00
|
|
|
closeallfds(3);
|
1994-05-26 05:23:31 +00:00
|
|
|
execl(_PATH_PR, "pr", width, length,
|
1999-11-30 16:15:22 +00:00
|
|
|
"-h", *title ? title : " ",
|
|
|
|
"-L", *locale ? locale : "C",
|
2001-07-09 09:24:06 +00:00
|
|
|
"-F", (char *)0);
|
1994-05-26 05:23:31 +00:00
|
|
|
syslog(LOG_ERR, "cannot execl %s", _PATH_PR);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
(void) close(p[1]); /* close output side */
|
|
|
|
(void) close(fi);
|
|
|
|
if (prchild < 0) {
|
|
|
|
prchild = 0;
|
|
|
|
(void) close(p[0]);
|
|
|
|
return(ERROR);
|
|
|
|
}
|
|
|
|
fi = p[0]; /* use pipe for input */
|
|
|
|
case 'f': /* print plain text file */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_INPUT];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = width;
|
|
|
|
av[2] = length;
|
|
|
|
av[3] = indent;
|
|
|
|
n = 4;
|
|
|
|
break;
|
2001-11-27 01:32:25 +00:00
|
|
|
case 'o': /* print postscript file */
|
|
|
|
/*
|
|
|
|
* Treat this as a "plain file with control characters", and
|
|
|
|
* assume the standard LPF_INPUT filter will recognize that
|
|
|
|
* the data is postscript and know what to do with it. These
|
|
|
|
* 'o'-file requests could come from MacOS 10.1 systems.
|
|
|
|
* (later versions of MacOS 10 will explicitly use 'l')
|
|
|
|
* A postscript file can contain binary data, which is why 'l'
|
|
|
|
* is somewhat more appropriate than 'f'.
|
|
|
|
*/
|
|
|
|
/* FALLTHROUGH */
|
1994-05-26 05:23:31 +00:00
|
|
|
case 'l': /* like 'f' but pass control characters */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_INPUT];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = "-c";
|
|
|
|
av[2] = width;
|
|
|
|
av[3] = length;
|
|
|
|
av[4] = indent;
|
|
|
|
n = 5;
|
|
|
|
break;
|
|
|
|
case 'r': /* print a fortran text file */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_FORTRAN];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = width;
|
|
|
|
av[2] = length;
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
case 't': /* print troff output */
|
|
|
|
case 'n': /* print ditroff output */
|
|
|
|
case 'd': /* print tex output */
|
|
|
|
(void) unlink(".railmag");
|
|
|
|
if ((fo = creat(".railmag", FILMOD)) < 0) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: cannot create .railmag",
|
|
|
|
pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) unlink(".railmag");
|
|
|
|
} else {
|
|
|
|
for (n = 0; n < 4; n++) {
|
|
|
|
if (fonts[n][0] != '/')
|
|
|
|
(void) write(fo, _PATH_VFONT,
|
|
|
|
sizeof(_PATH_VFONT) - 1);
|
|
|
|
(void) write(fo, fonts[n], strlen(fonts[n]));
|
|
|
|
(void) write(fo, "\n", 1);
|
|
|
|
}
|
|
|
|
(void) close(fo);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = (format == 't') ? pp->filters[LPF_TROFF]
|
|
|
|
: ((format == 'n') ? pp->filters[LPF_DITROFF]
|
|
|
|
: pp->filters[LPF_DVI]);
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = pxwidth;
|
|
|
|
av[2] = pxlength;
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
case 'c': /* print cifplot output */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_CIFPLOT];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = pxwidth;
|
|
|
|
av[2] = pxlength;
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
case 'g': /* print plot(1G) output */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_GRAPH];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = pxwidth;
|
|
|
|
av[2] = pxlength;
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
case 'v': /* print raster output */
|
1997-12-02 20:46:22 +00:00
|
|
|
prog = pp->filters[LPF_RASTER];
|
1994-05-26 05:23:31 +00:00
|
|
|
av[1] = pxwidth;
|
|
|
|
av[2] = pxlength;
|
|
|
|
n = 3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
(void) close(fi);
|
|
|
|
syslog(LOG_ERR, "%s: illegal format character '%c'",
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->printer, format);
|
1994-05-26 05:23:31 +00:00
|
|
|
return(ERROR);
|
|
|
|
}
|
1996-05-05 22:40:51 +00:00
|
|
|
if (prog == NULL) {
|
|
|
|
(void) close(fi);
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"%s: no filter found in printcap for format character '%c'",
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->printer, format);
|
1996-05-05 22:40:51 +00:00
|
|
|
return(ERROR);
|
|
|
|
}
|
1997-07-23 20:53:41 +00:00
|
|
|
if ((av[0] = strrchr(prog, '/')) != NULL)
|
1994-05-26 05:23:31 +00:00
|
|
|
av[0]++;
|
|
|
|
else
|
|
|
|
av[0] = prog;
|
|
|
|
av[n++] = "-n";
|
|
|
|
av[n++] = logname;
|
|
|
|
av[n++] = "-h";
|
2001-06-15 22:03:07 +00:00
|
|
|
av[n++] = origin_host;
|
1997-12-02 20:46:22 +00:00
|
|
|
av[n++] = pp->acct_file;
|
1994-05-26 05:23:31 +00:00
|
|
|
av[n] = 0;
|
|
|
|
fo = pfd;
|
|
|
|
if (ofilter > 0) { /* stop output filter */
|
|
|
|
write(ofd, "\031\1", 2);
|
|
|
|
while ((pid =
|
2002-06-03 20:47:01 +00:00
|
|
|
wait3(&wstatus, WUNTRACED, 0)) > 0 && pid != ofilter)
|
1994-05-26 05:23:31 +00:00
|
|
|
;
|
2001-07-14 21:49:17 +00:00
|
|
|
if (pid < 0)
|
|
|
|
syslog(LOG_WARNING, "%s: after stopping 'of', wait3() returned: %m",
|
|
|
|
pp->printer);
|
2002-06-03 20:47:01 +00:00
|
|
|
else if (!WIFSTOPPED(wstatus)) {
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) close(fi);
|
2002-06-03 20:47:01 +00:00
|
|
|
syslog(LOG_WARNING, "%s: output filter died "
|
|
|
|
"(pid=%d retcode=%d termsig=%d)",
|
|
|
|
pp->printer, ofilter, WEXITSTATUS(wstatus),
|
|
|
|
WTERMSIG(wstatus));
|
1994-05-26 05:23:31 +00:00
|
|
|
return(REPRINT);
|
|
|
|
}
|
|
|
|
stopped++;
|
|
|
|
}
|
|
|
|
start:
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((child = dofork(pp, DORETURN)) == 0) { /* child */
|
1994-05-26 05:23:31 +00:00
|
|
|
dup2(fi, 0);
|
|
|
|
dup2(fo, 1);
|
2000-11-13 05:44:50 +00:00
|
|
|
/* setup stderr for the filter (child process)
|
|
|
|
* so it goes to our temporary errors file */
|
|
|
|
n = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
|
1994-05-26 05:23:31 +00:00
|
|
|
if (n >= 0)
|
|
|
|
dup2(n, 2);
|
1995-04-27 04:56:10 +00:00
|
|
|
closelog();
|
1997-12-02 20:46:22 +00:00
|
|
|
closeallfds(3);
|
1994-05-26 05:23:31 +00:00
|
|
|
execv(prog, av);
|
2002-04-19 18:28:35 +00:00
|
|
|
syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer,
|
|
|
|
prog);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
(void) close(fi);
|
2002-06-04 01:16:13 +00:00
|
|
|
wstatus_set = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
if (child < 0)
|
2002-06-03 20:47:01 +00:00
|
|
|
retcode = 100;
|
2001-07-14 21:49:17 +00:00
|
|
|
else {
|
2002-06-03 20:47:01 +00:00
|
|
|
while ((pid = wait(&wstatus)) > 0 && pid != child)
|
1994-05-26 05:23:31 +00:00
|
|
|
;
|
2001-07-14 21:49:17 +00:00
|
|
|
if (pid < 0) {
|
2002-06-03 20:47:01 +00:00
|
|
|
retcode = 100;
|
2001-07-14 21:49:17 +00:00
|
|
|
syslog(LOG_WARNING, "%s: after execv(%s), wait() returned: %m",
|
|
|
|
pp->printer, prog);
|
2002-06-04 01:16:13 +00:00
|
|
|
} else {
|
|
|
|
wstatus_set = 1;
|
2002-06-03 20:47:01 +00:00
|
|
|
retcode = WEXITSTATUS(wstatus);
|
2002-06-04 01:16:13 +00:00
|
|
|
}
|
2001-07-14 21:49:17 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
child = 0;
|
|
|
|
prchild = 0;
|
|
|
|
if (stopped) { /* restart output filter */
|
|
|
|
if (kill(ofilter, SIGCONT) < 0) {
|
|
|
|
syslog(LOG_ERR, "cannot restart output filter");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->tof = 0;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2000-11-13 05:44:50 +00:00
|
|
|
/* Copy the filter's output to "lf" logfile */
|
|
|
|
if ((fp = fopen(tempstderr, "r"))) {
|
1994-05-26 05:23:31 +00:00
|
|
|
while (fgets(buf, sizeof(buf), fp))
|
|
|
|
fputs(buf, stderr);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2002-06-04 01:16:13 +00:00
|
|
|
if (wstatus_set && !WIFEXITED(wstatus)) {
|
1996-05-05 22:40:51 +00:00
|
|
|
syslog(LOG_WARNING, "%s: filter '%c' terminated (termsig=%d)",
|
2002-06-03 20:47:01 +00:00
|
|
|
pp->printer, format, WTERMSIG(wstatus));
|
1994-05-26 05:23:31 +00:00
|
|
|
return(ERROR);
|
|
|
|
}
|
2002-06-03 20:47:01 +00:00
|
|
|
switch (retcode) {
|
1994-05-26 05:23:31 +00:00
|
|
|
case 0:
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->tof = 1;
|
1994-05-26 05:23:31 +00:00
|
|
|
return(OK);
|
|
|
|
case 1:
|
|
|
|
return(REPRINT);
|
|
|
|
case 2:
|
|
|
|
return(ERROR);
|
1996-05-05 22:40:51 +00:00
|
|
|
default:
|
|
|
|
syslog(LOG_WARNING, "%s: filter '%c' exited (retcode=%d)",
|
2002-06-03 20:47:01 +00:00
|
|
|
pp->printer, format, retcode);
|
1996-05-05 22:40:51 +00:00
|
|
|
return(FILTERERR);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send the daemon control file (cf) and any data files.
|
|
|
|
* Return -1 if a non-recoverable error occured, 1 if a recoverable error and
|
|
|
|
* 0 if all is well.
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
sendit(struct printer *pp, char *file)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2002-04-23 00:06:10 +00:00
|
|
|
int dfcopies, err, i;
|
1994-05-26 05:23:31 +00:00
|
|
|
char *cp, last[BUFSIZ];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open control file
|
|
|
|
*/
|
|
|
|
if ((cfp = fopen(file, "r")) == NULL)
|
|
|
|
return(OK);
|
2000-11-02 19:22:06 +00:00
|
|
|
|
|
|
|
/* initialize job-specific count of datafiles processed */
|
|
|
|
job_dfcnt = 0;
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* read the control file for work to do
|
|
|
|
*
|
|
|
|
* file format -- first character in the line is a command
|
|
|
|
* rest of the line is the argument.
|
|
|
|
* commands of interest are:
|
|
|
|
*
|
|
|
|
* a-z -- "file name" name of file to print
|
|
|
|
* U -- "unlink" name of file to remove
|
|
|
|
* (after we print it. (Pass 2 only)).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pass 1
|
|
|
|
*/
|
2002-04-23 00:06:10 +00:00
|
|
|
err = OK;
|
1994-05-26 05:23:31 +00:00
|
|
|
while (getline(cfp)) {
|
|
|
|
again:
|
|
|
|
if (line[0] == 'S') {
|
|
|
|
cp = line+1;
|
|
|
|
i = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
i = i * 10 + (*cp++ - '0');
|
|
|
|
fdev = i;
|
|
|
|
cp++;
|
|
|
|
i = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
i = i * 10 + (*cp++ - '0');
|
|
|
|
fino = i;
|
1997-04-12 04:23:13 +00:00
|
|
|
} else if (line[0] == 'H') {
|
2001-06-15 22:03:07 +00:00
|
|
|
strlcpy(origin_host, line + 1, sizeof(origin_host));
|
2000-11-05 03:24:26 +00:00
|
|
|
if (class[0] == '\0') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(class, line + 1, sizeof(class));
|
2000-11-05 03:24:26 +00:00
|
|
|
}
|
1997-04-12 04:23:13 +00:00
|
|
|
} else if (line[0] == 'P') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(logname, line + 1, sizeof(logname));
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->restricted) { /* restricted */
|
1997-04-12 04:23:13 +00:00
|
|
|
if (getpwnam(logname) == NULL) {
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, line+1, NOACCT);
|
1997-04-12 04:23:13 +00:00
|
|
|
err = ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (line[0] == 'I') {
|
2001-07-22 07:25:27 +00:00
|
|
|
strlcpy(indent+2, line + 1, sizeof(indent) - 2);
|
1997-04-12 04:23:13 +00:00
|
|
|
} else if (line[0] >= 'a' && line[0] <= 'z') {
|
2002-04-23 00:06:10 +00:00
|
|
|
dfcopies = 1;
|
1994-05-26 05:23:31 +00:00
|
|
|
strcpy(last, line);
|
2002-04-23 00:06:10 +00:00
|
|
|
while ((i = getline(cfp)) != 0) {
|
|
|
|
if (strcmp(last, line) != 0)
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
2002-04-23 00:06:10 +00:00
|
|
|
dfcopies++;
|
|
|
|
}
|
|
|
|
switch (sendfile(pp, '\3', last+1, *last, dfcopies)) {
|
1994-05-26 05:23:31 +00:00
|
|
|
case OK:
|
|
|
|
if (i)
|
|
|
|
goto again;
|
|
|
|
break;
|
|
|
|
case REPRINT:
|
|
|
|
(void) fclose(cfp);
|
|
|
|
return(REPRINT);
|
|
|
|
case ACCESS:
|
1997-12-02 20:46:22 +00:00
|
|
|
sendmail(pp, logname, ACCESS);
|
1994-05-26 05:23:31 +00:00
|
|
|
case ERROR:
|
|
|
|
err = ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2002-04-23 00:06:10 +00:00
|
|
|
if (err == OK && sendfile(pp, '\2', file, '\0', 1) > 0) {
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) fclose(cfp);
|
|
|
|
return(REPRINT);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* pass 2
|
|
|
|
*/
|
|
|
|
fseek(cfp, 0L, 0);
|
|
|
|
while (getline(cfp))
|
1997-07-29 04:17:19 +00:00
|
|
|
if (line[0] == 'U' && !strchr(line+1, '/'))
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) unlink(line+1);
|
|
|
|
/*
|
|
|
|
* clean-up in case another control file exists
|
|
|
|
*/
|
|
|
|
(void) fclose(cfp);
|
|
|
|
(void) unlink(file);
|
|
|
|
return(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a data file to the remote machine and spool it.
|
|
|
|
* Return positive if we should try resending.
|
|
|
|
*/
|
|
|
|
static int
|
2002-04-23 00:06:10 +00:00
|
|
|
sendfile(struct printer *pp, int type, char *file, char format, int copyreq)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2002-04-07 07:48:32 +00:00
|
|
|
int i, amt;
|
1994-05-26 05:23:31 +00:00
|
|
|
struct stat stb;
|
2002-04-07 05:37:27 +00:00
|
|
|
char *av[15], *filtcmd;
|
|
|
|
char buf[BUFSIZ], opt_c[4], opt_h[4], opt_n[4];
|
2002-04-23 00:06:10 +00:00
|
|
|
int copycnt, filtstat, narg, resp, sfd, sfres, sizerr, statrc;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2001-03-12 00:22:03 +00:00
|
|
|
statrc = lstat(file, &stb);
|
|
|
|
if (statrc < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: error from lstat(%s): %m",
|
|
|
|
pp->printer, file);
|
2002-04-07 07:48:32 +00:00
|
|
|
return (ERROR);
|
2001-03-12 00:22:03 +00:00
|
|
|
}
|
2002-04-07 07:48:32 +00:00
|
|
|
sfd = open(file, O_RDONLY);
|
|
|
|
if (sfd < 0) {
|
2001-03-12 00:22:03 +00:00
|
|
|
syslog(LOG_ERR, "%s: error from open(%s,O_RDONLY): %m",
|
|
|
|
pp->printer, file);
|
2002-04-07 07:48:32 +00:00
|
|
|
return (ERROR);
|
2001-03-12 00:22:03 +00:00
|
|
|
}
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Check to see if data file is a symbolic link. If so, it should
|
|
|
|
* still point to the same file or someone is trying to print something
|
|
|
|
* he shouldn't.
|
|
|
|
*/
|
2002-04-07 07:48:32 +00:00
|
|
|
if ((stb.st_mode & S_IFMT) == S_IFLNK && fstat(sfd, &stb) == 0 &&
|
|
|
|
(stb.st_dev != fdev || stb.st_ino != fino)) {
|
|
|
|
close(sfd);
|
|
|
|
return (ACCESS);
|
|
|
|
}
|
1997-04-12 04:23:13 +00:00
|
|
|
|
2002-04-07 05:37:27 +00:00
|
|
|
/* Everything seems OK for reading the file, now to send it */
|
|
|
|
filtcmd = NULL;
|
1997-04-12 04:23:13 +00:00
|
|
|
sizerr = 0;
|
2002-04-07 05:37:27 +00:00
|
|
|
tfd = -1;
|
1997-04-12 04:23:13 +00:00
|
|
|
if (type == '\3') {
|
2002-04-07 05:37:27 +00:00
|
|
|
/*
|
|
|
|
* Type == 3 means this is a datafile, not a control file.
|
|
|
|
* Increment the counter of data-files in this job, and
|
|
|
|
* then check for input or output filters (which are only
|
|
|
|
* applied to datafiles, not control files).
|
|
|
|
*/
|
|
|
|
job_dfcnt++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that here we are filtering datafiles, one at a time,
|
|
|
|
* as they are sent to the remote machine. Here, the *only*
|
|
|
|
* difference between an input filter (`if=') and an output
|
|
|
|
* filter (`of=') is the argument list that the filter is
|
|
|
|
* started up with. Here, the output filter is executed
|
|
|
|
* for each individual file as it is sent. This is not the
|
|
|
|
* same as local print queues, where the output filter is
|
|
|
|
* started up once, and then all jobs are passed thru that
|
|
|
|
* single invocation of the output filter.
|
|
|
|
*
|
|
|
|
* Also note that a queue for a remote-machine can have an
|
|
|
|
* input filter or an output filter, but not both.
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->filters[LPF_INPUT]) {
|
2002-04-07 05:37:27 +00:00
|
|
|
filtcmd = pp->filters[LPF_INPUT];
|
|
|
|
av[0] = filtcmd;
|
|
|
|
narg = 0;
|
|
|
|
strcpy(opt_c, "-c");
|
|
|
|
strcpy(opt_h, "-h");
|
|
|
|
strcpy(opt_n, "-n");
|
1997-04-12 04:23:13 +00:00
|
|
|
if (format == 'l')
|
2002-04-07 05:37:27 +00:00
|
|
|
av[++narg] = opt_c;
|
|
|
|
av[++narg] = width;
|
|
|
|
av[++narg] = length;
|
|
|
|
av[++narg] = indent;
|
|
|
|
av[++narg] = opt_n;
|
|
|
|
av[++narg] = logname;
|
|
|
|
av[++narg] = opt_h;
|
|
|
|
av[++narg] = origin_host;
|
|
|
|
av[++narg] = pp->acct_file;
|
|
|
|
av[++narg] = NULL;
|
|
|
|
} else if (pp->filters[LPF_OUTPUT]) {
|
|
|
|
filtcmd = pp->filters[LPF_OUTPUT];
|
|
|
|
av[0] = filtcmd;
|
|
|
|
narg = 0;
|
|
|
|
av[++narg] = width;
|
|
|
|
av[++narg] = length;
|
|
|
|
av[++narg] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (filtcmd) {
|
|
|
|
/*
|
|
|
|
* If there is an input or output filter, we have to run
|
|
|
|
* the datafile thru that filter and store the result as
|
|
|
|
* a temporary spool file, because the protocol requires
|
|
|
|
* that we send the remote host the file-size before we
|
|
|
|
* start to send any of the data.
|
|
|
|
*/
|
|
|
|
strcpy(tfile, TFILENAME);
|
|
|
|
tfd = mkstemp(tfile);
|
|
|
|
if (tfd == -1) {
|
|
|
|
syslog(LOG_ERR, "%s: mkstemp(%s): %m", pp->printer,
|
|
|
|
TFILENAME);
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = ERROR;
|
|
|
|
goto return_sfres;
|
2002-04-07 05:37:27 +00:00
|
|
|
}
|
2002-04-07 07:48:32 +00:00
|
|
|
filtstat = execfilter(pp, filtcmd, av, sfd, tfd);
|
2002-04-07 05:37:27 +00:00
|
|
|
|
|
|
|
/* process the return-code from the filter */
|
|
|
|
switch (filtstat) {
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 1:
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = REPRINT;
|
|
|
|
goto return_sfres;
|
2002-04-07 05:37:27 +00:00
|
|
|
case 2:
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = ERROR;
|
|
|
|
goto return_sfres;
|
2002-04-07 05:37:27 +00:00
|
|
|
default:
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"%s: filter '%c' exited (retcode=%d)",
|
|
|
|
pp->printer, format, filtstat);
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = FILTERERR;
|
|
|
|
goto return_sfres;
|
2002-04-07 05:37:27 +00:00
|
|
|
}
|
|
|
|
statrc = fstat(tfd, &stb); /* to find size of tfile */
|
|
|
|
if (statrc < 0) {
|
|
|
|
syslog(LOG_ERR,
|
|
|
|
"%s: error processing 'if', fstat(%s): %m",
|
|
|
|
pp->printer, tfile);
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = ERROR;
|
|
|
|
goto return_sfres;
|
1997-04-12 04:23:13 +00:00
|
|
|
}
|
2002-04-07 07:48:32 +00:00
|
|
|
close(sfd);
|
|
|
|
sfd = tfd;
|
|
|
|
lseek(sfd, 0, SEEK_SET);
|
1997-04-12 04:23:13 +00:00
|
|
|
}
|
|
|
|
|
2002-04-23 00:06:10 +00:00
|
|
|
copycnt = 0;
|
|
|
|
sendagain:
|
|
|
|
copycnt++;
|
|
|
|
|
|
|
|
if (copycnt < 2)
|
|
|
|
(void) sprintf(buf, "%c%qd %s\n", type, stb.st_size, file);
|
|
|
|
else
|
|
|
|
(void) sprintf(buf, "%c%qd %s_c%d\n", type, stb.st_size,
|
|
|
|
file, copycnt);
|
1994-05-26 05:23:31 +00:00
|
|
|
amt = strlen(buf);
|
|
|
|
for (i = 0; ; i++) {
|
|
|
|
if (write(pfd, buf, amt) != amt ||
|
1997-12-02 20:46:22 +00:00
|
|
|
(resp = response(pp)) < 0 || resp == '\1') {
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = REPRINT;
|
|
|
|
goto return_sfres;
|
1994-05-26 05:23:31 +00:00
|
|
|
} else if (resp == '\0')
|
|
|
|
break;
|
|
|
|
if (i == 0)
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp,
|
|
|
|
"no space on remote; waiting for queue to drain");
|
1994-05-26 05:23:31 +00:00
|
|
|
if (i == 10)
|
|
|
|
syslog(LOG_ALERT, "%s: can't send to %s; queue full",
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->printer, pp->remote_host);
|
1994-05-26 05:23:31 +00:00
|
|
|
sleep(5 * 60);
|
|
|
|
}
|
|
|
|
if (i)
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp, "sending to %s", pp->remote_host);
|
2002-04-23 00:06:10 +00:00
|
|
|
/*
|
|
|
|
* XXX - we should change trstat_init()/trstat_write() to include
|
|
|
|
* the copycnt in the statistics record it may write.
|
|
|
|
*/
|
2000-11-02 19:22:06 +00:00
|
|
|
if (type == '\3')
|
|
|
|
trstat_init(pp, file, job_dfcnt);
|
1994-05-26 05:23:31 +00:00
|
|
|
for (i = 0; i < stb.st_size; i += BUFSIZ) {
|
|
|
|
amt = BUFSIZ;
|
|
|
|
if (i + amt > stb.st_size)
|
|
|
|
amt = stb.st_size - i;
|
2002-04-07 07:48:32 +00:00
|
|
|
if (sizerr == 0 && read(sfd, buf, amt) != amt)
|
1994-05-26 05:23:31 +00:00
|
|
|
sizerr = 1;
|
|
|
|
if (write(pfd, buf, amt) != amt) {
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = REPRINT;
|
|
|
|
goto return_sfres;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizerr) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_INFO, "%s: %s: changed size", pp->printer, file);
|
1994-05-26 05:23:31 +00:00
|
|
|
/* tell recvjob to ignore this file */
|
|
|
|
(void) write(pfd, "\1", 1);
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = ERROR;
|
|
|
|
goto return_sfres;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (write(pfd, "", 1) != 1 || response(pp)) {
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = REPRINT;
|
|
|
|
goto return_sfres;
|
1997-04-12 04:23:13 +00:00
|
|
|
}
|
2002-04-23 00:06:10 +00:00
|
|
|
if (type == '\3') {
|
2000-11-02 19:22:06 +00:00
|
|
|
trstat_write(pp, TR_SENDING, stb.st_size, logname,
|
2002-04-23 00:06:10 +00:00
|
|
|
pp->remote_host, origin_host);
|
|
|
|
/*
|
|
|
|
* Usually we only need to send one copy of a datafile,
|
|
|
|
* because the control-file will simply print the same
|
|
|
|
* file multiple times. However, some printers ignore
|
|
|
|
* the control file, and simply print each data file as
|
|
|
|
* it arrives. For such "remote hosts", we need to
|
|
|
|
* transfer the same data file multiple times. Such a
|
|
|
|
* a host is indicated by adding 'rc' to the printcap
|
|
|
|
* entry.
|
|
|
|
* XXX - Right now this ONLY works for remote hosts which
|
|
|
|
* do ignore the name of the data file, because
|
|
|
|
* this sends the file multiple times with slight
|
|
|
|
* changes to the filename. To do this right would
|
|
|
|
* require that we also rewrite the control file
|
|
|
|
* to match those filenames.
|
|
|
|
*/
|
|
|
|
if (pp->resend_copies && (copycnt < copyreq)) {
|
|
|
|
lseek(sfd, 0, SEEK_SET);
|
|
|
|
goto sendagain;
|
|
|
|
}
|
|
|
|
}
|
2002-04-07 07:48:32 +00:00
|
|
|
sfres = OK;
|
|
|
|
|
|
|
|
return_sfres:
|
|
|
|
(void)close(sfd);
|
|
|
|
if (tfd != -1) {
|
|
|
|
/*
|
|
|
|
* If tfd is set, then it is the same value as sfd, and
|
|
|
|
* therefore it is already closed at this point. All
|
|
|
|
* we need to do is remove the temporary file.
|
|
|
|
*/
|
|
|
|
tfd = -1;
|
|
|
|
unlink(tfile);
|
|
|
|
}
|
|
|
|
return (sfres);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
2002-04-07 05:37:27 +00:00
|
|
|
/*
|
|
|
|
* This routine is called to execute one of the filters as was
|
2002-04-07 07:48:32 +00:00
|
|
|
* specified in a printcap entry. While the child-process will read
|
|
|
|
* all of 'infd', it is up to the caller to close that file descriptor
|
|
|
|
* in the parent process.
|
2002-04-07 05:37:27 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
execfilter(struct printer *pp, char *f_cmd, char *f_av[], int infd, int outfd)
|
|
|
|
{
|
2002-06-03 20:47:01 +00:00
|
|
|
int errfd, fpid, retcode, wpid, wstatus;
|
2002-04-07 05:37:27 +00:00
|
|
|
FILE *errfp;
|
|
|
|
char buf[BUFSIZ], *slash;
|
|
|
|
|
|
|
|
fpid = dofork(pp, DORETURN);
|
|
|
|
if (fpid != 0) {
|
|
|
|
/*
|
|
|
|
* This is the parent process, which just waits for the child
|
|
|
|
* to complete and then returns the result. Note that it is
|
|
|
|
* the child process which reads the input stream.
|
|
|
|
*/
|
|
|
|
if (fpid < 0)
|
2002-06-03 20:47:01 +00:00
|
|
|
retcode = 100;
|
2002-04-07 05:37:27 +00:00
|
|
|
else {
|
2002-06-03 20:47:01 +00:00
|
|
|
while ((wpid = wait(&wstatus)) > 0 &&
|
2002-04-07 05:37:27 +00:00
|
|
|
wpid != fpid)
|
|
|
|
;
|
|
|
|
if (wpid < 0) {
|
2002-06-03 20:47:01 +00:00
|
|
|
retcode = 100;
|
2002-04-07 05:37:27 +00:00
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"%s: after execv(%s), wait() returned: %m",
|
|
|
|
pp->printer, f_cmd);
|
2002-06-03 20:47:01 +00:00
|
|
|
} else
|
|
|
|
retcode = WEXITSTATUS(wstatus);
|
2002-04-07 05:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy everything the filter wrote to stderr from our
|
|
|
|
* temporary errors file to the "lf=" logfile.
|
|
|
|
*/
|
|
|
|
errfp = fopen(tempstderr, "r");
|
|
|
|
if (errfp) {
|
|
|
|
while (fgets(buf, sizeof(buf), errfp))
|
|
|
|
fputs(buf, stderr);
|
|
|
|
fclose(errfp);
|
|
|
|
}
|
|
|
|
|
2002-06-03 20:47:01 +00:00
|
|
|
return (retcode);
|
2002-04-07 05:37:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the child process, which is the one that executes the
|
|
|
|
* given filter.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* If the first parameter has any slashes in it, then change it
|
|
|
|
* to point to the first character after the last slash.
|
|
|
|
*/
|
|
|
|
slash = strrchr(f_av[0], '/');
|
|
|
|
if (slash != NULL)
|
|
|
|
f_av[0] = slash + 1;
|
|
|
|
/*
|
|
|
|
* XXX - in the future, this should setup an explicit list of
|
|
|
|
* environment variables and use execve()!
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup stdin, stdout, and stderr as we want them when the filter
|
|
|
|
* is running. Stderr is setup so it points to a temporary errors
|
|
|
|
* file, and the parent process will copy that temporary file to
|
|
|
|
* the real logfile after the filter completes.
|
|
|
|
*/
|
|
|
|
dup2(infd, 0);
|
|
|
|
dup2(outfd, 1);
|
|
|
|
errfd = open(tempstderr, O_WRONLY|O_TRUNC, 0664);
|
|
|
|
if (errfd >= 0)
|
|
|
|
dup2(errfd, 2);
|
|
|
|
closelog();
|
|
|
|
closeallfds(3);
|
|
|
|
execv(f_cmd, f_av);
|
2002-04-19 18:28:35 +00:00
|
|
|
syslog(LOG_ERR, "%s: cannot execv(%s): %m", pp->printer, f_cmd);
|
2002-04-07 05:37:27 +00:00
|
|
|
exit(2);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Check to make sure there have been no errors and that both programs
|
|
|
|
* are in sync with eachother.
|
|
|
|
* Return non-zero if the connection was lost.
|
|
|
|
*/
|
|
|
|
static char
|
2001-06-12 16:38:20 +00:00
|
|
|
response(const struct printer *pp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
char resp;
|
|
|
|
|
|
|
|
if (read(pfd, &resp, 1) != 1) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_INFO, "%s: lost connection", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
return(resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Banner printing stuff
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
banner(struct printer *pp, char *name1, char *name2)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
time_t tvec;
|
|
|
|
|
|
|
|
time(&tvec);
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_formfeed && !pp->tof)
|
|
|
|
(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
|
|
|
|
if (pp->short_banner) { /* short banner only */
|
1994-05-26 05:23:31 +00:00
|
|
|
if (class[0]) {
|
|
|
|
(void) write(ofd, class, strlen(class));
|
|
|
|
(void) write(ofd, ":", 1);
|
|
|
|
}
|
|
|
|
(void) write(ofd, name1, strlen(name1));
|
|
|
|
(void) write(ofd, " Job: ", 7);
|
|
|
|
(void) write(ofd, name2, strlen(name2));
|
|
|
|
(void) write(ofd, " Date: ", 8);
|
|
|
|
(void) write(ofd, ctime(&tvec), 24);
|
|
|
|
(void) write(ofd, "\n", 1);
|
|
|
|
} else { /* normal banner */
|
|
|
|
(void) write(ofd, "\n\n\n", 3);
|
1997-12-02 20:46:22 +00:00
|
|
|
scan_out(pp, ofd, name1, '\0');
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) write(ofd, "\n\n", 2);
|
1997-12-02 20:46:22 +00:00
|
|
|
scan_out(pp, ofd, name2, '\0');
|
1994-05-26 05:23:31 +00:00
|
|
|
if (class[0]) {
|
|
|
|
(void) write(ofd,"\n\n\n",3);
|
1997-12-02 20:46:22 +00:00
|
|
|
scan_out(pp, ofd, class, '\0');
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
(void) write(ofd, "\n\n\n\n\t\t\t\t\tJob: ", 15);
|
|
|
|
(void) write(ofd, name2, strlen(name2));
|
|
|
|
(void) write(ofd, "\n\t\t\t\t\tDate: ", 12);
|
|
|
|
(void) write(ofd, ctime(&tvec), 24);
|
|
|
|
(void) write(ofd, "\n", 1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (!pp->no_formfeed)
|
|
|
|
(void) write(ofd, pp->form_feed, strlen(pp->form_feed));
|
|
|
|
pp->tof = 1;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2001-06-12 16:38:20 +00:00
|
|
|
scnline(int key, char *p, int c)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
1998-09-11 18:49:33 +00:00
|
|
|
register int scnwidth;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
for (scnwidth = WIDTH; --scnwidth;) {
|
|
|
|
key <<= 1;
|
|
|
|
*p++ = key & 0200 ? c : BACKGND;
|
|
|
|
}
|
|
|
|
return (p);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TRC(q) (((q)-' ')&0177)
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
scan_out(struct printer *pp, int scfd, char *scsp, int dlm)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register char *strp;
|
1998-09-11 18:49:33 +00:00
|
|
|
register int nchrs, j;
|
1994-05-26 05:23:31 +00:00
|
|
|
char outbuf[LINELEN+1], *sp, c, cc;
|
|
|
|
int d, scnhgt;
|
|
|
|
|
|
|
|
for (scnhgt = 0; scnhgt++ < HEIGHT+DROP; ) {
|
|
|
|
strp = &outbuf[0];
|
|
|
|
sp = scsp;
|
|
|
|
for (nchrs = 0; ; ) {
|
|
|
|
d = dropit(c = TRC(cc = *sp++));
|
|
|
|
if ((!d && scnhgt > HEIGHT) || (scnhgt <= DROP && d))
|
|
|
|
for (j = WIDTH; --j;)
|
|
|
|
*strp++ = BACKGND;
|
|
|
|
else
|
1997-12-02 20:46:22 +00:00
|
|
|
strp = scnline(scnkey[(int)c][scnhgt-1-d], strp, cc);
|
|
|
|
if (*sp == dlm || *sp == '\0' ||
|
|
|
|
nchrs++ >= pp->page_width/(WIDTH+1)-1)
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
*strp++ = BACKGND;
|
|
|
|
*strp++ = BACKGND;
|
|
|
|
}
|
|
|
|
while (*--strp == BACKGND && strp >= outbuf)
|
|
|
|
;
|
|
|
|
strp++;
|
1995-05-30 03:57:47 +00:00
|
|
|
*strp++ = '\n';
|
1994-05-26 05:23:31 +00:00
|
|
|
(void) write(scfd, outbuf, strp-outbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
dropit(int c)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
switch(c) {
|
|
|
|
|
|
|
|
case TRC('_'):
|
|
|
|
case TRC(';'):
|
|
|
|
case TRC(','):
|
|
|
|
case TRC('g'):
|
|
|
|
case TRC('j'):
|
|
|
|
case TRC('p'):
|
|
|
|
case TRC('q'):
|
|
|
|
case TRC('y'):
|
|
|
|
return (DROP);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* sendmail ---
|
|
|
|
* tell people about job completion
|
|
|
|
*/
|
|
|
|
static void
|
2002-04-07 08:01:42 +00:00
|
|
|
sendmail(struct printer *pp, char *userid, int bombed)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
int p[2], s;
|
2001-06-12 16:38:20 +00:00
|
|
|
register const char *cp;
|
1994-05-26 05:23:31 +00:00
|
|
|
struct stat stb;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
pipe(p);
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((s = dofork(pp, DORETURN)) == 0) { /* child */
|
1994-05-26 05:23:31 +00:00
|
|
|
dup2(p[0], 0);
|
1995-04-27 04:56:10 +00:00
|
|
|
closelog();
|
1997-12-02 20:46:22 +00:00
|
|
|
closeallfds(3);
|
1997-07-23 20:53:41 +00:00
|
|
|
if ((cp = strrchr(_PATH_SENDMAIL, '/')) != NULL)
|
1994-05-26 05:23:31 +00:00
|
|
|
cp++;
|
1997-12-02 20:46:22 +00:00
|
|
|
else
|
1994-05-26 05:23:31 +00:00
|
|
|
cp = _PATH_SENDMAIL;
|
2001-07-09 09:24:06 +00:00
|
|
|
execl(_PATH_SENDMAIL, cp, "-t", (char *)0);
|
1997-12-02 20:46:22 +00:00
|
|
|
_exit(0);
|
1994-05-26 05:23:31 +00:00
|
|
|
} else if (s > 0) { /* parent */
|
|
|
|
dup2(p[1], 1);
|
2002-04-07 08:01:42 +00:00
|
|
|
printf("To: %s@%s\n", userid, origin_host);
|
1997-12-02 20:46:22 +00:00
|
|
|
printf("Subject: %s printer job \"%s\"\n", pp->printer,
|
1996-05-05 22:40:51 +00:00
|
|
|
*jobname ? jobname : "<unknown>");
|
2001-06-15 22:03:07 +00:00
|
|
|
printf("Reply-To: root@%s\n\n", local_host);
|
1994-05-26 05:23:31 +00:00
|
|
|
printf("Your printer job ");
|
|
|
|
if (*jobname)
|
|
|
|
printf("(%s) ", jobname);
|
2002-04-07 08:12:39 +00:00
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
switch (bombed) {
|
|
|
|
case OK:
|
1996-05-05 22:40:51 +00:00
|
|
|
cp = "OK";
|
2002-04-07 08:12:39 +00:00
|
|
|
printf("\ncompleted successfully\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case FATALERR:
|
1996-05-05 22:40:51 +00:00
|
|
|
cp = "FATALERR";
|
2002-04-07 08:12:39 +00:00
|
|
|
printf("\ncould not be printed\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
case NOACCT:
|
2002-04-07 08:12:39 +00:00
|
|
|
cp = "NOACCT";
|
2001-06-15 22:03:07 +00:00
|
|
|
printf("\ncould not be printed without an account on %s\n",
|
|
|
|
local_host);
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
case FILTERERR:
|
2002-04-07 08:12:39 +00:00
|
|
|
cp = "FILTERERR";
|
2000-11-13 05:44:50 +00:00
|
|
|
if (stat(tempstderr, &stb) < 0 || stb.st_size == 0
|
|
|
|
|| (fp = fopen(tempstderr, "r")) == NULL) {
|
1996-05-05 22:40:51 +00:00
|
|
|
printf("\nhad some errors and may not have printed\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
break;
|
|
|
|
}
|
1996-05-05 22:40:51 +00:00
|
|
|
printf("\nhad the following errors and may not have printed:\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
while ((i = getc(fp)) != EOF)
|
|
|
|
putchar(i);
|
|
|
|
(void) fclose(fp);
|
|
|
|
break;
|
|
|
|
case ACCESS:
|
1996-05-05 22:40:51 +00:00
|
|
|
cp = "ACCESS";
|
2002-04-07 08:12:39 +00:00
|
|
|
printf("\nwas not printed because it was not linked to the original file\n");
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
(void) close(1);
|
1997-12-02 20:46:22 +00:00
|
|
|
} else {
|
2002-04-07 08:01:42 +00:00
|
|
|
syslog(LOG_WARNING, "unable to send mail to %s: %m", userid);
|
1997-12-02 20:46:22 +00:00
|
|
|
return;
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
(void) close(p[0]);
|
|
|
|
(void) close(p[1]);
|
1996-05-05 22:40:51 +00:00
|
|
|
wait(NULL);
|
|
|
|
syslog(LOG_INFO, "mail sent to user %s about job %s on printer %s (%s)",
|
2002-04-07 08:01:42 +00:00
|
|
|
userid, *jobname ? jobname : "<unknown>", pp->printer, cp);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* dofork - fork with retries on failure
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
dofork(const struct printer *pp, int action)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2001-07-23 23:13:39 +00:00
|
|
|
int i, fail, forkpid;
|
2000-05-24 11:38:50 +00:00
|
|
|
struct passwd *pwd;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2001-07-23 23:13:39 +00:00
|
|
|
forkpid = -1;
|
|
|
|
if (daemon_uname == NULL) {
|
|
|
|
pwd = getpwuid(pp->daemon_user);
|
|
|
|
if (pwd == NULL) {
|
|
|
|
syslog(LOG_ERR, "%s: Can't lookup default daemon uid (%ld) in password file",
|
|
|
|
pp->printer, pp->daemon_user);
|
|
|
|
goto error_ret;
|
|
|
|
}
|
|
|
|
daemon_uname = strdup(pwd->pw_name);
|
|
|
|
daemon_defgid = pwd->pw_gid;
|
|
|
|
}
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
for (i = 0; i < 20; i++) {
|
2001-07-23 23:13:39 +00:00
|
|
|
forkpid = fork();
|
|
|
|
if (forkpid < 0) {
|
1994-05-26 05:23:31 +00:00
|
|
|
sleep((unsigned)(i*i));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Child should run as daemon instead of root
|
|
|
|
*/
|
2001-06-12 16:38:20 +00:00
|
|
|
if (forkpid == 0) {
|
2001-07-23 23:13:39 +00:00
|
|
|
errno = 0;
|
|
|
|
fail = initgroups(daemon_uname, daemon_defgid);
|
|
|
|
if (fail) {
|
|
|
|
syslog(LOG_ERR, "%s: initgroups(%s,%u): %m",
|
|
|
|
pp->printer, daemon_uname, daemon_defgid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fail = setgid(daemon_defgid);
|
|
|
|
if (fail) {
|
|
|
|
syslog(LOG_ERR, "%s: setgid(%u): %m",
|
|
|
|
pp->printer, daemon_defgid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fail = setuid(pp->daemon_user);
|
|
|
|
if (fail) {
|
|
|
|
syslog(LOG_ERR, "%s: setuid(%ld): %m",
|
|
|
|
pp->printer, pp->daemon_user);
|
2000-05-24 11:38:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-07-23 23:13:39 +00:00
|
|
|
return forkpid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An error occurred. If the error is in the child process, then
|
|
|
|
* this routine MUST always exit(). DORETURN only effects how
|
|
|
|
* errors should be handled in the parent process.
|
|
|
|
*/
|
|
|
|
error_ret:
|
|
|
|
if (forkpid == 0) {
|
|
|
|
syslog(LOG_ERR, "%s: dofork(): aborting child process...",
|
|
|
|
pp->printer);
|
|
|
|
exit(1);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
2001-07-23 23:13:39 +00:00
|
|
|
syslog(LOG_ERR, "%s: dofork(): failure in fork", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
|
2001-07-23 23:13:39 +00:00
|
|
|
sleep(1); /* throttle errors, as a safety measure */
|
1994-05-26 05:23:31 +00:00
|
|
|
switch (action) {
|
|
|
|
case DORETURN:
|
2001-07-23 23:13:39 +00:00
|
|
|
return -1;
|
1994-05-26 05:23:31 +00:00
|
|
|
default:
|
|
|
|
syslog(LOG_ERR, "bad action (%d) to dofork", action);
|
2001-07-23 23:13:39 +00:00
|
|
|
/* FALLTHROUGH */
|
1994-05-26 05:23:31 +00:00
|
|
|
case DOABORT:
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Kill child processes to abort current job.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
abortpr(int signo __unused)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
2000-11-13 05:44:50 +00:00
|
|
|
|
|
|
|
(void) unlink(tempstderr);
|
1994-05-26 05:23:31 +00:00
|
|
|
kill(0, SIGINT);
|
|
|
|
if (ofilter > 0)
|
|
|
|
kill(ofilter, SIGCONT);
|
|
|
|
while (wait(NULL) > 0)
|
|
|
|
;
|
1997-04-12 04:23:13 +00:00
|
|
|
if (ofilter > 0 && tfd != -1)
|
|
|
|
unlink(tfile);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
init(struct printer *pp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
sprintf(&width[2], "%ld", pp->page_width);
|
|
|
|
sprintf(&length[2], "%ld", pp->page_length);
|
|
|
|
sprintf(&pxwidth[2], "%ld", pp->page_pwidth);
|
|
|
|
sprintf(&pxlength[2], "%ld", pp->page_plength);
|
|
|
|
if ((s = checkremote(pp)) != 0) {
|
|
|
|
syslog(LOG_WARNING, "%s", s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-06-12 16:38:20 +00:00
|
|
|
startprinting(const char *printer)
|
1997-12-02 20:46:22 +00:00
|
|
|
{
|
|
|
|
struct printer myprinter, *pp = &myprinter;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
init_printer(pp);
|
|
|
|
status = getprintcap(printer, pp);
|
|
|
|
switch(status) {
|
|
|
|
case PCAPERR_OSERR:
|
|
|
|
syslog(LOG_ERR, "can't open printer description file: %m");
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
1997-12-02 20:46:22 +00:00
|
|
|
case PCAPERR_NOTFOUND:
|
1994-05-26 05:23:31 +00:00
|
|
|
syslog(LOG_ERR, "unknown printer: %s", printer);
|
|
|
|
exit(1);
|
1997-12-02 20:46:22 +00:00
|
|
|
case PCAPERR_TCLOOP:
|
|
|
|
fatal(pp, "potential reference loop detected in printcap file");
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printjob(pp);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire line printer or remote connection.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
openpr(const struct printer *pp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
1997-12-02 20:46:22 +00:00
|
|
|
int p[2];
|
1996-05-05 22:40:51 +00:00
|
|
|
char *cp;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->remote) {
|
|
|
|
openrem(pp);
|
2002-04-07 05:37:27 +00:00
|
|
|
/*
|
|
|
|
* Lpd does support the setting of 'of=' filters for
|
|
|
|
* jobs going to remote machines, but that does not
|
|
|
|
* have the same meaning as 'of=' does when handling
|
|
|
|
* local print queues. For remote machines, all 'of='
|
|
|
|
* filter processing is handled in sendfile(), and that
|
|
|
|
* does not use these global "output filter" variables.
|
|
|
|
*/
|
|
|
|
ofd = -1;
|
|
|
|
ofilter = 0;
|
|
|
|
return;
|
1997-12-02 20:46:22 +00:00
|
|
|
} else if (*pp->lp) {
|
|
|
|
if ((cp = strchr(pp->lp, '@')) != NULL)
|
|
|
|
opennet(pp);
|
1996-05-05 22:40:51 +00:00
|
|
|
else
|
1997-12-02 20:46:22 +00:00
|
|
|
opentty(pp);
|
1994-05-26 05:23:31 +00:00
|
|
|
} else {
|
|
|
|
syslog(LOG_ERR, "%s: no line printer device or host name",
|
1997-12-02 20:46:22 +00:00
|
|
|
pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-05-05 22:40:51 +00:00
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* Start up an output filter, if needed.
|
|
|
|
*/
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->filters[LPF_OUTPUT] && !pp->filters[LPF_INPUT] && !ofilter) {
|
1994-05-26 05:23:31 +00:00
|
|
|
pipe(p);
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->remote) {
|
|
|
|
strcpy(tfile, TFILENAME);
|
1997-04-12 04:23:13 +00:00
|
|
|
tfd = mkstemp(tfile);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((ofilter = dofork(pp, DOABORT)) == 0) { /* child */
|
1994-05-26 05:23:31 +00:00
|
|
|
dup2(p[0], 0); /* pipe is std in */
|
1997-04-12 04:23:13 +00:00
|
|
|
/* tfile/printer is stdout */
|
1997-12-02 20:46:22 +00:00
|
|
|
dup2(pp->remote ? tfd : pfd, 1);
|
1995-04-27 04:56:10 +00:00
|
|
|
closelog();
|
1997-12-02 20:46:22 +00:00
|
|
|
closeallfds(3);
|
|
|
|
if ((cp = strrchr(pp->filters[LPF_OUTPUT], '/')) == NULL)
|
|
|
|
cp = pp->filters[LPF_OUTPUT];
|
1994-05-26 05:23:31 +00:00
|
|
|
else
|
|
|
|
cp++;
|
2001-07-09 09:24:06 +00:00
|
|
|
execl(pp->filters[LPF_OUTPUT], cp, width, length,
|
|
|
|
(char *)0);
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer,
|
|
|
|
pp->filters[LPF_OUTPUT]);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
(void) close(p[0]); /* close input side */
|
|
|
|
ofd = p[1]; /* use pipe for output */
|
|
|
|
} else {
|
|
|
|
ofd = pfd;
|
|
|
|
ofilter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1996-05-05 22:40:51 +00:00
|
|
|
/*
|
|
|
|
* Printer connected directly to the network
|
|
|
|
* or to a terminal server on the net
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
opennet(const struct printer *pp)
|
1996-05-05 22:40:51 +00:00
|
|
|
{
|
|
|
|
register int i;
|
1997-12-02 20:46:22 +00:00
|
|
|
int resp;
|
|
|
|
u_long port;
|
|
|
|
char *ep;
|
1997-10-14 16:01:02 +00:00
|
|
|
void (*savealrm)(int);
|
1996-05-05 22:40:51 +00:00
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
port = strtoul(pp->lp, &ep, 0);
|
1998-08-21 18:08:46 +00:00
|
|
|
if (*ep != '@' || port > 65535) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: bad port number: %s", pp->printer,
|
|
|
|
pp->lp);
|
1996-05-05 22:40:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
ep++;
|
1996-05-05 22:40:51 +00:00
|
|
|
|
|
|
|
for (i = 1; ; i = i < 256 ? i << 1 : i) {
|
|
|
|
resp = -1;
|
1997-10-14 16:01:02 +00:00
|
|
|
savealrm = signal(SIGALRM, alarmhandler);
|
1997-12-02 20:46:22 +00:00
|
|
|
alarm(pp->conn_timeout);
|
|
|
|
pfd = getport(pp, ep, port);
|
1997-11-07 13:18:50 +00:00
|
|
|
alarm(0);
|
1997-10-14 16:01:02 +00:00
|
|
|
(void)signal(SIGALRM, savealrm);
|
1996-05-05 22:40:51 +00:00
|
|
|
if (pfd < 0 && errno == ECONNREFUSED)
|
|
|
|
resp = 1;
|
|
|
|
else if (pfd >= 0) {
|
|
|
|
/*
|
|
|
|
* need to delay a bit for rs232 lines
|
|
|
|
* to stabilize in case printer is
|
|
|
|
* connected via a terminal server
|
|
|
|
*/
|
|
|
|
delay(500);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == 1) {
|
1997-12-02 20:46:22 +00:00
|
|
|
if (resp < 0)
|
|
|
|
pstatus(pp, "waiting for %s to come up",
|
|
|
|
pp->lp);
|
|
|
|
else
|
|
|
|
pstatus(pp,
|
|
|
|
"waiting for access to printer on %s",
|
|
|
|
pp->lp);
|
1996-05-05 22:40:51 +00:00
|
|
|
}
|
|
|
|
sleep(i);
|
|
|
|
}
|
2001-07-15 00:09:46 +00:00
|
|
|
pstatus(pp, "sending to %s port %lu", ep, port);
|
1996-05-05 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Printer is connected to an RS232 port on this host
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
opentty(const struct printer *pp)
|
1996-05-05 22:40:51 +00:00
|
|
|
{
|
|
|
|
register int i;
|
|
|
|
|
|
|
|
for (i = 1; ; i = i < 32 ? i << 1 : i) {
|
1997-12-02 20:46:22 +00:00
|
|
|
pfd = open(pp->lp, pp->rw ? O_RDWR : O_WRONLY);
|
1996-05-05 22:40:51 +00:00
|
|
|
if (pfd >= 0) {
|
|
|
|
delay(500);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (errno == ENOENT) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: %m", pp->lp);
|
1996-05-05 22:40:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (i == 1)
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp,
|
|
|
|
"waiting for %s to become ready (offline?)",
|
|
|
|
pp->printer);
|
1996-05-05 22:40:51 +00:00
|
|
|
sleep(i);
|
|
|
|
}
|
|
|
|
if (isatty(pfd))
|
1997-12-02 20:46:22 +00:00
|
|
|
setty(pp);
|
|
|
|
pstatus(pp, "%s is ready and printing", pp->printer);
|
1996-05-05 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Printer is on a remote host
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
openrem(const struct printer *pp)
|
1996-05-05 22:40:51 +00:00
|
|
|
{
|
1997-12-02 20:46:22 +00:00
|
|
|
register int i;
|
1997-07-29 04:17:19 +00:00
|
|
|
int resp;
|
1997-10-14 16:01:02 +00:00
|
|
|
void (*savealrm)(int);
|
1996-05-05 22:40:51 +00:00
|
|
|
|
|
|
|
for (i = 1; ; i = i < 256 ? i << 1 : i) {
|
|
|
|
resp = -1;
|
1997-10-14 16:01:02 +00:00
|
|
|
savealrm = signal(SIGALRM, alarmhandler);
|
1997-12-02 20:46:22 +00:00
|
|
|
alarm(pp->conn_timeout);
|
|
|
|
pfd = getport(pp, pp->remote_host, 0);
|
1997-11-07 13:18:50 +00:00
|
|
|
alarm(0);
|
1997-10-14 16:01:02 +00:00
|
|
|
(void)signal(SIGALRM, savealrm);
|
1996-05-05 22:40:51 +00:00
|
|
|
if (pfd >= 0) {
|
1997-12-02 20:46:22 +00:00
|
|
|
if ((writel(pfd, "\2", pp->remote_queue, "\n",
|
|
|
|
(char *)0)
|
|
|
|
== 2 + strlen(pp->remote_queue))
|
|
|
|
&& (resp = response(pp)) == 0)
|
1996-05-05 22:40:51 +00:00
|
|
|
break;
|
|
|
|
(void) close(pfd);
|
|
|
|
}
|
|
|
|
if (i == 1) {
|
|
|
|
if (resp < 0)
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp, "waiting for %s to come up",
|
|
|
|
pp->remote_host);
|
1996-05-05 22:40:51 +00:00
|
|
|
else {
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp,
|
|
|
|
"waiting for queue to be enabled on %s",
|
|
|
|
pp->remote_host);
|
1996-05-05 22:40:51 +00:00
|
|
|
i = 256;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sleep(i);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(pp, "sending to %s", pp->remote_host);
|
1996-05-05 22:40:51 +00:00
|
|
|
}
|
|
|
|
|
1994-05-26 05:23:31 +00:00
|
|
|
/*
|
|
|
|
* setup tty lines.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
setty(const struct printer *pp)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
1996-04-03 22:49:15 +00:00
|
|
|
struct termios ttybuf;
|
1994-05-26 05:23:31 +00:00
|
|
|
|
|
|
|
if (ioctl(pfd, TIOCEXCL, (char *)0) < 0) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: ioctl(TIOCEXCL): %m", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1996-04-03 22:49:15 +00:00
|
|
|
if (tcgetattr(pfd, &ttybuf) < 0) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: tcgetattr: %m", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->baud_rate > 0)
|
|
|
|
cfsetspeed(&ttybuf, pp->baud_rate);
|
|
|
|
if (pp->mode_set) {
|
|
|
|
char *s = strdup(pp->mode_set), *tmp;
|
1996-04-03 22:49:15 +00:00
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
while ((tmp = strsep(&s, ",")) != NULL) {
|
1998-09-11 18:49:33 +00:00
|
|
|
(void) msearch(tmp, &ttybuf);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pp->mode_set != 0 || pp->baud_rate > 0) {
|
1996-04-03 22:49:15 +00:00
|
|
|
if (tcsetattr(pfd, TCSAFLUSH, &ttybuf) == -1) {
|
1997-12-02 20:46:22 +00:00
|
|
|
syslog(LOG_ERR, "%s: tcsetattr: %m", pp->printer);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
1996-05-05 22:40:51 +00:00
|
|
|
static void
|
1997-12-02 20:46:22 +00:00
|
|
|
pstatus(const struct printer *pp, const char *msg, ...)
|
1994-05-26 05:23:31 +00:00
|
|
|
{
|
1997-12-02 20:46:22 +00:00
|
|
|
int fd;
|
|
|
|
char *buf;
|
1994-05-26 05:23:31 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, msg);
|
|
|
|
|
|
|
|
umask(0);
|
1997-12-02 20:46:22 +00:00
|
|
|
fd = open(pp->status_file, O_WRONLY|O_CREAT|O_EXLOCK, STAT_FILE_MODE);
|
|
|
|
if (fd < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %s: %m", pp->printer, pp->status_file);
|
1994-05-26 05:23:31 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ftruncate(fd, 0);
|
1997-12-02 20:46:22 +00:00
|
|
|
vasprintf(&buf, msg, ap);
|
1994-05-26 05:23:31 +00:00
|
|
|
va_end(ap);
|
1997-12-02 20:46:22 +00:00
|
|
|
writel(fd, buf, "\n", (char *)0);
|
|
|
|
close(fd);
|
|
|
|
free(buf);
|
1994-05-26 05:23:31 +00:00
|
|
|
}
|
1997-10-14 16:01:02 +00:00
|
|
|
|
|
|
|
void
|
2001-06-12 16:38:20 +00:00
|
|
|
alarmhandler(int signo __unused)
|
1997-10-14 16:01:02 +00:00
|
|
|
{
|
2001-06-12 16:38:20 +00:00
|
|
|
/* the signal is ignored */
|
|
|
|
/* (the '__unused' is just to avoid a compile-time warning) */
|
1997-10-14 16:01:02 +00:00
|
|
|
}
|