1998-07-18 09:04:35 +04:00
|
|
|
/* $NetBSD: lpd.c,v 1.17 1998/07/18 05:04:40 lukem Exp $ */
|
1996-04-24 18:54:06 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-05-18 05:25:37 +04:00
|
|
|
* Copyright (c) 1983, 1993, 1994
|
|
|
|
* 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:49:13 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-07-17 09:49:13 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1983, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1997-07-10 10:26:44 +04:00
|
|
|
#if 0
|
1997-10-05 15:52:17 +04:00
|
|
|
static char sccsid[] = "@(#)lpd.c 8.7 (Berkeley) 5/10/95";
|
1997-07-10 10:26:44 +04:00
|
|
|
#else
|
1998-07-18 09:04:35 +04:00
|
|
|
__RCSID("$NetBSD: lpd.c,v 1.17 1998/07/18 05:04:40 lukem Exp $");
|
1997-07-10 10:26:44 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* lpd -- line printer daemon.
|
|
|
|
*
|
|
|
|
* Listen for a connection and perform the requested operation.
|
|
|
|
* Operations are:
|
|
|
|
* \1printer\n
|
|
|
|
* check the queue for jobs and print any found.
|
|
|
|
* \2printer\n
|
|
|
|
* receive a job from another machine and queue it.
|
|
|
|
* \3printer [users ...] [jobs ...]\n
|
|
|
|
* return the current state of the queue (short form).
|
|
|
|
* \4printer [users ...] [jobs ...]\n
|
|
|
|
* return the current state of the queue (long form).
|
|
|
|
* \5printer person [users ...] [jobs ...]\n
|
|
|
|
* remove jobs from the queue.
|
|
|
|
*
|
|
|
|
* Strategy to maintain protected spooling area:
|
|
|
|
* 1. Spooling area is writable only by daemon and spooling group
|
|
|
|
* 2. lpr runs setuid root and setgrp spooling group; it uses
|
|
|
|
* root to access any file it wants (verifying things before
|
|
|
|
* with an access call) and group id to know how it should
|
|
|
|
* set up ownership of files in the spooling area.
|
|
|
|
* 3. Files in spooling area are owned by root, group spooling
|
|
|
|
* group, with mode 660.
|
|
|
|
* 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
|
|
|
|
* access files and printer. Users can't get to anything
|
|
|
|
* w/o help of lpq and lprm programs.
|
|
|
|
*/
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
1997-10-05 15:52:17 +04:00
|
|
|
#include <sys/file.h>
|
1994-05-18 05:25:37 +04:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
1997-07-17 09:49:13 +04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "lp.h"
|
1994-05-18 05:25:37 +04:00
|
|
|
#include "lp.local.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "pathnames.h"
|
1994-05-18 05:25:37 +04:00
|
|
|
#include "extern.h"
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
int lflag; /* log requests flag */
|
1996-09-21 19:57:21 +04:00
|
|
|
int sflag; /* secure (no inet) flag */
|
1993-03-21 12:45:37 +03:00
|
|
|
int from_remote; /* from remote socket */
|
|
|
|
|
1997-07-17 09:49:13 +04:00
|
|
|
int main __P((int, char **));
|
1994-05-18 05:25:37 +04:00
|
|
|
static void reapchild __P((int));
|
|
|
|
static void mcleanup __P((int));
|
|
|
|
static void doit __P((void));
|
|
|
|
static void startup __P((void));
|
|
|
|
static void chkhost __P((struct sockaddr_in *));
|
1997-10-05 15:52:17 +04:00
|
|
|
static int ckqueue __P((char *));
|
1997-10-05 19:11:58 +04:00
|
|
|
static void usage __P((void));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-10-03 18:02:17 +03:00
|
|
|
uid_t uid, euid;
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
{
|
1994-05-18 05:25:37 +04:00
|
|
|
int f, funix, finet, options, fromlen;
|
|
|
|
fd_set defreadfds;
|
|
|
|
struct sockaddr_un un, fromunix;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct sockaddr_in sin, frominet;
|
1997-10-05 19:11:58 +04:00
|
|
|
int omask, lfd, errs, i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-10-03 18:02:17 +03:00
|
|
|
euid = geteuid(); /* these shouldn't be different */
|
|
|
|
uid = getuid();
|
1994-05-18 05:25:37 +04:00
|
|
|
options = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
gethostname(host, sizeof(host));
|
1998-07-06 10:56:06 +04:00
|
|
|
host[sizeof(host) - 1] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
name = argv[0];
|
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
errs = 0;
|
1997-10-09 11:58:39 +04:00
|
|
|
while ((i = getopt(argc, argv, "dls")) != -1)
|
1997-10-05 19:11:58 +04:00
|
|
|
switch (i) {
|
|
|
|
case 'd':
|
|
|
|
options |= SO_DEBUG;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag++;
|
|
|
|
break;
|
1997-10-09 11:58:39 +04:00
|
|
|
case 's':
|
|
|
|
sflag++;
|
|
|
|
break;
|
1997-10-05 19:11:58 +04:00
|
|
|
default:
|
|
|
|
errs++;
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (errs || argc != 0)
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
#ifndef DEBUG
|
|
|
|
/*
|
|
|
|
* Set up standard environment by detaching from the parent.
|
|
|
|
*/
|
|
|
|
daemon(0, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
openlog("lpd", LOG_PID, LOG_LPR);
|
1994-05-18 05:25:37 +04:00
|
|
|
syslog(LOG_INFO, "restarted");
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)umask(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT, 0644);
|
|
|
|
if (lfd < 0) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (flock(lfd, LOCK_EX|LOCK_NB) < 0) {
|
|
|
|
if (errno == EWOULDBLOCK) /* active deamon present */
|
|
|
|
exit(0);
|
|
|
|
syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
ftruncate(lfd, 0);
|
|
|
|
/*
|
|
|
|
* write process id for others to know
|
|
|
|
*/
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)snprintf(line, sizeof(line), "%u\n", getpid());
|
1993-03-21 12:45:37 +03:00
|
|
|
f = strlen(line);
|
|
|
|
if (write(lfd, line, f) != f) {
|
|
|
|
syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
signal(SIGCHLD, reapchild);
|
|
|
|
/*
|
|
|
|
* Restart all the printers.
|
|
|
|
*/
|
|
|
|
startup();
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)unlink(_PATH_SOCKETNAME);
|
1998-07-18 09:04:35 +04:00
|
|
|
funix = socket(AF_LOCAL, SOCK_STREAM, 0);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (funix < 0) {
|
|
|
|
syslog(LOG_ERR, "socket: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#define mask(s) (1 << ((s) - 1))
|
|
|
|
omask = sigblock(mask(SIGHUP)|mask(SIGINT)|mask(SIGQUIT)|mask(SIGTERM));
|
|
|
|
signal(SIGHUP, mcleanup);
|
|
|
|
signal(SIGINT, mcleanup);
|
|
|
|
signal(SIGQUIT, mcleanup);
|
|
|
|
signal(SIGTERM, mcleanup);
|
1994-05-18 05:25:37 +04:00
|
|
|
memset(&un, 0, sizeof(un));
|
1998-07-18 09:04:35 +04:00
|
|
|
un.sun_family = AF_LOCAL;
|
1996-12-09 12:57:40 +03:00
|
|
|
strncpy(un.sun_path, _PATH_SOCKETNAME, sizeof(un.sun_path) - 1);
|
1994-05-18 05:25:37 +04:00
|
|
|
#ifndef SUN_LEN
|
|
|
|
#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
|
|
|
|
#endif
|
|
|
|
if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
syslog(LOG_ERR, "ubind: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
sigsetmask(omask);
|
1994-05-18 05:25:37 +04:00
|
|
|
FD_ZERO(&defreadfds);
|
|
|
|
FD_SET(funix, &defreadfds);
|
1993-03-21 12:45:37 +03:00
|
|
|
listen(funix, 5);
|
1996-09-21 19:57:21 +04:00
|
|
|
if (!sflag)
|
|
|
|
finet = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
else
|
|
|
|
finet = -1; /* pretend we couldn't open TCP socket. */
|
1993-03-21 12:45:37 +03:00
|
|
|
if (finet >= 0) {
|
|
|
|
struct servent *sp;
|
|
|
|
|
|
|
|
if (options & SO_DEBUG)
|
|
|
|
if (setsockopt(finet, SOL_SOCKET, SO_DEBUG, 0, 0) < 0) {
|
|
|
|
syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
|
1994-05-18 05:25:37 +04:00
|
|
|
mcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
sp = getservbyname("printer", "tcp");
|
|
|
|
if (sp == NULL) {
|
|
|
|
syslog(LOG_ERR, "printer/tcp: unknown service");
|
1994-05-18 05:25:37 +04:00
|
|
|
mcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-05-18 05:25:37 +04:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
1993-03-21 12:45:37 +03:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = sp->s_port;
|
|
|
|
if (bind(finet, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
|
|
|
|
syslog(LOG_ERR, "bind: %m");
|
1994-05-18 05:25:37 +04:00
|
|
|
mcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-05-18 05:25:37 +04:00
|
|
|
FD_SET(finet, &defreadfds);
|
1993-03-21 12:45:37 +03:00
|
|
|
listen(finet, 5);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Main loop: accept, do a request, continue.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
memset(&frominet, 0, sizeof(frominet));
|
|
|
|
memset(&fromunix, 0, sizeof(fromunix));
|
1993-03-21 12:45:37 +03:00
|
|
|
for (;;) {
|
1994-05-18 05:25:37 +04:00
|
|
|
int domain, nfds, s;
|
|
|
|
fd_set readfds;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
FD_COPY(&defreadfds, &readfds);
|
1993-03-21 12:45:37 +03:00
|
|
|
nfds = select(20, &readfds, 0, 0, 0);
|
|
|
|
if (nfds <= 0) {
|
|
|
|
if (nfds < 0 && errno != EINTR)
|
|
|
|
syslog(LOG_WARNING, "select: %m");
|
|
|
|
continue;
|
|
|
|
}
|
1994-05-18 05:25:37 +04:00
|
|
|
if (FD_ISSET(funix, &readfds)) {
|
1998-07-18 09:04:35 +04:00
|
|
|
domain = AF_LOCAL, fromlen = sizeof(fromunix);
|
1993-03-21 12:45:37 +03:00
|
|
|
s = accept(funix,
|
|
|
|
(struct sockaddr *)&fromunix, &fromlen);
|
1994-05-18 05:25:37 +04:00
|
|
|
} else /* if (FD_ISSET(finet, &readfds)) */ {
|
1993-03-21 12:45:37 +03:00
|
|
|
domain = AF_INET, fromlen = sizeof(frominet);
|
|
|
|
s = accept(finet,
|
|
|
|
(struct sockaddr *)&frominet, &fromlen);
|
|
|
|
}
|
|
|
|
if (s < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
syslog(LOG_WARNING, "accept: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fork() == 0) {
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(funix);
|
1996-09-21 19:57:21 +04:00
|
|
|
if (!sflag)
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(finet);
|
1993-03-21 12:45:37 +03:00
|
|
|
dup2(s, 1);
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(s);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (domain == AF_INET) {
|
|
|
|
from_remote = 1;
|
|
|
|
chkhost(&frominet);
|
|
|
|
} else
|
|
|
|
from_remote = 0;
|
|
|
|
doit();
|
|
|
|
exit(0);
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)close(s);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
|
|
|
reapchild(signo)
|
|
|
|
int signo;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
union wait status;
|
|
|
|
|
|
|
|
while (wait3((int *)&status, WNOHANG, 0) > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
|
|
|
mcleanup(signo)
|
|
|
|
int signo;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
if (lflag)
|
|
|
|
syslog(LOG_INFO, "exiting");
|
|
|
|
unlink(_PATH_SOCKETNAME);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Stuff for handling job specifications
|
|
|
|
*/
|
|
|
|
char *user[MAXUSERS]; /* users to process */
|
|
|
|
int users; /* # of users in user array */
|
|
|
|
int requ[MAXREQUESTS]; /* job number of spool entries */
|
|
|
|
int requests; /* # of spool requests */
|
|
|
|
char *person; /* name of person doing lprm */
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
char fromb[MAXHOSTNAMELEN]; /* buffer for client's machine name */
|
|
|
|
char cbuf[BUFSIZ]; /* command line buffer */
|
1993-03-21 12:45:37 +03:00
|
|
|
char *cmdnames[] = {
|
|
|
|
"null",
|
|
|
|
"printjob",
|
|
|
|
"recvjob",
|
|
|
|
"displayq short",
|
|
|
|
"displayq long",
|
|
|
|
"rmjob"
|
|
|
|
};
|
|
|
|
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
1993-03-21 12:45:37 +03:00
|
|
|
doit()
|
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp;
|
|
|
|
int n;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cp = cbuf;
|
|
|
|
do {
|
|
|
|
if (cp >= &cbuf[sizeof(cbuf) - 1])
|
|
|
|
fatal("Command line too long");
|
|
|
|
if ((n = read(1, cp, 1)) != 1) {
|
|
|
|
if (n < 0)
|
|
|
|
fatal("Lost connection");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (*cp++ != '\n');
|
|
|
|
*--cp = '\0';
|
|
|
|
cp = cbuf;
|
|
|
|
if (lflag) {
|
|
|
|
if (*cp >= '\1' && *cp <= '\5')
|
|
|
|
syslog(LOG_INFO, "%s requests %s %s",
|
1997-07-10 10:26:44 +04:00
|
|
|
from, cmdnames[(int)*cp], cp+1);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "bad request (%d) from %s",
|
|
|
|
*cp, from);
|
|
|
|
}
|
|
|
|
switch (*cp++) {
|
|
|
|
case '\1': /* check the queue and print any jobs there */
|
|
|
|
printer = cp;
|
|
|
|
printjob();
|
|
|
|
break;
|
|
|
|
case '\2': /* receive files to be queued */
|
|
|
|
if (!from_remote) {
|
|
|
|
syslog(LOG_INFO, "illegal request (%d)", *cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printer = cp;
|
|
|
|
recvjob();
|
|
|
|
break;
|
|
|
|
case '\3': /* display the queue (short form) */
|
|
|
|
case '\4': /* display the queue (long form) */
|
|
|
|
printer = cp;
|
|
|
|
while (*cp) {
|
|
|
|
if (*cp != ' ') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
while (isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == '\0')
|
|
|
|
break;
|
|
|
|
if (isdigit(*cp)) {
|
|
|
|
if (requests >= MAXREQUESTS)
|
|
|
|
fatal("Too many requests");
|
|
|
|
requ[requests++] = atoi(cp);
|
|
|
|
} else {
|
|
|
|
if (users >= MAXUSERS)
|
|
|
|
fatal("Too many users");
|
|
|
|
user[users++] = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
displayq(cbuf[0] - '\3');
|
|
|
|
exit(0);
|
|
|
|
case '\5': /* remove a job from the queue */
|
|
|
|
if (!from_remote) {
|
|
|
|
syslog(LOG_INFO, "illegal request (%d)", *cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printer = cp;
|
|
|
|
while (*cp && *cp != ' ')
|
|
|
|
cp++;
|
|
|
|
if (!*cp)
|
|
|
|
break;
|
|
|
|
*cp++ = '\0';
|
|
|
|
person = cp;
|
|
|
|
while (*cp) {
|
|
|
|
if (*cp != ' ') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
while (isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == '\0')
|
|
|
|
break;
|
|
|
|
if (isdigit(*cp)) {
|
|
|
|
if (requests >= MAXREQUESTS)
|
|
|
|
fatal("Too many requests");
|
|
|
|
requ[requests++] = atoi(cp);
|
|
|
|
} else {
|
|
|
|
if (users >= MAXUSERS)
|
|
|
|
fatal("Too many users");
|
|
|
|
user[users++] = cp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rmjob();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
fatal("Illegal service request");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a pass through the printcap database and start printing any
|
|
|
|
* files left from the last time the machine went down.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
1993-03-21 12:45:37 +03:00
|
|
|
startup()
|
|
|
|
{
|
1994-05-18 05:25:37 +04:00
|
|
|
char *buf;
|
1997-10-05 19:11:58 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
int pid;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Restart the daemons.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
while (cgetnext(&buf, printcapdb) > 0) {
|
1997-10-05 15:52:17 +04:00
|
|
|
if (ckqueue(buf) <= 0) {
|
|
|
|
free(buf);
|
|
|
|
continue; /* no work to do for this printer */
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
for (cp = buf; *cp; cp++)
|
|
|
|
if (*cp == '|' || *cp == ':') {
|
|
|
|
*cp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
1997-10-05 15:52:17 +04:00
|
|
|
if (lflag)
|
|
|
|
syslog(LOG_INFO, "work for %s", buf);
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((pid = fork()) < 0) {
|
|
|
|
syslog(LOG_WARNING, "startup: cannot fork");
|
1994-05-18 05:25:37 +04:00
|
|
|
mcleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (!pid) {
|
1994-05-18 05:25:37 +04:00
|
|
|
printer = buf;
|
|
|
|
cgetclose();
|
1993-03-21 12:45:37 +03:00
|
|
|
printjob();
|
1997-10-05 15:52:17 +04:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-05 15:52:17 +04:00
|
|
|
else free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure there's some work to do before forking off a child
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ckqueue(cap)
|
|
|
|
char *cap;
|
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
struct dirent *d;
|
1997-10-05 15:52:17 +04:00
|
|
|
DIR *dirp;
|
|
|
|
char *spooldir;
|
|
|
|
|
|
|
|
if (cgetstr(cap, "sd", &spooldir) == -1)
|
|
|
|
spooldir = _PATH_DEFSPOOL;
|
|
|
|
if ((dirp = opendir(spooldir)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
while ((d = readdir(dirp)) != NULL) {
|
|
|
|
if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
|
|
|
|
continue; /* daemon control files only */
|
|
|
|
closedir(dirp);
|
|
|
|
return (1); /* found something */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-05 15:52:17 +04:00
|
|
|
closedir(dirp);
|
|
|
|
return (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#define DUMMY ":nobody::"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if the from host has access to the line printer.
|
|
|
|
*/
|
1994-05-18 05:25:37 +04:00
|
|
|
static void
|
1993-03-21 12:45:37 +03:00
|
|
|
chkhost(f)
|
|
|
|
struct sockaddr_in *f;
|
|
|
|
{
|
1997-10-05 19:11:58 +04:00
|
|
|
struct hostent *hp;
|
|
|
|
FILE *hostf;
|
|
|
|
int first = 1, good = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
f->sin_port = ntohs(f->sin_port);
|
|
|
|
if (f->sin_family != AF_INET || f->sin_port >= IPPORT_RESERVED)
|
|
|
|
fatal("Malformed from address");
|
1994-05-18 05:25:37 +04:00
|
|
|
|
|
|
|
/* Need real hostname for temporary filenames */
|
1993-03-21 12:45:37 +03:00
|
|
|
hp = gethostbyaddr((char *)&f->sin_addr,
|
|
|
|
sizeof(struct in_addr), f->sin_family);
|
1994-05-18 05:25:37 +04:00
|
|
|
if (hp == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
fatal("Host name for your address (%s) unknown",
|
|
|
|
inet_ntoa(f->sin_addr));
|
|
|
|
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)strncpy(fromb, hp->h_name, sizeof(fromb) - 1);
|
1994-05-18 05:25:37 +04:00
|
|
|
from[sizeof(fromb) - 1] = '\0';
|
1993-03-21 12:45:37 +03:00
|
|
|
from = fromb;
|
1994-05-18 05:25:37 +04:00
|
|
|
|
1997-10-05 19:11:58 +04:00
|
|
|
/* Check for spoof, ala rlogind */
|
|
|
|
hp = gethostbyname(fromb);
|
|
|
|
if (!hp)
|
|
|
|
fatal("hostname for your address (%s) unknown",
|
|
|
|
inet_ntoa(f->sin_addr));
|
|
|
|
for (; good == 0 && hp->h_addr_list[0] != NULL; hp->h_addr_list++) {
|
1997-10-18 12:52:17 +04:00
|
|
|
if (!memcmp(hp->h_addr_list[0], (caddr_t)&f->sin_addr,
|
1997-10-05 19:11:58 +04:00
|
|
|
sizeof(f->sin_addr)))
|
|
|
|
good = 1;
|
|
|
|
}
|
|
|
|
if (good == 0)
|
|
|
|
fatal("address for your hostname (%s) not matched",
|
|
|
|
inet_ntoa(f->sin_addr));
|
1993-03-21 12:45:37 +03:00
|
|
|
hostf = fopen(_PATH_HOSTSEQUIV, "r");
|
|
|
|
again:
|
|
|
|
if (hostf) {
|
1994-05-18 05:25:37 +04:00
|
|
|
if (__ivaliduser(hostf, f->sin_addr.s_addr,
|
|
|
|
DUMMY, DUMMY) == 0) {
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)fclose(hostf);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
1996-12-09 12:57:40 +03:00
|
|
|
(void)fclose(hostf);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (first == 1) {
|
|
|
|
first = 0;
|
|
|
|
hostf = fopen(_PATH_HOSTSLPD, "r");
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
fatal("Your host does not have line printer access");
|
1994-05-18 05:25:37 +04:00
|
|
|
/*NOTREACHED*/
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1997-10-05 19:11:58 +04:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
extern char *__progname; /* XXX */
|
|
|
|
|
|
|
|
fprintf(stderr, "usage: %s [-d] [-l]\n", __progname);
|
|
|
|
exit(1);
|
|
|
|
}
|