18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
40b561052SJoerg Wunsch * Copyright (c) 1983, 1993, 1994
50b561052SJoerg Wunsch * The Regents of the University of California. All rights reserved.
60b561052SJoerg Wunsch *
70b561052SJoerg Wunsch *
80b561052SJoerg Wunsch * Redistribution and use in source and binary forms, with or without
90b561052SJoerg Wunsch * modification, are permitted provided that the following conditions
100b561052SJoerg Wunsch * are met:
110b561052SJoerg Wunsch * 1. Redistributions of source code must retain the above copyright
120b561052SJoerg Wunsch * notice, this list of conditions and the following disclaimer.
130b561052SJoerg Wunsch * 2. Redistributions in binary form must reproduce the above copyright
140b561052SJoerg Wunsch * notice, this list of conditions and the following disclaimer in the
150b561052SJoerg Wunsch * documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
170b561052SJoerg Wunsch * may be used to endorse or promote products derived from this software
180b561052SJoerg Wunsch * without specific prior written permission.
190b561052SJoerg Wunsch *
200b561052SJoerg Wunsch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
210b561052SJoerg Wunsch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220b561052SJoerg Wunsch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
230b561052SJoerg Wunsch * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
240b561052SJoerg Wunsch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
250b561052SJoerg Wunsch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
260b561052SJoerg Wunsch * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
270b561052SJoerg Wunsch * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
280b561052SJoerg Wunsch * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
290b561052SJoerg Wunsch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
300b561052SJoerg Wunsch * SUCH DAMAGE.
310b561052SJoerg Wunsch */
320b561052SJoerg Wunsch
33055c9045SGarance A Drosehn #include "lp.cdefs.h" /* A cross-platform version of <sys/cdefs.h> */
340b561052SJoerg Wunsch /*
350b561052SJoerg Wunsch * lpd -- line printer daemon.
360b561052SJoerg Wunsch *
370b561052SJoerg Wunsch * Listen for a connection and perform the requested operation.
380b561052SJoerg Wunsch * Operations are:
390b561052SJoerg Wunsch * \1printer\n
400b561052SJoerg Wunsch * check the queue for jobs and print any found.
410b561052SJoerg Wunsch * \2printer\n
420b561052SJoerg Wunsch * receive a job from another machine and queue it.
430b561052SJoerg Wunsch * \3printer [users ...] [jobs ...]\n
440b561052SJoerg Wunsch * return the current state of the queue (short form).
450b561052SJoerg Wunsch * \4printer [users ...] [jobs ...]\n
460b561052SJoerg Wunsch * return the current state of the queue (long form).
470b561052SJoerg Wunsch * \5printer person [users ...] [jobs ...]\n
480b561052SJoerg Wunsch * remove jobs from the queue.
490b561052SJoerg Wunsch *
500b561052SJoerg Wunsch * Strategy to maintain protected spooling area:
510b561052SJoerg Wunsch * 1. Spooling area is writable only by daemon and spooling group
520b561052SJoerg Wunsch * 2. lpr runs setuid root and setgrp spooling group; it uses
530b561052SJoerg Wunsch * root to access any file it wants (verifying things before
540b561052SJoerg Wunsch * with an access call) and group id to know how it should
550b561052SJoerg Wunsch * set up ownership of files in the spooling area.
560b561052SJoerg Wunsch * 3. Files in spooling area are owned by root, group spooling
570b561052SJoerg Wunsch * group, with mode 660.
580b561052SJoerg Wunsch * 4. lpd, lpq and lprm run setuid daemon and setgrp spooling group to
590b561052SJoerg Wunsch * access files and printer. Users can't get to anything
600b561052SJoerg Wunsch * w/o help of lpq and lprm programs.
610b561052SJoerg Wunsch */
620b561052SJoerg Wunsch
630b561052SJoerg Wunsch #include <sys/param.h>
640b561052SJoerg Wunsch #include <sys/wait.h>
650b561052SJoerg Wunsch #include <sys/types.h>
660b561052SJoerg Wunsch #include <sys/socket.h>
670b561052SJoerg Wunsch #include <sys/un.h>
680b561052SJoerg Wunsch #include <sys/stat.h>
690b561052SJoerg Wunsch #include <sys/file.h>
700b561052SJoerg Wunsch #include <netinet/in.h>
717f72cbbaSJoerg Wunsch #include <arpa/inet.h>
720b561052SJoerg Wunsch
730b561052SJoerg Wunsch #include <netdb.h>
740b561052SJoerg Wunsch #include <unistd.h>
750b561052SJoerg Wunsch #include <syslog.h>
760b561052SJoerg Wunsch #include <signal.h>
7736d0e2a3SJoerg Wunsch #include <err.h>
780b561052SJoerg Wunsch #include <errno.h>
790b561052SJoerg Wunsch #include <fcntl.h>
800b561052SJoerg Wunsch #include <dirent.h>
810b561052SJoerg Wunsch #include <stdio.h>
820b561052SJoerg Wunsch #include <stdlib.h>
830b561052SJoerg Wunsch #include <string.h>
8436d0e2a3SJoerg Wunsch #include <sysexits.h>
850b561052SJoerg Wunsch #include <ctype.h>
860b561052SJoerg Wunsch #include "lp.h"
870b561052SJoerg Wunsch #include "lp.local.h"
880b561052SJoerg Wunsch #include "pathnames.h"
890b561052SJoerg Wunsch #include "extern.h"
900b561052SJoerg Wunsch
910b561052SJoerg Wunsch int lflag; /* log requests flag */
92c7e56d32SGarance A Drosehn int sflag; /* no incoming port flag */
933c636606SChris Rees int Fflag; /* run in foreground flag */
940b561052SJoerg Wunsch int from_remote; /* from remote socket */
950b561052SJoerg Wunsch
96ba7a1ad7SGarance A Drosehn int main(int argc, char **_argv);
97ba7a1ad7SGarance A Drosehn static void reapchild(int _signo);
98ba7a1ad7SGarance A Drosehn static void mcleanup(int _signo);
99ba7a1ad7SGarance A Drosehn static void doit(void);
100ba7a1ad7SGarance A Drosehn static void startup(void);
1016ddb63caSGarance A Drosehn static void chkhost(struct sockaddr *_f, int _ch_opts);
102ba7a1ad7SGarance A Drosehn static int ckqueue(struct printer *_pp);
103bd1d08a1SGarance A Drosehn static void fhosterr(int _ch_opts, char *_sysmsg, char *_usermsg);
104aa4ad562SGarance A Drosehn static int *socksetup(int _af, int _debuglvl);
105ba7a1ad7SGarance A Drosehn static void usage(void);
10608829865SHajimu UMEMOTO
10708829865SHajimu UMEMOTO /* XXX from libc/net/rcmd.c */
108784bddbcSKevin Lo extern int __ivaliduser_sa(FILE *, struct sockaddr *, socklen_t,
109784bddbcSKevin Lo const char *, const char *);
1100b561052SJoerg Wunsch
111360d4ad5SWarner Losh uid_t uid, euid;
112360d4ad5SWarner Losh
1136ddb63caSGarance A Drosehn #define LPD_NOPORTCHK 0001 /* skip reserved-port check */
1146ddb63caSGarance A Drosehn #define LPD_LOGCONNERR 0002 /* (sys)log connection errors */
115bd1d08a1SGarance A Drosehn #define LPD_ADDFROMLINE 0004 /* just used for fhosterr() */
1166ddb63caSGarance A Drosehn
1170b561052SJoerg Wunsch int
main(int argc,char ** argv)118ba7a1ad7SGarance A Drosehn main(int argc, char **argv)
1190b561052SJoerg Wunsch {
120e95b3b9bSGarance A Drosehn int ch_options, errs, f, funix, *finet, i, lfd, socket_debug;
1210b561052SJoerg Wunsch fd_set defreadfds;
1220b561052SJoerg Wunsch struct sockaddr_un un, fromunix;
12308829865SHajimu UMEMOTO struct sockaddr_storage frominet;
124e95b3b9bSGarance A Drosehn socklen_t fromlen;
1254a1a0dbeSGarrett Wollman sigset_t omask, nmask;
12636d0e2a3SJoerg Wunsch struct servent *sp, serv;
12708829865SHajimu UMEMOTO int inet_flag = 0, inet6_flag = 0;
1280b561052SJoerg Wunsch
129360d4ad5SWarner Losh euid = geteuid(); /* these shouldn't be different */
130360d4ad5SWarner Losh uid = getuid();
1316ddb63caSGarance A Drosehn
1326ddb63caSGarance A Drosehn ch_options = 0;
133f6a3be39SGarance A Drosehn socket_debug = 0;
134cc3fd56fSGarance A Drosehn gethostname(local_host, sizeof(local_host));
135360d4ad5SWarner Losh
13631058a75SGarance A Drosehn progname = "lpd";
137360d4ad5SWarner Losh
13836d0e2a3SJoerg Wunsch if (euid != 0)
13936d0e2a3SJoerg Wunsch errx(EX_NOPERM,"must run as root");
1400b561052SJoerg Wunsch
14136d0e2a3SJoerg Wunsch errs = 0;
1423c636606SChris Rees while ((i = getopt(argc, argv, "cdlpswFW46")) != -1)
14336d0e2a3SJoerg Wunsch switch (i) {
1446ddb63caSGarance A Drosehn case 'c':
1456ddb63caSGarance A Drosehn /* log all kinds of connection-errors to syslog */
1466ddb63caSGarance A Drosehn ch_options |= LPD_LOGCONNERR;
1476ddb63caSGarance A Drosehn break;
1480b561052SJoerg Wunsch case 'd':
149f6a3be39SGarance A Drosehn socket_debug++;
1500b561052SJoerg Wunsch break;
1510b561052SJoerg Wunsch case 'l':
1520b561052SJoerg Wunsch lflag++;
1530b561052SJoerg Wunsch break;
154c7e56d32SGarance A Drosehn case 'p': /* letter initially used for -s */
155c7e56d32SGarance A Drosehn /*
156c7e56d32SGarance A Drosehn * This will probably be removed with 5.0-release.
157c7e56d32SGarance A Drosehn */
158c7e56d32SGarance A Drosehn /* FALLTHROUGH */
159c7e56d32SGarance A Drosehn case 's': /* secure (no inet) */
160c7e56d32SGarance A Drosehn sflag++;
161ba901a11SSheldon Hearn break;
162605d466bSGarance A Drosehn case 'w': /* netbsd uses -w for maxwait */
163605d466bSGarance A Drosehn /*
164605d466bSGarance A Drosehn * This will be removed after the release of 4.4, as
165605d466bSGarance A Drosehn * it conflicts with -w in netbsd's lpd. For now it
166605d466bSGarance A Drosehn * is just a warning, so we won't suddenly break lpd
167605d466bSGarance A Drosehn * for anyone who is currently using the option.
168605d466bSGarance A Drosehn */
169605d466bSGarance A Drosehn syslog(LOG_WARNING,
170605d466bSGarance A Drosehn "NOTE: the -w option has been renamed -W");
171605d466bSGarance A Drosehn syslog(LOG_WARNING,
172605d466bSGarance A Drosehn "NOTE: please change your lpd config to use -W");
173605d466bSGarance A Drosehn /* FALLTHROUGH */
1743c636606SChris Rees case 'F':
1753c636606SChris Rees Fflag++;
1763c636606SChris Rees break;
177605d466bSGarance A Drosehn case 'W':
1786ddb63caSGarance A Drosehn /* allow connections coming from a non-reserved port */
1796ddb63caSGarance A Drosehn /* (done by some lpr-implementations for MS-Windows) */
1806ddb63caSGarance A Drosehn ch_options |= LPD_NOPORTCHK;
1816ddb63caSGarance A Drosehn break;
18208829865SHajimu UMEMOTO case '4':
18308829865SHajimu UMEMOTO family = PF_INET;
18408829865SHajimu UMEMOTO inet_flag++;
18508829865SHajimu UMEMOTO break;
18608829865SHajimu UMEMOTO case '6':
187affa0039SGarance A Drosehn #ifdef INET6
18808829865SHajimu UMEMOTO family = PF_INET6;
18908829865SHajimu UMEMOTO inet6_flag++;
190affa0039SGarance A Drosehn #else
191affa0039SGarance A Drosehn errx(EX_USAGE, "lpd compiled sans INET6 (IPv6 support)");
192affa0039SGarance A Drosehn #endif
19308829865SHajimu UMEMOTO break;
194605d466bSGarance A Drosehn /*
195605d466bSGarance A Drosehn * The following options are not in FreeBSD (yet?), but are
196605d466bSGarance A Drosehn * listed here to "reserve" them, because the option-letters
197605d466bSGarance A Drosehn * are used by either NetBSD or OpenBSD (as of July 2001).
198605d466bSGarance A Drosehn */
199605d466bSGarance A Drosehn case 'b': /* set bind-addr */
200605d466bSGarance A Drosehn case 'n': /* set max num of children */
201605d466bSGarance A Drosehn case 'r': /* allow 'of' for remote ptrs */
202605d466bSGarance A Drosehn /* ...[not needed in freebsd] */
203605d466bSGarance A Drosehn /* FALLTHROUGH */
20436d0e2a3SJoerg Wunsch default:
20536d0e2a3SJoerg Wunsch errs++;
2060b561052SJoerg Wunsch }
20708829865SHajimu UMEMOTO if (inet_flag && inet6_flag)
20808829865SHajimu UMEMOTO family = PF_UNSPEC;
20936d0e2a3SJoerg Wunsch argc -= optind;
21036d0e2a3SJoerg Wunsch argv += optind;
21136d0e2a3SJoerg Wunsch if (errs)
21236d0e2a3SJoerg Wunsch usage();
21336d0e2a3SJoerg Wunsch
21436d0e2a3SJoerg Wunsch if (argc == 1) {
21536d0e2a3SJoerg Wunsch if ((i = atoi(argv[0])) == 0)
21636d0e2a3SJoerg Wunsch usage();
21736d0e2a3SJoerg Wunsch if (i < 0 || i > USHRT_MAX)
21836d0e2a3SJoerg Wunsch errx(EX_USAGE, "port # %d is invalid", i);
21936d0e2a3SJoerg Wunsch
22036d0e2a3SJoerg Wunsch serv.s_port = htons(i);
22136d0e2a3SJoerg Wunsch sp = &serv;
22236d0e2a3SJoerg Wunsch argc--;
22336d0e2a3SJoerg Wunsch } else {
22436d0e2a3SJoerg Wunsch sp = getservbyname("printer", "tcp");
22536d0e2a3SJoerg Wunsch if (sp == NULL)
22636d0e2a3SJoerg Wunsch errx(EX_OSFILE, "printer/tcp: unknown service");
2270b561052SJoerg Wunsch }
2280b561052SJoerg Wunsch
22936d0e2a3SJoerg Wunsch if (argc != 0)
23036d0e2a3SJoerg Wunsch usage();
23136d0e2a3SJoerg Wunsch
2324a1a0dbeSGarrett Wollman /*
2334a1a0dbeSGarrett Wollman * We run chkprintcap right away to catch any errors and blat them
2344a1a0dbeSGarrett Wollman * to stderr while we still have it open, rather than sending them
2354a1a0dbeSGarrett Wollman * to syslog and leaving the user wondering why lpd started and
2364a1a0dbeSGarrett Wollman * then stopped. There should probably be a command-line flag to
2374a1a0dbeSGarrett Wollman * ignore errors from chkprintcap.
2384a1a0dbeSGarrett Wollman */
2394a1a0dbeSGarrett Wollman {
2404a1a0dbeSGarrett Wollman pid_t pid;
2414a1a0dbeSGarrett Wollman int status;
2424a1a0dbeSGarrett Wollman pid = fork();
2434a1a0dbeSGarrett Wollman if (pid < 0) {
2444a1a0dbeSGarrett Wollman err(EX_OSERR, "cannot fork");
2454a1a0dbeSGarrett Wollman } else if (pid == 0) { /* child */
2464a1a0dbeSGarrett Wollman execl(_PATH_CHKPRINTCAP, _PATH_CHKPRINTCAP, (char *)0);
2474a1a0dbeSGarrett Wollman err(EX_OSERR, "cannot execute %s", _PATH_CHKPRINTCAP);
2484a1a0dbeSGarrett Wollman }
2494a1a0dbeSGarrett Wollman if (waitpid(pid, &status, 0) < 0) {
2504a1a0dbeSGarrett Wollman err(EX_OSERR, "cannot wait");
2514a1a0dbeSGarrett Wollman }
2524a1a0dbeSGarrett Wollman if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
2534a1a0dbeSGarrett Wollman errx(EX_OSFILE, "%d errors in printcap file, exiting",
2544a1a0dbeSGarrett Wollman WEXITSTATUS(status));
2554a1a0dbeSGarrett Wollman }
2564a1a0dbeSGarrett Wollman
2573c636606SChris Rees #ifdef DEBUG
2583c636606SChris Rees Fflag++;
2590b561052SJoerg Wunsch #endif
2603c636606SChris Rees /*
2613c636606SChris Rees * Set up standard environment by detaching from the parent
2623c636606SChris Rees * if -F not specified
2633c636606SChris Rees */
2643c636606SChris Rees if (Fflag == 0) {
2653c636606SChris Rees daemon(0, 0);
2663c636606SChris Rees }
2670b561052SJoerg Wunsch
2680b561052SJoerg Wunsch openlog("lpd", LOG_PID, LOG_LPR);
269c7e56d32SGarance A Drosehn syslog(LOG_INFO, "lpd startup: logging=%d%s%s", lflag,
270c7e56d32SGarance A Drosehn socket_debug ? " dbg" : "", sflag ? " net-secure" : "");
2710b561052SJoerg Wunsch (void) umask(0);
2724a1a0dbeSGarrett Wollman /*
2734a1a0dbeSGarrett Wollman * NB: This depends on O_NONBLOCK semantics doing the right thing;
2744a1a0dbeSGarrett Wollman * i.e., applying only to the O_EXLOCK and not to the rest of the
2754a1a0dbeSGarrett Wollman * open/creation. As of 1997-12-02, this is the case for commonly-
2764a1a0dbeSGarrett Wollman * used filesystems. There are other places in this code which
2774a1a0dbeSGarrett Wollman * make the same assumption.
2784a1a0dbeSGarrett Wollman */
2794a1a0dbeSGarrett Wollman lfd = open(_PATH_MASTERLOCK, O_WRONLY|O_CREAT|O_EXLOCK|O_NONBLOCK,
2804a1a0dbeSGarrett Wollman LOCK_FILE_MODE);
2810b561052SJoerg Wunsch if (lfd < 0) {
282f6d56683SGarance A Drosehn if (errno == EWOULDBLOCK) /* active daemon present */
2830b561052SJoerg Wunsch exit(0);
2840b561052SJoerg Wunsch syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
2850b561052SJoerg Wunsch exit(1);
2860b561052SJoerg Wunsch }
2874a1a0dbeSGarrett Wollman fcntl(lfd, F_SETFL, 0); /* turn off non-blocking mode */
2880b561052SJoerg Wunsch ftruncate(lfd, 0);
2890b561052SJoerg Wunsch /*
2900b561052SJoerg Wunsch * write process id for others to know
2910b561052SJoerg Wunsch */
2920b561052SJoerg Wunsch sprintf(line, "%u\n", getpid());
2930b561052SJoerg Wunsch f = strlen(line);
2940b561052SJoerg Wunsch if (write(lfd, line, f) != f) {
2950b561052SJoerg Wunsch syslog(LOG_ERR, "%s: %m", _PATH_MASTERLOCK);
2960b561052SJoerg Wunsch exit(1);
2970b561052SJoerg Wunsch }
2980b561052SJoerg Wunsch signal(SIGCHLD, reapchild);
2990b561052SJoerg Wunsch /*
3000b561052SJoerg Wunsch * Restart all the printers.
3010b561052SJoerg Wunsch */
3020b561052SJoerg Wunsch startup();
3030b561052SJoerg Wunsch (void) unlink(_PATH_SOCKETNAME);
3040b561052SJoerg Wunsch funix = socket(AF_UNIX, SOCK_STREAM, 0);
3050b561052SJoerg Wunsch if (funix < 0) {
3060b561052SJoerg Wunsch syslog(LOG_ERR, "socket: %m");
3070b561052SJoerg Wunsch exit(1);
3080b561052SJoerg Wunsch }
3094a1a0dbeSGarrett Wollman
3104a1a0dbeSGarrett Wollman sigemptyset(&nmask);
3114a1a0dbeSGarrett Wollman sigaddset(&nmask, SIGHUP);
3124a1a0dbeSGarrett Wollman sigaddset(&nmask, SIGINT);
3134a1a0dbeSGarrett Wollman sigaddset(&nmask, SIGQUIT);
3144a1a0dbeSGarrett Wollman sigaddset(&nmask, SIGTERM);
3154a1a0dbeSGarrett Wollman sigprocmask(SIG_BLOCK, &nmask, &omask);
3164a1a0dbeSGarrett Wollman
317*6cde8f3eSStanislav Shalunov (void) umask(077);
3180b561052SJoerg Wunsch signal(SIGHUP, mcleanup);
3190b561052SJoerg Wunsch signal(SIGINT, mcleanup);
3200b561052SJoerg Wunsch signal(SIGQUIT, mcleanup);
3210b561052SJoerg Wunsch signal(SIGTERM, mcleanup);
3220b561052SJoerg Wunsch memset(&un, 0, sizeof(un));
3230b561052SJoerg Wunsch un.sun_family = AF_UNIX;
3240b561052SJoerg Wunsch strcpy(un.sun_path, _PATH_SOCKETNAME);
3250b561052SJoerg Wunsch #ifndef SUN_LEN
3260b561052SJoerg Wunsch #define SUN_LEN(unp) (strlen((unp)->sun_path) + 2)
3270b561052SJoerg Wunsch #endif
3280b561052SJoerg Wunsch if (bind(funix, (struct sockaddr *)&un, SUN_LEN(&un)) < 0) {
3290b561052SJoerg Wunsch syslog(LOG_ERR, "ubind: %m");
3300b561052SJoerg Wunsch exit(1);
3310b561052SJoerg Wunsch }
332bc407914SWarner Losh (void) umask(0);
3334a1a0dbeSGarrett Wollman sigprocmask(SIG_SETMASK, &omask, (sigset_t *)0);
3340b561052SJoerg Wunsch FD_ZERO(&defreadfds);
3350b561052SJoerg Wunsch FD_SET(funix, &defreadfds);
3360b561052SJoerg Wunsch listen(funix, 5);
337c7e56d32SGarance A Drosehn if (sflag == 0) {
338aa4ad562SGarance A Drosehn finet = socksetup(family, socket_debug);
33908829865SHajimu UMEMOTO } else
34008829865SHajimu UMEMOTO finet = NULL; /* pretend we couldn't open TCP socket. */
34108829865SHajimu UMEMOTO if (finet) {
34208829865SHajimu UMEMOTO for (i = 1; i <= *finet; i++) {
34308829865SHajimu UMEMOTO FD_SET(finet[i], &defreadfds);
34408829865SHajimu UMEMOTO listen(finet[i], 5);
3450b561052SJoerg Wunsch }
346ba901a11SSheldon Hearn }
3470b561052SJoerg Wunsch /*
3480b561052SJoerg Wunsch * Main loop: accept, do a request, continue.
3490b561052SJoerg Wunsch */
3500b561052SJoerg Wunsch memset(&frominet, 0, sizeof(frominet));
3510b561052SJoerg Wunsch memset(&fromunix, 0, sizeof(fromunix));
352f6a3be39SGarance A Drosehn if (lflag)
353f6a3be39SGarance A Drosehn syslog(LOG_INFO, "lpd startup: ready to accept requests");
3544a1a0dbeSGarrett Wollman /*
3554a1a0dbeSGarrett Wollman * XXX - should be redone for multi-protocol
3564a1a0dbeSGarrett Wollman */
3570b561052SJoerg Wunsch for (;;) {
358aa4ad562SGarance A Drosehn int domain, nfds, s;
3590b561052SJoerg Wunsch fd_set readfds;
3600b561052SJoerg Wunsch
3610b561052SJoerg Wunsch FD_COPY(&defreadfds, &readfds);
3620b561052SJoerg Wunsch nfds = select(20, &readfds, 0, 0, 0);
3630b561052SJoerg Wunsch if (nfds <= 0) {
3640b561052SJoerg Wunsch if (nfds < 0 && errno != EINTR)
3650b561052SJoerg Wunsch syslog(LOG_WARNING, "select: %m");
3660b561052SJoerg Wunsch continue;
3670b561052SJoerg Wunsch }
368aa4ad562SGarance A Drosehn domain = -1; /* avoid compile-time warning */
369aa4ad562SGarance A Drosehn s = -1; /* avoid compile-time warning */
3700b561052SJoerg Wunsch if (FD_ISSET(funix, &readfds)) {
3710b561052SJoerg Wunsch domain = AF_UNIX, fromlen = sizeof(fromunix);
3720b561052SJoerg Wunsch s = accept(funix,
3730b561052SJoerg Wunsch (struct sockaddr *)&fromunix, &fromlen);
37408829865SHajimu UMEMOTO } else {
37508829865SHajimu UMEMOTO for (i = 1; i <= *finet; i++)
37608829865SHajimu UMEMOTO if (FD_ISSET(finet[i], &readfds)) {
37708829865SHajimu UMEMOTO domain = AF_INET;
37808829865SHajimu UMEMOTO fromlen = sizeof(frominet);
37908829865SHajimu UMEMOTO s = accept(finet[i],
38008829865SHajimu UMEMOTO (struct sockaddr *)&frominet,
38108829865SHajimu UMEMOTO &fromlen);
382bc407914SWarner Losh }
3830b561052SJoerg Wunsch }
3840b561052SJoerg Wunsch if (s < 0) {
3850b561052SJoerg Wunsch if (errno != EINTR)
3860b561052SJoerg Wunsch syslog(LOG_WARNING, "accept: %m");
3870b561052SJoerg Wunsch continue;
3880b561052SJoerg Wunsch }
3890b561052SJoerg Wunsch if (fork() == 0) {
390bfb9fa63SGarance A Drosehn /*
391bfb9fa63SGarance A Drosehn * Note that printjob() also plays around with
392bfb9fa63SGarance A Drosehn * signal-handling routines, and may need to be
393bfb9fa63SGarance A Drosehn * changed when making changes to signal-handling.
394bfb9fa63SGarance A Drosehn */
395bfb9fa63SGarance A Drosehn signal(SIGCHLD, SIG_DFL);
3960b561052SJoerg Wunsch signal(SIGHUP, SIG_IGN);
3970b561052SJoerg Wunsch signal(SIGINT, SIG_IGN);
3980b561052SJoerg Wunsch signal(SIGQUIT, SIG_IGN);
3990b561052SJoerg Wunsch signal(SIGTERM, SIG_IGN);
4000b561052SJoerg Wunsch (void) close(funix);
401c7e56d32SGarance A Drosehn if (sflag == 0 && finet) {
40208829865SHajimu UMEMOTO for (i = 1; i <= *finet; i++)
40308829865SHajimu UMEMOTO (void)close(finet[i]);
404ba901a11SSheldon Hearn }
40553953407SGarance A Drosehn dup2(s, STDOUT_FILENO);
4060b561052SJoerg Wunsch (void) close(s);
4070b561052SJoerg Wunsch if (domain == AF_INET) {
40808829865SHajimu UMEMOTO /* for both AF_INET and AF_INET6 */
4090b561052SJoerg Wunsch from_remote = 1;
4106ddb63caSGarance A Drosehn chkhost((struct sockaddr *)&frominet,
4116ddb63caSGarance A Drosehn ch_options);
4120b561052SJoerg Wunsch } else
4130b561052SJoerg Wunsch from_remote = 0;
4140b561052SJoerg Wunsch doit();
4150b561052SJoerg Wunsch exit(0);
4160b561052SJoerg Wunsch }
4170b561052SJoerg Wunsch (void) close(s);
4180b561052SJoerg Wunsch }
4190b561052SJoerg Wunsch }
4200b561052SJoerg Wunsch
4210b561052SJoerg Wunsch static void
reapchild(int signo __unused)422ba7a1ad7SGarance A Drosehn reapchild(int signo __unused)
4230b561052SJoerg Wunsch {
42407602870SGarance A Drosehn int status;
4250b561052SJoerg Wunsch
42607602870SGarance A Drosehn while (wait3(&status, WNOHANG, 0) > 0)
4270b561052SJoerg Wunsch ;
4280b561052SJoerg Wunsch }
4290b561052SJoerg Wunsch
4300b561052SJoerg Wunsch static void
mcleanup(int signo)431ba7a1ad7SGarance A Drosehn mcleanup(int signo)
4320b561052SJoerg Wunsch {
433545ed065SGarrett Wollman /*
434545ed065SGarrett Wollman * XXX syslog(3) is not signal-safe.
435545ed065SGarrett Wollman */
436545ed065SGarrett Wollman if (lflag) {
437545ed065SGarrett Wollman if (signo)
438545ed065SGarrett Wollman syslog(LOG_INFO, "exiting on signal %d", signo);
439545ed065SGarrett Wollman else
4400b561052SJoerg Wunsch syslog(LOG_INFO, "exiting");
441545ed065SGarrett Wollman }
4420b561052SJoerg Wunsch unlink(_PATH_SOCKETNAME);
4430b561052SJoerg Wunsch exit(0);
4440b561052SJoerg Wunsch }
4450b561052SJoerg Wunsch
4460b561052SJoerg Wunsch /*
4470b561052SJoerg Wunsch * Stuff for handling job specifications
4480b561052SJoerg Wunsch */
4490b561052SJoerg Wunsch char *user[MAXUSERS]; /* users to process */
4500b561052SJoerg Wunsch int users; /* # of users in user array */
4510b561052SJoerg Wunsch int requ[MAXREQUESTS]; /* job number of spool entries */
4520b561052SJoerg Wunsch int requests; /* # of spool requests */
4530b561052SJoerg Wunsch char *person; /* name of person doing lprm */
4540b561052SJoerg Wunsch
455cc3fd56fSGarance A Drosehn /* buffer to hold the client's machine-name */
456cc3fd56fSGarance A Drosehn static char frombuf[MAXHOSTNAMELEN];
4570b561052SJoerg Wunsch char cbuf[BUFSIZ]; /* command line buffer */
458ba7a1ad7SGarance A Drosehn const char *cmdnames[] = {
4590b561052SJoerg Wunsch "null",
4600b561052SJoerg Wunsch "printjob",
4610b561052SJoerg Wunsch "recvjob",
4620b561052SJoerg Wunsch "displayq short",
4630b561052SJoerg Wunsch "displayq long",
4640b561052SJoerg Wunsch "rmjob"
4650b561052SJoerg Wunsch };
4660b561052SJoerg Wunsch
4670b561052SJoerg Wunsch static void
doit(void)468ba7a1ad7SGarance A Drosehn doit(void)
4690b561052SJoerg Wunsch {
4704a1a0dbeSGarrett Wollman char *cp, *printer;
4714a1a0dbeSGarrett Wollman int n;
4724a1a0dbeSGarrett Wollman int status;
4734a1a0dbeSGarrett Wollman struct printer myprinter, *pp = &myprinter;
4744a1a0dbeSGarrett Wollman
4754a1a0dbeSGarrett Wollman init_printer(&myprinter);
4760b561052SJoerg Wunsch
4770b561052SJoerg Wunsch for (;;) {
4780b561052SJoerg Wunsch cp = cbuf;
4790b561052SJoerg Wunsch do {
4800b561052SJoerg Wunsch if (cp >= &cbuf[sizeof(cbuf) - 1])
4814a1a0dbeSGarrett Wollman fatal(0, "Command line too long");
4826897f282SGarance A Drosehn if ((n = read(STDOUT_FILENO, cp, 1)) != 1) {
4830b561052SJoerg Wunsch if (n < 0)
4844a1a0dbeSGarrett Wollman fatal(0, "Lost connection");
4850b561052SJoerg Wunsch return;
4860b561052SJoerg Wunsch }
4870b561052SJoerg Wunsch } while (*cp++ != '\n');
4880b561052SJoerg Wunsch *--cp = '\0';
4890b561052SJoerg Wunsch cp = cbuf;
4900b561052SJoerg Wunsch if (lflag) {
4910b561052SJoerg Wunsch if (*cp >= '\1' && *cp <= '\5')
4920b561052SJoerg Wunsch syslog(LOG_INFO, "%s requests %s %s",
493cc3fd56fSGarance A Drosehn from_host, cmdnames[(u_char)*cp], cp+1);
4940b561052SJoerg Wunsch else
4950b561052SJoerg Wunsch syslog(LOG_INFO, "bad request (%d) from %s",
496cc3fd56fSGarance A Drosehn *cp, from_host);
4970b561052SJoerg Wunsch }
4980b561052SJoerg Wunsch switch (*cp++) {
4994a1a0dbeSGarrett Wollman case CMD_CHECK_QUE: /* check the queue, print any jobs there */
5004a1a0dbeSGarrett Wollman startprinting(cp);
5010b561052SJoerg Wunsch break;
5024a1a0dbeSGarrett Wollman case CMD_TAKE_THIS: /* receive files to be queued */
5030b561052SJoerg Wunsch if (!from_remote) {
5040b561052SJoerg Wunsch syslog(LOG_INFO, "illegal request (%d)", *cp);
5050b561052SJoerg Wunsch exit(1);
5060b561052SJoerg Wunsch }
5074a1a0dbeSGarrett Wollman recvjob(cp);
5080b561052SJoerg Wunsch break;
5094a1a0dbeSGarrett Wollman case CMD_SHOWQ_SHORT: /* display the queue (short form) */
5104a1a0dbeSGarrett Wollman case CMD_SHOWQ_LONG: /* display the queue (long form) */
5114a1a0dbeSGarrett Wollman /* XXX - this all needs to be redone. */
5120b561052SJoerg Wunsch printer = cp;
5130b561052SJoerg Wunsch while (*cp) {
5140b561052SJoerg Wunsch if (*cp != ' ') {
5150b561052SJoerg Wunsch cp++;
5160b561052SJoerg Wunsch continue;
5170b561052SJoerg Wunsch }
5180b561052SJoerg Wunsch *cp++ = '\0';
5190b561052SJoerg Wunsch while (isspace(*cp))
5200b561052SJoerg Wunsch cp++;
5210b561052SJoerg Wunsch if (*cp == '\0')
5220b561052SJoerg Wunsch break;
5230b561052SJoerg Wunsch if (isdigit(*cp)) {
5240b561052SJoerg Wunsch if (requests >= MAXREQUESTS)
5254a1a0dbeSGarrett Wollman fatal(0, "Too many requests");
5260b561052SJoerg Wunsch requ[requests++] = atoi(cp);
5270b561052SJoerg Wunsch } else {
5280b561052SJoerg Wunsch if (users >= MAXUSERS)
5294a1a0dbeSGarrett Wollman fatal(0, "Too many users");
5300b561052SJoerg Wunsch user[users++] = cp;
5310b561052SJoerg Wunsch }
5320b561052SJoerg Wunsch }
5334a1a0dbeSGarrett Wollman status = getprintcap(printer, pp);
5344a1a0dbeSGarrett Wollman if (status < 0)
5356d39e1b7SGarance A Drosehn fatal(pp, "%s", pcaperr(status));
5364a1a0dbeSGarrett Wollman displayq(pp, cbuf[0] == CMD_SHOWQ_LONG);
5370b561052SJoerg Wunsch exit(0);
5384a1a0dbeSGarrett Wollman case CMD_RMJOB: /* remove a job from the queue */
5390b561052SJoerg Wunsch if (!from_remote) {
5400b561052SJoerg Wunsch syslog(LOG_INFO, "illegal request (%d)", *cp);
5410b561052SJoerg Wunsch exit(1);
5420b561052SJoerg Wunsch }
5430b561052SJoerg Wunsch printer = cp;
5440b561052SJoerg Wunsch while (*cp && *cp != ' ')
5450b561052SJoerg Wunsch cp++;
5460b561052SJoerg Wunsch if (!*cp)
5470b561052SJoerg Wunsch break;
5480b561052SJoerg Wunsch *cp++ = '\0';
5490b561052SJoerg Wunsch person = cp;
5500b561052SJoerg Wunsch while (*cp) {
5510b561052SJoerg Wunsch if (*cp != ' ') {
5520b561052SJoerg Wunsch cp++;
5530b561052SJoerg Wunsch continue;
5540b561052SJoerg Wunsch }
5550b561052SJoerg Wunsch *cp++ = '\0';
5560b561052SJoerg Wunsch while (isspace(*cp))
5570b561052SJoerg Wunsch cp++;
5580b561052SJoerg Wunsch if (*cp == '\0')
5590b561052SJoerg Wunsch break;
5600b561052SJoerg Wunsch if (isdigit(*cp)) {
5610b561052SJoerg Wunsch if (requests >= MAXREQUESTS)
5624a1a0dbeSGarrett Wollman fatal(0, "Too many requests");
5630b561052SJoerg Wunsch requ[requests++] = atoi(cp);
5640b561052SJoerg Wunsch } else {
5650b561052SJoerg Wunsch if (users >= MAXUSERS)
5664a1a0dbeSGarrett Wollman fatal(0, "Too many users");
5670b561052SJoerg Wunsch user[users++] = cp;
5680b561052SJoerg Wunsch }
5690b561052SJoerg Wunsch }
5704a1a0dbeSGarrett Wollman rmjob(printer);
5710b561052SJoerg Wunsch break;
5720b561052SJoerg Wunsch }
5734a1a0dbeSGarrett Wollman fatal(0, "Illegal service request");
5740b561052SJoerg Wunsch }
5750b561052SJoerg Wunsch }
5760b561052SJoerg Wunsch
5770b561052SJoerg Wunsch /*
5780b561052SJoerg Wunsch * Make a pass through the printcap database and start printing any
5790b561052SJoerg Wunsch * files left from the last time the machine went down.
5800b561052SJoerg Wunsch */
5810b561052SJoerg Wunsch static void
startup(void)582ba7a1ad7SGarance A Drosehn startup(void)
5830b561052SJoerg Wunsch {
5844a1a0dbeSGarrett Wollman int pid, status, more;
5854a1a0dbeSGarrett Wollman struct printer myprinter, *pp = &myprinter;
5860b561052SJoerg Wunsch
5874a1a0dbeSGarrett Wollman more = firstprinter(pp, &status);
5884a1a0dbeSGarrett Wollman if (status)
5894a1a0dbeSGarrett Wollman goto errloop;
5904a1a0dbeSGarrett Wollman while (more) {
5914a1a0dbeSGarrett Wollman if (ckqueue(pp) <= 0) {
5924a1a0dbeSGarrett Wollman goto next;
5930b561052SJoerg Wunsch }
5940b561052SJoerg Wunsch if (lflag)
595f6a3be39SGarance A Drosehn syslog(LOG_INFO, "lpd startup: work for %s",
596f6a3be39SGarance A Drosehn pp->printer);
5970b561052SJoerg Wunsch if ((pid = fork()) < 0) {
598f6a3be39SGarance A Drosehn syslog(LOG_WARNING, "lpd startup: cannot fork for %s",
599f6a3be39SGarance A Drosehn pp->printer);
6000b561052SJoerg Wunsch mcleanup(0);
6010b561052SJoerg Wunsch }
6024a1a0dbeSGarrett Wollman if (pid == 0) {
6034a1a0dbeSGarrett Wollman lastprinter();
6044a1a0dbeSGarrett Wollman printjob(pp);
6050b561052SJoerg Wunsch /* NOTREACHED */
6060b561052SJoerg Wunsch }
6074a1a0dbeSGarrett Wollman do {
6084a1a0dbeSGarrett Wollman next:
6094a1a0dbeSGarrett Wollman more = nextprinter(pp, &status);
6104a1a0dbeSGarrett Wollman errloop:
6114a1a0dbeSGarrett Wollman if (status)
6124a1a0dbeSGarrett Wollman syslog(LOG_WARNING,
613f6a3be39SGarance A Drosehn "lpd startup: printcap entry for %s has errors, skipping",
614685f7a38SGarance A Drosehn pp->printer ? pp->printer : "<noname?>");
6154a1a0dbeSGarrett Wollman } while (more && status);
6160b561052SJoerg Wunsch }
6170b561052SJoerg Wunsch }
6180b561052SJoerg Wunsch
6190b561052SJoerg Wunsch /*
6200b561052SJoerg Wunsch * Make sure there's some work to do before forking off a child
6210b561052SJoerg Wunsch */
6220b561052SJoerg Wunsch static int
ckqueue(struct printer * pp)623ba7a1ad7SGarance A Drosehn ckqueue(struct printer *pp)
6240b561052SJoerg Wunsch {
6250b561052SJoerg Wunsch register struct dirent *d;
6260b561052SJoerg Wunsch DIR *dirp;
6270b561052SJoerg Wunsch char *spooldir;
6280b561052SJoerg Wunsch
6294a1a0dbeSGarrett Wollman spooldir = pp->spool_dir;
6300b561052SJoerg Wunsch if ((dirp = opendir(spooldir)) == NULL)
6310b561052SJoerg Wunsch return (-1);
6320b561052SJoerg Wunsch while ((d = readdir(dirp)) != NULL) {
6330b561052SJoerg Wunsch if (d->d_name[0] != 'c' || d->d_name[1] != 'f')
6340b561052SJoerg Wunsch continue; /* daemon control files only */
6350b561052SJoerg Wunsch closedir(dirp);
6360b561052SJoerg Wunsch return (1); /* found something */
6370b561052SJoerg Wunsch }
6380b561052SJoerg Wunsch closedir(dirp);
6390b561052SJoerg Wunsch return (0);
6400b561052SJoerg Wunsch }
6410b561052SJoerg Wunsch
6420b561052SJoerg Wunsch #define DUMMY ":nobody::"
6430b561052SJoerg Wunsch
6440b561052SJoerg Wunsch /*
6456ddb63caSGarance A Drosehn * Check to see if the host connecting to this host has access to any
6466ddb63caSGarance A Drosehn * lpd services on this host.
6470b561052SJoerg Wunsch */
6480b561052SJoerg Wunsch static void
chkhost(struct sockaddr * f,int ch_opts)6496ddb63caSGarance A Drosehn chkhost(struct sockaddr *f, int ch_opts)
6500b561052SJoerg Wunsch {
65108829865SHajimu UMEMOTO struct addrinfo hints, *res, *r;
6520b561052SJoerg Wunsch register FILE *hostf;
653cc3fd56fSGarance A Drosehn char hostbuf[NI_MAXHOST], ip[NI_MAXHOST];
65408829865SHajimu UMEMOTO char serv[NI_MAXSERV];
655bd1d08a1SGarance A Drosehn char *syserr, *usererr;
656820a9567SGarance A Drosehn int error, errsav, fpass, good;
6576ddb63caSGarance A Drosehn
6586ddb63caSGarance A Drosehn from_host = ".na.";
6590b561052SJoerg Wunsch
6600b561052SJoerg Wunsch /* Need real hostname for temporary filenames */
661cc3fd56fSGarance A Drosehn error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0,
66208829865SHajimu UMEMOTO NI_NAMEREQD);
66308829865SHajimu UMEMOTO if (error) {
6646ddb63caSGarance A Drosehn errsav = error;
665cc3fd56fSGarance A Drosehn error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf),
6669ccf2f38SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST);
667bd1d08a1SGarance A Drosehn if (error) {
668bd1d08a1SGarance A Drosehn asprintf(&syserr,
669bd1d08a1SGarance A Drosehn "can not determine hostname for remote host (%d,%d)",
670bd1d08a1SGarance A Drosehn errsav, error);
671bd1d08a1SGarance A Drosehn asprintf(&usererr,
672bd1d08a1SGarance A Drosehn "Host name for your address is not known");
673bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
674bd1d08a1SGarance A Drosehn /* NOTREACHED */
675bd1d08a1SGarance A Drosehn }
676bd1d08a1SGarance A Drosehn asprintf(&syserr,
6776ddb63caSGarance A Drosehn "Host name for remote host (%s) not known (%d)",
6786ddb63caSGarance A Drosehn hostbuf, errsav);
679bd1d08a1SGarance A Drosehn asprintf(&usererr,
680bd1d08a1SGarance A Drosehn "Host name for your address (%s) is not known",
681bd1d08a1SGarance A Drosehn hostbuf);
682bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
683bd1d08a1SGarance A Drosehn /* NOTREACHED */
68408829865SHajimu UMEMOTO }
6850b561052SJoerg Wunsch
686cc3fd56fSGarance A Drosehn strlcpy(frombuf, hostbuf, sizeof(frombuf));
687cc3fd56fSGarance A Drosehn from_host = frombuf;
688bd1d08a1SGarance A Drosehn ch_opts |= LPD_ADDFROMLINE;
68908829865SHajimu UMEMOTO
69008829865SHajimu UMEMOTO /* Need address in stringform for comparison (no DNS lookup here) */
691cc3fd56fSGarance A Drosehn error = getnameinfo(f, f->sa_len, hostbuf, sizeof(hostbuf), NULL, 0,
6929ccf2f38SHajimu UMEMOTO NI_NUMERICHOST);
693bd1d08a1SGarance A Drosehn if (error) {
694bd1d08a1SGarance A Drosehn asprintf(&syserr, "Cannot print IP address (error %d)",
695bd1d08a1SGarance A Drosehn error);
696bd1d08a1SGarance A Drosehn asprintf(&usererr, "Cannot print IP address for your host");
697bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
698bd1d08a1SGarance A Drosehn /* NOTREACHED */
699bd1d08a1SGarance A Drosehn }
700cc3fd56fSGarance A Drosehn from_ip = strdup(hostbuf);
7010b561052SJoerg Wunsch
70208829865SHajimu UMEMOTO /* Reject numeric addresses */
70308829865SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
70408829865SHajimu UMEMOTO hints.ai_family = family;
70508829865SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*dummy*/
70608829865SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
707cc3fd56fSGarance A Drosehn if (getaddrinfo(from_host, NULL, &hints, &res) == 0) {
70808829865SHajimu UMEMOTO freeaddrinfo(res);
709bd1d08a1SGarance A Drosehn /* This syslog message already includes from_host */
710bd1d08a1SGarance A Drosehn ch_opts &= ~LPD_ADDFROMLINE;
711bd1d08a1SGarance A Drosehn asprintf(&syserr, "reverse lookup results in non-FQDN %s",
7126ddb63caSGarance A Drosehn from_host);
713bd1d08a1SGarance A Drosehn /* same message to both syslog and remote user */
714bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, syserr);
715bd1d08a1SGarance A Drosehn /* NOTREACHED */
71608829865SHajimu UMEMOTO }
71708829865SHajimu UMEMOTO
718bc407914SWarner Losh /* Check for spoof, ala rlogind */
71908829865SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
72008829865SHajimu UMEMOTO hints.ai_family = family;
72108829865SHajimu UMEMOTO hints.ai_socktype = SOCK_DGRAM; /*dummy*/
722cc3fd56fSGarance A Drosehn error = getaddrinfo(from_host, NULL, &hints, &res);
72308829865SHajimu UMEMOTO if (error) {
724bd1d08a1SGarance A Drosehn asprintf(&syserr, "dns lookup for address %s failed: %s",
725bd1d08a1SGarance A Drosehn from_ip, gai_strerror(error));
726bd1d08a1SGarance A Drosehn asprintf(&usererr, "hostname for your address (%s) unknown: %s",
727bd1d08a1SGarance A Drosehn from_ip, gai_strerror(error));
728bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
729bd1d08a1SGarance A Drosehn /* NOTREACHED */
73008829865SHajimu UMEMOTO }
73108829865SHajimu UMEMOTO good = 0;
73208829865SHajimu UMEMOTO for (r = res; good == 0 && r; r = r->ai_next) {
73308829865SHajimu UMEMOTO error = getnameinfo(r->ai_addr, r->ai_addrlen, ip, sizeof(ip),
7349ccf2f38SHajimu UMEMOTO NULL, 0, NI_NUMERICHOST);
73508829865SHajimu UMEMOTO if (!error && !strcmp(from_ip, ip))
736bc407914SWarner Losh good = 1;
737bc407914SWarner Losh }
73808829865SHajimu UMEMOTO if (res)
73908829865SHajimu UMEMOTO freeaddrinfo(res);
740bd1d08a1SGarance A Drosehn if (good == 0) {
741bd1d08a1SGarance A Drosehn asprintf(&syserr, "address for remote host (%s) not matched",
742bd1d08a1SGarance A Drosehn from_ip);
743bd1d08a1SGarance A Drosehn asprintf(&usererr,
7446ddb63caSGarance A Drosehn "address for your hostname (%s) not matched", from_ip);
745bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
746bd1d08a1SGarance A Drosehn /* NOTREACHED */
747bd1d08a1SGarance A Drosehn }
748bc407914SWarner Losh
7496ddb63caSGarance A Drosehn fpass = 1;
7500b561052SJoerg Wunsch hostf = fopen(_PATH_HOSTSEQUIV, "r");
7510b561052SJoerg Wunsch again:
7520b561052SJoerg Wunsch if (hostf) {
75308829865SHajimu UMEMOTO if (__ivaliduser_sa(hostf, f, f->sa_len, DUMMY, DUMMY) == 0) {
7540b561052SJoerg Wunsch (void) fclose(hostf);
7556ddb63caSGarance A Drosehn goto foundhost;
7560b561052SJoerg Wunsch }
7570b561052SJoerg Wunsch (void) fclose(hostf);
7580b561052SJoerg Wunsch }
7596ddb63caSGarance A Drosehn if (fpass == 1) {
7606ddb63caSGarance A Drosehn fpass = 2;
7610b561052SJoerg Wunsch hostf = fopen(_PATH_HOSTSLPD, "r");
7620b561052SJoerg Wunsch goto again;
7630b561052SJoerg Wunsch }
764bd1d08a1SGarance A Drosehn /* This syslog message already includes from_host */
765bd1d08a1SGarance A Drosehn ch_opts &= ~LPD_ADDFROMLINE;
766bd1d08a1SGarance A Drosehn asprintf(&syserr, "refused connection from %s, sip=%s", from_host,
7676ddb63caSGarance A Drosehn from_ip);
768bd1d08a1SGarance A Drosehn asprintf(&usererr,
769bd1d08a1SGarance A Drosehn "Print-services are not available to your host (%s).", from_host);
770bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, usererr);
7710b561052SJoerg Wunsch /* NOTREACHED */
7726ddb63caSGarance A Drosehn
7736ddb63caSGarance A Drosehn foundhost:
7746ddb63caSGarance A Drosehn if (ch_opts & LPD_NOPORTCHK)
7756ddb63caSGarance A Drosehn return; /* skip the reserved-port check */
7766ddb63caSGarance A Drosehn
7776ddb63caSGarance A Drosehn error = getnameinfo(f, f->sa_len, NULL, 0, serv, sizeof(serv),
7786ddb63caSGarance A Drosehn NI_NUMERICSERV);
779bd1d08a1SGarance A Drosehn if (error) {
780bd1d08a1SGarance A Drosehn /* same message to both syslog and remote user */
781bd1d08a1SGarance A Drosehn asprintf(&syserr, "malformed from-address (%d)", error);
782bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, syserr);
783bd1d08a1SGarance A Drosehn /* NOTREACHED */
7846ddb63caSGarance A Drosehn }
7856ddb63caSGarance A Drosehn
786bd1d08a1SGarance A Drosehn if (atoi(serv) >= IPPORT_RESERVED) {
787bd1d08a1SGarance A Drosehn /* same message to both syslog and remote user */
788bd1d08a1SGarance A Drosehn asprintf(&syserr, "connected from invalid port (%s)", serv);
789bd1d08a1SGarance A Drosehn fhosterr(ch_opts, syserr, syserr);
790bd1d08a1SGarance A Drosehn /* NOTREACHED */
791bd1d08a1SGarance A Drosehn }
792bd1d08a1SGarance A Drosehn }
793bd1d08a1SGarance A Drosehn
7946ddb63caSGarance A Drosehn /*
795bd1d08a1SGarance A Drosehn * Handle fatal errors in chkhost. The first message will optionally be
796bd1d08a1SGarance A Drosehn * sent to syslog, the second one is sent to the connecting host.
7976ddb63caSGarance A Drosehn *
7986ddb63caSGarance A Drosehn * The idea is that the syslog message is meant for an administrator of a
7996ddb63caSGarance A Drosehn * print server (the host receiving connections), while the usermsg is meant
8006ddb63caSGarance A Drosehn * for a remote user who may or may not be clueful, and may or may not be
8016ddb63caSGarance A Drosehn * doing something nefarious. Some remote users (eg, MS-Windows...) may not
8026ddb63caSGarance A Drosehn * even see whatever message is sent, which is why there's the option to
8036ddb63caSGarance A Drosehn * start 'lpd' with the connection-errors also sent to syslog.
8046ddb63caSGarance A Drosehn *
8056ddb63caSGarance A Drosehn * Given that hostnames can theoretically be fairly long (well, over 250
8066ddb63caSGarance A Drosehn * bytes), it would probably be helpful to have the 'from_host' field at
8076ddb63caSGarance A Drosehn * the end of any error messages which include that info.
808bd1d08a1SGarance A Drosehn *
809bd1d08a1SGarance A Drosehn * These are Fatal host-connection errors, so this routine does not return.
8106ddb63caSGarance A Drosehn */
811bd1d08a1SGarance A Drosehn static void
fhosterr(int ch_opts,char * sysmsg,char * usermsg)812bd1d08a1SGarance A Drosehn fhosterr(int ch_opts, char *sysmsg, char *usermsg)
8136ddb63caSGarance A Drosehn {
8146ddb63caSGarance A Drosehn
8156ddb63caSGarance A Drosehn /*
816bd1d08a1SGarance A Drosehn * If lpd was started up to print connection errors, then write
817bd1d08a1SGarance A Drosehn * the syslog message before the user message.
818bd1d08a1SGarance A Drosehn * And for many of the syslog messages, it is helpful to first
819bd1d08a1SGarance A Drosehn * write the from_host (if it is known) as a separate syslog
820bd1d08a1SGarance A Drosehn * message, since the hostname may be so long.
8216ddb63caSGarance A Drosehn */
822bd1d08a1SGarance A Drosehn if (ch_opts & LPD_LOGCONNERR) {
823bd1d08a1SGarance A Drosehn if (ch_opts & LPD_ADDFROMLINE) {
8246ddb63caSGarance A Drosehn syslog(LOG_WARNING, "for connection from %s:", from_host);
8256ddb63caSGarance A Drosehn }
826bd1d08a1SGarance A Drosehn syslog(LOG_WARNING, "%s", sysmsg);
8276ddb63caSGarance A Drosehn }
8286ddb63caSGarance A Drosehn
829bd1d08a1SGarance A Drosehn /*
830bd1d08a1SGarance A Drosehn * Now send the error message to the remote host which is trying
831bd1d08a1SGarance A Drosehn * to make the connection.
832bd1d08a1SGarance A Drosehn */
833bd1d08a1SGarance A Drosehn printf("%s [@%s]: %s\n", progname, local_host, usermsg);
8346ddb63caSGarance A Drosehn fflush(stdout);
8356ddb63caSGarance A Drosehn
8366ddb63caSGarance A Drosehn /*
8376ddb63caSGarance A Drosehn * Add a minimal delay before exiting (and disconnecting from the
8386ddb63caSGarance A Drosehn * sending-host). This is just in case that machine responds by
8396ddb63caSGarance A Drosehn * INSTANTLY retrying (and instantly re-failing...). This may also
8406ddb63caSGarance A Drosehn * give the other side more time to read the error message.
8416ddb63caSGarance A Drosehn */
8426ddb63caSGarance A Drosehn sleep(2); /* a paranoid throttling measure */
8436ddb63caSGarance A Drosehn exit(1);
8440b561052SJoerg Wunsch }
84536d0e2a3SJoerg Wunsch
84608829865SHajimu UMEMOTO /* setup server socket for specified address family */
84708829865SHajimu UMEMOTO /* if af is PF_UNSPEC more than one socket may be returned */
84808829865SHajimu UMEMOTO /* the returned list is dynamically allocated, so caller needs to free it */
84908829865SHajimu UMEMOTO static int *
socksetup(int af,int debuglvl)850aa4ad562SGarance A Drosehn socksetup(int af, int debuglvl)
85108829865SHajimu UMEMOTO {
85208829865SHajimu UMEMOTO struct addrinfo hints, *res, *r;
85308829865SHajimu UMEMOTO int error, maxs, *s, *socks;
85408829865SHajimu UMEMOTO const int on = 1;
85508829865SHajimu UMEMOTO
85608829865SHajimu UMEMOTO memset(&hints, 0, sizeof(hints));
85708829865SHajimu UMEMOTO hints.ai_flags = AI_PASSIVE;
85808829865SHajimu UMEMOTO hints.ai_family = af;
85908829865SHajimu UMEMOTO hints.ai_socktype = SOCK_STREAM;
86008829865SHajimu UMEMOTO error = getaddrinfo(NULL, "printer", &hints, &res);
86108829865SHajimu UMEMOTO if (error) {
86208829865SHajimu UMEMOTO syslog(LOG_ERR, "%s", gai_strerror(error));
86308829865SHajimu UMEMOTO mcleanup(0);
86408829865SHajimu UMEMOTO }
86508829865SHajimu UMEMOTO
86608829865SHajimu UMEMOTO /* Count max number of sockets we may open */
86708829865SHajimu UMEMOTO for (maxs = 0, r = res; r; r = r->ai_next, maxs++)
86808829865SHajimu UMEMOTO ;
86908829865SHajimu UMEMOTO socks = malloc((maxs + 1) * sizeof(int));
87008829865SHajimu UMEMOTO if (!socks) {
87108829865SHajimu UMEMOTO syslog(LOG_ERR, "couldn't allocate memory for sockets");
87208829865SHajimu UMEMOTO mcleanup(0);
87308829865SHajimu UMEMOTO }
87408829865SHajimu UMEMOTO
87508829865SHajimu UMEMOTO *socks = 0; /* num of sockets counter at start of array */
87608829865SHajimu UMEMOTO s = socks + 1;
87708829865SHajimu UMEMOTO for (r = res; r; r = r->ai_next) {
87808829865SHajimu UMEMOTO *s = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
87908829865SHajimu UMEMOTO if (*s < 0) {
88008829865SHajimu UMEMOTO syslog(LOG_DEBUG, "socket(): %m");
88108829865SHajimu UMEMOTO continue;
88208829865SHajimu UMEMOTO }
883aa4ad562SGarance A Drosehn if (setsockopt(*s, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
884aa4ad562SGarance A Drosehn < 0) {
88508829865SHajimu UMEMOTO syslog(LOG_ERR, "setsockopt(SO_REUSEADDR): %m");
88608829865SHajimu UMEMOTO close(*s);
88708829865SHajimu UMEMOTO continue;
88808829865SHajimu UMEMOTO }
889aa4ad562SGarance A Drosehn if (debuglvl)
890aa4ad562SGarance A Drosehn if (setsockopt(*s, SOL_SOCKET, SO_DEBUG, &debuglvl,
891aa4ad562SGarance A Drosehn sizeof(debuglvl)) < 0) {
89208829865SHajimu UMEMOTO syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
89308829865SHajimu UMEMOTO close(*s);
89408829865SHajimu UMEMOTO continue;
89508829865SHajimu UMEMOTO }
89608829865SHajimu UMEMOTO if (r->ai_family == AF_INET6) {
8974a78ccd7SHajimu UMEMOTO if (setsockopt(*s, IPPROTO_IPV6, IPV6_V6ONLY,
89808829865SHajimu UMEMOTO &on, sizeof(on)) < 0) {
89908829865SHajimu UMEMOTO syslog(LOG_ERR,
9004a78ccd7SHajimu UMEMOTO "setsockopt (IPV6_V6ONLY): %m");
90108829865SHajimu UMEMOTO close(*s);
90208829865SHajimu UMEMOTO continue;
90308829865SHajimu UMEMOTO }
90408829865SHajimu UMEMOTO }
90508829865SHajimu UMEMOTO if (bind(*s, r->ai_addr, r->ai_addrlen) < 0) {
90608829865SHajimu UMEMOTO syslog(LOG_DEBUG, "bind(): %m");
90708829865SHajimu UMEMOTO close(*s);
90808829865SHajimu UMEMOTO continue;
90908829865SHajimu UMEMOTO }
91008829865SHajimu UMEMOTO (*socks)++;
91108829865SHajimu UMEMOTO s++;
91208829865SHajimu UMEMOTO }
91308829865SHajimu UMEMOTO
91408829865SHajimu UMEMOTO if (res)
91508829865SHajimu UMEMOTO freeaddrinfo(res);
91608829865SHajimu UMEMOTO
91708829865SHajimu UMEMOTO if (*socks == 0) {
91808829865SHajimu UMEMOTO syslog(LOG_ERR, "Couldn't bind to any socket");
91908829865SHajimu UMEMOTO free(socks);
92008829865SHajimu UMEMOTO mcleanup(0);
92108829865SHajimu UMEMOTO }
92208829865SHajimu UMEMOTO return(socks);
92308829865SHajimu UMEMOTO }
924affa0039SGarance A Drosehn
925affa0039SGarance A Drosehn static void
usage(void)926ba7a1ad7SGarance A Drosehn usage(void)
927affa0039SGarance A Drosehn {
928affa0039SGarance A Drosehn #ifdef INET6
9293c636606SChris Rees fprintf(stderr, "usage: lpd [-cdlsFW46] [port#]\n");
930affa0039SGarance A Drosehn #else
9313c636606SChris Rees fprintf(stderr, "usage: lpd [-cdlsFW] [port#]\n");
932affa0039SGarance A Drosehn #endif
933affa0039SGarance A Drosehn exit(EX_USAGE);
934affa0039SGarance A Drosehn }
935