inetd.c revision 1.45 1 /* $NetBSD: inetd.c,v 1.45 1999/01/20 04:42:17 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Copyright (c) 1983, 1991, 1993, 1994
42 * The Regents of the University of California. All rights reserved.
43 *
44 * Redistribution and use in source and binary forms, with or without
45 * modification, are permitted provided that the following conditions
46 * are met:
47 * 1. Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
49 * 2. Redistributions in binary form must reproduce the above copyright
50 * notice, this list of conditions and the following disclaimer in the
51 * documentation and/or other materials provided with the distribution.
52 * 3. All advertising materials mentioning features or use of this software
53 * must display the following acknowledgement:
54 * This product includes software developed by the University of
55 * California, Berkeley and its contributors.
56 * 4. Neither the name of the University nor the names of its contributors
57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 */
72
73 #include <sys/cdefs.h>
74 #ifndef lint
75 __COPYRIGHT("@(#) Copyright (c) 1983, 1991, 1993, 1994\n\
76 The Regents of the University of California. All rights reserved.\n");
77 #if 0
78 static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94";
79 #else
80 __RCSID("$NetBSD: inetd.c,v 1.45 1999/01/20 04:42:17 mycroft Exp $");
81 #endif
82 #endif /* not lint */
83
84 /*
85 * Inetd - Internet super-server
86 *
87 * This program invokes all internet services as needed. Connection-oriented
88 * services are invoked each time a connection is made, by creating a process.
89 * This process is passed the connection as file descriptor 0 and is expected
90 * to do a getpeername to find out the source host and port.
91 *
92 * Datagram oriented services are invoked when a datagram
93 * arrives; a process is created and passed a pending message
94 * on file descriptor 0. Datagram servers may either connect
95 * to their peer, freeing up the original socket for inetd
96 * to receive further messages on, or ``take over the socket'',
97 * processing all arriving datagrams and, eventually, timing
98 * out. The first type of server is said to be ``multi-threaded'';
99 * the second type of server ``single-threaded''.
100 *
101 * Inetd uses a configuration file which is read at startup
102 * and, possibly, at some later time in response to a hangup signal.
103 * The configuration file is ``free format'' with fields given in the
104 * order shown below. Continuation lines for an entry must being with
105 * a space or tab. All fields must be present in each entry.
106 *
107 * service name must be in /etc/services or must
108 * name a tcpmux service
109 * socket type stream/dgram/raw/rdm/seqpacket
110 * protocol must be in /etc/protocols
111 * wait/nowait[.max] single-threaded/multi-threaded, max #
112 * user[.group] user/group to run daemon as
113 * server program full path name
114 * server program arguments maximum of MAXARGS (20)
115 *
116 * For RPC services
117 * service name/version must be in /etc/rpc
118 * socket type stream/dgram/raw/rdm/seqpacket
119 * protocol must be in /etc/protocols
120 * wait/nowait[.max] single-threaded/multi-threaded
121 * user[.group] user to run daemon as
122 * server program full path name
123 * server program arguments maximum of MAXARGS (20)
124 *
125 * For non-RPC services, the "service name" can be of the form
126 * hostaddress:servicename, in which case the hostaddress is used
127 * as the host portion of the address to listen on. If hostaddress
128 * consists of a single `*' character, INADDR_ANY is used.
129 *
130 * A line can also consist of just
131 * hostaddress:
132 * where hostaddress is as in the preceding paragraph. Such a line must
133 * have no further fields; the specified hostaddress is remembered and
134 * used for all further lines that have no hostaddress specified,
135 * until the next such line (or EOF). (This is why * is provided to
136 * allow explicit specification of INADDR_ANY.) A line
137 * *:
138 * is implicitly in effect at the beginning of the file.
139 *
140 * The hostaddress specifier may (and often will) contain dots;
141 * the service name must not.
142 *
143 * For RPC services, host-address specifiers are accepted and will
144 * work to some extent; however, because of limitations in the
145 * portmapper interface, it will not work to try to give more than
146 * one line for any given RPC service, even if the host-address
147 * specifiers are different.
148 *
149 * TCP services without official port numbers are handled with the
150 * RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
151 * requests. When a connection is made from a foreign host, the service
152 * requested is passed to tcpmux, which looks it up in the servtab list
153 * and returns the proper entry for the service. Tcpmux returns a
154 * negative reply if the service doesn't exist, otherwise the invoked
155 * server is expected to return the positive reply if the service type in
156 * inetd.conf file has the prefix "tcpmux/". If the service type has the
157 * prefix "tcpmux/+", tcpmux will return the positive reply for the
158 * process; this is for compatibility with older server code, and also
159 * allows you to invoke programs that use stdin/stdout without putting any
160 * special server code in them. Services that use tcpmux are "nowait"
161 * because they do not have a well-known port and hence cannot listen
162 * for new requests.
163 *
164 * Comment lines are indicated by a `#' in column 1.
165 */
166
167 /*
168 * Here's the scoop concerning the user.group feature:
169 *
170 * 1) set-group-option off.
171 *
172 * a) user = root: NO setuid() or setgid() is done
173 *
174 * b) other: setuid()
175 * setgid(primary group as found in passwd)
176 * initgroups(name, primary group)
177 *
178 * 2) set-group-option on.
179 *
180 * a) user = root: NO setuid()
181 * setgid(specified group)
182 * NO initgroups()
183 *
184 * b) other: setuid()
185 * setgid(specified group)
186 * initgroups(name, specified group)
187 *
188 */
189
190 #include <sys/param.h>
191 #include <sys/stat.h>
192 #include <sys/ioctl.h>
193 #include <sys/socket.h>
194 #include <sys/un.h>
195 #include <sys/wait.h>
196 #include <sys/time.h>
197 #include <sys/resource.h>
198
199 #ifndef RLIMIT_NOFILE
200 #define RLIMIT_NOFILE RLIMIT_OFILE
201 #endif
202
203 #define RPC
204
205 #include <netinet/in.h>
206 #include <arpa/inet.h>
207 #ifdef RPC
208 #include <rpc/rpc.h>
209 #include <rpc/pmap_clnt.h>
210 #endif
211
212 #include <ctype.h>
213 #include <errno.h>
214 #include <fcntl.h>
215 #include <grp.h>
216 #include <netdb.h>
217 #include <pwd.h>
218 #include <signal.h>
219 #include <stdio.h>
220 #include <stdlib.h>
221 #include <string.h>
222 #include <syslog.h>
223 #include <unistd.h>
224
225 #include "pathnames.h"
226
227 #ifdef LIBWRAP
228 # include <tcpd.h>
229 #ifndef LIBWRAP_ALLOW_FACILITY
230 # define LIBWRAP_ALLOW_FACILITY LOG_AUTH
231 #endif
232 #ifndef LIBWRAP_ALLOW_SEVERITY
233 # define LIBWRAP_ALLOW_SEVERITY LOG_INFO
234 #endif
235 #ifndef LIBWRAP_DENY_FACILITY
236 # define LIBWRAP_DENY_FACILITY LOG_AUTH
237 #endif
238 #ifndef LIBWRAP_DENY_SEVERITY
239 # define LIBWRAP_DENY_SEVERITY LOG_WARNING
240 #endif
241 int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
242 int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
243 #endif
244
245 #define TOOMANY 40 /* don't start more than TOOMANY */
246 #define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
247 #define RETRYTIME (60*10) /* retry after bind or server fail */
248
249 #define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
250
251 int debug;
252 #ifdef LIBWRAP
253 int lflag;
254 #endif
255 int nsock, maxsock;
256 fd_set allsock;
257 int options;
258 int timingout;
259 struct servent *sp;
260 char *curdom;
261
262 #ifndef OPEN_MAX
263 #define OPEN_MAX 64
264 #endif
265
266 /* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
267 #define FD_MARGIN (8)
268 typeof(((struct rlimit *)0)->rlim_cur) rlim_ofile_cur = OPEN_MAX;
269
270 #ifdef RLIMIT_NOFILE
271 struct rlimit rlim_ofile;
272 #endif
273
274 struct servtab {
275 char *se_hostaddr; /* host address to listen on */
276 char *se_service; /* name of service */
277 int se_socktype; /* type of socket to use */
278 int se_family; /* address family */
279 char *se_proto; /* protocol used */
280 int se_sndbuf; /* sndbuf size */
281 int se_rcvbuf; /* rcvbuf size */
282 int se_rpcprog; /* rpc program number */
283 int se_rpcversl; /* rpc program lowest version */
284 int se_rpcversh; /* rpc program highest version */
285 #define isrpcservice(sep) ((sep)->se_rpcversl != 0)
286 short se_wait; /* single threaded server */
287 short se_checked; /* looked at during merge */
288 char *se_user; /* user name to run as */
289 char *se_group; /* group name to run as */
290 struct biltin *se_bi; /* if built-in, description */
291 char *se_server; /* server program */
292 #define MAXARGV 20
293 char *se_argv[MAXARGV+1]; /* program arguments */
294 int se_fd; /* open descriptor */
295 int se_type; /* type */
296 union {
297 struct sockaddr se_un_ctrladdr;
298 struct sockaddr_in se_un_ctrladdr_in;
299 struct sockaddr_un se_un_ctrladdr_un;
300 } se_un; /* bound address */
301 #define se_ctrladdr se_un.se_un_ctrladdr
302 #define se_ctrladdr_in se_un.se_un_ctrladdr_in
303 #define se_ctrladdr_un se_un.se_un_ctrladdr_un
304 int se_ctrladdr_size;
305 int se_max; /* max # of instances of this service */
306 int se_count; /* number started since se_time */
307 struct timeval se_time; /* start of se_count */
308 #ifdef MULOG
309 int se_log;
310 #define MULOG_RFC931 0x40000000
311 #endif
312 struct servtab *se_next;
313 } *servtab;
314
315 #define NORM_TYPE 0
316 #define MUX_TYPE 1
317 #define MUXPLUS_TYPE 2
318 #define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \
319 ((sep)->se_type == MUXPLUS_TYPE))
320 #define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE)
321
322
323 void chargen_dg __P((int, struct servtab *));
324 void chargen_stream __P((int, struct servtab *));
325 void close_sep __P((struct servtab *));
326 void config __P((int));
327 void daytime_dg __P((int, struct servtab *));
328 void daytime_stream __P((int, struct servtab *));
329 void discard_dg __P((int, struct servtab *));
330 void discard_stream __P((int, struct servtab *));
331 void echo_dg __P((int, struct servtab *));
332 void echo_stream __P((int, struct servtab *));
333 void endconfig __P((void));
334 struct servtab *enter __P((struct servtab *));
335 void freeconfig __P((struct servtab *));
336 struct servtab *getconfigent __P((void));
337 void goaway __P((int));
338 void machtime_dg __P((int, struct servtab *));
339 void machtime_stream __P((int, struct servtab *));
340 char *newstr __P((char *));
341 char *nextline __P((FILE *));
342 void print_service __P((char *, struct servtab *));
343 void reapchild __P((int));
344 void retry __P((int));
345 void run_service __P((int, struct servtab *));
346 int setconfig __P((void));
347 void setup __P((struct servtab *));
348 char *sskip __P((char **));
349 char *skip __P((char **));
350 void tcpmux __P((int, struct servtab *));
351 void usage __P((void));
352 void logpid __P((void));
353 void register_rpc __P((struct servtab *sep));
354 void unregister_rpc __P((struct servtab *sep));
355 void bump_nofile __P((void));
356 void inetd_setproctitle __P((char *, int));
357 void initring __P((void));
358 long machtime __P((void));
359 static int getline __P((int, char *, int));
360 int main __P((int, char *[], char *[]));
361
362 struct biltin {
363 char *bi_service; /* internally provided service name */
364 int bi_socktype; /* type of socket supported */
365 short bi_fork; /* 1 if should fork before call */
366 short bi_wait; /* 1 if should wait for child */
367 void (*bi_fn) __P((int, struct servtab *));
368 /* function which performs it */
369 } biltins[] = {
370 /* Echo received data */
371 { "echo", SOCK_STREAM, 1, 0, echo_stream },
372 { "echo", SOCK_DGRAM, 0, 0, echo_dg },
373
374 /* Internet /dev/null */
375 { "discard", SOCK_STREAM, 1, 0, discard_stream },
376 { "discard", SOCK_DGRAM, 0, 0, discard_dg },
377
378 /* Return 32 bit time since 1970 */
379 { "time", SOCK_STREAM, 0, 0, machtime_stream },
380 { "time", SOCK_DGRAM, 0, 0, machtime_dg },
381
382 /* Return human-readable time */
383 { "daytime", SOCK_STREAM, 0, 0, daytime_stream },
384 { "daytime", SOCK_DGRAM, 0, 0, daytime_dg },
385
386 /* Familiar character generator */
387 { "chargen", SOCK_STREAM, 1, 0, chargen_stream },
388 { "chargen", SOCK_DGRAM, 0, 0, chargen_dg },
389
390 { "tcpmux", SOCK_STREAM, 1, 0, tcpmux },
391
392 { NULL }
393 };
394
395 #define NUMINT (sizeof(intab) / sizeof(struct inent))
396 char *CONFIG = _PATH_INETDCONF;
397 char **Argv;
398 char *LastArg;
399 extern char *__progname;
400
401 #ifdef sun
402 /*
403 * Sun's RPC library caches the result of `dtablesize()'
404 * This is incompatible with our "bumping" of file descriptors "on demand"
405 */
406 int
407 _rpc_dtablesize()
408 {
409 return rlim_ofile_cur;
410 }
411 #endif
412
413 int
414 main(argc, argv, envp)
415 int argc;
416 char *argv[], *envp[];
417 {
418 struct servtab *sep, *nsep;
419 struct sigvec sv;
420 int ch, dofork;
421 pid_t pid;
422
423 Argv = argv;
424 if (envp == 0 || *envp == 0)
425 envp = argv;
426 while (*envp)
427 envp++;
428 LastArg = envp[-1] + strlen(envp[-1]);
429
430 while ((ch = getopt(argc, argv,
431 #ifdef LIBWRAP
432 "dl"
433 #else
434 "d"
435 #endif
436 )) != -1)
437 switch(ch) {
438 case 'd':
439 debug = 1;
440 options |= SO_DEBUG;
441 break;
442 #ifdef LIBWRAP
443 case 'l':
444 lflag = 1;
445 break;
446 #endif
447 case '?':
448 default:
449 usage();
450 }
451 argc -= optind;
452 argv += optind;
453
454 if (argc > 0)
455 CONFIG = argv[0];
456
457 if (debug == 0)
458 daemon(0, 0);
459 openlog(__progname, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
460 logpid();
461
462 #ifdef RLIMIT_NOFILE
463 if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
464 syslog(LOG_ERR, "getrlimit: %m");
465 } else {
466 rlim_ofile_cur = rlim_ofile.rlim_cur;
467 if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
468 rlim_ofile_cur = OPEN_MAX;
469 }
470 #endif
471
472 memset(&sv, 0, sizeof(sv));
473 sv.sv_mask = SIGBLOCK;
474 sv.sv_handler = retry;
475 sigvec(SIGALRM, &sv, (struct sigvec *)0);
476 config(SIGHUP);
477 sv.sv_handler = config;
478 sigvec(SIGHUP, &sv, (struct sigvec *)0);
479 sv.sv_handler = reapchild;
480 sigvec(SIGCHLD, &sv, (struct sigvec *)0);
481 sv.sv_handler = goaway;
482 sigvec(SIGTERM, &sv, (struct sigvec *)0);
483 sv.sv_handler = goaway;
484 sigvec(SIGINT, &sv, (struct sigvec *)0);
485 sv.sv_mask = 0L;
486 sv.sv_handler = SIG_IGN;
487 sigvec(SIGPIPE, &sv, (struct sigvec *)0);
488
489 {
490 /* space for daemons to overwrite environment for ps */
491 #define DUMMYSIZE 100
492 char dummy[DUMMYSIZE];
493
494 (void)memset(dummy, 'x', DUMMYSIZE - 1);
495 dummy[DUMMYSIZE - 1] = '\0';
496
497 (void)setenv("inetd_dummy", dummy, 1);
498 }
499
500 for (;;) {
501 int n, ctrl;
502 fd_set readable;
503
504 if (nsock == 0) {
505 (void) sigblock(SIGBLOCK);
506 while (nsock == 0)
507 sigpause(0L);
508 (void) sigsetmask(0L);
509 }
510 readable = allsock;
511 if ((n = select(maxsock + 1, &readable, (fd_set *)0,
512 (fd_set *)0, (struct timeval *)0)) <= 0) {
513 if (n == -1 && errno != EINTR) {
514 syslog(LOG_WARNING, "select: %m");
515 sleep(1);
516 }
517 continue;
518 }
519 for (sep = servtab; n && sep; sep = nsep) {
520 nsep = sep->se_next;
521 if (sep->se_fd != -1 && FD_ISSET(sep->se_fd, &readable)) {
522 n--;
523 if (debug)
524 fprintf(stderr, "someone wants %s\n", sep->se_service);
525 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
526 /* XXX here do the libwrap check-before-accept */
527 ctrl = accept(sep->se_fd, (struct sockaddr *)0,
528 (int *)0);
529 if (debug)
530 fprintf(stderr, "accept, ctrl %d\n", ctrl);
531 if (ctrl < 0) {
532 if (errno != EINTR)
533 syslog(LOG_WARNING,
534 "accept (for %s): %m",
535 sep->se_service);
536 continue;
537 }
538 /* Set the socket to blocking mode. */
539 if (fcntl(ctrl, F_SETFL, 0) < 0)
540 syslog(LOG_ERR, "fcntl (F_SETFL, 0): %m");
541 } else
542 ctrl = sep->se_fd;
543 (void) sigblock(SIGBLOCK);
544 pid = 0;
545 #ifdef LIBWRAP_INTERNAL
546 dofork = 1;
547 #else
548 dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
549 #endif
550 if (dofork) {
551 if (sep->se_count++ == 0)
552 (void)gettimeofday(&sep->se_time,
553 (struct timezone *)0);
554 else if (sep->se_count >= sep->se_max) {
555 struct timeval now;
556
557 (void)gettimeofday(&now, (struct timezone *)0);
558 if (now.tv_sec - sep->se_time.tv_sec >
559 CNT_INTVL) {
560 sep->se_time = now;
561 sep->se_count = 1;
562 } else {
563 syslog(LOG_ERR,
564 "%s/%s server failing (looping), service terminated\n",
565 sep->se_service, sep->se_proto);
566 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
567 close(ctrl);
568 close_sep(sep);
569 sigsetmask(0L);
570 if (!timingout) {
571 timingout = 1;
572 alarm(RETRYTIME);
573 }
574 continue;
575 }
576 }
577 pid = fork();
578 if (pid < 0) {
579 syslog(LOG_ERR, "fork: %m");
580 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
581 close(ctrl);
582 sigsetmask(0L);
583 sleep(1);
584 continue;
585 }
586 if (pid != 0 && sep->se_wait) {
587 sep->se_wait = pid;
588 FD_CLR(sep->se_fd, &allsock);
589 nsock--;
590 }
591 if (pid == 0) {
592 sv.sv_mask = 0L;
593 sv.sv_handler = SIG_DFL;
594 sigvec(SIGPIPE, &sv, (struct sigvec *)0);
595 if (debug)
596 setsid();
597 }
598 }
599 sigsetmask(0L);
600 if (pid == 0) {
601 run_service(ctrl, sep);
602 if (dofork)
603 exit(0);
604 }
605 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
606 close(ctrl);
607 }
608 }
609 }
610 }
611
612 void
613 run_service(ctrl, sep)
614 int ctrl;
615 struct servtab *sep;
616 {
617 struct passwd *pwd;
618 struct group *grp = NULL; /* XXX gcc */
619 char buf[7];
620 #ifdef LIBWRAP
621 struct request_info req;
622 int denied;
623 char *service = NULL; /* XXX gcc */
624 #endif
625
626 #ifdef LIBWRAP
627 #ifndef LIBWRAP_INTERNAL
628 if (sep->se_bi == 0)
629 #endif
630 if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
631 request_init(&req, RQ_DAEMON, sep->se_argv[0] ?
632 sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL);
633 fromhost(&req);
634 denied = !hosts_access(&req);
635 if (denied || lflag) {
636 sp = getservbyport(sep->se_ctrladdr_in.sin_port,
637 sep->se_proto);
638 if (sp == NULL) {
639 (void)snprintf(buf, sizeof buf, "%d",
640 ntohs(sep->se_ctrladdr_in.sin_port));
641 service = buf;
642 } else
643 service = sp->s_name;
644 }
645 if (denied) {
646 syslog(deny_severity,
647 "refused connection from %.500s, service %s (%s)",
648 eval_client(&req), service, sep->se_proto);
649 goto reject;
650 }
651 if (lflag) {
652 syslog(allow_severity,
653 "connection from %.500s, service %s (%s)",
654 eval_client(&req), service, sep->se_proto);
655 }
656 }
657 #endif /* LIBWRAP */
658
659 if (sep->se_bi) {
660 (*sep->se_bi->bi_fn)(ctrl, sep);
661 } else {
662 if ((pwd = getpwnam(sep->se_user)) == NULL) {
663 syslog(LOG_ERR, "%s/%s: %s: No such user",
664 sep->se_service, sep->se_proto, sep->se_user);
665 goto reject;
666 }
667 if (sep->se_group &&
668 (grp = getgrnam(sep->se_group)) == NULL) {
669 syslog(LOG_ERR, "%s/%s: %s: No such group",
670 sep->se_service, sep->se_proto, sep->se_group);
671 goto reject;
672 }
673 if (pwd->pw_uid) {
674 if (sep->se_group)
675 pwd->pw_gid = grp->gr_gid;
676 if (setgid(pwd->pw_gid) < 0) {
677 syslog(LOG_ERR,
678 "%s/%s: can't set gid %d: %m", sep->se_service,
679 sep->se_proto, pwd->pw_gid);
680 goto reject;
681 }
682 (void) initgroups(pwd->pw_name,
683 pwd->pw_gid);
684 if (setuid(pwd->pw_uid) < 0) {
685 syslog(LOG_ERR,
686 "%s/%s: can't set uid %d: %m", sep->se_service,
687 sep->se_proto, pwd->pw_uid);
688 goto reject;
689 }
690 } else if (sep->se_group) {
691 (void) setgid((gid_t)grp->gr_gid);
692 }
693 if (debug)
694 fprintf(stderr, "%d execl %s\n",
695 getpid(), sep->se_server);
696 #ifdef MULOG
697 if (sep->se_log)
698 dolog(sep, ctrl);
699 #endif
700 /* Set our control descriptor to not close-on-exec... */
701 if (fcntl(ctrl, F_SETFD, 0) < 0)
702 syslog(LOG_ERR, "fcntl (F_SETFD, 0): %m");
703 /* ...and dup it to stdin, stdout, and stderr. */
704 if (ctrl != 0) {
705 dup2(ctrl, 0);
706 close(ctrl);
707 ctrl = 0;
708 }
709 dup2(0, 1);
710 dup2(0, 2);
711 #ifdef RLIMIT_NOFILE
712 if (rlim_ofile.rlim_cur != rlim_ofile_cur &&
713 setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
714 syslog(LOG_ERR, "setrlimit: %m");
715 #endif
716 execv(sep->se_server, sep->se_argv);
717 syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server);
718 reject:
719 if (sep->se_socktype != SOCK_STREAM)
720 recv(ctrl, buf, sizeof (buf), 0);
721 _exit(1);
722 }
723 }
724
725 void
726 reapchild(signo)
727 int signo;
728 {
729 int status;
730 pid_t pid;
731 struct servtab *sep;
732
733 for (;;) {
734 pid = wait3(&status, WNOHANG, (struct rusage *)0);
735 if (pid <= 0)
736 break;
737 if (debug)
738 fprintf(stderr, "%d reaped, status %#x\n",
739 pid, status);
740 for (sep = servtab; sep; sep = sep->se_next)
741 if (sep->se_wait == pid) {
742 if (WIFEXITED(status) && WEXITSTATUS(status))
743 syslog(LOG_WARNING,
744 "%s: exit status 0x%x",
745 sep->se_server, WEXITSTATUS(status));
746 else if (WIFSIGNALED(status))
747 syslog(LOG_WARNING,
748 "%s: exit signal 0x%x",
749 sep->se_server, WTERMSIG(status));
750 sep->se_wait = 1;
751 FD_SET(sep->se_fd, &allsock);
752 nsock++;
753 if (debug)
754 fprintf(stderr, "restored %s, fd %d\n",
755 sep->se_service, sep->se_fd);
756 }
757 }
758 }
759
760 void
761 config(signo)
762 int signo;
763 {
764 struct servtab *sep, *cp, **sepp;
765 long omask;
766 int n;
767
768 if (!setconfig()) {
769 syslog(LOG_ERR, "%s: %m", CONFIG);
770 return;
771 }
772 for (sep = servtab; sep; sep = sep->se_next)
773 sep->se_checked = 0;
774 while ((cp = getconfigent())) {
775 for (sep = servtab; sep; sep = sep->se_next)
776 if (strcmp(sep->se_service, cp->se_service) == 0 &&
777 strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 &&
778 strcmp(sep->se_proto, cp->se_proto) == 0 &&
779 ISMUX(sep) == ISMUX(cp))
780 break;
781 if (sep != 0) {
782 int i;
783
784 #define SWAP(type, a, b) {type c=(type)a; (type)a=(type)b; (type)b=(type)c;}
785
786 omask = sigblock(SIGBLOCK);
787 /*
788 * sep->se_wait may be holding the pid of a daemon
789 * that we're waiting for. If so, don't overwrite
790 * it unless the config file explicitly says don't
791 * wait.
792 */
793 if (cp->se_bi == 0 &&
794 (sep->se_wait == 1 || cp->se_wait == 0))
795 sep->se_wait = cp->se_wait;
796 SWAP(char *, sep->se_user, cp->se_user);
797 SWAP(char *, sep->se_group, cp->se_group);
798 SWAP(char *, sep->se_server, cp->se_server);
799 for (i = 0; i < MAXARGV; i++)
800 SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
801 SWAP(int, cp->se_type, sep->se_type);
802 SWAP(int, cp->se_max, sep->se_max);
803 #undef SWAP
804 if (isrpcservice(sep))
805 unregister_rpc(sep);
806 sep->se_rpcversl = cp->se_rpcversl;
807 sep->se_rpcversh = cp->se_rpcversh;
808 sigsetmask(omask);
809 freeconfig(cp);
810 if (debug)
811 print_service("REDO", sep);
812 } else {
813 sep = enter(cp);
814 if (debug)
815 print_service("ADD ", sep);
816 }
817 sep->se_checked = 1;
818
819 switch (sep->se_family) {
820 case AF_LOCAL:
821 if (sep->se_fd != -1)
822 break;
823 n = strlen(sep->se_service);
824 if (n > sizeof(sep->se_ctrladdr_un.sun_path)) {
825 syslog(LOG_ERR, "%s: address too long",
826 sep->se_service);
827 sep->se_checked = 0;
828 continue;
829 }
830 (void)unlink(sep->se_service);
831 strncpy(sep->se_ctrladdr_un.sun_path,
832 sep->se_service, n);
833 sep->se_ctrladdr_un.sun_family = AF_LOCAL;
834 sep->se_ctrladdr_size = n +
835 sizeof(sep->se_ctrladdr_un) -
836 sizeof(sep->se_ctrladdr_un.sun_path);
837 if (!ISMUX(sep))
838 setup(sep);
839 break;
840 case AF_INET:
841 sep->se_ctrladdr_in.sin_family = AF_INET;
842 if (!strcmp(sep->se_hostaddr,"*"))
843 sep->se_ctrladdr_in.sin_addr.s_addr =
844 INADDR_ANY;
845 else if (!inet_aton(sep->se_hostaddr,
846 &sep->se_ctrladdr_in.sin_addr)) {
847 /* Do we really want to support hostname lookups here? */
848 struct hostent *hp;
849 hp = gethostbyname(sep->se_hostaddr);
850 if (hp == 0) {
851 syslog(LOG_ERR, "%s: unknown host",
852 sep->se_hostaddr);
853 sep->se_checked = 0;
854 continue;
855 } else if (hp->h_addrtype != AF_INET) {
856 syslog(LOG_ERR,
857 "%s: address isn't an Internet address",
858 sep->se_hostaddr);
859 sep->se_checked = 0;
860 continue;
861 } else if (hp->h_length != sizeof(struct in_addr)) {
862 syslog(LOG_ERR,
863 "%s: address size wrong (under DNS corruption attack?)",
864 sep->se_hostaddr);
865 sep->se_checked = 0;
866 continue;
867 } else {
868 memcpy(&sep->se_ctrladdr_in.sin_addr,
869 hp->h_addr_list[0],
870 sizeof(struct in_addr));
871 }
872 }
873 if (ISMUX(sep)) {
874 sep->se_fd = -1;
875 continue;
876 }
877 sep->se_ctrladdr_size = sizeof(sep->se_ctrladdr_in);
878 if (isrpcservice(sep)) {
879 struct rpcent *rp;
880
881 sep->se_rpcprog = atoi(sep->se_service);
882 if (sep->se_rpcprog == 0) {
883 rp = getrpcbyname(sep->se_service);
884 if (rp == 0) {
885 syslog(LOG_ERR,
886 "%s/%s: unknown service",
887 sep->se_service,
888 sep->se_proto);
889 sep->se_checked = 0;
890 continue;
891 }
892 sep->se_rpcprog = rp->r_number;
893 }
894 if (sep->se_fd == -1 && !ISMUX(sep))
895 setup(sep);
896 if (sep->se_fd != -1)
897 register_rpc(sep);
898 } else {
899 u_short port = htons(atoi(sep->se_service));
900
901 if (!port) {
902 sp = getservbyname(sep->se_service,
903 sep->se_proto);
904 if (sp == 0) {
905 syslog(LOG_ERR,
906 "%s/%s: unknown service",
907 sep->se_service,
908 sep->se_proto);
909 sep->se_checked = 0;
910 continue;
911 }
912 port = sp->s_port;
913 }
914 if (port != sep->se_ctrladdr_in.sin_port) {
915 sep->se_ctrladdr_in.sin_port = port;
916 if (sep->se_fd >= 0)
917 close_sep(sep);
918 }
919 if (sep->se_fd == -1 && !ISMUX(sep))
920 setup(sep);
921 }
922 }
923 }
924 endconfig();
925 /*
926 * Purge anything not looked at above.
927 */
928 omask = sigblock(SIGBLOCK);
929 sepp = &servtab;
930 while ((sep = *sepp)) {
931 if (sep->se_checked) {
932 sepp = &sep->se_next;
933 continue;
934 }
935 *sepp = sep->se_next;
936 if (sep->se_fd >= 0)
937 close_sep(sep);
938 if (isrpcservice(sep))
939 unregister_rpc(sep);
940 if (sep->se_family == AF_LOCAL)
941 (void)unlink(sep->se_service);
942 if (debug)
943 print_service("FREE", sep);
944 freeconfig(sep);
945 free((char *)sep);
946 }
947 (void) sigsetmask(omask);
948 }
949
950 void
951 retry(signo)
952 int signo;
953 {
954 struct servtab *sep;
955
956 timingout = 0;
957 for (sep = servtab; sep; sep = sep->se_next) {
958 if (sep->se_fd == -1 && !ISMUX(sep)) {
959 switch (sep->se_family) {
960 case AF_LOCAL:
961 case AF_INET:
962 setup(sep);
963 if (sep->se_fd != -1 && isrpcservice(sep))
964 register_rpc(sep);
965 break;
966 }
967 }
968 }
969 }
970
971 void
972 goaway(signo)
973 int signo;
974 {
975 struct servtab *sep;
976
977 for (sep = servtab; sep; sep = sep->se_next) {
978 if (sep->se_fd == -1)
979 continue;
980
981 switch (sep->se_family) {
982 case AF_LOCAL:
983 (void)unlink(sep->se_service);
984 break;
985 case AF_INET:
986 if (sep->se_wait == 1 && isrpcservice(sep))
987 unregister_rpc(sep);
988 break;
989 }
990 (void)close(sep->se_fd);
991 }
992 (void)unlink(_PATH_INETDPID);
993 exit(0);
994 }
995
996 void
997 setup(sep)
998 struct servtab *sep;
999 {
1000 int on = 1;
1001
1002 if ((sep->se_fd = socket(sep->se_family, sep->se_socktype, 0)) < 0) {
1003 if (debug)
1004 fprintf(stderr, "socket failed on %s/%s: %s\n",
1005 sep->se_service, sep->se_proto, strerror(errno));
1006 syslog(LOG_ERR, "%s/%s: socket: %m",
1007 sep->se_service, sep->se_proto);
1008 return;
1009 }
1010 /* Set all listening sockets to non-blocking and close-on-exec. */
1011 if (fcntl(sep->se_fd, F_SETFL, O_NONBLOCK) < 0)
1012 syslog(LOG_ERR, "fcntl (F_SETFL, O_NONBLOCK): %m");
1013 if (fcntl(sep->se_fd, F_SETFD, FD_CLOEXEC) < 0)
1014 syslog(LOG_ERR, "fcntl (F_SETFD, FD_CLOEXEC): %m");
1015
1016 #define turnon(fd, opt) \
1017 setsockopt(fd, SOL_SOCKET, opt, (char *)&on, sizeof (on))
1018 if (strcmp(sep->se_proto, "tcp") == 0 && (options & SO_DEBUG) &&
1019 turnon(sep->se_fd, SO_DEBUG) < 0)
1020 syslog(LOG_ERR, "setsockopt (SO_DEBUG): %m");
1021 if (turnon(sep->se_fd, SO_REUSEADDR) < 0)
1022 syslog(LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
1023 #undef turnon
1024
1025 /* Set the socket buffer sizes, if specified. */
1026 if (sep->se_sndbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
1027 SO_SNDBUF, (char *)&sep->se_sndbuf, sizeof(sep->se_sndbuf)) < 0)
1028 syslog(LOG_ERR, "setsockopt (SO_SNDBUF %d): %m",
1029 sep->se_sndbuf);
1030 if (sep->se_rcvbuf != 0 && setsockopt(sep->se_fd, SOL_SOCKET,
1031 SO_RCVBUF, (char *)&sep->se_rcvbuf, sizeof(sep->se_rcvbuf)) < 0)
1032 syslog(LOG_ERR, "setsockopt (SO_RCVBUF %d): %m",
1033 sep->se_rcvbuf);
1034
1035 if (bind(sep->se_fd, &sep->se_ctrladdr, sep->se_ctrladdr_size) < 0) {
1036 if (debug)
1037 fprintf(stderr, "bind failed on %s/%s: %s\n",
1038 sep->se_service, sep->se_proto, strerror(errno));
1039 syslog(LOG_ERR, "%s/%s: bind: %m",
1040 sep->se_service, sep->se_proto);
1041 (void) close(sep->se_fd);
1042 sep->se_fd = -1;
1043 if (!timingout) {
1044 timingout = 1;
1045 alarm(RETRYTIME);
1046 }
1047 return;
1048 }
1049 if (sep->se_socktype == SOCK_STREAM)
1050 listen(sep->se_fd, 10);
1051
1052 FD_SET(sep->se_fd, &allsock);
1053 nsock++;
1054 if (sep->se_fd > maxsock) {
1055 maxsock = sep->se_fd;
1056 if (maxsock > rlim_ofile_cur - FD_MARGIN)
1057 bump_nofile();
1058 }
1059 if (debug)
1060 fprintf(stderr, "registered %s on %d\n",
1061 sep->se_server, sep->se_fd);
1062 }
1063
1064 /*
1065 * Finish with a service and its socket.
1066 */
1067 void
1068 close_sep(sep)
1069 struct servtab *sep;
1070 {
1071 if (sep->se_fd >= 0) {
1072 nsock--;
1073 FD_CLR(sep->se_fd, &allsock);
1074 (void) close(sep->se_fd);
1075 sep->se_fd = -1;
1076 }
1077 sep->se_count = 0;
1078 /*
1079 * Don't keep the pid of this running deamon: when reapchild()
1080 * reaps this pid, it would erroneously increment nsock.
1081 */
1082 if (sep->se_wait > 1)
1083 sep->se_wait = 1;
1084 }
1085
1086 void
1087 register_rpc(sep)
1088 struct servtab *sep;
1089 {
1090 #ifdef RPC
1091 int n;
1092 struct sockaddr_in sin;
1093 struct protoent *pp;
1094
1095 if ((pp = getprotobyname(sep->se_proto+4)) == NULL) {
1096 syslog(LOG_ERR, "%s: getproto: %m",
1097 sep->se_proto);
1098 return;
1099 }
1100 n = sizeof sin;
1101 if (getsockname(sep->se_fd, (struct sockaddr *)&sin, &n) < 0) {
1102 syslog(LOG_ERR, "%s/%s: getsockname: %m",
1103 sep->se_service, sep->se_proto);
1104 return;
1105 }
1106
1107 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
1108 if (debug)
1109 fprintf(stderr, "pmap_set: %u %u %u %u\n",
1110 sep->se_rpcprog, n, pp->p_proto,
1111 ntohs(sin.sin_port));
1112 (void)pmap_unset(sep->se_rpcprog, n);
1113 if (!pmap_set(sep->se_rpcprog, n, pp->p_proto, ntohs(sin.sin_port)))
1114 syslog(LOG_ERR, "pmap_set: %u %u %u %u: %m",
1115 sep->se_rpcprog, n, pp->p_proto,
1116 ntohs(sin.sin_port));
1117 }
1118 #endif /* RPC */
1119 }
1120
1121 void
1122 unregister_rpc(sep)
1123 struct servtab *sep;
1124 {
1125 #ifdef RPC
1126 int n;
1127
1128 for (n = sep->se_rpcversl; n <= sep->se_rpcversh; n++) {
1129 if (debug)
1130 fprintf(stderr, "pmap_unset(%u, %u)\n",
1131 sep->se_rpcprog, n);
1132 if (!pmap_unset(sep->se_rpcprog, n))
1133 syslog(LOG_ERR, "pmap_unset(%u, %u)\n",
1134 sep->se_rpcprog, n);
1135 }
1136 #endif /* RPC */
1137 }
1138
1139
1140 struct servtab *
1141 enter(cp)
1142 struct servtab *cp;
1143 {
1144 struct servtab *sep;
1145 long omask;
1146
1147 sep = (struct servtab *)malloc(sizeof (*sep));
1148 if (sep == (struct servtab *)0) {
1149 syslog(LOG_ERR, "Out of memory.");
1150 exit(-1);
1151 }
1152 *sep = *cp;
1153 sep->se_fd = -1;
1154 sep->se_rpcprog = -1;
1155 omask = sigblock(SIGBLOCK);
1156 sep->se_next = servtab;
1157 servtab = sep;
1158 sigsetmask(omask);
1159 return (sep);
1160 }
1161
1162 FILE *fconfig = NULL;
1163 struct servtab serv;
1164 char line[LINE_MAX];
1165 char *defhost;
1166
1167 int
1168 setconfig()
1169 {
1170 if (defhost) free(defhost);
1171 defhost = newstr("*");
1172 if (fconfig != NULL) {
1173 fseek(fconfig, 0L, SEEK_SET);
1174 return (1);
1175 }
1176 fconfig = fopen(CONFIG, "r");
1177 return (fconfig != NULL);
1178 }
1179
1180 void
1181 endconfig()
1182 {
1183 if (fconfig) {
1184 (void) fclose(fconfig);
1185 fconfig = NULL;
1186 }
1187 if (defhost) {
1188 free(defhost);
1189 defhost = 0;
1190 }
1191 }
1192
1193 struct servtab *
1194 getconfigent()
1195 {
1196 struct servtab *sep = &serv;
1197 int argc, val;
1198 char *cp, *cp0, *arg, *buf0, *buf1, *sz0, *sz1;
1199 static char TCPMUX_TOKEN[] = "tcpmux/";
1200 #define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
1201 char *hostdelim;
1202
1203 more:
1204 #ifdef MULOG
1205 while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0')) {
1206 /* Avoid use of `skip' if there is a danger of it looking
1207 * at continuation lines.
1208 */
1209 do {
1210 cp++;
1211 } while (*cp == ' ' || *cp == '\t');
1212 if (*cp == '\0')
1213 continue;
1214 if ((arg = skip(&cp)) == NULL)
1215 continue;
1216 if (strcmp(arg, "DOMAIN"))
1217 continue;
1218 if (curdom)
1219 free(curdom);
1220 curdom = NULL;
1221 while (*cp == ' ' || *cp == '\t')
1222 cp++;
1223 if (*cp == '\0')
1224 continue;
1225 arg = cp;
1226 while (*cp && *cp != ' ' && *cp != '\t')
1227 cp++;
1228 if (*cp != '\0')
1229 *cp++ = '\0';
1230 curdom = newstr(arg);
1231 }
1232 #else
1233 while ((cp = nextline(fconfig)) && (*cp == '#' || *cp == '\0'))
1234 ;
1235 #endif
1236 if (cp == NULL)
1237 return ((struct servtab *)0);
1238 /*
1239 * clear the static buffer, since some fields (se_ctrladdr,
1240 * for example) don't get initialized here.
1241 */
1242 memset((caddr_t)sep, 0, sizeof *sep);
1243 arg = skip(&cp);
1244 if (cp == NULL) {
1245 /* got an empty line containing just blanks/tabs. */
1246 goto more;
1247 }
1248 /* Check for a host name. */
1249 hostdelim = strrchr(arg, ':');
1250 if (hostdelim) {
1251 *hostdelim = '\0';
1252 sep->se_hostaddr = newstr(arg);
1253 arg = hostdelim + 1;
1254 /*
1255 * If the line is of the form `host:', then just change the
1256 * default host for the following lines.
1257 */
1258 if (*arg == '\0') {
1259 arg = skip(&cp);
1260 if (cp == NULL) {
1261 free(defhost);
1262 defhost = sep->se_hostaddr;
1263 goto more;
1264 }
1265 }
1266 } else
1267 sep->se_hostaddr = newstr(defhost);
1268 if (strncmp(arg, TCPMUX_TOKEN, MUX_LEN) == 0) {
1269 char *c = arg + MUX_LEN;
1270 if (*c == '+') {
1271 sep->se_type = MUXPLUS_TYPE;
1272 c++;
1273 } else
1274 sep->se_type = MUX_TYPE;
1275 sep->se_service = newstr(c);
1276 } else {
1277 sep->se_service = newstr(arg);
1278 sep->se_type = NORM_TYPE;
1279 }
1280
1281 arg = sskip(&cp);
1282 if (strcmp(arg, "stream") == 0)
1283 sep->se_socktype = SOCK_STREAM;
1284 else if (strcmp(arg, "dgram") == 0)
1285 sep->se_socktype = SOCK_DGRAM;
1286 else if (strcmp(arg, "rdm") == 0)
1287 sep->se_socktype = SOCK_RDM;
1288 else if (strcmp(arg, "seqpacket") == 0)
1289 sep->se_socktype = SOCK_SEQPACKET;
1290 else if (strcmp(arg, "raw") == 0)
1291 sep->se_socktype = SOCK_RAW;
1292 else
1293 sep->se_socktype = -1;
1294
1295 sep->se_proto = newstr(sskip(&cp));
1296
1297 #define MALFORMED(arg) \
1298 do { \
1299 syslog(LOG_ERR, "%s: malformed buffer size option `%s'", \
1300 sep->se_service, (arg)); \
1301 goto more; \
1302 } while (0)
1303
1304 #define GETVAL(arg) \
1305 do { \
1306 if (!isdigit(*(arg))) \
1307 MALFORMED(arg); \
1308 val = strtol((arg), &cp0, 10); \
1309 if (cp0 != NULL) { \
1310 if (cp0[1] != '\0') \
1311 MALFORMED((arg)); \
1312 if (cp0[0] == 'k') \
1313 val *= 1024; \
1314 if (cp0[0] == 'm') \
1315 val *= 1024 * 1024; \
1316 } \
1317 if (val < 1) { \
1318 syslog(LOG_ERR, "%s: invalid buffer size `%s'", \
1319 sep->se_service, (arg)); \
1320 goto more; \
1321 } \
1322 } while (0)
1323
1324 #define ASSIGN(arg) \
1325 do { \
1326 if (strcmp((arg), "sndbuf") == 0) \
1327 sep->se_sndbuf = val; \
1328 else if (strcmp((arg), "rcvbuf") == 0) \
1329 sep->se_rcvbuf = val; \
1330 else \
1331 MALFORMED((arg)); \
1332 } while (0)
1333
1334 /*
1335 * Extract the send and receive buffer sizes before parsing
1336 * the protocol.
1337 */
1338 sep->se_sndbuf = sep->se_rcvbuf = 0;
1339 buf0 = buf1 = sz0 = sz1 = NULL;
1340 if ((buf0 = strchr(sep->se_proto, ',')) != NULL) {
1341 /* Not meaningful for Tcpmux services. */
1342 if (sep->se_type != NORM_TYPE) {
1343 syslog(LOG_ERR, "%s: can't specify buffer sizes for "
1344 "tcpmux services", sep->se_service);
1345 goto more;
1346 }
1347
1348 /* Skip the , */
1349 *buf0++ = '\0';
1350
1351 /* Check to see if another socket buffer size was specified. */
1352 if ((buf1 = strchr(buf0, ',')) != NULL) {
1353 /* Skip the , */
1354 *buf1++ = '\0';
1355
1356 /* Make sure a 3rd one wasn't specified. */
1357 if (strchr(buf1, ',') != NULL) {
1358 syslog(LOG_ERR, "%s: too many buffer sizes",
1359 sep->se_service);
1360 goto more;
1361 }
1362
1363 /* Locate the size. */
1364 if ((sz1 = strchr(buf1, '=')) == NULL)
1365 MALFORMED(buf1);
1366
1367 /* Skip the = */
1368 *sz1++ = '\0';
1369 }
1370
1371 /* Locate the size. */
1372 if ((sz0 = strchr(buf0, '=')) == NULL)
1373 MALFORMED(buf0);
1374
1375 /* Skip the = */
1376 *sz0++ = '\0';
1377
1378 GETVAL(sz0);
1379 ASSIGN(buf0);
1380
1381 if (buf1 != NULL) {
1382 GETVAL(sz1);
1383 ASSIGN(buf1);
1384 }
1385 }
1386
1387 #undef ASSIGN
1388 #undef GETVAL
1389 #undef MALFORMED
1390
1391 if (strcmp(sep->se_proto, "unix") == 0) {
1392 sep->se_family = AF_LOCAL;
1393 } else {
1394 sep->se_family = AF_INET;
1395 if (strncmp(sep->se_proto, "rpc/", 4) == 0) {
1396 #ifdef RPC
1397 char *cp, *ccp;
1398 cp = strchr(sep->se_service, '/');
1399 if (cp == 0) {
1400 syslog(LOG_ERR, "%s: no rpc version",
1401 sep->se_service);
1402 goto more;
1403 }
1404 *cp++ = '\0';
1405 sep->se_rpcversl = sep->se_rpcversh =
1406 strtol(cp, &ccp, 0);
1407 if (ccp == cp) {
1408 badafterall:
1409 syslog(LOG_ERR, "%s/%s: bad rpc version",
1410 sep->se_service, cp);
1411 goto more;
1412 }
1413 if (*ccp == '-') {
1414 cp = ccp + 1;
1415 sep->se_rpcversh = strtol(cp, &ccp, 0);
1416 if (ccp == cp)
1417 goto badafterall;
1418 }
1419 #else
1420 syslog(LOG_ERR, "%s: rpc services not suported",
1421 sep->se_service);
1422 goto more;
1423 #endif /* RPC */
1424 }
1425 }
1426 arg = sskip(&cp);
1427 {
1428 char *cp;
1429 cp = strchr(arg, '.');
1430 if (cp) {
1431 *cp++ = '\0';
1432 sep->se_max = atoi(cp);
1433 } else
1434 sep->se_max = TOOMANY;
1435 }
1436 sep->se_wait = strcmp(arg, "wait") == 0;
1437 if (ISMUX(sep)) {
1438 /*
1439 * Silently enforce "nowait" for TCPMUX services since
1440 * they don't have an assigned port to listen on.
1441 */
1442 sep->se_wait = 0;
1443
1444 if (strcmp(sep->se_proto, "tcp")) {
1445 syslog(LOG_ERR,
1446 "%s: bad protocol for tcpmux service %s",
1447 CONFIG, sep->se_service);
1448 goto more;
1449 }
1450 if (sep->se_socktype != SOCK_STREAM) {
1451 syslog(LOG_ERR,
1452 "%s: bad socket type for tcpmux service %s",
1453 CONFIG, sep->se_service);
1454 goto more;
1455 }
1456 }
1457 sep->se_user = newstr(sskip(&cp));
1458 if ((sep->se_group = strchr(sep->se_user, '.')))
1459 *sep->se_group++ = '\0';
1460 sep->se_server = newstr(sskip(&cp));
1461 if (strcmp(sep->se_server, "internal") == 0) {
1462 struct biltin *bi;
1463
1464 for (bi = biltins; bi->bi_service; bi++)
1465 if (bi->bi_socktype == sep->se_socktype &&
1466 strcmp(bi->bi_service, sep->se_service) == 0)
1467 break;
1468 if (bi->bi_service == 0) {
1469 syslog(LOG_ERR, "internal service %s unknown",
1470 sep->se_service);
1471 goto more;
1472 }
1473 sep->se_bi = bi;
1474 sep->se_wait = bi->bi_wait;
1475 } else
1476 sep->se_bi = NULL;
1477 argc = 0;
1478 for (arg = skip(&cp); cp; arg = skip(&cp)) {
1479 #if MULOG
1480 char *colon;
1481
1482 if (argc == 0 && (colon = strrchr(arg, ':'))) {
1483 while (arg < colon) {
1484 int x;
1485 char *ccp;
1486
1487 switch (*arg++) {
1488 case 'l':
1489 x = 1;
1490 if (isdigit(*arg)) {
1491 x = strtol(arg, &ccp, 0);
1492 if (ccp == arg)
1493 break;
1494 arg = ccp;
1495 }
1496 sep->se_log &= ~MULOG_RFC931;
1497 sep->se_log |= x;
1498 break;
1499 case 'a':
1500 sep->se_log |= MULOG_RFC931;
1501 break;
1502 default:
1503 break;
1504 }
1505 }
1506 arg = colon + 1;
1507 }
1508 #endif
1509 if (argc < MAXARGV)
1510 sep->se_argv[argc++] = newstr(arg);
1511 }
1512 while (argc <= MAXARGV)
1513 sep->se_argv[argc++] = NULL;
1514 return (sep);
1515 }
1516
1517 void
1518 freeconfig(cp)
1519 struct servtab *cp;
1520 {
1521 int i;
1522
1523 if (cp->se_hostaddr)
1524 free(cp->se_hostaddr);
1525 if (cp->se_service)
1526 free(cp->se_service);
1527 if (cp->se_proto)
1528 free(cp->se_proto);
1529 if (cp->se_user)
1530 free(cp->se_user);
1531 /* Note: se_group is part of the newstr'ed se_user */
1532 if (cp->se_server)
1533 free(cp->se_server);
1534 for (i = 0; i < MAXARGV; i++)
1535 if (cp->se_argv[i])
1536 free(cp->se_argv[i]);
1537 }
1538
1539
1540 /*
1541 * Safe skip - if skip returns null, log a syntax error in the
1542 * configuration file and exit.
1543 */
1544 char *
1545 sskip(cpp)
1546 char **cpp;
1547 {
1548 char *cp;
1549
1550 cp = skip(cpp);
1551 if (cp == NULL) {
1552 syslog(LOG_ERR, "%s: syntax error", CONFIG);
1553 exit(-1);
1554 }
1555 return (cp);
1556 }
1557
1558 char *
1559 skip(cpp)
1560 char **cpp;
1561 {
1562 char *cp = *cpp;
1563 char *start;
1564
1565 if (*cpp == NULL)
1566 return ((char *)0);
1567
1568 again:
1569 while (*cp == ' ' || *cp == '\t')
1570 cp++;
1571 if (*cp == '\0') {
1572 int c;
1573
1574 c = getc(fconfig);
1575 (void) ungetc(c, fconfig);
1576 if (c == ' ' || c == '\t')
1577 if ((cp = nextline(fconfig)))
1578 goto again;
1579 *cpp = (char *)0;
1580 return ((char *)0);
1581 }
1582 start = cp;
1583 while (*cp && *cp != ' ' && *cp != '\t')
1584 cp++;
1585 if (*cp != '\0')
1586 *cp++ = '\0';
1587 *cpp = cp;
1588 return (start);
1589 }
1590
1591 char *
1592 nextline(fd)
1593 FILE *fd;
1594 {
1595 char *cp;
1596
1597 if (fgets(line, sizeof (line), fd) == NULL)
1598 return ((char *)0);
1599 cp = strchr(line, '\n');
1600 if (cp)
1601 *cp = '\0';
1602 return (line);
1603 }
1604
1605 char *
1606 newstr(cp)
1607 char *cp;
1608 {
1609 if ((cp = strdup(cp ? cp : "")))
1610 return (cp);
1611 syslog(LOG_ERR, "strdup: %m");
1612 exit(-1);
1613 }
1614
1615 void
1616 inetd_setproctitle(a, s)
1617 char *a;
1618 int s;
1619 {
1620 int size;
1621 char *cp;
1622 struct sockaddr_in sin;
1623 char buf[80];
1624
1625 cp = Argv[0];
1626 size = sizeof(sin);
1627 if (getpeername(s, (struct sockaddr *)&sin, &size) == 0)
1628 (void)snprintf(buf, sizeof buf, "-%s [%s]", a,
1629 inet_ntoa(sin.sin_addr));
1630 else
1631 (void)snprintf(buf, sizeof buf, "-%s", a);
1632 strncpy(cp, buf, LastArg - cp);
1633 cp += strlen(cp);
1634 while (cp < LastArg)
1635 *cp++ = ' ';
1636 }
1637
1638 void
1639 logpid()
1640 {
1641 FILE *fp;
1642
1643 if ((fp = fopen(_PATH_INETDPID, "w")) != NULL) {
1644 fprintf(fp, "%u\n", getpid());
1645 (void)fclose(fp);
1646 }
1647 }
1648
1649 void
1650 bump_nofile()
1651 {
1652 #ifdef RLIMIT_NOFILE
1653
1654 #define FD_CHUNK 32
1655
1656 struct rlimit rl;
1657
1658 if (getrlimit(RLIMIT_NOFILE, &rl) < 0) {
1659 syslog(LOG_ERR, "getrlimit: %m");
1660 return;
1661 }
1662 rl.rlim_cur = MIN(rl.rlim_max, rl.rlim_cur + FD_CHUNK);
1663 if (rl.rlim_cur <= rlim_ofile_cur) {
1664 syslog(LOG_ERR,
1665 "bump_nofile: cannot extend file limit, max = %d",
1666 (int)rl.rlim_cur);
1667 return;
1668 }
1669
1670 if (setrlimit(RLIMIT_NOFILE, &rl) < 0) {
1671 syslog(LOG_ERR, "setrlimit: %m");
1672 return;
1673 }
1674
1675 rlim_ofile_cur = rl.rlim_cur;
1676 return;
1677
1678 #else
1679 syslog(LOG_ERR, "bump_nofile: cannot extend file limit");
1680 return;
1681 #endif
1682 }
1683
1684 /*
1685 * Internet services provided internally by inetd:
1686 */
1687 #define BUFSIZE 4096
1688
1689 /* ARGSUSED */
1690 void
1691 echo_stream(s, sep) /* Echo service -- echo data back */
1692 int s;
1693 struct servtab *sep;
1694 {
1695 char buffer[BUFSIZE];
1696 int i;
1697
1698 inetd_setproctitle(sep->se_service, s);
1699 while ((i = read(s, buffer, sizeof(buffer))) > 0 &&
1700 write(s, buffer, i) > 0)
1701 ;
1702 }
1703
1704 /* ARGSUSED */
1705 void
1706 echo_dg(s, sep) /* Echo service -- echo data back */
1707 int s;
1708 struct servtab *sep;
1709 {
1710 char buffer[BUFSIZE];
1711 int i, size;
1712 struct sockaddr sa;
1713
1714 size = sizeof(sa);
1715 if ((i = recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size)) < 0)
1716 return;
1717 (void) sendto(s, buffer, i, 0, &sa, sizeof(sa));
1718 }
1719
1720 /* ARGSUSED */
1721 void
1722 discard_stream(s, sep) /* Discard service -- ignore data */
1723 int s;
1724 struct servtab *sep;
1725 {
1726 char buffer[BUFSIZE];
1727
1728 inetd_setproctitle(sep->se_service, s);
1729 while ((errno = 0, read(s, buffer, sizeof(buffer)) > 0) ||
1730 errno == EINTR)
1731 ;
1732 }
1733
1734 /* ARGSUSED */
1735 void
1736 discard_dg(s, sep) /* Discard service -- ignore data */
1737 int s;
1738 struct servtab *sep;
1739 {
1740 char buffer[BUFSIZE];
1741
1742 (void) read(s, buffer, sizeof(buffer));
1743 }
1744
1745 #include <ctype.h>
1746 #define LINESIZ 72
1747 char ring[128];
1748 char *endring;
1749
1750 void
1751 initring()
1752 {
1753 int i;
1754
1755 endring = ring;
1756
1757 for (i = 0; i <= 128; ++i)
1758 if (isprint(i))
1759 *endring++ = i;
1760 }
1761
1762 /* ARGSUSED */
1763 void
1764 chargen_stream(s, sep) /* Character generator */
1765 int s;
1766 struct servtab *sep;
1767 {
1768 int len;
1769 char *rs, text[LINESIZ+2];
1770
1771 inetd_setproctitle(sep->se_service, s);
1772
1773 if (!endring) {
1774 initring();
1775 rs = ring;
1776 }
1777
1778 text[LINESIZ] = '\r';
1779 text[LINESIZ + 1] = '\n';
1780 for (rs = ring;;) {
1781 if ((len = endring - rs) >= LINESIZ)
1782 memmove(text, rs, LINESIZ);
1783 else {
1784 memmove(text, rs, len);
1785 memmove(text + len, ring, LINESIZ - len);
1786 }
1787 if (++rs == endring)
1788 rs = ring;
1789 if (write(s, text, sizeof(text)) != sizeof(text))
1790 break;
1791 }
1792 }
1793
1794 /* ARGSUSED */
1795 void
1796 chargen_dg(s, sep) /* Character generator */
1797 int s;
1798 struct servtab *sep;
1799 {
1800 struct sockaddr sa;
1801 static char *rs;
1802 int len, size;
1803 char text[LINESIZ+2];
1804
1805 if (endring == 0) {
1806 initring();
1807 rs = ring;
1808 }
1809
1810 size = sizeof(sa);
1811 if (recvfrom(s, text, sizeof(text), 0, &sa, &size) < 0)
1812 return;
1813
1814 if ((len = endring - rs) >= LINESIZ)
1815 memmove(text, rs, LINESIZ);
1816 else {
1817 memmove(text, rs, len);
1818 memmove(text + len, ring, LINESIZ - len);
1819 }
1820 if (++rs == endring)
1821 rs = ring;
1822 text[LINESIZ] = '\r';
1823 text[LINESIZ + 1] = '\n';
1824 (void) sendto(s, text, sizeof(text), 0, &sa, sizeof(sa));
1825 }
1826
1827 /*
1828 * Return a machine readable date and time, in the form of the
1829 * number of seconds since midnight, Jan 1, 1900. Since gettimeofday
1830 * returns the number of seconds since midnight, Jan 1, 1970,
1831 * we must add 2208988800 seconds to this figure to make up for
1832 * some seventy years Bell Labs was asleep.
1833 */
1834
1835 long
1836 machtime()
1837 {
1838 struct timeval tv;
1839
1840 if (gettimeofday(&tv, (struct timezone *)0) < 0) {
1841 if (debug)
1842 fprintf(stderr, "Unable to get time of day\n");
1843 return (0L);
1844 }
1845 #define OFFSET ((u_long)25567 * 24*60*60)
1846 return (htonl((long)(tv.tv_sec + OFFSET)));
1847 #undef OFFSET
1848 }
1849
1850 /* ARGSUSED */
1851 void
1852 machtime_stream(s, sep)
1853 int s;
1854 struct servtab *sep;
1855 {
1856 long result;
1857
1858 result = machtime();
1859 (void) write(s, (char *) &result, sizeof(result));
1860 }
1861
1862 /* ARGSUSED */
1863 void
1864 machtime_dg(s, sep)
1865 int s;
1866 struct servtab *sep;
1867 {
1868 long result;
1869 struct sockaddr sa;
1870 int size;
1871
1872 size = sizeof(sa);
1873 if (recvfrom(s, (char *)&result, sizeof(result), 0, &sa, &size) < 0)
1874 return;
1875 result = machtime();
1876 (void) sendto(s, (char *) &result, sizeof(result), 0, &sa, sizeof(sa));
1877 }
1878
1879 /* ARGSUSED */
1880 void
1881 daytime_stream(s, sep) /* Return human-readable time of day */
1882 int s;
1883 struct servtab *sep;
1884 {
1885 char buffer[256];
1886 time_t clock;
1887 int len;
1888
1889 clock = time((time_t *) 0);
1890
1891 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
1892 (void) write(s, buffer, len);
1893 }
1894
1895 /* ARGSUSED */
1896 void
1897 daytime_dg(s, sep) /* Return human-readable time of day */
1898 int s;
1899 struct servtab *sep;
1900 {
1901 char buffer[256];
1902 time_t clock;
1903 struct sockaddr sa;
1904 int size, len;
1905
1906 clock = time((time_t *) 0);
1907
1908 size = sizeof(sa);
1909 if (recvfrom(s, buffer, sizeof(buffer), 0, &sa, &size) < 0)
1910 return;
1911 len = snprintf(buffer, sizeof buffer, "%.24s\r\n", ctime(&clock));
1912 (void) sendto(s, buffer, len, 0, &sa, sizeof(sa));
1913 }
1914
1915 /*
1916 * print_service:
1917 * Dump relevant information to stderr
1918 */
1919 void
1920 print_service(action, sep)
1921 char *action;
1922 struct servtab *sep;
1923 {
1924 if (isrpcservice(sep))
1925 fprintf(stderr,
1926 "%s: %s rpcprog=%d, rpcvers = %d/%d, proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n",
1927 action, sep->se_service,
1928 sep->se_rpcprog, sep->se_rpcversh, sep->se_rpcversl, sep->se_proto,
1929 sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
1930 (long)sep->se_bi, sep->se_server);
1931 else
1932 fprintf(stderr,
1933 "%s: %s proto=%s, wait.max=%d.%d, user.group=%s.%s builtin=%lx server=%s\n",
1934 action, sep->se_service, sep->se_proto,
1935 sep->se_wait, sep->se_max, sep->se_user, sep->se_group,
1936 (long)sep->se_bi, sep->se_server);
1937 }
1938
1939 void
1940 usage()
1941 {
1942
1943 #ifdef LIBWRAP
1944 (void)fprintf(stderr, "usage: %s [-dl] [conf]\n", __progname);
1945 #else
1946 (void)fprintf(stderr, "usage: %s [-d] [conf]\n", __progname);
1947 #endif
1948 exit(1);
1949 }
1950
1951
1952 /*
1953 * Based on TCPMUX.C by Mark K. Lottor November 1988
1954 * sri-nic::ps:<mkl>tcpmux.c
1955 */
1956
1957 static int /* # of characters upto \r,\n or \0 */
1958 getline(fd, buf, len)
1959 int fd;
1960 char *buf;
1961 int len;
1962 {
1963 int count = 0, n;
1964
1965 do {
1966 n = read(fd, buf, len-count);
1967 if (n == 0)
1968 return (count);
1969 if (n < 0)
1970 return (-1);
1971 while (--n >= 0) {
1972 if (*buf == '\r' || *buf == '\n' || *buf == '\0')
1973 return (count);
1974 count++;
1975 buf++;
1976 }
1977 } while (count < len);
1978 return (count);
1979 }
1980
1981 #define MAX_SERV_LEN (256+2) /* 2 bytes for \r\n */
1982
1983 #define strwrite(fd, buf) (void) write(fd, buf, sizeof(buf)-1)
1984
1985 void
1986 tcpmux(ctrl, sep)
1987 int ctrl;
1988 struct servtab *sep;
1989 {
1990 char service[MAX_SERV_LEN+1];
1991 int len;
1992
1993 /* Get requested service name */
1994 if ((len = getline(ctrl, service, MAX_SERV_LEN)) < 0) {
1995 strwrite(ctrl, "-Error reading service name\r\n");
1996 goto reject;
1997 }
1998 service[len] = '\0';
1999
2000 if (debug)
2001 fprintf(stderr, "tcpmux: someone wants %s\n", service);
2002
2003 /*
2004 * Help is a required command, and lists available services,
2005 * one per line.
2006 */
2007 if (!strcasecmp(service, "help")) {
2008 strwrite(ctrl, "+Available services:\r\n");
2009 strwrite(ctrl, "help\r\n");
2010 for (sep = servtab; sep; sep = sep->se_next) {
2011 if (!ISMUX(sep))
2012 continue;
2013 (void)write(ctrl, sep->se_service,
2014 strlen(sep->se_service));
2015 strwrite(ctrl, "\r\n");
2016 }
2017 goto reject;
2018 }
2019
2020 /* Try matching a service in inetd.conf with the request */
2021 for (sep = servtab; sep; sep = sep->se_next) {
2022 if (!ISMUX(sep))
2023 continue;
2024 if (!strcasecmp(service, sep->se_service)) {
2025 if (ISMUXPLUS(sep))
2026 strwrite(ctrl, "+Go\r\n");
2027 run_service(ctrl, sep);
2028 return;
2029 }
2030 }
2031 strwrite(ctrl, "-Service not available\r\n");
2032 reject:
2033 _exit(1);
2034 }
2035
2036
2037 #ifdef MULOG
2038 dolog(sep, ctrl)
2039 struct servtab *sep;
2040 int ctrl;
2041 {
2042 struct sockaddr sa;
2043 struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
2044 int len = sizeof(sa);
2045 struct hostent *hp;
2046 char *host, *dp, buf[BUFSIZ], *rfc931_name();
2047 int connected = 1;
2048
2049 if (sep->se_family != AF_INET)
2050 return;
2051
2052 if (getpeername(ctrl, &sa, &len) < 0) {
2053 if (errno != ENOTCONN) {
2054 syslog(LOG_ERR, "getpeername: %m");
2055 return;
2056 }
2057 if (recvfrom(ctrl, buf, sizeof(buf), MSG_PEEK, &sa, &len) < 0) {
2058 syslog(LOG_ERR, "recvfrom: %m");
2059 return;
2060 }
2061 connected = 0;
2062 }
2063 if (sa.sa_family != AF_INET) {
2064 syslog(LOG_ERR, "unexpected address family %u", sa.sa_family);
2065 return;
2066 }
2067
2068 hp = gethostbyaddr((char *) &sin->sin_addr.s_addr,
2069 sizeof (sin->sin_addr.s_addr), AF_INET);
2070
2071 host = hp?hp->h_name:inet_ntoa(sin->sin_addr);
2072
2073 switch (sep->se_log & ~MULOG_RFC931) {
2074 case 0:
2075 return;
2076 case 1:
2077 if (curdom == NULL || *curdom == '\0')
2078 break;
2079 dp = host + strlen(host) - strlen(curdom);
2080 if (dp < host)
2081 break;
2082 if (debug)
2083 fprintf(stderr, "check \"%s\" against curdom \"%s\"\n",
2084 host, curdom);
2085 if (strcasecmp(dp, curdom) == 0)
2086 return;
2087 break;
2088 case 2:
2089 default:
2090 break;
2091 }
2092
2093 openlog("", LOG_NOWAIT, MULOG);
2094
2095 if (connected && (sep->se_log & MULOG_RFC931))
2096 syslog(LOG_INFO, "%s@%s wants %s",
2097 rfc931_name(sin, ctrl), host, sep->se_service);
2098 else
2099 syslog(LOG_INFO, "%s wants %s",
2100 host, sep->se_service);
2101 }
2102
2103 /*
2104 * From tcp_log by
2105 * Wietse Venema, Eindhoven University of Technology, The Netherlands.
2106 */
2107 #if 0
2108 static char sccsid[] = "@(#) rfc931.c 1.3 92/08/31 22:54:46";
2109 #endif
2110
2111 #include <setjmp.h>
2112
2113 #define RFC931_PORT 113 /* Semi-well-known port */
2114 #define TIMEOUT 4
2115 #define TIMEOUT2 10
2116
2117 static jmp_buf timebuf;
2118
2119 /* timeout - handle timeouts */
2120
2121 static void timeout(sig)
2122 int sig;
2123 {
2124 longjmp(timebuf, sig);
2125 }
2126
2127 /* rfc931_name - return remote user name */
2128
2129 char *
2130 rfc931_name(there, ctrl)
2131 struct sockaddr_in *there; /* remote link information */
2132 int ctrl;
2133 {
2134 struct sockaddr_in here; /* local link information */
2135 struct sockaddr_in sin; /* for talking to RFC931 daemon */
2136 int length;
2137 int s;
2138 unsigned remote;
2139 unsigned local;
2140 static char user[256]; /* XXX */
2141 char buf[256];
2142 char *cp;
2143 char *result = "USER_UNKNOWN";
2144 int len;
2145
2146 /* Find out local port number of our stdin. */
2147
2148 length = sizeof(here);
2149 if (getsockname(ctrl, (struct sockaddr *) &here, &length) == -1) {
2150 syslog(LOG_ERR, "getsockname: %m");
2151 return (result);
2152 }
2153 /* Set up timer so we won't get stuck. */
2154
2155 if ((s = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
2156 syslog(LOG_ERR, "socket: %m");
2157 return (result);
2158 }
2159
2160 sin = here;
2161 sin.sin_port = htons(0);
2162 if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
2163 syslog(LOG_ERR, "bind: %m");
2164 return (result);
2165 }
2166
2167 signal(SIGALRM, timeout);
2168 if (setjmp(timebuf)) {
2169 close(s); /* not: fclose(fp) */
2170 return (result);
2171 }
2172 alarm(TIMEOUT);
2173
2174 /* Connect to the RFC931 daemon. */
2175
2176 sin = *there;
2177 sin.sin_port = htons(RFC931_PORT);
2178 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == -1) {
2179 close(s);
2180 alarm(0);
2181 return (result);
2182 }
2183
2184 /* Query the RFC 931 server. Would 13-byte writes ever be broken up? */
2185 (void)snprintf(buf, sizeof buf, "%u,%u\r\n", ntohs(there->sin_port),
2186 ntohs(here.sin_port));
2187
2188
2189 for (len = 0, cp = buf; len < strlen(buf); ) {
2190 int n;
2191
2192 if ((n = write(s, cp, strlen(buf) - len)) == -1) {
2193 close(s);
2194 alarm(0);
2195 return (result);
2196 }
2197 cp += n;
2198 len += n;
2199 }
2200
2201 /* Read response */
2202 for (cp = buf; cp < buf + sizeof(buf) - 1; ) {
2203 char c;
2204 if (read(s, &c, 1) != 1) {
2205 close(s);
2206 alarm(0);
2207 return (result);
2208 }
2209 if (c == '\n')
2210 break;
2211 *cp++ = c;
2212 }
2213 *cp = '\0';
2214
2215 if (sscanf(buf, "%u , %u : USERID :%*[^:]:%255s", &remote, &local, user) == 3
2216 && ntohs(there->sin_port) == remote
2217 && ntohs(here.sin_port) == local) {
2218
2219 /* Strip trailing carriage return. */
2220 if (cp = strchr(user, '\r'))
2221 *cp = 0;
2222 result = user;
2223 }
2224
2225 alarm(0);
2226 close(s);
2227 return (result);
2228 }
2229 #endif
2230