-
Notifications
You must be signed in to change notification settings - Fork 2
/
inetd.c
2137 lines (1918 loc) · 50.6 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
/*
Copyright (C) 1994-2021 Free Software Foundation, Inc.
This file is part of GNU Inetutils.
GNU Inetutils is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Inetutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see `http://www.gnu.org/licenses/'. */
/*
* 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.
*/
/*
* 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 stream/dgram/raw/rdm/seqpacket
* protocol must be in /etc/protocols
* wait/nowait[.max] single-threaded/multi-threaded
* [with an optional fork limit]
* user[:group] or user[.group] user (and group) to run daemon as
* server program full path name
* server program arguments arguments starting with argv[0]
*
* 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.
*/
#include <config.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <time.h>
#ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include <argp.h>
#include <argp-version-etc.h>
#include <progname.h>
#include <sys/select.h>
#include <grp.h>
#include "libinetutils.h"
#include "argcv.h"
#include "version-etc.h"
#include "attribute.h"
#ifndef EAI_ADDRFAMILY
# define EAI_ADDRFAMILY 1
#endif
#define TOOMANY 1000 /* 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 */
#ifndef SIGCHLD
# define SIGCHLD SIGCLD
#endif
#define SIGBLOCK (sigmask(SIGCHLD)|sigmask(SIGHUP)|sigmask(SIGALRM))
bool debug = false;
int nsock, maxsock;
fd_set allsock;
int options;
int timingout;
unsigned toomany = TOOMANY;
char **Argv;
char *LastArg;
char **config_files;
static bool env_option = false; /* Set environment variables */
static bool resolve_option = false; /* Resolve IP addresses */
static bool pidfile_option = true; /* Record the PID in a file */
static const char *pid_file = PATH_INETDPID;
const char args_doc[] = "[CONF-FILE [CONF-DIR]]...";
const char doc[] = "Internet super-server.";
/* Define keys for long options that do not have short counterparts. */
enum {
OPT_ENVIRON = 256,
OPT_RESOLVE
};
const char *program_authors[] = {
"Alain Magloire", "Alfred M. Szmidt", "Debarshi Ray",
"Jakob 'sparky' Kaivo", "Jeff Bailey",
"Jeroen Dekkers", "Marcus Brinkmann", "Sergey Poznyakoff",
"others", NULL };
static struct argp_option argp_options[] = {
#define GRP 0
{"debug", 'd', NULL, 0,
"turn on debugging, run in foreground mode", GRP+1},
{"environment", OPT_ENVIRON, NULL, 0,
"pass local and remote socket information in environment variables", GRP+1},
{ "pidfile", 'p', "PIDFILE", OPTION_ARG_OPTIONAL,
"override pidfile (default: \"" PATH_INETDPID "\")",
GRP+1 },
{"rate", 'R', "NUMBER", 0,
"maximum invocation rate (per minute)", GRP+1},
{"resolve", OPT_RESOLVE, NULL, 0,
"resolve IP addresses when setting environment variables "
"(see --environment)", GRP+1},
#undef GRP
{NULL, 0, NULL, 0, NULL, 0}
};
static error_t
parse_opt (int key, char *arg,
struct argp_state *state MAYBE_UNUSED)
{
char *p;
int number;
switch (key)
{
case 'd':
debug = true;
options |= SO_DEBUG;
break;
case OPT_ENVIRON:
env_option = true;
break;
case 'p':
if (arg && strlen (arg))
pid_file = arg;
else
pidfile_option = false;
break;
case 'R':
number = strtol (arg, &p, 0);
if (number < 1 || *p)
syslog (LOG_ERR, "-R %s: bad value for service invocation rate", arg);
else
toomany = number;
break;
case OPT_RESOLVE:
resolve_option = true;
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static struct argp argp =
{argp_options, parse_opt, args_doc, doc, NULL, NULL, NULL};
struct servtab
{
const char *se_file;
int se_line;
char *se_node; /* node name */
char *se_service; /* name of service */
int se_socktype; /* type of socket to use */
char *se_proto; /* protocol used */
pid_t se_wait; /* single threaded server */
unsigned se_max; /* Maximum number of instances per CNT_INTVL */
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 */
char **se_argv; /* program arguments */
size_t se_argc; /* number of arguments */
int se_fd; /* open descriptor */
int se_type; /* type */
sa_family_t se_family; /* address family of the socket */
char se_v4mapped; /* 1 = accept v4mapped connection, 0 = don't */
struct sockaddr_storage se_ctrladdr; /* bound address */
socklen_t se_addrlen; /* exact address length in use */
unsigned se_refcnt;
unsigned 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 ISMUX(sep) (((sep)->se_type == MUX_TYPE) || \
((sep)->se_type == MUXPLUS_TYPE))
#define ISMUXPLUS(sep) ((sep)->se_type == MUXPLUS_TYPE)
/* Built-in services */
void chargen_dg (int, struct servtab *);
void chargen_stream (int, struct servtab *);
void daytime_dg (int, struct servtab *);
void daytime_stream (int, struct servtab *);
void discard_dg (int, struct servtab *);
void discard_stream (int, struct servtab *);
void echo_dg (int, struct servtab *);
void echo_stream (int, struct servtab *);
void machtime_dg (int, struct servtab *);
void machtime_stream (int, struct servtab *);
void tcpmux (int s, struct servtab *sep);
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 s, 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 1900 */
{"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}
};
#define NUMINT (sizeof(intab) / sizeof(struct inent))
struct biltin *
bi_lookup (const struct servtab *sep)
{
struct biltin *bi;
for (bi = biltins; bi->bi_service; bi++)
if (bi->bi_socktype == sep->se_socktype
&& strcmp (bi->bi_service, sep->se_service) == 0)
return bi;
return NULL;
}
/* Signal handling */
#if defined HAVE_SIGACTION
# define SIGSTATUS sigset_t
# define sigstatus_empty(s) sigemptyset(&s)
# define inetd_pause(s) sigsuspend(&s)
#else
# define SIGSTATUS long
# define sigstatus_empty(s) s = 0
# define inetd_pause(s) sigpause (s)
#endif
void
signal_set_handler (int signo, void (*handler) ())
{
#if defined HAVE_SIGACTION
struct sigaction sa;
memset ((char *) &sa, 0, sizeof (sa));
sigemptyset (&sa.sa_mask);
sigaddset (&sa.sa_mask, signo);
# ifdef SA_RESTART
sa.sa_flags = SA_RESTART;
# endif
sa.sa_handler = handler;
sigaction (signo, &sa, NULL);
#elif defined(HAVE_SIGVEC)
struct sigvec sv;
memset (&sv, 0, sizeof (sv));
sv.sv_mask = SIGBLOCK;
sv.sv_handler = handler;
sigvec (signo, &sv, NULL);
#else /* !HAVE_SIGVEC */
signal (signo, handler);
#endif /* HAVE_SIGACTION */
}
void
signal_block (SIGSTATUS * old_status)
{
#ifdef HAVE_SIGACTION
sigset_t sigs;
sigemptyset (&sigs);
sigaddset (&sigs, SIGCHLD);
sigaddset (&sigs, SIGHUP);
sigaddset (&sigs, SIGALRM);
sigprocmask (SIG_BLOCK, &sigs, old_status);
#else
long omask = sigblock (SIGBLOCK);
if (old_status)
*old_status = omask;
#endif
}
void
signal_unblock (SIGSTATUS * status)
{
#ifdef HAVE_SIGACTION
if (status)
sigprocmask (SIG_SETMASK, status, 0);
else
{
sigset_t empty;
sigemptyset (&empty);
sigprocmask (SIG_SETMASK, &empty, 0);
}
#else
sigsetmask (status ? *status : 0);
#endif
}
void
run_service (int ctrl, struct servtab *sep)
{
struct passwd *pwd;
struct group *grp = NULL;
char buf[50];
if (sep->se_bi)
{
(*sep->se_bi->bi_fn) (ctrl, sep);
}
else
{
if (debug)
fprintf (stderr, "%d execl %s\n", (int) getpid (), sep->se_server);
dup2 (ctrl, 0);
close (ctrl);
dup2 (0, 1);
dup2 (0, 2);
pwd = getpwnam (sep->se_user);
if (pwd == NULL)
{
syslog (LOG_ERR, "%s/%s: %s: No such user",
sep->se_service, sep->se_proto, sep->se_user);
if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof buf, 0);
_exit (EXIT_FAILURE);
}
if (sep->se_group && *sep->se_group)
{
grp = getgrnam (sep->se_group);
if (grp == NULL)
{
syslog (LOG_ERR, "%s/%s: %s: No such group",
sep->se_service, sep->se_proto, sep->se_group);
if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof buf, 0);
_exit (EXIT_FAILURE);
}
}
if (pwd->pw_uid)
{
if (grp && grp->gr_gid)
{
if (setgid (grp->gr_gid) < 0)
{
syslog (LOG_ERR, "%s: can't set gid %d: %m",
sep->se_service, grp->gr_gid);
_exit (EXIT_FAILURE);
}
}
else if (setgid (pwd->pw_gid) < 0)
{
syslog (LOG_ERR, "%s: can't set gid %d: %m",
sep->se_service, pwd->pw_gid);
_exit (EXIT_FAILURE);
}
#ifdef HAVE_INITGROUPS
initgroups (pwd->pw_name,
(grp && grp->gr_gid) ? grp->gr_gid : pwd->pw_gid);
#endif
if (setuid (pwd->pw_uid) < 0)
{
syslog (LOG_ERR, "%s: can't set uid %d: %m",
sep->se_service, pwd->pw_uid);
_exit (EXIT_FAILURE);
}
}
execv (sep->se_server, sep->se_argv);
if (sep->se_socktype != SOCK_STREAM)
recv (0, buf, sizeof buf, 0);
syslog (LOG_ERR, "cannot execute %s: %m", sep->se_server);
_exit (EXIT_FAILURE);
}
}
void
reapchild (int signo MAYBE_UNUSED)
{
int status;
pid_t pid;
struct servtab *sep;
for (;;)
{
#ifdef HAVE_WAIT3
pid = wait3 (&status, WNOHANG, NULL);
#else
pid = wait (&status);
#endif
if (pid <= 0)
break;
if (debug)
fprintf (stderr, "%d reaped, status %#x\n", (int) pid, status);
for (sep = servtab; sep; sep = sep->se_next)
if (sep->se_wait == pid)
{
if (status)
syslog (LOG_WARNING, "%s: exit status 0x%x",
sep->se_server, status);
if (debug)
fprintf (stderr, "restored %s, fd %d\n",
sep->se_service, sep->se_fd);
FD_SET (sep->se_fd, &allsock);
nsock++;
sep->se_wait = 1;
}
}
}
char *
newstr (const char *cp)
{
char *s;
s = strdup (cp ? cp : "");
if (s != NULL)
return s;
syslog (LOG_ERR, "strdup: %m");
exit (-1);
}
void
dupmem (void **pptr, size_t size)
{
void *ptr = malloc (size);
if (!ptr)
{
syslog (LOG_ERR, "dupmem: %m");
exit (-1);
}
memcpy (ptr, *pptr, size);
*pptr = ptr;
}
void
dupstr (char **pstr)
{
if (*pstr)
dupmem ((void**)pstr, strlen (*pstr) + 1);
}
/*
* print_service:
* Dump relevant information to stderr
*/
void
print_service (const char *action, struct servtab *sep)
{
fprintf (stderr,
"%s:%d: %s: %s:%s proto=%s, wait=%d, max=%u, "
"user=%s group=%s builtin=%s server=%s\n",
sep->se_file, sep->se_line,
action,
ISMUX (sep) ? (ISMUXPLUS (sep) ? "tcpmuxplus" : "tcpmux")
: (sep->se_node ? sep->se_node : "*"),
sep->se_service, sep->se_proto,
(int) sep->se_wait, sep->se_max,
sep->se_user, sep->se_group,
sep->se_bi ? sep->se_bi->bi_service : "no",
sep->se_server);
}
/* Configuration */
int
setup (struct servtab *sep)
{
int err;
int on = 1;
tryagain:
sep->se_fd = socket (sep->se_family, sep->se_socktype, 0);
if (sep->se_fd < 0)
{
/* If we don't support creating AF_INET6 sockets, create AF_INET
sockets. */
if (errno == EAFNOSUPPORT && sep->se_family == AF_INET6
&& sep->se_v4mapped)
{
/* Fall back to IPv4 silently. */
sep->se_family = AF_INET;
goto tryagain;
}
if (debug)
fprintf (stderr, "socket failed on %s/%s: %s\n",
sep->se_service, sep->se_proto, strerror (errno));
syslog (LOG_ERR, "%s/%s: socket: %m",
sep->se_service, sep->se_proto);
return 1;
}
#ifdef IPV6
if (sep->se_family == AF_INET6)
{
/* Reverse the value of SEP->se_v4mapped, since otherwise if
using `tcp6' as a protocol type, all connections will be
mapped to IPv6, and with `tcp6only', IPv4 gets mapped to
IPv6. */
int val = sep->se_v4mapped ? 0 : 1;
if (setsockopt (sep->se_fd, IPPROTO_IPV6, IPV6_V6ONLY,
(char *) &val, sizeof (val)) < 0)
syslog (LOG_ERR, "setsockopt (IPV6_V6ONLY): %m");
}
#endif
if (strncmp (sep->se_proto, "tcp", 3) == 0 && (options & SO_DEBUG))
{
if (setsockopt (sep->se_fd, SOL_SOCKET, SO_DEBUG,
(char *) &on, sizeof (on)) < 0
&& errno != EACCES) /* Ignore insufficient permission. */
syslog (LOG_ERR, "setsockopt (SO_DEBUG): %m");
}
err = setsockopt (sep->se_fd, SOL_SOCKET, SO_REUSEADDR,
(char *) &on, sizeof (on));
if (err < 0)
syslog (LOG_ERR, "setsockopt (SO_REUSEADDR): %m");
err = bind (sep->se_fd, (struct sockaddr *) &sep->se_ctrladdr,
sep->se_addrlen);
if (err < 0)
{
/* If we can't bind with AF_INET6 try again with AF_INET. */
if ((errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
&& sep->se_family == AF_INET6 && sep->se_v4mapped)
{
/* Fall back to IPv4 silently. */
sep->se_family = AF_INET;
close (sep->se_fd);
goto tryagain;
}
if (sep->se_node)
{
if (debug)
fprintf (stderr, "bind failed for %s %s/%s: %s\n",
sep->se_node, sep->se_service, sep->se_proto,
strerror (errno));
syslog (LOG_ERR,"%s %s/%s: bind: %m",
sep->se_node, sep->se_service, sep->se_proto);
}
else
{
if (debug)
fprintf (stderr, "bind failed for %s/%s: %s\n",
sep->se_service, sep->se_proto, strerror (errno));
syslog (LOG_ERR,"%s/%s: bind: %m", sep->se_service, sep->se_proto);
}
close (sep->se_fd);
sep->se_fd = -1;
if (!timingout)
{
timingout = 1;
alarm (RETRYTIME);
}
return 1;
}
return 0;
}
void
servent_setup (struct servtab *sep)
{
sep->se_checked = 1;
if (sep->se_fd == -1 && setup (sep) == 0)
{
if (sep->se_socktype == SOCK_STREAM)
listen (sep->se_fd, 10);
FD_SET (sep->se_fd, &allsock);
nsock++;
if (sep->se_fd > maxsock)
maxsock = sep->se_fd;
if (debug)
fprintf (stderr, "registered %s on %d\n", sep->se_server, sep->se_fd);
}
}
void
retry (int signo MAYBE_UNUSED)
{
struct servtab *sep;
timingout = 0;
for (sep = servtab; sep; sep = sep->se_next)
if (sep->se_fd == -1 && !ISMUX (sep))
setup (sep);
}
/*
* Finish with a service and its socket.
*/
void
close_sep (struct servtab *sep)
{
if (sep->se_fd >= 0)
{
nsock--;
FD_CLR (sep->se_fd, &allsock);
close (sep->se_fd);
sep->se_fd = -1;
}
sep->se_count = 0;
/*
* Don't keep the pid of this running deamon: when reapchild()
* reaps this pid, it would erroneously increment nsock.
*/
if (sep->se_wait > 1)
sep->se_wait = 1;
}
struct servtab *
enter (struct servtab *cp)
{
struct servtab *sep;
SIGSTATUS sigstatus;
size_t i;
/* Checking/Removing duplicates */
for (sep = servtab; sep; sep = sep->se_next)
if (memcmp (&sep->se_ctrladdr, &cp->se_ctrladdr,
sizeof (sep->se_ctrladdr)) == 0
&& strcmp (sep->se_service, cp->se_service) == 0
&& strcmp (sep->se_proto, cp->se_proto) == 0
&& ISMUX (sep) == ISMUX (cp))
break;
if (sep != 0)
{
signal_block (&sigstatus);
/*
* 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;
#define SWAP(a, b) { char *c = a; a = b; b = c; }
if (cp->se_user)
SWAP (sep->se_user, cp->se_user);
if (cp->se_group)
SWAP (sep->se_group, cp->se_group);
if (cp->se_server)
SWAP (sep->se_server, cp->se_server);
argcv_free (sep->se_argc, sep->se_argv);
sep->se_argc = cp->se_argc;
sep->se_argv = cp->se_argv;
cp->se_argc = 0;
cp->se_argv = NULL;
sep->se_checked = 1;
signal_unblock (&sigstatus);
if (debug)
print_service ("REDO", sep);
return sep;
}
if (debug)
print_service ("ADD ", cp);
sep = (struct servtab *) malloc (sizeof (*sep));
if (sep == NULL)
{
syslog (LOG_ERR, "Out of memory.");
exit (-1);
}
*sep = *cp;
dupstr (&sep->se_node);
dupstr (&sep->se_service);
dupstr (&sep->se_proto);
dupstr (&sep->se_user);
dupstr (&sep->se_group);
dupstr (&sep->se_server);
dupmem ((void**)&sep->se_argv, sep->se_argc * sizeof (sep->se_argv[0]));
for (i = 0; i < sep->se_argc; i++)
dupstr (&sep->se_argv[i]);
sep->se_fd = -1;
signal_block (&sigstatus);
sep->se_next = servtab;
servtab = sep;
signal_unblock (&sigstatus);
return sep;
}
#define IPV4_NUMCHARS ".0123456789"
#define IPV6_NUMCHARS ".:0123456789abcdefABCDEF"
int
inetd_getaddrinfo (struct servtab *sep, int proto, struct addrinfo **result)
{
struct addrinfo hints;
#ifdef IPV6
bool numeric_address = false;
/* In case a numerical address is supplied, which does not
apply to the indicated domain, a non-local resolver
will wait in vain until time out occurs, thus blocking.
Avoid this by falling back to numerical host resolving
when address string seems to be numerical. */
/* Purely numeric address? Separate criteria for IPv4 and IPv6,
since IPv6 allows hexadecimal coding and IPv4 mapping! */
if (sep->se_node
&& (strspn (sep->se_node, IPV4_NUMCHARS) == strlen (sep->se_node)
|| (strchr (sep->se_node, ':')
&& strspn (sep->se_node, IPV6_NUMCHARS)) ) )
numeric_address = true;
else
if (debug && sep->se_node)
fprintf (stderr, "Resolving address: %s\n", sep->se_node);
#endif /* IPV6 */
memset (&hints, 0, sizeof (hints));
hints.ai_flags = AI_PASSIVE;
#ifdef AI_V4MAPPED
if (sep->se_v4mapped && (sep->se_family != AF_INET))
hints.ai_flags |= AI_V4MAPPED;
#endif
#ifdef IPV6
if (numeric_address)
hints.ai_flags |= AI_NUMERICHOST;
#endif
hints.ai_family = sep->se_family;
hints.ai_socktype = sep->se_socktype;
hints.ai_protocol = proto;
return getaddrinfo (sep->se_node, sep->se_service, &hints, result);
}
int
expand_enter (struct servtab *sep)
{
int err;
struct addrinfo *result, *rp;
struct protoent *proto;
struct servtab *cp;
/* Make sure that tcp6 etc also work. */
if (strncmp (sep->se_proto, "tcp", 3) == 0)
proto = getprotobyname ("tcp");
else if (strncmp (sep->se_proto, "udp", 3) == 0)
proto = getprotobyname ("udp");
else
proto = getprotobyname (sep->se_proto);
if (!proto)
{
syslog (LOG_ERR, "%s: Unknown protocol", sep->se_proto);
return 1;
}
err = inetd_getaddrinfo (sep, proto->p_proto, &result);
#if IPV6
if (err == EAI_ADDRFAMILY
&& sep->se_family == AF_INET6 && sep->se_v4mapped)
{
/* Fall back to IPv4 silently. */
sep->se_family = AF_INET;
err = inetd_getaddrinfo (sep, proto->p_proto, &result);
}
#endif
if (err)
{
const char *errmsg;
if (err == EAI_SYSTEM)
errmsg = strerror (errno);
else
errmsg = gai_strerror (err);
if (sep->se_node)
{
if (debug)
fprintf (stderr, "resolution of %s %s/%s failed: %s\n",
sep->se_node, sep->se_service, sep->se_proto, errmsg);
syslog (LOG_ERR, "%s %s/%s: getaddrinfo: %s",
sep->se_node, sep->se_service, sep->se_proto, errmsg);
}
else
{
if (debug)
fprintf (stderr, "resolution of %s/%s failed: %s\n",
sep->se_service, sep->se_proto, errmsg);
syslog (LOG_ERR, "%s/%s: getaddrinfo: %s",
sep->se_service, sep->se_proto, errmsg);
}
return 1;
}
for (rp = result; rp != NULL; rp = rp->ai_next)
{
memset (&sep->se_ctrladdr, 0, sizeof (sep->se_ctrladdr));
memcpy (&sep->se_ctrladdr, rp->ai_addr, rp->ai_addrlen);
sep->se_addrlen = rp->ai_addrlen;
cp = enter (sep);
servent_setup (cp);
}
freeaddrinfo (result);
return 0;
}
/* Configuration parser */
char *global_serv_node;
char *serv_node;
size_t serv_node_offset;
char *linebuf = NULL;
size_t linebufsize = 0;
FILE *
setconfig (const char *file)
{
return fopen (file, "r");
}
void
endconfig (FILE *fconfig)
{
if (fconfig)
fclose (fconfig);
}
void
freeconfig (struct servtab *cp)
{
free (cp->se_node);
free (cp->se_service);
free (cp->se_proto);
free (cp->se_user);
free (cp->se_group);
free (cp->se_server);
argcv_free (cp->se_argc, cp->se_argv);
}
#define INETD_SERVICE 0 /* service name */
#define INETD_SOCKET 1 /* socket type */
#define INETD_PROTOCOL 2 /* protocol */
#define INETD_WAIT 3 /* wait/nowait */
#define INETD_USER 4 /* user */
#define INETD_SERVER_PATH 5 /* server program path */
#define INETD_SERVER_ARGS 6 /* server program arguments */
#define INETD_FIELDS_MIN 6 /* Minimum number of fields in entry */
struct servtab *
next_node_sep (struct servtab *sep)
{
if (serv_node)
{
size_t i = strcspn (serv_node + serv_node_offset, ",");
sep->se_node = malloc (i + 1);
if (!sep->se_node)
{
syslog (LOG_ERR, "malloc: %m");
exit (-1);
}
memcpy (sep->se_node, serv_node + serv_node_offset, i);
sep->se_node[i] = 0;
serv_node_offset += i;
if (serv_node[serv_node_offset])
serv_node_offset++;
else
{
free (serv_node);
serv_node = NULL;
}
}
return sep;
}
struct servtab *
getconfigent (FILE *fconfig, const char *file, size_t *line)
{
static struct servtab serv;
struct servtab *sep = &serv;
int argc = 0;
size_t i;
char **argv = NULL;
char *node, *service;
static char TCPMUX_TOKEN[] = "tcpmux/";
#define MUX_LEN (sizeof(TCPMUX_TOKEN)-1)
if (serv_node)
return next_node_sep (sep);