xref: /freebsd/crypto/heimdal/kadmin/kadm_conn.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
1 /*
2  * Copyright (c) 2000 - 2001 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 #include "kadmin_locl.h"
35 #ifdef HAVE_SYS_WAIT_H
36 #include <sys/wait.h>
37 #endif
38 
39 RCSID("$Id: kadm_conn.c,v 1.14 2002/10/21 13:21:24 joda Exp $");
40 
41 struct kadm_port {
42     char *port;
43     unsigned short def_port;
44     struct kadm_port *next;
45 } *kadm_ports;
46 
47 static void
48 add_kadm_port(krb5_context context, const char *service, unsigned int port)
49 {
50     struct kadm_port *p;
51     p = malloc(sizeof(*p));
52     if(p == NULL) {
53 	krb5_warnx(context, "failed to allocate %lu bytes\n",
54 		   (unsigned long)sizeof(*p));
55 	return;
56     }
57 
58     p->port = strdup(service);
59     p->def_port = port;
60 
61     p->next = kadm_ports;
62     kadm_ports = p;
63 }
64 
65 extern int do_kerberos4;
66 
67 static void
68 add_standard_ports (krb5_context context)
69 {
70     add_kadm_port(context, "kerberos-adm", 749);
71 #ifdef KRB4
72     if(do_kerberos4)
73 	add_kadm_port(context, "kerberos-master", 751);
74 #endif
75 }
76 
77 /*
78  * parse the set of space-delimited ports in `str' and add them.
79  * "+" => all the standard ones
80  * otherwise it's port|service[/protocol]
81  */
82 
83 void
84 parse_ports(krb5_context context, const char *str)
85 {
86     char p[128];
87 
88     while(strsep_copy(&str, " \t", p, sizeof(p)) != -1) {
89 	if(strcmp(p, "+") == 0)
90 	    add_standard_ports(context);
91 	else
92 	    add_kadm_port(context, p, 0);
93     }
94 }
95 
96 static pid_t pgrp;
97 sig_atomic_t term_flag, doing_useful_work;
98 
99 static RETSIGTYPE
100 sigchld(int sig)
101 {
102     int status;
103     waitpid(-1, &status, 0);
104     SIGRETURN(0);
105 }
106 
107 static RETSIGTYPE
108 terminate(int sig)
109 {
110     if(getpid() == pgrp) {
111 	/* parent */
112 	term_flag = 1;
113 	signal(sig, SIG_IGN);
114 	killpg(pgrp, sig);
115     } else {
116 	/* child */
117 	if(doing_useful_work)
118 	    term_flag = 1;
119 	else
120 	    exit(0);
121     }
122     SIGRETURN(0);
123 }
124 
125 static int
126 spawn_child(krb5_context context, int *socks, int num_socks, int this_sock)
127 {
128     int e, i;
129     struct sockaddr_storage __ss;
130     struct sockaddr *sa = (struct sockaddr *)&__ss;
131     socklen_t sa_size = sizeof(__ss);
132     int s;
133     pid_t pid;
134     krb5_address addr;
135     char buf[128];
136     size_t buf_len;
137 
138     s = accept(socks[this_sock], sa, &sa_size);
139     if(s < 0) {
140 	krb5_warn(context, errno, "accept");
141 	return 1;
142     }
143     e = krb5_sockaddr2address(context, sa, &addr);
144     if(e)
145 	krb5_warn(context, e, "krb5_sockaddr2address");
146     else {
147 	e = krb5_print_address (&addr, buf, sizeof(buf),
148 				&buf_len);
149 	if(e)
150 	    krb5_warn(context, e, "krb5_print_address");
151 	else
152 	    krb5_warnx(context, "connection from %s", buf);
153 	krb5_free_address(context, &addr);
154     }
155 
156     pid = fork();
157     if(pid == 0) {
158 	for(i = 0; i < num_socks; i++)
159 	    close(socks[i]);
160 	dup2(s, STDIN_FILENO);
161 	dup2(s, STDOUT_FILENO);
162 	if(s != STDIN_FILENO && s != STDOUT_FILENO)
163 	    close(s);
164 	return 0;
165     } else {
166 	close(s);
167     }
168     return 1;
169 }
170 
171 static int
172 wait_for_connection(krb5_context context,
173 		    int *socks, int num_socks)
174 {
175     int i, e;
176     fd_set orig_read_set, read_set;
177     int max_fd = -1;
178 
179     FD_ZERO(&orig_read_set);
180 
181     for(i = 0; i < num_socks; i++) {
182 	if (socks[i] >= FD_SETSIZE)
183 	    errx (1, "fd too large");
184 	FD_SET(socks[i], &orig_read_set);
185 	max_fd = max(max_fd, socks[i]);
186     }
187 
188     pgrp = getpid();
189 
190     if(setpgid(0, pgrp) < 0)
191 	err(1, "setpgid");
192 
193     signal(SIGTERM, terminate);
194     signal(SIGINT, terminate);
195     signal(SIGCHLD, sigchld);
196 
197     while (term_flag == 0) {
198 	read_set = orig_read_set;
199 	e = select(max_fd + 1, &read_set, NULL, NULL, NULL);
200 	if(e < 0) {
201 	    if(errno != EINTR)
202 		krb5_warn(context, errno, "select");
203 	} else if(e == 0)
204 	    krb5_warnx(context, "select returned 0");
205 	else {
206 	    for(i = 0; i < num_socks; i++) {
207 		if(FD_ISSET(socks[i], &read_set))
208 		    if(spawn_child(context, socks, num_socks, i) == 0)
209 			return 0;
210 	    }
211 	}
212     }
213     signal(SIGCHLD, SIG_IGN);
214     while(1) {
215 	int status;
216 	pid_t pid;
217 	pid = waitpid(-1, &status, 0);
218 	if(pid == -1 && errno == ECHILD)
219 	    break;
220     }
221     exit(0);
222 }
223 
224 
225 int
226 start_server(krb5_context context)
227 {
228     int e;
229     struct kadm_port *p;
230 
231     int *socks = NULL, *tmp;
232     int num_socks = 0;
233     int i;
234 
235     for(p = kadm_ports; p; p = p->next) {
236 	struct addrinfo hints, *ai, *ap;
237 	char portstr[32];
238 	memset (&hints, 0, sizeof(hints));
239 	hints.ai_flags    = AI_PASSIVE;
240 	hints.ai_socktype = SOCK_STREAM;
241 
242 	e = getaddrinfo(NULL, p->port, &hints, &ai);
243 	if(e) {
244 	    snprintf(portstr, sizeof(portstr), "%u", p->def_port);
245 	    e = getaddrinfo(NULL, portstr, &hints, &ai);
246 	}
247 
248 	if(e) {
249 	    krb5_warn(context, krb5_eai_to_heim_errno(e, errno),
250 		      "%s", portstr);
251 	    continue;
252 	}
253 	i = 0;
254 	for(ap = ai; ap; ap = ap->ai_next)
255 	    i++;
256 	tmp = realloc(socks, (num_socks + i) * sizeof(*socks));
257 	if(tmp == NULL) {
258 	    krb5_warnx(context, "failed to reallocate %lu bytes",
259 		       (unsigned long)(num_socks + i) * sizeof(*socks));
260 	    continue;
261 	}
262 	socks = tmp;
263 	for(ap = ai; ap; ap = ap->ai_next) {
264 	    int one = 1;
265 	    int s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
266 	    if(s < 0) {
267 		krb5_warn(context, errno, "socket");
268 		continue;
269 	    }
270 #if defined(SO_REUSEADDR) && defined(HAVE_SETSOCKOPT)
271 	    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&one,
272 			  sizeof(one)) < 0)
273 		krb5_warn(context, errno, "setsockopt");
274 #endif
275 	    if (bind (s, ap->ai_addr, ap->ai_addrlen) < 0) {
276 		krb5_warn(context, errno, "bind");
277 		close(s);
278 		continue;
279 	    }
280 	    if (listen (s, SOMAXCONN) < 0) {
281 		krb5_warn(context, errno, "listen");
282 		close(s);
283 		continue;
284 	    }
285 	    socks[num_socks++] = s;
286 	}
287 	freeaddrinfo (ai);
288     }
289     if(num_socks == 0)
290 	krb5_errx(context, 1, "no sockets to listen to - exiting");
291     return wait_for_connection(context, socks, num_socks);
292 }
293