1 /* $NetBSD: ipropd_master.c,v 1.1.1.2 2014/04/24 12:45:48 pettai Exp $ */
2
3 /*
4 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include "iprop.h"
37 #include <krb5/rtbl.h>
38
39 static krb5_log_facility *log_facility;
40
41 const char *slave_stats_file;
42 const char *slave_time_missing = "2 min";
43 const char *slave_time_gone = "5 min";
44
45 static int time_before_missing;
46 static int time_before_gone;
47
48 const char *master_hostname;
49
50 static krb5_socket_t
make_signal_socket(krb5_context context)51 make_signal_socket (krb5_context context)
52 {
53 #ifndef NO_UNIX_SOCKETS
54 struct sockaddr_un addr;
55 const char *fn;
56 krb5_socket_t fd;
57
58 fn = kadm5_log_signal_socket(context);
59
60 fd = socket (AF_UNIX, SOCK_DGRAM, 0);
61 if (fd < 0)
62 krb5_err (context, 1, errno, "socket AF_UNIX");
63 memset (&addr, 0, sizeof(addr));
64 addr.sun_family = AF_UNIX;
65 strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
66 unlink (addr.sun_path);
67 if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
68 krb5_err (context, 1, errno, "bind %s", addr.sun_path);
69 return fd;
70 #else
71 struct addrinfo *ai = NULL;
72 krb5_socket_t fd;
73
74 kadm5_log_signal_socket_info(context, 1, &ai);
75
76 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
77 if (rk_IS_BAD_SOCKET(fd))
78 krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF=%d", ai->ai_family);
79
80 if (rk_IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) ))
81 krb5_err (context, 1, rk_SOCK_ERRNO, "bind");
82 return fd;
83 #endif
84 }
85
86 static krb5_socket_t
make_listen_socket(krb5_context context,const char * port_str)87 make_listen_socket (krb5_context context, const char *port_str)
88 {
89 krb5_socket_t fd;
90 int one = 1;
91 struct sockaddr_in addr;
92
93 fd = socket (AF_INET, SOCK_STREAM, 0);
94 if (rk_IS_BAD_SOCKET(fd))
95 krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF_INET");
96 setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
97 memset (&addr, 0, sizeof(addr));
98 addr.sin_family = AF_INET;
99
100 if (port_str) {
101 addr.sin_port = krb5_getportbyname (context,
102 port_str, "tcp",
103 0);
104 if (addr.sin_port == 0) {
105 char *ptr;
106 long port;
107
108 port = strtol (port_str, &ptr, 10);
109 if (port == 0 && ptr == port_str)
110 krb5_errx (context, 1, "bad port `%s'", port_str);
111 addr.sin_port = htons(port);
112 }
113 } else {
114 addr.sin_port = krb5_getportbyname (context, IPROP_SERVICE,
115 "tcp", IPROP_PORT);
116 }
117 if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
118 krb5_err (context, 1, errno, "bind");
119 if (listen(fd, SOMAXCONN) < 0)
120 krb5_err (context, 1, errno, "listen");
121 return fd;
122 }
123
124 struct slave {
125 krb5_socket_t fd;
126 struct sockaddr_in addr;
127 char *name;
128 krb5_auth_context ac;
129 uint32_t version;
130 time_t seen;
131 unsigned long flags;
132 #define SLAVE_F_DEAD 0x1
133 #define SLAVE_F_AYT 0x2
134 struct slave *next;
135 };
136
137 typedef struct slave slave;
138
139 static int
check_acl(krb5_context context,const char * name)140 check_acl (krb5_context context, const char *name)
141 {
142 const char *fn;
143 FILE *fp;
144 char buf[256];
145 int ret = 1;
146 char *slavefile = NULL;
147
148 if (asprintf(&slavefile, "%s/slaves", hdb_db_dir(context)) == -1
149 || slavefile == NULL)
150 errx(1, "out of memory");
151
152 fn = krb5_config_get_string_default(context,
153 NULL,
154 slavefile,
155 "kdc",
156 "iprop-acl",
157 NULL);
158
159 fp = fopen (fn, "r");
160 free(slavefile);
161 if (fp == NULL)
162 return 1;
163 while (fgets(buf, sizeof(buf), fp) != NULL) {
164 buf[strcspn(buf, "\r\n")] = '\0';
165 if (strcmp (buf, name) == 0) {
166 ret = 0;
167 break;
168 }
169 }
170 fclose (fp);
171 return ret;
172 }
173
174 static void
slave_seen(slave * s)175 slave_seen(slave *s)
176 {
177 s->flags &= ~SLAVE_F_AYT;
178 s->seen = time(NULL);
179 }
180
181 static int
slave_missing_p(slave * s)182 slave_missing_p (slave *s)
183 {
184 if (time(NULL) > s->seen + time_before_missing)
185 return 1;
186 return 0;
187 }
188
189 static int
slave_gone_p(slave * s)190 slave_gone_p (slave *s)
191 {
192 if (time(NULL) > s->seen + time_before_gone)
193 return 1;
194 return 0;
195 }
196
197 static void
slave_dead(krb5_context context,slave * s)198 slave_dead(krb5_context context, slave *s)
199 {
200 krb5_warnx(context, "slave %s dead", s->name);
201
202 if (!rk_IS_BAD_SOCKET(s->fd)) {
203 rk_closesocket (s->fd);
204 s->fd = rk_INVALID_SOCKET;
205 }
206 s->flags |= SLAVE_F_DEAD;
207 slave_seen(s);
208 }
209
210 static void
remove_slave(krb5_context context,slave * s,slave ** root)211 remove_slave (krb5_context context, slave *s, slave **root)
212 {
213 slave **p;
214
215 if (!rk_IS_BAD_SOCKET(s->fd))
216 rk_closesocket (s->fd);
217 if (s->name)
218 free (s->name);
219 if (s->ac)
220 krb5_auth_con_free (context, s->ac);
221
222 for (p = root; *p; p = &(*p)->next)
223 if (*p == s) {
224 *p = s->next;
225 break;
226 }
227 free (s);
228 }
229
230 static void
add_slave(krb5_context context,krb5_keytab keytab,slave ** root,krb5_socket_t fd)231 add_slave (krb5_context context, krb5_keytab keytab, slave **root,
232 krb5_socket_t fd)
233 {
234 krb5_principal server;
235 krb5_error_code ret;
236 slave *s;
237 socklen_t addr_len;
238 krb5_ticket *ticket = NULL;
239 char hostname[128];
240
241 s = malloc(sizeof(*s));
242 if (s == NULL) {
243 krb5_warnx (context, "add_slave: no memory");
244 return;
245 }
246 s->name = NULL;
247 s->ac = NULL;
248
249 addr_len = sizeof(s->addr);
250 s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
251 if (rk_IS_BAD_SOCKET(s->fd)) {
252 krb5_warn (context, rk_SOCK_ERRNO, "accept");
253 goto error;
254 }
255 if (master_hostname)
256 strlcpy(hostname, master_hostname, sizeof(hostname));
257 else
258 gethostname(hostname, sizeof(hostname));
259
260 ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
261 KRB5_NT_SRV_HST, &server);
262 if (ret) {
263 krb5_warn (context, ret, "krb5_sname_to_principal");
264 goto error;
265 }
266
267 ret = krb5_recvauth (context, &s->ac, &s->fd,
268 IPROP_VERSION, server, 0, keytab, &ticket);
269 krb5_free_principal (context, server);
270 if (ret) {
271 krb5_warn (context, ret, "krb5_recvauth");
272 goto error;
273 }
274 ret = krb5_unparse_name (context, ticket->client, &s->name);
275 krb5_free_ticket (context, ticket);
276 if (ret) {
277 krb5_warn (context, ret, "krb5_unparse_name");
278 goto error;
279 }
280 if (check_acl (context, s->name)) {
281 krb5_warnx (context, "%s not in acl", s->name);
282 goto error;
283 }
284
285 {
286 slave *l = *root;
287
288 while (l) {
289 if (strcmp(l->name, s->name) == 0)
290 break;
291 l = l->next;
292 }
293 if (l) {
294 if (l->flags & SLAVE_F_DEAD) {
295 remove_slave(context, l, root);
296 } else {
297 krb5_warnx (context, "second connection from %s", s->name);
298 goto error;
299 }
300 }
301 }
302
303 krb5_warnx (context, "connection from %s", s->name);
304
305 s->version = 0;
306 s->flags = 0;
307 slave_seen(s);
308 s->next = *root;
309 *root = s;
310 return;
311 error:
312 remove_slave(context, s, root);
313 }
314
315 struct prop_context {
316 krb5_auth_context auth_context;
317 krb5_socket_t fd;
318 };
319
320 static int
prop_one(krb5_context context,HDB * db,hdb_entry_ex * entry,void * v)321 prop_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
322 {
323 krb5_error_code ret;
324 krb5_storage *sp;
325 krb5_data data;
326 struct slave *s = (struct slave *)v;
327
328 ret = hdb_entry2value (context, &entry->entry, &data);
329 if (ret)
330 return ret;
331 ret = krb5_data_realloc (&data, data.length + 4);
332 if (ret) {
333 krb5_data_free (&data);
334 return ret;
335 }
336 memmove ((char *)data.data + 4, data.data, data.length - 4);
337 sp = krb5_storage_from_data(&data);
338 if (sp == NULL) {
339 krb5_data_free (&data);
340 return ENOMEM;
341 }
342 krb5_store_int32(sp, ONE_PRINC);
343 krb5_storage_free(sp);
344
345 ret = krb5_write_priv_message (context, s->ac, &s->fd, &data);
346 krb5_data_free (&data);
347 return ret;
348 }
349
350 static int
send_complete(krb5_context context,slave * s,const char * database,uint32_t current_version)351 send_complete (krb5_context context, slave *s,
352 const char *database, uint32_t current_version)
353 {
354 krb5_error_code ret;
355 krb5_storage *sp;
356 HDB *db;
357 krb5_data data;
358 char buf[8];
359
360 ret = hdb_create (context, &db, database);
361 if (ret)
362 krb5_err (context, 1, ret, "hdb_create: %s", database);
363 ret = db->hdb_open (context, db, O_RDONLY, 0);
364 if (ret)
365 krb5_err (context, 1, ret, "db->open");
366
367 sp = krb5_storage_from_mem (buf, 4);
368 if (sp == NULL)
369 krb5_errx (context, 1, "krb5_storage_from_mem");
370 krb5_store_int32 (sp, TELL_YOU_EVERYTHING);
371 krb5_storage_free (sp);
372
373 data.data = buf;
374 data.length = 4;
375
376 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
377
378 if (ret) {
379 krb5_warn (context, ret, "krb5_write_priv_message");
380 slave_dead(context, s);
381 return ret;
382 }
383
384 ret = hdb_foreach (context, db, HDB_F_ADMIN_DATA, prop_one, s);
385 if (ret) {
386 krb5_warn (context, ret, "hdb_foreach");
387 slave_dead(context, s);
388 return ret;
389 }
390
391 (*db->hdb_close)(context, db);
392 (*db->hdb_destroy)(context, db);
393
394 sp = krb5_storage_from_mem (buf, 8);
395 if (sp == NULL)
396 krb5_errx (context, 1, "krb5_storage_from_mem");
397 krb5_store_int32 (sp, NOW_YOU_HAVE);
398 krb5_store_int32 (sp, current_version);
399 krb5_storage_free (sp);
400
401 data.length = 8;
402
403 s->version = current_version;
404
405 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
406 if (ret) {
407 slave_dead(context, s);
408 krb5_warn (context, ret, "krb5_write_priv_message");
409 return ret;
410 }
411
412 slave_seen(s);
413
414 return 0;
415 }
416
417 static int
send_are_you_there(krb5_context context,slave * s)418 send_are_you_there (krb5_context context, slave *s)
419 {
420 krb5_storage *sp;
421 krb5_data data;
422 char buf[4];
423 int ret;
424
425 if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
426 return 0;
427
428 krb5_warnx(context, "slave %s missing, sending AYT", s->name);
429
430 s->flags |= SLAVE_F_AYT;
431
432 data.data = buf;
433 data.length = 4;
434
435 sp = krb5_storage_from_mem (buf, 4);
436 if (sp == NULL) {
437 krb5_warnx (context, "are_you_there: krb5_data_alloc");
438 slave_dead(context, s);
439 return 1;
440 }
441 krb5_store_int32 (sp, ARE_YOU_THERE);
442 krb5_storage_free (sp);
443
444 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
445
446 if (ret) {
447 krb5_warn (context, ret, "are_you_there: krb5_write_priv_message");
448 slave_dead(context, s);
449 return 1;
450 }
451
452 return 0;
453 }
454
455 static int
send_diffs(krb5_context context,slave * s,int log_fd,const char * database,uint32_t current_version)456 send_diffs (krb5_context context, slave *s, int log_fd,
457 const char *database, uint32_t current_version)
458 {
459 krb5_storage *sp;
460 uint32_t ver;
461 time_t timestamp;
462 enum kadm_ops op;
463 uint32_t len;
464 off_t right, left;
465 krb5_data data;
466 int ret = 0;
467
468 if (s->version == current_version) {
469 krb5_warnx(context, "slave %s in sync already at version %ld",
470 s->name, (long)s->version);
471 return 0;
472 }
473
474 if (s->flags & SLAVE_F_DEAD)
475 return 0;
476
477 /* if slave is a fresh client, starting over */
478 if (s->version == 0) {
479 krb5_warnx(context, "sending complete log to fresh slave %s",
480 s->name);
481 return send_complete (context, s, database, current_version);
482 }
483
484 sp = kadm5_log_goto_end (log_fd);
485 right = krb5_storage_seek(sp, 0, SEEK_CUR);
486 for (;;) {
487 ret = kadm5_log_previous (context, sp, &ver, ×tamp, &op, &len);
488 if (ret)
489 krb5_err(context, 1, ret,
490 "send_diffs: failed to find previous entry");
491 left = krb5_storage_seek(sp, -16, SEEK_CUR);
492 if (ver == s->version)
493 return 0;
494 if (ver == s->version + 1)
495 break;
496 if (left == 0) {
497 krb5_storage_free(sp);
498 krb5_warnx(context,
499 "slave %s (version %lu) out of sync with master "
500 "(first version in log %lu), sending complete database",
501 s->name, (unsigned long)s->version, (unsigned long)ver);
502 return send_complete (context, s, database, current_version);
503 }
504 }
505
506 krb5_warnx(context,
507 "syncing slave %s from version %lu to version %lu",
508 s->name, (unsigned long)s->version,
509 (unsigned long)current_version);
510
511 ret = krb5_data_alloc (&data, right - left + 4);
512 if (ret) {
513 krb5_storage_free(sp);
514 krb5_warn (context, ret, "send_diffs: krb5_data_alloc");
515 slave_dead(context, s);
516 return 1;
517 }
518 krb5_storage_read (sp, (char *)data.data + 4, data.length - 4);
519 krb5_storage_free(sp);
520
521 sp = krb5_storage_from_data (&data);
522 if (sp == NULL) {
523 krb5_warnx (context, "send_diffs: krb5_storage_from_data");
524 slave_dead(context, s);
525 return 1;
526 }
527 krb5_store_int32 (sp, FOR_YOU);
528 krb5_storage_free(sp);
529
530 ret = krb5_write_priv_message(context, s->ac, &s->fd, &data);
531 krb5_data_free(&data);
532
533 if (ret) {
534 krb5_warn (context, ret, "send_diffs: krb5_write_priv_message");
535 slave_dead(context, s);
536 return 1;
537 }
538 slave_seen(s);
539
540 s->version = current_version;
541
542 return 0;
543 }
544
545 static int
process_msg(krb5_context context,slave * s,int log_fd,const char * database,uint32_t current_version)546 process_msg (krb5_context context, slave *s, int log_fd,
547 const char *database, uint32_t current_version)
548 {
549 int ret = 0;
550 krb5_data out;
551 krb5_storage *sp;
552 int32_t tmp;
553
554 ret = krb5_read_priv_message(context, s->ac, &s->fd, &out);
555 if(ret) {
556 krb5_warn (context, ret, "error reading message from %s", s->name);
557 return 1;
558 }
559
560 sp = krb5_storage_from_mem (out.data, out.length);
561 if (sp == NULL) {
562 krb5_warnx (context, "process_msg: no memory");
563 krb5_data_free (&out);
564 return 1;
565 }
566 if (krb5_ret_int32 (sp, &tmp) != 0) {
567 krb5_warnx (context, "process_msg: client send too short command");
568 krb5_data_free (&out);
569 return 1;
570 }
571 switch (tmp) {
572 case I_HAVE :
573 ret = krb5_ret_int32 (sp, &tmp);
574 if (ret != 0) {
575 krb5_warnx (context, "process_msg: client send too I_HAVE data");
576 break;
577 }
578 /* new started slave that have old log */
579 if (s->version == 0 && tmp != 0) {
580 if (current_version < (uint32_t)tmp) {
581 krb5_warnx (context, "Slave %s (version %lu) have later version "
582 "the master (version %lu) OUT OF SYNC",
583 s->name, (unsigned long)tmp,
584 (unsigned long)current_version);
585 }
586 s->version = tmp;
587 }
588 if ((uint32_t)tmp < s->version) {
589 krb5_warnx (context, "Slave claims to not have "
590 "version we already sent to it");
591 } else {
592 ret = send_diffs (context, s, log_fd, database, current_version);
593 }
594 break;
595 case I_AM_HERE :
596 break;
597 case ARE_YOU_THERE:
598 case FOR_YOU :
599 default :
600 krb5_warnx (context, "Ignoring command %d", tmp);
601 break;
602 }
603
604 krb5_data_free (&out);
605 krb5_storage_free (sp);
606
607 slave_seen(s);
608
609 return ret;
610 }
611
612 #define SLAVE_NAME "Name"
613 #define SLAVE_ADDRESS "Address"
614 #define SLAVE_VERSION "Version"
615 #define SLAVE_STATUS "Status"
616 #define SLAVE_SEEN "Last Seen"
617
618 static FILE *
open_stats(krb5_context context)619 open_stats(krb5_context context)
620 {
621 char *statfile = NULL;
622 const char *fn;
623 FILE *f;
624
625 if (slave_stats_file)
626 fn = slave_stats_file;
627 else {
628 asprintf(&statfile, "%s/slaves-stats", hdb_db_dir(context));
629 fn = krb5_config_get_string_default(context,
630 NULL,
631 statfile,
632 "kdc",
633 "iprop-stats",
634 NULL);
635 }
636 f = fopen(fn, "w");
637 if (statfile)
638 free(statfile);
639
640 return f;
641 }
642
643 static void
write_master_down(krb5_context context)644 write_master_down(krb5_context context)
645 {
646 char str[100];
647 time_t t = time(NULL);
648 FILE *fp;
649
650 fp = open_stats(context);
651 if (fp == NULL)
652 return;
653 krb5_format_time(context, t, str, sizeof(str), TRUE);
654 fprintf(fp, "master down at %s\n", str);
655
656 fclose(fp);
657 }
658
659 static void
write_stats(krb5_context context,slave * slaves,uint32_t current_version)660 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
661 {
662 char str[100];
663 rtbl_t tbl;
664 time_t t = time(NULL);
665 FILE *fp;
666
667 fp = open_stats(context);
668 if (fp == NULL)
669 return;
670
671 krb5_format_time(context, t, str, sizeof(str), TRUE);
672 fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
673
674 fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
675
676 tbl = rtbl_create();
677 if (tbl == NULL) {
678 fclose(fp);
679 return;
680 }
681
682 rtbl_add_column(tbl, SLAVE_NAME, 0);
683 rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
684 rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
685 rtbl_add_column(tbl, SLAVE_STATUS, 0);
686 rtbl_add_column(tbl, SLAVE_SEEN, 0);
687
688 rtbl_set_prefix(tbl, " ");
689 rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
690
691 while (slaves) {
692 krb5_address addr;
693 krb5_error_code ret;
694 rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
695 ret = krb5_sockaddr2address (context,
696 (struct sockaddr*)&slaves->addr, &addr);
697 if(ret == 0) {
698 krb5_print_address(&addr, str, sizeof(str), NULL);
699 krb5_free_address(context, &addr);
700 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
701 } else
702 rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
703
704 snprintf(str, sizeof(str), "%u", (unsigned)slaves->version);
705 rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
706
707 if (slaves->flags & SLAVE_F_DEAD)
708 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
709 else
710 rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
711
712 ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
713 rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
714
715 slaves = slaves->next;
716 }
717
718 rtbl_format(tbl, fp);
719 rtbl_destroy(tbl);
720
721 fclose(fp);
722 }
723
724
725 static char sHDB[] = "HDB:";
726 static char *realm;
727 static int version_flag;
728 static int help_flag;
729 static char *keytab_str = sHDB;
730 static char *database;
731 static char *config_file;
732 static char *port_str;
733 #ifdef SUPPORT_DETACH
734 static int detach_from_console = 0;
735 #endif
736
737 static struct getargs args[] = {
738 { "config-file", 'c', arg_string, &config_file, NULL, NULL },
739 { "realm", 'r', arg_string, &realm, NULL, NULL },
740 { "keytab", 'k', arg_string, &keytab_str,
741 "keytab to get authentication from", "kspec" },
742 { "database", 'd', arg_string, &database, "database", "file"},
743 { "slave-stats-file", 0, arg_string, rk_UNCONST(&slave_stats_file),
744 "file for slave status information", "file"},
745 { "time-missing", 0, arg_string, rk_UNCONST(&slave_time_missing),
746 "time before slave is polled for presence", "time"},
747 { "time-gone", 0, arg_string, rk_UNCONST(&slave_time_gone),
748 "time of inactivity after which a slave is considered gone", "time"},
749 { "port", 0, arg_string, &port_str,
750 "port ipropd will listen to", "port"},
751 #ifdef SUPPORT_DETACH
752 { "detach", 0, arg_flag, &detach_from_console,
753 "detach from console", NULL },
754 #endif
755 { "hostname", 0, arg_string, rk_UNCONST(&master_hostname),
756 "hostname of master (if not same as hostname)", "hostname" },
757 { "version", 0, arg_flag, &version_flag, NULL, NULL },
758 { "help", 0, arg_flag, &help_flag, NULL, NULL }
759 };
760 static int num_args = sizeof(args) / sizeof(args[0]);
761
762 int
main(int argc,char ** argv)763 main(int argc, char **argv)
764 {
765 krb5_error_code ret;
766 krb5_context context;
767 void *kadm_handle;
768 kadm5_server_context *server_context;
769 kadm5_config_params conf;
770 krb5_socket_t signal_fd, listen_fd;
771 int log_fd;
772 slave *slaves = NULL;
773 uint32_t current_version = 0, old_version = 0;
774 krb5_keytab keytab;
775 int optidx;
776 char **files;
777
778 optidx = krb5_program_setup(&context, argc, argv, args, num_args, NULL);
779
780 if(help_flag)
781 krb5_std_usage(0, args, num_args);
782 if(version_flag) {
783 print_version(NULL);
784 exit(0);
785 }
786
787 setup_signal();
788
789 if (config_file == NULL) {
790 asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
791 if (config_file == NULL)
792 errx(1, "out of memory");
793 }
794
795 ret = krb5_prepend_config_files_default(config_file, &files);
796 if (ret)
797 krb5_err(context, 1, ret, "getting configuration files");
798
799 ret = krb5_set_config_files(context, files);
800 krb5_free_config_files(files);
801 if (ret)
802 krb5_err(context, 1, ret, "reading configuration files");
803
804 time_before_gone = parse_time (slave_time_gone, "s");
805 if (time_before_gone < 0)
806 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
807 time_before_missing = parse_time (slave_time_missing, "s");
808 if (time_before_missing < 0)
809 krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
810
811 #ifdef SUPPORT_DETACH
812 if (detach_from_console)
813 daemon(0, 0);
814 #endif
815 pidfile (NULL);
816 krb5_openlog (context, "ipropd-master", &log_facility);
817 krb5_set_warn_dest(context, log_facility);
818
819 ret = krb5_kt_register(context, &hdb_kt_ops);
820 if(ret)
821 krb5_err(context, 1, ret, "krb5_kt_register");
822
823 ret = krb5_kt_resolve(context, keytab_str, &keytab);
824 if(ret)
825 krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
826
827 memset(&conf, 0, sizeof(conf));
828 if(realm) {
829 conf.mask |= KADM5_CONFIG_REALM;
830 conf.realm = realm;
831 }
832 ret = kadm5_init_with_skey_ctx (context,
833 KADM5_ADMIN_SERVICE,
834 NULL,
835 KADM5_ADMIN_SERVICE,
836 &conf, 0, 0,
837 &kadm_handle);
838 if (ret)
839 krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
840
841 server_context = (kadm5_server_context *)kadm_handle;
842
843 log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
844 if (log_fd < 0)
845 krb5_err (context, 1, errno, "open %s",
846 server_context->log_context.log_file);
847
848 signal_fd = make_signal_socket (context);
849 listen_fd = make_listen_socket (context, port_str);
850
851 kadm5_log_get_version_fd (log_fd, ¤t_version);
852
853 krb5_warnx(context, "ipropd-master started at version: %lu",
854 (unsigned long)current_version);
855
856 while(exit_flag == 0){
857 slave *p;
858 fd_set readset;
859 int max_fd = 0;
860 struct timeval to = {30, 0};
861 uint32_t vers;
862
863 #ifndef NO_LIMIT_FD_SETSIZE
864 if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE)
865 krb5_errx (context, 1, "fd too large");
866 #endif
867
868 FD_ZERO(&readset);
869 FD_SET(signal_fd, &readset);
870 max_fd = max(max_fd, signal_fd);
871 FD_SET(listen_fd, &readset);
872 max_fd = max(max_fd, listen_fd);
873
874 for (p = slaves; p != NULL; p = p->next) {
875 if (p->flags & SLAVE_F_DEAD)
876 continue;
877 FD_SET(p->fd, &readset);
878 max_fd = max(max_fd, p->fd);
879 }
880
881 ret = select (max_fd + 1,
882 &readset, NULL, NULL, &to);
883 if (ret < 0) {
884 if (errno == EINTR)
885 continue;
886 else
887 krb5_err (context, 1, errno, "select");
888 }
889
890 if (ret == 0) {
891 old_version = current_version;
892 kadm5_log_get_version_fd (log_fd, ¤t_version);
893
894 if (current_version > old_version) {
895 krb5_warnx(context,
896 "Missed a signal, updating slaves %lu to %lu",
897 (unsigned long)old_version,
898 (unsigned long)current_version);
899 for (p = slaves; p != NULL; p = p->next) {
900 if (p->flags & SLAVE_F_DEAD)
901 continue;
902 send_diffs (context, p, log_fd, database, current_version);
903 }
904 }
905 }
906
907 if (ret && FD_ISSET(signal_fd, &readset)) {
908 #ifndef NO_UNIX_SOCKETS
909 struct sockaddr_un peer_addr;
910 #else
911 struct sockaddr_storage peer_addr;
912 #endif
913 socklen_t peer_len = sizeof(peer_addr);
914
915 if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
916 (struct sockaddr *)&peer_addr, &peer_len) < 0) {
917 krb5_warn (context, errno, "recvfrom");
918 continue;
919 }
920 --ret;
921 assert(ret >= 0);
922 old_version = current_version;
923 kadm5_log_get_version_fd (log_fd, ¤t_version);
924 if (current_version > old_version) {
925 krb5_warnx(context,
926 "Got a signal, updating slaves %lu to %lu",
927 (unsigned long)old_version,
928 (unsigned long)current_version);
929 for (p = slaves; p != NULL; p = p->next) {
930 if (p->flags & SLAVE_F_DEAD)
931 continue;
932 send_diffs (context, p, log_fd, database, current_version);
933 }
934 } else {
935 krb5_warnx(context,
936 "Got a signal, but no update in log version %lu",
937 (unsigned long)current_version);
938 }
939 }
940
941 for(p = slaves; p != NULL; p = p->next) {
942 if (p->flags & SLAVE_F_DEAD)
943 continue;
944 if (ret && FD_ISSET(p->fd, &readset)) {
945 --ret;
946 assert(ret >= 0);
947 if(process_msg (context, p, log_fd, database, current_version))
948 slave_dead(context, p);
949 } else if (slave_gone_p (p))
950 slave_dead(context, p);
951 else if (slave_missing_p (p))
952 send_are_you_there (context, p);
953 }
954
955 if (ret && FD_ISSET(listen_fd, &readset)) {
956 add_slave (context, keytab, &slaves, listen_fd);
957 --ret;
958 assert(ret >= 0);
959 }
960 write_stats(context, slaves, current_version);
961 }
962
963 if(exit_flag == SIGINT || exit_flag == SIGTERM)
964 krb5_warnx(context, "%s terminated", getprogname());
965 #ifdef SIGXCPU
966 else if(exit_flag == SIGXCPU)
967 krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
968 #endif
969 else
970 krb5_warnx(context, "%s unexpected exit reason: %ld",
971 getprogname(), (long)exit_flag);
972
973 write_master_down(context);
974
975 return 0;
976 }
977