Initial import from FreeBSD RELENG_4:
[dragonfly.git] / crypto / kerberosIV / lib / roken / mini_inetd.c
1 /*
2  * Copyright (c) 1995 - 2000 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id: mini_inetd.c,v 1.18.2.1 2000/10/10 13:22:33 assar Exp $");
37 #endif
38
39 #include <stdio.h>
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_SYS_TYPES_H
45 #include <sys/types.h>
46 #endif
47 #ifdef HAVE_SYS_TIME_H
48 #include <sys/time.h>
49 #endif
50 #ifdef HAVE_SYS_SOCKET_H
51 #include <sys/socket.h>
52 #endif
53 #ifdef HAVE_NETINET_IN_H
54 #include <netinet/in.h>
55 #endif
56 #ifdef HAVE_NETINET_IN6_H
57 #include <netinet/in6.h>
58 #endif
59 #ifdef HAVE_NETINET6_IN6_H
60 #include <netinet6/in6.h>
61 #endif
62
63
64 #include <roken.h>
65
66 static int
67 listen_v4 (int port)
68 {
69      struct sockaddr_in sa;
70      int s;
71
72      s = socket(AF_INET, SOCK_STREAM, 0);
73      if(s < 0) {
74          if (errno == ENOSYS)
75              return -1;
76           perror("socket");
77           exit(1);
78      }
79      socket_set_reuseaddr (s, 1);
80      memset(&sa, 0, sizeof(sa));
81      sa.sin_family      = AF_INET;
82      sa.sin_port        = port;
83      sa.sin_addr.s_addr = INADDR_ANY;
84      if(bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0){
85           perror("bind");
86           exit(1);
87      }
88      if(listen(s, SOMAXCONN) < 0){
89           perror("listen");
90           exit(1);
91      }
92      return s;
93 }
94
95 #ifdef HAVE_IPV6
96 static int
97 listen_v6 (int port)
98 {
99      struct sockaddr_in6 sa;
100      int s;
101
102      s = socket(AF_INET6, SOCK_STREAM, 0);
103      if(s < 0) {
104          if (errno == ENOSYS)
105              return -1;
106          perror("socket");
107          exit(1);
108      }
109      socket_set_reuseaddr (s, 1);
110      memset(&sa, 0, sizeof(sa));
111      sa.sin6_family = AF_INET6;
112      sa.sin6_port   = port;
113      sa.sin6_addr   = in6addr_any;
114      if(bind(s, (struct sockaddr*)&sa, sizeof(sa)) < 0){
115           perror("bind");
116           exit(1);
117      }
118      if(listen(s, SOMAXCONN) < 0){
119           perror("listen");
120           exit(1);
121      }
122      return s;
123 }
124 #endif /* HAVE_IPV6 */
125
126 /*
127  * accept a connection on `s' and pretend it's served by inetd.
128  */
129
130 static void
131 accept_it (int s)
132 {
133     int s2;
134
135     s2 = accept(s, NULL, 0);
136     if(s2 < 0){
137         perror("accept");
138         exit(1);
139     }
140     close(s);
141     dup2(s2, STDIN_FILENO);
142     dup2(s2, STDOUT_FILENO);
143     /* dup2(s2, STDERR_FILENO); */
144     close(s2);
145 }
146
147 /*
148  * Listen on `port' emulating inetd.
149  */
150
151 void
152 mini_inetd (int port)
153 {
154     int ret;
155     int max_fd = -1;
156     int sock_v4 = -1;
157     int sock_v6 = -1;
158     fd_set orig_read_set, read_set;
159
160     FD_ZERO(&orig_read_set);
161
162     sock_v4 = listen_v4 (port);
163     if (sock_v4 >= 0) {
164         max_fd  = max(max_fd, sock_v4);
165         if (max_fd >= FD_SETSIZE)
166             errx (1, "fd too large");
167         FD_SET(sock_v4, &orig_read_set);
168     }
169 #ifdef HAVE_IPV6
170     sock_v6 = listen_v6 (port);
171     if (sock_v6 >= 0) {
172         max_fd  = max(max_fd, sock_v6);
173         if (max_fd >= FD_SETSIZE)
174             errx (1, "fd too large");
175         FD_SET(sock_v6, &orig_read_set);
176     }
177 #endif    
178
179     do {
180         read_set = orig_read_set;
181
182         ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
183         if (ret < 0 && ret != EINTR) {
184             perror ("select");
185             exit (1);
186         }
187     } while (ret <= 0);
188
189     if (sock_v4 > 0 && FD_ISSET (sock_v4, &read_set)) {
190         accept_it (sock_v4);
191         return;
192     }
193     if (sock_v6 > 0 && FD_ISSET (sock_v6, &read_set)) {
194         accept_it (sock_v6);
195         return;
196     }
197     abort ();
198 }