clean files
[heimdal.git] / lib / kadm5 / ipropd_master.c
blob6f27ca5520aa8108a0dda1bacfec64b6d51487a5
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 = NULL;
146 if (asprintf(&slavefile, "%s/slaves", hdb_db_dir(context)) == -1
147 || slavefile == NULL)
148 errx(1, "out of memory");
150 fn = krb5_config_get_string_default(context,
151 NULL,
152 slavefile,
153 "kdc",
154 "iprop-acl",
155 NULL);
157 fp = fopen (fn, "r");
158 free(slavefile);
159 if (fp == NULL)
160 return 1;
161 while (fgets(buf, sizeof(buf), fp) != NULL) {
162 buf[strcspn(buf, "\r\n")] = '\0';
163 if (strcmp (buf, name) == 0) {
164 ret = 0;
165 break;
168 fclose (fp);
169 return ret;
172 static void
173 slave_seen(slave *s)
175 s->flags &= ~SLAVE_F_AYT;
176 s->seen = time(NULL);
179 static int
180 slave_missing_p (slave *s)
182 if (time(NULL) > s->seen + time_before_missing)
183 return 1;
184 return 0;
187 static int
188 slave_gone_p (slave *s)
190 if (time(NULL) > s->seen + time_before_gone)
191 return 1;
192 return 0;
195 static void
196 slave_dead(krb5_context context, slave *s)
198 krb5_warnx(context, "slave %s dead", s->name);
200 if (!rk_IS_BAD_SOCKET(s->fd)) {
201 rk_closesocket (s->fd);
202 s->fd = rk_INVALID_SOCKET;
204 s->flags |= SLAVE_F_DEAD;
205 slave_seen(s);
208 static void
209 remove_slave (krb5_context context, slave *s, slave **root)
211 slave **p;
213 if (!rk_IS_BAD_SOCKET(s->fd))
214 rk_closesocket (s->fd);
215 if (s->name)
216 free (s->name);
217 if (s->ac)
218 krb5_auth_con_free (context, s->ac);
220 for (p = root; *p; p = &(*p)->next)
221 if (*p == s) {
222 *p = s->next;
223 break;
225 free (s);
228 static void
229 add_slave (krb5_context context, krb5_keytab keytab, slave **root,
230 krb5_socket_t fd)
232 krb5_principal server;
233 krb5_error_code ret;
234 slave *s;
235 socklen_t addr_len;
236 krb5_ticket *ticket = NULL;
237 char hostname[128];
239 s = malloc(sizeof(*s));
240 if (s == NULL) {
241 krb5_warnx (context, "add_slave: no memory");
242 return;
244 s->name = NULL;
245 s->ac = NULL;
247 addr_len = sizeof(s->addr);
248 s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
249 if (rk_IS_BAD_SOCKET(s->fd)) {
250 krb5_warn (context, rk_SOCK_ERRNO, "accept");
251 goto error;
253 if (master_hostname)
254 strlcpy(hostname, master_hostname, sizeof(hostname));
255 else
256 gethostname(hostname, sizeof(hostname));
258 ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
259 KRB5_NT_SRV_HST, &server);
260 if (ret) {
261 krb5_warn (context, ret, "krb5_sname_to_principal");
262 goto error;
265 ret = krb5_recvauth (context, &s->ac, &s->fd,
266 IPROP_VERSION, server, 0, keytab, &ticket);
267 krb5_free_principal (context, server);
268 if (ret) {
269 krb5_warn (context, ret, "krb5_recvauth");
270 goto error;
272 ret = krb5_unparse_name (context, ticket->client, &s->name);
273 krb5_free_ticket (context, ticket);
274 if (ret) {
275 krb5_warn (context, ret, "krb5_unparse_name");
276 goto error;
278 if (check_acl (context, s->name)) {
279 krb5_warnx (context, "%s not in acl", s->name);
280 goto error;
284 slave *l = *root;
286 while (l) {
287 if (strcmp(l->name, s->name) == 0)
288 break;
289 l = l->next;
291 if (l) {
292 if (l->flags & SLAVE_F_DEAD) {
293 remove_slave(context, l, root);
294 } else {
295 krb5_warnx (context, "second connection from %s", s->name);
296 goto error;
301 krb5_warnx (context, "connection from %s", s->name);
303 s->version = 0;
304 s->flags = 0;
305 slave_seen(s);
306 s->next = *root;
307 *root = s;
308 return;
309 error:
310 remove_slave(context, s, root);
313 static int
314 dump_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
316 krb5_error_code ret;
317 krb5_storage *dump = (krb5_storage *)v;
318 krb5_storage *sp;
319 krb5_data data;
321 ret = hdb_entry2value (context, &entry->entry, &data);
322 if (ret)
323 return ret;
324 ret = krb5_data_realloc (&data, data.length + 4);
325 if (ret)
326 goto done;
327 memmove ((char *)data.data + 4, data.data, data.length - 4);
328 sp = krb5_storage_from_data(&data);
329 if (sp == NULL) {
330 ret = ENOMEM;
331 goto done;
333 krb5_store_int32(sp, ONE_PRINC);
334 krb5_storage_free(sp);
336 ret = krb5_store_data(dump, data);
338 done:
339 krb5_data_free (&data);
340 return ret;
343 static int
344 write_dump (krb5_context context, krb5_storage *dump,
345 const char *database, uint32_t current_version)
347 krb5_error_code ret;
348 krb5_storage *sp;
349 HDB *db;
350 krb5_data data;
351 char buf[8];
353 /* we assume that the caller has obtained an exclusive lock */
355 ret = krb5_storage_truncate(dump, 0);
356 if (ret)
357 return ret;
360 * First we store zero as the HDB version, this will indicate to a
361 * later reader that the dumpfile is invalid. We later write the
362 * correct version in the file after we have written all of the
363 * messages. A dump with a zero version will not be considered
364 * to be valid.
367 ret = krb5_store_uint32(dump, 0);
369 ret = hdb_create (context, &db, database);
370 if (ret)
371 krb5_err (context, 1, ret, "hdb_create: %s", database);
372 ret = db->hdb_open (context, db, O_RDONLY, 0);
373 if (ret)
374 krb5_err (context, 1, ret, "db->open");
376 sp = krb5_storage_from_mem (buf, 4);
377 if (sp == NULL)
378 krb5_errx (context, 1, "krb5_storage_from_mem");
379 krb5_store_int32 (sp, TELL_YOU_EVERYTHING);
380 krb5_storage_free (sp);
382 data.data = buf;
383 data.length = 4;
385 ret = krb5_store_data(dump, data);
386 if (ret) {
387 krb5_warn (context, ret, "write_dump");
388 return ret;
391 ret = hdb_foreach (context, db, HDB_F_ADMIN_DATA, dump_one, dump);
392 if (ret) {
393 krb5_warn (context, ret, "write_dump: hdb_foreach");
394 return ret;
397 (*db->hdb_close)(context, db);
398 (*db->hdb_destroy)(context, db);
400 sp = krb5_storage_from_mem (buf, 8);
401 if (sp == NULL)
402 krb5_errx (context, 1, "krb5_storage_from_mem");
403 krb5_store_int32 (sp, NOW_YOU_HAVE);
404 krb5_store_int32 (sp, current_version);
405 krb5_storage_free (sp);
407 data.length = 8;
409 ret = krb5_store_data(dump, data);
410 if (ret) {
411 krb5_warn (context, ret, "write_dump");
412 return ret;
416 * We must ensure that the entire valid dump is written to disk
417 * before we write the current version at the front thus making
418 * it a valid dump file. If we crash around here, this can be
419 * important upon reboot.
422 ret = krb5_storage_fsync(dump);
423 if (ret == -1) {
424 ret = errno;
425 krb5_warn(context, ret, "syncing iprop dumpfile");
426 return ret;
429 ret = krb5_storage_seek(dump, 0, SEEK_SET);
430 if (ret == -1) {
431 ret = errno;
432 krb5_warn(context, ret, "krb5_storage_seek(dump, 0, SEEK_SET)");
433 return ret;
436 /* Write current version at the front making the dump valid */
438 ret = krb5_store_uint32(dump, current_version);
439 if (ret) {
440 krb5_warn(context, ret, "writing version to dumpfile");
441 return ret;
445 * We don't need to fsync(2) after the real version is written as
446 * it is not a disaster if it doesn't make it to disk if we crash.
447 * After all, we'll just create a new dumpfile.
450 krb5_warnx(context, "wrote new dumpfile (version %u)", current_version);
452 return 0;
455 static int
456 send_complete (krb5_context context, slave *s, const char *database,
457 uint32_t current_version, uint32_t oldest_version)
459 krb5_error_code ret;
460 krb5_storage *dump = NULL;
461 uint32_t vno = 0;
462 krb5_data data;
463 int fd = -1;
464 char *dfn;
466 ret = asprintf(&dfn, "%s/ipropd.dumpfile", hdb_db_dir(context));
467 if (ret == -1 || !dfn) {
468 krb5_warn(context, ENOMEM, "Cannot allocate memory");
469 return ENOMEM;
472 fd = open(dfn, O_CREAT|O_RDWR, 0600);
473 if (fd == -1) {
474 ret = errno;
475 krb5_warn(context, ret, "Cannot open/create iprop dumpfile %s", dfn);
476 free(dfn);
477 return ret;
479 free(dfn);
481 dump = krb5_storage_from_fd(fd);
482 if (!dump) {
483 ret = errno;
484 krb5_warn(context, ret, "krb5_storage_from_fd");
485 goto done;
488 for (;;) {
489 ret = flock(fd, LOCK_SH);
490 if (ret == -1) {
491 ret = errno;
492 krb5_warn(context, ret, "flock(fd, LOCK_SH)");
493 goto done;
496 krb5_storage_seek(dump, 0, SEEK_SET);
497 if (ret == -1) {
498 ret = errno;
499 krb5_warn(context, ret, "krb5_storage_seek(dump, 0, SEEK_SET)");
500 goto done;
503 vno = 0;
504 ret = krb5_ret_uint32(dump, &vno);
505 if (ret && ret != HEIM_ERR_EOF) {
506 krb5_warn(context, ret, "krb5_ret_uint32(dump, &vno)");
507 goto done;
511 * If the current dump has an appropriate version, then we can
512 * break out of the loop and send the file below.
515 if (vno >= oldest_version)
516 break;
519 * Otherwise, we may need to write a new dump file. We
520 * obtain an exclusive lock on the fd. Because this is
521 * not guaranteed to be an upgrade of our existing shared
522 * lock, someone else may have written a new dumpfile while
523 * we were waiting and so we must first check the vno of
524 * the dump to see if that happened. If it did, we need
525 * to go back to the top of the loop so that we can downgrade
526 * our lock to a shared one.
529 flock(fd, LOCK_EX);
530 if (ret == -1) {
531 ret = errno;
532 krb5_warn(context, ret, "flock(fd, LOCK_EX)");
533 goto done;
536 krb5_storage_seek(dump, 0, SEEK_SET);
537 if (ret == -1) {
538 ret = errno;
539 krb5_warn(context, ret, "krb5_storage_seek(dump, 0, SEEK_SET)");
540 goto done;
543 vno = 0;
544 ret = krb5_ret_uint32(dump, &vno);
545 if (ret && ret != HEIM_ERR_EOF) {
546 krb5_warn(context, ret, "krb5_ret_uint32(dump, &vno)");
547 goto done;
550 /* check if someone wrote a better version for us */
551 if (vno >= oldest_version)
552 continue;
554 /* Now, we know that we must write a new dump file. */
556 ret = write_dump(context, dump, database, current_version);
557 if (ret)
558 goto done;
561 * And we must continue to the top of the loop so that we can
562 * downgrade to a shared lock.
567 * Leaving the above loop, dump should have a ptr right after the initial
568 * 4 byte DB version number and we should have a shared lock on the file
569 * (which we may have just created), so we are reading to simply blast
570 * the data down the wire.
573 for (;;) {
574 ret = krb5_ret_data(dump, &data);
575 if (ret == HEIM_ERR_EOF) {
576 ret = 0; /* EOF is not an error, it's success */
577 goto done;
580 if (ret) {
581 krb5_warn(context, ret, "krb5_ret_data(dump, &data)");
582 slave_dead(context, s);
583 goto done;
586 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
587 krb5_data_free(&data);
589 if (ret) {
590 krb5_warn (context, ret, "krb5_write_priv_message");
591 slave_dead(context, s);
592 goto done;
596 done:
597 if (!ret) {
598 s->version = vno;
599 slave_seen(s);
601 if (fd != -1)
602 close(fd);
603 if (dump)
604 krb5_storage_free(dump);
605 return ret;
608 static int
609 send_are_you_there (krb5_context context, slave *s)
611 krb5_storage *sp;
612 krb5_data data;
613 char buf[4];
614 int ret;
616 if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
617 return 0;
619 krb5_warnx(context, "slave %s missing, sending AYT", s->name);
621 s->flags |= SLAVE_F_AYT;
623 data.data = buf;
624 data.length = 4;
626 sp = krb5_storage_from_mem (buf, 4);
627 if (sp == NULL) {
628 krb5_warnx (context, "are_you_there: krb5_data_alloc");
629 slave_dead(context, s);
630 return 1;
632 krb5_store_int32 (sp, ARE_YOU_THERE);
633 krb5_storage_free (sp);
635 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
637 if (ret) {
638 krb5_warn (context, ret, "are_you_there: krb5_write_priv_message");
639 slave_dead(context, s);
640 return 1;
643 return 0;
646 static int
647 send_diffs (krb5_context context, slave *s, int log_fd,
648 const char *database, uint32_t current_version)
650 krb5_storage *sp;
651 uint32_t ver;
652 time_t timestamp;
653 enum kadm_ops op;
654 uint32_t len;
655 off_t right, left;
656 krb5_data data;
657 int ret = 0;
659 if (s->version == current_version) {
660 char buf[4];
662 sp = krb5_storage_from_mem(buf, 4);
663 if (sp == NULL)
664 krb5_errx(context, 1, "krb5_storage_from_mem");
665 krb5_store_int32(sp, YOU_HAVE_LAST_VERSION);
666 krb5_storage_free(sp);
667 data.data = buf;
668 data.length = 4;
669 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
670 krb5_warnx(context, "slave %s in sync already at version %ld",
671 s->name, (long)s->version);
672 return ret;
675 if (s->flags & SLAVE_F_DEAD)
676 return 0;
678 flock(log_fd, LOCK_SH);
679 sp = kadm5_log_goto_end (log_fd);
680 flock(log_fd, LOCK_UN);
681 right = krb5_storage_seek(sp, 0, SEEK_CUR);
682 for (;;) {
683 ret = kadm5_log_previous (context, sp, &ver, &timestamp, &op, &len);
684 if (ret)
685 krb5_err(context, 1, ret,
686 "send_diffs: failed to find previous entry");
687 left = krb5_storage_seek(sp, -16, SEEK_CUR);
688 if (ver == s->version)
689 return 0;
690 if (ver == s->version + 1)
691 break;
692 if (left == 0) {
693 krb5_storage_free(sp);
694 krb5_warnx(context,
695 "slave %s (version %lu) out of sync with master "
696 "(first version in log %lu), sending complete database",
697 s->name, (unsigned long)s->version, (unsigned long)ver);
698 return send_complete (context, s, database, current_version, ver);
702 krb5_warnx(context,
703 "syncing slave %s from version %lu to version %lu",
704 s->name, (unsigned long)s->version,
705 (unsigned long)current_version);
707 ret = krb5_data_alloc (&data, right - left + 4);
708 if (ret) {
709 krb5_storage_free(sp);
710 krb5_warn (context, ret, "send_diffs: krb5_data_alloc");
711 slave_dead(context, s);
712 return 1;
714 krb5_storage_read (sp, (char *)data.data + 4, data.length - 4);
715 krb5_storage_free(sp);
717 sp = krb5_storage_from_data (&data);
718 if (sp == NULL) {
719 krb5_warnx (context, "send_diffs: krb5_storage_from_data");
720 slave_dead(context, s);
721 return 1;
723 krb5_store_int32 (sp, FOR_YOU);
724 krb5_storage_free(sp);
726 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
727 krb5_data_free(&data);
729 if (ret) {
730 krb5_warn (context, ret, "send_diffs: krb5_write_priv_message");
731 slave_dead(context, s);
732 return 1;
734 slave_seen(s);
736 s->version = current_version;
738 return 0;
741 static int
742 process_msg (krb5_context context, slave *s, int log_fd,
743 const char *database, uint32_t current_version)
745 int ret = 0;
746 krb5_data out;
747 krb5_storage *sp;
748 int32_t tmp;
750 ret = krb5_read_priv_message(context, s->ac, &s->fd, &out);
751 if(ret) {
752 krb5_warn (context, ret, "error reading message from %s", s->name);
753 return 1;
756 sp = krb5_storage_from_mem (out.data, out.length);
757 if (sp == NULL) {
758 krb5_warnx (context, "process_msg: no memory");
759 krb5_data_free (&out);
760 return 1;
762 if (krb5_ret_int32 (sp, &tmp) != 0) {
763 krb5_warnx (context, "process_msg: client send too short command");
764 krb5_data_free (&out);
765 return 1;
767 switch (tmp) {
768 case I_HAVE :
769 ret = krb5_ret_int32 (sp, &tmp);
770 if (ret != 0) {
771 krb5_warnx (context, "process_msg: client send too I_HAVE data");
772 break;
774 /* new started slave that have old log */
775 if (s->version == 0 && tmp != 0) {
776 if (current_version < (uint32_t)tmp) {
777 krb5_warnx (context, "Slave %s (version %lu) have later version "
778 "the master (version %lu) OUT OF SYNC",
779 s->name, (unsigned long)tmp,
780 (unsigned long)current_version);
782 s->version = tmp;
784 if ((uint32_t)tmp < s->version) {
785 krb5_warnx (context, "Slave claims to not have "
786 "version we already sent to it");
787 } else {
788 ret = send_diffs (context, s, log_fd, database, current_version);
790 break;
791 case I_AM_HERE :
792 break;
793 case ARE_YOU_THERE:
794 case FOR_YOU :
795 default :
796 krb5_warnx (context, "Ignoring command %d", tmp);
797 break;
800 krb5_data_free (&out);
801 krb5_storage_free (sp);
803 slave_seen(s);
805 return ret;
808 #define SLAVE_NAME "Name"
809 #define SLAVE_ADDRESS "Address"
810 #define SLAVE_VERSION "Version"
811 #define SLAVE_STATUS "Status"
812 #define SLAVE_SEEN "Last Seen"
814 static FILE *
815 open_stats(krb5_context context)
817 char *statfile = NULL;
818 const char *fn = NULL;
819 FILE *out = NULL;
822 * krb5_config_get_string_default() returs default value as-is,
823 * delay free() of "statfile" until we're done with "fn".
825 if (slave_stats_file)
826 fn = slave_stats_file;
827 else if (asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context)) != -1
828 && statfile != NULL)
829 fn = krb5_config_get_string_default(context,
830 NULL,
831 statfile,
832 "kdc",
833 "iprop-stats",
834 NULL);
835 if (fn != NULL)
836 out = fopen(fn, "w");
837 if (statfile != NULL)
838 free(statfile);
839 return out;
842 static void
843 write_master_down(krb5_context context)
845 char str[100];
846 time_t t = time(NULL);
847 FILE *fp;
849 fp = open_stats(context);
850 if (fp == NULL)
851 return;
852 krb5_format_time(context, t, str, sizeof(str), TRUE);
853 fprintf(fp, "master down at %s\n", str);
855 fclose(fp);
858 static void
859 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
861 char str[100];
862 rtbl_t tbl;
863 time_t t = time(NULL);
864 FILE *fp;
866 fp = open_stats(context);
867 if (fp == NULL)
868 return;
870 krb5_format_time(context, t, str, sizeof(str), TRUE);
871 fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
873 fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
875 tbl = rtbl_create();
876 if (tbl == NULL) {
877 fclose(fp);
878 return;
881 rtbl_add_column(tbl, SLAVE_NAME, 0);
882 rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
883 rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
884 rtbl_add_column(tbl, SLAVE_STATUS, 0);
885 rtbl_add_column(tbl, SLAVE_SEEN, 0);
887 rtbl_set_prefix(tbl, " ");
888 rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
890 while (slaves) {
891 krb5_address addr;
892 krb5_error_code ret;
893 rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
894 ret = krb5_sockaddr2address (context,
895 (struct sockaddr*)&slaves->addr, &addr);
896 if(ret == 0) {
897 krb5_print_address(&addr, str, sizeof(str), NULL);
898 krb5_free_address(context, &addr);
899 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
900 } else
901 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
903 snprintf(str, sizeof(str), "%u", (unsigned)slaves->version);
904 rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
906 if (slaves->flags & SLAVE_F_DEAD)
907 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
908 else
909 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
911 ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
912 rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
914 slaves = slaves->next;
917 rtbl_format(tbl, fp);
918 rtbl_destroy(tbl);
920 fclose(fp);
924 static char sHDB[] = "HDBGET:";
925 static char *realm;
926 static int version_flag;
927 static int help_flag;
928 static char *keytab_str = sHDB;
929 static char *database;
930 static char *config_file;
931 static char *port_str;
932 #ifdef SUPPORT_DETACH
933 static int detach_from_console = 0;
934 #endif
936 static struct getargs args[] = {
937 { "config-file", 'c', arg_string, &config_file, NULL, NULL },
938 { "realm", 'r', arg_string, &realm, NULL, NULL },
939 { "keytab", 'k', arg_string, &keytab_str,
940 "keytab to get authentication from", "kspec" },
941 { "database", 'd', arg_string, &database, "database", "file"},
942 { "slave-stats-file", 0, arg_string, rk_UNCONST(&slave_stats_file),
943 "file for slave status information", "file"},
944 { "time-missing", 0, arg_string, rk_UNCONST(&slave_time_missing),
945 "time before slave is polled for presence", "time"},
946 { "time-gone", 0, arg_string, rk_UNCONST(&slave_time_gone),
947 "time of inactivity after which a slave is considered gone", "time"},
948 { "port", 0, arg_string, &port_str,
949 "port ipropd will listen to", "port"},
950 #ifdef SUPPORT_DETACH
951 { "detach", 0, arg_flag, &detach_from_console,
952 "detach from console", NULL },
953 #endif
954 { "hostname", 0, arg_string, rk_UNCONST(&master_hostname),
955 "hostname of master (if not same as hostname)", "hostname" },
956 { "version", 0, arg_flag, &version_flag, NULL, NULL },
957 { "help", 0, arg_flag, &help_flag, NULL, NULL }
959 static int num_args = sizeof(args) / sizeof(args[0]);
962 main(int argc, char **argv)
964 krb5_error_code ret;
965 krb5_context context;
966 void *kadm_handle;
967 kadm5_server_context *server_context;
968 kadm5_config_params conf;
969 krb5_socket_t signal_fd, listen_fd;
970 int log_fd;
971 slave *slaves = NULL;
972 uint32_t current_version = 0, old_version = 0;
973 krb5_keytab keytab;
974 char **files;
975 int aret;
977 (void) krb5_program_setup(&context, argc, argv, args, num_args, NULL);
979 if(help_flag)
980 krb5_std_usage(0, args, num_args);
981 if(version_flag) {
982 print_version(NULL);
983 exit(0);
986 setup_signal();
988 if (config_file == NULL) {
989 aret = asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
990 if (aret == -1 || config_file == NULL)
991 errx(1, "out of memory");
994 ret = krb5_prepend_config_files_default(config_file, &files);
995 if (ret)
996 krb5_err(context, 1, ret, "getting configuration files");
998 ret = krb5_set_config_files(context, files);
999 krb5_free_config_files(files);
1000 if (ret)
1001 krb5_err(context, 1, ret, "reading configuration files");
1003 time_before_gone = parse_time (slave_time_gone, "s");
1004 if (time_before_gone < 0)
1005 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
1006 time_before_missing = parse_time (slave_time_missing, "s");
1007 if (time_before_missing < 0)
1008 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
1010 #ifdef SUPPORT_DETACH
1011 if (detach_from_console) {
1012 aret = daemon(0, 0);
1013 if (aret == -1) {
1014 /* not much to do if detaching fails... */
1015 krb5_err(context, 1, aret, "failed to daemon(3)ise");
1018 #endif
1019 pidfile (NULL);
1020 krb5_openlog (context, "ipropd-master", &log_facility);
1021 krb5_set_warn_dest(context, log_facility);
1023 ret = krb5_kt_register(context, &hdb_get_kt_ops);
1024 if(ret)
1025 krb5_err(context, 1, ret, "krb5_kt_register");
1027 ret = krb5_kt_resolve(context, keytab_str, &keytab);
1028 if(ret)
1029 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
1031 memset(&conf, 0, sizeof(conf));
1032 if(realm) {
1033 conf.mask |= KADM5_CONFIG_REALM;
1034 conf.realm = realm;
1036 ret = kadm5_init_with_skey_ctx (context,
1037 KADM5_ADMIN_SERVICE,
1038 NULL,
1039 KADM5_ADMIN_SERVICE,
1040 &conf, 0, 0,
1041 &kadm_handle);
1042 if (ret)
1043 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
1045 server_context = (kadm5_server_context *)kadm_handle;
1047 log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
1048 if (log_fd < 0)
1049 krb5_err (context, 1, errno, "open %s",
1050 server_context->log_context.log_file);
1052 signal_fd = make_signal_socket (context);
1053 listen_fd = make_listen_socket (context, port_str);
1055 flock(log_fd, LOCK_SH);
1056 kadm5_log_get_version_fd (log_fd, &current_version);
1057 flock(log_fd, LOCK_UN);
1059 krb5_warnx(context, "ipropd-master started at version: %lu",
1060 (unsigned long)current_version);
1062 while(exit_flag == 0){
1063 slave *p;
1064 fd_set readset;
1065 int max_fd = 0;
1066 struct timeval to = {30, 0};
1067 uint32_t vers;
1069 #ifndef NO_LIMIT_FD_SETSIZE
1070 if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE)
1071 krb5_errx (context, 1, "fd too large");
1072 #endif
1074 FD_ZERO(&readset);
1075 FD_SET(signal_fd, &readset);
1076 max_fd = max(max_fd, signal_fd);
1077 FD_SET(listen_fd, &readset);
1078 max_fd = max(max_fd, listen_fd);
1080 for (p = slaves; p != NULL; p = p->next) {
1081 if (p->flags & SLAVE_F_DEAD)
1082 continue;
1083 FD_SET(p->fd, &readset);
1084 max_fd = max(max_fd, p->fd);
1087 ret = select (max_fd + 1,
1088 &readset, NULL, NULL, &to);
1089 if (ret < 0) {
1090 if (errno == EINTR)
1091 continue;
1092 else
1093 krb5_err (context, 1, errno, "select");
1096 if (ret == 0) {
1097 old_version = current_version;
1098 flock(log_fd, LOCK_SH);
1099 kadm5_log_get_version_fd (log_fd, &current_version);
1100 flock(log_fd, LOCK_UN);
1102 if (current_version > old_version) {
1103 krb5_warnx(context,
1104 "Missed a signal, updating slaves %lu to %lu",
1105 (unsigned long)old_version,
1106 (unsigned long)current_version);
1107 for (p = slaves; p != NULL; p = p->next) {
1108 if (p->flags & SLAVE_F_DEAD)
1109 continue;
1110 send_diffs (context, p, log_fd, database, current_version);
1115 if (ret && FD_ISSET(signal_fd, &readset)) {
1116 #ifndef NO_UNIX_SOCKETS
1117 struct sockaddr_un peer_addr;
1118 #else
1119 struct sockaddr_storage peer_addr;
1120 #endif
1121 socklen_t peer_len = sizeof(peer_addr);
1123 if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
1124 (struct sockaddr *)&peer_addr, &peer_len) < 0) {
1125 krb5_warn (context, errno, "recvfrom");
1126 continue;
1128 --ret;
1129 assert(ret >= 0);
1130 old_version = current_version;
1131 flock(log_fd, LOCK_SH);
1132 kadm5_log_get_version_fd (log_fd, &current_version);
1133 flock(log_fd, LOCK_UN);
1134 if (current_version > old_version) {
1135 krb5_warnx(context,
1136 "Got a signal, updating slaves %lu to %lu",
1137 (unsigned long)old_version,
1138 (unsigned long)current_version);
1139 for (p = slaves; p != NULL; p = p->next) {
1140 if (p->flags & SLAVE_F_DEAD)
1141 continue;
1142 send_diffs (context, p, log_fd, database, current_version);
1144 } else {
1145 krb5_warnx(context,
1146 "Got a signal, but no update in log version %lu",
1147 (unsigned long)current_version);
1151 for(p = slaves; p != NULL; p = p->next) {
1152 if (p->flags & SLAVE_F_DEAD)
1153 continue;
1154 if (ret && FD_ISSET(p->fd, &readset)) {
1155 --ret;
1156 assert(ret >= 0);
1157 if(process_msg (context, p, log_fd, database, current_version))
1158 slave_dead(context, p);
1159 } else if (slave_gone_p (p))
1160 slave_dead(context, p);
1161 else if (slave_missing_p (p))
1162 send_are_you_there (context, p);
1165 if (ret && FD_ISSET(listen_fd, &readset)) {
1166 add_slave (context, keytab, &slaves, listen_fd);
1167 --ret;
1168 assert(ret >= 0);
1170 write_stats(context, slaves, current_version);
1173 if(exit_flag == SIGINT || exit_flag == SIGTERM)
1174 krb5_warnx(context, "%s terminated", getprogname());
1175 #ifdef SIGXCPU
1176 else if(exit_flag == SIGXCPU)
1177 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
1178 #endif
1179 else
1180 krb5_warnx(context, "%s unexpected exit reason: %ld",
1181 getprogname(), (long)exit_flag);
1183 write_master_down(context);
1185 return 0;