heimdal: Use krb5_free_default_realm() for free()
[heimdal.git] / kpasswd / kpasswdd.c
blob6459f5e44fe57acb8c3403afbaa612d50a03d041
1 /*
2 * Copyright (c) 1997-2005 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 "kpasswd_locl.h"
35 RCSID("$Id$");
37 #include <kadm5/admin.h>
38 #ifdef HAVE_SYS_UN_H
39 #include <sys/un.h>
40 #endif
41 #include <hdb.h>
42 #include <kadm5/private.h>
44 static krb5_context context;
45 static krb5_log_facility *log_facility;
47 static struct getarg_strings addresses_str;
48 krb5_addresses explicit_addresses;
50 static sig_atomic_t exit_flag = 0;
52 static void
53 add_one_address (const char *str, int first)
55 krb5_error_code ret;
56 krb5_addresses tmp;
58 ret = krb5_parse_address (context, str, &tmp);
59 if (ret)
60 krb5_err (context, 1, ret, "parse_address `%s'", str);
61 if (first)
62 krb5_copy_addresses(context, &tmp, &explicit_addresses);
63 else
64 krb5_append_addresses(context, &explicit_addresses, &tmp);
65 krb5_free_addresses (context, &tmp);
68 static void
69 send_reply (int s,
70 struct sockaddr *sa,
71 int sa_size,
72 krb5_data *ap_rep,
73 krb5_data *rest)
75 struct msghdr msghdr;
76 struct iovec iov[3];
77 uint16_t len, ap_rep_len;
78 u_char header[6];
79 u_char *p;
81 if (ap_rep)
82 ap_rep_len = ap_rep->length;
83 else
84 ap_rep_len = 0;
86 len = 6 + ap_rep_len + rest->length;
87 p = header;
88 *p++ = (len >> 8) & 0xFF;
89 *p++ = (len >> 0) & 0xFF;
90 *p++ = 0;
91 *p++ = 1;
92 *p++ = (ap_rep_len >> 8) & 0xFF;
93 *p++ = (ap_rep_len >> 0) & 0xFF;
95 memset (&msghdr, 0, sizeof(msghdr));
96 msghdr.msg_name = (void *)sa;
97 msghdr.msg_namelen = sa_size;
98 msghdr.msg_iov = iov;
99 msghdr.msg_iovlen = sizeof(iov)/sizeof(*iov);
100 #if 0
101 msghdr.msg_control = NULL;
102 msghdr.msg_controllen = 0;
103 #endif
105 iov[0].iov_base = (char *)header;
106 iov[0].iov_len = 6;
107 if (ap_rep_len) {
108 iov[1].iov_base = ap_rep->data;
109 iov[1].iov_len = ap_rep->length;
110 } else {
111 iov[1].iov_base = NULL;
112 iov[1].iov_len = 0;
114 iov[2].iov_base = rest->data;
115 iov[2].iov_len = rest->length;
117 if (sendmsg (s, &msghdr, 0) < 0)
118 krb5_warn (context, errno, "sendmsg");
121 static int
122 make_result (krb5_data *data,
123 uint16_t result_code,
124 const char *expl)
126 krb5_error_code ret;
127 krb5_storage *sp;
129 sp = krb5_storage_emem();
130 if (sp == NULL) goto out;
131 ret = krb5_store_uint16(sp, result_code);
132 if (ret) goto out;
133 ret = krb5_store_stringz(sp, expl);
134 if (ret) goto out;
135 ret = krb5_storage_to_data(sp, data);
136 if (ret) goto out;
137 krb5_storage_free(sp);
139 return 0;
140 out:
141 if (sp)
142 krb5_storage_free(sp);
144 krb5_warnx (context, "Out of memory generating error reply");
145 return 1;
148 static void
149 reply_error (krb5_realm realm,
150 int s,
151 struct sockaddr *sa,
152 int sa_size,
153 krb5_error_code error_code,
154 uint16_t result_code,
155 const char *expl)
157 krb5_error_code ret;
158 krb5_data error_data;
159 krb5_data e_data;
160 krb5_principal server = NULL;
162 if (make_result(&e_data, result_code, expl))
163 return;
165 if (realm) {
166 ret = krb5_make_principal (context, &server, realm,
167 "kadmin", "changepw", NULL);
168 if (ret) {
169 krb5_data_free (&e_data);
170 return;
174 ret = krb5_mk_error (context,
175 error_code,
176 NULL,
177 &e_data,
178 NULL,
179 server,
180 NULL,
181 NULL,
182 &error_data);
183 if (server)
184 krb5_free_principal(context, server);
185 krb5_data_free (&e_data);
186 if (ret) {
187 krb5_warn (context, ret, "Could not even generate error reply");
188 return;
190 send_reply (s, sa, sa_size, NULL, &error_data);
191 krb5_data_free (&error_data);
194 static void
195 reply_priv (krb5_auth_context auth_context,
196 int s,
197 struct sockaddr *sa,
198 int sa_size,
199 uint16_t result_code,
200 const char *expl)
202 krb5_error_code ret;
203 krb5_data krb_priv_data;
204 krb5_data ap_rep_data;
205 krb5_data e_data;
207 ret = krb5_mk_rep (context,
208 auth_context,
209 &ap_rep_data);
210 if (ret) {
211 krb5_warn (context, ret, "Could not even generate error reply");
212 return;
215 if (make_result(&e_data, result_code, expl))
216 return;
218 ret = krb5_mk_priv (context,
219 auth_context,
220 &e_data,
221 &krb_priv_data,
222 NULL);
223 krb5_data_free (&e_data);
224 if (ret) {
225 krb5_warn (context, ret, "Could not even generate error reply");
226 return;
228 send_reply (s, sa, sa_size, &ap_rep_data, &krb_priv_data);
229 krb5_data_free (&ap_rep_data);
230 krb5_data_free (&krb_priv_data);
234 * Change the password for `principal', sending the reply back on `s'
235 * (`sa', `sa_size') to `pwd_data'.
238 static void
239 change (krb5_auth_context auth_context,
240 krb5_principal admin_principal,
241 uint16_t version,
242 int s,
243 struct sockaddr *sa,
244 int sa_size,
245 krb5_data *in_data)
247 krb5_error_code ret;
248 char *client = NULL, *admin = NULL;
249 const char *pwd_reason;
250 kadm5_config_params conf;
251 void *kadm5_handle = NULL;
252 krb5_principal principal = NULL;
253 krb5_data *pwd_data = NULL;
254 char *tmp;
255 ChangePasswdDataMS chpw;
257 memset (&conf, 0, sizeof(conf));
258 memset(&chpw, 0, sizeof(chpw));
260 if (version == KRB5_KPASSWD_VERS_CHANGEPW) {
261 ret = krb5_copy_data(context, in_data, &pwd_data);
262 if (ret) {
263 krb5_warn (context, ret, "krb5_copy_data");
264 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_MALFORMED,
265 "out out memory copying password");
266 return;
268 principal = admin_principal;
269 } else if (version == KRB5_KPASSWD_VERS_SETPW) {
270 size_t len;
272 ret = decode_ChangePasswdDataMS(in_data->data, in_data->length,
273 &chpw, &len);
274 if (ret) {
275 krb5_warn (context, ret, "decode_ChangePasswdDataMS");
276 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_MALFORMED,
277 "malformed ChangePasswdData");
278 return;
282 ret = krb5_copy_data(context, &chpw.newpasswd, &pwd_data);
283 if (ret) {
284 krb5_warn (context, ret, "krb5_copy_data");
285 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_MALFORMED,
286 "out out memory copying password");
287 goto out;
290 if (chpw.targname == NULL && chpw.targrealm != NULL) {
291 krb5_warn (context, ret, "kadm5_init_with_password_ctx");
292 reply_priv (auth_context, s, sa, sa_size,
293 KRB5_KPASSWD_MALFORMED,
294 "targrealm but not targname");
295 goto out;
298 if (chpw.targname) {
299 krb5_principal_data princ;
301 memset(&princ, 0, sizeof (princ));
302 princ.name = *chpw.targname;
303 princ.realm = *chpw.targrealm;
304 if (princ.realm == NULL) {
305 ret = krb5_get_default_realm(context, &princ.realm);
307 if (ret) {
308 krb5_warnx (context,
309 "kadm5_init_with_password_ctx: "
310 "failed to allocate realm");
311 reply_priv (auth_context, s, sa, sa_size,
312 KRB5_KPASSWD_SOFTERROR,
313 "failed to allocate realm");
314 goto out;
317 ret = krb5_copy_principal(context, &princ, &principal);
318 if (*chpw.targrealm == NULL)
319 free(princ.realm);
320 if (ret) {
321 krb5_warn(context, ret, "krb5_copy_principal");
322 reply_priv(auth_context, s, sa, sa_size,
323 KRB5_KPASSWD_HARDERROR,
324 "failed to allocate principal");
325 goto out;
327 } else
328 principal = admin_principal;
329 } else {
330 krb5_warnx (context, "kadm5_init_with_password_ctx: unknown proto");
331 reply_priv (auth_context, s, sa, sa_size,
332 KRB5_KPASSWD_HARDERROR,
333 "Unknown protocol used");
334 return;
337 ret = krb5_unparse_name (context, admin_principal, &admin);
338 if (ret) {
339 krb5_warn (context, ret, "unparse_name failed");
340 reply_priv (auth_context, s, sa, sa_size,
341 KRB5_KPASSWD_HARDERROR, "out of memory error");
342 goto out;
345 conf.realm = principal->realm;
346 conf.mask |= KADM5_CONFIG_REALM;
348 ret = kadm5_init_with_password_ctx(context,
349 admin,
350 NULL,
351 KADM5_ADMIN_SERVICE,
352 &conf, 0, 0,
353 &kadm5_handle);
354 if (ret) {
355 krb5_warn (context, ret, "kadm5_init_with_password_ctx");
356 reply_priv (auth_context, s, sa, sa_size, 2,
357 "Internal error");
358 goto out;
361 ret = krb5_unparse_name(context, principal, &client);
362 if (ret) {
363 krb5_warn (context, ret, "unparse_name failed");
364 reply_priv (auth_context, s, sa, sa_size,
365 KRB5_KPASSWD_HARDERROR, "out of memory error");
366 goto out;
370 * Check password quality if not changing as administrator
373 if (krb5_principal_compare(context, admin_principal, principal) == TRUE) {
375 pwd_reason = kadm5_check_password_quality (context, principal,
376 pwd_data);
377 if (pwd_reason != NULL ) {
378 krb5_warnx (context,
379 "%s didn't pass password quality check with error: %s",
380 client, pwd_reason);
381 reply_priv (auth_context, s, sa, sa_size,
382 KRB5_KPASSWD_SOFTERROR, pwd_reason);
383 goto out;
385 krb5_warnx (context, "Changing password for %s", client);
386 } else {
387 ret = _kadm5_acl_check_permission(kadm5_handle, KADM5_PRIV_CPW,
388 principal);
389 if (ret) {
390 krb5_warn (context, ret,
391 "Check ACL failed for %s for changing %s password",
392 admin, client);
393 reply_priv (auth_context, s, sa, sa_size,
394 KRB5_KPASSWD_HARDERROR, "permission denied");
395 goto out;
397 krb5_warnx (context, "%s is changing password for %s", admin, client);
400 ret = krb5_data_realloc(pwd_data, pwd_data->length + 1);
401 if (ret) {
402 krb5_warn (context, ret, "malloc: out of memory");
403 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_HARDERROR,
404 "Internal error");
405 goto out;
407 tmp = pwd_data->data;
408 tmp[pwd_data->length - 1] = '\0';
410 ret = kadm5_s_chpass_principal_cond (kadm5_handle, principal, 1, tmp);
411 krb5_free_data (context, pwd_data);
412 pwd_data = NULL;
413 if (ret) {
414 const char *str = krb5_get_error_message(context, ret);
415 krb5_warnx(context, "kadm5_s_chpass_principal_cond: %s", str);
416 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_SOFTERROR,
417 str ? str : "Internal error");
418 krb5_free_error_message(context, str);
419 goto out;
421 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_SUCCESS,
422 "Password changed");
423 out:
424 free_ChangePasswdDataMS(&chpw);
425 if (principal != admin_principal)
426 krb5_free_principal(context, principal);
427 if (admin)
428 free(admin);
429 if (client)
430 free(client);
431 if (pwd_data)
432 krb5_free_data(context, pwd_data);
433 if (kadm5_handle)
434 kadm5_destroy (kadm5_handle);
437 static int
438 verify (krb5_auth_context *auth_context,
439 krb5_keytab keytab,
440 krb5_ticket **ticket,
441 krb5_data *out_data,
442 uint16_t *version,
443 int s,
444 struct sockaddr *sa,
445 int sa_size,
446 u_char *msg,
447 size_t len,
448 krb5_address *client_addr)
450 krb5_error_code ret;
451 uint16_t pkt_len, pkt_ver, ap_req_len;
452 krb5_data ap_req_data;
453 krb5_data krb_priv_data;
454 krb5_const_realm client_realm;
455 krb5_principal sprinc;
456 int same;
459 * Only send an error reply if the request passes basic length
460 * verification. Otherwise, kpasswdd would reply to every UDP packet,
461 * allowing an attacker to set up a ping-pong DoS attack via a spoofed UDP
462 * packet with a source address of another UDP service that also replies
463 * to every packet.
465 * Also suppress the error reply if ap_req_len is 0, which indicates
466 * either an invalid request or an error packet. An error packet may be
467 * the result of a ping-pong attacker pointing us at another kpasswdd.
469 pkt_len = (msg[0] << 8) | (msg[1]);
470 pkt_ver = (msg[2] << 8) | (msg[3]);
471 ap_req_len = (msg[4] << 8) | (msg[5]);
472 if (pkt_len != len) {
473 krb5_warnx (context, "Strange len: %ld != %ld",
474 (long)pkt_len, (long)len);
475 return 1;
477 if (ap_req_len == 0) {
478 krb5_warnx (context, "Request is error packet (ap_req_len == 0)");
479 return 1;
481 if (pkt_ver != KRB5_KPASSWD_VERS_CHANGEPW &&
482 pkt_ver != KRB5_KPASSWD_VERS_SETPW) {
483 krb5_warnx (context, "Bad version (%d)", pkt_ver);
484 reply_error (NULL, s, sa, sa_size, 0, 1, "Wrong program version");
485 return 1;
487 *version = pkt_ver;
489 ap_req_data.data = msg + 6;
490 ap_req_data.length = ap_req_len;
492 ret = krb5_rd_req (context,
493 auth_context,
494 &ap_req_data,
495 NULL,
496 keytab,
497 NULL,
498 ticket);
499 if (ret) {
500 krb5_warn (context, ret, "krb5_rd_req");
501 reply_error (NULL, s, sa, sa_size, ret, 3, "Authentication failed");
502 return 1;
505 if (!(*ticket)->ticket.flags.initial) {
506 krb5_warnx(context, "initial flag not set");
507 reply_error((*ticket)->server->realm, s, sa, sa_size, ret, 1,
508 "Bad request");
509 goto out;
513 * The service principal must be kadmin/changepw@CLIENT-REALM, there
514 * is no reason to require the KDC's default realm(s) to be the same
515 * as the realm(s) it serves. The only potential issue is when a KDC
516 * is a master for realm A and a slave for realm B, in which case it
517 * should not accept requests to change passwords for realm B, these
518 * should be sent to realm B's master. This same issue is present in
519 * the checks that only accepted local realms, there is no new risk.
522 client_realm = krb5_principal_get_realm(context, (*ticket)->client);
523 ret = krb5_make_principal(context, &sprinc, client_realm,
524 "kadmin", "changepw", NULL);
525 if (ret)
526 goto out;
527 same = krb5_principal_compare(context, sprinc, (*ticket)->server);
528 krb5_free_principal(context, sprinc);
530 if (!same) {
531 char *sname;
533 krb5_unparse_name(context, (*ticket)->server, &sname);
534 krb5_warnx(context, "Invalid kpasswd service principal %s", sname);
535 free(sname);
536 reply_error(NULL, s, sa, sa_size, ret, 1, "Bad request");
537 goto out;
539 krb_priv_data.data = msg + 6 + ap_req_len;
540 krb_priv_data.length = len - 6 - ap_req_len;
543 * Only enforce client addresses on on tickets with addresses. If
544 * its addressless, we are guessing its behind NAT and really
545 * can't know this information.
548 if ((*ticket)->ticket.caddr && (*ticket)->ticket.caddr->len > 0) {
549 ret = krb5_auth_con_setaddrs (context, *auth_context,
550 NULL, client_addr);
551 if (ret) {
552 krb5_warn (context, ret, "krb5_auth_con_setaddr(this)");
553 goto out;
557 ret = krb5_rd_priv (context,
558 *auth_context,
559 &krb_priv_data,
560 out_data,
561 NULL);
563 if (ret) {
564 krb5_warn (context, ret, "krb5_rd_priv");
565 reply_error ((*ticket)->server->realm, s, sa, sa_size, ret, 3,
566 "Bad request");
567 goto out;
569 return 0;
570 out:
571 krb5_free_ticket (context, *ticket);
572 ticket = NULL;
573 return 1;
576 static void
577 process (krb5_keytab keytab,
578 int s,
579 krb5_address *this_addr,
580 struct sockaddr *sa,
581 int sa_size,
582 u_char *msg,
583 int len)
585 krb5_error_code ret;
586 krb5_auth_context auth_context = NULL;
587 krb5_data out_data;
588 krb5_ticket *ticket;
589 krb5_address other_addr;
590 uint16_t version;
592 memset(&other_addr, 0, sizeof(other_addr));
593 krb5_data_zero (&out_data);
595 ret = krb5_auth_con_init (context, &auth_context);
596 if (ret) {
597 krb5_warn (context, ret, "krb5_auth_con_init");
598 return;
601 krb5_auth_con_setflags (context, auth_context,
602 KRB5_AUTH_CONTEXT_DO_SEQUENCE);
604 ret = krb5_sockaddr2address (context, sa, &other_addr);
605 if (ret) {
606 krb5_warn (context, ret, "krb5_sockaddr2address");
607 goto out;
610 ret = krb5_auth_con_setaddrs (context, auth_context, this_addr, NULL);
611 if (ret) {
612 krb5_warn (context, ret, "krb5_auth_con_setaddr(this)");
613 goto out;
616 if (verify (&auth_context, keytab, &ticket, &out_data,
617 &version, s, sa, sa_size, msg, len, &other_addr) == 0)
620 * We always set the client_addr, to assume that the client
621 * can ignore it if it choose to do so (just the server does
622 * so for addressless tickets).
624 ret = krb5_auth_con_setaddrs (context, auth_context,
625 this_addr, &other_addr);
626 if (ret) {
627 krb5_warn (context, ret, "krb5_auth_con_setaddr(other)");
628 goto out;
631 change (auth_context,
632 ticket->client,
633 version,
635 sa, sa_size,
636 &out_data);
637 memset (out_data.data, 0, out_data.length);
638 krb5_free_ticket (context, ticket);
641 out:
642 krb5_free_address(context, &other_addr);
643 krb5_data_free(&out_data);
644 krb5_auth_con_free(context, auth_context);
647 static const char *check_library = NULL;
648 static const char *check_function = NULL;
649 static getarg_strings policy_libraries = { 0, NULL };
650 static char sHDB[] = "HDBGET:";
651 static char *keytab_str = sHDB;
652 static char *realm_str;
653 static int version_flag;
654 static int help_flag;
655 static int detach_from_console;
656 static int daemon_child = -1;
657 static char *port_str;
658 static char *config_file;
660 struct getargs args[] = {
661 #ifdef HAVE_DLOPEN
662 { "check-library", 0, arg_string, &check_library,
663 "library to load password check function from", "library" },
664 { "check-function", 0, arg_string, &check_function,
665 "password check function to load", "function" },
666 { "policy-libraries", 0, arg_strings, &policy_libraries,
667 "password check function to load", "function" },
668 #endif
669 { "addresses", 0, arg_strings, &addresses_str,
670 "addresses to listen on", "list of addresses" },
671 { "detach", 0, arg_flag, &detach_from_console,
672 "detach from console", NULL },
673 { "daemon-child", 0 , arg_integer, &daemon_child,
674 "private argument, do not use", NULL },
675 { "keytab", 'k', arg_string, &keytab_str,
676 "keytab to get authentication key from", "kspec" },
677 { "config-file", 'c', arg_string, &config_file, NULL, NULL },
678 { "realm", 'r', arg_string, &realm_str, "default realm", "realm" },
679 { "port", 'p', arg_string, &port_str, "port", NULL },
680 { "version", 0, arg_flag, &version_flag, NULL, NULL },
681 { "help", 0, arg_flag, &help_flag, NULL, NULL }
683 int num_args = sizeof(args) / sizeof(args[0]);
685 static int
686 doit(krb5_keytab keytab, int port)
688 krb5_error_code ret;
689 int *sockets;
690 int maxfd;
691 krb5_addresses addrs;
692 unsigned n, i;
693 fd_set real_fdset;
694 struct sockaddr_storage __ss;
695 struct sockaddr *sa = (struct sockaddr *)&__ss;
697 if (explicit_addresses.len) {
698 addrs = explicit_addresses;
699 } else {
700 ret = krb5_get_all_server_addrs(context, &addrs);
701 if (ret)
702 krb5_err(context, 1, ret, "krb5_get_all_server_addrs");
704 n = addrs.len;
706 sockets = malloc(n * sizeof(*sockets));
707 if (sockets == NULL)
708 krb5_errx(context, 1, "out of memory");
709 maxfd = -1;
710 FD_ZERO(&real_fdset);
711 for (i = 0; i < n; ++i) {
712 krb5_socklen_t sa_size = sizeof(__ss);
714 krb5_addr2sockaddr(context, &addrs.val[i], sa, &sa_size, port);
716 sockets[i] = socket(__ss.ss_family, SOCK_DGRAM, 0);
717 if (sockets[i] < 0)
718 krb5_err(context, 1, errno, "socket");
719 if (bind(sockets[i], sa, sa_size) < 0) {
720 char str[128];
721 size_t len;
722 int save_errno = errno;
724 ret = krb5_print_address(&addrs.val[i], str, sizeof(str), &len);
725 if (ret)
726 strlcpy(str, "unknown address", sizeof(str));
727 krb5_warn(context, save_errno, "bind(%s)", str);
728 continue;
730 maxfd = max(maxfd, sockets[i]);
731 if (maxfd >= FD_SETSIZE)
732 krb5_errx(context, 1, "fd too large");
733 FD_SET(sockets[i], &real_fdset);
735 if (maxfd == -1)
736 krb5_errx(context, 1, "No sockets!");
738 roken_detach_finish(NULL, daemon_child);
740 while (exit_flag == 0) {
741 krb5_ssize_t retx;
742 fd_set fdset = real_fdset;
744 retx = select(maxfd + 1, &fdset, NULL, NULL, NULL);
745 if (retx < 0) {
746 if (errno == EINTR)
747 continue;
748 else
749 krb5_err(context, 1, errno, "select");
751 for (i = 0; i < n; ++i)
752 if (FD_ISSET(sockets[i], &fdset)) {
753 u_char buf[BUFSIZ];
754 socklen_t addrlen = sizeof(__ss);
756 retx = recvfrom(sockets[i], buf, sizeof(buf), 0,
757 sa, &addrlen);
758 if (retx < 0) {
759 if (errno == EINTR)
760 break;
761 else
762 krb5_err(context, 1, errno, "recvfrom");
765 process(keytab, sockets[i],
766 &addrs.val[i],
767 sa, addrlen,
768 buf, retx);
772 for (i = 0; i < n; ++i)
773 close(sockets[i]);
774 free(sockets);
776 krb5_free_addresses(context, &addrs);
777 krb5_free_context(context);
778 return 0;
781 static RETSIGTYPE
782 sigterm(int sig)
784 exit_flag = 1;
788 main(int argc, char **argv)
790 krb5_keytab keytab;
791 krb5_error_code ret;
792 char **files;
793 int port, i;
794 int aret;
796 krb5_program_setup(&context, argc, argv, args, num_args, NULL);
798 if (help_flag)
799 krb5_std_usage(0, args, num_args);
801 if (version_flag) {
802 print_version(NULL);
803 exit(0);
806 if (detach_from_console > 0 && daemon_child == -1)
807 roken_detach_prep(argc, argv, "--daemon-child");
809 if (config_file == NULL) {
810 aret = asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
811 if (aret == -1)
812 errx(1, "out of memory");
815 ret = krb5_prepend_config_files_default(config_file, &files);
816 if (ret)
817 krb5_err(context, 1, ret, "getting configuration files");
819 ret = krb5_set_config_files(context, files);
820 krb5_free_config_files(files);
821 if (ret)
822 krb5_err(context, 1, ret, "reading configuration files");
824 if (realm_str)
825 krb5_set_default_realm(context, realm_str);
827 krb5_openlog(context, "kpasswdd", &log_facility);
828 krb5_set_warn_dest(context, log_facility);
830 if (port_str != NULL) {
831 struct servent *s = roken_getservbyname(port_str, "udp");
833 if (s != NULL)
834 port = s->s_port;
835 else {
836 char *ptr;
838 port = strtol(port_str, &ptr, 10);
839 if (port == 0 && ptr == port_str)
840 krb5_errx(context, 1, "bad port `%s'", port_str);
841 port = htons(port);
843 } else
844 port = krb5_getportbyname(context, "kpasswd", "udp", KPASSWD_PORT);
846 ret = krb5_kt_register(context, &hdb_get_kt_ops);
847 if (ret)
848 krb5_err(context, 1, ret, "krb5_kt_register");
850 ret = krb5_kt_resolve(context, keytab_str, &keytab);
851 if (ret)
852 krb5_err(context, 1, ret, "%s", keytab_str);
854 kadm5_setup_passwd_quality_check(context, check_library, check_function);
856 for (i = 0; i < policy_libraries.num_strings; i++) {
857 ret = kadm5_add_passwd_quality_verifier(context,
858 policy_libraries.strings[i]);
859 if (ret)
860 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
862 ret = kadm5_add_passwd_quality_verifier(context, NULL);
863 if (ret)
864 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
867 explicit_addresses.len = 0;
869 if (addresses_str.num_strings) {
870 int j;
872 for (j = 0; j < addresses_str.num_strings; ++j)
873 add_one_address(addresses_str.strings[j], j == 0);
874 free_getarg_strings(&addresses_str);
875 } else {
876 char **foo = krb5_config_get_strings(context, NULL,
877 "kdc", "addresses", NULL);
879 if (foo != NULL) {
880 add_one_address(*foo++, TRUE);
881 while (*foo)
882 add_one_address(*foo++, FALSE);
886 #ifdef HAVE_SIGACTION
888 struct sigaction sa;
890 sa.sa_flags = 0;
891 sa.sa_handler = sigterm;
892 sigemptyset(&sa.sa_mask);
894 sigaction(SIGINT, &sa, NULL);
895 sigaction(SIGTERM, &sa, NULL);
897 #else
898 signal(SIGINT, sigterm);
899 signal(SIGTERM, sigterm);
900 #endif
902 pidfile(NULL);
904 return doit(keytab, port);