support output passphrases for copy certificate
[heimdal.git] / lib / kadm5 / ipropd_master.c
blob925b43d435bf63dbcb3ccbb18904e52e2880ab4d
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 int
49 make_signal_socket (krb5_context context)
51 struct sockaddr_un addr;
52 const char *fn;
53 int fd;
55 fn = kadm5_log_signal_socket(context);
57 fd = socket (AF_UNIX, SOCK_DGRAM, 0);
58 if (fd < 0)
59 krb5_err (context, 1, errno, "socket AF_UNIX");
60 memset (&addr, 0, sizeof(addr));
61 addr.sun_family = AF_UNIX;
62 strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
63 unlink (addr.sun_path);
64 if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
65 krb5_err (context, 1, errno, "bind %s", addr.sun_path);
66 return fd;
69 static int
70 make_listen_socket (krb5_context context, const char *port_str)
72 int fd;
73 int one = 1;
74 struct sockaddr_in addr;
76 fd = socket (AF_INET, SOCK_STREAM, 0);
77 if (fd < 0)
78 krb5_err (context, 1, errno, "socket AF_INET");
79 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
80 memset (&addr, 0, sizeof(addr));
81 addr.sin_family = AF_INET;
83 if (port_str) {
84 addr.sin_port = krb5_getportbyname (context,
85 port_str, "tcp",
86 0);
87 if (addr.sin_port == 0) {
88 char *ptr;
89 long port;
91 port = strtol (port_str, &ptr, 10);
92 if (port == 0 && ptr == port_str)
93 krb5_errx (context, 1, "bad port `%s'", port_str);
94 addr.sin_port = htons(port);
96 } else {
97 addr.sin_port = krb5_getportbyname (context, IPROP_SERVICE,
98 "tcp", IPROP_PORT);
100 if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
101 krb5_err (context, 1, errno, "bind");
102 if (listen(fd, SOMAXCONN) < 0)
103 krb5_err (context, 1, errno, "listen");
104 return fd;
107 struct slave {
108 int fd;
109 struct sockaddr_in addr;
110 char *name;
111 krb5_auth_context ac;
112 uint32_t version;
113 time_t seen;
114 unsigned long flags;
115 #define SLAVE_F_DEAD 0x1
116 #define SLAVE_F_AYT 0x2
117 struct slave *next;
120 typedef struct slave slave;
122 static int
123 check_acl (krb5_context context, const char *name)
125 const char *fn;
126 FILE *fp;
127 char buf[256];
128 int ret = 1;
129 char *slavefile;
131 asprintf(&slavefile, "%s/slaves", hdb_db_dir(context));
133 fn = krb5_config_get_string_default(context,
134 NULL,
135 slavefile,
136 "kdc",
137 "iprop-acl",
138 NULL);
140 fp = fopen (fn, "r");
141 free(slavefile);
142 if (fp == NULL)
143 return 1;
144 while (fgets(buf, sizeof(buf), fp) != NULL) {
145 buf[strcspn(buf, "\r\n")] = '\0';
146 if (strcmp (buf, name) == 0) {
147 ret = 0;
148 break;
151 fclose (fp);
152 return ret;
155 static void
156 slave_seen(slave *s)
158 s->flags &= ~SLAVE_F_AYT;
159 s->seen = time(NULL);
162 static int
163 slave_missing_p (slave *s)
165 if (time(NULL) > s->seen + time_before_missing)
166 return 1;
167 return 0;
170 static int
171 slave_gone_p (slave *s)
173 if (time(NULL) > s->seen + time_before_gone)
174 return 1;
175 return 0;
178 static void
179 slave_dead(krb5_context context, slave *s)
181 krb5_warnx(context, "slave %s dead", s->name);
183 if (s->fd >= 0) {
184 close (s->fd);
185 s->fd = -1;
187 s->flags |= SLAVE_F_DEAD;
188 slave_seen(s);
191 static void
192 remove_slave (krb5_context context, slave *s, slave **root)
194 slave **p;
196 if (s->fd >= 0)
197 close (s->fd);
198 if (s->name)
199 free (s->name);
200 if (s->ac)
201 krb5_auth_con_free (context, s->ac);
203 for (p = root; *p; p = &(*p)->next)
204 if (*p == s) {
205 *p = s->next;
206 break;
208 free (s);
211 static void
212 add_slave (krb5_context context, krb5_keytab keytab, slave **root, int fd)
214 krb5_principal server;
215 krb5_error_code ret;
216 slave *s;
217 socklen_t addr_len;
218 krb5_ticket *ticket = NULL;
219 char hostname[128];
221 s = malloc(sizeof(*s));
222 if (s == NULL) {
223 krb5_warnx (context, "add_slave: no memory");
224 return;
226 s->name = NULL;
227 s->ac = NULL;
229 addr_len = sizeof(s->addr);
230 s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
231 if (s->fd < 0) {
232 krb5_warn (context, errno, "accept");
233 goto error;
235 if (master_hostname)
236 strlcpy(hostname, master_hostname, sizeof(hostname));
237 else
238 gethostname(hostname, sizeof(hostname));
240 ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
241 KRB5_NT_SRV_HST, &server);
242 if (ret) {
243 krb5_warn (context, ret, "krb5_sname_to_principal");
244 goto error;
247 ret = krb5_recvauth (context, &s->ac, &s->fd,
248 IPROP_VERSION, server, 0, keytab, &ticket);
249 krb5_free_principal (context, server);
250 if (ret) {
251 krb5_warn (context, ret, "krb5_recvauth");
252 goto error;
254 ret = krb5_unparse_name (context, ticket->client, &s->name);
255 krb5_free_ticket (context, ticket);
256 if (ret) {
257 krb5_warn (context, ret, "krb5_unparse_name");
258 goto error;
260 if (check_acl (context, s->name)) {
261 krb5_warnx (context, "%s not in acl", s->name);
262 goto error;
266 slave *l = *root;
268 while (l) {
269 if (strcmp(l->name, s->name) == 0)
270 break;
271 l = l->next;
273 if (l) {
274 if (l->flags & SLAVE_F_DEAD) {
275 remove_slave(context, l, root);
276 } else {
277 krb5_warnx (context, "second connection from %s", s->name);
278 goto error;
283 krb5_warnx (context, "connection from %s", s->name);
285 s->version = 0;
286 s->flags = 0;
287 slave_seen(s);
288 s->next = *root;
289 *root = s;
290 return;
291 error:
292 remove_slave(context, s, root);
295 struct prop_context {
296 krb5_auth_context auth_context;
297 int fd;
300 static int
301 prop_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
303 krb5_error_code ret;
304 krb5_storage *sp;
305 krb5_data data;
306 struct slave *s = (struct slave *)v;
308 ret = hdb_entry2value (context, &entry->entry, &data);
309 if (ret)
310 return ret;
311 ret = krb5_data_realloc (&data, data.length + 4);
312 if (ret) {
313 krb5_data_free (&data);
314 return ret;
316 memmove ((char *)data.data + 4, data.data, data.length - 4);
317 sp = krb5_storage_from_data(&data);
318 if (sp == NULL) {
319 krb5_data_free (&data);
320 return ENOMEM;
322 krb5_store_int32(sp, ONE_PRINC);
323 krb5_storage_free(sp);
325 ret = krb5_write_priv_message (context, s->ac, &s->fd, &data);
326 krb5_data_free (&data);
327 return ret;
330 static int
331 send_complete (krb5_context context, slave *s,
332 const char *database, uint32_t current_version)
334 krb5_error_code ret;
335 krb5_storage *sp;
336 HDB *db;
337 krb5_data data;
338 char buf[8];
340 ret = hdb_create (context, &db, database);
341 if (ret)
342 krb5_err (context, 1, ret, "hdb_create: %s", database);
343 ret = db->hdb_open (context, db, O_RDONLY, 0);
344 if (ret)
345 krb5_err (context, 1, ret, "db->open");
347 sp = krb5_storage_from_mem (buf, 4);
348 if (sp == NULL)
349 krb5_errx (context, 1, "krb5_storage_from_mem");
350 krb5_store_int32 (sp, TELL_YOU_EVERYTHING);
351 krb5_storage_free (sp);
353 data.data = buf;
354 data.length = 4;
356 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
358 if (ret) {
359 krb5_warn (context, ret, "krb5_write_priv_message");
360 slave_dead(context, s);
361 return ret;
364 ret = hdb_foreach (context, db, 0, prop_one, s);
365 if (ret) {
366 krb5_warn (context, ret, "hdb_foreach");
367 slave_dead(context, s);
368 return ret;
371 (*db->hdb_close)(context, db);
372 (*db->hdb_destroy)(context, db);
374 sp = krb5_storage_from_mem (buf, 8);
375 if (sp == NULL)
376 krb5_errx (context, 1, "krb5_storage_from_mem");
377 krb5_store_int32 (sp, NOW_YOU_HAVE);
378 krb5_store_int32 (sp, current_version);
379 krb5_storage_free (sp);
381 data.length = 8;
383 s->version = current_version;
385 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
386 if (ret) {
387 slave_dead(context, s);
388 krb5_warn (context, ret, "krb5_write_priv_message");
389 return ret;
392 slave_seen(s);
394 return 0;
397 static int
398 send_are_you_there (krb5_context context, slave *s)
400 krb5_storage *sp;
401 krb5_data data;
402 char buf[4];
403 int ret;
405 if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
406 return 0;
408 krb5_warnx(context, "slave %s missing, sending AYT", s->name);
410 s->flags |= SLAVE_F_AYT;
412 data.data = buf;
413 data.length = 4;
415 sp = krb5_storage_from_mem (buf, 4);
416 if (sp == NULL) {
417 krb5_warnx (context, "are_you_there: krb5_data_alloc");
418 slave_dead(context, s);
419 return 1;
421 krb5_store_int32 (sp, ARE_YOU_THERE);
422 krb5_storage_free (sp);
424 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
426 if (ret) {
427 krb5_warn (context, ret, "are_you_there: krb5_write_priv_message");
428 slave_dead(context, s);
429 return 1;
432 return 0;
435 static int
436 send_diffs (krb5_context context, slave *s, int log_fd,
437 const char *database, uint32_t current_version)
439 krb5_storage *sp;
440 uint32_t ver;
441 time_t timestamp;
442 enum kadm_ops op;
443 uint32_t len;
444 off_t right, left;
445 krb5_data data;
446 int ret = 0;
448 if (s->version == current_version) {
449 krb5_warnx(context, "slave %s in sync already at version %ld",
450 s->name, (long)s->version);
451 return 0;
454 if (s->flags & SLAVE_F_DEAD)
455 return 0;
457 /* if slave is a fresh client, starting over */
458 if (s->version == 0) {
459 krb5_warnx(context, "sending complete log to fresh slave %s",
460 s->name);
461 return send_complete (context, s, database, current_version);
464 sp = kadm5_log_goto_end (log_fd);
465 right = krb5_storage_seek(sp, 0, SEEK_CUR);
466 for (;;) {
467 ret = kadm5_log_previous (context, sp, &ver, &timestamp, &op, &len);
468 if (ret)
469 krb5_err(context, 1, ret,
470 "send_diffs: failed to find previous entry");
471 left = krb5_storage_seek(sp, -16, SEEK_CUR);
472 if (ver == s->version)
473 return 0;
474 if (ver == s->version + 1)
475 break;
476 if (left == 0) {
477 krb5_warnx(context,
478 "slave %s (version %lu) out of sync with master "
479 "(first version in log %lu), sending complete database",
480 s->name, (unsigned long)s->version, (unsigned long)ver);
481 return send_complete (context, s, database, current_version);
485 krb5_warnx(context,
486 "syncing slave %s from version %lu to version %lu",
487 s->name, (unsigned long)s->version,
488 (unsigned long)current_version);
490 ret = krb5_data_alloc (&data, right - left + 4);
491 if (ret) {
492 krb5_warn (context, ret, "send_diffs: krb5_data_alloc");
493 slave_dead(context, s);
494 return 1;
496 krb5_storage_read (sp, (char *)data.data + 4, data.length - 4);
497 krb5_storage_free(sp);
499 sp = krb5_storage_from_data (&data);
500 if (sp == NULL) {
501 krb5_warnx (context, "send_diffs: krb5_storage_from_data");
502 slave_dead(context, s);
503 return 1;
505 krb5_store_int32 (sp, FOR_YOU);
506 krb5_storage_free(sp);
508 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
509 krb5_data_free(&data);
511 if (ret) {
512 krb5_warn (context, ret, "send_diffs: krb5_write_priv_message");
513 slave_dead(context, s);
514 return 1;
516 slave_seen(s);
518 s->version = current_version;
520 return 0;
523 static int
524 process_msg (krb5_context context, slave *s, int log_fd,
525 const char *database, uint32_t current_version)
527 int ret = 0;
528 krb5_data out;
529 krb5_storage *sp;
530 int32_t tmp;
532 ret = krb5_read_priv_message(context, s->ac, &s->fd, &out);
533 if(ret) {
534 krb5_warn (context, ret, "error reading message from %s", s->name);
535 return 1;
538 sp = krb5_storage_from_mem (out.data, out.length);
539 if (sp == NULL) {
540 krb5_warnx (context, "process_msg: no memory");
541 krb5_data_free (&out);
542 return 1;
544 if (krb5_ret_int32 (sp, &tmp) != 0) {
545 krb5_warnx (context, "process_msg: client send too short command");
546 krb5_data_free (&out);
547 return 1;
549 switch (tmp) {
550 case I_HAVE :
551 ret = krb5_ret_int32 (sp, &tmp);
552 if (ret != 0) {
553 krb5_warnx (context, "process_msg: client send too I_HAVE data");
554 break;
556 /* new started slave that have old log */
557 if (s->version == 0 && tmp != 0) {
558 if (current_version < tmp) {
559 krb5_warnx (context, "Slave %s have later version the master "
560 "OUT OF SYNC", s->name);
562 s->version = tmp;
564 if (tmp < s->version) {
565 krb5_warnx (context, "Slave claims to not have "
566 "version we already sent to it");
567 } else {
568 ret = send_diffs (context, s, log_fd, database, current_version);
570 break;
571 case I_AM_HERE :
572 break;
573 case ARE_YOU_THERE:
574 case FOR_YOU :
575 default :
576 krb5_warnx (context, "Ignoring command %d", tmp);
577 break;
580 krb5_data_free (&out);
581 krb5_storage_free (sp);
583 slave_seen(s);
585 return ret;
588 #define SLAVE_NAME "Name"
589 #define SLAVE_ADDRESS "Address"
590 #define SLAVE_VERSION "Version"
591 #define SLAVE_STATUS "Status"
592 #define SLAVE_SEEN "Last Seen"
594 static FILE *
595 open_stats(krb5_context context)
597 char *statfile = NULL;
598 const char *fn;
599 FILE *f;
601 if (slave_stats_file)
602 fn = slave_stats_file;
603 else {
604 asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context));
605 fn = krb5_config_get_string_default(context,
606 NULL,
607 statfile,
608 "kdc",
609 "iprop-stats",
610 NULL);
612 f = fopen(fn, "w");
613 if (statfile)
614 free(statfile);
616 return f;
619 static void
620 write_master_down(krb5_context context)
622 char str[100];
623 time_t t = time(NULL);
624 FILE *fp;
626 fp = open_stats(context);
627 if (fp == NULL)
628 return;
629 krb5_format_time(context, t, str, sizeof(str), TRUE);
630 fprintf(fp, "master down at %s\n", str);
632 fclose(fp);
635 static void
636 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
638 char str[100];
639 rtbl_t tbl;
640 time_t t = time(NULL);
641 FILE *fp;
643 fp = open_stats(context);
644 if (fp == NULL)
645 return;
647 krb5_format_time(context, t, str, sizeof(str), TRUE);
648 fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
650 fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
652 tbl = rtbl_create();
653 if (tbl == NULL) {
654 fclose(fp);
655 return;
658 rtbl_add_column(tbl, SLAVE_NAME, 0);
659 rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
660 rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
661 rtbl_add_column(tbl, SLAVE_STATUS, 0);
662 rtbl_add_column(tbl, SLAVE_SEEN, 0);
664 rtbl_set_prefix(tbl, " ");
665 rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
667 while (slaves) {
668 krb5_address addr;
669 krb5_error_code ret;
670 rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
671 ret = krb5_sockaddr2address (context,
672 (struct sockaddr*)&slaves->addr, &addr);
673 if(ret == 0) {
674 krb5_print_address(&addr, str, sizeof(str), NULL);
675 krb5_free_address(context, &addr);
676 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
677 } else
678 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
680 snprintf(str, sizeof(str), "%u", (unsigned)slaves->version);
681 rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
683 if (slaves->flags & SLAVE_F_DEAD)
684 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
685 else
686 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
688 ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
689 rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
691 slaves = slaves->next;
694 rtbl_format(tbl, fp);
695 rtbl_destroy(tbl);
697 fclose(fp);
701 static char *realm;
702 static int version_flag;
703 static int help_flag;
704 static char *keytab_str = "HDB:";
705 static char *database;
706 static char *config_file;
707 static char *port_str;
708 #ifdef SUPPORT_DETACH
709 static int detach_from_console = 0;
710 #endif
712 static struct getargs args[] = {
713 { "config-file", 'c', arg_string, &config_file },
714 { "realm", 'r', arg_string, &realm },
715 { "keytab", 'k', arg_string, &keytab_str,
716 "keytab to get authentication from", "kspec" },
717 { "database", 'd', arg_string, &database, "database", "file"},
718 { "slave-stats-file", 0, arg_string, &slave_stats_file,
719 "file for slave status information", "file"},
720 { "time-missing", 0, arg_string, &slave_time_missing,
721 "time before slave is polled for presence", "time"},
722 { "time-gone", 0, arg_string, &slave_time_gone,
723 "time of inactivity after which a slave is considered gone", "time"},
724 { "port", 0, arg_string, &port_str,
725 "port ipropd will listen to", "port"},
726 #ifdef SUPPORT_DETACH
727 { "detach", 0, arg_flag, &detach_from_console,
728 "detach from console" },
729 #endif
730 { "hostname", 0, arg_string, &master_hostname,
731 "hostname of master (if not same as hostname)", "hostname" },
732 { "version", 0, arg_flag, &version_flag },
733 { "help", 0, arg_flag, &help_flag }
735 static int num_args = sizeof(args) / sizeof(args[0]);
738 main(int argc, char **argv)
740 krb5_error_code ret;
741 krb5_context context;
742 void *kadm_handle;
743 kadm5_server_context *server_context;
744 kadm5_config_params conf;
745 int signal_fd, listen_fd;
746 int log_fd;
747 slave *slaves = NULL;
748 uint32_t current_version = 0, old_version = 0;
749 krb5_keytab keytab;
750 int optidx;
751 char **files;
753 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
755 if(help_flag)
756 krb5_std_usage(0, args, num_args);
757 if(version_flag) {
758 print_version(NULL);
759 exit(0);
762 setup_signal();
764 if (config_file == NULL) {
765 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
766 if (config_file == NULL)
767 errx(1, "out of memory");
770 ret = krb5_prepend_config_files_default(config_file, &files);
771 if (ret)
772 krb5_err(context, 1, ret, "getting configuration files");
774 ret = krb5_set_config_files(context, files);
775 krb5_free_config_files(files);
776 if (ret)
777 krb5_err(context, 1, ret, "reading configuration files");
779 time_before_gone = parse_time (slave_time_gone, "s");
780 if (time_before_gone < 0)
781 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
782 time_before_missing = parse_time (slave_time_missing, "s");
783 if (time_before_missing < 0)
784 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
786 #ifdef SUPPORT_DETACH
787 if (detach_from_console)
788 daemon(0, 0);
789 #endif
790 pidfile (NULL);
791 krb5_openlog (context, "ipropd-master", &log_facility);
792 krb5_set_warn_dest(context, log_facility);
794 ret = krb5_kt_register(context, &hdb_kt_ops);
795 if(ret)
796 krb5_err(context, 1, ret, "krb5_kt_register");
798 ret = krb5_kt_resolve(context, keytab_str, &keytab);
799 if(ret)
800 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
802 memset(&conf, 0, sizeof(conf));
803 if(realm) {
804 conf.mask |= KADM5_CONFIG_REALM;
805 conf.realm = realm;
807 ret = kadm5_init_with_skey_ctx (context,
808 KADM5_ADMIN_SERVICE,
809 NULL,
810 KADM5_ADMIN_SERVICE,
811 &conf, 0, 0,
812 &kadm_handle);
813 if (ret)
814 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
816 server_context = (kadm5_server_context *)kadm_handle;
818 log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
819 if (log_fd < 0)
820 krb5_err (context, 1, errno, "open %s",
821 server_context->log_context.log_file);
823 signal_fd = make_signal_socket (context);
824 listen_fd = make_listen_socket (context, port_str);
826 kadm5_log_get_version_fd (log_fd, &current_version);
828 krb5_warnx(context, "ipropd-master started at version: %lu",
829 (unsigned long)current_version);
831 while(exit_flag == 0){
832 slave *p;
833 fd_set readset;
834 int max_fd = 0;
835 struct timeval to = {30, 0};
836 uint32_t vers;
838 if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE)
839 krb5_errx (context, 1, "fd too large");
841 FD_ZERO(&readset);
842 FD_SET(signal_fd, &readset);
843 max_fd = max(max_fd, signal_fd);
844 FD_SET(listen_fd, &readset);
845 max_fd = max(max_fd, listen_fd);
847 for (p = slaves; p != NULL; p = p->next) {
848 if (p->flags & SLAVE_F_DEAD)
849 continue;
850 FD_SET(p->fd, &readset);
851 max_fd = max(max_fd, p->fd);
854 ret = select (max_fd + 1,
855 &readset, NULL, NULL, &to);
856 if (ret < 0) {
857 if (errno == EINTR)
858 continue;
859 else
860 krb5_err (context, 1, errno, "select");
863 if (ret == 0) {
864 old_version = current_version;
865 kadm5_log_get_version_fd (log_fd, &current_version);
867 if (current_version > old_version) {
868 krb5_warnx(context,
869 "Missed a signal, updating slaves %lu to %lu",
870 (unsigned long)old_version,
871 (unsigned long)current_version);
872 for (p = slaves; p != NULL; p = p->next) {
873 if (p->flags & SLAVE_F_DEAD)
874 continue;
875 send_diffs (context, p, log_fd, database, current_version);
880 if (ret && FD_ISSET(signal_fd, &readset)) {
881 struct sockaddr_un peer_addr;
882 socklen_t peer_len = sizeof(peer_addr);
884 if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
885 (struct sockaddr *)&peer_addr, &peer_len) < 0) {
886 krb5_warn (context, errno, "recvfrom");
887 continue;
889 --ret;
890 assert(ret >= 0);
891 old_version = current_version;
892 kadm5_log_get_version_fd (log_fd, &current_version);
893 if (current_version > old_version) {
894 krb5_warnx(context,
895 "Got a signal, updating slaves %lu to %lu",
896 (unsigned long)old_version,
897 (unsigned long)current_version);
898 for (p = slaves; p != NULL; p = p->next) {
899 if (p->flags & SLAVE_F_DEAD)
900 continue;
901 send_diffs (context, p, log_fd, database, current_version);
903 } else {
904 krb5_warnx(context,
905 "Got a signal, but no update in log version %lu",
906 (unsigned long)current_version);
910 for(p = slaves; p != NULL; p = p->next) {
911 if (p->flags & SLAVE_F_DEAD)
912 continue;
913 if (ret && FD_ISSET(p->fd, &readset)) {
914 --ret;
915 assert(ret >= 0);
916 if(process_msg (context, p, log_fd, database, current_version))
917 slave_dead(context, p);
918 } else if (slave_gone_p (p))
919 slave_dead(context, p);
920 else if (slave_missing_p (p))
921 send_are_you_there (context, p);
924 if (ret && FD_ISSET(listen_fd, &readset)) {
925 add_slave (context, keytab, &slaves, listen_fd);
926 --ret;
927 assert(ret >= 0);
929 write_stats(context, slaves, current_version);
932 if(exit_flag == SIGXCPU)
933 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
934 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
935 krb5_warnx(context, "%s terminated", getprogname());
936 else
937 krb5_warnx(context, "%s unexpected exit reason: %ld",
938 getprogname(), (long)exit_flag);
940 write_master_down(context);
942 return 0;