2017-11-20 19:49:47 +00:00
|
|
|
/*-
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
1996-05-05 14:04:33 +00:00
|
|
|
* Copyright (c) 1983, 1993, 1994
|
|
|
|
* 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.
|
2017-02-28 23:42:47 +00:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1996-05-05 14:04:33 +00:00
|
|
|
* 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-09-24 06:48:24 +00:00
|
|
|
static const char copyright[] =
|
1996-05-05 14:04:33 +00:00
|
|
|
"@(#) Copyright (c) 1983, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-09-24 06:48:24 +00:00
|
|
|
#if 0
|
2003-07-14 19:59:33 +00:00
|
|
|
#ifndef lint
|
1996-05-05 14:04:33 +00:00
|
|
|
static char sccsid[] = "@(#)lpd.c 8.7 (Berkeley) 5/10/95";
|
2003-07-14 19:59:33 +00:00
|
|
|
#endif /* not lint */
|
2003-07-06 12:44:11 +00:00
|
|
|
#endif
|
|
|
|
|
2003-07-14 15:54:41 +00:00
|
|
|
#include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
|
2003-07-06 12:44:11 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <netinet/in.h>
|
1996-05-09 22:44:28 +00:00
|
|
|
#include <arpa/inet.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <signal.h>
|
1997-08-23 15:53:00 +00:00
|
|
|
#include <err.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
1997-08-23 15:53:00 +00:00
|
|
|
#include <sysexits.h>
|
1996-05-05 14:04:33 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include "lp.h"
|
|
|
|
#include "lp.local.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
int lflag; /* log requests flag */
|
2002-04-19 19:33:24 +00:00
|
|
|
int sflag; /* no incoming port flag */
|
2021-04-03 17:51:56 +00:00
|
|
|
int Fflag; /* run in foreground flag */
|
1996-05-05 14:04:33 +00:00
|
|
|
int from_remote; /* from remote socket */
|
|
|
|
|
2001-06-12 16:38:20 +00:00
|
|
|
int main(int argc, char **_argv);
|
|
|
|
static void reapchild(int _signo);
|
|
|
|
static void mcleanup(int _signo);
|
|
|
|
static void doit(void);
|
|
|
|
static void startup(void);
|
2001-06-25 01:45:25 +00:00
|
|
|
static void chkhost(struct sockaddr *_f, int _ch_opts);
|
2001-06-12 16:38:20 +00:00
|
|
|
static int ckqueue(struct printer *_pp);
|
2002-06-24 20:57:14 +00:00
|
|
|
static void fhosterr(int _ch_opts, char *_sysmsg, char *_usermsg);
|
2001-06-15 22:30:02 +00:00
|
|
|
static int *socksetup(int _af, int _debuglvl);
|
2001-06-12 16:38:20 +00:00
|
|
|
static void usage(void);
|
2000-12-16 18:06:09 +00:00
|
|
|
|
|
|
|
/* XXX from libc/net/rcmd.c */
|
2007-11-07 10:53:41 +00:00
|
|
|
extern int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
|
|
|
|
const char *, const char *);
|
1996-05-05 14:04:33 +00:00
|
|
|
|
1997-07-23 00:49:46 +00:00
|
|
|
uid_t uid, euid;
|
|
|
|
|
2001-06-25 01:45:25 +00:00
|
|
|
#define LPD_NOPORTCHK 0001 /* skip reserved-port check */
|
|
|
|
#define LPD_LOGCONNERR 0002 /* (sys)log connection errors */
|
2002-06-24 20:57:14 +00:00
|
|
|
#define LPD_ADDFROMLINE 0004 /* just used for fhosterr() */
|
2001-06-25 01:45:25 +00:00
|
|
|
|
1996-05-05 14:04:33 +00:00
|
|
|
int
|
2001-06-12 16:38:20 +00:00
|
|
|
main(int argc, char **argv)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2001-07-23 00:13:02 +00:00
|
|
|
int ch_options, errs, f, funix, *finet, i, lfd, socket_debug;
|
1996-05-05 14:04:33 +00:00
|
|
|
fd_set defreadfds;
|
|
|
|
struct sockaddr_un un, fromunix;
|
2000-12-16 18:06:09 +00:00
|
|
|
struct sockaddr_storage frominet;
|
2001-07-23 00:13:02 +00:00
|
|
|
socklen_t fromlen;
|
1997-12-02 20:46:22 +00:00
|
|
|
sigset_t omask, nmask;
|
1997-08-23 15:53:00 +00:00
|
|
|
struct servent *sp, serv;
|
2000-12-16 18:06:09 +00:00
|
|
|
int inet_flag = 0, inet6_flag = 0;
|
1996-05-05 14:04:33 +00:00
|
|
|
|
1997-07-23 00:49:46 +00:00
|
|
|
euid = geteuid(); /* these shouldn't be different */
|
|
|
|
uid = getuid();
|
2001-06-25 01:45:25 +00:00
|
|
|
|
|
|
|
ch_options = 0;
|
2000-11-15 06:00:42 +00:00
|
|
|
socket_debug = 0;
|
2001-06-15 22:03:07 +00:00
|
|
|
gethostname(local_host, sizeof(local_host));
|
1997-07-23 00:49:46 +00:00
|
|
|
|
2001-06-15 16:28:37 +00:00
|
|
|
progname = "lpd";
|
1997-07-23 00:49:46 +00:00
|
|
|
|
1997-08-23 15:53:00 +00:00
|
|
|
if (euid != 0)
|
|
|
|
errx(EX_NOPERM,"must run as root");
|
1996-05-05 14:04:33 +00:00
|
|
|
|
1997-08-23 15:53:00 +00:00
|
|
|
errs = 0;
|
2021-04-03 17:51:56 +00:00
|
|
|
while ((i = getopt(argc, argv, "cdlpswFW46")) != -1)
|
1997-08-23 15:53:00 +00:00
|
|
|
switch (i) {
|
2001-06-25 01:45:25 +00:00
|
|
|
case 'c':
|
|
|
|
/* log all kinds of connection-errors to syslog */
|
|
|
|
ch_options |= LPD_LOGCONNERR;
|
|
|
|
break;
|
1997-08-23 15:53:00 +00:00
|
|
|
case 'd':
|
2000-11-15 06:00:42 +00:00
|
|
|
socket_debug++;
|
1997-08-23 15:53:00 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
lflag++;
|
|
|
|
break;
|
2002-04-19 19:33:24 +00:00
|
|
|
case 'p': /* letter initially used for -s */
|
|
|
|
/*
|
|
|
|
* This will probably be removed with 5.0-release.
|
|
|
|
*/
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 's': /* secure (no inet) */
|
|
|
|
sflag++;
|
2000-03-29 11:15:54 +00:00
|
|
|
break;
|
2001-07-22 01:05:20 +00:00
|
|
|
case 'w': /* netbsd uses -w for maxwait */
|
|
|
|
/*
|
|
|
|
* This will be removed after the release of 4.4, as
|
|
|
|
* it conflicts with -w in netbsd's lpd. For now it
|
|
|
|
* is just a warning, so we won't suddenly break lpd
|
|
|
|
* for anyone who is currently using the option.
|
|
|
|
*/
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"NOTE: the -w option has been renamed -W");
|
|
|
|
syslog(LOG_WARNING,
|
|
|
|
"NOTE: please change your lpd config to use -W");
|
|
|
|
/* FALLTHROUGH */
|
2021-04-03 17:51:56 +00:00
|
|
|
case 'F':
|
|
|
|
Fflag++;
|
|
|
|
break;
|
2001-07-22 01:05:20 +00:00
|
|
|
case 'W':
|
2001-06-25 01:45:25 +00:00
|
|
|
/* allow connections coming from a non-reserved port */
|
|
|
|
/* (done by some lpr-implementations for MS-Windows) */
|
|
|
|
ch_options |= LPD_NOPORTCHK;
|
|
|
|
break;
|
2000-12-16 18:06:09 +00:00
|
|
|
case '4':
|
|
|
|
family = PF_INET;
|
|
|
|
inet_flag++;
|
|
|
|
break;
|
|
|
|
case '6':
|
2001-03-12 02:03:53 +00:00
|
|
|
#ifdef INET6
|
2000-12-16 18:06:09 +00:00
|
|
|
family = PF_INET6;
|
|
|
|
inet6_flag++;
|
2001-03-12 02:03:53 +00:00
|
|
|
#else
|
|
|
|
errx(EX_USAGE, "lpd compiled sans INET6 (IPv6 support)");
|
|
|
|
#endif
|
2000-12-16 18:06:09 +00:00
|
|
|
break;
|
2001-07-22 01:05:20 +00:00
|
|
|
/*
|
|
|
|
* The following options are not in FreeBSD (yet?), but are
|
|
|
|
* listed here to "reserve" them, because the option-letters
|
|
|
|
* are used by either NetBSD or OpenBSD (as of July 2001).
|
|
|
|
*/
|
|
|
|
case 'b': /* set bind-addr */
|
|
|
|
case 'n': /* set max num of children */
|
|
|
|
case 'r': /* allow 'of' for remote ptrs */
|
|
|
|
/* ...[not needed in freebsd] */
|
|
|
|
/* FALLTHROUGH */
|
1997-08-23 15:53:00 +00:00
|
|
|
default:
|
|
|
|
errs++;
|
|
|
|
}
|
2000-12-16 18:06:09 +00:00
|
|
|
if (inet_flag && inet6_flag)
|
|
|
|
family = PF_UNSPEC;
|
1997-08-23 15:53:00 +00:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (errs)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (argc == 1) {
|
|
|
|
if ((i = atoi(argv[0])) == 0)
|
|
|
|
usage();
|
|
|
|
if (i < 0 || i > USHRT_MAX)
|
|
|
|
errx(EX_USAGE, "port # %d is invalid", i);
|
|
|
|
|
|
|
|
serv.s_port = htons(i);
|
|
|
|
sp = &serv;
|
|
|
|
argc--;
|
|
|
|
} else {
|
|
|
|
sp = getservbyname("printer", "tcp");
|
|
|
|
if (sp == NULL)
|
|
|
|
errx(EX_OSFILE, "printer/tcp: unknown service");
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
|
1997-08-23 15:53:00 +00:00
|
|
|
if (argc != 0)
|
|
|
|
usage();
|
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
/*
|
|
|
|
* We run chkprintcap right away to catch any errors and blat them
|
|
|
|
* to stderr while we still have it open, rather than sending them
|
|
|
|
* to syslog and leaving the user wondering why lpd started and
|
|
|
|
* then stopped. There should probably be a command-line flag to
|
|
|
|
* ignore errors from chkprintcap.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
pid = fork();
|
|
|
|
if (pid < 0) {
|
|
|
|
err(EX_OSERR, "cannot fork");
|
|
|
|
} else if (pid == 0) { /* child */
|
|
|
|
execl(_PATH_CHKPRINTCAP, _PATH_CHKPRINTCAP, (char *)0);
|
|
|
|
err(EX_OSERR, "cannot execute %s", _PATH_CHKPRINTCAP);
|
|
|
|
}
|
|
|
|
if (waitpid(pid, &status, 0) < 0) {
|
|
|
|
err(EX_OSERR, "cannot wait");
|
|
|
|
}
|
|
|
|
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
|
|
|
|
errx(EX_OSFILE, "%d errors in printcap file, exiting",
|
|
|
|
WEXITSTATUS(status));
|
|
|
|
}
|
|
|
|
|
2021-04-03 17:51:56 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
Fflag++;
|
|
|
|
#endif
|
1996-05-05 14:04:33 +00:00
|
|
|
/*
|
2021-04-03 17:51:56 +00:00
|
|
|
* Set up standard environment by detaching from the parent
|
|
|
|
* if -F not specified
|
1996-05-05 14:04:33 +00:00
|
|
|
*/
|
2021-04-03 17:51:56 +00:00
|
|
|
if (Fflag == 0) {
|
|
|
|
daemon(0, 0);
|
|
|
|
}
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
openlog("lpd", LOG_PID, LOG_LPR);
|
2002-04-19 19:33:24 +00:00
|
|
|
syslog(LOG_INFO, "lpd startup: logging=%d%s%s", lflag,
|
|
|
|
socket_debug ? " dbg" : "", sflag ? " net-secure" : "");
|
1996-05-05 14:04:33 +00:00
|
|
|
(void) umask(0);
|
1997-12-02 20:46:22 +00:00
|
|
|
/*
|
|
|
|
* NB: This depends on O_NONBLOCK semantics doing the right thing;
|
|
|
|
* i.e., applying only to the O_EXLOCK and not to the rest of the
|
|
|
|
* open/creation. As of 1997-12-02, this is the case for commonly-
|
|
|
|
* used filesystems. There are other places in this code which
|
|
|
|
* make the same assumption.
|
|
|
|
*/
|
|
|
|
lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
|
|
|
|
LOCK_FILE_MODE);
|
1996-05-05 14:04:33 +00:00
|
|
|
if (lfd < 0) {
|
2002-04-19 18:36:56 +00:00
|
|
|
if (errno == EWOULDBLOCK) /* active daemon present */
|
1996-05-05 14:04:33 +00:00
|
|
|
exit(0);
|
|
|
|
syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
fcntl(lfd, F_SETFL, 0); /* turn off non-blocking mode */
|
1996-05-05 14:04:33 +00:00
|
|
|
ftruncate(lfd, 0);
|
|
|
|
/*
|
|
|
|
* write process id for others to know
|
|
|
|
*/
|
|
|
|
sprintf(line, "%u\n", getpid());
|
|
|
|
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();
|
|
|
|
(void) unlink(_PATH_SOCKETNAME);
|
|
|
|
funix = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (funix < 0) {
|
|
|
|
syslog(LOG_ERR, "socket: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
|
|
|
|
sigemptyset(&nmask);
|
|
|
|
sigaddset(&nmask, SIGHUP);
|
|
|
|
sigaddset(&nmask, SIGINT);
|
|
|
|
sigaddset(&nmask, SIGQUIT);
|
|
|
|
sigaddset(&nmask, SIGTERM);
|
|
|
|
sigprocmask(SIG_BLOCK, &nmask, &omask);
|
|
|
|
|
1996-10-27 03:06:52 +00:00
|
|
|
(void) umask(07);
|
1996-05-05 14:04:33 +00:00
|
|
|
signal(SIGHUP, mcleanup);
|
|
|
|
signal(SIGINT, mcleanup);
|
|
|
|
signal(SIGQUIT, mcleanup);
|
|
|
|
signal(SIGTERM, mcleanup);
|
|
|
|
memset(&un, 0, sizeof(un));
|
|
|
|
un.sun_family = AF_UNIX;
|
|
|
|
strcpy(un.sun_path, _PATH_SOCKETNAME);
|
|
|
|
#ifndef SUN_LEN
|
|
|
|
#define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
|
|
|
|
#endif
|
|
|
|
if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
|
|
|
|
syslog(LOG_ERR, "ubind: %m");
|
|
|
|
exit(1);
|
|
|
|
}
|
1996-10-27 03:06:52 +00:00
|
|
|
(void) umask(0);
|
1997-12-02 20:46:22 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
|
1996-05-05 14:04:33 +00:00
|
|
|
FD_ZERO(&defreadfds);
|
|
|
|
FD_SET(funix, &defreadfds);
|
|
|
|
listen(funix, 5);
|
2002-04-19 19:33:24 +00:00
|
|
|
if (sflag == 0) {
|
2001-06-15 22:30:02 +00:00
|
|
|
finet = socksetup(family, socket_debug);
|
2000-12-16 18:06:09 +00:00
|
|
|
} else
|
|
|
|
finet = NULL; /* pretend we couldn't open TCP socket. */
|
|
|
|
if (finet) {
|
|
|
|
for (i = 1; i <= *finet; i++) {
|
|
|
|
FD_SET(finet[i], &defreadfds);
|
|
|
|
listen(finet[i], 5);
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Main loop: accept, do a request, continue.
|
|
|
|
*/
|
|
|
|
memset(&frominet, 0, sizeof(frominet));
|
|
|
|
memset(&fromunix, 0, sizeof(fromunix));
|
2000-11-15 06:00:42 +00:00
|
|
|
if (lflag)
|
|
|
|
syslog(LOG_INFO, "lpd startup: ready to accept requests");
|
1997-12-02 20:46:22 +00:00
|
|
|
/*
|
|
|
|
* XXX - should be redone for multi-protocol
|
|
|
|
*/
|
1996-05-05 14:04:33 +00:00
|
|
|
for (;;) {
|
2001-06-15 22:30:02 +00:00
|
|
|
int domain, nfds, s;
|
1996-05-05 14:04:33 +00:00
|
|
|
fd_set readfds;
|
|
|
|
|
|
|
|
FD_COPY(&defreadfds, &readfds);
|
|
|
|
nfds = select(20, &readfds, 0, 0, 0);
|
|
|
|
if (nfds <= 0) {
|
|
|
|
if (nfds < 0 && errno != EINTR)
|
|
|
|
syslog(LOG_WARNING, "select: %m");
|
|
|
|
continue;
|
|
|
|
}
|
2001-06-15 22:30:02 +00:00
|
|
|
domain = -1; /* avoid compile-time warning */
|
|
|
|
s = -1; /* avoid compile-time warning */
|
1996-05-05 14:04:33 +00:00
|
|
|
if (FD_ISSET(funix, &readfds)) {
|
|
|
|
domain = AF_UNIX, fromlen = sizeof(fromunix);
|
|
|
|
s = accept(funix,
|
|
|
|
(struct sockaddr *)&fromunix, &fromlen);
|
2000-12-16 18:06:09 +00:00
|
|
|
} else {
|
|
|
|
for (i = 1; i <= *finet; i++)
|
|
|
|
if (FD_ISSET(finet[i], &readfds)) {
|
|
|
|
domain = AF_INET;
|
|
|
|
fromlen = sizeof(frominet);
|
|
|
|
s = accept(finet[i],
|
|
|
|
(struct sockaddr *)&frominet,
|
|
|
|
&fromlen);
|
|
|
|
}
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
if (s < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
syslog(LOG_WARNING, "accept: %m");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (fork() == 0) {
|
2001-07-14 21:49:17 +00:00
|
|
|
/*
|
|
|
|
* Note that printjob() also plays around with
|
|
|
|
* signal-handling routines, and may need to be
|
|
|
|
* changed when making changes to signal-handling.
|
|
|
|
*/
|
|
|
|
signal(SIGCHLD, SIG_DFL);
|
1996-05-05 14:04:33 +00:00
|
|
|
signal(SIGHUP, SIG_IGN);
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
signal(SIGQUIT, SIG_IGN);
|
|
|
|
signal(SIGTERM, SIG_IGN);
|
|
|
|
(void) close(funix);
|
2002-04-19 19:33:24 +00:00
|
|
|
if (sflag == 0 && finet) {
|
2000-12-16 18:06:09 +00:00
|
|
|
for (i = 1; i <= *finet; i++)
|
|
|
|
(void)close(finet[i]);
|
2000-03-29 11:15:54 +00:00
|
|
|
}
|
2003-08-13 20:31:33 +00:00
|
|
|
dup2(s, STDOUT_FILENO);
|
1996-05-05 14:04:33 +00:00
|
|
|
(void) close(s);
|
|
|
|
if (domain == AF_INET) {
|
2000-12-16 18:06:09 +00:00
|
|
|
/* for both AF_INET and AF_INET6 */
|
1996-05-05 14:04:33 +00:00
|
|
|
from_remote = 1;
|
2001-06-25 01:45:25 +00:00
|
|
|
chkhost((struct sockaddr *)&frominet,
|
|
|
|
ch_options);
|
1996-05-05 14:04:33 +00:00
|
|
|
} else
|
|
|
|
from_remote = 0;
|
|
|
|
doit();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
(void) close(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
reapchild(int signo __unused)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2002-06-03 20:47:01 +00:00
|
|
|
int status;
|
1996-05-05 14:04:33 +00:00
|
|
|
|
2002-06-03 20:47:01 +00:00
|
|
|
while (wait3(&status, WNOHANG, 0) > 0)
|
1996-05-05 14:04:33 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
mcleanup(int signo)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2000-09-27 19:03:05 +00:00
|
|
|
/*
|
|
|
|
* XXX syslog(3) is not signal-safe.
|
|
|
|
*/
|
|
|
|
if (lflag) {
|
|
|
|
if (signo)
|
|
|
|
syslog(LOG_INFO, "exiting on signal %d", signo);
|
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "exiting");
|
|
|
|
}
|
1996-05-05 14:04:33 +00:00
|
|
|
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 */
|
|
|
|
|
2001-06-15 22:03:07 +00:00
|
|
|
/* buffer to hold the client's machine-name */
|
|
|
|
static char frombuf[MAXHOSTNAMELEN];
|
1996-05-05 14:04:33 +00:00
|
|
|
char cbuf[BUFSIZ]; /* command line buffer */
|
2001-06-12 16:38:20 +00:00
|
|
|
const char *cmdnames[] = {
|
1996-05-05 14:04:33 +00:00
|
|
|
"null",
|
|
|
|
"printjob",
|
|
|
|
"recvjob",
|
|
|
|
"displayq short",
|
|
|
|
"displayq long",
|
|
|
|
"rmjob"
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
doit(void)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
1997-12-02 20:46:22 +00:00
|
|
|
char *cp, *printer;
|
|
|
|
int n;
|
|
|
|
int status;
|
|
|
|
struct printer myprinter, *pp = &myprinter;
|
|
|
|
|
|
|
|
init_printer(&myprinter);
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cp = cbuf;
|
|
|
|
do {
|
|
|
|
if (cp >= &cbuf[sizeof(cbuf) - 1])
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Command line too long");
|
2001-07-22 00:03:21 +00:00
|
|
|
if ((n = read(STDOUT_FILENO, cp, 1)) != 1) {
|
1996-05-05 14:04:33 +00:00
|
|
|
if (n < 0)
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Lost connection");
|
1996-05-05 14:04:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} while (*cp++ != '\n');
|
|
|
|
*--cp = '\0';
|
|
|
|
cp = cbuf;
|
|
|
|
if (lflag) {
|
|
|
|
if (*cp >= '\1' && *cp <= '\5')
|
|
|
|
syslog(LOG_INFO, "%s requests %s %s",
|
2001-06-15 22:03:07 +00:00
|
|
|
from_host, cmdnames[(u_char)*cp], cp+1);
|
1996-05-05 14:04:33 +00:00
|
|
|
else
|
|
|
|
syslog(LOG_INFO, "bad request (%d) from %s",
|
2001-06-15 22:03:07 +00:00
|
|
|
*cp, from_host);
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
switch (*cp++) {
|
1997-12-02 20:46:22 +00:00
|
|
|
case CMD_CHECK_QUE: /* check the queue, print any jobs there */
|
|
|
|
startprinting(cp);
|
1996-05-05 14:04:33 +00:00
|
|
|
break;
|
1997-12-02 20:46:22 +00:00
|
|
|
case CMD_TAKE_THIS: /* receive files to be queued */
|
1996-05-05 14:04:33 +00:00
|
|
|
if (!from_remote) {
|
|
|
|
syslog(LOG_INFO, "illegal request (%d)", *cp);
|
|
|
|
exit(1);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
recvjob(cp);
|
1996-05-05 14:04:33 +00:00
|
|
|
break;
|
1997-12-02 20:46:22 +00:00
|
|
|
case CMD_SHOWQ_SHORT: /* display the queue (short form) */
|
|
|
|
case CMD_SHOWQ_LONG: /* display the queue (long form) */
|
|
|
|
/* XXX - this all needs to be redone. */
|
1996-05-05 14:04:33 +00:00
|
|
|
printer = cp;
|
|
|
|
while (*cp) {
|
|
|
|
if (*cp != ' ') {
|
|
|
|
cp++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
while (isspace(*cp))
|
|
|
|
cp++;
|
|
|
|
if (*cp == '\0')
|
|
|
|
break;
|
|
|
|
if (isdigit(*cp)) {
|
|
|
|
if (requests >= MAXREQUESTS)
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Too many requests");
|
1996-05-05 14:04:33 +00:00
|
|
|
requ[requests++] = atoi(cp);
|
|
|
|
} else {
|
|
|
|
if (users >= MAXUSERS)
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Too many users");
|
1996-05-05 14:04:33 +00:00
|
|
|
user[users++] = cp;
|
|
|
|
}
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
status = getprintcap(printer, pp);
|
|
|
|
if (status < 0)
|
2001-07-15 00:09:46 +00:00
|
|
|
fatal(pp, "%s", pcaperr(status));
|
1997-12-02 20:46:22 +00:00
|
|
|
displayq(pp, cbuf[0] == CMD_SHOWQ_LONG);
|
1996-05-05 14:04:33 +00:00
|
|
|
exit(0);
|
1997-12-02 20:46:22 +00:00
|
|
|
case CMD_RMJOB: /* remove a job from the queue */
|
1996-05-05 14:04:33 +00:00
|
|
|
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)
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Too many requests");
|
1996-05-05 14:04:33 +00:00
|
|
|
requ[requests++] = atoi(cp);
|
|
|
|
} else {
|
|
|
|
if (users >= MAXUSERS)
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Too many users");
|
1996-05-05 14:04:33 +00:00
|
|
|
user[users++] = cp;
|
|
|
|
}
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
rmjob(printer);
|
1996-05-05 14:04:33 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
fatal(0, "Illegal service request");
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a pass through the printcap database and start printing any
|
|
|
|
* files left from the last time the machine went down.
|
|
|
|
*/
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
startup(void)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
1997-12-02 20:46:22 +00:00
|
|
|
int pid, status, more;
|
|
|
|
struct printer myprinter, *pp = &myprinter;
|
|
|
|
|
|
|
|
more = firstprinter(pp, &status);
|
|
|
|
if (status)
|
|
|
|
goto errloop;
|
|
|
|
while (more) {
|
|
|
|
if (ckqueue(pp) <= 0) {
|
|
|
|
goto next;
|
1997-05-17 03:04:43 +00:00
|
|
|
}
|
1996-05-05 14:04:33 +00:00
|
|
|
if (lflag)
|
2000-11-15 06:00:42 +00:00
|
|
|
syslog(LOG_INFO, "lpd startup: work for %s",
|
|
|
|
pp->printer);
|
1996-05-05 14:04:33 +00:00
|
|
|
if ((pid = fork()) < 0) {
|
2000-11-15 06:00:42 +00:00
|
|
|
syslog(LOG_WARNING, "lpd startup: cannot fork for %s",
|
|
|
|
pp->printer);
|
1996-05-05 14:04:33 +00:00
|
|
|
mcleanup(0);
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
if (pid == 0) {
|
|
|
|
lastprinter();
|
|
|
|
printjob(pp);
|
1996-05-05 14:04:33 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1997-12-02 20:46:22 +00:00
|
|
|
do {
|
|
|
|
next:
|
|
|
|
more = nextprinter(pp, &status);
|
|
|
|
errloop:
|
|
|
|
if (status)
|
|
|
|
syslog(LOG_WARNING,
|
2000-11-15 06:00:42 +00:00
|
|
|
"lpd startup: printcap entry for %s has errors, skipping",
|
2001-06-16 00:14:02 +00:00
|
|
|
pp->printer ? pp->printer : "<noname?>");
|
1997-12-02 20:46:22 +00:00
|
|
|
} while (more && status);
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure there's some work to do before forking off a child
|
|
|
|
*/
|
|
|
|
static int
|
2001-06-12 16:38:20 +00:00
|
|
|
ckqueue(struct printer *pp)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
|
|
|
register struct dirent *d;
|
|
|
|
DIR *dirp;
|
|
|
|
char *spooldir;
|
|
|
|
|
1997-12-02 20:46:22 +00:00
|
|
|
spooldir = pp->spool_dir;
|
1996-05-05 14:04:33 +00:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DUMMY ":nobody::"
|
|
|
|
|
|
|
|
/*
|
2001-06-25 01:45:25 +00:00
|
|
|
* Check to see if the host connecting to this host has access to any
|
|
|
|
* lpd services on this host.
|
1996-05-05 14:04:33 +00:00
|
|
|
*/
|
|
|
|
static void
|
2001-06-25 01:45:25 +00:00
|
|
|
chkhost(struct sockaddr *f, int ch_opts)
|
1996-05-05 14:04:33 +00:00
|
|
|
{
|
2000-12-16 18:06:09 +00:00
|
|
|
struct addrinfo hints, *res, *r;
|
1996-05-05 14:04:33 +00:00
|
|
|
register FILE *hostf;
|
2001-06-15 22:03:07 +00:00
|
|
|
char hostbuf[NI_MAXHOST], ip[NI_MAXHOST];
|
2000-12-16 18:06:09 +00:00
|
|
|
char serv[NI_MAXSERV];
|
2002-06-24 20:57:14 +00:00
|
|
|
char *syserr, *usererr;
|
2016-04-25 20:58:54 +00:00
|
|
|
int error, errsav, fpass, good;
|
2001-06-25 01:45:25 +00:00
|
|
|
|
|
|
|
from_host = ".na.";
|
1996-05-05 14:04:33 +00:00
|
|
|
|
|
|
|
/* Need real hostname for temporary filenames */
|
2001-06-15 22:03:07 +00:00
|
|
|
error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0,
|
|
|
|
NI_NAMEREQD);
|
2000-12-16 18:06:09 +00:00
|
|
|
if (error) {
|
2001-06-25 01:45:25 +00:00
|
|
|
errsav = error;
|
2001-06-15 22:03:07 +00:00
|
|
|
error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf),
|
2005-05-13 16:43:28 +00:00
|
|
|
NULL, 0, NI_NUMERICHOST);
|
2002-06-24 20:57:14 +00:00
|
|
|
if (error) {
|
|
|
|
asprintf(&syserr,
|
|
|
|
"can not determine hostname for remote host (%d,%d)",
|
|
|
|
errsav, error);
|
|
|
|
asprintf(&usererr,
|
|
|
|
"Host name for your address is not known");
|
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
asprintf(&syserr,
|
|
|
|
"Host name for remote host (%s) not known (%d)",
|
|
|
|
hostbuf, errsav);
|
|
|
|
asprintf(&usererr,
|
|
|
|
"Host name for your address (%s) is not known",
|
|
|
|
hostbuf);
|
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
2000-12-16 18:06:09 +00:00
|
|
|
}
|
1996-05-05 14:04:33 +00:00
|
|
|
|
2001-06-15 22:03:07 +00:00
|
|
|
strlcpy(frombuf, hostbuf, sizeof(frombuf));
|
|
|
|
from_host = frombuf;
|
2002-06-24 20:57:14 +00:00
|
|
|
ch_opts |= LPD_ADDFROMLINE;
|
2000-12-16 18:06:09 +00:00
|
|
|
|
|
|
|
/* Need address in stringform for comparison (no DNS lookup here) */
|
2001-06-15 22:03:07 +00:00
|
|
|
error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0,
|
2005-05-13 16:43:28 +00:00
|
|
|
NI_NUMERICHOST);
|
2002-06-24 20:57:14 +00:00
|
|
|
if (error) {
|
|
|
|
asprintf(&syserr, "Cannot print IP address (error %d)",
|
|
|
|
error);
|
|
|
|
asprintf(&usererr, "Cannot print IP address for your host");
|
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-06-15 22:03:07 +00:00
|
|
|
from_ip = strdup(hostbuf);
|
1996-05-05 14:04:33 +00:00
|
|
|
|
2000-12-16 18:06:09 +00:00
|
|
|
/* Reject numeric addresses */
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = family;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
|
|
|
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
|
2001-06-15 22:03:07 +00:00
|
|
|
if (getaddrinfo(from_host, NULL, &hints, &res) == 0) {
|
2000-12-16 18:06:09 +00:00
|
|
|
freeaddrinfo(res);
|
2002-06-24 20:57:14 +00:00
|
|
|
/* This syslog message already includes from_host */
|
|
|
|
ch_opts &= ~LPD_ADDFROMLINE;
|
|
|
|
asprintf(&syserr, "reverse lookup results in non-FQDN %s",
|
2001-06-25 01:45:25 +00:00
|
|
|
from_host);
|
2002-06-24 20:57:14 +00:00
|
|
|
/* same message to both syslog and remote user */
|
|
|
|
fhosterr(ch_opts, syserr, syserr);
|
|
|
|
/* NOTREACHED */
|
2000-12-16 18:06:09 +00:00
|
|
|
}
|
|
|
|
|
1996-10-27 03:06:52 +00:00
|
|
|
/* Check for spoof, ala rlogind */
|
2000-12-16 18:06:09 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = family;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; /*dummy*/
|
2001-06-15 22:03:07 +00:00
|
|
|
error = getaddrinfo(from_host, NULL, &hints, &res);
|
2000-12-16 18:06:09 +00:00
|
|
|
if (error) {
|
2002-06-24 20:57:14 +00:00
|
|
|
asprintf(&syserr, "dns lookup for address %s failed: %s",
|
|
|
|
from_ip, gai_strerror(error));
|
|
|
|
asprintf(&usererr, "hostname for your address (%s) unknown: %s",
|
|
|
|
from_ip, gai_strerror(error));
|
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
2000-12-16 18:06:09 +00:00
|
|
|
}
|
|
|
|
good = 0;
|
|
|
|
for (r = res; good == 0 && r; r = r->ai_next) {
|
|
|
|
error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
|
2005-05-13 16:43:28 +00:00
|
|
|
NULL, 0, NI_NUMERICHOST);
|
2000-12-16 18:06:09 +00:00
|
|
|
if (!error && !strcmp(from_ip, ip))
|
1996-10-27 03:06:52 +00:00
|
|
|
good = 1;
|
|
|
|
}
|
2000-12-16 18:06:09 +00:00
|
|
|
if (res)
|
|
|
|
freeaddrinfo(res);
|
2002-06-24 20:57:14 +00:00
|
|
|
if (good == 0) {
|
|
|
|
asprintf(&syserr, "address for remote host (%s) not matched",
|
|
|
|
from_ip);
|
|
|
|
asprintf(&usererr,
|
2001-06-25 01:45:25 +00:00
|
|
|
"address for your hostname (%s) not matched", from_ip);
|
2002-06-24 20:57:14 +00:00
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1996-10-27 03:06:52 +00:00
|
|
|
|
2001-06-25 01:45:25 +00:00
|
|
|
fpass = 1;
|
1996-05-05 14:04:33 +00:00
|
|
|
hostf = fopen(_PATH_HOSTSEQUIV, "r");
|
|
|
|
again:
|
|
|
|
if (hostf) {
|
2000-12-16 18:06:09 +00:00
|
|
|
if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
|
1996-05-05 14:04:33 +00:00
|
|
|
(void) fclose(hostf);
|
2001-06-25 01:45:25 +00:00
|
|
|
goto foundhost;
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
|
|
|
(void) fclose(hostf);
|
|
|
|
}
|
2001-06-25 01:45:25 +00:00
|
|
|
if (fpass == 1) {
|
|
|
|
fpass = 2;
|
1996-05-05 14:04:33 +00:00
|
|
|
hostf = fopen(_PATH_HOSTSLPD, "r");
|
|
|
|
goto again;
|
|
|
|
}
|
2002-06-24 20:57:14 +00:00
|
|
|
/* This syslog message already includes from_host */
|
|
|
|
ch_opts &= ~LPD_ADDFROMLINE;
|
|
|
|
asprintf(&syserr, "refused connection from %s, sip=%s", from_host,
|
2001-06-25 01:45:25 +00:00
|
|
|
from_ip);
|
2002-06-24 20:57:14 +00:00
|
|
|
asprintf(&usererr,
|
|
|
|
"Print-services are not available to your host (%s).", from_host);
|
|
|
|
fhosterr(ch_opts, syserr, usererr);
|
|
|
|
/* NOTREACHED */
|
2001-06-25 01:45:25 +00:00
|
|
|
|
|
|
|
foundhost:
|
|
|
|
if (ch_opts & LPD_NOPORTCHK)
|
|
|
|
return; /* skip the reserved-port check */
|
|
|
|
|
|
|
|
error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
|
|
|
|
NI_NUMERICSERV);
|
2002-06-24 20:57:14 +00:00
|
|
|
if (error) {
|
|
|
|
/* same message to both syslog and remote user */
|
|
|
|
asprintf(&syserr, "malformed from-address (%d)", error);
|
|
|
|
fhosterr(ch_opts, syserr, syserr);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-06-25 01:45:25 +00:00
|
|
|
|
2002-06-24 20:57:14 +00:00
|
|
|
if (atoi(serv) >= IPPORT_RESERVED) {
|
|
|
|
/* same message to both syslog and remote user */
|
|
|
|
asprintf(&syserr, "connected from invalid port (%s)", serv);
|
|
|
|
fhosterr(ch_opts, syserr, syserr);
|
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
2001-06-25 01:45:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-06-24 20:57:14 +00:00
|
|
|
* Handle fatal errors in chkhost. The first message will optionally be
|
|
|
|
* sent to syslog, the second one is sent to the connecting host.
|
2001-06-25 01:45:25 +00:00
|
|
|
*
|
|
|
|
* The idea is that the syslog message is meant for an administrator of a
|
|
|
|
* print server (the host receiving connections), while the usermsg is meant
|
|
|
|
* for a remote user who may or may not be clueful, and may or may not be
|
|
|
|
* doing something nefarious. Some remote users (eg, MS-Windows...) may not
|
|
|
|
* even see whatever message is sent, which is why there's the option to
|
|
|
|
* start 'lpd' with the connection-errors also sent to syslog.
|
|
|
|
*
|
|
|
|
* Given that hostnames can theoretically be fairly long (well, over 250
|
|
|
|
* bytes), it would probably be helpful to have the 'from_host' field at
|
|
|
|
* the end of any error messages which include that info.
|
2002-06-24 20:57:14 +00:00
|
|
|
*
|
|
|
|
* These are Fatal host-connection errors, so this routine does not return.
|
2001-06-25 01:45:25 +00:00
|
|
|
*/
|
2002-06-24 20:57:14 +00:00
|
|
|
static void
|
|
|
|
fhosterr(int ch_opts, char *sysmsg, char *usermsg)
|
2001-06-25 01:45:25 +00:00
|
|
|
{
|
2002-06-24 20:57:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If lpd was started up to print connection errors, then write
|
|
|
|
* the syslog message before the user message.
|
|
|
|
* And for many of the syslog messages, it is helpful to first
|
|
|
|
* write the from_host (if it is known) as a separate syslog
|
|
|
|
* message, since the hostname may be so long.
|
|
|
|
*/
|
|
|
|
if (ch_opts & LPD_LOGCONNERR) {
|
|
|
|
if (ch_opts & LPD_ADDFROMLINE) {
|
2001-06-25 01:45:25 +00:00
|
|
|
syslog(LOG_WARNING, "for connection from %s:", from_host);
|
|
|
|
}
|
2002-06-24 20:57:14 +00:00
|
|
|
syslog(LOG_WARNING, "%s", sysmsg);
|
2001-06-25 01:45:25 +00:00
|
|
|
}
|
|
|
|
|
2002-06-24 20:57:14 +00:00
|
|
|
/*
|
|
|
|
* Now send the error message to the remote host which is trying
|
|
|
|
* to make the connection.
|
|
|
|
*/
|
|
|
|
printf("%s [@%s]: %s\n", progname, local_host, usermsg);
|
2001-06-25 01:45:25 +00:00
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a minimal delay before exiting (and disconnecting from the
|
|
|
|
* sending-host). This is just in case that machine responds by
|
|
|
|
* INSTANTLY retrying (and instantly re-failing...). This may also
|
|
|
|
* give the other side more time to read the error message.
|
|
|
|
*/
|
|
|
|
sleep(2); /* a paranoid throttling measure */
|
|
|
|
exit(1);
|
1996-05-05 14:04:33 +00:00
|
|
|
}
|
1997-08-23 15:53:00 +00:00
|
|
|
|
2000-12-16 18:06:09 +00:00
|
|
|
/* setup server socket for specified address family */
|
|
|
|
/* if af is PF_UNSPEC more than one socket may be returned */
|
|
|
|
/* the returned list is dynamically allocated, so caller needs to free it */
|
|
|
|
static int *
|
2001-06-15 22:30:02 +00:00
|
|
|
socksetup(int af, int debuglvl)
|
2000-12-16 18:06:09 +00:00
|
|
|
{
|
|
|
|
struct addrinfo hints, *res, *r;
|
|
|
|
int error, maxs, *s, *socks;
|
|
|
|
const int on = 1;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
hints.ai_family = af;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
error = getaddrinfo(NULL, "printer", &hints, &res);
|
|
|
|
if (error) {
|
|
|
|
syslog(LOG_ERR, "%s", gai_strerror(error));
|
|
|
|
mcleanup(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Count max number of sockets we may open */
|
|
|
|
for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
|
|
|
|
;
|
|
|
|
socks = malloc((maxs + 1) * sizeof(int));
|
|
|
|
if (!socks) {
|
|
|
|
syslog(LOG_ERR, "couldn't allocate memory for sockets");
|
|
|
|
mcleanup(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
*socks = 0; /* num of sockets counter at start of array */
|
|
|
|
s = socks + 1;
|
|
|
|
for (r = res; r; r = r->ai_next) {
|
|
|
|
*s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
|
|
|
|
if (*s < 0) {
|
|
|
|
syslog(LOG_DEBUG, "socket(): %m");
|
|
|
|
continue;
|
|
|
|
}
|
2001-06-15 22:30:02 +00:00
|
|
|
if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
|
|
|
|
< 0) {
|
|
|
|
syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
|
|
|
|
close(*s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (debuglvl)
|
|
|
|
if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
|
|
|
|
sizeof(debuglvl)) < 0) {
|
2000-12-16 18:06:09 +00:00
|
|
|
syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
|
|
|
|
close(*s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (r->ai_family == AF_INET6) {
|
2002-07-22 18:16:05 +00:00
|
|
|
if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
|
2000-12-16 18:06:09 +00:00
|
|
|
&on, sizeof(on)) < 0) {
|
|
|
|
syslog(LOG_ERR,
|
2002-07-22 18:16:05 +00:00
|
|
|
"setsockopt (IPV6_V6ONLY): %m");
|
2000-12-16 18:06:09 +00:00
|
|
|
close(*s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
|
|
|
|
syslog(LOG_DEBUG, "bind(): %m");
|
|
|
|
close(*s);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(*socks)++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
if (*socks == 0) {
|
|
|
|
syslog(LOG_ERR, "Couldn't bind to any socket");
|
|
|
|
free(socks);
|
|
|
|
mcleanup(0);
|
|
|
|
}
|
|
|
|
return(socks);
|
|
|
|
}
|
2001-03-12 02:03:53 +00:00
|
|
|
|
|
|
|
static void
|
2001-06-12 16:38:20 +00:00
|
|
|
usage(void)
|
2001-03-12 02:03:53 +00:00
|
|
|
{
|
|
|
|
#ifdef INET6
|
2021-04-03 17:51:56 +00:00
|
|
|
fprintf(stderr, "usage: lpd [-cdlsFW46] [port#]\n");
|
2001-03-12 02:03:53 +00:00
|
|
|
#else
|
2021-04-03 17:51:56 +00:00
|
|
|
fprintf(stderr, "usage: lpd [-cdlsFW] [port#]\n");
|
2001-03-12 02:03:53 +00:00
|
|
|
#endif
|
|
|
|
exit(EX_USAGE);
|
|
|
|
}
|