3 Persistent database management routines for DHCPD... */
6 * Copyright (c) 2004-2010 by Internet Systems Consortium, Inc. ("ISC")
7 * Copyright (c) 1995-2003 by Internet Software Consortium
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 * Internet Systems Consortium, Inc.
23 * Redwood City, CA 94063
25 * https://www.isc.org/
27 * This software has been written for Internet Systems Consortium
28 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29 * To learn more about Internet Systems Consortium, see
30 * ``https://www.isc.org/''. To learn more about Vixie Enterprises,
31 * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
32 * ``http://www.nominum.com''.
39 static isc_result_t write_binding_scope(FILE *db_file, struct binding *bnd,
44 static int counting = 0;
47 int lease_file_is_corrupt = 0;
49 /* Write a single binding scope value in parsable format.
53 write_binding_scope(FILE *db_file, struct binding *bnd, char *prepend) {
56 if ((db_file == NULL) || (bnd == NULL) || (prepend == NULL))
57 return DHCP_R_INVALIDARG;
59 if (bnd->value->type == binding_data) {
60 if (bnd->value->value.data.data != NULL) {
61 s = quotify_buf(bnd->value->value.data.data,
62 bnd->value->value.data.len, MDL);
65 fprintf(db_file, "%sset %s = \"%s\";",
66 prepend, bnd->name, s);
75 } else if (bnd->value->type == binding_numeric) {
77 fprintf(db_file, "%sset %s = %%%ld;", prepend,
78 bnd->name, bnd->value->value.intval);
81 } else if (bnd->value->type == binding_boolean) {
83 fprintf(db_file, "%sset %s = %s;", prepend, bnd->name,
84 bnd->value->value.intval ? "true" : "false");
87 } else if (bnd->value->type == binding_dns) {
88 log_error("%s: persistent dns values not supported.",
90 } else if (bnd->value->type == binding_function) {
91 log_error("%s: persistent functions not supported.",
94 log_fatal("%s: unknown binding type %d", bnd->name,
101 /* Write the specified lease to the current lease database file. */
103 int write_lease (lease)
111 /* If the lease file is corrupt, don't try to write any more leases
112 until we've written a good lease file. */
113 if (lease_file_is_corrupt)
114 if (!new_lease_file ())
120 fprintf (db_file, "lease %s {", piaddr (lease -> ip_addr));
126 ((tval = print_time(lease->starts)) == NULL ||
127 fprintf(db_file, "\n starts %s", tval) < 0))
131 ((tval = print_time(lease->ends)) == NULL ||
132 fprintf(db_file, "\n ends %s", tval) < 0))
136 ((tval = print_time(lease->tstp)) == NULL ||
137 fprintf(db_file, "\n tstp %s", tval) < 0))
141 ((tval = print_time(lease->tsfp)) == NULL ||
142 fprintf(db_file, "\n tsfp %s", tval) < 0))
146 ((tval = print_time(lease->atsfp)) == NULL ||
147 fprintf(db_file, "\n atsfp %s", tval) < 0))
151 ((tval = print_time(lease->cltt)) == NULL ||
152 fprintf(db_file, "\n cltt %s", tval) < 0))
155 if (fprintf (db_file, "\n binding state %s;",
156 ((lease -> binding_state > 0 &&
157 lease -> binding_state <= FTS_LAST)
158 ? binding_state_names [lease -> binding_state - 1]
162 if (lease -> binding_state != lease -> next_binding_state)
163 if (fprintf (db_file, "\n next binding state %s;",
164 ((lease -> next_binding_state > 0 &&
165 lease -> next_binding_state <= FTS_LAST)
166 ? (binding_state_names
167 [lease -> next_binding_state - 1])
172 * In this case, if the rewind state is not present in the lease file,
173 * the reader will use the current binding state as the most
174 * conservative (safest) state. So if the in-memory rewind state is
175 * for some reason invalid, the best thing to do is not to write a
176 * state and let the reader take on a safe state.
178 if ((lease->binding_state != lease->rewind_binding_state) &&
179 (lease->rewind_binding_state > 0) &&
180 (lease->rewind_binding_state <= FTS_LAST) &&
181 (fprintf(db_file, "\n rewind binding state %s;",
182 binding_state_names[lease->rewind_binding_state-1])) < 0)
185 if (lease->flags & RESERVED_LEASE)
186 if (fprintf(db_file, "\n reserved;") < 0)
189 if (lease->flags & BOOTP_LEASE)
190 if (fprintf(db_file, "\n dynamic-bootp;") < 0)
193 /* If this lease is billed to a class and is still valid,
195 if (lease -> billing_class && lease -> ends > cur_time) {
196 if (!write_billing_class (lease -> billing_class)) {
197 log_error ("unable to write class %s",
198 lease -> billing_class -> name);
203 if (lease -> hardware_addr.hlen) {
205 fprintf (db_file, "\n hardware %s %s;",
206 hardware_types [lease -> hardware_addr.hbuf [0]],
207 print_hw_addr (lease -> hardware_addr.hbuf [0],
208 lease -> hardware_addr.hlen - 1,
209 &lease -> hardware_addr.hbuf [1]));
213 if (lease -> uid_len) {
214 s = quotify_buf (lease -> uid, lease -> uid_len, MDL);
217 fprintf (db_file, "\n uid \"%s\";", s);
225 if (lease->scope != NULL) {
226 for (b = lease->scope->bindings; b; b = b->next) {
230 if (write_binding_scope(db_file, b, "\n ") != ISC_R_SUCCESS)
235 if (lease -> agent_options) {
236 struct option_cache *oc;
237 struct data_string ds;
240 memset (&ds, 0, sizeof ds);
241 for (p = lease -> agent_options -> first; p; p = p -> cdr) {
242 oc = (struct option_cache *)p -> car;
243 if (oc -> data.len) {
245 fprintf (db_file, "\n option agent.%s %s;",
246 oc -> option -> name,
247 pretty_print_option (oc -> option, oc -> data.data,
248 oc -> data.len, 1, 1));
254 if (lease -> client_hostname &&
255 db_printable((unsigned char *)lease->client_hostname)) {
256 s = quotify_string (lease -> client_hostname, MDL);
259 fprintf (db_file, "\n client-hostname \"%s\";", s);
266 if (lease -> on_expiry) {
268 fprintf (db_file, "\n on expiry%s {",
269 lease -> on_expiry == lease -> on_release
270 ? " or release" : "");
271 write_statements (db_file, lease -> on_expiry, 4);
273 fprintf (db_file, "\n }");
277 if (lease -> on_release && lease -> on_release != lease -> on_expiry) {
279 fprintf (db_file, "\n on release {");
280 write_statements (db_file, lease -> on_release, 4);
282 fprintf (db_file, "\n }");
288 fputs ("\n}\n", db_file);
293 log_info ("write_lease: unable to write lease %s",
294 piaddr (lease -> ip_addr));
295 lease_file_is_corrupt = 1;
301 int write_host (host)
302 struct host_decl *host;
306 struct data_string ip_addrs;
308 /* If the lease file is corrupt, don't try to write any more leases
309 until we've written a good lease file. */
310 if (lease_file_is_corrupt)
311 if (!new_lease_file ())
314 if (!db_printable((unsigned char *)host->name))
321 fprintf (db_file, "host %s {", host -> name);
325 if (host -> flags & HOST_DECL_DYNAMIC) {
327 fprintf (db_file, "\n dynamic;");
332 if (host -> flags & HOST_DECL_DELETED) {
334 fprintf (db_file, "\n deleted;");
338 if (host -> interface.hlen) {
340 fprintf (db_file, "\n hardware %s %s;",
341 hardware_types [host -> interface.hbuf [0]],
342 print_hw_addr (host -> interface.hbuf [0],
343 host -> interface.hlen - 1,
344 &host -> interface.hbuf [1]));
348 if (host -> client_identifier.len) {
351 if (db_printable_len (host -> client_identifier.data,
352 host -> client_identifier.len)) {
353 fprintf (db_file, "\n uid \"%.*s\";",
354 (int)host -> client_identifier.len,
355 host -> client_identifier.data);
361 host -> client_identifier.data [0]);
365 i < host -> client_identifier.len; i++) {
367 fprintf (db_file, ":%2.2x",
369 client_identifier.data [i]);
375 fputc (';', db_file);
381 memset (&ip_addrs, 0, sizeof ip_addrs);
382 if (host -> fixed_addr &&
383 evaluate_option_cache (&ip_addrs, (struct packet *)0,
385 (struct client_state *)0,
386 (struct option_state *)0,
387 (struct option_state *)0,
389 host -> fixed_addr, MDL)) {
392 fprintf (db_file, "\n fixed-address ");
395 for (i = 0; i < ip_addrs.len - 3; i += 4) {
398 fprintf (db_file, "%u.%u.%u.%u%s",
399 ip_addrs.data [i] & 0xff,
400 ip_addrs.data [i + 1] & 0xff,
401 ip_addrs.data [i + 2] & 0xff,
402 ip_addrs.data [i + 3] & 0xff,
403 i + 7 < ip_addrs.len ? "," : "");
409 fputc (';', db_file);
414 if (host -> named_group) {
416 fprintf (db_file, "\n group \"%s\";",
417 host -> named_group -> name);
423 (!host -> named_group ||
424 host -> group != host -> named_group -> group) &&
425 host -> group != root_group) {
427 write_statements (db_file,
428 host -> group -> statements, 8);
435 fputs ("\n}\n", db_file);
440 log_info ("write_host: unable to write host %s",
442 lease_file_is_corrupt = 1;
448 int write_group (group)
449 struct group_object *group;
453 /* If the lease file is corrupt, don't try to write any more leases
454 until we've written a good lease file. */
455 if (lease_file_is_corrupt)
456 if (!new_lease_file ())
459 if (!db_printable((unsigned char *)group->name))
466 fprintf (db_file, "group %s {", group -> name);
470 if (group -> flags & GROUP_OBJECT_DYNAMIC) {
472 fprintf (db_file, "\n dynamic;");
477 if (group -> flags & GROUP_OBJECT_STATIC) {
479 fprintf (db_file, "\n static;");
484 if (group -> flags & GROUP_OBJECT_DELETED) {
486 fprintf (db_file, "\n deleted;");
490 if (group -> group) {
492 write_statements (db_file,
493 group -> group -> statements, 8);
500 fputs ("\n}\n", db_file);
505 log_info ("write_group: unable to write group %s",
507 lease_file_is_corrupt = 1;
514 * Write an IA and the options it has.
517 write_ia(const struct ia_xx *ia) {
518 struct iasubopt *iasubopt;
521 char addr_buf[sizeof("ffff:ffff:ffff:ffff:ffff:ffff.255.255.255.255")];
522 const char *binding_state;
528 * If the lease file is corrupt, don't try to write any more
529 * leases until we've written a good lease file.
531 if (lease_file_is_corrupt) {
532 if (!new_lease_file()) {
542 s = quotify_buf(ia->iaid_duid.data, ia->iaid_duid.len, MDL);
546 switch (ia->ia_type) {
548 fprintf_ret = fprintf(db_file, "ia-na \"%s\" {\n", s);
551 fprintf_ret = fprintf(db_file, "ia-ta \"%s\" {\n", s);
554 fprintf_ret = fprintf(db_file, "ia-pd \"%s\" {\n", s);
557 log_error("Unknown ia type %u for \"%s\" at %s:%d",
558 (unsigned)ia->ia_type, s, MDL);
562 if (fprintf_ret < 0) {
565 if (ia->cltt != MIN_TIME) {
566 tval = print_time(ia->cltt);
570 if (fprintf(db_file, " cltt %s\n", tval) < 0) {
574 for (i=0; i<ia->num_iasubopt; i++) {
575 iasubopt = ia->iasubopt[i];
577 inet_ntop(AF_INET6, &iasubopt->addr,
578 addr_buf, sizeof(addr_buf));
579 if ((ia->ia_type != D6O_IA_PD) &&
580 (fprintf(db_file, " iaaddr %s {\n", addr_buf) < 0)) {
583 if ((ia->ia_type == D6O_IA_PD) &&
584 (fprintf(db_file, " iaprefix %s/%d {\n",
585 addr_buf, (int)iasubopt->plen) < 0)) {
588 if ((iasubopt->state <= 0) || (iasubopt->state > FTS_LAST)) {
589 log_fatal("Unknown iasubopt state %d at %s:%d",
590 iasubopt->state, MDL);
592 binding_state = binding_state_names[iasubopt->state-1];
593 if (fprintf(db_file, " binding state %s;\n",
594 binding_state) < 0) {
597 if (fprintf(db_file, " preferred-life %u;\n",
598 (unsigned)iasubopt->prefer) < 0) {
601 if (fprintf(db_file, " max-life %u;\n",
602 (unsigned)iasubopt->valid) < 0) {
606 /* Note that from here on out, the \n is prepended to the
607 * next write, rather than appended to the current write.
609 if ((iasubopt->state == FTS_ACTIVE) ||
610 (iasubopt->state == FTS_ABANDONED) ||
611 (iasubopt->hard_lifetime_end_time != 0)) {
612 tval = print_time(iasubopt->hard_lifetime_end_time);
614 tval = print_time(iasubopt->soft_lifetime_end_time);
619 if (fprintf(db_file, " ends %s", tval) < 0) {
623 /* Write out any binding scopes: note that 'ends' above does
624 * not have \n on the end! We want that.
626 if (iasubopt->scope != NULL)
627 bnd = iasubopt->scope->bindings;
631 for (; bnd != NULL ; bnd = bnd->next) {
632 if (bnd->value == NULL)
635 /* We don't do a regular error_exit because the
636 * lease db is not corrupt in this case.
638 if (write_binding_scope(db_file, bnd,
639 "\n ") != ISC_R_SUCCESS)
644 if (fprintf(db_file, "\n }\n") < 0)
647 if (fprintf(db_file, "}\n\n") < 0)
654 log_info("write_ia: unable to write ia");
655 lease_file_is_corrupt = 1;
661 * Put a copy of the server DUID in the leases file.
664 write_server_duid(void) {
665 struct data_string server_duid;
670 * Only write the DUID if it's been set.
672 if (!server_duid_isset()) {
677 * If the lease file is corrupt, don't try to write any more
678 * leases until we've written a good lease file.
680 if (lease_file_is_corrupt) {
681 if (!new_lease_file()) {
687 * Get a copy of our server DUID and convert to a quoted string.
689 memset(&server_duid, 0, sizeof(server_duid));
690 copy_server_duid(&server_duid, MDL);
691 s = quotify_buf(server_duid.data, server_duid.len, MDL);
692 data_string_forget(&server_duid, MDL);
698 * Write to the leases file.
700 fprintf_ret = fprintf(db_file, "server-duid \"%s\";\n\n", s);
702 if (fprintf_ret < 0) {
707 * Check if we actually managed to write.
713 log_info("write_server_duid: unable to write server-duid");
714 lease_file_is_corrupt = 1;
719 #if defined (FAILOVER_PROTOCOL)
720 int write_failover_state (dhcp_failover_state_t *state)
725 if (lease_file_is_corrupt)
726 if (!new_lease_file ())
730 fprintf (db_file, "\nfailover peer \"%s\" state {", state -> name);
734 tval = print_time(state->me.stos);
736 fprintf(db_file, "\n my state %s at %s",
737 (state->me.state == startup) ?
738 dhcp_failover_state_name_print(state->saved_state) :
739 dhcp_failover_state_name_print(state->me.state),
743 tval = print_time(state->partner.stos);
745 fprintf(db_file, "\n partner state %s at %s",
746 dhcp_failover_state_name_print(state->partner.state),
750 if (state -> i_am == secondary) {
752 fprintf (db_file, "\n mclt %ld;",
753 (unsigned long)state -> mclt);
759 fprintf (db_file, "\n}\n");
764 log_info ("write_failover_state: unable to write state %s",
766 lease_file_is_corrupt = 1;
776 const unsigned char *s;
779 for (i = 0; s [i]; i++)
780 if (!isascii (s [i]) || !isprint (s [i])
781 || s [i] == '"' || s [i] == '\\')
786 int db_printable_len (s, len)
787 const unsigned char *s;
792 for (i = 0; i < len; i++)
793 if (!isascii (s [i]) || !isprint (s [i]) ||
794 s [i] == '"' || s [i] == '\\')
799 static int print_hash_string(FILE *fp, struct class *class)
803 for (i = 0 ; i < class->hash_string.len ; i++)
804 if (!isascii(class->hash_string.data[i]) ||
805 !isprint(class->hash_string.data[i]))
808 if (i == class->hash_string.len) {
809 if (fprintf(fp, " \"%.*s\"", (int)class->hash_string.len,
810 class->hash_string.data) <= 0) {
811 log_error("Failure writing hash string: %m");
815 if (fprintf(fp, " %2.2x", class->hash_string.data[0]) <= 0) {
816 log_error("Failure writing hash string: %m");
819 for (i = 1 ; i < class->hash_string.len ; i++) {
820 if (fprintf(fp, ":%2.2x",
821 class->hash_string.data[i]) <= 0) {
822 log_error("Failure writing hash string: %m");
833 write_named_billing_class(const void *key, unsigned len, void *object)
835 const unsigned char *name = key;
836 struct class *class = object;
838 if (class->flags & CLASS_DECL_DYNAMIC) {
840 if (class->superclass == 0) {
841 if (fprintf(db_file, "class \"%s\" {\n", name) <= 0)
842 return ISC_R_IOERROR;
844 if (fprintf(db_file, "subclass \"%s\"",
845 class->superclass->name) <= 0)
846 return ISC_R_IOERROR;
847 if (!print_hash_string(db_file, class))
848 return ISC_R_IOERROR;
849 if (fprintf(db_file, " {\n") <= 0)
850 return ISC_R_IOERROR;
853 if ((class->flags & CLASS_DECL_DELETED) != 0) {
854 if (fprintf(db_file, " deleted;\n") <= 0)
855 return ISC_R_IOERROR;
857 if (fprintf(db_file, " dynamic;\n") <= 0)
858 return ISC_R_IOERROR;
861 if (class->lease_limit > 0) {
862 if (fprintf(db_file, " lease limit %d;\n",
863 class->lease_limit) <= 0)
864 return ISC_R_IOERROR;
867 if (class->expr != 0) {
868 if (fprintf(db_file, " match if ") <= 0)
869 return ISC_R_IOERROR;
872 write_expression(db_file, class->expr, 5, 5, 0);
874 return ISC_R_IOERROR;
876 if (fprintf(db_file, ";\n") <= 0)
877 return ISC_R_IOERROR;
880 if (class->submatch != 0) {
881 if (class->spawning) {
882 if (fprintf(db_file, " spawn ") <= 0)
883 return ISC_R_IOERROR;
885 if (fprintf(db_file, " match ") <= 0)
886 return ISC_R_IOERROR;
890 write_expression(db_file, class->submatch, 5, 5, 0);
892 return ISC_R_IOERROR;
894 if (fprintf(db_file, ";\n") <= 0)
895 return ISC_R_IOERROR;
898 if (class->statements != 0) {
900 write_statements(db_file, class->statements, 8);
902 return ISC_R_IOERROR;
905 /* XXXJAB this isn't right, but classes read in off the
906 leases file don't get the root group assigned to them
907 (due to clone_group() call). */
908 if (class->group != 0 && class->group->authoritative != 0) {
910 write_statements(db_file, class->group->statements, 8);
912 return ISC_R_IOERROR;
915 if (fprintf(db_file, "}\n\n") <= 0)
916 return ISC_R_IOERROR;
919 if (class->hash != NULL) { /* yep. recursive. god help us. */
920 /* XXX - cannot check error status of this...
921 * foo_hash_foreach returns a count of operations completed.
923 class_hash_foreach(class->hash, write_named_billing_class);
926 return ISC_R_SUCCESS;
929 void write_billing_classes ()
931 struct collection *lp;
934 for (lp = collections; lp; lp = lp -> next) {
935 for (cp = lp -> classes; cp; cp = cp -> nic) {
936 if (cp -> spawning && cp -> hash) {
937 class_hash_foreach (cp -> hash, write_named_billing_class);
943 /* Write a spawned class to the database file. */
945 int write_billing_class (class)
950 if (lease_file_is_corrupt)
951 if (!new_lease_file ())
954 if (!class -> superclass) {
956 fprintf (db_file, "\n billing class \"%s\";", class -> name);
960 if (fprintf(db_file, "\n billing subclass \"%s\"",
961 class -> superclass -> name) < 0)
964 if (!print_hash_string(db_file, class))
967 if (fprintf(db_file, ";") < 0)
970 class -> dirty = !errors;
972 lease_file_is_corrupt = 1;
977 /* Commit leases after a timeout. */
978 void commit_leases_timeout (void *foo)
983 /* Commit any leases that have been written out... */
987 /* Commit any outstanding writes to the lease database file.
988 We need to do this even if we're rewriting the file below,
989 just in case the rewrite fails. */
990 if (fflush (db_file) == EOF) {
991 log_info ("commit_leases: unable to commit: %m");
994 if (fsync (fileno (db_file)) < 0) {
995 log_info ("commit_leases: unable to commit: %m");
999 /* send out all deferred ACKs now */
1000 flush_ackqueue(NULL);
1002 /* If we haven't rewritten the lease database in over an
1003 hour, rewrite it now. (The length of time should probably
1005 if (count && cur_time - write_time > 3600) {
1007 write_time = cur_time;
1013 void db_startup (testp)
1016 isc_result_t status;
1018 #if defined (TRACING)
1019 if (!trace_playback ()) {
1021 /* Read in the existing lease file... */
1022 status = read_conf_file (path_dhcpd_db,
1023 (struct group *)0, 0, 1);
1024 /* XXX ignore status? */
1025 #if defined (TRACING)
1029 #if defined (TRACING)
1030 /* If we're playing back, there is no lease file, so we can't
1031 append it, so we create one immediately (maybe this isn't
1032 the best solution... */
1033 if (trace_playback ()) {
1038 db_file = fopen (path_dhcpd_db, "a");
1040 log_fatal ("Can't open %s for append.", path_dhcpd_db);
1041 expire_all_pools ();
1042 #if defined (TRACING)
1043 if (trace_playback ())
1044 write_time = cur_time;
1051 #if defined(REPORT_HASH_PERFORMANCE)
1052 log_info("Host HW hash: %s", host_hash_report(host_hw_addr_hash));
1053 log_info("Host UID hash: %s", host_hash_report(host_uid_hash));
1054 log_info("Lease IP hash: %s",
1055 lease_ip_hash_report(lease_ip_addr_hash));
1056 log_info("Lease UID hash: %s", lease_id_hash_report(lease_uid_hash));
1057 log_info("Lease HW hash: %s",
1058 lease_id_hash_report(lease_hw_addr_hash));
1062 int new_lease_file ()
1064 char newfname [512];
1065 char backfname [512];
1071 /* Make a temporary lease file... */
1074 db_validity = lease_file_is_corrupt;
1076 /* %Audit% Truncated filename causes panic. %2004.06.17,Safe%
1077 * This should never happen since the path is a configuration
1078 * variable from build-time or command-line. But if it should,
1079 * either by malice or ignorance, we panic, since the potential
1080 * for havoc is high.
1082 if (snprintf (newfname, sizeof newfname, "%s.%d",
1083 path_dhcpd_db, (int)t) >= sizeof newfname)
1084 log_fatal("new_lease_file: lease file path too long");
1086 db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);
1088 log_error ("Can't create new lease file: %m");
1091 if ((new_db_file = fdopen(db_fd, "w")) == NULL) {
1092 log_error("Can't fdopen new lease file: %m");
1097 /* Close previous database, if any. */
1100 db_file = new_db_file;
1103 fprintf (db_file, "# The format of this file is documented in the %s",
1104 "dhcpd.leases(5) manual page.\n");
1108 fprintf (db_file, "# This lease file was written by isc-dhcp-%s\n\n",
1113 /* At this point we have a new lease file that, so far, could not
1114 * be described as either corrupt nor valid.
1116 lease_file_is_corrupt = 0;
1118 /* Write out all the leases that we know of... */
1120 if (!write_leases ())
1123 #if defined (TRACING)
1124 if (!trace_playback ()) {
1126 /* %Audit% Truncated filename causes panic. %2004.06.17,Safe%
1127 * This should never happen since the path is a configuration
1128 * variable from build-time or command-line. But if it should,
1129 * either by malice or ignorance, we panic, since the potential
1130 * for havoc is too high.
1132 if (snprintf (backfname, sizeof backfname, "%s~", path_dhcpd_db)
1133 >= sizeof backfname)
1134 log_fatal("new_lease_file: backup lease file path too long");
1136 /* Get the old database out of the way... */
1137 if (unlink (backfname) < 0 && errno != ENOENT) {
1138 log_error ("Can't remove old lease database backup %s: %m",
1142 if (link(path_dhcpd_db, backfname) < 0) {
1143 if (errno == ENOENT) {
1144 log_error("%s is missing - no lease db to backup.",
1147 log_error("Can't backup lease database %s to %s: %m",
1148 path_dhcpd_db, backfname);
1152 #if defined (TRACING)
1156 /* Move in the new file... */
1157 if (rename (newfname, path_dhcpd_db) < 0) {
1158 log_error ("Can't install new lease database %s to %s: %m",
1159 newfname, path_dhcpd_db);
1167 lease_file_is_corrupt = db_validity;
1173 int group_writer (struct group_object *group)
1175 if (!write_group (group))
1177 if (!commit_leases ())