2002-10-26 05:47:52 +04:00
|
|
|
/* $NetBSD: recvjob.c,v 1.17 2002/10/26 01:47:52 thorpej Exp $ */
|
1997-07-17 09:44:32 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-18 05:25:37 +04:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-07-17 09:44:32 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-07-17 09:44:32 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1994-05-18 05:25:37 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-17 09:44:32 +04:00
|
|
|
#if 0
|
1997-10-05 15:52:17 +04:00
|
|
|
static char sccsid[] = "@(#)recvjob.c 8.2 (Berkeley) 4/27/95";
|
1997-07-17 09:44:32 +04:00
|
|
|
#else
|
2002-10-26 05:47:52 +04:00
|
|
|
__RCSID("$NetBSD: recvjob.c,v 1.17 2002/10/26 01:47:52 thorpej Exp $");
|
1997-07-17 09:44:32 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receive printer jobs from the network, queue them and
|
|
|
|
* start the printer daemon.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "lp.h"
|
1994-05-18 05:25:37 +04:00
|
|
|
#include "lp.local.h"
|
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
|
|
|
|
2001-10-09 06:15:37 +04:00
|
|
|
#define ack() (void)write(STDOUT_FILENO, sp, 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
static char dfname[NAME_MAX]; /* data files */
|
1994-05-18 05:25:37 +04:00
|
|
|
static int minfree; /* keep at least minfree blocks available */
|
|
|
|
static char *sp = "";
|
1997-10-05 19:11:58 +04:00
|
|
|
static char tfname[NAME_MAX]; /* tmp copy of cf before linking */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2002-07-14 19:27:58 +04:00
|
|
|
static int chksize(int);
|
|
|
|
static void frecverr(const char *, ...)
|
2000-10-12 00:23:46 +04:00
|
|
|
__attribute__((__format__(__printf__, 1, 2)));
|
2002-07-14 19:27:58 +04:00
|
|
|
static int noresponse(void);
|
|
|
|
static void rcleanup(int);
|
|
|
|
static int read_number(char *);
|
|
|
|
static int readfile(char *, int);
|
|
|
|
static int readjob(void);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
|
|
|
|
void
|
2001-10-09 06:15:37 +04:00
|
|
|
recvjob(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat stb;
|
2002-10-26 05:47:52 +04:00
|
|
|
int status, fd;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform lookup for printer name or abbreviation
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
if ((status = cgetent(&bp, printcapdb, printer)) == -2)
|
1993-03-21 12:45:37 +03:00
|
|
|
frecverr("cannot open printer description file");
|
1994-05-18 05:25:37 +04:00
|
|
|
else if (status == -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
frecverr("unknown printer %s", printer);
|
1994-05-18 05:25:37 +04:00
|
|
|
else if (status == -3)
|
|
|
|
fatal("potential reference loop detected in printcap file");
|
|
|
|
|
|
|
|
if (cgetstr(bp, "lf", &LF) == -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
LF = _PATH_CONSOLE;
|
1994-05-18 05:25:37 +04:00
|
|
|
if (cgetstr(bp, "sd", &SD) == -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
SD = _PATH_DEFSPOOL;
|
1994-05-18 05:25:37 +04:00
|
|
|
if (cgetstr(bp, "lo", &LO) == -1)
|
1993-03-21 12:45:37 +03:00
|
|
|
LO = DEFLOCK;
|
|
|
|
|
2002-10-26 05:47:52 +04:00
|
|
|
/* Set up the log file. */
|
|
|
|
if ((fd = open(LF, O_WRONLY|O_APPEND, 0664)) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
syslog(LOG_ERR, "%s: %m", LF);
|
2002-10-26 05:47:52 +04:00
|
|
|
fd = open(_PATH_DEVNULL, O_WRONLY);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-10-26 05:47:52 +04:00
|
|
|
if (fd > 0) {
|
|
|
|
(void) dup2(fd, STDERR_FILENO);
|
|
|
|
(void) close(fd);
|
|
|
|
} else
|
|
|
|
(void) close(STDERR_FILENO);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (chdir(SD) < 0)
|
|
|
|
frecverr("%s: %s: %m", printer, SD);
|
|
|
|
if (stat(LO, &stb) == 0) {
|
|
|
|
if (stb.st_mode & 010) {
|
|
|
|
/* queue is disabled */
|
|
|
|
putchar('\1'); /* return error code */
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else if (stat(SD, &stb) < 0)
|
|
|
|
frecverr("%s: %s: %m", printer, SD);
|
|
|
|
minfree = 2 * read_number("minfree"); /* scale KB to 512 blocks */
|
|
|
|
signal(SIGTERM, rcleanup);
|
|
|
|
signal(SIGPIPE, rcleanup);
|
|
|
|
|
|
|
|
if (readjob())
|
|
|
|
printjob();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read printer jobs sent by lpd and copy them to the spooling directory.
|
|
|
|
* Return the number of jobs successfully transfered.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2001-10-09 06:15:37 +04:00
|
|
|
readjob(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
int size, nfiles;
|
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
ack();
|
|
|
|
nfiles = 0;
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* Read a command to tell us what to do
|
|
|
|
*/
|
|
|
|
cp = line;
|
|
|
|
do {
|
2001-10-09 06:15:37 +04:00
|
|
|
if ((size = read(STDOUT_FILENO, cp, 1)) != 1) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (size < 0)
|
1997-10-05 19:11:58 +04:00
|
|
|
frecverr("%s: Lost connection",
|
|
|
|
printer);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(nfiles);
|
|
|
|
}
|
1997-10-05 19:11:58 +04:00
|
|
|
} while (*cp++ != '\n' && (cp - line + 1) < sizeof(line));
|
|
|
|
if (cp - line + 1 >= sizeof(line))
|
|
|
|
frecverr("readjob overflow");
|
1993-03-21 12:45:37 +03:00
|
|
|
*--cp = '\0';
|
|
|
|
cp = line;
|
|
|
|
switch (*cp++) {
|
|
|
|
case '\1': /* cleanup because data sent was bad */
|
1994-05-18 05:25:37 +04:00
|
|
|
rcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\2': /* read cf file */
|
|
|
|
size = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
size = size * 10 + (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ')
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* host name has been authenticated, we use our
|
|
|
|
* view of the host name since we may be passed
|
|
|
|
* something different than what gethostbyaddr()
|
|
|
|
* returns
|
|
|
|
*/
|
2001-12-05 01:52:44 +03:00
|
|
|
(void)strlcpy(cp + 6, from,
|
|
|
|
sizeof(line) + line - cp - 6);
|
|
|
|
if (strchr(cp, '/'))
|
|
|
|
frecverr("readjob: %s: illegal path name", cp);
|
|
|
|
(void)strlcpy(tfname, cp, sizeof(tfname));
|
1993-03-21 12:45:37 +03:00
|
|
|
tfname[0] = 't';
|
|
|
|
if (!chksize(size)) {
|
2001-10-09 06:15:37 +04:00
|
|
|
(void)write(STDOUT_FILENO, "\2", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!readfile(tfname, size)) {
|
1994-05-18 05:25:37 +04:00
|
|
|
rcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (link(tfname, cp) < 0)
|
|
|
|
frecverr("%s: %m", tfname);
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)unlink(tfname);
|
1993-03-21 12:45:37 +03:00
|
|
|
tfname[0] = '\0';
|
|
|
|
nfiles++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\3': /* read df file */
|
|
|
|
size = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '9')
|
|
|
|
size = size * 10 + (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ')
|
|
|
|
break;
|
|
|
|
if (!chksize(size)) {
|
2001-10-09 06:15:37 +04:00
|
|
|
(void)write(STDOUT_FILENO, "\2", 1);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
2001-12-05 01:52:44 +03:00
|
|
|
if (strchr(cp, '/'))
|
|
|
|
frecverr("readjob: %s: illegal path name", cp);
|
|
|
|
(void)strlcpy(dfname, cp, sizeof(dfname));
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)readfile(dfname, size);
|
1993-03-21 12:45:37 +03:00
|
|
|
continue;
|
|
|
|
}
|
1994-05-18 05:25:37 +04:00
|
|
|
frecverr("protocol screwup: %s", line);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read files send by lpd and copy them to the spooling directory.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2001-10-09 06:15:37 +04:00
|
|
|
readfile(char *file, int size)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char buf[BUFSIZ];
|
1997-10-05 19:11:58 +04:00
|
|
|
int i, j, amt;
|
1993-03-21 12:45:37 +03:00
|
|
|
int fd, err;
|
|
|
|
|
|
|
|
fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
|
|
|
|
if (fd < 0)
|
|
|
|
frecverr("readfile: %s: illegal path name: %m", file);
|
|
|
|
ack();
|
|
|
|
err = 0;
|
|
|
|
for (i = 0; i < size; i += BUFSIZ) {
|
|
|
|
amt = BUFSIZ;
|
|
|
|
cp = buf;
|
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
do {
|
2001-10-09 06:15:37 +04:00
|
|
|
j = read(STDOUT_FILENO, cp, amt);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (j <= 0)
|
|
|
|
frecverr("Lost connection");
|
|
|
|
amt -= j;
|
|
|
|
cp += j;
|
|
|
|
} while (amt > 0);
|
|
|
|
amt = BUFSIZ;
|
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
if (write(fd, buf, amt) != amt) {
|
|
|
|
err++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(fd);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (err)
|
|
|
|
frecverr("%s: write error", file);
|
|
|
|
if (noresponse()) { /* file sent had bad data in it */
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)unlink(file);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
ack();
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2001-10-09 06:15:37 +04:00
|
|
|
noresponse(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char resp;
|
|
|
|
|
2001-10-09 06:15:37 +04:00
|
|
|
if (read(STDOUT_FILENO, &resp, 1) != 1)
|
1993-03-21 12:45:37 +03:00
|
|
|
frecverr("Lost connection");
|
|
|
|
if (resp == '\0')
|
|
|
|
return(0);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if there is enough space on the disk for size bytes.
|
|
|
|
* 1 == OK, 0 == Not OK.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2001-10-09 06:15:37 +04:00
|
|
|
chksize(int size)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int spacefree;
|
|
|
|
struct statfs sfb;
|
|
|
|
|
|
|
|
if (statfs(".", &sfb) < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
|
|
|
|
return (1);
|
|
|
|
}
|
1994-04-21 22:53:47 +04:00
|
|
|
spacefree = sfb.f_bavail * (sfb.f_bsize / 512);
|
1993-03-21 12:45:37 +03:00
|
|
|
size = (size + 511) / 512;
|
|
|
|
if (minfree + size > spacefree)
|
|
|
|
return(0);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static int
|
2002-07-09 05:12:35 +04:00
|
|
|
read_number(char *fn)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char lin[80];
|
1997-10-05 19:11:58 +04:00
|
|
|
FILE *fp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if ((fp = fopen(fn, "r")) == NULL)
|
|
|
|
return (0);
|
|
|
|
if (fgets(lin, 80, fp) == NULL) {
|
|
|
|
fclose(fp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
return (atoi(lin));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove all the files associated with the current job being transfered.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
2001-10-09 06:15:37 +04:00
|
|
|
rcleanup(int signo)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (tfname[0])
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)unlink(tfname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (dfname[0])
|
|
|
|
do {
|
1997-10-05 19:21:46 +04:00
|
|
|
do {
|
|
|
|
if (strchr(dfname, '/') == 0)
|
|
|
|
(void)unlink(dfname);
|
|
|
|
} while (dfname[2]-- != 'A');
|
1993-03-21 12:45:37 +03:00
|
|
|
dfname[2] = 'z';
|
|
|
|
} while (dfname[0]-- != 'd');
|
|
|
|
dfname[0] = '\0';
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
static void
|
|
|
|
frecverr(const char *msg, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-12-11 19:07:22 +03:00
|
|
|
extern char fromb[];
|
1994-05-18 05:25:37 +04:00
|
|
|
va_list ap;
|
2002-07-14 19:27:58 +04:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
va_start(ap, msg);
|
|
|
|
rcleanup(0);
|
|
|
|
syslog(LOG_ERR, "%s", fromb);
|
|
|
|
vsyslog(LOG_ERR, msg, ap);
|
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
putchar('\1'); /* return error code */
|
|
|
|
exit(1);
|
|
|
|
}
|