2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
38 static const char *config_name
= "ipropd-slave";
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
;
49 connect_to_master (krb5_context context
, const char *master
,
52 char port
[NI_MAXSERV
];
53 struct addrinfo
*ai
, *a
;
54 struct addrinfo hints
;
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
);
67 error
= getaddrinfo(master
, port_str
, &hints
, &ai
);
69 krb5_warnx(context
, "Failed to get address of to %s: %s",
70 master
, gai_strerror(error
));
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
);
79 strlcpy(node
, "[unknown-addr]", sizeof(node
));
81 s
= socket(a
->ai_family
, a
->ai_socktype
, a
->ai_protocol
);
84 if (connect(s
, a
->ai_addr
, a
->ai_addrlen
) < 0) {
85 krb5_warn(context
, errno
, "connection failed to %s[%s]",
90 krb5_warnx(context
, "connection successful "
91 "to master: %s[%s]", master
, node
);
99 if (setsockopt(s
, SOL_SOCKET
, SO_KEEPALIVE
, &one
, sizeof(one
)) < 0)
100 krb5_warn(context
, errno
, "setsockopt(SO_KEEPALIVE) failed");
106 get_creds(krb5_context context
, const char *keytab_str
,
107 krb5_ccache
*cache
, const char *serverhost
)
110 krb5_principal client
;
112 krb5_get_init_creds_opt
*init_opts
;
115 char keytab_buf
[256];
118 if (keytab_str
== NULL
) {
119 ret
= krb5_kt_default_name (context
, keytab_buf
, sizeof(keytab_buf
));
121 krb5_err (context
, 1, ret
, "krb5_kt_default_name");
122 keytab_str
= keytab_buf
;
125 ret
= krb5_kt_resolve(context
, keytab_str
, &keytab
);
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
);
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
)
172 sp
= krb5_storage_from_mem(buf
, 8);
173 ret
= krb5_store_uint32(sp
, I_HAVE
);
175 ret
= krb5_store_uint32(sp
, version
);
176 krb5_storage_free(sp
);
182 krb5_warnx(context
, "telling master we are at %u", version
);
184 ret
= krb5_write_priv_message(context
, auth_context
, &fd
, &data
);
186 krb5_warn(context
, ret
, "krb5_write_message");
192 /* There's no EDQUOT on WIN32, for example */
193 #define EDQUOT ENOSPC
197 append_to_log_file(krb5_context context
,
198 kadm5_server_context
*server_context
,
199 krb5_storage
*sp
, off_t start
, ssize_t slen
)
208 krb5_warnx(context
, "appending diffs to log");
219 if (buf
== NULL
&& len
!= 0) {
220 krb5_warn(context
, errno
, "malloc: no memory");
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
);
231 if (len
!= (size_t)sret
) {
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
);
241 * Use net_write() so we get an errno if less that len bytes were
244 sret
= net_write(server_context
->log_context
.log_fd
, buf
, len
);
249 ret
= fsync(server_context
->log_context
.log_fd
);
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
);
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
);
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");
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");
301 receive_loop (krb5_context context
,
303 kadm5_server_context
*server_context
)
306 off_t left
, right
, off
;
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.
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
, ×tamp
) != 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");
341 if (vers
> server_context
->log_context
.version
) {
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");
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");
368 * ...and then write them out to the on-disk log.
371 ret
= append_to_log_file(context
, server_context
, sp
, left
, right
- left
);
376 * Replay the new entries.
379 krb5_warnx(context
, "replaying entries from master");
380 ret
= kadm5_log_recover(server_context
, kadm_recover_replay
);
382 krb5_warn(context
, ret
, "replay failed");
386 ret
= kadm5_log_get_version(server_context
, &vers
);
388 krb5_warn(context
, ret
,
389 "could not get log version after applying diffs!");
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
);
405 receive(krb5_context context
,
407 kadm5_server_context
*server_context
)
409 krb5_error_code ret
, ret2
;
411 ret
= server_context
->db
->hdb_open(context
,
413 O_RDWR
| O_CREAT
, 0600);
415 krb5_err(context
, IPROPD_RESTART_SLOW
, ret
, "db->open");
417 ret2
= receive_loop(context
, sp
, server_context
);
419 krb5_warn(context
, ret
, "receive from ipropd-master had errors");
421 ret
= server_context
->db
->hdb_close(context
, server_context
->db
);
423 krb5_err(context
, IPROPD_RESTART_SLOW
, ret
, "db->close");
429 send_im_here(krb5_context context
, int fd
,
430 krb5_auth_context auth_context
)
436 ret
= krb5_data_alloc(&data
, 4);
438 krb5_err(context
, IPROPD_RESTART
, ret
, "send_im_here");
440 sp
= krb5_storage_from_data (&data
);
442 krb5_errx(context
, IPROPD_RESTART
, "krb5_storage_from_data");
443 ret
= krb5_store_uint32(sp
, I_AM_HERE
);
444 krb5_storage_free(sp
);
447 ret
= krb5_write_priv_message(context
, auth_context
, &fd
, &data
);
448 krb5_data_free(&data
);
451 krb5_err(context
, IPROPD_RESTART
, ret
, "krb5_write_priv_message");
454 krb5_warnx(context
, "pinged master");
461 reinit_log(krb5_context context
,
462 kadm5_server_context
*server_context
,
468 krb5_warnx(context
, "truncating log on slave");
470 ret
= kadm5_log_reinit(server_context
, vno
);
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
)
490 krb5_warnx(context
, "receive complete database");
492 ret
= asprintf(&dbname
, "%s-NEW", server_context
->db
->hdb_name
);
494 krb5_err(context
, IPROPD_RESTART
, ENOMEM
, "asprintf");
495 ret
= hdb_create(context
, &mydb
, dbname
);
497 krb5_err(context
, IPROPD_RESTART
, ret
, "hdb_create");
500 ret
= hdb_set_master_keyfile(context
,
501 mydb
, server_context
->config
.stash_file
);
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);
509 krb5_err(context
, IPROPD_RESTART
, ret
, "db->open");
512 krb5_data_zero(&data
);
514 ret
= krb5_read_priv_message(context
, auth_context
, &fd
, &data
);
517 krb5_warn(context
, ret
, "krb5_read_priv_message");
521 sp
= krb5_storage_from_data(&data
);
523 krb5_errx(context
, IPROPD_RESTART
, "krb5_storage_from_data");
524 krb5_ret_uint32(sp
, &opcode
);
525 if (opcode
== ONE_PRINC
) {
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
);
538 krb5_err(context
, IPROPD_RESTART
, ret
, "hdb_value2entry");
539 ret
= mydb
->hdb_store(server_context
->context
,
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
)
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
);
564 krb5_err(context
, IPROPD_RESTART_SLOW
, ret
, "db->close");
566 ret
= mydb
->hdb_rename(context
, mydb
, server_context
->db
->hdb_name
);
568 krb5_err(context
, IPROPD_RESTART_SLOW
, ret
, "db->rename");
574 krb5_data_free(&data
);
577 krb5_err(context
, IPROPD_RESTART_SLOW
, ret
, "db->close");
579 ret
= mydb
->hdb_destroy(context
, mydb
);
581 krb5_err(context
, IPROPD_RESTART
, ret
, "db->destroy");
583 krb5_warnx(context
, "receive complete database, version %ld", (long)vno
);
588 slave_status(krb5_context context
,
590 const char *status
, ...)
591 __attribute__ ((format (printf
, 3, 4)));
595 slave_status(krb5_context context
,
597 const char *fmt
, ...)
604 if (asprintf(&fmt2
, "%s\n", fmt
) == -1 || fmt2
== NULL
) {
609 len
= vasprintf(&status
, fmt2
, args
);
612 if (len
< 0 || status
== NULL
) {
616 krb5_warnx(context
, "slave status change: %s", status
);
618 rk_dumpdata(file
, status
, len
);
623 is_up_to_date(krb5_context context
, const char *file
,
624 kadm5_server_context
*server_context
)
628 ret
= krb5_format_time(context
, time(NULL
), buf
, sizeof(buf
), 1);
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
;
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]);
674 arg_printusage(args
, num_args
, NULL
, "master");
679 main(int argc
, char **argv
)
681 krb5_error_code ret
, ret2
;
682 krb5_context context
;
683 krb5_auth_context auth_context
;
685 kadm5_server_context
*server_context
;
686 kadm5_config_params conf
;
689 krb5_principal server
;
692 time_t reconnect_min
;
694 time_t reconnect_max
;
697 int restarter_fd
= -1;
701 setprogname(argv
[0]);
703 if (getarg(args
, num_args
, argc
, argv
, &optidx
))
714 if (detach_from_console
&& daemon_child
== -1)
715 roken_detach_prep(argc
, argv
, "--daemon-child");
718 ret
= krb5_init_context(&context
);
720 errx (1, "krb5_init_context failed: %d", ret
);
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
);
732 krb5_err(context
, 1, ret
, "getting configuration files");
734 ret
= krb5_set_config_files(context
, files
);
735 krb5_free_config_files(files
);
737 krb5_err(context
, 1, ret
, "reading configuration files");
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
);
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
));
769 conf
.mask
|= KADM5_CONFIG_REALM
;
772 ret
= kadm5_init_with_password_ctx (context
,
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
);
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
);
794 krb5_err (context
, 1, ret
, "krb5_sname_to_principal");
799 krb5_appdefault_time(context
, config_name
, NULL
, "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",
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
);
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");
824 if (restarter_fd
> -1)
825 FD_SET(restarter_fd
, &readset
);
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
);
836 if (select(restarter_fd
+ 1, &readset
, NULL
, NULL
, &to
) == 1) {
843 slave_status(context
, status_file
, "connecting to master: %s\n", master
);
845 master_fd
= connect_to_master (context
, master
, port_str
);
849 reconnect
= reconnect_min
;
852 krb5_auth_con_free(context
, auth_context
);
854 krb5_cc_destroy(context
, ccache
);
855 get_creds(context
, keytab_str
, &ccache
, master
);
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
);
864 krb5_warn (context
, ret
, "krb5_sendauth");
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
);
879 krb5_warnx(context
, "connected to master");
881 slave_status(context
, status_file
, "connected to master, waiting instructions");
883 while (connected
&& !exit_flag
) {
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
);
898 FD_SET(master_fd
, &readset
);
899 if (restarter_fd
!= -1)
900 FD_SET(restarter_fd
, &readset
);
902 to
.tv_sec
= time_before_lost
;
905 ret
= select (max_fd
+ 1,
906 &readset
, NULL
, NULL
, &to
);
911 krb5_err (context
, 1, errno
, "select");
914 krb5_warnx(context
, "server didn't send a message "
915 "in %d seconds", time_before_lost
);
920 if (restarter_fd
> -1 && FD_ISSET(restarter_fd
, &readset
)) {
922 krb5_warnx(context
, "slave restarter exited");
926 if (!FD_ISSET(master_fd
, &readset
))
930 krb5_warnx(context
, "message from master");
932 ret
= krb5_read_priv_message(context
, auth_context
, &master_fd
, &out
);
934 krb5_warn(context
, ret
, "krb5_read_priv_message");
939 sp
= krb5_storage_from_mem (out
.data
, out
.length
);
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");
949 krb5_warn(context
, ret
, "couldn't read master's message");
954 ret
= kadm5_log_init(server_context
);
956 krb5_err(context
, IPROPD_RESTART
, ret
, "kadm5_log_init while "
957 "handling a message from the master");
962 krb5_warnx(context
, "master sent us diffs");
963 ret2
= receive(context
, sp
, server_context
);
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
);
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
);
979 case TELL_YOU_EVERYTHING
:
981 krb5_warnx(context
, "master sent us a full dump");
982 ret
= receive_everything(context
, master_fd
, server_context
,
985 ret
= ihave(context
, auth_context
, master_fd
,
986 server_context
->log_context
.version
);
991 is_up_to_date(context
, status_file
, server_context
);
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
);
1002 send_im_here(context
, master_fd
, auth_context
);
1004 case YOU_HAVE_LAST_VERSION
:
1006 krb5_warnx(context
, "master tells us we are up to date");
1007 is_up_to_date(context
, status_file
, server_context
);
1014 krb5_warnx (context
, "Ignoring command %d", tmp
);
1017 krb5_storage_free (sp
);
1018 krb5_data_free (&out
);
1022 slave_status(context
, status_file
, "disconnected from master");
1024 if (connected
== FALSE
)
1025 krb5_warnx (context
, "disconnected for server");
1028 krb5_warnx (context
, "got an exit signal");
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
;
1041 /* XXX It'd be better to leave it saying we're not here */
1042 unlink(status_file
);
1047 else if(exit_flag
== SIGXCPU
)
1048 krb5_warnx(context
, "%s CPU time limit exceeded", getprogname());
1050 else if(exit_flag
== SIGINT
|| exit_flag
== SIGTERM
)
1051 krb5_warnx(context
, "%s terminated", getprogname());
1053 krb5_warnx(context
, "%s unexpected exit reason: %ld",
1054 getprogname(), (long)exit_flag
);