xref: /freebsd/crypto/heimdal/lib/roken/mini_inetd.c (revision 4137ff4cc173ea2e05227027e1c9e0ea42bcc0dc)
1b528cefcSMark Murray /*
24137ff4cSJacques Vidrine  * Copyright (c) 1995 - 2001 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>
364137ff4cSJacques Vidrine RCSID("$Id: mini_inetd.c,v 1.29 2001/08/01 14:48:54 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;
794137ff4cSJacques Vidrine     hints.ai_family   = PF_UNSPEC;
80b528cefcSMark Murray 
81b528cefcSMark Murray     snprintf (portstr, sizeof(portstr), "%d", ntohs(port));
82b528cefcSMark Murray 
83b528cefcSMark Murray     error = getaddrinfo (NULL, portstr, &hints, &ai);
84b528cefcSMark Murray     if (error)
85b528cefcSMark Murray 	errx (1, "getaddrinfo: %s", gai_strerror (error));
86b528cefcSMark Murray 
8713e3f4d6SMark Murray     for (nalloc = 0, a = ai; a != NULL; a = a->ai_next)
8813e3f4d6SMark Murray 	++nalloc;
89b528cefcSMark Murray 
9013e3f4d6SMark Murray     fds = malloc (nalloc * sizeof(*fds));
91b528cefcSMark Murray     if (fds == NULL)
92b528cefcSMark Murray 	errx (1, "mini_inetd: out of memory");
93b528cefcSMark Murray 
94b528cefcSMark Murray     FD_ZERO(&orig_read_set);
95b528cefcSMark Murray 
9613e3f4d6SMark Murray     for (i = 0, a = ai; a != NULL; a = a->ai_next) {
97b528cefcSMark Murray 	fds[i] = socket (a->ai_family, a->ai_socktype, a->ai_protocol);
9813e3f4d6SMark Murray 	if (fds[i] < 0) {
994137ff4cSJacques Vidrine 	    warn ("socket af = %d", a->ai_family);
10013e3f4d6SMark Murray 	    continue;
10113e3f4d6SMark Murray 	}
102b528cefcSMark Murray 	socket_set_reuseaddr (fds[i], 1);
1034137ff4cSJacques Vidrine 	if (bind (fds[i], a->ai_addr, a->ai_addrlen) < 0) {
1044137ff4cSJacques Vidrine 	    warn ("bind af = %d", a->ai_family);
1054137ff4cSJacques Vidrine 	    close(fds[i]);
1064137ff4cSJacques Vidrine 	    continue;
1074137ff4cSJacques Vidrine 	}
1084137ff4cSJacques Vidrine 	if (listen (fds[i], SOMAXCONN) < 0) {
1094137ff4cSJacques Vidrine 	    warn ("listen af = %d", a->ai_family);
1104137ff4cSJacques Vidrine 	    close(fds[i]);
1114137ff4cSJacques Vidrine 	    continue;
1124137ff4cSJacques Vidrine 	}
1135e9cd1aeSAssar Westerlund 	if (fds[i] >= FD_SETSIZE)
1145e9cd1aeSAssar Westerlund 	    errx (1, "fd too large");
115b528cefcSMark Murray 	FD_SET(fds[i], &orig_read_set);
116b528cefcSMark Murray 	max_fd = max(max_fd, fds[i]);
11713e3f4d6SMark Murray 	++i;
118b528cefcSMark Murray     }
119b528cefcSMark Murray     freeaddrinfo (ai);
12013e3f4d6SMark Murray     if (i == 0)
12113e3f4d6SMark Murray 	errx (1, "no sockets");
12213e3f4d6SMark Murray     n = i;
123b528cefcSMark Murray 
124b528cefcSMark Murray     do {
125b528cefcSMark Murray 	read_set = orig_read_set;
126b528cefcSMark Murray 
127b528cefcSMark Murray 	ret = select (max_fd + 1, &read_set, NULL, NULL, NULL);
128b528cefcSMark Murray 	if (ret < 0 && errno != EINTR)
129b528cefcSMark Murray 	    err (1, "select");
130b528cefcSMark Murray     } while (ret <= 0);
131b528cefcSMark Murray 
132b528cefcSMark Murray     for (i = 0; i < n; ++i)
133b528cefcSMark Murray 	if (FD_ISSET (fds[i], &read_set)) {
134b528cefcSMark Murray 	    accept_it (fds[i]);
135b528cefcSMark Murray 	    return;
136b528cefcSMark Murray 	}
137b528cefcSMark Murray     abort ();
138b528cefcSMark Murray }
139