No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / kadm5 / ipropd_master.c
bloba3d2103960deb19e15dfda5908390eb61d4bb113
1 /*
2 * Copyright (c) 1997 - 2007 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 __RCSID("$Heimdal: ipropd_master.c 22211 2007-12-07 19:27:27Z lha $"
38 "$NetBSD: ipropd_master.c,v 1.3 2008/03/23 23:09:04 he Exp $");
40 static krb5_log_facility *log_facility;
42 const char *slave_stats_file;
43 const char *slave_time_missing = "2 min";
44 const char *slave_time_gone = "5 min";
46 static int time_before_missing;
47 static int time_before_gone;
49 const char *master_hostname;
51 static int
52 make_signal_socket (krb5_context context)
54 struct sockaddr_un addr;
55 const char *fn;
56 int fd;
58 fn = kadm5_log_signal_socket(context);
60 fd = socket (AF_UNIX, SOCK_DGRAM, 0);
61 if (fd < 0)
62 krb5_err (context, 1, errno, "socket AF_UNIX");
63 memset (&addr, 0, sizeof(addr));
64 addr.sun_family = AF_UNIX;
65 strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
66 unlink (addr.sun_path);
67 if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
68 krb5_err (context, 1, errno, "bind %s", addr.sun_path);
69 return fd;
72 static int
73 make_listen_socket (krb5_context context, const char *port_str)
75 int fd;
76 int one = 1;
77 struct sockaddr_in addr;
79 fd = socket (AF_INET, SOCK_STREAM, 0);
80 if (fd < 0)
81 krb5_err (context, 1, errno, "socket AF_INET");
82 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
83 memset (&addr, 0, sizeof(addr));
84 addr.sin_family = AF_INET;
86 if (port_str) {
87 addr.sin_port = krb5_getportbyname (context,
88 port_str, "tcp",
89 0);
90 if (addr.sin_port == 0) {
91 char *ptr;
92 long port;
94 port = strtol (port_str, &ptr, 10);
95 if (port == 0 && ptr == port_str)
96 krb5_errx (context, 1, "bad port `%s'", port_str);
97 addr.sin_port = htons(port);
99 } else {
100 addr.sin_port = krb5_getportbyname (context, IPROP_SERVICE,
101 "tcp", IPROP_PORT);
103 if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
104 krb5_err (context, 1, errno, "bind");
105 if (listen(fd, SOMAXCONN) < 0)
106 krb5_err (context, 1, errno, "listen");
107 return fd;
110 struct slave {
111 int fd;
112 struct sockaddr_in addr;
113 char *name;
114 krb5_auth_context ac;
115 uint32_t version;
116 time_t seen;
117 unsigned long flags;
118 #define SLAVE_F_DEAD 0x1
119 #define SLAVE_F_AYT 0x2
120 struct slave *next;
123 typedef struct slave slave;
125 static int
126 check_acl (krb5_context context, const char *name)
128 const char *fn;
129 FILE *fp;
130 char buf[256];
131 int ret = 1;
132 char *slavefile;
134 asprintf(&slavefile, "%s/slaves", hdb_db_dir(context));
136 fn = krb5_config_get_string_default(context,
137 NULL,
138 slavefile,
139 "kdc",
140 "iprop-acl",
141 NULL);
143 fp = fopen (fn, "r");
144 free(slavefile);
145 if (fp == NULL)
146 return 1;
147 while (fgets(buf, sizeof(buf), fp) != NULL) {
148 buf[strcspn(buf, "\r\n")] = '\0';
149 if (strcmp (buf, name) == 0) {
150 ret = 0;
151 break;
154 fclose (fp);
155 return ret;
158 static void
159 slave_seen(slave *s)
161 s->flags &= ~SLAVE_F_AYT;
162 s->seen = time(NULL);
165 static int
166 slave_missing_p (slave *s)
168 if (time(NULL) > s->seen + time_before_missing)
169 return 1;
170 return 0;
173 static int
174 slave_gone_p (slave *s)
176 if (time(NULL) > s->seen + time_before_gone)
177 return 1;
178 return 0;
181 static void
182 slave_dead(krb5_context context, slave *s)
184 krb5_warnx(context, "slave %s dead", s->name);
186 if (s->fd >= 0) {
187 close (s->fd);
188 s->fd = -1;
190 s->flags |= SLAVE_F_DEAD;
191 slave_seen(s);
194 static void
195 remove_slave (krb5_context context, slave *s, slave **root)
197 slave **p;
199 if (s->fd >= 0)
200 close (s->fd);
201 if (s->name)
202 free (s->name);
203 if (s->ac)
204 krb5_auth_con_free (context, s->ac);
206 for (p = root; *p; p = &(*p)->next)
207 if (*p == s) {
208 *p = s->next;
209 break;
211 free (s);
214 static void
215 add_slave (krb5_context context, krb5_keytab keytab, slave **root, int fd)
217 krb5_principal server;
218 krb5_error_code ret;
219 slave *s;
220 socklen_t addr_len;
221 krb5_ticket *ticket = NULL;
222 char hostname[128];
224 s = malloc(sizeof(*s));
225 if (s == NULL) {
226 krb5_warnx (context, "add_slave: no memory");
227 return;
229 s->name = NULL;
230 s->ac = NULL;
232 addr_len = sizeof(s->addr);
233 s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
234 if (s->fd < 0) {
235 krb5_warn (context, errno, "accept");
236 goto error;
238 if (master_hostname)
239 strlcpy(hostname, master_hostname, sizeof(hostname));
240 else
241 gethostname(hostname, sizeof(hostname));
243 ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
244 KRB5_NT_SRV_HST, &server);
245 if (ret) {
246 krb5_warn (context, ret, "krb5_sname_to_principal");
247 goto error;
250 ret = krb5_recvauth (context, &s->ac, &s->fd,
251 IPROP_VERSION, server, 0, keytab, &ticket);
252 krb5_free_principal (context, server);
253 if (ret) {
254 krb5_warn (context, ret, "krb5_recvauth");
255 goto error;
257 ret = krb5_unparse_name (context, ticket->client, &s->name);
258 if (ret) {
259 krb5_warn (context, ret, "krb5_unparse_name");
260 goto error;
262 if (check_acl (context, s->name)) {
263 krb5_warnx (context, "%s not in acl", s->name);
264 goto error;
266 krb5_free_ticket (context, ticket);
267 ticket = NULL;
270 slave *l = *root;
272 while (l) {
273 if (strcmp(l->name, s->name) == 0)
274 break;
275 l = l->next;
277 if (l) {
278 if (l->flags & SLAVE_F_DEAD) {
279 remove_slave(context, l, root);
280 } else {
281 krb5_warnx (context, "second connection from %s", s->name);
282 goto error;
287 krb5_warnx (context, "connection from %s", s->name);
289 s->version = 0;
290 s->flags = 0;
291 slave_seen(s);
292 s->next = *root;
293 *root = s;
294 return;
295 error:
296 remove_slave(context, s, root);
299 struct prop_context {
300 krb5_auth_context auth_context;
301 int fd;
304 static int
305 prop_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
307 krb5_error_code ret;
308 krb5_storage *sp;
309 krb5_data data;
310 struct slave *s = (struct slave *)v;
312 ret = hdb_entry2value (context, &entry->entry, &data);
313 if (ret)
314 return ret;
315 ret = krb5_data_realloc (&data, data.length + 4);
316 if (ret) {
317 krb5_data_free (&data);
318 return ret;
320 memmove ((char *)data.data + 4, data.data, data.length - 4);
321 sp = krb5_storage_from_data(&data);
322 if (sp == NULL) {
323 krb5_data_free (&data);
324 return ENOMEM;
326 krb5_store_int32(sp, ONE_PRINC);
327 krb5_storage_free(sp);
329 ret = krb5_write_priv_message (context, s->ac, &s->fd, &data);
330 krb5_data_free (&data);
331 return ret;
334 static int
335 send_complete (krb5_context context, slave *s,
336 const char *database, uint32_t current_version)
338 krb5_error_code ret;
339 krb5_storage *sp;
340 HDB *db;
341 krb5_data data;
342 char buf[8];
344 ret = hdb_create (context, &db, database);
345 if (ret)
346 krb5_err (context, 1, ret, "hdb_create: %s", database);
347 ret = db->hdb_open (context, db, O_RDONLY, 0);
348 if (ret)
349 krb5_err (context, 1, ret, "db->open");
351 sp = krb5_storage_from_mem (buf, 4);
352 if (sp == NULL)
353 krb5_errx (context, 1, "krb5_storage_from_mem");
354 krb5_store_int32 (sp, TELL_YOU_EVERYTHING);
355 krb5_storage_free (sp);
357 data.data = buf;
358 data.length = 4;
360 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
362 if (ret) {
363 krb5_warn (context, ret, "krb5_write_priv_message");
364 slave_dead(context, s);
365 return ret;
368 ret = hdb_foreach (context, db, 0, prop_one, s);
369 if (ret) {
370 krb5_warn (context, ret, "hdb_foreach");
371 slave_dead(context, s);
372 return ret;
375 (*db->hdb_close)(context, db);
376 (*db->hdb_destroy)(context, db);
378 sp = krb5_storage_from_mem (buf, 8);
379 if (sp == NULL)
380 krb5_errx (context, 1, "krb5_storage_from_mem");
381 krb5_store_int32 (sp, NOW_YOU_HAVE);
382 krb5_store_int32 (sp, current_version);
383 krb5_storage_free (sp);
385 data.length = 8;
387 s->version = current_version;
389 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
390 if (ret) {
391 slave_dead(context, s);
392 krb5_warn (context, ret, "krb5_write_priv_message");
393 return ret;
396 slave_seen(s);
398 return 0;
401 static int
402 send_are_you_there (krb5_context context, slave *s)
404 krb5_storage *sp;
405 krb5_data data;
406 char buf[4];
407 int ret;
409 if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
410 return 0;
412 s->flags |= SLAVE_F_AYT;
414 data.data = buf;
415 data.length = 4;
417 sp = krb5_storage_from_mem (buf, 4);
418 if (sp == NULL) {
419 krb5_warnx (context, "are_you_there: krb5_data_alloc");
420 slave_dead(context, s);
421 return 1;
423 krb5_store_int32 (sp, ARE_YOU_THERE);
424 krb5_storage_free (sp);
426 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
428 if (ret) {
429 krb5_warn (context, ret, "are_you_there: krb5_write_priv_message");
430 slave_dead(context, s);
431 return 1;
434 return 0;
437 static int
438 send_diffs (krb5_context context, slave *s, int log_fd,
439 const char *database, uint32_t current_version)
441 krb5_storage *sp;
442 uint32_t ver;
443 time_t timestamp;
444 enum kadm_ops op;
445 uint32_t len;
446 off_t right, left;
447 krb5_data data;
448 int ret = 0;
450 if (s->version == current_version) {
451 krb5_warnx(context, "slave %s in sync already at version %ld",
452 s->name, (long)s->version);
453 return 0;
456 if (s->flags & SLAVE_F_DEAD)
457 return 0;
459 /* if slave is a fresh client, starting over */
460 if (s->version == 0) {
461 krb5_warnx(context, "sending complete log to fresh slave %s",
462 s->name);
463 return send_complete (context, s, database, current_version);
466 sp = kadm5_log_goto_end (log_fd);
467 right = krb5_storage_seek(sp, 0, SEEK_CUR);
468 for (;;) {
469 ret = kadm5_log_previous (context, sp, &ver, &timestamp, &op, &len);
470 if (ret)
471 krb5_err(context, 1, ret,
472 "send_diffs: failed to find previous entry");
473 left = krb5_storage_seek(sp, -16, SEEK_CUR);
474 if (ver == s->version)
475 return 0;
476 if (ver == s->version + 1)
477 break;
478 if (left == 0) {
479 krb5_warnx(context,
480 "slave %s (version %lu) out of sync with master "
481 "(first version in log %lu), sending complete database",
482 s->name, (unsigned long)s->version, (unsigned long)ver);
483 return send_complete (context, s, database, current_version);
487 krb5_warnx(context,
488 "syncing slave %s from version %lu to version %lu",
489 s->name, (unsigned long)s->version,
490 (unsigned long)current_version);
492 ret = krb5_data_alloc (&data, right - left + 4);
493 if (ret) {
494 krb5_warn (context, ret, "send_diffs: krb5_data_alloc");
495 slave_dead(context, s);
496 return 1;
498 krb5_storage_read (sp, (char *)data.data + 4, data.length - 4);
499 krb5_storage_free(sp);
501 sp = krb5_storage_from_data (&data);
502 if (sp == NULL) {
503 krb5_warnx (context, "send_diffs: krb5_storage_from_data");
504 slave_dead(context, s);
505 return 1;
507 krb5_store_int32 (sp, FOR_YOU);
508 krb5_storage_free(sp);
510 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
511 krb5_data_free(&data);
513 if (ret) {
514 krb5_warn (context, ret, "send_diffs: krb5_write_priv_message");
515 slave_dead(context, s);
516 return 1;
518 slave_seen(s);
520 s->version = current_version;
522 return 0;
525 static int
526 process_msg (krb5_context context, slave *s, int log_fd,
527 const char *database, uint32_t current_version)
529 int ret = 0;
530 krb5_data out;
531 krb5_storage *sp;
532 int32_t tmp;
534 ret = krb5_read_priv_message(context, s->ac, &s->fd, &out);
535 if(ret) {
536 krb5_warn (context, ret, "error reading message from %s", s->name);
537 return 1;
540 sp = krb5_storage_from_mem (out.data, out.length);
541 if (sp == NULL) {
542 krb5_warnx (context, "process_msg: no memory");
543 krb5_data_free (&out);
544 return 1;
546 if (krb5_ret_int32 (sp, &tmp) != 0) {
547 krb5_warnx (context, "process_msg: client send too short command");
548 krb5_data_free (&out);
549 return 1;
551 switch (tmp) {
552 case I_HAVE :
553 ret = krb5_ret_int32 (sp, &tmp);
554 if (ret != 0) {
555 krb5_warnx (context, "process_msg: client send too I_HAVE data");
556 break;
558 /* new started slave that have old log */
559 if (s->version == 0 && tmp != 0) {
560 if (s->version < tmp) {
561 krb5_warnx (context, "Slave %s have later version the master "
562 "OUT OF SYNC", s->name);
563 } else {
564 s->version = tmp;
567 if (tmp < s->version) {
568 krb5_warnx (context, "Slave claims to not have "
569 "version we already sent to it");
570 } else {
571 ret = send_diffs (context, s, log_fd, database, current_version);
573 break;
574 case I_AM_HERE :
575 break;
576 case ARE_YOU_THERE:
577 case FOR_YOU :
578 default :
579 krb5_warnx (context, "Ignoring command %d", tmp);
580 break;
583 krb5_data_free (&out);
585 slave_seen(s);
587 return ret;
590 #define SLAVE_NAME "Name"
591 #define SLAVE_ADDRESS "Address"
592 #define SLAVE_VERSION "Version"
593 #define SLAVE_STATUS "Status"
594 #define SLAVE_SEEN "Last Seen"
596 static FILE *
597 open_stats(krb5_context context)
599 char *statfile = NULL;
600 const char *fn;
601 FILE *f;
603 if (slave_stats_file)
604 fn = slave_stats_file;
605 else {
606 asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context));
607 fn = krb5_config_get_string_default(context,
608 NULL,
609 statfile,
610 "kdc",
611 "iprop-stats",
612 NULL);
614 f = fopen(fn, "w");
615 if (statfile)
616 free(statfile);
618 return f;
621 static void
622 write_master_down(krb5_context context)
624 char str[100];
625 time_t t = time(NULL);
626 FILE *fp;
628 fp = open_stats(context);
629 if (fp == NULL)
630 return;
631 krb5_format_time(context, t, str, sizeof(str), TRUE);
632 fprintf(fp, "master down at %s\n", str);
634 fclose(fp);
637 static void
638 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
640 char str[100];
641 rtbl_t tbl;
642 time_t t = time(NULL);
643 FILE *fp;
645 fp = open_stats(context);
646 if (fp == NULL)
647 return;
649 krb5_format_time(context, t, str, sizeof(str), TRUE);
650 fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
652 fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
654 tbl = rtbl_create();
655 if (tbl == NULL) {
656 fclose(fp);
657 return;
660 rtbl_add_column(tbl, SLAVE_NAME, 0);
661 rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
662 rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
663 rtbl_add_column(tbl, SLAVE_STATUS, 0);
664 rtbl_add_column(tbl, SLAVE_SEEN, 0);
666 rtbl_set_prefix(tbl, " ");
667 rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
669 while (slaves) {
670 krb5_address addr;
671 krb5_error_code ret;
672 rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
673 ret = krb5_sockaddr2address (context,
674 (struct sockaddr*)&slaves->addr, &addr);
675 if(ret == 0) {
676 krb5_print_address(&addr, str, sizeof(str), NULL);
677 krb5_free_address(context, &addr);
678 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
679 } else
680 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
682 snprintf(str, sizeof(str), "%u", (unsigned)slaves->version);
683 rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
685 if (slaves->flags & SLAVE_F_DEAD)
686 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
687 else
688 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
690 ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
691 rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
693 slaves = slaves->next;
696 rtbl_format(tbl, fp);
697 rtbl_destroy(tbl);
699 fclose(fp);
703 static char *realm;
704 static int version_flag;
705 static int help_flag;
706 static char *keytab_str = "HDB:";
707 static char *database;
708 static char *config_file;
709 static char *port_str;
710 static int detach_from_console = 0;
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 { "detach", 0, arg_flag, &detach_from_console,
727 "detach from console" },
728 { "hostname", 0, arg_string, &master_hostname,
729 "hostname of master (if not same as hostname)", "hostname" },
730 { "version", 0, arg_flag, &version_flag },
731 { "help", 0, arg_flag, &help_flag }
733 static int num_args = sizeof(args) / sizeof(args[0]);
736 main(int argc, char **argv)
738 krb5_error_code ret;
739 krb5_context context;
740 void *kadm_handle;
741 kadm5_server_context *server_context;
742 kadm5_config_params conf;
743 int signal_fd, listen_fd;
744 int log_fd;
745 slave *slaves = NULL;
746 uint32_t current_version = 0, old_version = 0;
747 krb5_keytab keytab;
748 int optidx;
749 char **files;
751 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
753 if(help_flag)
754 krb5_std_usage(0, args, num_args);
755 if(version_flag) {
756 print_version(NULL);
757 exit(0);
760 setup_signal();
762 if (config_file == NULL) {
763 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
764 if (config_file == NULL)
765 errx(1, "out of memory");
768 ret = krb5_prepend_config_files_default(config_file, &files);
769 if (ret)
770 krb5_err(context, 1, ret, "getting configuration files");
772 ret = krb5_set_config_files(context, files);
773 krb5_free_config_files(files);
774 if (ret)
775 krb5_err(context, 1, ret, "reading configuration files");
777 time_before_gone = parse_time (slave_time_gone, "s");
778 if (time_before_gone < 0)
779 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
780 time_before_missing = parse_time (slave_time_missing, "s");
781 if (time_before_missing < 0)
782 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
784 if (detach_from_console)
785 daemon(0, 0);
786 pidfile (NULL);
787 krb5_openlog (context, "ipropd-master", &log_facility);
788 krb5_set_warn_dest(context, log_facility);
790 ret = krb5_kt_register(context, &hdb_kt_ops);
791 if(ret)
792 krb5_err(context, 1, ret, "krb5_kt_register");
794 ret = krb5_kt_resolve(context, keytab_str, &keytab);
795 if(ret)
796 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
798 memset(&conf, 0, sizeof(conf));
799 if(realm) {
800 conf.mask |= KADM5_CONFIG_REALM;
801 conf.realm = realm;
803 ret = kadm5_init_with_skey_ctx (context,
804 KADM5_ADMIN_SERVICE,
805 NULL,
806 KADM5_ADMIN_SERVICE,
807 &conf, 0, 0,
808 &kadm_handle);
809 if (ret)
810 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
812 server_context = (kadm5_server_context *)kadm_handle;
814 log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
815 if (log_fd < 0)
816 krb5_err (context, 1, errno, "open %s",
817 server_context->log_context.log_file);
819 signal_fd = make_signal_socket (context);
820 listen_fd = make_listen_socket (context, port_str);
822 kadm5_log_get_version_fd (log_fd, &current_version);
824 krb5_warnx(context, "ipropd-master started at version: %lu",
825 (unsigned long)current_version);
827 while(exit_flag == 0){
828 slave *p;
829 fd_set readset;
830 int max_fd = 0;
831 struct timeval to = {30, 0};
832 uint32_t vers;
834 if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE)
835 krb5_errx (context, 1, "fd too large");
837 FD_ZERO(&readset);
838 FD_SET(signal_fd, &readset);
839 max_fd = max(max_fd, signal_fd);
840 FD_SET(listen_fd, &readset);
841 max_fd = max(max_fd, listen_fd);
843 for (p = slaves; p != NULL; p = p->next) {
844 if (p->flags & SLAVE_F_DEAD)
845 continue;
846 FD_SET(p->fd, &readset);
847 max_fd = max(max_fd, p->fd);
850 ret = select (max_fd + 1,
851 &readset, NULL, NULL, &to);
852 if (ret < 0) {
853 if (errno == EINTR)
854 continue;
855 else
856 krb5_err (context, 1, errno, "select");
859 if (ret == 0) {
860 old_version = current_version;
861 kadm5_log_get_version_fd (log_fd, &current_version);
863 if (current_version > old_version) {
864 krb5_warnx(context,
865 "Missed a signal, updating slaves %lu to %lu",
866 (unsigned long)old_version,
867 (unsigned long)current_version);
868 for (p = slaves; p != NULL; p = p->next) {
869 if (p->flags & SLAVE_F_DEAD)
870 continue;
871 send_diffs (context, p, log_fd, database, current_version);
876 if (ret && FD_ISSET(signal_fd, &readset)) {
877 struct sockaddr_un peer_addr;
878 socklen_t peer_len = sizeof(peer_addr);
880 if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
881 (struct sockaddr *)&peer_addr, &peer_len) < 0) {
882 krb5_warn (context, errno, "recvfrom");
883 continue;
885 --ret;
886 assert(ret >= 0);
887 old_version = current_version;
888 kadm5_log_get_version_fd (log_fd, &current_version);
889 if (current_version > old_version) {
890 krb5_warnx(context,
891 "Got a signal, updating slaves %lu to %lu",
892 (unsigned long)old_version,
893 (unsigned long)current_version);
894 for (p = slaves; p != NULL; p = p->next)
895 send_diffs (context, p, log_fd, database, current_version);
896 } else {
897 krb5_warnx(context,
898 "Got a signal, but no update in log version %lu",
899 (unsigned long)current_version);
903 for(p = slaves; p != NULL; p = p->next) {
904 if (p->flags & SLAVE_F_DEAD)
905 continue;
906 if (ret && FD_ISSET(p->fd, &readset)) {
907 --ret;
908 assert(ret >= 0);
909 if(process_msg (context, p, log_fd, database, current_version))
910 slave_dead(context, p);
911 } else if (slave_gone_p (p))
912 slave_dead(context, p);
913 else if (slave_missing_p (p)) {
914 krb5_warnx(context, "slave %s missing, sending AYT", p->name);
915 send_are_you_there (context, p);
919 if (ret && FD_ISSET(listen_fd, &readset)) {
920 add_slave (context, keytab, &slaves, listen_fd);
921 --ret;
922 assert(ret >= 0);
924 write_stats(context, slaves, current_version);
927 if(exit_flag == SIGXCPU)
928 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
929 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
930 krb5_warnx(context, "%s terminated", getprogname());
931 else
932 krb5_warnx(context, "%s unexpected exit reason: %ld",
933 getprogname(), (long)exit_flag);
935 write_master_down(context);
937 return 0;