HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / kadm5 / ipropd_master.c
1 /*
2  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
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.
16  *
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.
20  *
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.
32  */
33
34 #include "iprop.h"
35 #include <rtbl.h>
36
37 static krb5_log_facility *log_facility;
38
39 static int verbose;
40
41 static const char *slave_stats_file;
42 static const char *slave_stats_temp_file;
43 static const char *slave_time_missing = "2 min";
44 static const char *slave_time_gone = "5 min";
45
46 static int time_before_missing;
47 static int time_before_gone;
48
49 const char *master_hostname;
50 const char *pidfile_basename;
51
52 static krb5_socket_t
53 make_signal_socket (krb5_context context)
54 {
55 #ifndef NO_UNIX_SOCKETS
56     struct sockaddr_un addr;
57     const char *fn;
58     krb5_socket_t fd;
59
60     fn = kadm5_log_signal_socket(context);
61
62     fd = socket (AF_UNIX, SOCK_DGRAM, 0);
63     if (fd < 0)
64         krb5_err (context, 1, errno, "socket AF_UNIX");
65     memset (&addr, 0, sizeof(addr));
66     addr.sun_family = AF_UNIX;
67     strlcpy (addr.sun_path, fn, sizeof(addr.sun_path));
68     unlink (addr.sun_path);
69     if (bind (fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
70         krb5_err (context, 1, errno, "bind %s", addr.sun_path);
71     return fd;
72 #else
73     struct addrinfo *ai = NULL;
74     krb5_socket_t fd;
75
76     kadm5_log_signal_socket_info(context, 1, &ai);
77
78     fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
79     if (rk_IS_BAD_SOCKET(fd))
80         krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF=%d", ai->ai_family);
81
82     if (rk_IS_SOCKET_ERROR( bind (fd, ai->ai_addr, ai->ai_addrlen) ))
83         krb5_err (context, 1, rk_SOCK_ERRNO, "bind");
84     return fd;
85 #endif
86 }
87
88 static krb5_socket_t
89 make_listen_socket (krb5_context context, const char *port_str)
90 {
91     krb5_socket_t fd;
92     int one = 1;
93     struct sockaddr_in addr;
94
95     fd = socket (AF_INET, SOCK_STREAM, 0);
96     if (rk_IS_BAD_SOCKET(fd))
97         krb5_err (context, 1, rk_SOCK_ERRNO, "socket AF_INET");
98     setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one));
99     memset (&addr, 0, sizeof(addr));
100     addr.sin_family = AF_INET;
101
102     if (port_str) {
103         addr.sin_port = krb5_getportbyname (context,
104                                               port_str, "tcp",
105                                               0);
106         if (addr.sin_port == 0) {
107             char *ptr;
108             long port;
109
110             port = strtol (port_str, &ptr, 10);
111             if (port == 0 && ptr == port_str)
112                 krb5_errx (context, 1, "bad port `%s'", port_str);
113             addr.sin_port = htons(port);
114         }
115     } else {
116         addr.sin_port = krb5_getportbyname (context, IPROP_SERVICE,
117                                             "tcp", IPROP_PORT);
118     }
119     if(bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
120         krb5_err (context, 1, errno, "bind");
121     if (listen(fd, SOMAXCONN) < 0)
122         krb5_err (context, 1, errno, "listen");
123     return fd;
124 }
125
126
127 struct slave {
128     krb5_socket_t fd;
129     struct sockaddr_in addr;
130     char *name;
131     krb5_auth_context ac;
132     uint32_t version;
133     uint32_t version_tstamp;
134     uint32_t version_ack;
135     time_t seen;
136     unsigned long flags;
137 #define SLAVE_F_DEAD    0x1
138 #define SLAVE_F_AYT     0x2
139 #define SLAVE_F_READY   0x4
140     /*
141      * We'll use non-blocking I/O so no slave can hold us back.
142      *
143      * We call the state left over from a partial write a "tail".
144      *
145      * The krb5_data holding an KRB-PRIV will be the write buffer.
146      */
147     struct {
148         /* Every message we send is a KRB-PRIV with a 4-byte length prefixed */
149         uint8_t         header_buf[4];
150         krb5_data       header;
151         krb5_data       packet;
152         size_t          packet_off;
153         /* For send_complete() we need an sp as part of the tail */
154         krb5_storage    *dump;
155         uint32_t        vno;
156     } tail;
157     struct {
158         uint8_t         header_buf[4];
159         krb5_data       packet;
160         size_t          offset;
161         int             hlen;
162     } input;
163     /*
164      * Continuation for fair diff sending we send N entries at a time.
165      */
166     struct {
167         off_t       off_next_version;   /* offset in log of next diff */
168         uint32_t    initial_version;    /* at time of previous diff */
169         uint32_t    initial_tstamp;     /* at time of previous diff */
170         uint32_t    last_version_sent;
171         int         more;               /* need to send more diffs */
172     } next_diff;
173     struct slave *next;
174 };
175
176 typedef struct slave slave;
177
178 static int
179 check_acl (krb5_context context, const char *name)
180 {
181     const char *fn;
182     FILE *fp;
183     char buf[256];
184     int ret = 1;
185     char *slavefile = NULL;
186
187     if (asprintf(&slavefile, "%s/slaves", hdb_db_dir(context)) == -1
188         || slavefile == NULL)
189         errx(1, "out of memory");
190
191     fn = krb5_config_get_string_default(context,
192                                         NULL,
193                                         slavefile,
194                                         "kdc",
195                                         "iprop-acl",
196                                         NULL);
197
198     fp = fopen (fn, "r");
199     free(slavefile);
200     if (fp == NULL)
201         return 1;
202     while (fgets(buf, sizeof(buf), fp) != NULL) {
203         buf[strcspn(buf, "\r\n")] = '\0';
204         if (strcmp (buf, name) == 0) {
205             ret = 0;
206             break;
207         }
208     }
209     fclose (fp);
210     return ret;
211 }
212
213 static void
214 slave_seen(slave *s)
215 {
216     s->flags &= ~SLAVE_F_AYT;
217     s->seen = time(NULL);
218 }
219
220 static int
221 slave_missing_p (slave *s)
222 {
223     if (time(NULL) > s->seen + time_before_missing)
224         return 1;
225     return 0;
226 }
227
228 static int
229 slave_gone_p (slave *s)
230 {
231     if (time(NULL) > s->seen + time_before_gone)
232         return 1;
233     return 0;
234 }
235
236 static void
237 slave_dead(krb5_context context, slave *s)
238 {
239     krb5_warnx(context, "slave %s dead", s->name);
240
241     if (!rk_IS_BAD_SOCKET(s->fd)) {
242         rk_closesocket (s->fd);
243         s->fd = rk_INVALID_SOCKET;
244     }
245     s->flags |= SLAVE_F_DEAD;
246     slave_seen(s);
247 }
248
249 static void
250 remove_slave (krb5_context context, slave *s, slave **root)
251 {
252     slave **p;
253
254     if (!rk_IS_BAD_SOCKET(s->fd))
255         rk_closesocket (s->fd);
256     if (s->name)
257         free (s->name);
258     if (s->ac)
259         krb5_auth_con_free (context, s->ac);
260
261     /* Free any pending input/output state */
262     krb5_data_free(&s->input.packet);
263     krb5_data_free(&s->tail.packet);
264     krb5_storage_free(s->tail.dump);
265
266     for (p = root; *p; p = &(*p)->next)
267         if (*p == s) {
268             *p = s->next;
269             break;
270         }
271     free (s);
272 }
273
274 static void
275 add_slave (krb5_context context, krb5_keytab keytab, slave **root,
276            krb5_socket_t fd)
277 {
278     krb5_principal server;
279     krb5_error_code ret;
280     slave *s;
281     socklen_t addr_len;
282     krb5_ticket *ticket = NULL;
283     char hostname[128];
284
285     s = calloc(1, sizeof(*s));
286     if (s == NULL) {
287         krb5_warnx (context, "add_slave: no memory");
288         return;
289     }
290     s->name = NULL;
291     s->ac = NULL;
292     s->input.packet.data = NULL;
293     s->tail.header.data = NULL;
294     s->tail.packet.data = NULL;
295     s->tail.dump = NULL;
296
297     addr_len = sizeof(s->addr);
298     s->fd = accept (fd, (struct sockaddr *)&s->addr, &addr_len);
299     if (rk_IS_BAD_SOCKET(s->fd)) {
300         krb5_warn (context, rk_SOCK_ERRNO, "accept");
301         goto error;
302     }
303
304     if (master_hostname)
305         strlcpy(hostname, master_hostname, sizeof(hostname));
306     else
307         gethostname(hostname, sizeof(hostname));
308
309     ret = krb5_sname_to_principal (context, hostname, IPROP_NAME,
310                                    KRB5_NT_SRV_HST, &server);
311     if (ret) {
312         krb5_warn (context, ret, "krb5_sname_to_principal");
313         goto error;
314     }
315
316     ret = krb5_recvauth (context, &s->ac, &s->fd,
317                          IPROP_VERSION, server, 0, keytab, &ticket);
318
319     /*
320      * We'll be doing non-blocking I/O only after authentication.  We don't
321      * want to get stuck talking to any one slave.
322      *
323      * If we get a partial write, we'll finish writing when the socket becomes
324      * writable.
325      *
326      * Partial reads will be treated as EOF, causing the slave to be marked
327      * dead.
328      *
329      * To do non-blocking I/O for authentication we'll have to implement our
330      * own krb5_recvauth().
331      */
332     socket_set_nonblocking(s->fd, 1);
333
334     /*
335      * We write message lengths separately from the payload, and may do
336      * back-to-back small writes when flushing pending input and then a new
337      * update.  Avoid Nagle delays.
338      */
339 #if defined(IPPROTO_TCP) && defined(TCP_NODELAY)
340     {
341         int nodelay = 1;
342         (void) setsockopt(s->fd, IPPROTO_TCP, TCP_NODELAY,
343                           (void *)&nodelay, sizeof(nodelay));
344     }
345 #endif
346
347     krb5_free_principal (context, server);
348     if (ret) {
349         krb5_warn (context, ret, "krb5_recvauth");
350         goto error;
351     }
352     ret = krb5_unparse_name (context, ticket->client, &s->name);
353     krb5_free_ticket (context, ticket);
354     if (ret) {
355         krb5_warn (context, ret, "krb5_unparse_name");
356         goto error;
357     }
358     if (check_acl (context, s->name)) {
359         krb5_warnx (context, "%s not in acl", s->name);
360         goto error;
361     }
362
363     {
364         slave *l = *root;
365
366         while (l) {
367             if (strcmp(l->name, s->name) == 0)
368                 break;
369             l = l->next;
370         }
371         if (l) {
372             if (l->flags & SLAVE_F_DEAD) {
373                 remove_slave(context, l, root);
374             } else {
375                 krb5_warnx (context, "second connection from %s", s->name);
376                 goto error;
377             }
378         }
379     }
380
381     krb5_warnx (context, "connection from %s", s->name);
382
383     s->version = 0;
384     s->version_ack = 0;
385     s->flags = 0;
386     slave_seen(s);
387     s->next = *root;
388     *root = s;
389     return;
390 error:
391     remove_slave(context, s, root);
392 }
393
394 static int
395 dump_one (krb5_context context, HDB *db, hdb_entry_ex *entry, void *v)
396 {
397     krb5_error_code ret;
398     krb5_storage *dump = (krb5_storage *)v;
399     krb5_storage *sp;
400     krb5_data data;
401
402     ret = hdb_entry2value (context, &entry->entry, &data);
403     if (ret)
404         return ret;
405     ret = krb5_data_realloc (&data, data.length + 4);
406     if (ret)
407         goto done;
408     memmove ((char *)data.data + 4, data.data, data.length - 4);
409     sp = krb5_storage_from_data(&data);
410     if (sp == NULL) {
411         ret = krb5_enomem(context);
412         goto done;
413     }
414     ret = krb5_store_uint32(sp, ONE_PRINC);
415     krb5_storage_free(sp);
416
417     if (ret == 0)
418         ret = krb5_store_data(dump, data);
419
420 done:
421     krb5_data_free (&data);
422     return ret;
423 }
424
425 static int
426 write_dump (krb5_context context, krb5_storage *dump,
427             const char *database, uint32_t current_version)
428 {
429     krb5_error_code ret;
430     krb5_storage *sp;
431     HDB *db;
432     krb5_data data;
433     char buf[8];
434
435     /* we assume that the caller has obtained an exclusive lock */
436
437     ret = krb5_storage_truncate(dump, 0);
438     if (ret)
439         return ret;
440
441     if (krb5_storage_seek(dump, 0, SEEK_SET) != 0)
442         return errno;
443
444     /*
445      * First we store zero as the HDB version, this will indicate to a
446      * later reader that the dumpfile is invalid.  We later write the
447      * correct version in the file after we have written all of the
448      * messages.  A dump with a zero version will not be considered
449      * to be valid.
450      */
451
452     ret = krb5_store_uint32(dump, 0);
453
454     ret = hdb_create (context, &db, database);
455     if (ret)
456         krb5_err (context, IPROPD_RESTART, ret, "hdb_create: %s", database);
457     ret = db->hdb_open (context, db, O_RDONLY, 0);
458     if (ret)
459         krb5_err (context, IPROPD_RESTART, ret, "db->open");
460
461     sp = krb5_storage_from_mem (buf, 4);
462     if (sp == NULL)
463         krb5_errx (context, IPROPD_RESTART, "krb5_storage_from_mem");
464     krb5_store_uint32 (sp, TELL_YOU_EVERYTHING);
465     krb5_storage_free (sp);
466
467     data.data   = buf;
468     data.length = 4;
469
470     ret = krb5_store_data(dump, data);
471     if (ret) {
472         krb5_warn (context, ret, "write_dump");
473         return ret;
474     }
475
476     ret = hdb_foreach (context, db, HDB_F_ADMIN_DATA, dump_one, dump);
477     if (ret) {
478         krb5_warn (context, ret, "write_dump: hdb_foreach");
479         return ret;
480     }
481
482     (*db->hdb_close)(context, db);
483     (*db->hdb_destroy)(context, db);
484
485     sp = krb5_storage_from_mem (buf, 8);
486     if (sp == NULL)
487         krb5_errx (context, IPROPD_RESTART, "krb5_storage_from_mem");
488     ret = krb5_store_uint32(sp, NOW_YOU_HAVE);
489     if (ret == 0)
490       krb5_store_uint32(sp, current_version);
491     krb5_storage_free (sp);
492
493     data.length = 8;
494
495     if (ret == 0)
496         ret = krb5_store_data(dump, data);
497
498     /*
499      * We must ensure that the entire valid dump is written to disk
500      * before we write the current version at the front thus making
501      * it a valid dump file.  If we crash around here, this can be
502      * important upon reboot.
503      */
504
505     if (ret == 0)
506         ret = krb5_storage_fsync(dump);
507
508     if (ret == 0 && krb5_storage_seek(dump, 0, SEEK_SET) == -1)
509         ret = errno;
510
511     /* Write current version at the front making the dump valid */
512
513     if (ret == 0)
514         ret = krb5_store_uint32(dump, current_version);
515
516     /*
517      * We don't need to fsync(2) after the real version is written as
518      * it is not a disaster if it doesn't make it to disk if we crash.
519      * After all, we'll just create a new dumpfile.
520      */
521
522     if (ret == 0)
523         krb5_warnx(context, "wrote new dumpfile (version %u)",
524                    current_version);
525     else
526         krb5_warn(context, ret, "failed to write new dumpfile (version %u)",
527                   current_version);
528
529     return ret;
530 }
531
532 static int
533 mk_priv_tail(krb5_context context, slave *s, krb5_data *data)
534 {
535     uint32_t len;
536     int ret;
537
538     ret = krb5_mk_priv(context, s->ac, data, &s->tail.packet, NULL);
539     if (ret)
540         return ret;
541
542     len = s->tail.packet.length;
543     _krb5_put_int(s->tail.header_buf, len, sizeof(s->tail.header_buf));
544     s->tail.header.length = sizeof(s->tail.header_buf);
545     s->tail.header.data = s->tail.header_buf;
546     return 0;
547 }
548
549 static int
550 have_tail(slave *s)
551 {
552     return s->tail.header.length || s->tail.packet.length || s->tail.dump;
553 }
554
555 static int
556 more_diffs(slave *s)
557 {
558     return s->next_diff.more;
559 }
560
561 #define SEND_COMPLETE_MAX_RECORDS 50
562 #define SEND_DIFFS_MAX_RECORDS 50
563
564 static int
565 send_tail(krb5_context context, slave *s)
566 {
567     krb5_data data;
568     ssize_t bytes = 0;
569     size_t rem = 0;
570     size_t n;
571     int ret;
572
573     if (! have_tail(s))
574         return 0;
575
576     /*
577      * For the case where we're continuing a send_complete() send up to
578      * SEND_COMPLETE_MAX_RECORDS records now, and the rest asynchronously
579      * later.  This ensures that sending a complete dump to a slow-to-drain
580      * client does not prevent others from getting serviced.
581      */
582     for (n = 0; n < SEND_COMPLETE_MAX_RECORDS; n++) {
583         if (! have_tail(s))
584             return 0;
585
586         if (s->tail.header.length) {
587             bytes = krb5_net_write(context, &s->fd,
588                                    s->tail.header.data,
589                                    s->tail.header.length);
590             if (bytes < 0)
591                 goto err;
592
593             s->tail.header.length -= bytes;
594             s->tail.header.data = (char *)s->tail.header.data + bytes;
595             rem = s->tail.header.length;
596             if (rem)
597                 goto ewouldblock;
598         }
599
600         if (s->tail.packet.length) {
601             bytes = krb5_net_write(context, &s->fd,
602                                    (char *)s->tail.packet.data + s->tail.packet_off,
603                                    s->tail.packet.length - s->tail.packet_off);
604             if (bytes < 0)
605                 goto err;
606             s->tail.packet_off += bytes;
607             if (bytes)
608                 slave_seen(s);
609             rem = s->tail.packet.length - s->tail.packet_off;
610             if (rem)
611                 goto ewouldblock;
612
613             krb5_data_free(&s->tail.packet);
614             s->tail.packet_off = 0;
615         }
616
617         if (s->tail.dump == NULL)
618             return 0;
619
620         /*
621          * We're in the middle of a send_complete() that was interrupted by
622          * EWOULDBLOCK.  Continue the sending of the dump.
623          */
624         ret = krb5_ret_data(s->tail.dump, &data);
625         if (ret == HEIM_ERR_EOF) {
626             krb5_storage_free(s->tail.dump);
627             s->tail.dump = NULL;
628             s->version = s->tail.vno;
629             return 0;
630         }
631
632         if (ret) {
633             krb5_warn(context, ret, "failed to read entry from dump!");
634         } else {
635             ret = mk_priv_tail(context, s, &data);
636             krb5_data_free(&data);
637             if (ret == 0)
638                 continue;
639             krb5_warn(context, ret, "failed to make and send a KRB-PRIV to %s",
640                       s->name);
641         }
642
643         slave_dead(context, s);
644         return ret;
645     }
646
647     if (ret == 0 && s->tail.dump != NULL)
648         return EWOULDBLOCK;
649
650 err:
651     if (errno != EAGAIN && errno != EWOULDBLOCK) {
652         krb5_warn(context, ret = errno,
653                   "error sending diffs to now-dead slave %s", s->name);
654         slave_dead(context, s);
655         return ret;
656     }
657
658 ewouldblock:
659     if (verbose)
660         krb5_warnx(context, "would block writing %llu bytes to slave %s",
661                    (unsigned long long)rem, s->name);
662     return EWOULDBLOCK;
663 }
664
665 static int
666 send_complete(krb5_context context, slave *s, const char *database,
667               uint32_t current_version, uint32_t oldest_version,
668               uint32_t initial_log_tstamp)
669 {
670     krb5_error_code ret;
671     krb5_storage *dump = NULL;
672     uint32_t vno = 0;
673     int fd = -1;
674     struct stat st;
675     char *dfn;
676
677     ret = asprintf(&dfn, "%s/ipropd.dumpfile", hdb_db_dir(context));
678     if (ret == -1 || !dfn)
679         return krb5_enomem(context);
680
681     fd = open(dfn, O_CREAT|O_RDWR, 0600);
682     if (fd == -1) {
683         ret = errno;
684         krb5_warn(context, ret, "Cannot open/create iprop dumpfile %s", dfn);
685         free(dfn);
686         return ret;
687     }
688     free(dfn);
689
690     dump = krb5_storage_from_fd(fd);
691     if (!dump) {
692         ret = errno;
693         krb5_warn(context, ret, "krb5_storage_from_fd");
694         goto done;
695     }
696
697     for (;;) {
698         ret = flock(fd, LOCK_SH);
699         if (ret == -1) {
700             ret = errno;
701             krb5_warn(context, ret, "flock(fd, LOCK_SH)");
702             goto done;
703         }
704
705         if (krb5_storage_seek(dump, 0, SEEK_SET) == (off_t)-1) {
706             ret = errno;
707             krb5_warn(context, ret, "krb5_storage_seek(dump, 0, SEEK_SET)");
708             goto done;
709         }
710
711         vno = 0;
712         ret = krb5_ret_uint32(dump, &vno);
713         if (ret && ret != HEIM_ERR_EOF) {
714             krb5_warn(context, ret, "krb5_ret_uint32(dump, &vno)");
715             goto done;
716         }
717
718         if (fstat(fd, &st) == -1) {
719             ret = errno;
720             krb5_warn(context, ret, "send_complete: could not stat dump file");
721             goto done;
722         }
723
724         /*
725          * If the current dump has an appropriate version, then we can
726          * break out of the loop and send the file below.
727          */
728         if (ret == 0 && vno != 0 && st.st_mtime > initial_log_tstamp &&
729             vno >= oldest_version && vno <= current_version)
730             break;
731
732         if (verbose)
733             krb5_warnx(context, "send_complete: dumping HDB");
734
735         /*
736          * Otherwise, we may need to write a new dump file.  We
737          * obtain an exclusive lock on the fd.  Because this is
738          * not guaranteed to be an upgrade of our existing shared
739          * lock, someone else may have written a new dumpfile while
740          * we were waiting and so we must first check the vno of
741          * the dump to see if that happened.  If it did, we need
742          * to go back to the top of the loop so that we can downgrade
743          * our lock to a shared one.
744          */
745
746         ret = flock(fd, LOCK_EX);
747         if (ret == -1) {
748             ret = errno;
749             krb5_warn(context, ret, "flock(fd, LOCK_EX)");
750             goto done;
751         }
752
753         ret = krb5_storage_seek(dump, 0, SEEK_SET);
754         if (ret == -1) {
755             ret = errno;
756             krb5_warn(context, ret, "krb5_storage_seek(dump, 0, SEEK_SET)");
757             goto done;
758         }
759
760         vno = 0;
761         ret = krb5_ret_uint32(dump, &vno);
762         if (ret && ret != HEIM_ERR_EOF) {
763             krb5_warn(context, ret, "krb5_ret_uint32(dump, &vno)");
764             goto done;
765         }
766
767         if (fstat(fd, &st) == -1) {
768             ret = errno;
769             krb5_warn(context, ret, "send_complete: could not stat dump file");
770             goto done;
771         }
772
773         /* check if someone wrote a better version for us */
774         if (ret == 0 && vno != 0 && st.st_mtime > initial_log_tstamp &&
775             vno >= oldest_version && vno <= current_version)
776             continue;
777
778         /* Now, we know that we must write a new dump file.  */
779
780         ret = write_dump(context, dump, database, current_version);
781         if (ret)
782             goto done;
783
784         /*
785          * And we must continue to the top of the loop so that we can
786          * downgrade to a shared lock.
787          */
788     }
789
790     /*
791      * Leaving the above loop, dump should have a ptr right after the initial
792      * 4 byte DB version number and we should have a shared lock on the file
793      * (which we may have just created), so we are reading to start sending
794      * the data down the wire.
795      *
796      * Note: (krb5_storage_from_fd() dup()'s the fd)
797      */
798
799     s->tail.dump = dump;
800     s->tail.vno = vno;
801     dump = NULL;
802     ret = send_tail(context, s);
803
804 done:
805     if (fd != -1)
806         close(fd);
807     if (dump)
808         krb5_storage_free(dump);
809     return ret;
810 }
811
812 static int
813 send_are_you_there (krb5_context context, slave *s)
814 {
815     krb5_storage *sp;
816     krb5_data data;
817     char buf[4];
818     int ret;
819
820     if (s->flags & (SLAVE_F_DEAD|SLAVE_F_AYT))
821         return 0;
822
823     /*
824      * Write any remainder of previous write, if we can.  If we'd block we'll
825      * return EWOULDBLOCK.
826      */
827     ret = send_tail(context, s);
828     if (ret)
829         return ret;
830
831     krb5_warnx(context, "slave %s missing, sending AYT", s->name);
832
833     s->flags |= SLAVE_F_AYT;
834
835     data.data = buf;
836     data.length = 4;
837
838     sp = krb5_storage_from_mem (buf, 4);
839     if (sp == NULL) {
840         krb5_warnx (context, "are_you_there: krb5_data_alloc");
841         slave_dead(context, s);
842         return ENOMEM;
843     }
844     ret = krb5_store_uint32(sp, ARE_YOU_THERE);
845     krb5_storage_free (sp);
846
847     if (ret == 0)
848         ret = mk_priv_tail(context, s, &data);
849     if (ret == 0)
850         ret = send_tail(context, s);
851     if (ret && ret != EWOULDBLOCK) {
852         krb5_warn(context, ret, "are_you_there");
853         slave_dead(context, s);
854     }
855     return ret;
856 }
857
858 static int
859 diffready(krb5_context context, slave *s)
860 {
861     /*
862      * Don't send any diffs until slave has sent an I_HAVE telling us the
863      * initial version number!
864      */
865     if ((s->flags & SLAVE_F_READY) == 0)
866         return 0;
867
868     if (s->flags & SLAVE_F_DEAD) {
869         if (verbose)
870             krb5_warnx(context, "not sending diffs to dead slave %s", s->name);
871         return 0;
872     }
873
874     /* Write any remainder of previous write, if we can. */
875     if (send_tail(context, s) != 0)
876         return 0;
877
878     return 1;
879 }
880
881 static int
882 nodiffs(krb5_context context, slave *s, uint32_t current_version)
883 {
884     krb5_storage *sp;
885     krb5_data data;
886     int ret;
887
888     if (s->version < current_version)
889         return 0;
890
891     /*
892      * If we had sent a partial diff, and now they're caught up, then there's
893      * no more.
894      */
895     s->next_diff.more = 0;
896
897     if (verbose)
898         krb5_warnx(context, "slave %s version %ld already sent", s->name,
899                    (long)s->version);
900     sp = krb5_storage_emem();
901     if (sp == NULL)
902         krb5_errx(context, IPROPD_RESTART, "krb5_storage_from_mem");
903
904     ret = krb5_store_uint32(sp, YOU_HAVE_LAST_VERSION);
905     if (ret == 0) {
906         krb5_data_zero(&data);
907         ret = krb5_storage_to_data(sp, &data);
908     }
909     krb5_storage_free(sp);
910     if (ret == 0) {
911         ret = mk_priv_tail(context, s, &data);
912         krb5_data_free(&data);
913     }
914     if (ret == 0)
915         send_tail(context, s);
916
917     return 1;
918 }
919
920 /*
921  * Lock the log and return initial version and timestamp
922  */
923 static int
924 get_first(kadm5_server_context *server_context, int log_fd,
925           uint32_t *initial_verp, uint32_t *initial_timep)
926 {
927     krb5_context context = server_context->context;
928     int ret;
929
930     /*
931      * We don't want to perform tight retry loops on log access errors, so on
932      * error mark the slave dead.  The slave reconnect after a delay...
933      */
934     if (flock(log_fd, LOCK_SH) == -1) {
935         krb5_warn(context, errno, "could not obtain shared lock on log file");
936         return -1;
937     }
938
939     ret = kadm5_log_get_version_fd(server_context, log_fd, LOG_VERSION_FIRST,
940                                    initial_verp, initial_timep);
941     if (ret == HEIM_ERR_EOF)
942         ret = kadm5_log_get_version_fd(server_context, log_fd,
943                                        LOG_VERSION_UBER, initial_verp,
944                                        initial_timep);
945     if (ret != 0) {
946         flock(log_fd, LOCK_UN);
947         krb5_warn(context, ret, "could not read initial log entry");
948         return -1;
949     }
950
951     return 0;
952 }
953
954 /*-
955  * Find the left end of the diffs in the log we want to send.
956  *
957  * - On success, return a positive offset to the first new entry, retaining
958  *   a read lock on the log file.
959  * - On error, return a negative offset, with the lock released.
960  * - If we simply find no successor entry in the log, return zero
961  *   with the lock released, which indicates that fallback to send_complete()
962  *   is needed.
963  */
964 static off_t
965 get_left(kadm5_server_context *server_context, slave *s, krb5_storage *sp,
966          int log_fd, uint32_t current_version,
967          uint32_t *initial_verp, uint32_t *initial_timep)
968 {
969     krb5_context context = server_context->context;
970     off_t pos;
971     off_t left;
972     int ret;
973
974     for (;;) {
975         uint32_t ver = s->version;
976
977         /* This acquires a read lock on success */
978         ret = get_first(server_context, log_fd,
979                         initial_verp, initial_timep);
980         if (ret != 0)
981             return -1;
982
983         /* When the slave version is out of range, send the whole database. */
984         if (ver == 0 || ver < *initial_verp || ver > current_version) {
985             flock(log_fd, LOCK_UN);
986             return 0;
987         }
988
989         /* Avoid seeking past the last committed record */
990         if (kadm5_log_goto_end(server_context, sp) != 0 ||
991             (pos = krb5_storage_seek(sp, 0, SEEK_CUR)) < 0)
992             goto err;
993
994         /*
995          * First try to see if we can find it quickly by seeking to the right
996          * end of the previous diff sent.
997          */
998         if (s->next_diff.last_version_sent > 0 &&
999             s->next_diff.off_next_version > 0 &&
1000             s->next_diff.off_next_version < pos &&
1001             s->next_diff.initial_version == *initial_verp &&
1002             s->next_diff.initial_tstamp == *initial_timep) {
1003             /*
1004              * Sanity check that the left version matches what we wanted, the
1005              * log may have been truncated since.
1006              */
1007             left = s->next_diff.off_next_version;
1008             if (krb5_storage_seek(sp, left, SEEK_SET) != left)
1009                 goto err;
1010             if (kadm5_log_next(context, sp, &ver, NULL, NULL, NULL) == 0 &&
1011                 ver == s->next_diff.last_version_sent + 1)
1012                 return left;
1013         }
1014
1015         if (krb5_storage_seek(sp, pos, SEEK_SET) != pos)
1016             goto err;
1017
1018         /*
1019          * Drop the lock and try to find the left entry by seeking backward
1020          * from the end of the end of the log.  If we succeed, re-acquire the
1021          * lock, update "next_diff", and retry the fast-path.
1022          */
1023         flock(log_fd, LOCK_UN);
1024
1025         /* Slow path: seek backwards, entry by entry, from the end */
1026         for (;;) {
1027             enum kadm_ops op;
1028             uint32_t len;
1029
1030             ret = kadm5_log_previous(context, sp, &ver, NULL, &op, &len);
1031             if (ret)
1032                 return -1;
1033             left = krb5_storage_seek(sp, -16, SEEK_CUR);
1034             if (left < 0)
1035                 return left;
1036             if (ver == s->version + 1)
1037                 break;
1038
1039             /*
1040              * We don't expect to reach the slave's version, unless the log
1041              * has been modified after we released the lock.
1042              */
1043             if (ver == s->version) {
1044                 krb5_warnx(context, "iprop log truncated while sending diffs "
1045                            "to slave??  ver = %lu", (unsigned long)ver);
1046                 return -1;
1047             }
1048
1049             /* If we've reached the uber record, send the complete database */
1050             if (left == 0 || (ver == 0 && op == kadm_nop))
1051                 return 0;
1052         }
1053         assert(ver == s->version + 1);
1054
1055         /* Set up the fast-path pre-conditions */
1056         s->next_diff.last_version_sent = s->version;
1057         s->next_diff.off_next_version = left;
1058         s->next_diff.initial_version = *initial_verp;
1059         s->next_diff.initial_tstamp = *initial_timep;
1060
1061         /*
1062          * If we loop then we're hoping to hit the fast path so we can return a
1063          * non-zero, positive left offset with the lock held.
1064          *
1065          * We just updated the fast path pre-conditions, so unless a log
1066          * truncation event happens between the point where we dropped the lock
1067          * and the point where we rearcuire it above, we will hit the fast
1068          * path.
1069          */
1070     }
1071
1072  err:
1073     flock(log_fd, LOCK_UN);
1074     return -1;
1075 }
1076
1077 static off_t
1078 get_right(krb5_context context, int log_fd, krb5_storage *sp,
1079           int lastver, slave *s, off_t left, uint32_t *verp)
1080 {
1081     int ret = 0;
1082     int i = 0;
1083     uint32_t ver = s->version;
1084     off_t right = krb5_storage_seek(sp, left, SEEK_SET);
1085
1086     if (right <= 0) {
1087         flock(log_fd, LOCK_UN);
1088         return -1;
1089     }
1090
1091     /* The "lastver" bound should preclude us reaching EOF */
1092     for (; ret == 0 && i < SEND_DIFFS_MAX_RECORDS && ver < lastver; ++i) {
1093         uint32_t logver;
1094
1095         ret = kadm5_log_next(context, sp, &logver, NULL, NULL, NULL);
1096         if (logver != ++ver)
1097             ret = KADM5_LOG_CORRUPT;
1098     }
1099
1100     if (ret == 0)
1101         right = krb5_storage_seek(sp, 0, SEEK_CUR);
1102     else
1103         right = -1;
1104     if (right <= 0) {
1105         flock(log_fd, LOCK_UN);
1106         return -1;
1107     }
1108     *verp = ver;
1109     return right;
1110 }
1111
1112 static void
1113 send_diffs(kadm5_server_context *server_context, slave *s, int log_fd,
1114            const char *database, uint32_t current_version)
1115 {
1116     krb5_context context = server_context->context;
1117     krb5_storage *sp;
1118     uint32_t initial_version;
1119     uint32_t initial_tstamp;
1120     uint32_t ver;
1121     off_t left = 0;
1122     off_t right = 0;
1123     krb5_ssize_t bytes;
1124     krb5_data data;
1125     int ret = 0;
1126
1127     if (!diffready(context, s) || nodiffs(context, s, current_version))
1128         return;
1129
1130     if (verbose)
1131         krb5_warnx(context, "sending diffs to live-seeming slave %s", s->name);
1132
1133     sp = krb5_storage_from_fd(log_fd);
1134     if (sp == NULL)
1135         krb5_err(context, IPROPD_RESTART_SLOW, ENOMEM,
1136                  "send_diffs: out of memory");
1137
1138     left = get_left(server_context, s, sp, log_fd, current_version,
1139                     &initial_version, &initial_tstamp);
1140     if (left < 0) {
1141         krb5_storage_free(sp);
1142         slave_dead(context, s);
1143         return;
1144     }
1145
1146     if (left == 0) {
1147         /* Slave's version is not in the log, fall back on send_complete() */
1148         krb5_storage_free(sp);
1149         send_complete(context, s, database, current_version,
1150                       initial_version, initial_tstamp);
1151         return;
1152     }
1153
1154     /* We still hold the read lock, if right > 0 */
1155     right = get_right(server_context->context, log_fd, sp, current_version,
1156                       s, left, &ver);
1157     if (right == left) {
1158         flock(log_fd, LOCK_UN);
1159         krb5_storage_free(sp);
1160         return;
1161     }
1162     if (right < left) {
1163         assert(right < 0);
1164         krb5_storage_free(sp);
1165         slave_dead(context, s);
1166         return;
1167     }
1168
1169     if (krb5_storage_seek(sp, left, SEEK_SET) != left) {
1170         ret = errno ? errno : EIO;
1171         flock(log_fd, LOCK_UN);
1172         krb5_warn(context, ret, "send_diffs: krb5_storage_seek");
1173         krb5_storage_free(sp);
1174         slave_dead(context, s);
1175         return;
1176     }
1177
1178     ret = krb5_data_alloc(&data, right - left + 4);
1179     if (ret) {
1180         flock(log_fd, LOCK_UN);
1181         krb5_warn(context, ret, "send_diffs: krb5_data_alloc");
1182         krb5_storage_free(sp);
1183         slave_dead(context, s);
1184         return;
1185     }
1186
1187     bytes = krb5_storage_read(sp, (char *)data.data + 4, data.length - 4);
1188     flock(log_fd, LOCK_UN);
1189     krb5_storage_free(sp);
1190     if (bytes != data.length - 4)
1191         krb5_errx(context, IPROPD_RESTART, "locked log truncated???");
1192
1193     sp = krb5_storage_from_data(&data);
1194     if (sp == NULL) {
1195         krb5_err(context, IPROPD_RESTART_SLOW, ENOMEM, "out of memory");
1196         return;
1197     }
1198     krb5_store_uint32(sp, FOR_YOU);
1199     krb5_storage_free(sp);
1200
1201     ret = mk_priv_tail(context, s, &data);
1202     krb5_data_free(&data);
1203     if (ret == 0) {
1204         /* Save the fast-path continuation */
1205         s->next_diff.last_version_sent = ver;
1206         s->next_diff.off_next_version = right;
1207         s->next_diff.initial_version = initial_version;
1208         s->next_diff.initial_tstamp = initial_tstamp;
1209         s->next_diff.more = ver < current_version;
1210         ret = send_tail(context, s);
1211
1212         krb5_warnx(context,
1213                    "syncing slave %s from version %lu to version %lu",
1214                    s->name, (unsigned long)s->version,
1215                    (unsigned long)ver);
1216         s->version = ver;
1217     }
1218
1219     if (ret && ret != EWOULDBLOCK) {
1220         krb5_warn(context, ret, "send_diffs: making or sending "
1221                   "KRB-PRIV message");
1222         slave_dead(context, s);
1223         return;
1224     }
1225     slave_seen(s);
1226     return;
1227 }
1228
1229 /* Sensible bound on slave message size */
1230 #define SLAVE_MSG_MAX 65536
1231
1232 static int
1233 fill_input(krb5_context context, slave *s)
1234 {
1235     krb5_error_code ret;
1236
1237     if (s->input.hlen < 4) {
1238         uint8_t *buf = s->input.header_buf + s->input.hlen;
1239         size_t len = 4 - s->input.hlen;
1240         krb5_ssize_t bytes = krb5_net_read(context, &s->fd, buf, len);
1241
1242         if (bytes == 0)
1243             return HEIM_ERR_EOF;
1244         if (bytes < 0) {
1245             if (errno == EWOULDBLOCK || errno == EAGAIN)
1246                 return EWOULDBLOCK;
1247             return errno ? errno : EIO;
1248         }
1249         s->input.hlen += bytes;
1250         if (bytes < len)
1251             return EWOULDBLOCK;
1252
1253         buf = s->input.header_buf;
1254         len = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
1255         if (len > SLAVE_MSG_MAX)
1256             return EINVAL;
1257         ret = krb5_data_alloc(&s->input.packet, len);
1258         if (ret != 0)
1259             return ret;
1260     }
1261
1262     if (s->input.offset < s->input.packet.length) {
1263         u_char *buf = (u_char *)s->input.packet.data + s->input.offset;
1264         size_t len = s->input.packet.length - s->input.offset;
1265         krb5_ssize_t bytes = krb5_net_read(context, &s->fd, buf, len);
1266
1267         if (bytes == 0)
1268             return HEIM_ERR_EOF;
1269         if (bytes < 0) {
1270             if (errno == EWOULDBLOCK || errno == EAGAIN)
1271                 return EWOULDBLOCK;
1272             return errno ? errno : EIO;
1273         }
1274         s->input.offset += bytes;
1275         if (bytes != len)
1276             return EWOULDBLOCK;
1277     }
1278     return 0;
1279 }
1280
1281 static int
1282 read_msg(krb5_context context, slave *s, krb5_data *out)
1283 {
1284     int ret = fill_input(context, s);
1285
1286     if (ret != 0)
1287         return ret;
1288
1289     ret = krb5_rd_priv(context, s->ac, &s->input.packet, out, NULL);
1290
1291     /* Prepare for next packet */
1292     krb5_data_free(&s->input.packet);
1293     s->input.offset = 0;
1294     s->input.hlen = 0;
1295
1296     return ret;
1297 }
1298
1299 static int
1300 process_msg(kadm5_server_context *server_context, slave *s, int log_fd,
1301             const char *database, uint32_t current_version)
1302 {
1303     krb5_context context = server_context->context;
1304     int ret = 0;
1305     krb5_data out;
1306     krb5_storage *sp;
1307     uint32_t tmp;
1308
1309     ret = read_msg(context, s, &out);
1310     if (ret) {
1311         if (ret != EWOULDBLOCK)
1312             krb5_warn(context, ret, "error reading message from %s", s->name);
1313         return ret;
1314     }
1315
1316     sp = krb5_storage_from_mem(out.data, out.length);
1317     if (sp == NULL) {
1318         krb5_warnx(context, "process_msg: no memory");
1319         krb5_data_free(&out);
1320         return 1;
1321     }
1322     if (krb5_ret_uint32(sp, &tmp) != 0) {
1323         krb5_warnx(context, "process_msg: client send too short command");
1324         krb5_data_free(&out);
1325         return 1;
1326     }
1327     switch (tmp) {
1328     case I_HAVE :
1329         ret = krb5_ret_uint32(sp, &tmp);
1330         if (ret != 0) {
1331             krb5_warnx(context, "process_msg: client send too little I_HAVE data");
1332             break;
1333         }
1334         /*
1335          * XXX Make the slave send the timestamp as well, and try to get it
1336          * here, and pass it to send_diffs().
1337          */
1338         /*
1339          * New slave whose version number we've not yet seen.  If the version
1340          * number is zero, the slave has no data, and we'll send a complete
1341          * database (that happens in send_diffs()).  Otherwise, we'll record a
1342          * non-zero initial version and attempt an incremental update.
1343          *
1344          * NOTE!: Once the slave is "ready" (its first I_HAVE has conveyed its
1345          * initial version), we MUST NOT update s->version to the slave's
1346          * I_HAVE version, since we may already have sent later updates, and
1347          * MUST NOT send them again, otherwise we can get further and further
1348          * out of sync resending larger and larger diffs.  The "not yet ready"
1349          * is an essential precondition for setting s->version to the value
1350          * in the I_HAVE message.  This happens only once when the slave
1351          * first connects.
1352          */
1353         if (!(s->flags & SLAVE_F_READY)) {
1354             if (current_version < tmp) {
1355                 krb5_warnx(context, "Slave %s (version %u) has later version "
1356                            "than the master (version %u) OUT OF SYNC",
1357                            s->name, tmp, current_version);
1358                 /* Force send_complete() */
1359                 tmp = 0;
1360             }
1361             /*
1362              * Mark the slave as ready for updates based on incoming signals.
1363              * Prior to the initial I_HAVE, we don't know the slave's version
1364              * number, and MUST not send it anything, since we'll needlessly
1365              * attempt to send the whole database!
1366              */
1367             s->version = tmp;
1368             s->flags |= SLAVE_F_READY;
1369             if (verbose)
1370                 krb5_warnx(context, "slave %s ready for updates from version %u",
1371                            s->name, tmp);
1372         }
1373         if ((s->version_ack = tmp) < s->version)
1374             break;
1375         send_diffs(server_context, s, log_fd, database, current_version);
1376         break;
1377     case I_AM_HERE :
1378         if (verbose)
1379             krb5_warnx(context, "slave %s is there", s->name);
1380         break;
1381     case ARE_YOU_THERE:
1382     case FOR_YOU :
1383     default :
1384         krb5_warnx(context, "Ignoring command %d", tmp);
1385         break;
1386     }
1387
1388     krb5_data_free(&out);
1389     krb5_storage_free(sp);
1390
1391     slave_seen(s);
1392
1393     return ret;
1394 }
1395
1396 #define SLAVE_NAME      "Name"
1397 #define SLAVE_ADDRESS   "Address"
1398 #define SLAVE_VERSION   "Version"
1399 #define SLAVE_STATUS    "Status"
1400 #define SLAVE_SEEN      "Last Seen"
1401
1402 static void
1403 init_stats_names(krb5_context context)
1404 {
1405     const char *fn = NULL;
1406     char *buf = NULL;
1407
1408     if (slave_stats_file)
1409         fn = slave_stats_file;
1410     else if ((fn = krb5_config_get_string(context, NULL, "kdc",
1411                                           "iprop-stats", NULL)) == NULL) {
1412         if (asprintf(&buf, "%s/slaves-stats", hdb_db_dir(context)) != -1
1413             && buf != NULL)
1414             fn = buf;
1415         buf = NULL;
1416     }
1417     if (fn != NULL) {
1418         slave_stats_file = fn;
1419         if (asprintf(&buf, "%s.tmp", fn) != -1 && buf != NULL)
1420             slave_stats_temp_file = buf;
1421     }
1422 }
1423
1424 static void
1425 write_master_down(krb5_context context)
1426 {
1427     char str[100];
1428     time_t t = time(NULL);
1429     FILE *fp = NULL;
1430
1431     if (slave_stats_temp_file != NULL)
1432         fp = fopen(slave_stats_temp_file, "w");
1433     if (fp == NULL)
1434         return;
1435     krb5_format_time(context, t, str, sizeof(str), TRUE);
1436     fprintf(fp, "master down at %s\n", str);
1437
1438     if (fclose(fp) != EOF)
1439         rk_rename(slave_stats_temp_file, slave_stats_file);
1440 }
1441
1442 static void
1443 write_stats(krb5_context context, slave *slaves, uint32_t current_version)
1444 {
1445     char str[100];
1446     rtbl_t tbl;
1447     time_t t = time(NULL);
1448     FILE *fp = NULL;
1449
1450     if (slave_stats_temp_file != NULL)
1451         fp = fopen(slave_stats_temp_file, "w");
1452     if (fp == NULL)
1453         return;
1454
1455     krb5_format_time(context, t, str, sizeof(str), TRUE);
1456     fprintf(fp, "Status for slaves, last updated: %s\n\n", str);
1457
1458     fprintf(fp, "Master version: %lu\n\n", (unsigned long)current_version);
1459
1460     tbl = rtbl_create();
1461     if (tbl == NULL) {
1462         fclose(fp);
1463         return;
1464     }
1465
1466     rtbl_add_column(tbl, SLAVE_NAME, 0);
1467     rtbl_add_column(tbl, SLAVE_ADDRESS, 0);
1468     rtbl_add_column(tbl, SLAVE_VERSION, RTBL_ALIGN_RIGHT);
1469     rtbl_add_column(tbl, SLAVE_STATUS, 0);
1470     rtbl_add_column(tbl, SLAVE_SEEN, 0);
1471
1472     rtbl_set_prefix(tbl, "  ");
1473     rtbl_set_column_prefix(tbl, SLAVE_NAME, "");
1474
1475     while (slaves) {
1476         krb5_address addr;
1477         krb5_error_code ret;
1478         rtbl_add_column_entry(tbl, SLAVE_NAME, slaves->name);
1479         ret = krb5_sockaddr2address (context,
1480                                      (struct sockaddr*)&slaves->addr, &addr);
1481         if(ret == 0) {
1482             krb5_print_address(&addr, str, sizeof(str), NULL);
1483             krb5_free_address(context, &addr);
1484             rtbl_add_column_entry(tbl, SLAVE_ADDRESS, str);
1485         } else
1486             rtbl_add_column_entry(tbl, SLAVE_ADDRESS, "<unknown>");
1487
1488         snprintf(str, sizeof(str), "%u", (unsigned)slaves->version_ack);
1489         rtbl_add_column_entry(tbl, SLAVE_VERSION, str);
1490
1491         if (slaves->flags & SLAVE_F_DEAD)
1492             rtbl_add_column_entry(tbl, SLAVE_STATUS, "Down");
1493         else
1494             rtbl_add_column_entry(tbl, SLAVE_STATUS, "Up");
1495
1496         ret = krb5_format_time(context, slaves->seen, str, sizeof(str), TRUE);
1497         rtbl_add_column_entry(tbl, SLAVE_SEEN, str);
1498
1499         slaves = slaves->next;
1500     }
1501
1502     rtbl_format(tbl, fp);
1503     rtbl_destroy(tbl);
1504
1505     if (fclose(fp) != EOF)
1506         rk_rename(slave_stats_temp_file, slave_stats_file);
1507 }
1508
1509
1510 static char sHDB[] = "HDBGET:";
1511 static char *realm;
1512 static int version_flag;
1513 static int help_flag;
1514 static char *keytab_str = sHDB;
1515 static char *database;
1516 static char *config_file;
1517 static char *port_str;
1518 static int detach_from_console;
1519 static int daemon_child = -1;
1520
1521 static struct getargs args[] = {
1522     { "config-file", 'c', arg_string, &config_file, NULL, NULL },
1523     { "realm", 'r', arg_string, &realm, NULL, NULL },
1524     { "keytab", 'k', arg_string, &keytab_str,
1525       "keytab to get authentication from", "kspec" },
1526     { "database", 'd', arg_string, &database, "database", "file"},
1527     { "slave-stats-file", 0, arg_string, rk_UNCONST(&slave_stats_file),
1528       "file for slave status information", "file"},
1529     { "time-missing", 0, arg_string, rk_UNCONST(&slave_time_missing),
1530       "time before slave is polled for presence", "time"},
1531     { "time-gone", 0, arg_string, rk_UNCONST(&slave_time_gone),
1532       "time of inactivity after which a slave is considered gone", "time"},
1533     { "port", 0, arg_string, &port_str,
1534       "port ipropd will listen to", "port"},
1535     { "detach", 0, arg_flag, &detach_from_console,
1536       "detach from console", NULL },
1537     { "daemon-child", 0, arg_integer, &daemon_child,
1538       "private argument, do not use", NULL },
1539     { "pidfile-basename", 0, arg_string, &pidfile_basename,
1540       "basename of pidfile; private argument for testing", "NAME" },
1541     { "hostname", 0, arg_string, rk_UNCONST(&master_hostname),
1542       "hostname of master (if not same as hostname)", "hostname" },
1543     { "verbose", 0, arg_flag, &verbose, NULL, NULL },
1544     { "version", 0, arg_flag, &version_flag, NULL, NULL },
1545     { "help", 0, arg_flag, &help_flag, NULL, NULL }
1546 };
1547 static int num_args = sizeof(args) / sizeof(args[0]);
1548
1549 int
1550 main(int argc, char **argv)
1551 {
1552     krb5_error_code ret;
1553     krb5_context context;
1554     void *kadm_handle;
1555     kadm5_server_context *server_context;
1556     kadm5_config_params conf;
1557     krb5_socket_t signal_fd, listen_fd;
1558     int log_fd;
1559     slave *slaves = NULL;
1560     uint32_t current_version = 0, old_version = 0;
1561     krb5_keytab keytab;
1562     char **files;
1563     int aret;
1564     int optidx = 0;
1565     int restarter_fd = -1;
1566     struct stat st;
1567
1568     setprogname(argv[0]);
1569
1570     if (getarg(args, num_args, argc, argv, &optidx))
1571         krb5_std_usage(1, args, num_args);
1572
1573     if (help_flag)
1574         krb5_std_usage(0, args, num_args);
1575
1576     if (version_flag) {
1577         print_version(NULL);
1578         exit(0);
1579     }
1580
1581     if (detach_from_console && daemon_child == -1)
1582         daemon_child = roken_detach_prep(argc, argv, "--daemon-child");
1583     rk_pidfile(pidfile_basename);
1584
1585     ret = krb5_init_context(&context);
1586     if (ret)
1587         errx(1, "krb5_init_context failed: %d", ret);
1588
1589     setup_signal();
1590
1591     if (config_file == NULL) {
1592         aret = asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context));
1593         if (aret == -1 || config_file == NULL)
1594             errx(1, "out of memory");
1595     }
1596
1597     ret = krb5_prepend_config_files_default(config_file, &files);
1598     if (ret)
1599         krb5_err(context, 1, ret, "getting configuration files");
1600
1601     ret = krb5_set_config_files(context, files);
1602     krb5_free_config_files(files);
1603     if (ret)
1604         krb5_err(context, 1, ret, "reading configuration files");
1605
1606     init_stats_names(context);
1607
1608     time_before_gone = parse_time (slave_time_gone,  "s");
1609     if (time_before_gone < 0)
1610         krb5_errx (context, 1, "couldn't parse time: %s", slave_time_gone);
1611     time_before_missing = parse_time (slave_time_missing,  "s");
1612     if (time_before_missing < 0)
1613         krb5_errx (context, 1, "couldn't parse time: %s", slave_time_missing);
1614
1615     krb5_openlog(context, "ipropd-master", &log_facility);
1616     krb5_set_warn_dest(context, log_facility);
1617
1618     ret = krb5_kt_register(context, &hdb_get_kt_ops);
1619     if(ret)
1620         krb5_err(context, 1, ret, "krb5_kt_register");
1621
1622     ret = krb5_kt_resolve(context, keytab_str, &keytab);
1623     if(ret)
1624         krb5_err(context, 1, ret, "krb5_kt_resolve: %s", keytab_str);
1625
1626     memset(&conf, 0, sizeof(conf));
1627     if(realm) {
1628         conf.mask |= KADM5_CONFIG_REALM;
1629         conf.realm = realm;
1630     }
1631     ret = kadm5_init_with_skey_ctx (context,
1632                                     KADM5_ADMIN_SERVICE,
1633                                     NULL,
1634                                     KADM5_ADMIN_SERVICE,
1635                                     &conf, 0, 0,
1636                                     &kadm_handle);
1637     if (ret)
1638         krb5_err (context, 1, ret, "kadm5_init_with_password_ctx");
1639
1640     server_context = (kadm5_server_context *)kadm_handle;
1641
1642     log_fd = open (server_context->log_context.log_file, O_RDONLY, 0);
1643     if (log_fd < 0)
1644         krb5_err (context, 1, errno, "open %s",
1645                   server_context->log_context.log_file);
1646
1647     if (fstat(log_fd, &st) == -1)
1648         krb5_err(context, 1, errno, "stat %s",
1649                  server_context->log_context.log_file);
1650
1651     if (flock(log_fd, LOCK_SH) == -1)
1652         krb5_err(context, 1, errno, "shared flock %s",
1653                  server_context->log_context.log_file);
1654     kadm5_log_get_version_fd(server_context, log_fd, LOG_VERSION_LAST,
1655                              &current_version, NULL);
1656     flock(log_fd, LOCK_UN);
1657
1658     signal_fd = make_signal_socket (context);
1659     listen_fd = make_listen_socket (context, port_str);
1660
1661     krb5_warnx(context, "ipropd-master started at version: %lu",
1662                (unsigned long)current_version);
1663
1664     roken_detach_finish(NULL, daemon_child);
1665     restarter_fd = restarter(context, NULL);
1666
1667     while (exit_flag == 0){
1668         slave *p;
1669         fd_set readset, writeset;
1670         int max_fd = 0;
1671         struct timeval to = {30, 0};
1672         uint32_t vers;
1673         struct stat st2;;
1674
1675 #ifndef NO_LIMIT_FD_SETSIZE
1676         if (signal_fd >= FD_SETSIZE || listen_fd >= FD_SETSIZE ||
1677             restarter_fd >= FD_SETSIZE)
1678             krb5_errx (context, IPROPD_RESTART, "fd too large");
1679 #endif
1680
1681         FD_ZERO(&readset);
1682         FD_ZERO(&writeset);
1683         FD_SET(signal_fd, &readset);
1684         max_fd = max(max_fd, signal_fd);
1685         FD_SET(listen_fd, &readset);
1686         max_fd = max(max_fd, listen_fd);
1687         if (restarter_fd > -1) {
1688             FD_SET(restarter_fd, &readset);
1689             max_fd = max(max_fd, restarter_fd);
1690         }
1691
1692         for (p = slaves; p != NULL; p = p->next) {
1693             if (p->flags & SLAVE_F_DEAD)
1694                 continue;
1695             FD_SET(p->fd, &readset);
1696             if (have_tail(p) || more_diffs(p))
1697                 FD_SET(p->fd, &writeset);
1698             max_fd = max(max_fd, p->fd);
1699         }
1700
1701         ret = select(max_fd + 1, &readset, &writeset, NULL, &to);
1702         if (ret < 0) {
1703             if (errno == EINTR)
1704                 continue;
1705             else
1706                 krb5_err (context, IPROPD_RESTART, errno, "select");
1707         }
1708
1709         if (stat(server_context->log_context.log_file, &st2) == -1) {
1710             krb5_warn(context, errno, "could not stat log file by path");
1711             st2 = st;
1712         }
1713
1714         if (st2.st_dev != st.st_dev || st2.st_ino != st.st_ino) {
1715             (void) close(log_fd);
1716
1717             log_fd = open(server_context->log_context.log_file, O_RDONLY, 0);
1718             if (log_fd < 0)
1719                 krb5_err(context, IPROPD_RESTART_SLOW, errno, "open %s",
1720                           server_context->log_context.log_file);
1721
1722             if (fstat(log_fd, &st) == -1)
1723                 krb5_err(context, IPROPD_RESTART_SLOW, errno, "stat %s",
1724                          server_context->log_context.log_file);
1725
1726             if (flock(log_fd, LOCK_SH) == -1)
1727                 krb5_err(context, IPROPD_RESTART, errno, "shared flock %s",
1728                          server_context->log_context.log_file);
1729             kadm5_log_get_version_fd(server_context, log_fd, LOG_VERSION_LAST,
1730                                      &current_version, NULL);
1731             flock(log_fd, LOCK_UN);
1732         }
1733
1734         if (ret == 0) {
1735             /* Recover from failed transactions */
1736             if (kadm5_log_init_nb(server_context) == 0)
1737                 kadm5_log_end(server_context);
1738
1739             if (flock(log_fd, LOCK_SH) == -1)
1740                 krb5_err(context, IPROPD_RESTART, errno,
1741                          "could not lock log file");
1742             kadm5_log_get_version_fd(server_context, log_fd, LOG_VERSION_LAST,
1743                                      &current_version, NULL);
1744             flock(log_fd, LOCK_UN);
1745
1746             if (current_version > old_version) {
1747                 if (verbose)
1748                     krb5_warnx(context,
1749                                "Missed a signal, updating slaves %lu to %lu",
1750                                (unsigned long)old_version,
1751                                (unsigned long)current_version);
1752                 for (p = slaves; p != NULL; p = p->next) {
1753                     if (p->flags & SLAVE_F_DEAD)
1754                         continue;
1755                     send_diffs(server_context, p, log_fd, database,
1756                                current_version);
1757                 }
1758                 old_version = current_version;
1759             }
1760         }
1761
1762         if (ret && FD_ISSET(restarter_fd, &readset)) {
1763             exit_flag = SIGTERM;
1764             break;
1765         }
1766
1767         if (ret && FD_ISSET(signal_fd, &readset)) {
1768 #ifndef NO_UNIX_SOCKETS
1769             struct sockaddr_un peer_addr;
1770 #else
1771             struct sockaddr_storage peer_addr;
1772 #endif
1773             socklen_t peer_len = sizeof(peer_addr);
1774
1775             if(recvfrom(signal_fd, (void *)&vers, sizeof(vers), 0,
1776                         (struct sockaddr *)&peer_addr, &peer_len) < 0) {
1777                 krb5_warn (context, errno, "recvfrom");
1778                 continue;
1779             }
1780             --ret;
1781             assert(ret >= 0);
1782             old_version = current_version;
1783             if (flock(log_fd, LOCK_SH) == -1)
1784                 krb5_err(context, IPROPD_RESTART, errno, "shared flock %s",
1785                          server_context->log_context.log_file);
1786             kadm5_log_get_version_fd(server_context, log_fd, LOG_VERSION_LAST,
1787                                      &current_version, NULL);
1788             flock(log_fd, LOCK_UN);
1789             if (current_version != old_version) {
1790                 /*
1791                  * If current_version < old_version then the log got
1792                  * truncated and we'll end up doing full propagations.
1793                  *
1794                  * Truncating the log when the current version is
1795                  * numerically small can lead to race conditions.
1796                  * Ideally we should identify log versions as
1797                  * {init_or_trunc_time, vno}, then we could not have any
1798                  * such race conditions, but this would either require
1799                  * breaking backwards compatibility for the protocol or
1800                  * adding new messages to it.
1801                  */
1802                 if (verbose)
1803                     krb5_warnx(context,
1804                                "Got a signal, updating slaves %lu to %lu",
1805                                (unsigned long)old_version,
1806                                (unsigned long)current_version);
1807                 for (p = slaves; p != NULL; p = p->next) {
1808                     if (p->flags & SLAVE_F_DEAD)
1809                         continue;
1810                     send_diffs(server_context, p, log_fd, database,
1811                                current_version);
1812                 }
1813             } else {
1814                 if (verbose)
1815                     krb5_warnx(context,
1816                                "Got a signal, but no update in log version %lu",
1817                                (unsigned long)current_version);
1818             }
1819         }
1820
1821         for (p = slaves; p != NULL; p = p->next) {
1822             if (!(p->flags & SLAVE_F_DEAD) &&
1823                 FD_ISSET(p->fd, &writeset) &&
1824                 ((have_tail(p) && send_tail(context, p) == 0) ||
1825                  (!have_tail(p) && more_diffs(p)))) {
1826                 send_diffs(server_context, p, log_fd, database,
1827                            current_version);
1828             }
1829         }
1830
1831         for(p = slaves; p != NULL; p = p->next) {
1832             if (p->flags & SLAVE_F_DEAD)
1833                 continue;
1834             if (ret && FD_ISSET(p->fd, &readset)) {
1835                 --ret;
1836                 assert(ret >= 0);
1837                 ret = process_msg(server_context, p, log_fd, database,
1838                                   current_version);
1839                 if (ret && ret != EWOULDBLOCK)
1840                     slave_dead(context, p);
1841             } else if (slave_gone_p (p))
1842                 slave_dead(context, p);
1843             else if (slave_missing_p (p))
1844                 send_are_you_there (context, p);
1845         }
1846
1847         if (ret && FD_ISSET(listen_fd, &readset)) {
1848             add_slave (context, keytab, &slaves, listen_fd);
1849             --ret;
1850             assert(ret >= 0);
1851         }
1852         write_stats(context, slaves, current_version);
1853     }
1854
1855     if(exit_flag == SIGINT || exit_flag == SIGTERM)
1856         krb5_warnx(context, "%s terminated", getprogname());
1857 #ifdef SIGXCPU
1858     else if(exit_flag == SIGXCPU)
1859         krb5_warnx(context, "%s CPU time limit exceeded", getprogname());
1860 #endif
1861     else
1862         krb5_warnx(context, "%s unexpected exit reason: %ld",
1863                    getprogname(), (long)exit_flag);
1864
1865     write_master_down(context);
1866
1867     return 0;
1868 }