xref: /freebsd/crypto/heimdal/lib/roken/mini_inetd.c (revision 5e9cd1ae3e10592ed70e7575551cba1bbab04d84)
1b528cefcSMark Murray /*
213e3f4d6SMark Murray  * Copyright (c) 1995 - 2000 Kungliga Tekniska H�gskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #ifdef HAVE_CONFIG_H
35b528cefcSMark Murray #include <config.h>
365e9cd1aeSAssar Westerlund RCSID("$Id: mini_inetd.c,v 1.28 2000/10/08 13:38:47 assar Exp $");
37b528cefcSMark Murray #endif
38b528cefcSMark Murray 
39b528cefcSMark Murray #include <err.h>
405e9cd1aeSAssar Westerlund #include "roken.h"
41b528cefcSMark Murray 
42b528cefcSMark Murray /*
43b528cefcSMark Murray  * accept a connection on `s' and pretend it's served by inetd.
44b528cefcSMark Murray  */
45b528cefcSMark Murray 
46b528cefcSMark Murray static void
47b528cefcSMark Murray accept_it (int s)
48b528cefcSMark Murray {
49b528cefcSMark Murray     int s2;
50b528cefcSMark Murray 
515e9cd1aeSAssar Westerlund     s2 = accept(s, NULL, NULL);
52b528cefcSMark Murray     if(s2 < 0)
53b528cefcSMark Murray 	err (1, "accept");
54b528cefcSMark Murray     close(s);
55b528cefcSMark Murray     dup2(s2, STDIN_FILENO);
56b528cefcSMark Murray     dup2(s2, STDOUT_FILENO);
57b528cefcSMark Murray     /* dup2(s2, STDERR_FILENO); */
58b528cefcSMark Murray     close(s2);
59b528cefcSMark Murray }
60b528cefcSMark Murray 
61b528cefcSMark Murray /*
62b528cefcSMark Murray  * Listen on `port' emulating inetd.
63b528cefcSMark Murray  */
64b528cefcSMark Murray 
65b528cefcSMark Murray void
66b528cefcSMark Murray mini_inetd (int port)
67b528cefcSMark Murray {
68b528cefcSMark Murray     int error, ret;
69b528cefcSMark Murray     struct addrinfo *ai, *a, hints;
70b528cefcSMark Murray     char portstr[NI_MAXSERV];
7113e3f4d6SMark Murray     int n, nalloc, i;
72b528cefcSMark Murray     int *fds;
73b528cefcSMark Murray     fd_set orig_read_set, read_set;
74b528cefcSMark Murray     int max_fd = -1;
75b528cefcSMark Murray 
76b528cefcSMark Murray     memset (&hints, 0, sizeof(hints));
77b528cefcSMark Murray     hints.ai_flags    = AI_PASSIVE;
78b528cefcSMark Murray     hints.ai_socktype = SOCK_STREAM;
79b528cefcSMark Murray 
80b528cefcSMark Murray     snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
81b528cefcSMark Murray 
82b528cefcSMark Murray     error = getaddrinfo (NULL, portstr, &hints, &ai);
83b528cefcSMark Murray     if (error)
84b528cefcSMark Murray 	errx (1, "getaddrinfo: %s", gai_strerror (error));
85b528cefcSMark Murray 
8613e3f4d6SMark Murray     for (nalloc = 0, a = ai; a != NULL; a = a->ai_next)
8713e3f4d6SMark Murray 	++nalloc;
88b528cefcSMark Murray 
8913e3f4d6SMark Murray     fds = malloc (nalloc * sizeof(*fds));
90b528cefcSMark Murray     if (fds == NULL)
91b528cefcSMark Murray 	errx (1, "mini_inetd: out of memory");
92b528cefcSMark Murray 
93b528cefcSMark Murray     FD_ZERO(&orig_read_set);
94b528cefcSMark Murray 
9513e3f4d6SMark Murray     for (i = 0, a = ai; a != NULL; a = a->ai_next) {
96b528cefcSMark Murray 	fds[i] = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
9713e3f4d6SMark Murray 	if (fds[i] < 0) {
9813e3f4d6SMark Murray 	    warn ("socket");
9913e3f4d6SMark Murray 	    continue;
10013e3f4d6SMark Murray 	}
101b528cefcSMark Murray 	socket_set_reuseaddr (fds[i], 1);
102b528cefcSMark Murray 	if (bind (fds[i], a->ai_addr, a->ai_addrlen) < 0)
103b528cefcSMark Murray 	    err (1, "bind");
104b528cefcSMark Murray 	if (listen (fds[i], SOMAXCONN) < 0)
105b528cefcSMark Murray 	    err (1, "listen");
1065e9cd1aeSAssar Westerlund 	if (fds[i] >= FD_SETSIZE)
1075e9cd1aeSAssar Westerlund 	    errx (1, "fd too large");
108b528cefcSMark Murray 	FD_SET(fds[i], &orig_read_set);
109b528cefcSMark Murray 	max_fd = max(max_fd, fds[i]);
11013e3f4d6SMark Murray 	++i;
111b528cefcSMark Murray     }
112b528cefcSMark Murray     freeaddrinfo (ai);
11313e3f4d6SMark Murray     if (i == 0)
11413e3f4d6SMark Murray 	errx (1, "no sockets");
11513e3f4d6SMark Murray     n = i;
116b528cefcSMark Murray 
117b528cefcSMark Murray     do {
118b528cefcSMark Murray 	read_set = orig_read_set;
119b528cefcSMark Murray 
120b528cefcSMark Murray 	ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
121b528cefcSMark Murray 	if (ret < 0 && errno != EINTR)
122b528cefcSMark Murray 	    err (1, "select");
123b528cefcSMark Murray     } while (ret <= 0);
124b528cefcSMark Murray 
125b528cefcSMark Murray     for (i = 0; i < n; ++i)
126b528cefcSMark Murray 	if (FD_ISSET (fds[i], &read_set)) {
127b528cefcSMark Murray 	    accept_it (fds[i]);
128b528cefcSMark Murray 	    return;
129b528cefcSMark Murray 	}
130b528cefcSMark Murray     abort ();
131b528cefcSMark Murray }
132