Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / crypto / dist / heimdal / kadmin / kadm_conn.c
blob8c47278d2e557d71394197fc45224671ab25ad79
1 /*
2 * Copyright (c) 2000 - 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 #include "kadmin_locl.h"
35 #ifdef HAVE_SYS_WAIT_H
36 #include <sys/wait.h>
37 #endif
39 __RCSID("$Heimdal: kadm_conn.c 16007 2005-09-01 18:49:57Z lha $"
40 "$NetBSD$");
42 struct kadm_port {
43 char *port;
44 unsigned short def_port;
45 struct kadm_port *next;
46 } *kadm_ports;
48 static void
49 add_kadm_port(krb5_context context, const char *service, unsigned int port)
51 struct kadm_port *p;
52 p = malloc(sizeof(*p));
53 if(p == NULL) {
54 krb5_warnx(context, "failed to allocate %lu bytes\n",
55 (unsigned long)sizeof(*p));
56 return;
59 p->port = strdup(service);
60 p->def_port = port;
62 p->next = kadm_ports;
63 kadm_ports = p;
66 static void
67 add_standard_ports (krb5_context context)
69 add_kadm_port(context, "kerberos-adm", 749);
73 * parse the set of space-delimited ports in `str' and add them.
74 * "+" => all the standard ones
75 * otherwise it's port|service[/protocol]
78 void
79 parse_ports(krb5_context context, const char *str)
81 char p[128];
83 while(strsep_copy(&str, " \t", p, sizeof(p)) != -1) {
84 if(strcmp(p, "+") == 0)
85 add_standard_ports(context);
86 else
87 add_kadm_port(context, p, 0);
91 static pid_t pgrp;
92 sig_atomic_t term_flag, doing_useful_work;
94 static RETSIGTYPE
95 sigchld(int sig)
97 int status;
98 waitpid(-1, &status, 0);
99 SIGRETURN(0);
102 static RETSIGTYPE
103 terminate(int sig)
105 if(getpid() == pgrp) {
106 /* parent */
107 term_flag = 1;
108 signal(sig, SIG_IGN);
109 killpg(pgrp, sig);
110 } else {
111 /* child */
112 if(doing_useful_work)
113 term_flag = 1;
114 else
115 exit(0);
117 SIGRETURN(0);
120 static int
121 spawn_child(krb5_context context, int *socks, int num_socks, int this_sock)
123 int e, i;
124 struct sockaddr_storage __ss;
125 struct sockaddr *sa = (struct sockaddr *)&__ss;
126 socklen_t sa_size = sizeof(__ss);
127 int s;
128 pid_t pid;
129 krb5_address addr;
130 char buf[128];
131 size_t buf_len;
133 s = accept(socks[this_sock], sa, &sa_size);
134 if(s < 0) {
135 krb5_warn(context, errno, "accept");
136 return 1;
138 e = krb5_sockaddr2address(context, sa, &addr);
139 if(e)
140 krb5_warn(context, e, "krb5_sockaddr2address");
141 else {
142 e = krb5_print_address (&addr, buf, sizeof(buf),
143 &buf_len);
144 if(e)
145 krb5_warn(context, e, "krb5_print_address");
146 else
147 krb5_warnx(context, "connection from %s", buf);
148 krb5_free_address(context, &addr);
151 pid = fork();
152 if(pid == 0) {
153 for(i = 0; i < num_socks; i++)
154 close(socks[i]);
155 dup2(s, STDIN_FILENO);
156 dup2(s, STDOUT_FILENO);
157 if(s != STDIN_FILENO && s != STDOUT_FILENO)
158 close(s);
159 return 0;
160 } else {
161 close(s);
163 return 1;
166 static int
167 wait_for_connection(krb5_context context,
168 int *socks, int num_socks)
170 int i, e;
171 fd_set orig_read_set, read_set;
172 int max_fd = -1;
174 FD_ZERO(&orig_read_set);
176 for(i = 0; i < num_socks; i++) {
177 if (socks[i] >= FD_SETSIZE)
178 errx (1, "fd too large");
179 FD_SET(socks[i], &orig_read_set);
180 max_fd = max(max_fd, socks[i]);
183 pgrp = getpid();
185 if(setpgid(0, pgrp) < 0)
186 err(1, "setpgid");
188 signal(SIGTERM, terminate);
189 signal(SIGINT, terminate);
190 signal(SIGCHLD, sigchld);
192 while (term_flag == 0) {
193 read_set = orig_read_set;
194 e = select(max_fd + 1, &read_set, NULL, NULL, NULL);
195 if(e < 0) {
196 if(errno != EINTR)
197 krb5_warn(context, errno, "select");
198 } else if(e == 0)
199 krb5_warnx(context, "select returned 0");
200 else {
201 for(i = 0; i < num_socks; i++) {
202 if(FD_ISSET(socks[i], &read_set))
203 if(spawn_child(context, socks, num_socks, i) == 0)
204 return 0;
208 signal(SIGCHLD, SIG_IGN);
209 while(1) {
210 int status;
211 pid_t pid;
212 pid = waitpid(-1, &status, 0);
213 if(pid == -1 && errno == ECHILD)
214 break;
216 exit(0);
221 start_server(krb5_context context)
223 int e;
224 struct kadm_port *p;
226 int *socks = NULL, *tmp;
227 int num_socks = 0;
228 int i;
230 for(p = kadm_ports; p; p = p->next) {
231 struct addrinfo hints, *ai, *ap;
232 char portstr[32];
233 memset (&hints, 0, sizeof(hints));
234 hints.ai_flags = AI_PASSIVE;
235 hints.ai_socktype = SOCK_STREAM;
237 e = getaddrinfo(NULL, p->port, &hints, &ai);
238 if(e) {
239 snprintf(portstr, sizeof(portstr), "%u", p->def_port);
240 e = getaddrinfo(NULL, portstr, &hints, &ai);
243 if(e) {
244 krb5_warn(context, krb5_eai_to_heim_errno(e, errno),
245 "%s", portstr);
246 continue;
248 i = 0;
249 for(ap = ai; ap; ap = ap->ai_next)
250 i++;
251 tmp = realloc(socks, (num_socks + i) * sizeof(*socks));
252 if(tmp == NULL) {
253 krb5_warnx(context, "failed to reallocate %lu bytes",
254 (unsigned long)(num_socks + i) * sizeof(*socks));
255 continue;
257 socks = tmp;
258 for(ap = ai; ap; ap = ap->ai_next) {
259 int s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
260 if(s < 0) {
261 krb5_warn(context, errno, "socket");
262 continue;
265 socket_set_reuseaddr(s, 1);
266 socket_set_ipv6only(s, 1);
268 if (bind (s, ap->ai_addr, ap->ai_addrlen) < 0) {
269 krb5_warn(context, errno, "bind");
270 close(s);
271 continue;
273 if (listen (s, SOMAXCONN) < 0) {
274 krb5_warn(context, errno, "listen");
275 close(s);
276 continue;
278 socks[num_socks++] = s;
280 freeaddrinfo (ai);
282 if(num_socks == 0)
283 krb5_errx(context, 1, "no sockets to listen to - exiting");
284 return wait_for_connection(context, socks, num_socks);