Fix check-iprop races
[heimdal.git] / lib / kadm5 / ipropd_slave.c
blob463a7563177652f40b81b4ccd821b1a21341a189
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"
36 RCSID("$Id$");
38 static const char *config_name = "ipropd-slave";
40 static int verbose;
42 static krb5_log_facility *log_facility;
43 static char five_min[] = "5 min";
44 static char *server_time_lost = five_min;
45 static int time_before_lost;
46 const char *slave_str = NULL;
48 static int
49 connect_to_master (krb5_context context, const char *master,
50 const char *port_str)
52 char port[NI_MAXSERV];
53 struct addrinfo *ai, *a;
54 struct addrinfo hints;
55 int error;
56 int one = 1;
57 int s = -1;
59 memset(&hints, 0, sizeof(hints));
60 hints.ai_socktype = SOCK_STREAM;
62 if (port_str == NULL) {
63 snprintf(port, sizeof(port), "%u", IPROP_PORT);
64 port_str = port;
67 error = getaddrinfo(master, port_str, &hints, &ai);
68 if (error) {
69 krb5_warnx(context, "Failed to get address of to %s: %s",
70 master, gai_strerror(error));
71 return -1;
74 for (a = ai; a != NULL; a = a->ai_next) {
75 char node[NI_MAXHOST];
76 error = getnameinfo(a->ai_addr, a->ai_addrlen,
77 node, sizeof(node), NULL, 0, NI_NUMERICHOST);
78 if (error)
79 strlcpy(node, "[unknown-addr]", sizeof(node));
81 s = socket(a->ai_family, a->ai_socktype, a->ai_protocol);
82 if (s < 0)
83 continue;
84 if (connect(s, a->ai_addr, a->ai_addrlen) < 0) {
85 krb5_warn(context, errno, "connection failed to %s[%s]",
86 master, node);
87 close(s);
88 continue;
90 krb5_warnx(context, "connection successful "
91 "to master: %s[%s]", master, node);
92 break;
94 freeaddrinfo(ai);
96 if (a == NULL)
97 return -1;
99 if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &one, sizeof(one)) < 0)
100 krb5_warn(context, errno, "setsockopt(SO_KEEPALIVE) failed");
102 return s;
105 static void
106 get_creds(krb5_context context, const char *keytab_str,
107 krb5_ccache *cache, const char *serverhost)
109 krb5_keytab keytab;
110 krb5_principal client;
111 krb5_error_code ret;
112 krb5_get_init_creds_opt *init_opts;
113 krb5_creds creds;
114 char *server;
115 char keytab_buf[256];
116 int aret;
118 if (keytab_str == NULL) {
119 ret = krb5_kt_default_name (context, keytab_buf, sizeof(keytab_buf));
120 if (ret)
121 krb5_err (context, 1, ret, "krb5_kt_default_name");
122 keytab_str = keytab_buf;
125 ret = krb5_kt_resolve(context, keytab_str, &keytab);
126 if(ret)
127 krb5_err(context, 1, ret, "%s", keytab_str);
130 ret = krb5_sname_to_principal (context, slave_str, IPROP_NAME,
131 KRB5_NT_SRV_HST, &client);
132 if (ret) krb5_err(context, 1, ret, "krb5_sname_to_principal");
134 ret = krb5_get_init_creds_opt_alloc(context, &init_opts);
135 if (ret) krb5_err(context, 1, ret, "krb5_get_init_creds_opt_alloc");
137 aret = asprintf (&server, "%s/%s", IPROP_NAME, serverhost);
138 if (aret == -1 || server == NULL)
139 krb5_errx (context, 1, "malloc: no memory");
141 ret = krb5_get_init_creds_keytab(context, &creds, client, keytab,
142 0, server, init_opts);
143 free (server);
144 krb5_get_init_creds_opt_free(context, init_opts);
145 if(ret) krb5_err(context, 1, ret, "krb5_get_init_creds");
147 ret = krb5_kt_close(context, keytab);
148 if(ret) krb5_err(context, 1, ret, "krb5_kt_close");
150 ret = krb5_cc_new_unique(context, krb5_cc_type_memory, NULL, cache);
151 if(ret) krb5_err(context, 1, ret, "krb5_cc_new_unique");
153 ret = krb5_cc_initialize(context, *cache, creds.client);
154 if(ret) krb5_err(context, 1, ret, "krb5_cc_initialize");
156 ret = krb5_cc_store_cred(context, *cache, &creds);
157 if(ret) krb5_err(context, 1, ret, "krb5_cc_store_cred");
159 krb5_free_cred_contents(context, &creds);
160 krb5_free_principal(context, client);
163 static krb5_error_code
164 ihave(krb5_context context, krb5_auth_context auth_context,
165 int fd, uint32_t version)
167 int ret;
168 u_char buf[8];
169 krb5_storage *sp;
170 krb5_data data;
172 sp = krb5_storage_from_mem(buf, 8);
173 ret = krb5_store_uint32(sp, I_HAVE);
174 if (ret == 0)
175 ret = krb5_store_uint32(sp, version);
176 krb5_storage_free(sp);
177 data.length = 8;
178 data.data = buf;
180 if (ret == 0) {
181 if (verbose)
182 krb5_warnx(context, "telling master we are at %u", version);
184 ret = krb5_write_priv_message(context, auth_context, &fd, &data);
185 if (ret)
186 krb5_warn(context, ret, "krb5_write_message");
188 return ret;
191 #ifndef EDQUOT
192 /* There's no EDQUOT on WIN32, for example */
193 #define EDQUOT ENOSPC
194 #endif
196 static int
197 append_to_log_file(krb5_context context,
198 kadm5_server_context *server_context,
199 krb5_storage *sp, off_t start, ssize_t slen)
201 size_t len;
202 ssize_t sret;
203 off_t log_off;
204 int ret, ret2;
205 void *buf;
207 if (verbose)
208 krb5_warnx(context, "appending diffs to log");
210 if (slen == 0)
211 return 0;
212 if (slen < 0)
213 return EINVAL;
214 len = slen;
215 if (len != slen)
216 return EOVERFLOW;
218 buf = malloc(len);
219 if (buf == NULL && len != 0) {
220 krb5_warn(context, errno, "malloc: no memory");
221 return ENOMEM;
224 if (krb5_storage_seek(sp, start, SEEK_SET) != start) {
225 krb5_errx(context, IPROPD_RESTART,
226 "krb5_storage_seek() failed"); /* can't happen */
228 sret = krb5_storage_read(sp, buf, len);
229 if (sret < 0)
230 return errno;
231 if (len != (size_t)sret) {
232 /* Can't happen */
233 krb5_errx(context, IPROPD_RESTART,
234 "short krb5_storage_read() from memory buffer");
236 log_off = lseek(server_context->log_context.log_fd, 0, SEEK_CUR);
237 if (log_off == -1)
238 return errno;
241 * Use net_write() so we get an errno if less that len bytes were
242 * written.
244 sret = net_write(server_context->log_context.log_fd, buf, len);
245 free(buf);
246 if (sret != slen)
247 ret = errno;
248 else
249 ret = fsync(server_context->log_context.log_fd);
250 if (ret == 0)
251 return 0;
254 * Attempt to recover from this. First, truncate the log file
255 * and reset the fd offset. Failure to do this -> unlink the
256 * log file and re-create it. Since we're the slave, we ought to be
257 * able to recover from the log being unlinked...
259 if (ftruncate(server_context->log_context.log_fd, log_off) == -1 ||
260 lseek(server_context->log_context.log_fd, log_off, SEEK_SET) == -1) {
261 (void) kadm5_log_end(server_context);
262 if (unlink(server_context->log_context.log_file) == -1) {
263 krb5_err(context, IPROPD_FATAL, errno,
264 "Failed to recover from failure to write log "
265 "entries from master to disk");
267 ret2 = kadm5_log_init(server_context);
268 if (ret2) {
269 krb5_err(context, IPROPD_RESTART_SLOW, ret2,
270 "Failed to initialize log to recover from "
271 "failure to write log entries from master to disk");
274 if (ret == ENOSPC || ret == EDQUOT || ret == EFBIG) {
275 /* Unlink the file in these cases. */
276 krb5_warn(context, IPROPD_RESTART_SLOW,
277 "Failed to write log entries from master to disk");
278 (void) kadm5_log_end(server_context);
279 if (unlink(server_context->log_context.log_file) == -1) {
280 krb5_err(context, IPROPD_FATAL, errno,
281 "Failed to recover from failure to write log "
282 "entries from master to disk");
284 ret2 = kadm5_log_init(server_context);
285 if (ret2) {
286 krb5_err(context, IPROPD_RESTART_SLOW, ret2,
287 "Failed to initialize log to recover from "
288 "failure to write log entries from master to disk");
290 return ret;
293 * All other errors we treat as fatal here. This includes, for
294 * example, EIO and EPIPE (sorry, can't log to pipes nor sockets).
296 krb5_err(context, IPROPD_FATAL, ret,
297 "Failed to write log entries from master to disk");
300 static int
301 receive_loop (krb5_context context,
302 krb5_storage *sp,
303 kadm5_server_context *server_context)
305 int ret;
306 off_t left, right, off;
307 uint32_t len, vers;
309 if (verbose)
310 krb5_warnx(context, "receiving diffs");
313 * Seek to the first entry in the message from the master that is
314 * past the current version of the local database.
316 do {
317 uint32_t timestamp;
318 uint32_t op;
321 * TODO We could do more to validate the entries from the master
322 * here. And we could use/reuse more kadm5_log_*() code here.
324 * Alternatively we should trust that the master sent us exactly
325 * what we needed and just write this to the log file and let
326 * kadm5_log_recover() do the rest.
328 if (krb5_ret_uint32(sp, &vers) != 0 ||
329 krb5_ret_uint32(sp, &timestamp) != 0 ||
330 krb5_ret_uint32(sp, &op) != 0 ||
331 krb5_ret_uint32(sp, &len) != 0) {
334 * This shouldn't happen. Reconnecting probably won't help
335 * if it does happen, but by reconnecting we get a chance to
336 * connect to a new master if a new one is configured.
338 krb5_warnx(context, "iprop entries from master were truncated");
339 return EINVAL;
341 if (vers > server_context->log_context.version) {
342 break;
344 off = krb5_storage_seek(sp, 0, SEEK_CUR);
345 if (krb5_storage_seek(sp, len + 8, SEEK_CUR) != off + len + 8) {
346 krb5_warnx(context, "iprop entries from master were truncated");
347 return 0;
349 if (verbose) {
350 krb5_warnx(context, "diff contains old log record version "
351 "%u %lld %u length %u",
352 vers, (long long)timestamp, op, len);
354 } while(vers <= server_context->log_context.version);
357 * Read the remaining entries into memory...
359 /* SEEK_CUR is a header into the first entry we care about */
360 left = krb5_storage_seek(sp, -16, SEEK_CUR);
361 right = krb5_storage_seek(sp, 0, SEEK_END);
362 if (right - left < 24 + len) {
363 krb5_warnx(context, "iprop entries from master were truncated");
364 return EINVAL;
368 * ...and then write them out to the on-disk log.
371 ret = append_to_log_file(context, server_context, sp, left, right - left);
372 if (ret)
373 return ret;
376 * Replay the new entries.
378 if (verbose)
379 krb5_warnx(context, "replaying entries from master");
380 ret = kadm5_log_recover(server_context, kadm_recover_replay);
381 if (ret) {
382 krb5_warn(context, ret, "replay failed");
383 return ret;
386 ret = kadm5_log_get_version(server_context, &vers);
387 if (ret) {
388 krb5_warn(context, ret,
389 "could not get log version after applying diffs!");
390 return ret;
392 if (verbose)
393 krb5_warnx(context, "slave at version %u", vers);
395 if (vers != server_context->log_context.version) {
396 krb5_warnx(context, "slave's log_context version (%u) is "
397 "inconsistent with log's version (%u)",
398 server_context->log_context.version, vers);
401 return 0;
404 static int
405 receive(krb5_context context,
406 krb5_storage *sp,
407 kadm5_server_context *server_context)
409 krb5_error_code ret, ret2;
411 ret = server_context->db->hdb_open(context,
412 server_context->db,
413 O_RDWR | O_CREAT, 0600);
414 if (ret)
415 krb5_err(context, IPROPD_RESTART_SLOW, ret, "db->open");
417 ret2 = receive_loop(context, sp, server_context);
418 if (ret2)
419 krb5_warn(context, ret, "receive from ipropd-master had errors");
421 ret = server_context->db->hdb_close(context, server_context->db);
422 if (ret)
423 krb5_err(context, IPROPD_RESTART_SLOW, ret, "db->close");
425 return ret2;
428 static void
429 send_im_here(krb5_context context, int fd,
430 krb5_auth_context auth_context)
432 krb5_storage *sp;
433 krb5_data data;
434 krb5_error_code ret;
436 ret = krb5_data_alloc(&data, 4);
437 if (ret)
438 krb5_err(context, IPROPD_RESTART, ret, "send_im_here");
440 sp = krb5_storage_from_data (&data);
441 if (sp == NULL)
442 krb5_errx(context, IPROPD_RESTART, "krb5_storage_from_data");
443 ret = krb5_store_uint32(sp, I_AM_HERE);
444 krb5_storage_free(sp);
446 if (ret == 0) {
447 ret = krb5_write_priv_message(context, auth_context, &fd, &data);
448 krb5_data_free(&data);
450 if (ret)
451 krb5_err(context, IPROPD_RESTART, ret, "krb5_write_priv_message");
453 if (verbose)
454 krb5_warnx(context, "pinged master");
457 return;
460 static void
461 reinit_log(krb5_context context,
462 kadm5_server_context *server_context,
463 uint32_t vno)
465 krb5_error_code ret;
467 if (verbose)
468 krb5_warnx(context, "truncating log on slave");
470 ret = kadm5_log_reinit(server_context, vno);
471 if (ret)
472 krb5_err(context, IPROPD_RESTART_SLOW, ret, "kadm5_log_reinit");
476 static krb5_error_code
477 receive_everything(krb5_context context, int fd,
478 kadm5_server_context *server_context,
479 krb5_auth_context auth_context)
481 int ret;
482 krb5_data data;
483 uint32_t vno = 0;
484 uint32_t opcode;
485 krb5_storage *sp;
487 char *dbname;
488 HDB *mydb;
490 krb5_warnx(context, "receive complete database");
492 ret = asprintf(&dbname, "%s-NEW", server_context->db->hdb_name);
493 if (ret == -1)
494 krb5_err(context, IPROPD_RESTART, ENOMEM, "asprintf");
495 ret = hdb_create(context, &mydb, dbname);
496 if(ret)
497 krb5_err(context, IPROPD_RESTART, ret, "hdb_create");
498 free(dbname);
500 ret = hdb_set_master_keyfile(context,
501 mydb, server_context->config.stash_file);
502 if(ret)
503 krb5_err(context, IPROPD_RESTART, ret, "hdb_set_master_keyfile");
505 /* I really want to use O_EXCL here, but given that I can't easily clean
506 up on error, I won't */
507 ret = mydb->hdb_open(context, mydb, O_RDWR | O_CREAT | O_TRUNC, 0600);
508 if (ret)
509 krb5_err(context, IPROPD_RESTART, ret, "db->open");
511 sp = NULL;
512 krb5_data_zero(&data);
513 do {
514 ret = krb5_read_priv_message(context, auth_context, &fd, &data);
516 if (ret) {
517 krb5_warn(context, ret, "krb5_read_priv_message");
518 goto cleanup;
521 sp = krb5_storage_from_data(&data);
522 if (sp == NULL)
523 krb5_errx(context, IPROPD_RESTART, "krb5_storage_from_data");
524 krb5_ret_uint32(sp, &opcode);
525 if (opcode == ONE_PRINC) {
526 krb5_data fake_data;
527 hdb_entry_ex entry;
529 krb5_storage_free(sp);
531 fake_data.data = (char *)data.data + 4;
532 fake_data.length = data.length - 4;
534 memset(&entry, 0, sizeof(entry));
536 ret = hdb_value2entry(context, &fake_data, &entry.entry);
537 if (ret)
538 krb5_err(context, IPROPD_RESTART, ret, "hdb_value2entry");
539 ret = mydb->hdb_store(server_context->context,
540 mydb,
541 0, &entry);
542 if (ret)
543 krb5_err(context, IPROPD_RESTART_SLOW, ret, "hdb_store");
545 hdb_free_entry(context, &entry);
546 krb5_data_free(&data);
547 } else if (opcode == NOW_YOU_HAVE)
549 else
550 krb5_errx(context, 1, "strange opcode %d", opcode);
551 } while (opcode == ONE_PRINC);
553 if (opcode != NOW_YOU_HAVE)
554 krb5_errx(context, IPROPD_RESTART_SLOW,
555 "receive_everything: strange %d", opcode);
557 krb5_ret_uint32(sp, &vno);
558 krb5_storage_free(sp);
560 reinit_log(context, server_context, vno);
562 ret = mydb->hdb_close(context, mydb);
563 if (ret)
564 krb5_err(context, IPROPD_RESTART_SLOW, ret, "db->close");
566 ret = mydb->hdb_rename(context, mydb, server_context->db->hdb_name);
567 if (ret)
568 krb5_err(context, IPROPD_RESTART_SLOW, ret, "db->rename");
571 return 0;
573 cleanup:
574 krb5_data_free(&data);
576 if (ret)
577 krb5_err(context, IPROPD_RESTART_SLOW, ret, "db->close");
579 ret = mydb->hdb_destroy(context, mydb);
580 if (ret)
581 krb5_err(context, IPROPD_RESTART, ret, "db->destroy");
583 krb5_warnx(context, "receive complete database, version %ld", (long)vno);
584 return ret;
587 static void
588 slave_status(krb5_context context,
589 const char *file,
590 const char *status, ...)
591 __attribute__ ((format (printf, 3, 4)));
594 static void
595 slave_status(krb5_context context,
596 const char *file,
597 const char *fmt, ...)
599 char *status;
600 char *fmt2;
601 va_list args;
602 int len;
604 if (asprintf(&fmt2, "%s\n", fmt) == -1 || fmt2 == NULL) {
605 (void) unlink(file);
606 return;
608 va_start(args, fmt);
609 len = vasprintf(&status, fmt2, args);
610 free(fmt2);
611 va_end(args);
612 if (len < 0 || status == NULL) {
613 (void) unlink(file);
614 return;
616 krb5_warnx(context, "slave status change: %s", status);
618 rk_dumpdata(file, status, len);
619 free(status);
622 static void
623 is_up_to_date(krb5_context context, const char *file,
624 kadm5_server_context *server_context)
626 krb5_error_code ret;
627 char buf[80];
628 ret = krb5_format_time(context, time(NULL), buf, sizeof(buf), 1);
629 if (ret) {
630 unlink(file);
631 return;
633 slave_status(context, file, "up-to-date with version: %lu at %s",
634 (unsigned long)server_context->log_context.version, buf);
637 static char *status_file;
638 static char *config_file;
639 static char *realm;
640 static int version_flag;
641 static int help_flag;
642 static char *keytab_str;
643 static char *port_str;
644 static int detach_from_console;
645 static int daemon_child = -1;
647 static struct getargs args[] = {
648 { "config-file", 'c', arg_string, &config_file, NULL, NULL },
649 { "realm", 'r', arg_string, &realm, NULL, NULL },
650 { "keytab", 'k', arg_string, &keytab_str,
651 "keytab to get authentication from", "kspec" },
652 { "time-lost", 0, arg_string, &server_time_lost,
653 "time before server is considered lost", "time" },
654 { "status-file", 0, arg_string, &status_file,
655 "file to write out status into", "file" },
656 { "port", 0, arg_string, &port_str,
657 "port ipropd-slave will connect to", "port"},
658 { "detach", 0, arg_flag, &detach_from_console,
659 "detach from console", NULL },
660 { "daemon-child", 0 , arg_integer, &daemon_child,
661 "private argument, do not use", NULL },
662 { "hostname", 0, arg_string, rk_UNCONST(&slave_str),
663 "hostname of slave (if not same as hostname)", "hostname" },
664 { "verbose", 0, arg_flag, &verbose, NULL, NULL },
665 { "version", 0, arg_flag, &version_flag, NULL, NULL },
666 { "help", 0, arg_flag, &help_flag, NULL, NULL }
669 static int num_args = sizeof(args) / sizeof(args[0]);
671 static void
672 usage(int status)
674 arg_printusage(args, num_args, NULL, "master");
675 exit(status);
679 main(int argc, char **argv)
681 krb5_error_code ret, ret2;
682 krb5_context context;
683 krb5_auth_context auth_context;
684 void *kadm_handle;
685 kadm5_server_context *server_context;
686 kadm5_config_params conf;
687 int master_fd;
688 krb5_ccache ccache;
689 krb5_principal server;
690 char **files;
691 int optidx = 0;
692 time_t reconnect_min;
693 time_t backoff;
694 time_t reconnect_max;
695 time_t reconnect;
696 time_t before = 0;
697 int restarter_fd = -1;
699 const char *master;
701 setprogname(argv[0]);
703 if (getarg(args, num_args, argc, argv, &optidx))
704 usage(1);
706 if (help_flag)
707 usage(0);
709 if (version_flag) {
710 print_version(NULL);
711 exit(0);
714 if (detach_from_console && daemon_child == -1)
715 roken_detach_prep(argc, argv, "--daemon-child");
716 rk_pidfile(NULL);
718 ret = krb5_init_context(&context);
719 if (ret)
720 errx (1, "krb5_init_context failed: %d", ret);
722 setup_signal();
724 if (config_file == NULL) {
725 if (asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context)) == -1
726 || config_file == NULL)
727 errx(1, "out of memory");
730 ret = krb5_prepend_config_files_default(config_file, &files);
731 if (ret)
732 krb5_err(context, 1, ret, "getting configuration files");
734 ret = krb5_set_config_files(context, files);
735 krb5_free_config_files(files);
736 if (ret)
737 krb5_err(context, 1, ret, "reading configuration files");
739 argc -= optidx;
740 argv += optidx;
742 if (argc != 1)
743 usage(1);
745 master = argv[0];
747 if (status_file == NULL) {
748 if (asprintf(&status_file, "%s/ipropd-slave-status", hdb_db_dir(context)) < 0 || status_file == NULL)
749 krb5_errx(context, 1, "can't allocate status file buffer");
752 krb5_openlog(context, "ipropd-slave", &log_facility);
753 krb5_set_warn_dest(context, log_facility);
755 slave_status(context, status_file, "bootstrapping");
757 ret = krb5_kt_register(context, &hdb_get_kt_ops);
758 if(ret)
759 krb5_err(context, 1, ret, "krb5_kt_register");
761 time_before_lost = parse_time (server_time_lost, "s");
762 if (time_before_lost < 0)
763 krb5_errx (context, 1, "couldn't parse time: %s", server_time_lost);
765 slave_status(context, status_file, "getting credentials from keytab/database");
767 memset(&conf, 0, sizeof(conf));
768 if(realm) {
769 conf.mask |= KADM5_CONFIG_REALM;
770 conf.realm = realm;
772 ret = kadm5_init_with_password_ctx (context,
773 KADM5_ADMIN_SERVICE,
774 NULL,
775 KADM5_ADMIN_SERVICE,
776 &conf, 0, 0,
777 &kadm_handle);
778 if (ret)
779 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
781 server_context = (kadm5_server_context *)kadm_handle;
783 slave_status(context, status_file, "creating log file");
785 ret = kadm5_log_init (server_context);
786 if (ret)
787 krb5_err (context, 1, ret, "kadm5_log_init");
789 get_creds(context, keytab_str, &ccache, master);
791 ret = krb5_sname_to_principal (context, master, IPROP_NAME,
792 KRB5_NT_SRV_HST, &server);
793 if (ret)
794 krb5_err (context, 1, ret, "krb5_sname_to_principal");
796 auth_context = NULL;
797 master_fd = -1;
799 krb5_appdefault_time(context, config_name, NULL, "reconnect-min",
800 10, &reconnect_min);
801 krb5_appdefault_time(context, config_name, NULL, "reconnect-max",
802 300, &reconnect_max);
803 krb5_appdefault_time(context, config_name, NULL, "reconnect-backoff",
804 10, &backoff);
805 reconnect = reconnect_min;
807 slave_status(context, status_file, "ipropd-slave started");
809 roken_detach_finish(NULL, daemon_child);
810 restarter_fd = restarter(context, NULL);
812 while (!exit_flag) {
813 struct timeval to;
814 time_t now, elapsed;
815 fd_set readset;
816 int connected = FALSE;
818 #ifndef NO_LIMIT_FD_SETSIZE
819 if (restarter_fd >= FD_SETSIZE)
820 krb5_errx(context, IPROPD_RESTART, "fd too large");
821 #endif
823 FD_ZERO(&readset);
824 if (restarter_fd > -1)
825 FD_SET(restarter_fd, &readset);
827 now = time(NULL);
828 elapsed = now - before;
830 if (elapsed < reconnect) {
831 time_t left = reconnect - elapsed;
832 krb5_warnx(context, "sleeping %d seconds before "
833 "retrying to connect", (int)left);
834 to.tv_sec = left;
835 to.tv_usec = 0;
836 if (select(restarter_fd + 1, &readset, NULL, NULL, &to) == 1) {
837 exit_flag = SIGTERM;
838 continue;
841 before = now;
843 slave_status(context, status_file, "connecting to master: %s\n", master);
845 master_fd = connect_to_master (context, master, port_str);
846 if (master_fd < 0)
847 goto retry;
849 reconnect = reconnect_min;
851 if (auth_context) {
852 krb5_auth_con_free(context, auth_context);
853 auth_context = NULL;
854 krb5_cc_destroy(context, ccache);
855 get_creds(context, keytab_str, &ccache, master);
857 if (verbose)
858 krb5_warnx(context, "authenticating to master");
859 ret = krb5_sendauth (context, &auth_context, &master_fd,
860 IPROP_VERSION, NULL, server,
861 AP_OPTS_MUTUAL_REQUIRED, NULL, NULL,
862 ccache, NULL, NULL, NULL);
863 if (ret) {
864 krb5_warn (context, ret, "krb5_sendauth");
865 goto retry;
868 krb5_warnx(context, "ipropd-slave started at version: %ld",
869 (long)server_context->log_context.version);
871 ret = ihave(context, auth_context, master_fd,
872 server_context->log_context.version);
873 if (ret)
874 goto retry;
876 connected = TRUE;
878 if (verbose)
879 krb5_warnx(context, "connected to master");
881 slave_status(context, status_file, "connected to master, waiting instructions");
883 while (connected && !exit_flag) {
884 krb5_data out;
885 krb5_storage *sp;
886 uint32_t tmp;
887 int max_fd;
889 #ifndef NO_LIMIT_FD_SETSIZE
890 if (master_fd >= FD_SETSIZE)
891 krb5_errx(context, IPROPD_RESTART, "fd too large");
892 if (restarter_fd >= FD_SETSIZE)
893 krb5_errx(context, IPROPD_RESTART, "fd too large");
894 max_fd = max(restarter_fd, master_fd);
895 #endif
897 FD_ZERO(&readset);
898 FD_SET(master_fd, &readset);
899 if (restarter_fd != -1)
900 FD_SET(restarter_fd, &readset);
902 to.tv_sec = time_before_lost;
903 to.tv_usec = 0;
905 ret = select (max_fd + 1,
906 &readset, NULL, NULL, &to);
907 if (ret < 0) {
908 if (errno == EINTR)
909 continue;
910 else
911 krb5_err (context, 1, errno, "select");
913 if (ret == 0) {
914 krb5_warnx(context, "server didn't send a message "
915 "in %d seconds", time_before_lost);
916 connected = FALSE;
917 continue;
920 if (restarter_fd > -1 && FD_ISSET(restarter_fd, &readset)) {
921 if (verbose)
922 krb5_warnx(context, "slave restarter exited");
923 exit_flag = SIGTERM;
926 if (!FD_ISSET(master_fd, &readset))
927 continue;
929 if (verbose)
930 krb5_warnx(context, "message from master");
932 ret = krb5_read_priv_message(context, auth_context, &master_fd, &out);
933 if (ret) {
934 krb5_warn(context, ret, "krb5_read_priv_message");
935 connected = FALSE;
936 continue;
939 sp = krb5_storage_from_mem (out.data, out.length);
940 if (sp == NULL)
941 krb5_err(context, IPROPD_RESTART, errno, "krb5_storage_from_mem");
942 ret = krb5_ret_uint32(sp, &tmp);
943 if (ret == HEIM_ERR_EOF) {
944 krb5_warn(context, ret, "master sent zero-length message");
945 connected = FALSE;
946 continue;
948 if (ret != 0) {
949 krb5_warn(context, ret, "couldn't read master's message");
950 connected = FALSE;
951 continue;
954 ret = kadm5_log_init(server_context);
955 if (ret) {
956 krb5_err(context, IPROPD_RESTART, ret, "kadm5_log_init while "
957 "handling a message from the master");
959 switch (tmp) {
960 case FOR_YOU :
961 if (verbose)
962 krb5_warnx(context, "master sent us diffs");
963 ret2 = receive(context, sp, server_context);
964 if (ret2)
965 krb5_warn(context, ret,
966 "receive from ipropd-master had errors");
967 ret = ihave(context, auth_context, master_fd,
968 server_context->log_context.version);
969 if (ret || ret2)
970 connected = FALSE;
973 * If it returns an error, receive() may nonetheless
974 * have committed some entries successfully, so we must
975 * update the slave_status even if there were errors.
977 is_up_to_date(context, status_file, server_context);
978 break;
979 case TELL_YOU_EVERYTHING :
980 if (verbose)
981 krb5_warnx(context, "master sent us a full dump");
982 ret = receive_everything(context, master_fd, server_context,
983 auth_context);
984 if (ret == 0) {
985 ret = ihave(context, auth_context, master_fd,
986 server_context->log_context.version);
988 if (ret)
989 connected = FALSE;
990 else
991 is_up_to_date(context, status_file, server_context);
992 break;
993 case ARE_YOU_THERE :
994 if (verbose)
995 krb5_warnx(context, "master sent us a ping");
996 is_up_to_date(context, status_file, server_context);
997 ret = ihave(context, auth_context, master_fd,
998 server_context->log_context.version);
999 if (ret)
1000 connected = FALSE;
1002 send_im_here(context, master_fd, auth_context);
1003 break;
1004 case YOU_HAVE_LAST_VERSION:
1005 if (verbose)
1006 krb5_warnx(context, "master tells us we are up to date");
1007 is_up_to_date(context, status_file, server_context);
1008 break;
1009 case NOW_YOU_HAVE :
1010 case I_HAVE :
1011 case ONE_PRINC :
1012 case I_AM_HERE :
1013 default :
1014 krb5_warnx (context, "Ignoring command %d", tmp);
1015 break;
1017 krb5_storage_free (sp);
1018 krb5_data_free (&out);
1022 slave_status(context, status_file, "disconnected from master");
1023 retry:
1024 if (connected == FALSE)
1025 krb5_warnx (context, "disconnected for server");
1027 if (exit_flag)
1028 krb5_warnx (context, "got an exit signal");
1030 if (master_fd >= 0)
1031 close(master_fd);
1033 reconnect += backoff;
1034 if (reconnect > reconnect_max) {
1035 slave_status(context, status_file, "disconnected from master for a long time");
1036 reconnect = reconnect_max;
1040 if (status_file) {
1041 /* XXX It'd be better to leave it saying we're not here */
1042 unlink(status_file);
1045 if (0);
1046 #ifndef NO_SIGXCPU
1047 else if(exit_flag == SIGXCPU)
1048 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
1049 #endif
1050 else if(exit_flag == SIGINT || exit_flag == SIGTERM)
1051 krb5_warnx(context, "%s terminated", getprogname());
1052 else
1053 krb5_warnx(context, "%s unexpected exit reason: %ld",
1054 getprogname(), (long)exit_flag);
1056 return 0;