connect.c revision 1.1.1.4 1 1.1 elric /* $NetBSD: connect.c,v 1.1.1.4 2019/12/15 22:45:39 christos Exp $ */
2 1.1 elric
3 1.1 elric /*
4 1.1 elric * Copyright (c) 1997-2005 Kungliga Tekniska Hgskolan
5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden).
6 1.1 elric * All rights reserved.
7 1.1 elric *
8 1.1 elric * Redistribution and use in source and binary forms, with or without
9 1.1 elric * modification, are permitted provided that the following conditions
10 1.1 elric * are met:
11 1.1 elric *
12 1.1 elric * 1. Redistributions of source code must retain the above copyright
13 1.1 elric * notice, this list of conditions and the following disclaimer.
14 1.1 elric *
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors
20 1.1 elric * may be used to endorse or promote products derived from this software
21 1.1 elric * without specific prior written permission.
22 1.1 elric *
23 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 elric * SUCH DAMAGE.
34 1.1 elric */
35 1.1 elric
36 1.1 elric #include "kdc_locl.h"
37 1.1 elric
38 1.1 elric /*
39 1.1 elric * a tuple describing on what to listen
40 1.1 elric */
41 1.1 elric
42 1.1 elric struct port_desc{
43 1.1 elric int family;
44 1.1 elric int type;
45 1.1 elric int port;
46 1.1 elric };
47 1.1 elric
48 1.1 elric /* the current ones */
49 1.1 elric
50 1.1 elric static struct port_desc *ports;
51 1.1.1.2 pettai static size_t num_ports;
52 1.1.1.3 christos static pid_t bonjour_pid = -1;
53 1.1 elric
54 1.1 elric /*
55 1.1 elric * add `family, port, protocol' to the list with duplicate suppresion.
56 1.1 elric */
57 1.1 elric
58 1.1 elric static void
59 1.1 elric add_port(krb5_context context,
60 1.1 elric int family, int port, const char *protocol)
61 1.1 elric {
62 1.1 elric int type;
63 1.1.1.2 pettai size_t i;
64 1.1 elric
65 1.1 elric if(strcmp(protocol, "udp") == 0)
66 1.1 elric type = SOCK_DGRAM;
67 1.1 elric else if(strcmp(protocol, "tcp") == 0)
68 1.1 elric type = SOCK_STREAM;
69 1.1 elric else
70 1.1 elric return;
71 1.1 elric for(i = 0; i < num_ports; i++){
72 1.1 elric if(ports[i].type == type
73 1.1 elric && ports[i].port == port
74 1.1 elric && ports[i].family == family)
75 1.1 elric return;
76 1.1 elric }
77 1.1 elric ports = realloc(ports, (num_ports + 1) * sizeof(*ports));
78 1.1 elric if (ports == NULL)
79 1.1 elric krb5_err (context, 1, errno, "realloc");
80 1.1 elric ports[num_ports].family = family;
81 1.1 elric ports[num_ports].type = type;
82 1.1 elric ports[num_ports].port = port;
83 1.1 elric num_ports++;
84 1.1 elric }
85 1.1 elric
86 1.1 elric /*
87 1.1 elric * add a triple but with service -> port lookup
88 1.1 elric * (this prints warnings for stuff that does not exist)
89 1.1 elric */
90 1.1 elric
91 1.1 elric static void
92 1.1 elric add_port_service(krb5_context context,
93 1.1 elric int family, const char *service, int port,
94 1.1 elric const char *protocol)
95 1.1 elric {
96 1.1 elric port = krb5_getportbyname (context, service, protocol, port);
97 1.1 elric add_port (context, family, port, protocol);
98 1.1 elric }
99 1.1 elric
100 1.1 elric /*
101 1.1 elric * add the port with service -> port lookup or string -> number
102 1.1 elric * (no warning is printed)
103 1.1 elric */
104 1.1 elric
105 1.1 elric static void
106 1.1 elric add_port_string (krb5_context context,
107 1.1 elric int family, const char *str, const char *protocol)
108 1.1 elric {
109 1.1 elric struct servent *sp;
110 1.1 elric int port;
111 1.1 elric
112 1.1 elric sp = roken_getservbyname (str, protocol);
113 1.1 elric if (sp != NULL) {
114 1.1 elric port = sp->s_port;
115 1.1 elric } else {
116 1.1 elric char *end;
117 1.1 elric
118 1.1 elric port = htons(strtol(str, &end, 0));
119 1.1 elric if (end == str)
120 1.1 elric return;
121 1.1 elric }
122 1.1 elric add_port (context, family, port, protocol);
123 1.1 elric }
124 1.1 elric
125 1.1 elric /*
126 1.1 elric * add the standard collection of ports for `family'
127 1.1 elric */
128 1.1 elric
129 1.1 elric static void
130 1.1.1.2 pettai add_standard_ports (krb5_context context,
131 1.1 elric krb5_kdc_configuration *config,
132 1.1 elric int family)
133 1.1 elric {
134 1.1 elric add_port_service(context, family, "kerberos", 88, "udp");
135 1.1 elric add_port_service(context, family, "kerberos", 88, "tcp");
136 1.1 elric add_port_service(context, family, "kerberos-sec", 88, "udp");
137 1.1 elric add_port_service(context, family, "kerberos-sec", 88, "tcp");
138 1.1 elric if(enable_http)
139 1.1 elric add_port_service(context, family, "http", 80, "tcp");
140 1.1 elric if(config->enable_kx509) {
141 1.1 elric add_port_service(context, family, "kca_service", 9878, "udp");
142 1.1 elric add_port_service(context, family, "kca_service", 9878, "tcp");
143 1.1 elric }
144 1.1 elric
145 1.1 elric }
146 1.1 elric
147 1.1 elric /*
148 1.1 elric * parse the set of space-delimited ports in `str' and add them.
149 1.1 elric * "+" => all the standard ones
150 1.1 elric * otherwise it's port|service[/protocol]
151 1.1 elric */
152 1.1 elric
153 1.1 elric static void
154 1.1.1.2 pettai parse_ports(krb5_context context,
155 1.1 elric krb5_kdc_configuration *config,
156 1.1 elric const char *str)
157 1.1 elric {
158 1.1 elric char *pos = NULL;
159 1.1 elric char *p;
160 1.1 elric char *str_copy = strdup (str);
161 1.1 elric
162 1.1 elric p = strtok_r(str_copy, " \t", &pos);
163 1.1 elric while(p != NULL) {
164 1.1 elric if(strcmp(p, "+") == 0) {
165 1.1 elric #ifdef HAVE_IPV6
166 1.1 elric add_standard_ports(context, config, AF_INET6);
167 1.1 elric #endif
168 1.1 elric add_standard_ports(context, config, AF_INET);
169 1.1 elric } else {
170 1.1 elric char *q = strchr(p, '/');
171 1.1 elric if(q){
172 1.1 elric *q++ = 0;
173 1.1 elric #ifdef HAVE_IPV6
174 1.1 elric add_port_string(context, AF_INET6, p, q);
175 1.1 elric #endif
176 1.1 elric add_port_string(context, AF_INET, p, q);
177 1.1 elric }else {
178 1.1 elric #ifdef HAVE_IPV6
179 1.1 elric add_port_string(context, AF_INET6, p, "udp");
180 1.1 elric add_port_string(context, AF_INET6, p, "tcp");
181 1.1 elric #endif
182 1.1 elric add_port_string(context, AF_INET, p, "udp");
183 1.1 elric add_port_string(context, AF_INET, p, "tcp");
184 1.1 elric }
185 1.1 elric }
186 1.1.1.2 pettai
187 1.1 elric p = strtok_r(NULL, " \t", &pos);
188 1.1 elric }
189 1.1 elric free (str_copy);
190 1.1 elric }
191 1.1 elric
192 1.1 elric /*
193 1.1 elric * every socket we listen on
194 1.1 elric */
195 1.1 elric
196 1.1 elric struct descr {
197 1.1 elric krb5_socket_t s;
198 1.1 elric int type;
199 1.1 elric int port;
200 1.1 elric unsigned char *buf;
201 1.1 elric size_t size;
202 1.1 elric size_t len;
203 1.1 elric time_t timeout;
204 1.1 elric struct sockaddr_storage __ss;
205 1.1 elric struct sockaddr *sa;
206 1.1 elric socklen_t sock_len;
207 1.1 elric char addr_string[128];
208 1.1 elric };
209 1.1 elric
210 1.1 elric static void
211 1.1 elric init_descr(struct descr *d)
212 1.1 elric {
213 1.1 elric memset(d, 0, sizeof(*d));
214 1.1 elric d->sa = (struct sockaddr *)&d->__ss;
215 1.1 elric d->s = rk_INVALID_SOCKET;
216 1.1 elric }
217 1.1 elric
218 1.1 elric /*
219 1.1 elric * re-initialize all `n' ->sa in `d'.
220 1.1 elric */
221 1.1 elric
222 1.1 elric static void
223 1.1 elric reinit_descrs (struct descr *d, int n)
224 1.1 elric {
225 1.1 elric int i;
226 1.1 elric
227 1.1 elric for (i = 0; i < n; ++i)
228 1.1 elric d[i].sa = (struct sockaddr *)&d[i].__ss;
229 1.1 elric }
230 1.1 elric
231 1.1 elric /*
232 1.1 elric * Create the socket (family, type, port) in `d'
233 1.1 elric */
234 1.1 elric
235 1.1 elric static void
236 1.1 elric init_socket(krb5_context context,
237 1.1 elric krb5_kdc_configuration *config,
238 1.1 elric struct descr *d, krb5_address *a, int family, int type, int port)
239 1.1 elric {
240 1.1 elric krb5_error_code ret;
241 1.1 elric struct sockaddr_storage __ss;
242 1.1 elric struct sockaddr *sa = (struct sockaddr *)&__ss;
243 1.1 elric krb5_socklen_t sa_size = sizeof(__ss);
244 1.1 elric
245 1.1 elric init_descr (d);
246 1.1 elric
247 1.1 elric ret = krb5_addr2sockaddr (context, a, sa, &sa_size, port);
248 1.1 elric if (ret) {
249 1.1 elric krb5_warn(context, ret, "krb5_addr2sockaddr");
250 1.1 elric rk_closesocket(d->s);
251 1.1 elric d->s = rk_INVALID_SOCKET;
252 1.1 elric return;
253 1.1 elric }
254 1.1 elric
255 1.1 elric if (sa->sa_family != family)
256 1.1 elric return;
257 1.1 elric
258 1.1 elric d->s = socket(family, type, 0);
259 1.1 elric if(rk_IS_BAD_SOCKET(d->s)){
260 1.1 elric krb5_warn(context, errno, "socket(%d, %d, 0)", family, type);
261 1.1 elric d->s = rk_INVALID_SOCKET;
262 1.1 elric return;
263 1.1 elric }
264 1.1.1.3 christos rk_cloexec(d->s);
265 1.1 elric #if defined(HAVE_SETSOCKOPT) && defined(SOL_SOCKET) && defined(SO_REUSEADDR)
266 1.1 elric {
267 1.1 elric int one = 1;
268 1.1 elric setsockopt(d->s, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
269 1.1 elric }
270 1.1 elric #endif
271 1.1 elric d->type = type;
272 1.1 elric d->port = port;
273 1.1 elric
274 1.1.1.3 christos socket_set_nonblocking(d->s, 1);
275 1.1.1.3 christos
276 1.1 elric if(rk_IS_SOCKET_ERROR(bind(d->s, sa, sa_size))){
277 1.1 elric char a_str[256];
278 1.1 elric size_t len;
279 1.1 elric
280 1.1 elric krb5_print_address (a, a_str, sizeof(a_str), &len);
281 1.1 elric krb5_warn(context, errno, "bind %s/%d", a_str, ntohs(port));
282 1.1 elric rk_closesocket(d->s);
283 1.1 elric d->s = rk_INVALID_SOCKET;
284 1.1 elric return;
285 1.1 elric }
286 1.1 elric if(type == SOCK_STREAM && rk_IS_SOCKET_ERROR(listen(d->s, SOMAXCONN))){
287 1.1 elric char a_str[256];
288 1.1 elric size_t len;
289 1.1 elric
290 1.1 elric krb5_print_address (a, a_str, sizeof(a_str), &len);
291 1.1 elric krb5_warn(context, errno, "listen %s/%d", a_str, ntohs(port));
292 1.1 elric rk_closesocket(d->s);
293 1.1 elric d->s = rk_INVALID_SOCKET;
294 1.1 elric return;
295 1.1 elric }
296 1.1 elric }
297 1.1 elric
298 1.1 elric /*
299 1.1 elric * Allocate descriptors for all the sockets that we should listen on
300 1.1 elric * and return the number of them.
301 1.1 elric */
302 1.1 elric
303 1.1 elric static int
304 1.1 elric init_sockets(krb5_context context,
305 1.1 elric krb5_kdc_configuration *config,
306 1.1 elric struct descr **desc)
307 1.1 elric {
308 1.1 elric krb5_error_code ret;
309 1.1.1.2 pettai size_t i, j;
310 1.1 elric struct descr *d;
311 1.1 elric int num = 0;
312 1.1 elric krb5_addresses addresses;
313 1.1 elric
314 1.1 elric if (explicit_addresses.len) {
315 1.1 elric addresses = explicit_addresses;
316 1.1 elric } else {
317 1.1 elric ret = krb5_get_all_server_addrs (context, &addresses);
318 1.1 elric if (ret)
319 1.1 elric krb5_err (context, 1, ret, "krb5_get_all_server_addrs");
320 1.1 elric }
321 1.1 elric parse_ports(context, config, port_str);
322 1.1 elric d = malloc(addresses.len * num_ports * sizeof(*d));
323 1.1 elric if (d == NULL)
324 1.1 elric krb5_errx(context, 1, "malloc(%lu) failed",
325 1.1 elric (unsigned long)num_ports * sizeof(*d));
326 1.1 elric
327 1.1 elric for (i = 0; i < num_ports; i++){
328 1.1 elric for (j = 0; j < addresses.len; ++j) {
329 1.1 elric init_socket(context, config, &d[num], &addresses.val[j],
330 1.1 elric ports[i].family, ports[i].type, ports[i].port);
331 1.1 elric if(d[num].s != rk_INVALID_SOCKET){
332 1.1 elric char a_str[80];
333 1.1 elric size_t len;
334 1.1 elric
335 1.1 elric krb5_print_address (&addresses.val[j], a_str,
336 1.1 elric sizeof(a_str), &len);
337 1.1 elric
338 1.1 elric kdc_log(context, config, 5, "listening on %s port %u/%s",
339 1.1 elric a_str,
340 1.1 elric ntohs(ports[i].port),
341 1.1 elric (ports[i].type == SOCK_STREAM) ? "tcp" : "udp");
342 1.1 elric /* XXX */
343 1.1 elric num++;
344 1.1 elric }
345 1.1 elric }
346 1.1 elric }
347 1.1 elric krb5_free_addresses (context, &addresses);
348 1.1 elric d = realloc(d, num * sizeof(*d));
349 1.1 elric if (d == NULL && num != 0)
350 1.1 elric krb5_errx(context, 1, "realloc(%lu) failed",
351 1.1 elric (unsigned long)num * sizeof(*d));
352 1.1 elric reinit_descrs (d, num);
353 1.1 elric *desc = d;
354 1.1 elric return num;
355 1.1 elric }
356 1.1 elric
357 1.1 elric /*
358 1.1 elric *
359 1.1 elric */
360 1.1 elric
361 1.1 elric static const char *
362 1.1 elric descr_type(struct descr *d)
363 1.1 elric {
364 1.1 elric if (d->type == SOCK_DGRAM)
365 1.1 elric return "udp";
366 1.1 elric else if (d->type == SOCK_STREAM)
367 1.1 elric return "tcp";
368 1.1 elric return "unknown";
369 1.1 elric }
370 1.1 elric
371 1.1 elric static void
372 1.1.1.2 pettai addr_to_string(krb5_context context,
373 1.1 elric struct sockaddr *addr, size_t addr_len, char *str, size_t len)
374 1.1 elric {
375 1.1 elric krb5_address a;
376 1.1 elric if(krb5_sockaddr2address(context, addr, &a) == 0) {
377 1.1 elric if(krb5_print_address(&a, str, len, &len) == 0) {
378 1.1 elric krb5_free_address(context, &a);
379 1.1 elric return;
380 1.1 elric }
381 1.1 elric krb5_free_address(context, &a);
382 1.1 elric }
383 1.1 elric snprintf(str, len, "<family=%d>", addr->sa_family);
384 1.1 elric }
385 1.1 elric
386 1.1 elric /*
387 1.1 elric *
388 1.1 elric */
389 1.1 elric
390 1.1 elric static void
391 1.1 elric send_reply(krb5_context context,
392 1.1 elric krb5_kdc_configuration *config,
393 1.1 elric krb5_boolean prependlength,
394 1.1 elric struct descr *d,
395 1.1 elric krb5_data *reply)
396 1.1 elric {
397 1.1 elric kdc_log(context, config, 5,
398 1.1 elric "sending %lu bytes to %s", (unsigned long)reply->length,
399 1.1 elric d->addr_string);
400 1.1 elric if(prependlength){
401 1.1 elric unsigned char l[4];
402 1.1 elric l[0] = (reply->length >> 24) & 0xff;
403 1.1 elric l[1] = (reply->length >> 16) & 0xff;
404 1.1 elric l[2] = (reply->length >> 8) & 0xff;
405 1.1 elric l[3] = reply->length & 0xff;
406 1.1 elric if(rk_IS_SOCKET_ERROR(sendto(d->s, l, sizeof(l), 0, d->sa, d->sock_len))) {
407 1.1 elric kdc_log (context, config,
408 1.1 elric 0, "sendto(%s): %s", d->addr_string,
409 1.1 elric strerror(rk_SOCK_ERRNO));
410 1.1 elric return;
411 1.1 elric }
412 1.1 elric }
413 1.1 elric if(rk_IS_SOCKET_ERROR(sendto(d->s, reply->data, reply->length, 0, d->sa, d->sock_len))) {
414 1.1 elric kdc_log (context, config, 0, "sendto(%s): %s", d->addr_string,
415 1.1 elric strerror(rk_SOCK_ERRNO));
416 1.1 elric return;
417 1.1 elric }
418 1.1 elric }
419 1.1 elric
420 1.1 elric /*
421 1.1 elric * Handle the request in `buf, len' to socket `d'
422 1.1 elric */
423 1.1 elric
424 1.1 elric static void
425 1.1 elric do_request(krb5_context context,
426 1.1 elric krb5_kdc_configuration *config,
427 1.1 elric void *buf, size_t len, krb5_boolean prependlength,
428 1.1 elric struct descr *d)
429 1.1 elric {
430 1.1 elric krb5_error_code ret;
431 1.1 elric krb5_data reply;
432 1.1 elric int datagram_reply = (d->type == SOCK_DGRAM);
433 1.1 elric
434 1.1 elric krb5_kdc_update_time(NULL);
435 1.1 elric
436 1.1 elric krb5_data_zero(&reply);
437 1.1 elric ret = krb5_kdc_process_request(context, config,
438 1.1 elric buf, len, &reply, &prependlength,
439 1.1 elric d->addr_string, d->sa,
440 1.1 elric datagram_reply);
441 1.1 elric if(request_log)
442 1.1 elric krb5_kdc_save_request(context, request_log, buf, len, &reply, d->sa);
443 1.1 elric if(reply.length){
444 1.1 elric send_reply(context, config, prependlength, d, &reply);
445 1.1 elric krb5_data_free(&reply);
446 1.1 elric }
447 1.1 elric if(ret)
448 1.1 elric kdc_log(context, config, 0,
449 1.1 elric "Failed processing %lu byte request from %s",
450 1.1 elric (unsigned long)len, d->addr_string);
451 1.1 elric }
452 1.1 elric
453 1.1 elric /*
454 1.1 elric * Handle incoming data to the UDP socket in `d'
455 1.1 elric */
456 1.1 elric
457 1.1 elric static void
458 1.1 elric handle_udp(krb5_context context,
459 1.1 elric krb5_kdc_configuration *config,
460 1.1 elric struct descr *d)
461 1.1 elric {
462 1.1 elric unsigned char *buf;
463 1.1.1.2 pettai ssize_t n;
464 1.1 elric
465 1.1 elric buf = malloc(max_request_udp);
466 1.1.1.3 christos if (buf == NULL){
467 1.1.1.3 christos kdc_log(context, config, 0, "Failed to allocate %lu bytes",
468 1.1.1.3 christos (unsigned long)max_request_udp);
469 1.1 elric return;
470 1.1 elric }
471 1.1 elric
472 1.1 elric d->sock_len = sizeof(d->__ss);
473 1.1 elric n = recvfrom(d->s, buf, max_request_udp, 0, d->sa, &d->sock_len);
474 1.1.1.3 christos if (rk_IS_SOCKET_ERROR(n)) {
475 1.1.1.3 christos if (rk_SOCK_ERRNO != EAGAIN && rk_SOCK_ERRNO != EINTR)
476 1.1.1.3 christos krb5_warn(context, rk_SOCK_ERRNO, "recvfrom");
477 1.1.1.3 christos } else {
478 1.1 elric addr_to_string (context, d->sa, d->sock_len,
479 1.1 elric d->addr_string, sizeof(d->addr_string));
480 1.1.1.2 pettai if ((size_t)n == max_request_udp) {
481 1.1 elric krb5_data data;
482 1.1 elric krb5_warn(context, errno,
483 1.1 elric "recvfrom: truncated packet from %s, asking for TCP",
484 1.1 elric d->addr_string);
485 1.1 elric krb5_mk_error(context,
486 1.1 elric KRB5KRB_ERR_RESPONSE_TOO_BIG,
487 1.1 elric NULL,
488 1.1 elric NULL,
489 1.1 elric NULL,
490 1.1 elric NULL,
491 1.1 elric NULL,
492 1.1 elric NULL,
493 1.1 elric &data);
494 1.1 elric send_reply(context, config, FALSE, d, &data);
495 1.1 elric krb5_data_free(&data);
496 1.1 elric } else {
497 1.1 elric do_request(context, config, buf, n, FALSE, d);
498 1.1 elric }
499 1.1 elric }
500 1.1 elric free (buf);
501 1.1 elric }
502 1.1 elric
503 1.1 elric static void
504 1.1 elric clear_descr(struct descr *d)
505 1.1 elric {
506 1.1 elric if(d->buf)
507 1.1 elric memset(d->buf, 0, d->size);
508 1.1 elric d->len = 0;
509 1.1 elric if(d->s != rk_INVALID_SOCKET)
510 1.1 elric rk_closesocket(d->s);
511 1.1 elric d->s = rk_INVALID_SOCKET;
512 1.1 elric }
513 1.1 elric
514 1.1 elric
515 1.1 elric /* remove HTTP %-quoting from buf */
516 1.1 elric static int
517 1.1 elric de_http(char *buf)
518 1.1 elric {
519 1.1 elric unsigned char *p, *q;
520 1.1.1.4 christos unsigned int x;
521 1.1.1.4 christos
522 1.1.1.4 christos for (p = q = (unsigned char *)buf; *p; p++, q++) {
523 1.1.1.4 christos if (*p == '%') {
524 1.1.1.4 christos if (!(isxdigit(p[1]) && isxdigit(p[2])))
525 1.1.1.4 christos return -1;
526 1.1.1.4 christos
527 1.1.1.4 christos if (sscanf((char *)p + 1, "%2x", &x) != 1)
528 1.1 elric return -1;
529 1.1.1.4 christos
530 1.1 elric *q = x;
531 1.1 elric p += 2;
532 1.1.1.4 christos } else {
533 1.1 elric *q = *p;
534 1.1.1.4 christos }
535 1.1 elric }
536 1.1 elric *q = '\0';
537 1.1 elric return 0;
538 1.1 elric }
539 1.1 elric
540 1.1 elric #define TCP_TIMEOUT 4
541 1.1 elric
542 1.1 elric /*
543 1.1 elric * accept a new TCP connection on `d[parent]' and store it in `d[child]'
544 1.1 elric */
545 1.1 elric
546 1.1 elric static void
547 1.1 elric add_new_tcp (krb5_context context,
548 1.1 elric krb5_kdc_configuration *config,
549 1.1 elric struct descr *d, int parent, int child)
550 1.1 elric {
551 1.1 elric krb5_socket_t s;
552 1.1 elric
553 1.1 elric if (child == -1)
554 1.1 elric return;
555 1.1 elric
556 1.1 elric d[child].sock_len = sizeof(d[child].__ss);
557 1.1 elric s = accept(d[parent].s, d[child].sa, &d[child].sock_len);
558 1.1 elric if(rk_IS_BAD_SOCKET(s)) {
559 1.1.1.3 christos if (rk_SOCK_ERRNO != EAGAIN && rk_SOCK_ERRNO != EINTR)
560 1.1.1.3 christos krb5_warn(context, rk_SOCK_ERRNO, "accept");
561 1.1 elric return;
562 1.1 elric }
563 1.1 elric
564 1.1 elric #ifdef FD_SETSIZE
565 1.1 elric if (s >= FD_SETSIZE) {
566 1.1 elric krb5_warnx(context, "socket FD too large");
567 1.1 elric rk_closesocket (s);
568 1.1 elric return;
569 1.1 elric }
570 1.1 elric #endif
571 1.1 elric
572 1.1 elric d[child].s = s;
573 1.1 elric d[child].timeout = time(NULL) + TCP_TIMEOUT;
574 1.1 elric d[child].type = SOCK_STREAM;
575 1.1 elric addr_to_string (context,
576 1.1 elric d[child].sa, d[child].sock_len,
577 1.1 elric d[child].addr_string, sizeof(d[child].addr_string));
578 1.1 elric }
579 1.1 elric
580 1.1 elric /*
581 1.1 elric * Grow `d' to handle at least `n'.
582 1.1 elric * Return != 0 if fails
583 1.1 elric */
584 1.1 elric
585 1.1 elric static int
586 1.1 elric grow_descr (krb5_context context,
587 1.1 elric krb5_kdc_configuration *config,
588 1.1 elric struct descr *d, size_t n)
589 1.1 elric {
590 1.1 elric if (d->size - d->len < n) {
591 1.1 elric unsigned char *tmp;
592 1.1 elric size_t grow;
593 1.1 elric
594 1.1 elric grow = max(1024, d->len + n);
595 1.1 elric if (d->size + grow > max_request_tcp) {
596 1.1 elric kdc_log(context, config, 0, "Request exceeds max request size (%lu bytes).",
597 1.1 elric (unsigned long)d->size + grow);
598 1.1 elric clear_descr(d);
599 1.1 elric return -1;
600 1.1 elric }
601 1.1 elric tmp = realloc (d->buf, d->size + grow);
602 1.1 elric if (tmp == NULL) {
603 1.1 elric kdc_log(context, config, 0, "Failed to re-allocate %lu bytes.",
604 1.1 elric (unsigned long)d->size + grow);
605 1.1 elric clear_descr(d);
606 1.1 elric return -1;
607 1.1 elric }
608 1.1 elric d->size += grow;
609 1.1 elric d->buf = tmp;
610 1.1 elric }
611 1.1 elric return 0;
612 1.1 elric }
613 1.1 elric
614 1.1 elric /*
615 1.1 elric * Try to handle the TCP data at `d->buf, d->len'.
616 1.1 elric * Return -1 if failed, 0 if succesful, and 1 if data is complete.
617 1.1 elric */
618 1.1 elric
619 1.1 elric static int
620 1.1 elric handle_vanilla_tcp (krb5_context context,
621 1.1 elric krb5_kdc_configuration *config,
622 1.1 elric struct descr *d)
623 1.1 elric {
624 1.1 elric krb5_storage *sp;
625 1.1 elric uint32_t len;
626 1.1 elric
627 1.1 elric sp = krb5_storage_from_mem(d->buf, d->len);
628 1.1 elric if (sp == NULL) {
629 1.1 elric kdc_log (context, config, 0, "krb5_storage_from_mem failed");
630 1.1 elric return -1;
631 1.1 elric }
632 1.1 elric krb5_ret_uint32(sp, &len);
633 1.1 elric krb5_storage_free(sp);
634 1.1 elric if(d->len - 4 >= len) {
635 1.1 elric memmove(d->buf, d->buf + 4, d->len - 4);
636 1.1 elric d->len -= 4;
637 1.1 elric return 1;
638 1.1 elric }
639 1.1 elric return 0;
640 1.1 elric }
641 1.1 elric
642 1.1 elric /*
643 1.1 elric * Try to handle the TCP/HTTP data at `d->buf, d->len'.
644 1.1 elric * Return -1 if failed, 0 if succesful, and 1 if data is complete.
645 1.1 elric */
646 1.1 elric
647 1.1 elric static int
648 1.1 elric handle_http_tcp (krb5_context context,
649 1.1 elric krb5_kdc_configuration *config,
650 1.1 elric struct descr *d)
651 1.1 elric {
652 1.1 elric char *s, *p, *t;
653 1.1 elric void *data;
654 1.1 elric char *proto;
655 1.1 elric int len;
656 1.1 elric
657 1.1 elric s = (char *)d->buf;
658 1.1 elric
659 1.1 elric /* If its a multi line query, truncate off the first line */
660 1.1 elric p = strstr(s, "\r\n");
661 1.1 elric if (p)
662 1.1 elric *p = 0;
663 1.1 elric
664 1.1 elric p = NULL;
665 1.1 elric t = strtok_r(s, " \t", &p);
666 1.1 elric if (t == NULL) {
667 1.1 elric kdc_log(context, config, 0,
668 1.1 elric "Missing HTTP operand (GET) request from %s", d->addr_string);
669 1.1 elric return -1;
670 1.1 elric }
671 1.1 elric
672 1.1 elric t = strtok_r(NULL, " \t", &p);
673 1.1 elric if(t == NULL) {
674 1.1 elric kdc_log(context, config, 0,
675 1.1 elric "Missing HTTP GET data in request from %s", d->addr_string);
676 1.1 elric return -1;
677 1.1 elric }
678 1.1 elric
679 1.1 elric data = malloc(strlen(t));
680 1.1 elric if (data == NULL) {
681 1.1 elric kdc_log(context, config, 0, "Failed to allocate %lu bytes",
682 1.1 elric (unsigned long)strlen(t));
683 1.1 elric return -1;
684 1.1 elric }
685 1.1 elric if(*t == '/')
686 1.1 elric t++;
687 1.1 elric if(de_http(t) != 0) {
688 1.1 elric kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
689 1.1 elric kdc_log(context, config, 5, "HTTP request: %s", t);
690 1.1 elric free(data);
691 1.1 elric return -1;
692 1.1 elric }
693 1.1 elric proto = strtok_r(NULL, " \t", &p);
694 1.1 elric if (proto == NULL) {
695 1.1 elric kdc_log(context, config, 0, "Malformed HTTP request from %s", d->addr_string);
696 1.1 elric free(data);
697 1.1 elric return -1;
698 1.1 elric }
699 1.1.1.3 christos len = rk_base64_decode(t, data);
700 1.1 elric if(len <= 0){
701 1.1 elric const char *msg =
702 1.1 elric " 404 Not found\r\n"
703 1.1 elric "Server: Heimdal/" VERSION "\r\n"
704 1.1 elric "Cache-Control: no-cache\r\n"
705 1.1 elric "Pragma: no-cache\r\n"
706 1.1 elric "Content-type: text/html\r\n"
707 1.1 elric "Content-transfer-encoding: 8bit\r\n\r\n"
708 1.1 elric "<TITLE>404 Not found</TITLE>\r\n"
709 1.1 elric "<H1>404 Not found</H1>\r\n"
710 1.1 elric "That page doesn't exist, maybe you are looking for "
711 1.1 elric "<A HREF=\"http://www.h5l.org/\">Heimdal</A>?\r\n";
712 1.1 elric kdc_log(context, config, 0, "HTTP request from %s is non KDC request", d->addr_string);
713 1.1 elric kdc_log(context, config, 5, "HTTP request: %s", t);
714 1.1 elric free(data);
715 1.1 elric if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
716 1.1 elric kdc_log(context, config, 0, "HTTP write failed: %s: %s",
717 1.1 elric d->addr_string, strerror(rk_SOCK_ERRNO));
718 1.1 elric return -1;
719 1.1 elric }
720 1.1 elric if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
721 1.1 elric kdc_log(context, config, 0, "HTTP write failed: %s: %s",
722 1.1 elric d->addr_string, strerror(rk_SOCK_ERRNO));
723 1.1 elric return -1;
724 1.1 elric }
725 1.1 elric return -1;
726 1.1 elric }
727 1.1 elric {
728 1.1 elric const char *msg =
729 1.1 elric " 200 OK\r\n"
730 1.1 elric "Server: Heimdal/" VERSION "\r\n"
731 1.1 elric "Cache-Control: no-cache\r\n"
732 1.1 elric "Pragma: no-cache\r\n"
733 1.1 elric "Content-type: application/octet-stream\r\n"
734 1.1 elric "Content-transfer-encoding: binary\r\n\r\n";
735 1.1 elric if (rk_IS_SOCKET_ERROR(send(d->s, proto, strlen(proto), 0))) {
736 1.1 elric free(data);
737 1.1 elric kdc_log(context, config, 0, "HTTP write failed: %s: %s",
738 1.1 elric d->addr_string, strerror(rk_SOCK_ERRNO));
739 1.1 elric return -1;
740 1.1 elric }
741 1.1 elric if (rk_IS_SOCKET_ERROR(send(d->s, msg, strlen(msg), 0))) {
742 1.1 elric free(data);
743 1.1 elric kdc_log(context, config, 0, "HTTP write failed: %s: %s",
744 1.1 elric d->addr_string, strerror(rk_SOCK_ERRNO));
745 1.1 elric return -1;
746 1.1 elric }
747 1.1 elric }
748 1.1.1.2 pettai if ((size_t)len > d->len)
749 1.1 elric len = d->len;
750 1.1 elric memcpy(d->buf, data, len);
751 1.1 elric d->len = len;
752 1.1 elric free(data);
753 1.1 elric return 1;
754 1.1 elric }
755 1.1 elric
756 1.1 elric /*
757 1.1 elric * Handle incoming data to the TCP socket in `d[index]'
758 1.1 elric */
759 1.1 elric
760 1.1 elric static void
761 1.1 elric handle_tcp(krb5_context context,
762 1.1 elric krb5_kdc_configuration *config,
763 1.1 elric struct descr *d, int idx, int min_free)
764 1.1 elric {
765 1.1 elric unsigned char buf[1024];
766 1.1 elric int n;
767 1.1 elric int ret = 0;
768 1.1 elric
769 1.1 elric if (d[idx].timeout == 0) {
770 1.1 elric add_new_tcp (context, config, d, idx, min_free);
771 1.1 elric return;
772 1.1 elric }
773 1.1 elric
774 1.1 elric n = recvfrom(d[idx].s, buf, sizeof(buf), 0, NULL, NULL);
775 1.1 elric if(rk_IS_SOCKET_ERROR(n)){
776 1.1 elric krb5_warn(context, rk_SOCK_ERRNO, "recvfrom failed from %s to %s/%d",
777 1.1 elric d[idx].addr_string, descr_type(d + idx),
778 1.1 elric ntohs(d[idx].port));
779 1.1 elric return;
780 1.1 elric } else if (n == 0) {
781 1.1 elric krb5_warnx(context, "connection closed before end of data after %lu "
782 1.1 elric "bytes from %s to %s/%d", (unsigned long)d[idx].len,
783 1.1 elric d[idx].addr_string, descr_type(d + idx),
784 1.1 elric ntohs(d[idx].port));
785 1.1 elric clear_descr (d + idx);
786 1.1 elric return;
787 1.1 elric }
788 1.1 elric if (grow_descr (context, config, &d[idx], n))
789 1.1 elric return;
790 1.1 elric memcpy(d[idx].buf + d[idx].len, buf, n);
791 1.1 elric d[idx].len += n;
792 1.1 elric if(d[idx].len > 4 && d[idx].buf[0] == 0) {
793 1.1 elric ret = handle_vanilla_tcp (context, config, &d[idx]);
794 1.1 elric } else if(enable_http &&
795 1.1 elric d[idx].len >= 4 &&
796 1.1 elric strncmp((char *)d[idx].buf, "GET ", 4) == 0 &&
797 1.1 elric strncmp((char *)d[idx].buf + d[idx].len - 4,
798 1.1 elric "\r\n\r\n", 4) == 0) {
799 1.1 elric
800 1.1 elric /* remove the trailing \r\n\r\n so the string is NUL terminated */
801 1.1 elric d[idx].buf[d[idx].len - 4] = '\0';
802 1.1 elric
803 1.1 elric ret = handle_http_tcp (context, config, &d[idx]);
804 1.1 elric if (ret < 0)
805 1.1 elric clear_descr (d + idx);
806 1.1 elric } else if (d[idx].len > 4) {
807 1.1 elric kdc_log (context, config,
808 1.1 elric 0, "TCP data of strange type from %s to %s/%d",
809 1.1 elric d[idx].addr_string, descr_type(d + idx),
810 1.1 elric ntohs(d[idx].port));
811 1.1 elric if (d[idx].buf[0] & 0x80) {
812 1.1 elric krb5_data reply;
813 1.1 elric
814 1.1 elric kdc_log (context, config, 0, "TCP extension not supported");
815 1.1 elric
816 1.1 elric ret = krb5_mk_error(context,
817 1.1 elric KRB5KRB_ERR_FIELD_TOOLONG,
818 1.1 elric NULL,
819 1.1 elric NULL,
820 1.1 elric NULL,
821 1.1 elric NULL,
822 1.1 elric NULL,
823 1.1 elric NULL,
824 1.1 elric &reply);
825 1.1 elric if (ret == 0) {
826 1.1 elric send_reply(context, config, TRUE, d + idx, &reply);
827 1.1 elric krb5_data_free(&reply);
828 1.1 elric }
829 1.1 elric }
830 1.1 elric clear_descr(d + idx);
831 1.1 elric return;
832 1.1 elric }
833 1.1 elric if (ret < 0)
834 1.1 elric return;
835 1.1 elric else if (ret == 1) {
836 1.1 elric do_request(context, config,
837 1.1 elric d[idx].buf, d[idx].len, TRUE, &d[idx]);
838 1.1 elric clear_descr(d + idx);
839 1.1 elric }
840 1.1 elric }
841 1.1 elric
842 1.1.1.3 christos #ifdef HAVE_FORK
843 1.1.1.3 christos static void
844 1.1.1.3 christos handle_islive(int fd)
845 1.1.1.3 christos {
846 1.1.1.3 christos char buf;
847 1.1.1.3 christos int ret;
848 1.1.1.3 christos
849 1.1.1.3 christos ret = read(fd, &buf, 1);
850 1.1.1.3 christos if (ret != 1)
851 1.1.1.3 christos exit_flag = -1;
852 1.1.1.3 christos }
853 1.1.1.3 christos #endif
854 1.1.1.3 christos
855 1.1.1.2 pettai krb5_boolean
856 1.1.1.2 pettai realloc_descrs(struct descr **d, unsigned int *ndescr)
857 1.1.1.2 pettai {
858 1.1.1.2 pettai struct descr *tmp;
859 1.1.1.2 pettai size_t i;
860 1.1.1.2 pettai
861 1.1.1.2 pettai tmp = realloc(*d, (*ndescr + 4) * sizeof(**d));
862 1.1.1.2 pettai if(tmp == NULL)
863 1.1.1.2 pettai return FALSE;
864 1.1.1.2 pettai
865 1.1.1.2 pettai *d = tmp;
866 1.1.1.2 pettai reinit_descrs (*d, *ndescr);
867 1.1.1.2 pettai memset(*d + *ndescr, 0, 4 * sizeof(**d));
868 1.1.1.2 pettai for(i = *ndescr; i < *ndescr + 4; i++)
869 1.1.1.2 pettai init_descr (*d + i);
870 1.1.1.2 pettai
871 1.1.1.2 pettai *ndescr += 4;
872 1.1.1.2 pettai
873 1.1.1.2 pettai return TRUE;
874 1.1.1.2 pettai }
875 1.1.1.2 pettai
876 1.1.1.2 pettai int
877 1.1.1.2 pettai next_min_free(krb5_context context, struct descr **d, unsigned int *ndescr)
878 1.1.1.2 pettai {
879 1.1.1.2 pettai size_t i;
880 1.1.1.2 pettai int min_free;
881 1.1.1.2 pettai
882 1.1.1.2 pettai for(i = 0; i < *ndescr; i++) {
883 1.1.1.2 pettai int s = (*d + i)->s;
884 1.1.1.2 pettai if(rk_IS_BAD_SOCKET(s))
885 1.1.1.2 pettai return i;
886 1.1.1.2 pettai }
887 1.1.1.2 pettai
888 1.1.1.2 pettai min_free = *ndescr;
889 1.1.1.2 pettai if(!realloc_descrs(d, ndescr)) {
890 1.1.1.2 pettai min_free = -1;
891 1.1.1.2 pettai krb5_warnx(context, "No memory");
892 1.1.1.2 pettai }
893 1.1.1.2 pettai
894 1.1.1.2 pettai return min_free;
895 1.1.1.2 pettai }
896 1.1.1.2 pettai
897 1.1.1.3 christos static void
898 1.1.1.3 christos loop(krb5_context context, krb5_kdc_configuration *config,
899 1.1.1.3 christos struct descr *d, unsigned int ndescr, int islive)
900 1.1 elric {
901 1.1 elric
902 1.1.1.3 christos while (exit_flag == 0) {
903 1.1 elric struct timeval tmout;
904 1.1 elric fd_set fds;
905 1.1 elric int min_free = -1;
906 1.1 elric int max_fd = 0;
907 1.1.1.2 pettai size_t i;
908 1.1 elric
909 1.1 elric FD_ZERO(&fds);
910 1.1.1.3 christos if (islive > -1) {
911 1.1.1.3 christos FD_SET(islive, &fds);
912 1.1.1.3 christos max_fd = islive;
913 1.1.1.3 christos }
914 1.1.1.3 christos for (i = 0; i < ndescr; i++) {
915 1.1.1.3 christos if (!rk_IS_BAD_SOCKET(d[i].s)) {
916 1.1.1.3 christos if (d[i].type == SOCK_STREAM &&
917 1.1 elric d[i].timeout && d[i].timeout < time(NULL)) {
918 1.1 elric kdc_log(context, config, 1,
919 1.1 elric "TCP-connection from %s expired after %lu bytes",
920 1.1 elric d[i].addr_string, (unsigned long)d[i].len);
921 1.1 elric clear_descr(&d[i]);
922 1.1 elric continue;
923 1.1 elric }
924 1.1 elric #ifndef NO_LIMIT_FD_SETSIZE
925 1.1.1.3 christos if (max_fd < d[i].s)
926 1.1 elric max_fd = d[i].s;
927 1.1 elric #ifdef FD_SETSIZE
928 1.1 elric if (max_fd >= FD_SETSIZE)
929 1.1 elric krb5_errx(context, 1, "fd too large");
930 1.1 elric #endif
931 1.1 elric #endif
932 1.1 elric FD_SET(d[i].s, &fds);
933 1.1 elric }
934 1.1 elric }
935 1.1 elric
936 1.1 elric tmout.tv_sec = TCP_TIMEOUT;
937 1.1 elric tmout.tv_usec = 0;
938 1.1 elric switch(select(max_fd + 1, &fds, 0, 0, &tmout)){
939 1.1 elric case 0:
940 1.1 elric break;
941 1.1 elric case -1:
942 1.1 elric if (errno != EINTR)
943 1.1 elric krb5_warn(context, rk_SOCK_ERRNO, "select");
944 1.1 elric break;
945 1.1 elric default:
946 1.1.1.3 christos #ifdef HAVE_FORK
947 1.1.1.3 christos if (islive > -1 && FD_ISSET(islive, &fds))
948 1.1.1.3 christos handle_islive(islive);
949 1.1.1.3 christos #endif
950 1.1.1.3 christos for (i = 0; i < ndescr; i++)
951 1.1.1.3 christos if (!rk_IS_BAD_SOCKET(d[i].s) && FD_ISSET(d[i].s, &fds)) {
952 1.1.1.3 christos min_free = next_min_free(context, &d, &ndescr);
953 1.1.1.3 christos
954 1.1.1.3 christos if (d[i].type == SOCK_DGRAM)
955 1.1.1.3 christos handle_udp(context, config, &d[i]);
956 1.1.1.3 christos else if (d[i].type == SOCK_STREAM)
957 1.1.1.3 christos handle_tcp(context, config, d, i, min_free);
958 1.1 elric }
959 1.1 elric }
960 1.1 elric }
961 1.1.1.3 christos
962 1.1.1.3 christos switch (exit_flag) {
963 1.1.1.3 christos case -1:
964 1.1.1.3 christos kdc_log(context, config, 0,
965 1.1.1.3 christos "KDC worker process exiting because KDC master exited.");
966 1.1.1.3 christos break;
967 1.1 elric #ifdef SIGXCPU
968 1.1.1.3 christos case SIGXCPU:
969 1.1 elric kdc_log(context, config, 0, "CPU time limit exceeded");
970 1.1.1.3 christos break;
971 1.1 elric #endif
972 1.1.1.3 christos case SIGINT:
973 1.1.1.3 christos case SIGTERM:
974 1.1 elric kdc_log(context, config, 0, "Terminated");
975 1.1.1.3 christos break;
976 1.1.1.3 christos default:
977 1.1 elric kdc_log(context, config, 0, "Unexpected exit reason: %d", exit_flag);
978 1.1.1.3 christos break;
979 1.1.1.3 christos }
980 1.1.1.3 christos }
981 1.1.1.3 christos
982 1.1.1.3 christos #ifdef __APPLE__
983 1.1.1.3 christos static void
984 1.1.1.3 christos bonjour_kid(krb5_context context, krb5_kdc_configuration *config, const char *argv0, int *islive)
985 1.1.1.3 christos {
986 1.1.1.3 christos char buf;
987 1.1.1.3 christos
988 1.1.1.3 christos if (do_bonjour > 0) {
989 1.1.1.3 christos bonjour_announce(context, config);
990 1.1.1.3 christos
991 1.1.1.3 christos while (read(0, &buf, 1) == 1)
992 1.1.1.3 christos continue;
993 1.1.1.3 christos _exit(0);
994 1.1.1.3 christos }
995 1.1.1.3 christos
996 1.1.1.3 christos if ((bonjour_pid = fork()) != 0)
997 1.1.1.3 christos return;
998 1.1.1.3 christos
999 1.1.1.3 christos close(islive[0]);
1000 1.1.1.3 christos if (dup2(islive[1], 0) == -1)
1001 1.1.1.3 christos err(1, "failed to announce with bonjour (dup)");
1002 1.1.1.3 christos if (islive[1] != 0)
1003 1.1.1.3 christos close(islive[1]);
1004 1.1.1.3 christos execlp(argv0, "kdc", "--bonjour", NULL);
1005 1.1.1.3 christos err(1, "failed to announce with bonjour (exec)");
1006 1.1.1.3 christos }
1007 1.1.1.3 christos #endif
1008 1.1.1.3 christos
1009 1.1.1.3 christos #ifdef HAVE_FORK
1010 1.1.1.3 christos static void
1011 1.1.1.3 christos kill_kids(pid_t *pids, int max_kids, int sig)
1012 1.1.1.3 christos {
1013 1.1.1.3 christos int i;
1014 1.1.1.3 christos
1015 1.1.1.3 christos for (i=0; i < max_kids; i++)
1016 1.1.1.3 christos if (pids[i] > 0)
1017 1.1.1.3 christos kill(sig, pids[i]);
1018 1.1.1.3 christos if (bonjour_pid > 0)
1019 1.1.1.3 christos kill(sig, bonjour_pid);
1020 1.1.1.3 christos }
1021 1.1.1.3 christos
1022 1.1.1.3 christos static int
1023 1.1.1.3 christos reap_kid(krb5_context context, krb5_kdc_configuration *config,
1024 1.1.1.3 christos pid_t *pids, int max_kids, int options)
1025 1.1.1.3 christos {
1026 1.1.1.3 christos pid_t pid;
1027 1.1.1.3 christos char *what;
1028 1.1.1.3 christos int status;
1029 1.1.1.3 christos int i = 0; /* quiet warnings */
1030 1.1.1.3 christos
1031 1.1.1.3 christos pid = waitpid(-1, &status, options);
1032 1.1.1.3 christos if (pid < 1)
1033 1.1.1.3 christos return 0;
1034 1.1.1.3 christos
1035 1.1.1.3 christos if (pid != bonjour_pid) {
1036 1.1.1.3 christos for (i=0; i < max_kids; i++) {
1037 1.1.1.3 christos if (pids[i] == pid)
1038 1.1.1.3 christos break;
1039 1.1.1.3 christos }
1040 1.1.1.3 christos
1041 1.1.1.3 christos if (i == max_kids) {
1042 1.1.1.3 christos /* XXXrcd: this should not happen, have to do something, though */
1043 1.1.1.3 christos return 0;
1044 1.1.1.3 christos }
1045 1.1.1.3 christos }
1046 1.1.1.3 christos
1047 1.1.1.3 christos if (pid == bonjour_pid)
1048 1.1.1.3 christos what = "bonjour";
1049 1.1.1.3 christos else
1050 1.1.1.3 christos what = "worker";
1051 1.1.1.3 christos if (WIFEXITED(status))
1052 1.1.1.3 christos kdc_log(context, config, 0, "KDC reaped %s process: %d, exit status: %d",
1053 1.1.1.3 christos what, (int)pid, WEXITSTATUS(status));
1054 1.1.1.3 christos else if (WIFSIGNALED(status))
1055 1.1.1.3 christos kdc_log(context, config, 0, "KDC reaped %s process: %d, term signal %d%s",
1056 1.1.1.3 christos what, (int)pid, WTERMSIG(status),
1057 1.1.1.3 christos WCOREDUMP(status) ? " (core dumped)" : "");
1058 1.1.1.3 christos else
1059 1.1.1.3 christos kdc_log(context, config, 0, "KDC reaped %s process: %d",
1060 1.1.1.3 christos what, (int)pid);
1061 1.1.1.3 christos if (pid == bonjour_pid) {
1062 1.1.1.3 christos bonjour_pid = (pid_t)-1;
1063 1.1.1.3 christos return 0;
1064 1.1.1.3 christos } else {
1065 1.1.1.4 christos pids[i] = (pid_t)0;
1066 1.1.1.3 christos return 1;
1067 1.1.1.3 christos }
1068 1.1.1.3 christos }
1069 1.1.1.3 christos
1070 1.1.1.3 christos static int
1071 1.1.1.3 christos reap_kids(krb5_context context, krb5_kdc_configuration *config,
1072 1.1.1.3 christos pid_t *pids, int max_kids)
1073 1.1.1.3 christos {
1074 1.1.1.3 christos int reaped = 0;
1075 1.1.1.3 christos
1076 1.1.1.3 christos for (;;) {
1077 1.1.1.3 christos if (reap_kid(context, config, pids, max_kids, WNOHANG) == 0)
1078 1.1.1.3 christos break;
1079 1.1.1.3 christos reaped++;
1080 1.1.1.3 christos }
1081 1.1.1.3 christos
1082 1.1.1.3 christos return reaped;
1083 1.1.1.3 christos }
1084 1.1.1.3 christos
1085 1.1.1.3 christos static void
1086 1.1.1.3 christos select_sleep(int microseconds)
1087 1.1.1.3 christos {
1088 1.1.1.3 christos struct timeval tv;
1089 1.1.1.3 christos
1090 1.1.1.3 christos tv.tv_sec = microseconds / 1000000;
1091 1.1.1.3 christos tv.tv_usec = microseconds % 1000000;
1092 1.1.1.3 christos select(0, NULL, NULL, NULL, &tv);
1093 1.1.1.3 christos }
1094 1.1.1.3 christos #endif
1095 1.1.1.3 christos
1096 1.1.1.3 christos void
1097 1.1.1.3 christos start_kdc(krb5_context context,
1098 1.1.1.3 christos krb5_kdc_configuration *config, const char *argv0)
1099 1.1.1.3 christos {
1100 1.1.1.3 christos struct timeval tv1;
1101 1.1.1.3 christos struct timeval tv2;
1102 1.1.1.3 christos struct descr *d;
1103 1.1.1.3 christos unsigned int ndescr;
1104 1.1.1.3 christos pid_t pid = -1;
1105 1.1.1.3 christos #ifdef HAVE_FORK
1106 1.1.1.3 christos pid_t *pids;
1107 1.1.1.3 christos int max_kdcs = config->num_kdc_processes;
1108 1.1.1.3 christos int num_kdcs = 0;
1109 1.1.1.3 christos int i;
1110 1.1.1.3 christos int islive[2];
1111 1.1.1.3 christos #endif
1112 1.1.1.3 christos
1113 1.1.1.3 christos #ifdef __APPLE__
1114 1.1.1.3 christos if (do_bonjour > 0)
1115 1.1.1.3 christos bonjour_kid(context, config, argv0, NULL);
1116 1.1.1.3 christos #endif
1117 1.1.1.3 christos
1118 1.1.1.3 christos #ifdef HAVE_FORK
1119 1.1.1.3 christos #ifdef _SC_NPROCESSORS_ONLN
1120 1.1.1.3 christos if (max_kdcs < 1)
1121 1.1.1.3 christos max_kdcs = sysconf(_SC_NPROCESSORS_ONLN);
1122 1.1.1.3 christos #endif
1123 1.1.1.3 christos
1124 1.1.1.3 christos if (max_kdcs < 1)
1125 1.1.1.3 christos max_kdcs = 1;
1126 1.1.1.3 christos
1127 1.1.1.3 christos pids = calloc(max_kdcs, sizeof(*pids));
1128 1.1.1.3 christos if (!pids)
1129 1.1.1.3 christos krb5_err(context, 1, errno, "malloc");
1130 1.1.1.3 christos
1131 1.1.1.3 christos /*
1132 1.1.1.3 christos * We open a socketpair of which we hand one end to each of our kids.
1133 1.1.1.3 christos * When we exit, for whatever reason, the children will notice an EOF
1134 1.1.1.3 christos * on their end and be able to cleanly exit.
1135 1.1.1.3 christos */
1136 1.1.1.3 christos
1137 1.1.1.4 christos if (socketpair(PF_UNIX, SOCK_STREAM, 0, islive) == -1)
1138 1.1.1.3 christos krb5_errx(context, 1, "socketpair");
1139 1.1.1.3 christos socket_set_nonblocking(islive[1], 1);
1140 1.1.1.3 christos #endif
1141 1.1.1.3 christos
1142 1.1.1.3 christos ndescr = init_sockets(context, config, &d);
1143 1.1.1.3 christos if(ndescr <= 0)
1144 1.1.1.3 christos krb5_errx(context, 1, "No sockets!");
1145 1.1.1.3 christos
1146 1.1.1.3 christos #ifdef HAVE_FORK
1147 1.1.1.3 christos
1148 1.1.1.3 christos # ifdef __APPLE__
1149 1.1.1.3 christos if (do_bonjour < 0)
1150 1.1.1.3 christos bonjour_kid(context, config, argv0, islive);
1151 1.1.1.3 christos # endif
1152 1.1.1.3 christos
1153 1.1.1.3 christos kdc_log(context, config, 0, "KDC started master process pid=%d", getpid());
1154 1.1.1.3 christos #else
1155 1.1.1.3 christos kdc_log(context, config, 0, "KDC started pid=%d", getpid());
1156 1.1.1.3 christos #endif
1157 1.1.1.3 christos
1158 1.1.1.3 christos roken_detach_finish(NULL, daemon_child);
1159 1.1.1.3 christos
1160 1.1.1.3 christos tv1.tv_sec = 0;
1161 1.1.1.3 christos tv1.tv_usec = 0;
1162 1.1.1.3 christos
1163 1.1.1.3 christos #ifdef HAVE_FORK
1164 1.1.1.3 christos if (!testing_flag) {
1165 1.1.1.3 christos /* Note that we might never execute the body of this loop */
1166 1.1.1.3 christos while (exit_flag == 0) {
1167 1.1.1.3 christos
1168 1.1.1.3 christos /* Slow down the creation of KDCs... */
1169 1.1.1.3 christos
1170 1.1.1.3 christos gettimeofday(&tv2, NULL);
1171 1.1.1.3 christos if (tv1.tv_sec == tv2.tv_sec && tv2.tv_usec - tv1.tv_usec < 25000) {
1172 1.1.1.3 christos #if 0 /* XXXrcd: should print a message... */
1173 1.1.1.3 christos kdc_log(context, config, 0, "Spawning KDCs too quickly, "
1174 1.1.1.3 christos "pausing for 50ms");
1175 1.1.1.3 christos #endif
1176 1.1.1.3 christos select_sleep(12500);
1177 1.1.1.3 christos continue;
1178 1.1.1.3 christos }
1179 1.1.1.3 christos
1180 1.1.1.3 christos if (num_kdcs >= max_kdcs) {
1181 1.1.1.3 christos num_kdcs -= reap_kid(context, config, pids, max_kdcs, 0);
1182 1.1.1.3 christos continue;
1183 1.1.1.3 christos }
1184 1.1.1.3 christos
1185 1.1.1.3 christos if (num_kdcs > 0)
1186 1.1.1.3 christos num_kdcs -= reap_kids(context, config, pids, max_kdcs);
1187 1.1.1.3 christos
1188 1.1.1.3 christos pid = fork();
1189 1.1.1.3 christos switch (pid) {
1190 1.1.1.3 christos case 0:
1191 1.1.1.3 christos close(islive[0]);
1192 1.1.1.3 christos loop(context, config, d, ndescr, islive[1]);
1193 1.1.1.3 christos exit(0);
1194 1.1.1.3 christos case -1:
1195 1.1.1.3 christos /* XXXrcd: hmmm, do something useful?? */
1196 1.1.1.3 christos kdc_log(context, config, 0,
1197 1.1.1.3 christos "KDC master process could not fork worker process");
1198 1.1.1.3 christos sleep(10);
1199 1.1.1.3 christos break;
1200 1.1.1.3 christos default:
1201 1.1.1.3 christos for (i=0; i < max_kdcs; i++) {
1202 1.1.1.3 christos if (pids[i] == 0) {
1203 1.1.1.3 christos pids[i] = pid;
1204 1.1.1.3 christos break;
1205 1.1.1.3 christos }
1206 1.1.1.3 christos }
1207 1.1.1.3 christos kdc_log(context, config, 0, "KDC worker process started: %d",
1208 1.1.1.3 christos pid);
1209 1.1.1.3 christos num_kdcs++;
1210 1.1.1.3 christos gettimeofday(&tv1, NULL);
1211 1.1.1.3 christos break;
1212 1.1.1.3 christos }
1213 1.1.1.3 christos }
1214 1.1.1.3 christos
1215 1.1.1.3 christos /* Closing these sockets should cause the kids to die... */
1216 1.1.1.3 christos
1217 1.1.1.3 christos close(islive[0]);
1218 1.1.1.3 christos close(islive[1]);
1219 1.1.1.3 christos
1220 1.1.1.3 christos /* Close our listener sockets before terminating workers */
1221 1.1.1.3 christos for (i = 0; i < ndescr; ++i)
1222 1.1.1.3 christos clear_descr(&d[i]);
1223 1.1.1.3 christos
1224 1.1.1.3 christos gettimeofday(&tv1, NULL);
1225 1.1.1.3 christos tv2 = tv1;
1226 1.1.1.3 christos
1227 1.1.1.3 christos /* Reap every 10ms, terminate stragglers once a second, give up after 10 */
1228 1.1.1.3 christos for (;;) {
1229 1.1.1.3 christos struct timeval tv3;
1230 1.1.1.3 christos num_kdcs -= reap_kids(context, config, pids, max_kdcs);
1231 1.1.1.3 christos if (num_kdcs == 0 && bonjour_pid <= 0)
1232 1.1.1.3 christos goto end;
1233 1.1.1.3 christos /*
1234 1.1.1.3 christos * Using select to sleep will fail with EINTR if we receive a
1235 1.1.1.3 christos * SIGCHLD. This is desirable.
1236 1.1.1.3 christos */
1237 1.1.1.3 christos select_sleep(10000);
1238 1.1.1.3 christos gettimeofday(&tv3, NULL);
1239 1.1.1.3 christos if (tv3.tv_sec - tv1.tv_sec > 10 ||
1240 1.1.1.3 christos (tv3.tv_sec - tv1.tv_sec == 10 && tv3.tv_usec >= tv1.tv_usec))
1241 1.1.1.3 christos break;
1242 1.1.1.3 christos if (tv3.tv_sec - tv2.tv_sec > 1 ||
1243 1.1.1.3 christos (tv3.tv_sec - tv2.tv_sec == 1 && tv3.tv_usec >= tv2.tv_usec)) {
1244 1.1.1.3 christos kill_kids(pids, max_kdcs, SIGTERM);
1245 1.1.1.3 christos tv2 = tv3;
1246 1.1.1.3 christos }
1247 1.1.1.3 christos }
1248 1.1.1.3 christos
1249 1.1.1.3 christos /* Kill stragglers and reap every 200ms, give up after 15s */
1250 1.1.1.3 christos for (;;) {
1251 1.1.1.3 christos kill_kids(pids, max_kdcs, SIGKILL);
1252 1.1.1.3 christos num_kdcs -= reap_kids(context, config, pids, max_kdcs);
1253 1.1.1.3 christos if (num_kdcs == 0 && bonjour_pid <= 0)
1254 1.1.1.3 christos break;
1255 1.1.1.3 christos select_sleep(200000);
1256 1.1.1.3 christos gettimeofday(&tv2, NULL);
1257 1.1.1.3 christos if (tv2.tv_sec - tv1.tv_sec > 15 ||
1258 1.1.1.3 christos (tv2.tv_sec - tv1.tv_sec == 15 && tv2.tv_usec >= tv1.tv_usec))
1259 1.1.1.3 christos break;
1260 1.1.1.3 christos }
1261 1.1.1.3 christos
1262 1.1.1.3 christos end:
1263 1.1.1.3 christos kdc_log(context, config, 0, "KDC master process exiting", pid);
1264 1.1.1.3 christos free(pids);
1265 1.1.1.3 christos } else {
1266 1.1.1.3 christos loop(context, config, d, ndescr, -1);
1267 1.1.1.3 christos kdc_log(context, config, 0, "KDC exiting", pid);
1268 1.1.1.3 christos }
1269 1.1.1.3 christos #else
1270 1.1.1.3 christos loop(context, config, d, ndescr, -1);
1271 1.1.1.3 christos kdc_log(context, config, 0, "KDC exiting", pid);
1272 1.1.1.3 christos #endif
1273 1.1.1.3 christos
1274 1.1.1.3 christos free(d);
1275 1.1 elric }
1276