forked from NetBSD/src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinetd.c
2279 lines (2080 loc) · 54.9 KB
/
inetd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: inetd.c,v 1.126 2019/12/27 09:22:20 msaitoh Exp $ */
/*-
* Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center and by Matthias Scheler.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1983, 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1983, 1991, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#if 0
static char sccsid[] = "@(#)inetd.c 8.4 (Berkeley) 4/13/94";
#else
__RCSID("$NetBSD: inetd.c,v 1.126 2019/12/27 09:22:20 msaitoh Exp $");
#endif
#endif /* not lint */
/*
* Inetd - Internet super-server
*
* This program invokes all internet services as needed. Connection-oriented
* services are invoked each time a connection is made, by creating a process.
* This process is passed the connection as file descriptor 0 and is expected
* to do a getpeername to find out the source host and port.
*
* Datagram oriented services are invoked when a datagram
* arrives; a process is created and passed a pending message
* on file descriptor 0. Datagram servers may either connect
* to their peer, freeing up the original socket for inetd
* to receive further messages on, or ``take over the socket'',
* processing all arriving datagrams and, eventually, timing
* out. The first type of server is said to be ``multi-threaded'';
* the second type of server ``single-threaded''.
*
* Inetd uses a configuration file which is read at startup
* and, possibly, at some later time in response to a hangup signal.
* The configuration file is ``free format'' with fields given in the
* order shown below. Continuation lines for an entry must being with
* a space or tab. All fields must be present in each entry.
*
* service name must be in /etc/services or must
* name a tcpmux service
* socket type[:accf[,arg]] stream/dgram/raw/rdm/seqpacket,
only stream can name an accept filter
* protocol must be in /etc/protocols
* wait/nowait[:max] single-threaded/multi-threaded, max #
* user[:group] user/group to run daemon as
* server program full path name
* server program arguments maximum of MAXARGV (64)
*
* For RPC services
* service name/version must be in /etc/rpc
* socket type stream/dgram/raw/rdm/seqpacket
* protocol must be in /etc/protocols
* wait/nowait[:max] single-threaded/multi-threaded
* user[:group] user to run daemon as
* server program full path name
* server program arguments maximum of MAXARGV (64)
*
* For non-RPC services, the "service name" can be of the form
* hostaddress:servicename, in which case the hostaddress is used
* as the host portion of the address to listen on. If hostaddress
* consists of a single `*' character, INADDR_ANY is used.
*
* A line can also consist of just
* hostaddress:
* where hostaddress is as in the preceding paragraph. Such a line must
* have no further fields; the specified hostaddress is remembered and
* used for all further lines that have no hostaddress specified,
* until the next such line (or EOF). (This is why * is provided to
* allow explicit specification of INADDR_ANY.) A line
* *:
* is implicitly in effect at the beginning of the file.
*
* The hostaddress specifier may (and often will) contain dots;
* the service name must not.
*
* For RPC services, host-address specifiers are accepted and will
* work to some extent; however, because of limitations in the
* portmapper interface, it will not work to try to give more than
* one line for any given RPC service, even if the host-address
* specifiers are different.
*
* TCP services without official port numbers are handled with the
* RFC1078-based tcpmux internal service. Tcpmux listens on port 1 for
* requests. When a connection is made from a foreign host, the service
* requested is passed to tcpmux, which looks it up in the servtab list
* and returns the proper entry for the service. Tcpmux returns a
* negative reply if the service doesn't exist, otherwise the invoked
* server is expected to return the positive reply if the service type in
* inetd.conf file has the prefix "tcpmux/". If the service type has the
* prefix "tcpmux/+", tcpmux will return the positive reply for the
* process; this is for compatibility with older server code, and also
* allows you to invoke programs that use stdin/stdout without putting any
* special server code in them. Services that use tcpmux are "nowait"
* because they do not have a well-known port and hence cannot listen
* for new requests.
*
* Comment lines are indicated by a `#' in column 1.
*
* #ifdef IPSEC
* Comment lines that start with "#@" denote IPsec policy string, as described
* in ipsec_set_policy(3). This will affect all the following items in
* inetd.conf(8). To reset the policy, just use "#@" line. By default,
* there's no IPsec policy.
* #endif
*/
/*
* Here's the scoop concerning the user:group feature:
*
* 1) set-group-option off.
*
* a) user = root: NO setuid() or setgid() is done
*
* b) other: setuid()
* setgid(primary group as found in passwd)
* initgroups(name, primary group)
*
* 2) set-group-option on.
*
* a) user = root: NO setuid()
* setgid(specified group)
* NO initgroups()
*
* b) other: setuid()
* setgid(specified group)
* initgroups(name, specified group)
*
*/
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/event.h>
#ifndef NO_RPC
#define RPC
#endif
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifdef RPC
#include <rpc/rpc.h>
#include <rpc/rpcb_clnt.h>
#include <netconfig.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <util.h>
#include <ifaddrs.h>
#include "pathnames.h"
#ifdef IPSEC
#include <netipsec/ipsec.h>
#ifndef IPSEC_POLICY_IPSEC /* no ipsec support on old ipsec */
#undef IPSEC
#endif
#include "ipsec.h"
#endif
#ifdef LIBWRAP
# include <tcpd.h>
#ifndef LIBWRAP_ALLOW_FACILITY
# define LIBWRAP_ALLOW_FACILITY LOG_AUTH
#endif
#ifndef LIBWRAP_ALLOW_SEVERITY
# define LIBWRAP_ALLOW_SEVERITY LOG_INFO
#endif
#ifndef LIBWRAP_DENY_FACILITY
# define LIBWRAP_DENY_FACILITY LOG_AUTH
#endif
#ifndef LIBWRAP_DENY_SEVERITY
# define LIBWRAP_DENY_SEVERITY LOG_WARNING
#endif
int allow_severity = LIBWRAP_ALLOW_FACILITY|LIBWRAP_ALLOW_SEVERITY;
int deny_severity = LIBWRAP_DENY_FACILITY|LIBWRAP_DENY_SEVERITY;
#endif
#define TOOMANY 40 /* don't start more than TOOMANY */
#define CNT_INTVL 60 /* servers in CNT_INTVL sec. */
#define RETRYTIME (60*10) /* retry after bind or server fail */
#define A_CNT(a) (sizeof (a) / sizeof (a[0]))
int debug;
#ifdef LIBWRAP
int lflag;
#endif
int maxsock;
int kq;
int options;
int timingout;
const int niflags = NI_NUMERICHOST | NI_NUMERICSERV;
#ifndef OPEN_MAX
#define OPEN_MAX 64
#endif
/* Reserve some descriptors, 3 stdio + at least: 1 log, 1 conf. file */
#define FD_MARGIN (8)
rlim_t rlim_ofile_cur = OPEN_MAX;
struct rlimit rlim_ofile;
struct kevent changebuf[64];
size_t changes;
struct servtab {
char *se_hostaddr; /* host address to listen on */
char *se_service; /* name of service */
int se_socktype; /* type of socket to use */
int se_family; /* address family */
char *se_proto; /* protocol used */
int se_sndbuf; /* sndbuf size */
int se_rcvbuf; /* rcvbuf size */
int se_rpcprog; /* rpc program number */
int se_rpcversl; /* rpc program lowest version */
int se_rpcversh; /* rpc program highest version */
#define isrpcservice(sep) ((sep)->se_rpcversl != 0)
pid_t se_wait; /* single threaded server */
short se_checked; /* looked at during merge */
char *se_user; /* user name to run as */
char *se_group; /* group name to run as */
struct biltin *se_bi; /* if built-in, description */
char *se_server; /* server program */
#define MAXARGV 64
char *se_argv[MAXARGV+1]; /* program arguments */
#ifdef IPSEC
char *se_policy; /* IPsec poilcy string */
#endif
struct accept_filter_arg se_accf; /* accept filter for stream service */
int se_fd; /* open descriptor */
int se_type; /* type */
union {
struct sockaddr se_un_ctrladdr;
struct sockaddr_in se_un_ctrladdr_in;
struct sockaddr_in6 se_un_ctrladdr_in6;
struct sockaddr_un se_un_ctrladdr_un;
} se_un; /* bound address */
#define se_ctrladdr se_un.se_un_ctrladdr
#define se_ctrladdr_in se_un.se_un_ctrladdr_in
#define se_ctrladdr_un se_un.se_un_ctrladdr_un
int se_ctrladdr_size;
int se_max; /* max # of instances of this service */
int se_count; /* number started since se_time */
struct timeval se_time; /* start of se_count */
struct servtab *se_next;
} *servtab;
#define NORM_TYPE 0
#define MUX_TYPE 1
#define MUXPLUS_TYPE 2
#define FAITH_TYPE 3
#define ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \
((sep)->se_type == MUXPLUS_TYPE))
#define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE)
static void chargen_dg(int, struct servtab *);
static void chargen_stream(int, struct servtab *);
static void close_sep(struct servtab *);
static void config(void);
static void daytime_dg(int, struct servtab *);
static void daytime_stream(int, struct servtab *);
static void discard_dg(int, struct servtab *);
static void discard_stream(int, struct servtab *);
static void echo_dg(int, struct servtab *);
static void echo_stream(int, struct servtab *);
static void endconfig(void);
static struct servtab *enter(struct servtab *);
static void freeconfig(struct servtab *);
static struct servtab *getconfigent(void);
__dead static void goaway(void);
static void machtime_dg(int, struct servtab *);
static void machtime_stream(int, struct servtab *);
static char *newstr(const char *);
static char *nextline(FILE *);
static void print_service(const char *, struct servtab *);
static void reapchild(void);
static void retry(void);
static void run_service(int, struct servtab *, int);
static int setconfig(void);
static void setup(struct servtab *);
static char *sskip(char **);
static char *skip(char **);
static void tcpmux(int, struct servtab *);
__dead static void usage(void);
static void register_rpc(struct servtab *);
static void unregister_rpc(struct servtab *);
static void bump_nofile(void);
static void inetd_setproctitle(char *, int);
static void initring(void);
static uint32_t machtime(void);
static int port_good_dg(struct sockaddr *);
static int dg_broadcast(struct in_addr *);
static int my_kevent(const struct kevent *, size_t, struct kevent *,
size_t);
static struct kevent * allocchange(void);
static int get_line(int, char *, int);
static void spawn(struct servtab *, int);
struct biltin {
const char *bi_service; /* internally provided service name */
int bi_socktype; /* type of socket supported */
short bi_fork; /* 1 if should fork before call */
short bi_wait; /* 1 if should wait for child */
void (*bi_fn)(int, struct servtab *);
/* function which performs it */
} biltins[] = {
/* Echo received data */
{ "echo", SOCK_STREAM, 1, 0, echo_stream },
{ "echo", SOCK_DGRAM, 0, 0, echo_dg },
/* Internet /dev/null */
{ "discard", SOCK_STREAM, 1, 0, discard_stream },
{ "discard", SOCK_DGRAM, 0, 0, discard_dg },
/* Return 32 bit time since 1970 */
{ "time", SOCK_STREAM, 0, 0, machtime_stream },
{ "time", SOCK_DGRAM, 0, 0, machtime_dg },
/* Return human-readable time */
{ "daytime", SOCK_STREAM, 0, 0, daytime_stream },
{ "daytime", SOCK_DGRAM, 0, 0, daytime_dg },
/* Familiar character generator */
{ "chargen", SOCK_STREAM, 1, 0, chargen_stream },
{ "chargen", SOCK_DGRAM, 0, 0, chargen_dg },
{ "tcpmux", SOCK_STREAM, 1, 0, tcpmux },
{ NULL, 0, 0, 0, NULL }
};
/* list of "bad" ports. I.e. ports that are most obviously used for
* "cycling packets" denial of service attacks. See /etc/services.
* List must end with port number "0".
*/
u_int16_t bad_ports[] = { 7, 9, 13, 19, 37, 0 };
#define NUMINT (sizeof(intab) / sizeof(struct inent))
const char *CONFIG = _PATH_INETDCONF;
static int my_signals[] =
{ SIGALRM, SIGHUP, SIGCHLD, SIGTERM, SIGINT, SIGPIPE };
int
main(int argc, char *argv[])
{
int ch, n, reload = 1;
while ((ch = getopt(argc, argv,
#ifdef LIBWRAP
"dl"
#else
"d"
#endif
)) != -1)
switch(ch) {
case 'd':
debug = 1;
options |= SO_DEBUG;
break;
#ifdef LIBWRAP
case 'l':
lflag = 1;
break;
#endif
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc > 0)
CONFIG = argv[0];
if (!debug)
daemon(0, 0);
openlog("inetd", LOG_PID | LOG_NOWAIT, LOG_DAEMON);
pidfile(NULL);
kq = kqueue();
if (kq < 0) {
syslog(LOG_ERR, "kqueue: %m");
return (EXIT_FAILURE);
}
if (getrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0) {
syslog(LOG_ERR, "getrlimit: %m");
} else {
rlim_ofile_cur = rlim_ofile.rlim_cur;
if (rlim_ofile_cur == RLIM_INFINITY) /* ! */
rlim_ofile_cur = OPEN_MAX;
}
for (n = 0; n < (int)A_CNT(my_signals); n++) {
int signum;
signum = my_signals[n];
if (signum != SIGCHLD)
(void) signal(signum, SIG_IGN);
if (signum != SIGPIPE) {
struct kevent *ev;
ev = allocchange();
EV_SET(ev, signum, EVFILT_SIGNAL, EV_ADD | EV_ENABLE,
0, 0, 0);
}
}
for (;;) {
int ctrl;
struct kevent eventbuf[64], *ev;
struct servtab *sep;
if (reload) {
reload = 0;
config();
}
n = my_kevent(changebuf, changes, eventbuf, A_CNT(eventbuf));
changes = 0;
for (ev = eventbuf; n > 0; ev++, n--) {
if (ev->filter == EVFILT_SIGNAL) {
switch (ev->ident) {
case SIGALRM:
retry();
break;
case SIGCHLD:
reapchild();
break;
case SIGTERM:
case SIGINT:
goaway();
break;
case SIGHUP:
reload = 1;
break;
}
continue;
}
if (ev->filter != EVFILT_READ)
continue;
sep = (struct servtab *)ev->udata;
/* Paranoia */
if ((int)ev->ident != sep->se_fd)
continue;
if (debug)
fprintf(stderr, "someone wants %s\n",
sep->se_service);
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
/* XXX here do the libwrap check-before-accept*/
ctrl = accept(sep->se_fd, NULL, NULL);
if (debug)
fprintf(stderr, "accept, ctrl %d\n",
ctrl);
if (ctrl < 0) {
if (errno != EINTR)
syslog(LOG_WARNING,
"accept (for %s): %m",
sep->se_service);
continue;
}
} else
ctrl = sep->se_fd;
spawn(sep, ctrl);
}
}
}
static void
spawn(struct servtab *sep, int ctrl)
{
int dofork;
pid_t pid;
pid = 0;
#ifdef LIBWRAP_INTERNAL
dofork = 1;
#else
dofork = (sep->se_bi == 0 || sep->se_bi->bi_fork);
#endif
if (dofork) {
if (sep->se_count++ == 0)
(void)gettimeofday(&sep->se_time, NULL);
else if (sep->se_count >= sep->se_max) {
struct timeval now;
(void)gettimeofday(&now, NULL);
if (now.tv_sec - sep->se_time.tv_sec > CNT_INTVL) {
sep->se_time = now;
sep->se_count = 1;
} else {
syslog(LOG_ERR,
"%s/%s max spawn rate (%d in %d seconds) "
"exceeded; service not started",
sep->se_service, sep->se_proto,
sep->se_max, CNT_INTVL);
if (!sep->se_wait && sep->se_socktype ==
SOCK_STREAM)
close(ctrl);
close_sep(sep);
if (!timingout) {
timingout = 1;
alarm(RETRYTIME);
}
return;
}
}
pid = fork();
if (pid < 0) {
syslog(LOG_ERR, "fork: %m");
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close(ctrl);
sleep(1);
return;
}
if (pid != 0 && sep->se_wait) {
struct kevent *ev;
sep->se_wait = pid;
ev = allocchange();
EV_SET(ev, sep->se_fd, EVFILT_READ,
EV_DELETE, 0, 0, 0);
}
if (pid == 0) {
size_t n;
for (n = 0; n < A_CNT(my_signals); n++)
(void) signal(my_signals[n], SIG_DFL);
if (debug)
setsid();
}
}
if (pid == 0) {
run_service(ctrl, sep, dofork);
if (dofork)
exit(0);
}
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM)
close(ctrl);
}
static void
run_service(int ctrl, struct servtab *sep, int didfork)
{
struct passwd *pwd;
struct group *grp = NULL; /* XXX gcc */
char buf[NI_MAXSERV];
struct servtab *s;
#ifdef LIBWRAP
char abuf[BUFSIZ];
struct request_info req;
int denied;
char *service = NULL; /* XXX gcc */
#endif
#ifdef LIBWRAP
#ifndef LIBWRAP_INTERNAL
if (sep->se_bi == 0)
#endif
if (!sep->se_wait && sep->se_socktype == SOCK_STREAM) {
request_init(&req, RQ_DAEMON, sep->se_argv[0] ?
sep->se_argv[0] : sep->se_service, RQ_FILE, ctrl, NULL);
fromhost(&req);
denied = !hosts_access(&req);
if (denied || lflag) {
if (getnameinfo(&sep->se_ctrladdr,
(socklen_t)sep->se_ctrladdr.sa_len, NULL, 0,
buf, sizeof(buf), 0) != 0) {
/* shouldn't happen */
(void)snprintf(buf, sizeof buf, "%d",
ntohs(sep->se_ctrladdr_in.sin_port));
}
service = buf;
if (req.client->sin) {
sockaddr_snprintf(abuf, sizeof(abuf), "%a",
req.client->sin);
} else {
strcpy(abuf, "(null)");
}
}
if (denied) {
syslog(deny_severity,
"refused connection from %.500s(%s), service %s (%s)",
eval_client(&req), abuf, service, sep->se_proto);
goto reject;
}
if (lflag) {
syslog(allow_severity,
"connection from %.500s(%s), service %s (%s)",
eval_client(&req), abuf, service, sep->se_proto);
}
}
#endif /* LIBWRAP */
if (sep->se_bi) {
if (didfork) {
for (s = servtab; s; s = s->se_next)
if (s->se_fd != -1 && s->se_fd != ctrl) {
close(s->se_fd);
s->se_fd = -1;
}
}
(*sep->se_bi->bi_fn)(ctrl, sep);
} else {
if ((pwd = getpwnam(sep->se_user)) == NULL) {
syslog(LOG_ERR, "%s/%s: %s: No such user",
sep->se_service, sep->se_proto, sep->se_user);
goto reject;
}
if (sep->se_group &&
(grp = getgrnam(sep->se_group)) == NULL) {
syslog(LOG_ERR, "%s/%s: %s: No such group",
sep->se_service, sep->se_proto, sep->se_group);
goto reject;
}
if (pwd->pw_uid) {
if (sep->se_group)
pwd->pw_gid = grp->gr_gid;
if (setgid(pwd->pw_gid) < 0) {
syslog(LOG_ERR,
"%s/%s: can't set gid %d: %m", sep->se_service,
sep->se_proto, pwd->pw_gid);
goto reject;
}
(void) initgroups(pwd->pw_name,
pwd->pw_gid);
if (setuid(pwd->pw_uid) < 0) {
syslog(LOG_ERR,
"%s/%s: can't set uid %d: %m", sep->se_service,
sep->se_proto, pwd->pw_uid);
goto reject;
}
} else if (sep->se_group) {
(void) setgid((gid_t)grp->gr_gid);
}
if (debug)
fprintf(stderr, "%d execl %s\n",
getpid(), sep->se_server);
/* Set our control descriptor to not close-on-exec... */
if (fcntl(ctrl, F_SETFD, 0) < 0)
syslog(LOG_ERR, "fcntl (%d, F_SETFD, 0): %m", ctrl);
/* ...and dup it to stdin, stdout, and stderr. */
if (ctrl != 0) {
dup2(ctrl, 0);
close(ctrl);
ctrl = 0;
}
dup2(0, 1);
dup2(0, 2);
if (rlim_ofile.rlim_cur != rlim_ofile_cur &&
setrlimit(RLIMIT_NOFILE, &rlim_ofile) < 0)
syslog(LOG_ERR, "setrlimit: %m");
execv(sep->se_server, sep->se_argv);
syslog(LOG_ERR, "cannot execute %s: %m", sep->se_server);
reject:
if (sep->se_socktype != SOCK_STREAM)
recv(ctrl, buf, sizeof (buf), 0);
_exit(1);
}
}
static void
reapchild(void)
{
int status;
pid_t pid;
struct servtab *sep;
for (;;) {
pid = wait3(&status, WNOHANG, NULL);
if (pid <= 0)
break;
if (debug)
(void) fprintf(stderr, "%d reaped, status %#x\n",
pid, status);
for (sep = servtab; sep != NULL; sep = sep->se_next)
if (sep->se_wait == pid) {
struct kevent *ev;
if (WIFEXITED(status) && WEXITSTATUS(status))
syslog(LOG_WARNING,
"%s: exit status %u",
sep->se_server, WEXITSTATUS(status));
else if (WIFSIGNALED(status))
syslog(LOG_WARNING,
"%s: exit signal %u",
sep->se_server, WTERMSIG(status));
sep->se_wait = 1;
ev = allocchange();
EV_SET(ev, sep->se_fd, EVFILT_READ,
EV_ADD | EV_ENABLE, 0, 0, (intptr_t)sep);
if (debug)
fprintf(stderr, "restored %s, fd %d\n",
sep->se_service, sep->se_fd);
}
}
}
static void
config(void)
{
struct servtab *sep, *cp, **sepp;
size_t n;
if (!setconfig()) {
syslog(LOG_ERR, "%s: %m", CONFIG);
return;
}
for (sep = servtab; sep != NULL; sep = sep->se_next)
sep->se_checked = 0;
while ((cp = getconfigent()) != NULL) {
for (sep = servtab; sep != NULL; sep = sep->se_next)
if (strcmp(sep->se_service, cp->se_service) == 0 &&
strcmp(sep->se_hostaddr, cp->se_hostaddr) == 0 &&
strcmp(sep->se_proto, cp->se_proto) == 0 &&
ISMUX(sep) == ISMUX(cp))
break;
if (sep != NULL) {
int i;
#define SWAP(type, a, b) {type c = a; a = b; b = c;}
/*
* sep->se_wait may be holding the pid of a daemon
* that we're waiting for. If so, don't overwrite
* it unless the config file explicitly says don't
* wait.
*/
if (cp->se_bi == 0 &&
(sep->se_wait == 1 || cp->se_wait == 0))
sep->se_wait = cp->se_wait;
SWAP(char *, sep->se_user, cp->se_user);
SWAP(char *, sep->se_group, cp->se_group);
SWAP(char *, sep->se_server, cp->se_server);
for (i = 0; i < MAXARGV; i++)
SWAP(char *, sep->se_argv[i], cp->se_argv[i]);
#ifdef IPSEC
SWAP(char *, sep->se_policy, cp->se_policy);
#endif
SWAP(int, cp->se_type, sep->se_type);
SWAP(int, cp->se_max, sep->se_max);
#undef SWAP
if (isrpcservice(sep))
unregister_rpc(sep);
sep->se_rpcversl = cp->se_rpcversl;
sep->se_rpcversh = cp->se_rpcversh;
freeconfig(cp);
if (debug)
print_service("REDO", sep);
} else {
sep = enter(cp);
if (debug)
print_service("ADD ", sep);
}
sep->se_checked = 1;
switch (sep->se_family) {
case AF_LOCAL:
if (sep->se_fd != -1)
break;
n = strlen(sep->se_service);
if (n >= sizeof(sep->se_ctrladdr_un.sun_path)) {
syslog(LOG_ERR, "%s: address too long",
sep->se_service);
sep->se_checked = 0;
continue;
}
(void)unlink(sep->se_service);
strlcpy(sep->se_ctrladdr_un.sun_path,
sep->se_service, n + 1);
sep->se_ctrladdr_un.sun_family = AF_LOCAL;
sep->se_ctrladdr_size = (int)(n +
sizeof(sep->se_ctrladdr_un) -
sizeof(sep->se_ctrladdr_un.sun_path));
if (!ISMUX(sep))
setup(sep);
break;
case AF_INET:
#ifdef INET6
case AF_INET6:
#endif
{
struct addrinfo hints, *res;
char *host;
const char *port;
int error;
int s;
/* check if the family is supported */
s = socket(sep->se_family, SOCK_DGRAM, 0);
if (s < 0) {
syslog(LOG_WARNING,
"%s/%s: %s: the address family is not "
"supported by the kernel",
sep->se_service, sep->se_proto,
sep->se_hostaddr);
sep->se_checked = 0;
continue;
}
close(s);
memset(&hints, 0, sizeof(hints));
hints.ai_family = sep->se_family;
hints.ai_socktype = sep->se_socktype;
hints.ai_flags = AI_PASSIVE;
if (!strcmp(sep->se_hostaddr, "*"))
host = NULL;
else
host = sep->se_hostaddr;
if (isrpcservice(sep) || ISMUX(sep))
port = "0";
else
port = sep->se_service;
error = getaddrinfo(host, port, &hints, &res);
if (error) {
if (error == EAI_SERVICE) {
/* gai_strerror not friendly enough */
syslog(LOG_WARNING, "%s/%s: "
"unknown service",
sep->se_service, sep->se_proto);
} else {
syslog(LOG_ERR, "%s/%s: %s: %s",
sep->se_service, sep->se_proto,
sep->se_hostaddr,
gai_strerror(error));
}
sep->se_checked = 0;
continue;
}
if (res->ai_next) {
syslog(LOG_ERR,
"%s/%s: %s: resolved to multiple addr",
sep->se_service, sep->se_proto,
sep->se_hostaddr);
sep->se_checked = 0;
freeaddrinfo(res);
continue;
}
memcpy(&sep->se_ctrladdr, res->ai_addr,
res->ai_addrlen);
if (ISMUX(sep)) {
sep->se_fd = -1;
freeaddrinfo(res);
continue;
}
sep->se_ctrladdr_size = res->ai_addrlen;
freeaddrinfo(res);
#ifdef RPC
if (isrpcservice(sep)) {
struct rpcent *rp;
sep->se_rpcprog = atoi(sep->se_service);
if (sep->se_rpcprog == 0) {
rp = getrpcbyname(sep->se_service);
if (rp == 0) {
syslog(LOG_ERR,
"%s/%s: unknown service",
sep->se_service,
sep->se_proto);
sep->se_checked = 0;
continue;
}
sep->se_rpcprog = rp->r_number;
}
if (sep->se_fd == -1 && !ISMUX(sep))
setup(sep);
if (sep->se_fd != -1)
register_rpc(sep);
} else
#endif
{
if (sep->se_fd >= 0)
close_sep(sep);
if (sep->se_fd == -1 && !ISMUX(sep))
setup(sep);
}
}
}
}
endconfig();
/*
* Purge anything not looked at above.
*/
sepp = &servtab;
while ((sep = *sepp) != NULL) {
if (sep->se_checked) {
sepp = &sep->se_next;
continue;
}
*sepp = sep->se_next;
if (sep->se_fd >= 0)
close_sep(sep);
if (isrpcservice(sep))
unregister_rpc(sep);
if (sep->se_family == AF_LOCAL)
(void)unlink(sep->se_service);
if (debug)
print_service("FREE", sep);
freeconfig(sep);
free(sep);
}
}
static void
retry(void)
{
struct servtab *sep;
timingout = 0;
for (sep = servtab; sep != NULL; sep = sep->se_next) {
if (sep->se_fd == -1 && !ISMUX(sep)) {
switch (sep->se_family) {
case AF_LOCAL:
case AF_INET:
#ifdef INET6
case AF_INET6:
#endif