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