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
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
;
49 make_signal_socket (krb5_context context
)
51 #ifndef NO_UNIX_SOCKETS
52 struct sockaddr_un addr
;
56 fn
= kadm5_log_signal_socket(context
);
58 fd
= socket (AF_UNIX
, SOCK_DGRAM
, 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
);
69 struct addrinfo
*ai
= NULL
;
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");
85 make_listen_socket (krb5_context context
, const char *port_str
)
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
;
99 addr
.sin_port
= krb5_getportbyname (context
,
102 if (addr
.sin_port
== 0) {
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
);
112 addr
.sin_port
= krb5_getportbyname (context
, IPROP_SERVICE
,
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");
124 struct sockaddr_in addr
;
126 krb5_auth_context ac
;
130 #define SLAVE_F_DEAD 0x1
131 #define SLAVE_F_AYT 0x2
135 typedef struct slave slave
;
138 check_acl (krb5_context context
, const char *name
)
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
,
157 fp
= fopen (fn
, "r");
161 while (fgets(buf
, sizeof(buf
), fp
) != NULL
) {
162 buf
[strcspn(buf
, "\r\n")] = '\0';
163 if (strcmp (buf
, name
) == 0) {
175 s
->flags
&= ~SLAVE_F_AYT
;
176 s
->seen
= time(NULL
);
180 slave_missing_p (slave
*s
)
182 if (time(NULL
) > s
->seen
+ time_before_missing
)
188 slave_gone_p (slave
*s
)
190 if (time(NULL
) > s
->seen
+ time_before_gone
)
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
;
209 remove_slave (krb5_context context
, slave
*s
, slave
**root
)
213 if (!rk_IS_BAD_SOCKET(s
->fd
))
214 rk_closesocket (s
->fd
);
218 krb5_auth_con_free (context
, s
->ac
);
220 for (p
= root
; *p
; p
= &(*p
)->next
)
229 add_slave (krb5_context context
, krb5_keytab keytab
, slave
**root
,
232 krb5_principal server
;
236 krb5_ticket
*ticket
= NULL
;
239 s
= malloc(sizeof(*s
));
241 krb5_warnx (context
, "add_slave: no memory");
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");
254 strlcpy(hostname
, master_hostname
, sizeof(hostname
));
256 gethostname(hostname
, sizeof(hostname
));
258 ret
= krb5_sname_to_principal (context
, hostname
, IPROP_NAME
,
259 KRB5_NT_SRV_HST
, &server
);
261 krb5_warn (context
, ret
, "krb5_sname_to_principal");
265 ret
= krb5_recvauth (context
, &s
->ac
, &s
->fd
,
266 IPROP_VERSION
, server
, 0, keytab
, &ticket
);
267 krb5_free_principal (context
, server
);
269 krb5_warn (context
, ret
, "krb5_recvauth");
272 ret
= krb5_unparse_name (context
, ticket
->client
, &s
->name
);
273 krb5_free_ticket (context
, ticket
);
275 krb5_warn (context
, ret
, "krb5_unparse_name");
278 if (check_acl (context
, s
->name
)) {
279 krb5_warnx (context
, "%s not in acl", s
->name
);
287 if (strcmp(l
->name
, s
->name
) == 0)
292 if (l
->flags
& SLAVE_F_DEAD
) {
293 remove_slave(context
, l
, root
);
295 krb5_warnx (context
, "second connection from %s", s
->name
);
301 krb5_warnx (context
, "connection from %s", s
->name
);
310 remove_slave(context
, s
, root
);
314 dump_one (krb5_context context
, HDB
*db
, hdb_entry_ex
*entry
, void *v
)
317 krb5_storage
*dump
= (krb5_storage
*)v
;
321 ret
= hdb_entry2value (context
, &entry
->entry
, &data
);
324 ret
= krb5_data_realloc (&data
, data
.length
+ 4);
327 memmove ((char *)data
.data
+ 4, data
.data
, data
.length
- 4);
328 sp
= krb5_storage_from_data(&data
);
333 krb5_store_int32(sp
, ONE_PRINC
);
334 krb5_storage_free(sp
);
336 ret
= krb5_store_data(dump
, data
);
339 krb5_data_free (&data
);
344 write_dump (krb5_context context
, krb5_storage
*dump
,
345 const char *database
, uint32_t current_version
)
353 /* we assume that the caller has obtained an exclusive lock */
355 ret
= krb5_storage_truncate(dump
, 0);
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
367 ret
= krb5_store_uint32(dump
, 0);
369 ret
= hdb_create (context
, &db
, database
);
371 krb5_err (context
, 1, ret
, "hdb_create: %s", database
);
372 ret
= db
->hdb_open (context
, db
, O_RDONLY
, 0);
374 krb5_err (context
, 1, ret
, "db->open");
376 sp
= krb5_storage_from_mem (buf
, 4);
378 krb5_errx (context
, 1, "krb5_storage_from_mem");
379 krb5_store_int32 (sp
, TELL_YOU_EVERYTHING
);
380 krb5_storage_free (sp
);
385 ret
= krb5_store_data(dump
, data
);
387 krb5_warn (context
, ret
, "write_dump");
391 ret
= hdb_foreach (context
, db
, HDB_F_ADMIN_DATA
, dump_one
, dump
);
393 krb5_warn (context
, ret
, "write_dump: hdb_foreach");
397 (*db
->hdb_close
)(context
, db
);
398 (*db
->hdb_destroy
)(context
, db
);
400 sp
= krb5_storage_from_mem (buf
, 8);
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
);
409 ret
= krb5_store_data(dump
, data
);
411 krb5_warn (context
, ret
, "write_dump");
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
);
425 krb5_warn(context
, ret
, "syncing iprop dumpfile");
429 ret
= krb5_storage_seek(dump
, 0, SEEK_SET
);
432 krb5_warn(context
, ret
, "krb5_storage_seek(dump, 0, SEEK_SET)");
436 /* Write current version at the front making the dump valid */
438 ret
= krb5_store_uint32(dump
, current_version
);
440 krb5_warn(context
, ret
, "writing version to dumpfile");
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
);
456 send_complete (krb5_context context
, slave
*s
, const char *database
,
457 uint32_t current_version
, uint32_t oldest_version
)
460 krb5_storage
*dump
= NULL
;
466 ret
= asprintf(&dfn
, "%s/ipropd.dumpfile", hdb_db_dir(context
));
467 if (ret
== -1 || !dfn
) {
468 krb5_warn(context
, ENOMEM
, "Cannot allocate memory");
472 fd
= open(dfn
, O_CREAT
|O_RDWR
, 0600);
475 krb5_warn(context
, ret
, "Cannot open/create iprop dumpfile %s", dfn
);
481 dump
= krb5_storage_from_fd(fd
);
484 krb5_warn(context
, ret
, "krb5_storage_from_fd");
489 ret
= flock(fd
, LOCK_SH
);
492 krb5_warn(context
, ret
, "flock(fd, LOCK_SH)");
496 krb5_storage_seek(dump
, 0, SEEK_SET
);
499 krb5_warn(context
, ret
, "krb5_storage_seek(dump, 0, SEEK_SET)");
504 ret
= krb5_ret_uint32(dump
, &vno
);
505 if (ret
&& ret
!= HEIM_ERR_EOF
) {
506 krb5_warn(context
, ret
, "krb5_ret_uint32(dump, &vno)");
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
)
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.
532 krb5_warn(context
, ret
, "flock(fd, LOCK_EX)");
536 krb5_storage_seek(dump
, 0, SEEK_SET
);
539 krb5_warn(context
, ret
, "krb5_storage_seek(dump, 0, SEEK_SET)");
544 ret
= krb5_ret_uint32(dump
, &vno
);
545 if (ret
&& ret
!= HEIM_ERR_EOF
) {
546 krb5_warn(context
, ret
, "krb5_ret_uint32(dump, &vno)");
550 /* check if someone wrote a better version for us */
551 if (vno
>= oldest_version
)
554 /* Now, we know that we must write a new dump file. */
556 ret
= write_dump(context
, dump
, database
, current_version
);
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.
574 ret
= krb5_ret_data(dump
, &data
);
575 if (ret
== HEIM_ERR_EOF
) {
576 ret
= 0; /* EOF is not an error, it's success */
581 krb5_warn(context
, ret
, "krb5_ret_data(dump, &data)");
582 slave_dead(context
, s
);
586 ret
= krb5_write_priv_message(context
, s
->ac
, &s
->fd
, &data
);
587 krb5_data_free(&data
);
590 krb5_warn (context
, ret
, "krb5_write_priv_message");
591 slave_dead(context
, s
);
604 krb5_storage_free(dump
);
609 send_are_you_there (krb5_context context
, slave
*s
)
616 if (s
->flags
& (SLAVE_F_DEAD
|SLAVE_F_AYT
))
619 krb5_warnx(context
, "slave %s missing, sending AYT", s
->name
);
621 s
->flags
|= SLAVE_F_AYT
;
626 sp
= krb5_storage_from_mem (buf
, 4);
628 krb5_warnx (context
, "are_you_there: krb5_data_alloc");
629 slave_dead(context
, s
);
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
);
638 krb5_warn (context
, ret
, "are_you_there: krb5_write_priv_message");
639 slave_dead(context
, s
);
647 send_diffs (krb5_context context
, slave
*s
, int log_fd
,
648 const char *database
, uint32_t current_version
)
659 if (s
->version
== current_version
) {
662 sp
= krb5_storage_from_mem(buf
, 4);
664 krb5_errx(context
, 1, "krb5_storage_from_mem");
665 krb5_store_int32(sp
, YOU_HAVE_LAST_VERSION
);
666 krb5_storage_free(sp
);
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
);
675 if (s
->flags
& SLAVE_F_DEAD
)
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
);
683 ret
= kadm5_log_previous (context
, sp
, &ver
, ×tamp
, &op
, &len
);
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
)
690 if (ver
== s
->version
+ 1)
693 krb5_storage_free(sp
);
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
);
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);
709 krb5_storage_free(sp
);
710 krb5_warn (context
, ret
, "send_diffs: krb5_data_alloc");
711 slave_dead(context
, s
);
714 krb5_storage_read (sp
, (char *)data
.data
+ 4, data
.length
- 4);
715 krb5_storage_free(sp
);
717 sp
= krb5_storage_from_data (&data
);
719 krb5_warnx (context
, "send_diffs: krb5_storage_from_data");
720 slave_dead(context
, s
);
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
);
730 krb5_warn (context
, ret
, "send_diffs: krb5_write_priv_message");
731 slave_dead(context
, s
);
736 s
->version
= current_version
;
742 process_msg (krb5_context context
, slave
*s
, int log_fd
,
743 const char *database
, uint32_t current_version
)
750 ret
= krb5_read_priv_message(context
, s
->ac
, &s
->fd
, &out
);
752 krb5_warn (context
, ret
, "error reading message from %s", s
->name
);
756 sp
= krb5_storage_from_mem (out
.data
, out
.length
);
758 krb5_warnx (context
, "process_msg: no memory");
759 krb5_data_free (&out
);
762 if (krb5_ret_int32 (sp
, &tmp
) != 0) {
763 krb5_warnx (context
, "process_msg: client send too short command");
764 krb5_data_free (&out
);
769 ret
= krb5_ret_int32 (sp
, &tmp
);
771 krb5_warnx (context
, "process_msg: client send too I_HAVE data");
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
);
784 if ((uint32_t)tmp
< s
->version
) {
785 krb5_warnx (context
, "Slave claims to not have "
786 "version we already sent to it");
788 ret
= send_diffs (context
, s
, log_fd
, database
, current_version
);
796 krb5_warnx (context
, "Ignoring command %d", tmp
);
800 krb5_data_free (&out
);
801 krb5_storage_free (sp
);
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"
815 open_stats(krb5_context context
)
817 char *statfile
= NULL
;
818 const char *fn
= 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
829 fn
= krb5_config_get_string_default(context
,
836 out
= fopen(fn
, "w");
837 if (statfile
!= NULL
)
843 write_master_down(krb5_context context
)
846 time_t t
= time(NULL
);
849 fp
= open_stats(context
);
852 krb5_format_time(context
, t
, str
, sizeof(str
), TRUE
);
853 fprintf(fp
, "master down at %s\n", str
);
859 write_stats(krb5_context context
, slave
*slaves
, uint32_t current_version
)
863 time_t t
= time(NULL
);
866 fp
= open_stats(context
);
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
);
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
, "");
893 rtbl_add_column_entry(tbl
, SLAVE_NAME
, slaves
->name
);
894 ret
= krb5_sockaddr2address (context
,
895 (struct sockaddr
*)&slaves
->addr
, &addr
);
897 krb5_print_address(&addr
, str
, sizeof(str
), NULL
);
898 krb5_free_address(context
, &addr
);
899 rtbl_add_column_entry(tbl
, SLAVE_ADDRESS
, str
);
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");
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
);
924 static char sHDB
[] = "HDBGET:";
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;
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
},
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
)
965 krb5_context context
;
967 kadm5_server_context
*server_context
;
968 kadm5_config_params conf
;
969 krb5_socket_t signal_fd
, listen_fd
;
971 slave
*slaves
= NULL
;
972 uint32_t current_version
= 0, old_version
= 0;
977 (void) krb5_program_setup(&context
, argc
, argv
, args
, num_args
, NULL
);
980 krb5_std_usage(0, args
, num_args
);
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
);
996 krb5_err(context
, 1, ret
, "getting configuration files");
998 ret
= krb5_set_config_files(context
, files
);
999 krb5_free_config_files(files
);
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);
1014 /* not much to do if detaching fails... */
1015 krb5_err(context
, 1, aret
, "failed to daemon(3)ise");
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
);
1025 krb5_err(context
, 1, ret
, "krb5_kt_register");
1027 ret
= krb5_kt_resolve(context
, keytab_str
, &keytab
);
1029 krb5_err(context
, 1, ret
, "krb5_kt_resolve: %s", keytab_str
);
1031 memset(&conf
, 0, sizeof(conf
));
1033 conf
.mask
|= KADM5_CONFIG_REALM
;
1036 ret
= kadm5_init_with_skey_ctx (context
,
1037 KADM5_ADMIN_SERVICE
,
1039 KADM5_ADMIN_SERVICE
,
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);
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
, ¤t_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){
1066 struct timeval to
= {30, 0};
1069 #ifndef NO_LIMIT_FD_SETSIZE
1070 if (signal_fd
>= FD_SETSIZE
|| listen_fd
>= FD_SETSIZE
)
1071 krb5_errx (context
, 1, "fd too large");
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
)
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
);
1093 krb5_err (context
, 1, errno
, "select");
1097 old_version
= current_version
;
1098 flock(log_fd
, LOCK_SH
);
1099 kadm5_log_get_version_fd (log_fd
, ¤t_version
);
1100 flock(log_fd
, LOCK_UN
);
1102 if (current_version
> old_version
) {
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
)
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
;
1119 struct sockaddr_storage peer_addr
;
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");
1130 old_version
= current_version
;
1131 flock(log_fd
, LOCK_SH
);
1132 kadm5_log_get_version_fd (log_fd
, ¤t_version
);
1133 flock(log_fd
, LOCK_UN
);
1134 if (current_version
> old_version
) {
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
)
1142 send_diffs (context
, p
, log_fd
, database
, current_version
);
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
)
1154 if (ret
&& FD_ISSET(p
->fd
, &readset
)) {
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
);
1170 write_stats(context
, slaves
, current_version
);
1173 if(exit_flag
== SIGINT
|| exit_flag
== SIGTERM
)
1174 krb5_warnx(context
, "%s terminated", getprogname());
1176 else if(exit_flag
== SIGXCPU
)
1177 krb5_warnx(context
, "%s CPU time limit exceeded", getprogname());
1180 krb5_warnx(context
, "%s unexpected exit reason: %ld",
1181 getprogname(), (long)exit_flag
);
1183 write_master_down(context
);