Windows 10 SDK build fixes
[heimdal.git] / kpasswd / kpasswdd.c
blob344688e296e06243e60ea135b906278a2f339195
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>
43 #include <kadm5/kadm5_err.h>
45 static krb5_context context;
46 static krb5_log_facility *log_facility;
48 static struct getarg_strings addresses_str;
49 krb5_addresses explicit_addresses;
51 static sig_atomic_t exit_flag = 0;
53 static void
54 add_one_address (const char *str, int first)
56 krb5_error_code ret;
57 krb5_addresses tmp;
59 ret = krb5_parse_address (context, str, &tmp);
60 if (ret)
61 krb5_err (context, 1, ret, "parse_address `%s'", str);
62 if (first)
63 krb5_copy_addresses(context, &tmp, &explicit_addresses);
64 else
65 krb5_append_addresses(context, &explicit_addresses, &tmp);
66 krb5_free_addresses (context, &tmp);
69 static void
70 send_reply (int s,
71 struct sockaddr *sa,
72 int sa_size,
73 krb5_data *ap_rep,
74 krb5_data *rest)
76 struct msghdr msghdr;
77 struct iovec iov[3];
78 uint16_t len, ap_rep_len;
79 u_char header[6];
80 u_char *p;
82 if (ap_rep)
83 ap_rep_len = ap_rep->length;
84 else
85 ap_rep_len = 0;
87 len = 6 + ap_rep_len + rest->length;
88 p = header;
89 *p++ = (len >> 8) & 0xFF;
90 *p++ = (len >> 0) & 0xFF;
91 *p++ = 0;
92 *p++ = 1;
93 *p++ = (ap_rep_len >> 8) & 0xFF;
94 *p++ = (ap_rep_len >> 0) & 0xFF;
96 memset (&msghdr, 0, sizeof(msghdr));
97 msghdr.msg_name = (void *)sa;
98 msghdr.msg_namelen = sa_size;
99 msghdr.msg_iov = iov;
100 msghdr.msg_iovlen = sizeof(iov)/sizeof(*iov);
101 #if 0
102 msghdr.msg_control = NULL;
103 msghdr.msg_controllen = 0;
104 #endif
106 iov[0].iov_base = (char *)header;
107 iov[0].iov_len = 6;
108 if (ap_rep_len) {
109 iov[1].iov_base = ap_rep->data;
110 iov[1].iov_len = ap_rep->length;
111 } else {
112 iov[1].iov_base = NULL;
113 iov[1].iov_len = 0;
115 iov[2].iov_base = rest->data;
116 iov[2].iov_len = rest->length;
118 if (sendmsg (s, &msghdr, 0) < 0)
119 krb5_warn (context, errno, "sendmsg");
122 static int
123 make_result (krb5_data *data,
124 uint16_t result_code,
125 const char *expl)
127 krb5_error_code ret;
128 krb5_storage *sp;
130 sp = krb5_storage_emem();
131 if (sp == NULL) goto out;
132 ret = krb5_store_uint16(sp, result_code);
133 if (ret) goto out;
134 ret = krb5_store_stringz(sp, expl);
135 if (ret) goto out;
136 ret = krb5_storage_to_data(sp, data);
137 if (ret) goto out;
138 krb5_storage_free(sp);
140 return 0;
141 out:
142 if (sp)
143 krb5_storage_free(sp);
145 krb5_warnx (context, "Out of memory generating error reply");
146 return 1;
149 static void
150 reply_error (krb5_realm realm,
151 int s,
152 struct sockaddr *sa,
153 int sa_size,
154 krb5_error_code error_code,
155 uint16_t result_code,
156 const char *expl)
158 krb5_error_code ret;
159 krb5_data error_data;
160 krb5_data e_data;
161 krb5_principal server = NULL;
163 if (make_result(&e_data, result_code, expl))
164 return;
166 if (realm) {
167 ret = krb5_make_principal (context, &server, realm,
168 "kadmin", "changepw", NULL);
169 if (ret) {
170 krb5_data_free (&e_data);
171 return;
175 ret = krb5_mk_error (context,
176 error_code,
177 NULL,
178 &e_data,
179 NULL,
180 server,
181 NULL,
182 NULL,
183 &error_data);
184 if (server)
185 krb5_free_principal(context, server);
186 krb5_data_free (&e_data);
187 if (ret) {
188 krb5_warn (context, ret, "Could not even generate error reply");
189 return;
191 send_reply (s, sa, sa_size, NULL, &error_data);
192 krb5_data_free (&error_data);
195 static void
196 reply_priv (krb5_auth_context auth_context,
197 int s,
198 struct sockaddr *sa,
199 int sa_size,
200 uint16_t result_code,
201 const char *expl)
203 krb5_error_code ret;
204 krb5_data krb_priv_data;
205 krb5_data ap_rep_data;
206 krb5_data e_data;
208 ret = krb5_mk_rep (context,
209 auth_context,
210 &ap_rep_data);
211 if (ret) {
212 krb5_warn (context, ret, "Could not even generate error reply");
213 return;
216 if (make_result(&e_data, result_code, expl))
217 return;
219 ret = krb5_mk_priv (context,
220 auth_context,
221 &e_data,
222 &krb_priv_data,
223 NULL);
224 krb5_data_free (&e_data);
225 if (ret) {
226 krb5_warn (context, ret, "Could not even generate error reply");
227 return;
229 send_reply (s, sa, sa_size, &ap_rep_data, &krb_priv_data);
230 krb5_data_free (&ap_rep_data);
231 krb5_data_free (&krb_priv_data);
235 * Change the password for `principal', sending the reply back on `s'
236 * (`sa', `sa_size') to `pwd_data'.
239 static void
240 change (krb5_auth_context auth_context,
241 krb5_principal admin_principal,
242 uint16_t version,
243 int s,
244 struct sockaddr *sa,
245 int sa_size,
246 krb5_data *in_data)
248 krb5_error_code ret;
249 char *client = NULL, *admin = NULL;
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;
369 if (krb5_principal_compare(context, admin_principal, principal) == FALSE) {
370 ret = _kadm5_acl_check_permission(kadm5_handle, KADM5_PRIV_CPW,
371 principal);
372 if (ret) {
373 krb5_warn (context, ret,
374 "Check ACL failed for %s for changing %s password",
375 admin, client);
376 reply_priv (auth_context, s, sa, sa_size,
377 KRB5_KPASSWD_HARDERROR, "permission denied");
378 goto out;
380 krb5_warnx (context, "%s is changing password for %s", admin, client);
381 } else {
382 krb5_warnx (context, "Changing password for %s", client);
385 ret = krb5_data_realloc(pwd_data, pwd_data->length + 1);
386 if (ret) {
387 krb5_warn (context, ret, "malloc: out of memory");
388 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_HARDERROR,
389 "Internal error");
390 goto out;
392 tmp = pwd_data->data;
393 tmp[pwd_data->length - 1] = '\0';
395 ret = kadm5_s_chpass_principal_cond (kadm5_handle, principal, 1, tmp);
396 krb5_free_data (context, pwd_data);
397 pwd_data = NULL;
398 if (ret) {
399 const char *str = krb5_get_error_message(context, ret);
401 switch (ret) {
402 case KADM5_PASS_Q_TOOSHORT:
403 case KADM5_PASS_Q_CLASS:
404 case KADM5_PASS_Q_DICT:
405 case KADM5_PASS_Q_GENERIC:
406 krb5_warnx(context,
407 "%s didn't pass password quality check with error: %s",
408 client, str);
409 default:
410 krb5_warnx(context, "kadm5_s_chpass_principal_cond: %s", str);
412 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_SOFTERROR,
413 str ? str : "Internal error");
414 krb5_free_error_message(context, str);
415 goto out;
417 reply_priv (auth_context, s, sa, sa_size, KRB5_KPASSWD_SUCCESS,
418 "Password changed");
419 out:
420 free_ChangePasswdDataMS(&chpw);
421 if (principal != admin_principal)
422 krb5_free_principal(context, principal);
423 if (admin)
424 free(admin);
425 if (client)
426 free(client);
427 if (pwd_data)
428 krb5_free_data(context, pwd_data);
429 if (kadm5_handle)
430 kadm5_destroy (kadm5_handle);
433 static int
434 verify (krb5_auth_context *auth_context,
435 krb5_keytab keytab,
436 krb5_ticket **ticket,
437 krb5_data *out_data,
438 uint16_t *version,
439 int s,
440 struct sockaddr *sa,
441 int sa_size,
442 u_char *msg,
443 size_t len,
444 krb5_address *client_addr)
446 krb5_error_code ret;
447 uint16_t pkt_len, pkt_ver, ap_req_len;
448 krb5_data ap_req_data;
449 krb5_data krb_priv_data;
450 krb5_const_realm client_realm;
451 krb5_principal sprinc;
452 int same;
455 * Only send an error reply if the request passes basic length
456 * verification. Otherwise, kpasswdd would reply to every UDP packet,
457 * allowing an attacker to set up a ping-pong DoS attack via a spoofed UDP
458 * packet with a source address of another UDP service that also replies
459 * to every packet.
461 * Also suppress the error reply if ap_req_len is 0, which indicates
462 * either an invalid request or an error packet. An error packet may be
463 * the result of a ping-pong attacker pointing us at another kpasswdd.
465 pkt_len = (msg[0] << 8) | (msg[1]);
466 pkt_ver = (msg[2] << 8) | (msg[3]);
467 ap_req_len = (msg[4] << 8) | (msg[5]);
468 if (pkt_len != len) {
469 krb5_warnx (context, "Strange len: %ld != %ld",
470 (long)pkt_len, (long)len);
471 return 1;
473 if (ap_req_len == 0) {
474 krb5_warnx (context, "Request is error packet (ap_req_len == 0)");
475 return 1;
477 if (pkt_ver != KRB5_KPASSWD_VERS_CHANGEPW &&
478 pkt_ver != KRB5_KPASSWD_VERS_SETPW) {
479 krb5_warnx (context, "Bad version (%d)", pkt_ver);
480 reply_error (NULL, s, sa, sa_size, 0, 1, "Wrong program version");
481 return 1;
483 *version = pkt_ver;
485 ap_req_data.data = msg + 6;
486 ap_req_data.length = ap_req_len;
488 ret = krb5_rd_req (context,
489 auth_context,
490 &ap_req_data,
491 NULL,
492 keytab,
493 NULL,
494 ticket);
495 if (ret) {
496 krb5_warn (context, ret, "krb5_rd_req");
497 reply_error (NULL, s, sa, sa_size, ret, 3, "Authentication failed");
498 return 1;
501 if (!(*ticket)->ticket.flags.initial) {
502 krb5_warnx(context, "initial flag not set");
503 reply_error((*ticket)->server->realm, s, sa, sa_size, ret, 1,
504 "Bad request");
505 goto out;
509 * The service principal must be kadmin/changepw@CLIENT-REALM, there
510 * is no reason to require the KDC's default realm(s) to be the same
511 * as the realm(s) it serves. The only potential issue is when a KDC
512 * is a master for realm A and a slave for realm B, in which case it
513 * should not accept requests to change passwords for realm B, these
514 * should be sent to realm B's master. This same issue is present in
515 * the checks that only accepted local realms, there is no new risk.
518 client_realm = krb5_principal_get_realm(context, (*ticket)->client);
519 ret = krb5_make_principal(context, &sprinc, client_realm,
520 "kadmin", "changepw", NULL);
521 if (ret)
522 goto out;
523 same = krb5_principal_compare(context, sprinc, (*ticket)->server);
524 krb5_free_principal(context, sprinc);
526 if (!same) {
527 char *sname;
529 if (krb5_unparse_name(context, (*ticket)->server, &sname) != 0)
530 sname = NULL;
531 krb5_warnx(context, "Invalid kpasswd service principal %s",
532 sname ? sname : "<enomem>");
533 free(sname);
534 reply_error(NULL, s, sa, sa_size, ret, 1, "Bad request");
535 goto out;
537 krb_priv_data.data = msg + 6 + ap_req_len;
538 krb_priv_data.length = len - 6 - ap_req_len;
541 * Only enforce client addresses on on tickets with addresses. If
542 * its addressless, we are guessing its behind NAT and really
543 * can't know this information.
546 if ((*ticket)->ticket.caddr && (*ticket)->ticket.caddr->len > 0) {
547 ret = krb5_auth_con_setaddrs (context, *auth_context,
548 NULL, client_addr);
549 if (ret) {
550 krb5_warn (context, ret, "krb5_auth_con_setaddr(this)");
551 goto out;
555 ret = krb5_rd_priv (context,
556 *auth_context,
557 &krb_priv_data,
558 out_data,
559 NULL);
561 if (ret) {
562 krb5_warn (context, ret, "krb5_rd_priv");
563 reply_error ((*ticket)->server->realm, s, sa, sa_size, ret, 3,
564 "Bad request");
565 goto out;
567 return 0;
568 out:
569 krb5_free_ticket (context, *ticket);
570 ticket = NULL;
571 return 1;
574 static void
575 process (krb5_keytab keytab,
576 int s,
577 krb5_address *this_addr,
578 struct sockaddr *sa,
579 int sa_size,
580 u_char *msg,
581 int len)
583 krb5_error_code ret;
584 krb5_auth_context auth_context = NULL;
585 krb5_data out_data;
586 krb5_ticket *ticket;
587 krb5_address other_addr;
588 uint16_t version;
590 memset(&other_addr, 0, sizeof(other_addr));
591 krb5_data_zero (&out_data);
593 ret = krb5_auth_con_init (context, &auth_context);
594 if (ret) {
595 krb5_warn (context, ret, "krb5_auth_con_init");
596 return;
599 krb5_auth_con_setflags (context, auth_context,
600 KRB5_AUTH_CONTEXT_DO_SEQUENCE);
602 ret = krb5_sockaddr2address (context, sa, &other_addr);
603 if (ret) {
604 krb5_warn (context, ret, "krb5_sockaddr2address");
605 goto out;
608 ret = krb5_auth_con_setaddrs (context, auth_context, this_addr, NULL);
609 if (ret) {
610 krb5_warn (context, ret, "krb5_auth_con_setaddr(this)");
611 goto out;
614 if (verify (&auth_context, keytab, &ticket, &out_data,
615 &version, s, sa, sa_size, msg, len, &other_addr) == 0)
618 * We always set the client_addr, to assume that the client
619 * can ignore it if it choose to do so (just the server does
620 * so for addressless tickets).
622 ret = krb5_auth_con_setaddrs (context, auth_context,
623 this_addr, &other_addr);
624 if (ret) {
625 krb5_warn (context, ret, "krb5_auth_con_setaddr(other)");
626 goto out;
629 change (auth_context,
630 ticket->client,
631 version,
633 sa, sa_size,
634 &out_data);
635 memset (out_data.data, 0, out_data.length);
636 krb5_free_ticket (context, ticket);
639 out:
640 krb5_free_address(context, &other_addr);
641 krb5_data_free(&out_data);
642 krb5_auth_con_free(context, auth_context);
645 static const char *check_library = NULL;
646 static const char *check_function = NULL;
647 static getarg_strings policy_libraries = { 0, NULL };
648 static char sHDB[] = "HDBGET:";
649 static char *keytab_str = sHDB;
650 static char *realm_str;
651 static int version_flag;
652 static int help_flag;
653 static int detach_from_console;
654 static int daemon_child = -1;
655 static char *port_str;
656 static char *config_file;
658 struct getargs args[] = {
659 #ifdef HAVE_DLOPEN
660 { "check-library", 0, arg_string, &check_library,
661 "library to load password check function from", "library" },
662 { "check-function", 0, arg_string, &check_function,
663 "password check function to load", "function" },
664 { "policy-libraries", 0, arg_strings, &policy_libraries,
665 "password check function to load", "function" },
666 #endif
667 { "addresses", 0, arg_strings, &addresses_str,
668 "addresses to listen on", "list of addresses" },
669 { "detach", 0, arg_flag, &detach_from_console,
670 "detach from console", NULL },
671 { "daemon-child", 0 , arg_integer, &daemon_child,
672 "private argument, do not use", NULL },
673 { "keytab", 'k', arg_string, &keytab_str,
674 "keytab to get authentication key from", "kspec" },
675 { "config-file", 'c', arg_string, &config_file, NULL, NULL },
676 { "realm", 'r', arg_string, &realm_str, "default realm", "realm" },
677 { "port", 'p', arg_string, &port_str, "port", NULL },
678 { "version", 0, arg_flag, &version_flag, NULL, NULL },
679 { "help", 0, arg_flag, &help_flag, NULL, NULL }
681 int num_args = sizeof(args) / sizeof(args[0]);
683 static int
684 doit(krb5_keytab keytab, int port)
686 krb5_error_code ret;
687 int *sockets;
688 int maxfd;
689 krb5_addresses addrs;
690 unsigned n, i;
691 fd_set real_fdset;
692 struct sockaddr_storage __ss;
693 struct sockaddr *sa = (struct sockaddr *)&__ss;
695 if (explicit_addresses.len) {
696 addrs = explicit_addresses;
697 } else {
698 ret = krb5_get_all_server_addrs(context, &addrs);
699 if (ret)
700 krb5_err(context, 1, ret, "krb5_get_all_server_addrs");
702 n = addrs.len;
704 sockets = malloc(n * sizeof(*sockets));
705 if (sockets == NULL)
706 krb5_errx(context, 1, "out of memory");
707 maxfd = -1;
708 FD_ZERO(&real_fdset);
709 for (i = 0; i < n; ++i) {
710 krb5_socklen_t sa_size = sizeof(__ss);
712 krb5_addr2sockaddr(context, &addrs.val[i], sa, &sa_size, port);
714 sockets[i] = socket(__ss.ss_family, SOCK_DGRAM, 0);
715 if (sockets[i] < 0)
716 krb5_err(context, 1, errno, "socket");
717 if (bind(sockets[i], sa, sa_size) < 0) {
718 char str[128];
719 size_t len;
720 int save_errno = errno;
722 ret = krb5_print_address(&addrs.val[i], str, sizeof(str), &len);
723 if (ret)
724 strlcpy(str, "unknown address", sizeof(str));
725 krb5_warn(context, save_errno, "bind(%s)", str);
726 continue;
728 maxfd = max(maxfd, sockets[i]);
729 if (maxfd >= FD_SETSIZE)
730 krb5_errx(context, 1, "fd too large");
731 FD_SET(sockets[i], &real_fdset);
733 if (maxfd == -1)
734 krb5_errx(context, 1, "No sockets!");
736 roken_detach_finish(NULL, daemon_child);
738 while (exit_flag == 0) {
739 krb5_ssize_t retx;
740 fd_set fdset = real_fdset;
742 retx = select(maxfd + 1, &fdset, NULL, NULL, NULL);
743 if (retx < 0) {
744 if (errno == EINTR)
745 continue;
746 else
747 krb5_err(context, 1, errno, "select");
749 for (i = 0; i < n; ++i)
750 if (FD_ISSET(sockets[i], &fdset)) {
751 u_char buf[BUFSIZ];
752 socklen_t addrlen = sizeof(__ss);
754 retx = recvfrom(sockets[i], buf, sizeof(buf), 0,
755 sa, &addrlen);
756 if (retx < 0) {
757 if (errno == EINTR)
758 break;
759 else
760 krb5_err(context, 1, errno, "recvfrom");
763 process(keytab, sockets[i],
764 &addrs.val[i],
765 sa, addrlen,
766 buf, retx);
770 for (i = 0; i < n; ++i)
771 close(sockets[i]);
772 free(sockets);
774 krb5_free_addresses(context, &addrs);
775 krb5_free_context(context);
776 return 0;
779 static RETSIGTYPE
780 sigterm(int sig)
782 exit_flag = 1;
786 main(int argc, char **argv)
788 krb5_keytab keytab;
789 krb5_error_code ret;
790 char **files;
791 int port, i;
792 int aret;
794 krb5_program_setup(&context, argc, argv, args, num_args, NULL);
796 if (help_flag)
797 krb5_std_usage(0, args, num_args);
799 if (version_flag) {
800 print_version(NULL);
801 exit(0);
804 if (detach_from_console > 0 && daemon_child == -1)
805 daemon_child = roken_detach_prep(argc, argv, "--daemon-child");
807 if (config_file == NULL) {
808 aret = asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
809 if (aret == -1)
810 errx(1, "out of memory");
813 ret = krb5_prepend_config_files_default(config_file, &files);
814 if (ret)
815 krb5_err(context, 1, ret, "getting configuration files");
817 ret = krb5_set_config_files(context, files);
818 krb5_free_config_files(files);
819 if (ret)
820 krb5_err(context, 1, ret, "reading configuration files");
822 if (realm_str)
823 krb5_set_default_realm(context, realm_str);
825 krb5_openlog(context, "kpasswdd", &log_facility);
826 krb5_set_warn_dest(context, log_facility);
828 if (port_str != NULL) {
829 struct servent *s = roken_getservbyname(port_str, "udp");
831 if (s != NULL)
832 port = s->s_port;
833 else {
834 char *ptr;
836 port = strtol(port_str, &ptr, 10);
837 if (port == 0 && ptr == port_str)
838 krb5_errx(context, 1, "bad port `%s'", port_str);
839 port = htons(port);
841 } else
842 port = krb5_getportbyname(context, "kpasswd", "udp", KPASSWD_PORT);
844 ret = krb5_kt_register(context, &hdb_get_kt_ops);
845 if (ret)
846 krb5_err(context, 1, ret, "krb5_kt_register");
848 ret = krb5_kt_resolve(context, keytab_str, &keytab);
849 if (ret)
850 krb5_err(context, 1, ret, "%s", keytab_str);
852 kadm5_setup_passwd_quality_check(context, check_library, check_function);
854 for (i = 0; i < policy_libraries.num_strings; i++) {
855 ret = kadm5_add_passwd_quality_verifier(context,
856 policy_libraries.strings[i]);
857 if (ret)
858 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
860 ret = kadm5_add_passwd_quality_verifier(context, NULL);
861 if (ret)
862 krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier");
865 explicit_addresses.len = 0;
867 if (addresses_str.num_strings) {
868 int j;
870 for (j = 0; j < addresses_str.num_strings; ++j)
871 add_one_address(addresses_str.strings[j], j == 0);
872 free_getarg_strings(&addresses_str);
873 } else {
874 char **foo = krb5_config_get_strings(context, NULL,
875 "kdc", "addresses", NULL);
877 if (foo != NULL) {
878 add_one_address(*foo++, TRUE);
879 while (*foo)
880 add_one_address(*foo++, FALSE);
884 #ifdef HAVE_SIGACTION
886 struct sigaction sa;
888 sa.sa_flags = 0;
889 sa.sa_handler = sigterm;
890 sigemptyset(&sa.sa_mask);
892 sigaction(SIGINT, &sa, NULL);
893 sigaction(SIGTERM, &sa, NULL);
895 #else
896 signal(SIGINT, sigterm);
897 signal(SIGTERM, sigterm);
898 #endif
900 rk_pidfile(NULL);
902 return doit(keytab, port);