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