1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
|
/* db.c
Persistent database management routines for DHCPD... */
/*
* Copyright (c) 2004-2005 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-2003 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Internet Systems Consortium, Inc.
* 950 Charter Street
* Redwood City, CA 94063
* <info@isc.org>
* http://www.isc.org/
*
* This software has been written for Internet Systems Consortium
* by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
* To learn more about Internet Systems Consortium, see
* ``http://www.isc.org/''. To learn more about Vixie Enterprises,
* see ``http://www.vix.com''. To learn more about Nominum, Inc., see
* ``http://www.nominum.com''.
*/
#ifndef lint
static char copyright[] =
"$Id: db.c,v 1.63.2.14 2005/10/14 16:02:55 dhankins Exp $ Copyright (c) 2004-2005 Internet Systems Consortium. All rights reserved.\n";
#endif /* not lint */
#include "dhcpd.h"
#include <ctype.h>
#include "version.h"
FILE *db_file;
static int counting = 0;
static int count = 0;
TIME write_time;
int lease_file_is_corrupt = 0;
/* Write the specified lease to the current lease database file. */
int write_lease (lease)
struct lease *lease;
{
struct tm *t;
char tbuf [64];
int errors = 0;
int i;
struct binding *b;
char *s;
/* If the lease file is corrupt, don't try to write any more leases
until we've written a good lease file. */
if (lease_file_is_corrupt)
if (!new_lease_file ())
return 0;
if (counting)
++count;
errno = 0;
fprintf (db_file, "lease %s {", piaddr (lease -> ip_addr));
if (errno) {
++errors;
}
/* Note: the following is not a Y2K bug - it's a Y1.9K bug. Until
somebody invents a time machine, I think we can safely disregard
it. */
if (lease -> starts) {
if (lease -> starts != MAX_TIME) {
t = gmtime (&lease -> starts);
/* %Audit% Cannot exceed 59 bytes. %2004.06.17,Safe% */
sprintf (tbuf, "%d %d/%02d/%02d %02d:%02d:%02d;",
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
} else
strcpy (tbuf, "never;");
errno = 0;
fprintf (db_file, "\n starts %s", tbuf);
if (errno) {
++errors;
}
}
if (lease -> ends) {
if (lease -> ends != MAX_TIME) {
t = gmtime (&lease -> ends);
/* %Audit% Cannot exceed 59 bytes. %2004.06.17,Safe% */
sprintf (tbuf, "%d %d/%02d/%02d %02d:%02d:%02d;",
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
} else
strcpy (tbuf, "never;");
errno = 0;
fprintf (db_file, "\n ends %s", tbuf);
if (errno) {
++errors;
}
}
if (lease -> tstp) {
t = gmtime (&lease -> tstp);
errno = 0;
fprintf (db_file, "\n tstp %d %d/%02d/%02d %02d:%02d:%02d;",
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
if (errno) {
++errors;
}
}
if (lease -> tsfp) {
t = gmtime (&lease -> tsfp);
errno = 0;
fprintf (db_file, "\n tsfp %d %d/%02d/%02d %02d:%02d:%02d;",
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
if (errno) {
++errors;
}
}
if (lease->atsfp) {
t = gmtime(&lease->atsfp);
if (fprintf(db_file,
"\n atsfp %d %d/%02d/%02d %02d:%02d:%02d;",
t->tm_wday, t->tm_year + 1900, t->tm_mon + 1,
t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec) <= 0)
++errors;
}
if (lease -> cltt) {
t = gmtime (&lease -> cltt);
errno = 0;
fprintf (db_file, "\n cltt %d %d/%02d/%02d %02d:%02d:%02d;",
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
if (errno) {
++errors;
}
}
if (lease -> binding_state == FTS_ACTIVE &&
(lease -> flags & BOOTP_LEASE)) {
fprintf (db_file, "\n binding state bootp;\n");
} else {
fprintf (db_file, "\n binding state %s;",
((lease -> binding_state > 0 &&
lease -> binding_state <= FTS_LAST)
? binding_state_names [lease -> binding_state - 1]
: "abandoned"));
}
if (lease -> binding_state != lease -> next_binding_state) {
if (lease -> next_binding_state == FTS_ACTIVE &&
(lease -> flags & BOOTP_LEASE))
fprintf (db_file, "\n next binding state bootp;\n");
else
fprintf (db_file, "\n next binding state %s;",
((lease -> next_binding_state > 0 &&
lease -> next_binding_state <= FTS_LAST)
? (binding_state_names
[lease -> next_binding_state - 1])
: "abandoned"));
}
/* If this lease is billed to a class and is still valid,
write it out. */
if (lease -> billing_class && lease -> ends > cur_time) {
if (!write_billing_class (lease -> billing_class)) {
log_error ("unable to write class %s",
lease -> billing_class -> name);
++errors;
}
}
if (lease -> hardware_addr.hlen) {
errno = 0;
fprintf (db_file, "\n hardware %s %s;",
hardware_types [lease -> hardware_addr.hbuf [0]],
print_hw_addr (lease -> hardware_addr.hbuf [0],
lease -> hardware_addr.hlen - 1,
&lease -> hardware_addr.hbuf [1]));
if (errno) {
++errors;
}
}
if (lease -> uid_len) {
int i;
s = quotify_buf (lease -> uid, lease -> uid_len, MDL);
if (s) {
fprintf (db_file, "\n uid \"%s\";", s);
if (errno)
++errors;
dfree (s, MDL);
} else
++errors;
}
if (lease -> scope) {
for (b = lease -> scope -> bindings; b; b = b -> next) {
if (!b -> value)
continue;
if (b -> value -> type == binding_data) {
if (b -> value -> value.data.data) {
s = quotify_buf (b -> value -> value.data.data,
b -> value -> value.data.len, MDL);
if (s) {
errno = 0;
fprintf (db_file, "\n set %s = \"%s\";",
b -> name, s);
if (errno)
++errors;
dfree (s, MDL);
} else
++errors;
}
} else if (b -> value -> type == binding_numeric) {
errno = 0;
fprintf (db_file, "\n set %s = %%%ld;",
b -> name, b -> value -> value.intval);
if (errno)
++errors;
} else if (b -> value -> type == binding_boolean) {
errno = 0;
fprintf (db_file, "\n set %s = %s;",
b -> name,
b -> value -> value.intval ? "true" : "false");
if (errno)
++errors;
} else if (b -> value -> type == binding_dns) {
log_error ("%s: persistent dns values not supported.",
b -> name);
} else if (b -> value -> type == binding_function) {
log_error ("%s: persistent functions not supported.",
b -> name);
} else {
log_error ("%s: unknown binding type %d",
b -> name, b -> value -> type);
}
}
}
if (lease -> agent_options) {
struct option_cache *oc;
struct data_string ds;
pair p;
memset (&ds, 0, sizeof ds);
for (p = lease -> agent_options -> first; p; p = p -> cdr) {
oc = (struct option_cache *)p -> car;
if (oc -> data.len) {
errno = 0;
fprintf (db_file, "\n option agent.%s %s;",
oc -> option -> name,
pretty_print_option (oc -> option, oc -> data.data,
oc -> data.len, 1, 1));
if (errno)
++errors;
}
}
}
if (lease -> client_hostname &&
db_printable (lease -> client_hostname)) {
s = quotify_string (lease -> client_hostname, MDL);
if (s) {
errno = 0;
fprintf (db_file, "\n client-hostname \"%s\";", s);
if (errno)
++errors;
dfree (s, MDL);
} else
++errors;
}
if (lease -> on_expiry) {
errno = 0;
fprintf (db_file, "\n on expiry%s {",
lease -> on_expiry == lease -> on_release
? " or release" : "");
if (errno)
++errors;
write_statements (db_file, lease -> on_expiry, 4);
/* XXX */
fprintf (db_file, "\n }");
}
if (lease -> on_release && lease -> on_release != lease -> on_expiry) {
errno = 0;
fprintf (db_file, "\n on release {");
if (errno)
++errors;
write_statements (db_file, lease -> on_release, 4);
/* XXX */
fprintf (db_file, "\n }");
}
errno = 0;
fputs ("\n}\n", db_file);
if (errno) {
++errors;
}
if (errors)
log_info ("write_lease: unable to write lease %s",
piaddr (lease -> ip_addr));
if (errors)
lease_file_is_corrupt = 1;
return !errors;
}
int write_host (host)
struct host_decl *host;
{
int errors = 0;
int i;
struct data_string ip_addrs;
/* If the lease file is corrupt, don't try to write any more leases
until we've written a good lease file. */
if (lease_file_is_corrupt)
if (!new_lease_file ())
return 0;
if (!db_printable (host -> name))
return 0;
if (counting)
++count;
errno = 0;
fprintf (db_file, "host %s {", host -> name);
if (errno) {
++errors;
}
if (host -> flags & HOST_DECL_DYNAMIC) {
errno = 0;
fprintf (db_file, "\n dynamic;");
if (errno)
++errors;
}
if (host -> flags & HOST_DECL_DELETED) {
errno = 0;
fprintf (db_file, "\n deleted;");
if (errno)
++errors;
} else {
if (host -> interface.hlen) {
errno = 0;
fprintf (db_file, "\n hardware %s %s;",
hardware_types [host -> interface.hbuf [0]],
print_hw_addr (host -> interface.hbuf [0],
host -> interface.hlen - 1,
&host -> interface.hbuf [1]));
if (errno) {
++errors;
}
}
if (host -> client_identifier.len) {
int i;
errno = 0;
if (db_printable_len (host -> client_identifier.data,
host -> client_identifier.len)) {
fprintf (db_file, "\n uid \"%.*s\";",
(int)host -> client_identifier.len,
host -> client_identifier.data);
} else {
fprintf (db_file,
"\n uid %2.2x",
host -> client_identifier.data [0]);
if (errno) {
++errors;
}
for (i = 1;
i < host -> client_identifier.len; i++) {
errno = 0;
fprintf (db_file, ":%2.2x",
host ->
client_identifier.data [i]);
if (errno) {
++errors;
}
}
putc (';', db_file);
}
}
memset (&ip_addrs, 0, sizeof ip_addrs);
if (host -> fixed_addr &&
evaluate_option_cache (&ip_addrs, (struct packet *)0,
(struct lease *)0,
(struct client_state *)0,
(struct option_state *)0,
(struct option_state *)0,
&global_scope,
host -> fixed_addr, MDL)) {
errno = 0;
fprintf (db_file, "\n fixed-address ");
if (errno) {
++errors;
}
for (i = 0; i < ip_addrs.len - 3; i += 4) {
errno = 0;
fprintf (db_file, "%u.%u.%u.%u%s",
ip_addrs.data [i] & 0xff,
ip_addrs.data [i + 1] & 0xff,
ip_addrs.data [i + 2] & 0xff,
ip_addrs.data [i + 3] & 0xff,
i + 7 < ip_addrs.len ? "," : "");
if (errno) {
++errors;
}
}
errno = 0;
fputc (';', db_file);
if (errno) {
++errors;
}
}
if (host -> named_group) {
errno = 0;
fprintf (db_file, "\n group \"%s\";",
host -> named_group -> name);
if (errno) {
++errors;
}
}
if (host -> group &&
(!host -> named_group ||
host -> group != host -> named_group -> group) &&
host -> group != root_group) {
errno = 0;
write_statements (db_file,
host -> group -> statements, 8);
if (errno) {
++errors;
}
}
}
errno = 0;
fputs ("\n}\n", db_file);
if (errno) {
++errors;
}
if (errors) {
log_info ("write_host: unable to write host %s",
host -> name);
lease_file_is_corrupt = 1;
}
return !errors;
}
int write_group (group)
struct group_object *group;
{
int errors = 0;
int i;
/* If the lease file is corrupt, don't try to write any more leases
until we've written a good lease file. */
if (lease_file_is_corrupt)
if (!new_lease_file ())
return 0;
if (!db_printable (group -> name))
return 0;
if (counting)
++count;
errno = 0;
fprintf (db_file, "group %s {", group -> name);
if (errno) {
++errors;
}
if (group -> flags & GROUP_OBJECT_DYNAMIC) {
errno = 0;
fprintf (db_file, "\n dynamic;");
if (errno)
++errors;
}
if (group -> flags & GROUP_OBJECT_STATIC) {
errno = 0;
fprintf (db_file, "\n static;");
if (errno)
++errors;
}
if (group -> flags & GROUP_OBJECT_DELETED) {
errno = 0;
fprintf (db_file, "\n deleted;");
if (errno)
++errors;
} else {
if (group -> group) {
errno = 0;
write_statements (db_file,
group -> group -> statements, 8);
if (errno) {
++errors;
}
}
}
errno = 0;
fputs ("\n}\n", db_file);
if (errno) {
++errors;
}
if (errors) {
log_info ("write_group: unable to write group %s",
group -> name);
lease_file_is_corrupt = 1;
}
return !errors;
}
#if defined (FAILOVER_PROTOCOL)
int write_failover_state (dhcp_failover_state_t *state)
{
struct tm *t;
int errors = 0;
if (lease_file_is_corrupt)
if (!new_lease_file ())
return 0;
errno = 0;
fprintf (db_file, "\nfailover peer \"%s\" state {", state -> name);
if (errno)
++errors;
t = gmtime (&state -> me.stos);
errno = 0;
fprintf (db_file, "\n my state %s at %d %d/%02d/%02d %02d:%02d:%02d;",
/* Never record our state as "startup"! */
(state -> me.state == startup
? dhcp_failover_state_name_print (state -> saved_state)
: dhcp_failover_state_name_print (state -> me.state)),
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
if (errno)
++errors;
t = gmtime (&state -> partner.stos);
errno = 0;
fprintf (db_file,
"\n partner state %s at %d %d/%02d/%02d %02d:%02d:%02d;",
dhcp_failover_state_name_print (state -> partner.state),
t -> tm_wday, t -> tm_year + 1900,
t -> tm_mon + 1, t -> tm_mday,
t -> tm_hour, t -> tm_min, t -> tm_sec);
if (errno)
++errors;
if (state -> i_am == secondary) {
errno = 0;
fprintf (db_file, "\n mclt %ld;",
(unsigned long)state -> mclt);
if (errno)
++errors;
}
fprintf (db_file, "\n}\n");
if (errno)
++errors;
if (errors) {
log_info ("write_failover_state: unable to write state %s",
state -> name);
lease_file_is_corrupt = 1;
return 0;
}
return 1;
}
#endif
int db_printable (s)
const char *s;
{
int i;
for (i = 0; s [i]; i++)
if (!isascii (s [i]) || !isprint (s [i])
|| s [i] == '"' || s [i] == '\\')
return 0;
return 1;
}
int db_printable_len (s, len)
const unsigned char *s;
unsigned len;
{
int i;
for (i = 0; i < len; i++)
if (!isascii (s [i]) || !isprint (s [i]) ||
s [i] == '"' || s [i] == '\\')
return 0;
return 1;
}
void write_named_billing_class (const char *name, unsigned len,
struct class *class)
{
/* XXX billing classes that are modified by OMAPI need
XXX to be detected and written out here. */
}
void write_billing_classes ()
{
struct collection *lp;
struct class *cp;
struct hash_bucket *bp;
int i;
for (lp = collections; lp; lp = lp -> next) {
for (cp = lp -> classes; cp; cp = cp -> nic) {
if (cp -> spawning && cp -> hash) {
class_hash_foreach (cp -> hash, write_named_billing_class);
}
}
}
}
/* Write a spawned class to the database file. */
int write_billing_class (class)
struct class *class;
{
int errors = 0;
int i;
if (lease_file_is_corrupt)
if (!new_lease_file ())
return 0;
if (!class -> superclass) {
errno = 0;
fprintf (db_file, "\n billing class \"%s\";", class -> name);
return !errno;
}
errno = 0;
fprintf (db_file, "\n billing subclass \"%s\"",
class -> superclass -> name);
if (errno)
++errors;
for (i = 0; i < class -> hash_string.len; i++)
if (!isascii (class -> hash_string.data [i]) ||
!isprint (class -> hash_string.data [i]))
break;
if (i == class -> hash_string.len) {
errno = 0;
fprintf (db_file, " \"%.*s\";",
(int)class -> hash_string.len,
class -> hash_string.data);
if (errno)
++errors;
} else {
errno = 0;
fprintf (db_file, " %2.2x", class -> hash_string.data [0]);
if (errno)
++errors;
for (i = 1; i < class -> hash_string.len; i++) {
errno = 0;
fprintf (db_file, ":%2.2x",
class -> hash_string.data [i]);
if (errno)
++errors;
}
errno = 0;
fprintf (db_file, ";");
if (errno)
++errors;
}
class -> dirty = !errors;
if (errors)
lease_file_is_corrupt = 1;
return !errors;
}
/* Commit leases after a timeout. */
void commit_leases_timeout (void *foo)
{
commit_leases ();
}
/* Commit any leases that have been written out... */
int commit_leases ()
{
/* Commit any outstanding writes to the lease database file.
We need to do this even if we're rewriting the file below,
just in case the rewrite fails. */
if (fflush (db_file) == EOF) {
log_info ("commit_leases: unable to commit: %m");
return 0;
}
if (fsync (fileno (db_file)) < 0) {
log_info ("commit_leases: unable to commit: %m");
return 0;
}
/* If we haven't rewritten the lease database in over an
hour, rewrite it now. (The length of time should probably
be configurable. */
if (count && cur_time - write_time > 3600) {
count = 0;
write_time = cur_time;
new_lease_file ();
}
return 1;
}
void db_startup (testp)
int testp;
{
isc_result_t status;
#if defined (TRACING)
if (!trace_playback ()) {
#endif
/* Read in the existing lease file... */
status = read_conf_file (path_dhcpd_db,
(struct group *)0, 0, 1);
/* XXX ignore status? */
#if defined (TRACING)
}
#endif
#if defined (TRACING)
/* If we're playing back, there is no lease file, so we can't
append it, so we create one immediately (maybe this isn't
the best solution... */
if (trace_playback ()) {
new_lease_file ();
}
#endif
if (!testp) {
db_file = fopen (path_dhcpd_db, "a");
if (!db_file)
log_fatal ("Can't open %s for append.", path_dhcpd_db);
expire_all_pools ();
#if defined (TRACING)
if (trace_playback ())
write_time = cur_time;
else
#endif
GET_TIME (&write_time);
new_lease_file ();
}
}
int new_lease_file ()
{
char newfname [512];
char backfname [512];
TIME t;
int db_fd;
FILE *new_db_file;
/* Make a temporary lease file... */
GET_TIME (&t);
/* %Audit% Truncated filename causes panic. %2004.06.17,Safe%
* This should never happen since the path is a configuration
* variable from build-time or command-line. But if it should,
* either by malice or ignorance, we panic, since the potential
* for havoc is high.
*/
if (snprintf (newfname, sizeof newfname, "%s.%d",
path_dhcpd_db, (int)t) >= sizeof newfname)
log_fatal("new_lease_file: lease file path too long");
db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
if (db_fd < 0) {
log_error ("Can't create new lease file: %m");
return 0;
}
if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
log_error("Can't fdopen new lease file: %m");
close(db_fd);
goto fdfail;
}
/* Close previous database, if any. */
if (db_file)
fclose(db_file);
db_file = new_db_file;
/* Write an introduction so people don't complain about time
being off. */
errno = 0;
fprintf (db_file, "# All times in this file are in UTC (GMT), not %s",
"your local timezone. This is\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# not a bug, so please don't ask about it. %s",
"There is no portable way to\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# store leases in the local timezone, so please %s",
"don't request this as a\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# feature. If this is inconvenient or %s",
"confusing to you, we sincerely\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# apologize. Seriously, though - don't ask.\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# The format of this file is documented in the %s",
"dhcpd.leases(5) manual page.\n");
if (errno != 0)
goto fail;
fprintf (db_file, "# This lease file was written by isc-dhcp-%s\n\n",
DHCP_VERSION);
if (errno != 0)
goto fail;
/* Write out all the leases that we know of... */
counting = 0;
if (!write_leases ())
goto fail;
#if defined (TRACING)
if (!trace_playback ()) {
#endif
/* %Audit% Truncated filename causes panic. %2004.06.17,Safe%
* This should never happen since the path is a configuration
* variable from build-time or command-line. But if it should,
* either by malice or ignorance, we panic, since the potential
* for havoc is too high.
*/
if (snprintf (backfname, sizeof backfname, "%s~", path_dhcpd_db)
>= sizeof backfname)
log_fatal("new_lease_file: backup lease file path too long");
/* Get the old database out of the way... */
if (unlink (backfname) < 0 && errno != ENOENT) {
log_error ("Can't remove old lease database backup %s: %m",
backfname);
goto fail;
}
if (link(path_dhcpd_db, backfname) < 0) {
if (errno == ENOENT) {
log_error("%s is missing - no lease db to backup.",
path_dhcpd_db);
} else {
log_error("Can't backup lease database %s to %s: %m",
path_dhcpd_db, backfname);
goto fail;
}
}
#if defined (TRACING)
}
#endif
/* Move in the new file... */
if (rename (newfname, path_dhcpd_db) < 0) {
log_error ("Can't install new lease database %s to %s: %m",
newfname, path_dhcpd_db);
goto fail;
}
counting = 1;
lease_file_is_corrupt = 0;
return 1;
fail:
lease_file_is_corrupt = 1;
fdfail:
unlink (newfname);
return 0;
}
int group_writer (struct group_object *group)
{
if (!write_group (group))
return 0;
if (!commit_leases ())
return 0;
return 1;
}
|