Remove print that fools kpasswdd.
[heimdal.git] / lib / kadm5 / ipropd_master.c
blob5915c7915852f9383e9c6a0e51262f416f7a0bc5
1 /*
2 * Copyright (c) 1997 - 2008 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 "iprop.h"
35 #include <rtbl.h>
37 static krb5_log_facility *log_facility;
39 const char *slave_stats_file;
40 const char *slave_time_missing = "2 min";
41 const char *slave_time_gone = "5 min";
43 static int time_before_missing;
44 static int time_before_gone;
46 const char *master_hostname;
48 static krb5_socket_t
49 make_signal_socket (krb5_context context)
51 #ifndef NO_UNIX_SOCKETS
52 struct sockaddr_un addr;
53 const char *fn;
54 krb5_socket_t fd;
56 fn = kadm5_log_signal_socket(context);
58 fd = socket (AF_UNIX, SOCK_DGRAM, 0);
59 if (fd < 0)
60 krb5_err (context, 1, errno, "socket AF_UNIX");
61 memset (&addr, 0, sizeof(addr));
62 addr.sun_family = AF_UNIX;
63 strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
64 unlink (addr.sun_path);
65 if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
66 krb5_err (context, 1, errno, "bind %s", addr.sun_path);
67 return fd;
68 #else
69 struct addrinfo *ai = NULL;
70 krb5_socket_t fd;
72 kadm5_log_signal_socket_info(context, 1, &ai);
74 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
75 if (rk_IS_BAD_SOCKET(fd))
76 krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF=%d", ai->ai_family);
78 if (rk_IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) ))
79 krb5_err (context, 1, rk_SOCK_ERRNO, "bind");
80 return fd;
81 #endif
84 static krb5_socket_t
85 make_listen_socket (krb5_context context, const char *port_str)
87 krb5_socket_t fd;
88 int one = 1;
89 struct sockaddr_in addr;
91 fd = socket (AF_INET, SOCK_STREAM, 0);
92 if (rk_IS_BAD_SOCKET(fd))
93 krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF_INET");
94 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
95 memset (&addr, 0, sizeof(addr));
96 addr.sin_family = AF_INET;
98 if (port_str) {
99 addr.sin_port = krb5_getportbyname (context,
100 port_str, "tcp",
102 if (addr.sin_port == 0) {
103 char *ptr;
104 long port;
106 port = strtol (port_str, &ptr, 10);
107 if (port == 0 && ptr == port_str)
108 krb5_errx (context, 1, "bad port `%s'", port_str);
109 addr.sin_port = htons(port);
111 } else {
112 addr.sin_port = krb5_getportbyname (context, IPROP_SERVICE,
113 "tcp", IPROP_PORT);
115 if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
116 krb5_err (context, 1, errno, "bind");
117 if (listen(fd, SOMAXCONN) < 0)
118 krb5_err (context, 1, errno, "listen");
119 return fd;
122 struct slave {
123 krb5_socket_t fd;
124 struct sockaddr_in addr;
125 char *name;
126 krb5_auth_context ac;
127 uint32_t version;
128 time_t seen;
129 unsigned long flags;
130 #define SLAVE_F_DEAD 0x1
131 #define SLAVE_F_AYT 0x2
132 struct slave *next;
135 typedef struct slave slave;
137 static int
138 check_acl (krb5_context context, const char *name)
140 const char *fn;
141 FILE *fp;
142 char buf[256];
143 int ret = 1;
144 char *slavefile;
146 asprintf(&slavefile, "%s/slaves", hdb_db_dir(context));
148 fn = krb5_config_get_string_default(context,
149 NULL,
150 slavefile,
151 "kdc",
152 "iprop-acl",
153 NULL);
155 fp = fopen (fn, "r");
156 free(slavefile);
157 if (fp == NULL)
158 return 1;
159 while (fgets(buf, sizeof(buf), fp) != NULL) {
160 buf[strcspn(buf, "\r\n")] = '\0';
161 if (strcmp (buf, name) == 0) {
162 ret = 0;
163 break;
166 fclose (fp);
167 return ret;
170 static void
171 slave_seen(slave *s)
173 s->flags &= ~SLAVE_F_AYT;
174 s->seen = time(NULL);
177 static int
178 slave_missing_p (slave *s)
180 if (time(NULL) > s->seen + time_before_missing)
181 return 1;
182 return 0;
185 static int
186 slave_gone_p (slave *s)
188 if (time(NULL) > s->seen + time_before_gone)
189 return 1;
190 return 0;
193 static void
194 slave_dead(krb5_context context, slave *s)
196 krb5_warnx(context, "slave %s dead", s->name);
198 if (!rk_IS_BAD_SOCKET(s->fd)) {
199 rk_closesocket (s->fd);
200 s->fd = rk_INVALID_SOCKET;
202 s->flags |= SLAVE_F_DEAD;
203 slave_seen(s);
206 static void
207 remove_slave (krb5_context context, slave *s, slave **root)
209 slave **p;
211 if (!rk_IS_BAD_SOCKET(s->fd))
212 rk_closesocket (s->fd);
213 if (s->name)
214 free (s->name);
215 if (s->ac)
216 krb5_auth_con_free (context, s->ac);
218 for (p = root; *p; p = &(*p)->next)
219 if (*p == s) {
220 *p = s->next;
221 break;
223 free (s);
226 static void
227 add_slave (krb5_context context, krb5_keytab keytab, slave **root,
228 krb5_socket_t fd)
230 krb5_principal server;
231 krb5_error_code ret;
232 slave *s;
233 socklen_t addr_len;
234 krb5_ticket *ticket = NULL;
235 char hostname[128];
237 s = malloc(sizeof(*s));
238 if (s == NULL) {
239 krb5_warnx (context, "add_slave: no memory");
240 return;
242 s->name = NULL;
243 s->ac = NULL;
245 addr_len = sizeof(s->addr);
246 s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
247 if (rk_IS_BAD_SOCKET(s->fd)) {
248 krb5_warn (context, rk_SOCK_ERRNO, "accept");
249 goto error;
251 if (master_hostname)
252 strlcpy(hostname, master_hostname, sizeof(hostname));
253 else
254 gethostname(hostname, sizeof(hostname));
256 ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
257 KRB5_NT_SRV_HST, &server);
258 if (ret) {
259 krb5_warn (context, ret, "krb5_sname_to_principal");
260 goto error;
263 ret = krb5_recvauth (context, &s->ac, &s->fd,
264 IPROP_VERSION, server, 0, keytab, &ticket);
265 krb5_free_principal (context, server);
266 if (ret) {
267 krb5_warn (context, ret, "krb5_recvauth");
268 goto error;
270 ret = krb5_unparse_name (context, ticket->client, &s->name);
271 krb5_free_ticket (context, ticket);
272 if (ret) {
273 krb5_warn (context, ret, "krb5_unparse_name");
274 goto error;
276 if (check_acl (context, s->name)) {
277 krb5_warnx (context, "%s not in acl", s->name);
278 goto error;
282 slave *l = *root;
284 while (l) {
285 if (strcmp(l->name, s->name) == 0)
286 break;
287 l = l->next;
289 if (l) {
290 if (l->flags & SLAVE_F_DEAD) {
291 remove_slave(context, l, root);
292 } else {
293 krb5_warnx (context, "second connection from %s", s->name);
294 goto error;
299 krb5_warnx (context, "connection from %s", s->name);
301 s->version = 0;
302 s->flags = 0;
303 slave_seen(s);
304 s->next = *root;
305 *root = s;
306 return;
307 error:
308 remove_slave(context, s, root);
311 struct prop_context {
312 krb5_auth_context auth_context;
313 krb5_socket_t fd;
316 static int
317 prop_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
319 krb5_error_code ret;
320 krb5_storage *sp;
321 krb5_data data;
322 struct slave *s = (struct slave *)v;
324 ret = hdb_entry2value (context, &entry->entry, &data);
325 if (ret)
326 return ret;
327 ret = krb5_data_realloc (&data, data.length + 4);
328 if (ret) {
329 krb5_data_free (&data);
330 return ret;
332 memmove ((char *)data.data + 4, data.data, data.length - 4);
333 sp = krb5_storage_from_data(&data);
334 if (sp == NULL) {
335 krb5_data_free (&data);
336 return ENOMEM;
338 krb5_store_int32(sp, ONE_PRINC);
339 krb5_storage_free(sp);
341 ret = krb5_write_priv_message (context, s->ac, &s->fd, &data);
342 krb5_data_free (&data);
343 return ret;
346 static int
347 send_complete (krb5_context context, slave *s,
348 const char *database, uint32_t current_version)
350 krb5_error_code ret;
351 krb5_storage *sp;
352 HDB *db;
353 krb5_data data;
354 char buf[8];
356 ret = hdb_create (context, &db, database);
357 if (ret)
358 krb5_err (context, 1, ret, "hdb_create: %s", database);
359 ret = db->hdb_open (context, db, O_RDONLY, 0);
360 if (ret)
361 krb5_err (context, 1, ret, "db->open");
363 sp = krb5_storage_from_mem (buf, 4);
364 if (sp == NULL)
365 krb5_errx (context, 1, "krb5_storage_from_mem");
366 krb5_store_int32 (sp, TELL_YOU_EVERYTHING);
367 krb5_storage_free (sp);
369 data.data = buf;
370 data.length = 4;
372 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
374 if (ret) {
375 krb5_warn (context, ret, "krb5_write_priv_message");
376 slave_dead(context, s);
377 return ret;
380 ret = hdb_foreach (context, db, HDB_F_ADMIN_DATA, prop_one, s);
381 if (ret) {
382 krb5_warn (context, ret, "hdb_foreach");
383 slave_dead(context, s);
384 return ret;
387 (*db->hdb_close)(context, db);
388 (*db->hdb_destroy)(context, db);
390 sp = krb5_storage_from_mem (buf, 8);
391 if (sp == NULL)
392 krb5_errx (context, 1, "krb5_storage_from_mem");
393 krb5_store_int32 (sp, NOW_YOU_HAVE);
394 krb5_store_int32 (sp, current_version);
395 krb5_storage_free (sp);
397 data.length = 8;
399 s->version = current_version;
401 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
402 if (ret) {
403 slave_dead(context, s);
404 krb5_warn (context, ret, "krb5_write_priv_message");
405 return ret;
408 slave_seen(s);
410 return 0;
413 static int
414 send_are_you_there (krb5_context context, slave *s)
416 krb5_storage *sp;
417 krb5_data data;
418 char buf[4];
419 int ret;
421 if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
422 return 0;
424 krb5_warnx(context, "slave %s missing, sending AYT", s->name);
426 s->flags |= SLAVE_F_AYT;
428 data.data = buf;
429 data.length = 4;
431 sp = krb5_storage_from_mem (buf, 4);
432 if (sp == NULL) {
433 krb5_warnx (context, "are_you_there: krb5_data_alloc");
434 slave_dead(context, s);
435 return 1;
437 krb5_store_int32 (sp, ARE_YOU_THERE);
438 krb5_storage_free (sp);
440 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
442 if (ret) {
443 krb5_warn (context, ret, "are_you_there: krb5_write_priv_message");
444 slave_dead(context, s);
445 return 1;
448 return 0;
451 static int
452 send_diffs (krb5_context context, slave *s, int log_fd,
453 const char *database, uint32_t current_version)
455 krb5_storage *sp;
456 uint32_t ver;
457 time_t timestamp;
458 enum kadm_ops op;
459 uint32_t len;
460 off_t right, left;
461 krb5_data data;
462 int ret = 0;
464 if (s->version == current_version) {
465 krb5_warnx(context, "slave %s in sync already at version %ld",
466 s->name, (long)s->version);
467 return 0;
470 if (s->flags & SLAVE_F_DEAD)
471 return 0;
473 /* if slave is a fresh client, starting over */
474 if (s->version == 0) {
475 krb5_warnx(context, "sending complete log to fresh slave %s",
476 s->name);
477 return send_complete (context, s, database, current_version);
480 sp = kadm5_log_goto_end (log_fd);
481 right = krb5_storage_seek(sp, 0, SEEK_CUR);
482 for (;;) {
483 ret = kadm5_log_previous (context, sp, &ver, &timestamp, &op, &len);
484 if (ret)
485 krb5_err(context, 1, ret,
486 "send_diffs: failed to find previous entry");
487 left = krb5_storage_seek(sp, -16, SEEK_CUR);
488 if (ver == s->version)
489 return 0;
490 if (ver == s->version + 1)
491 break;
492 if (left == 0) {
493 krb5_storage_free(sp);
494 krb5_warnx(context,
495 "slave %s (version %lu) out of sync with master "
496 "(first version in log %lu), sending complete database",
497 s->name, (unsigned long)s->version, (unsigned long)ver);
498 return send_complete (context, s, database, current_version);
502 krb5_warnx(context,
503 "syncing slave %s from version %lu to version %lu",
504 s->name, (unsigned long)s->version,
505 (unsigned long)current_version);
507 ret = krb5_data_alloc (&data, right - left + 4);
508 if (ret) {
509 krb5_storage_free(sp);
510 krb5_warn (context, ret, "send_diffs: krb5_data_alloc");
511 slave_dead(context, s);
512 return 1;
514 krb5_storage_read (sp, (char *)data.data + 4, data.length - 4);
515 krb5_storage_free(sp);
517 sp = krb5_storage_from_data (&data);
518 if (sp == NULL) {
519 krb5_warnx (context, "send_diffs: krb5_storage_from_data");
520 slave_dead(context, s);
521 return 1;
523 krb5_store_int32 (sp, FOR_YOU);
524 krb5_storage_free(sp);
526 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
527 krb5_data_free(&data);
529 if (ret) {
530 krb5_warn (context, ret, "send_diffs: krb5_write_priv_message");
531 slave_dead(context, s);
532 return 1;
534 slave_seen(s);
536 s->version = current_version;
538 return 0;
541 static int
542 process_msg (krb5_context context, slave *s, int log_fd,
543 const char *database, uint32_t current_version)
545 int ret = 0;
546 krb5_data out;
547 krb5_storage *sp;
548 int32_t tmp;
550 ret = krb5_read_priv_message(context, s->ac, &s->fd, &out);
551 if(ret) {
552 krb5_warn (context, ret, "error reading message from %s", s->name);
553 return 1;
556 sp = krb5_storage_from_mem (out.data, out.length);
557 if (sp == NULL) {
558 krb5_warnx (context, "process_msg: no memory");
559 krb5_data_free (&out);
560 return 1;
562 if (krb5_ret_int32 (sp, &tmp) != 0) {
563 krb5_warnx (context, "process_msg: client send too short command");
564 krb5_data_free (&out);
565 return 1;
567 switch (tmp) {
568 case I_HAVE :
569 ret = krb5_ret_int32 (sp, &tmp);
570 if (ret != 0) {
571 krb5_warnx (context, "process_msg: client send too I_HAVE data");
572 break;
574 /* new started slave that have old log */
575 if (s->version == 0 && tmp != 0) {
576 if (current_version < tmp) {
577 krb5_warnx (context, "Slave %s have later version the master "
578 "OUT OF SYNC", s->name);
580 s->version = tmp;
582 if (tmp < s->version) {
583 krb5_warnx (context, "Slave claims to not have "
584 "version we already sent to it");
585 } else {
586 ret = send_diffs (context, s, log_fd, database, current_version);
588 break;
589 case I_AM_HERE :
590 break;
591 case ARE_YOU_THERE:
592 case FOR_YOU :
593 default :
594 krb5_warnx (context, "Ignoring command %d", tmp);
595 break;
598 krb5_data_free (&out);
599 krb5_storage_free (sp);
601 slave_seen(s);
603 return ret;
606 #define SLAVE_NAME "Name"
607 #define SLAVE_ADDRESS "Address"
608 #define SLAVE_VERSION "Version"
609 #define SLAVE_STATUS "Status"
610 #define SLAVE_SEEN "Last Seen"
612 static FILE *
613 open_stats(krb5_context context)
615 char *statfile = NULL;
616 const char *fn;
617 FILE *f;
619 if (slave_stats_file)
620 fn = slave_stats_file;
621 else {
622 asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context));
623 fn = krb5_config_get_string_default(context,
624 NULL,
625 statfile,
626 "kdc",
627 "iprop-stats",
628 NULL);
630 f = fopen(fn, "w");
631 if (statfile)
632 free(statfile);
634 return f;
637 static void
638 write_master_down(krb5_context context)
640 char str[100];
641 time_t t = time(NULL);
642 FILE *fp;
644 fp = open_stats(context);
645 if (fp == NULL)
646 return;
647 krb5_format_time(context, t, str, sizeof(str), TRUE);
648 fprintf(fp, "master down at %s\n", str);
650 fclose(fp);
653 static void
654 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
656 char str[100];
657 rtbl_t tbl;
658 time_t t = time(NULL);
659 FILE *fp;
661 fp = open_stats(context);
662 if (fp == NULL)
663 return;
665 krb5_format_time(context, t, str, sizeof(str), TRUE);
666 fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
668 fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
670 tbl = rtbl_create();
671 if (tbl == NULL) {
672 fclose(fp);
673 return;
676 rtbl_add_column(tbl, SLAVE_NAME, 0);
677 rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
678 rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
679 rtbl_add_column(tbl, SLAVE_STATUS, 0);
680 rtbl_add_column(tbl, SLAVE_SEEN, 0);
682 rtbl_set_prefix(tbl, " ");
683 rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
685 while (slaves) {
686 krb5_address addr;
687 krb5_error_code ret;
688 rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
689 ret = krb5_sockaddr2address (context,
690 (struct sockaddr*)&slaves->addr, &addr);
691 if(ret == 0) {
692 krb5_print_address(&addr, str, sizeof(str), NULL);
693 krb5_free_address(context, &addr);
694 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
695 } else
696 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
698 snprintf(str, sizeof(str), "%u", (unsigned)slaves->version);
699 rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
701 if (slaves->flags & SLAVE_F_DEAD)
702 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
703 else
704 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
706 ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
707 rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
709 slaves = slaves->next;
712 rtbl_format(tbl, fp);
713 rtbl_destroy(tbl);
715 fclose(fp);
719 static char *realm;
720 static int version_flag;
721 static int help_flag;
722 static char *keytab_str = "HDB:";
723 static char *database;
724 static char *config_file;
725 static char *port_str;
726 #ifdef SUPPORT_DETACH
727 static int detach_from_console = 0;
728 #endif
730 static struct getargs args[] = {
731 { "config-file", 'c', arg_string, &config_file },
732 { "realm", 'r', arg_string, &realm },
733 { "keytab", 'k', arg_string, &keytab_str,
734 "keytab to get authentication from", "kspec" },
735 { "database", 'd', arg_string, &database, "database", "file"},
736 { "slave-stats-file", 0, arg_string, &slave_stats_file,
737 "file for slave status information", "file"},
738 { "time-missing", 0, arg_string, &slave_time_missing,
739 "time before slave is polled for presence", "time"},
740 { "time-gone", 0, arg_string, &slave_time_gone,
741 "time of inactivity after which a slave is considered gone", "time"},
742 { "port", 0, arg_string, &port_str,
743 "port ipropd will listen to", "port"},
744 #ifdef SUPPORT_DETACH
745 { "detach", 0, arg_flag, &detach_from_console,
746 "detach from console" },
747 #endif
748 { "hostname", 0, arg_string, &master_hostname,
749 "hostname of master (if not same as hostname)", "hostname" },
750 { "version", 0, arg_flag, &version_flag },
751 { "help", 0, arg_flag, &help_flag }
753 static int num_args = sizeof(args) / sizeof(args[0]);
756 main(int argc, char **argv)
758 krb5_error_code ret;
759 krb5_context context;
760 void *kadm_handle;
761 kadm5_server_context *server_context;
762 kadm5_config_params conf;
763 krb5_socket_t signal_fd, listen_fd;
764 int log_fd;
765 slave *slaves = NULL;
766 uint32_t current_version = 0, old_version = 0;
767 krb5_keytab keytab;
768 int optidx;
769 char **files;
771 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
773 if(help_flag)
774 krb5_std_usage(0, args, num_args);
775 if(version_flag) {
776 print_version(NULL);
777 exit(0);
780 setup_signal();
782 if (config_file == NULL) {
783 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
784 if (config_file == NULL)
785 errx(1, "out of memory");
788 ret = krb5_prepend_config_files_default(config_file, &files);
789 if (ret)
790 krb5_err(context, 1, ret, "getting configuration files");
792 ret = krb5_set_config_files(context, files);
793 krb5_free_config_files(files);
794 if (ret)
795 krb5_err(context, 1, ret, "reading configuration files");
797 time_before_gone = parse_time (slave_time_gone, "s");
798 if (time_before_gone < 0)
799 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
800 time_before_missing = parse_time (slave_time_missing, "s");
801 if (time_before_missing < 0)
802 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
804 #ifdef SUPPORT_DETACH
805 if (detach_from_console)
806 daemon(0, 0);
807 #endif
808 pidfile (NULL);
809 krb5_openlog (context, "ipropd-master", &log_facility);
810 krb5_set_warn_dest(context, log_facility);
812 ret = krb5_kt_register(context, &hdb_kt_ops);
813 if(ret)
814 krb5_err(context, 1, ret, "krb5_kt_register");
816 ret = krb5_kt_resolve(context, keytab_str, &keytab);
817 if(ret)
818 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
820 memset(&conf, 0, sizeof(conf));
821 if(realm) {
822 conf.mask |= KADM5_CONFIG_REALM;
823 conf.realm = realm;
825 ret = kadm5_init_with_skey_ctx (context,
826 KADM5_ADMIN_SERVICE,
827 NULL,
828 KADM5_ADMIN_SERVICE,
829 &conf, 0, 0,
830 &kadm_handle);
831 if (ret)
832 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
834 server_context = (kadm5_server_context *)kadm_handle;
836 log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
837 if (log_fd < 0)
838 krb5_err (context, 1, errno, "open %s",
839 server_context->log_context.log_file);
841 signal_fd = make_signal_socket (context);
842 listen_fd = make_listen_socket (context, port_str);
844 kadm5_log_get_version_fd (log_fd, &current_version);
846 krb5_warnx(context, "ipropd-master started at version: %lu",
847 (unsigned long)current_version);
849 while(exit_flag == 0){
850 slave *p;
851 fd_set readset;
852 int max_fd = 0;
853 struct timeval to = {30, 0};
854 uint32_t vers;
856 #ifndef NO_LIMIT_FD_SETSIZE
857 if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE)
858 krb5_errx (context, 1, "fd too large");
859 #endif
861 FD_ZERO(&readset);
862 FD_SET(signal_fd, &readset);
863 max_fd = max(max_fd, signal_fd);
864 FD_SET(listen_fd, &readset);
865 max_fd = max(max_fd, listen_fd);
867 for (p = slaves; p != NULL; p = p->next) {
868 if (p->flags & SLAVE_F_DEAD)
869 continue;
870 FD_SET(p->fd, &readset);
871 max_fd = max(max_fd, p->fd);
874 ret = select (max_fd + 1,
875 &readset, NULL, NULL, &to);
876 if (ret < 0) {
877 if (errno == EINTR)
878 continue;
879 else
880 krb5_err (context, 1, errno, "select");
883 if (ret == 0) {
884 old_version = current_version;
885 kadm5_log_get_version_fd (log_fd, &current_version);
887 if (current_version > old_version) {
888 krb5_warnx(context,
889 "Missed a signal, updating slaves %lu to %lu",
890 (unsigned long)old_version,
891 (unsigned long)current_version);
892 for (p = slaves; p != NULL; p = p->next) {
893 if (p->flags & SLAVE_F_DEAD)
894 continue;
895 send_diffs (context, p, log_fd, database, current_version);
900 if (ret && FD_ISSET(signal_fd, &readset)) {
901 #ifndef NO_UNIX_SOCKETS
902 struct sockaddr_un peer_addr;
903 #else
904 struct sockaddr_storage peer_addr;
905 #endif
906 socklen_t peer_len = sizeof(peer_addr);
908 if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
909 (struct sockaddr *)&peer_addr, &peer_len) < 0) {
910 krb5_warn (context, errno, "recvfrom");
911 continue;
913 --ret;
914 assert(ret >= 0);
915 old_version = current_version;
916 kadm5_log_get_version_fd (log_fd, &current_version);
917 if (current_version > old_version) {
918 krb5_warnx(context,
919 "Got a signal, updating slaves %lu to %lu",
920 (unsigned long)old_version,
921 (unsigned long)current_version);
922 for (p = slaves; p != NULL; p = p->next) {
923 if (p->flags & SLAVE_F_DEAD)
924 continue;
925 send_diffs (context, p, log_fd, database, current_version);
927 } else {
928 krb5_warnx(context,
929 "Got a signal, but no update in log version %lu",
930 (unsigned long)current_version);
934 for(p = slaves; p != NULL; p = p->next) {
935 if (p->flags & SLAVE_F_DEAD)
936 continue;
937 if (ret && FD_ISSET(p->fd, &readset)) {
938 --ret;
939 assert(ret >= 0);
940 if(process_msg (context, p, log_fd, database, current_version))
941 slave_dead(context, p);
942 } else if (slave_gone_p (p))
943 slave_dead(context, p);
944 else if (slave_missing_p (p))
945 send_are_you_there (context, p);
948 if (ret && FD_ISSET(listen_fd, &readset)) {
949 add_slave (context, keytab, &slaves, listen_fd);
950 --ret;
951 assert(ret >= 0);
953 write_stats(context, slaves, current_version);
956 if(exit_flag == SIGINT || exit_flag == SIGTERM)
957 krb5_warnx(context, "%s terminated", getprogname());
958 #ifdef SIGXCPU
959 else if(exit_flag == SIGXCPU)
960 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
961 #endif
962 else
963 krb5_warnx(context, "%s unexpected exit reason: %ld",
964 getprogname(), (long)exit_flag);
966 write_master_down(context);
968 return 0;