]> git.ipfire.org Git - thirdparty/dhcp.git/commitdiff
[master] dhcpd -T now writes leases to temp file
authorThomas Markwalder <tmark@isc.org>
Wed, 6 Dec 2017 20:25:56 +0000 (15:25 -0500)
committerThomas Markwalder <tmark@isc.org>
Wed, 6 Dec 2017 20:25:56 +0000 (15:25 -0500)
    Merges in rt22267.

RELNOTES
includes/dhcpd.h
server/db.c
server/dhcpd.8

index bdaab1c90d2870869c838c905ae45e2d6ff4121e..3fe4d2e152ee75a566edff8e317a64856f3ba8be 100644 (file)
--- a/RELNOTES
+++ b/RELNOTES
@@ -210,6 +210,14 @@ dhcp-users@lists.isc.org.
   sent and logged.
   [ISC-Bugs #2729]
 
+- When given the -T command line argument, in addition to reading the
+  current lease file, the server will write the leases to a temporary
+  lease file.  This can help detect issues in server configuration that
+  only surface when leases are written to the file.  The current lease
+  file will not be modified and the temporary lease file is removed upon
+  completion of the test.
+  [ISC-Bugs #22267]
+
                        Changes since 4.3.0 (bug fixes)
 
 - Tidy up several small tickets.
index c83dc9a6fd7ddc5e13ecf555e5494a7aa4585984..cea60b27d434de7aa238eef1a4a35b0a96543eb7 100644 (file)
@@ -3032,7 +3032,7 @@ void commit_leases_timeout (void *);
 int commit_leases (void);
 int commit_leases_timed (void);
 void db_startup (int);
-int new_lease_file (void);
+int new_lease_file (int test_mode);
 int group_writer (struct group_object *);
 int write_ia(const struct ia_xx *);
 
index 91ee522b1c185ed65bdd5a227f6f219ba7d4026b..1240cee9c67479ae99998df45137700ea874fb89 100644 (file)
@@ -106,7 +106,7 @@ int write_lease (lease)
        /* 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 ())
+               if (!new_lease_file (0))
                        return 0;
 
        if (counting)
@@ -305,7 +305,7 @@ int write_host (host)
        /* 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 ())
+               if (!new_lease_file (0))
                        return 0;
 
        if (!db_printable((unsigned char *)host->name))
@@ -454,7 +454,7 @@ int write_group (group)
        /* 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 ())
+               if (!new_lease_file (0))
                        return 0;
 
        if (!db_printable((unsigned char *)group->name))
@@ -530,7 +530,7 @@ write_ia(const struct ia_xx *ia) {
         * leases until we've written a good lease file. 
         */
        if (lease_file_is_corrupt) {
-               if (!new_lease_file()) {
+               if (!new_lease_file(0)) {
                        return 0;
                }
        }
@@ -702,7 +702,7 @@ write_server_duid(void) {
         * leases until we've written a good lease file. 
         */
        if (lease_file_is_corrupt) {
-               if (!new_lease_file()) {
+               if (!new_lease_file(0)) {
                        return 0;
                }
        }
@@ -748,7 +748,7 @@ int write_failover_state (dhcp_failover_state_t *state)
        const char *tval;
 
        if (lease_file_is_corrupt)
-               if (!new_lease_file ())
+               if (!new_lease_file (0))
                        return 0;
 
        errno = 0;
@@ -973,7 +973,7 @@ int write_billing_class (class)
        int errors = 0;
 
        if (lease_file_is_corrupt)
-               if (!new_lease_file ())
+               if (!new_lease_file (0))
                        return 0;
 
        if (!class -> superclass) {
@@ -1028,7 +1028,7 @@ int commit_leases ()
        if (count && cur_time - write_time > LEASE_REWRITE_PERIOD) {
                count = 0;
                write_time = cur_time;
-               new_lease_file();
+               new_lease_file(0);
        }
        return (1);
 }
@@ -1048,9 +1048,9 @@ int commit_leases_timed()
        return (1);
 }
 
-void db_startup (testp)
-       int testp;
+void db_startup (int test_mode)
 {
+       const char *current_db_path;
        isc_result_t status;
 
 #if defined (TRACING)
@@ -1077,22 +1077,26 @@ void db_startup (testp)
           append it, so we create one immediately (maybe this isn't
           the best solution... */
        if (trace_playback ()) {
-               new_lease_file ();
+               new_lease_file (0);
        }
 #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 ();
+       /* expire_all_pools will cause writes to the "current" lease file.
+       * Therefore, in test mode we need to point db_file to a disposable
+       * file to protect the original lease file. */
+       current_db_path = (test_mode ? "/dev/null" : path_dhcpd_db);
+       db_file = fopen (current_db_path, "a");
+       if (!db_file) {
+               log_fatal ("Can't open %s for append.", current_db_path);
+       }
+
+       expire_all_pools ();
 #if defined (TRACING)
-               if (trace_playback ())
-                       write_time = cur_time;
-               else
+       if (trace_playback ())
+               write_time = cur_time;
+       else
 #endif
-                       time(&write_time);
-               new_lease_file ();
-       }
+               time(&write_time);
+       new_lease_file (test_mode);
 
 #if defined(REPORT_HASH_PERFORMANCE)
        log_info("Host HW hash:   %s", host_hash_report(host_hw_addr_hash));
@@ -1105,7 +1109,7 @@ void db_startup (testp)
 #endif
 }
 
-int new_lease_file ()
+int new_lease_file (int test_mode)
 {
        char newfname [512];
        char backfname [512];
@@ -1194,6 +1198,14 @@ int new_lease_file ()
        if (!write_leases ())
                goto fail;
 
+       if (test_mode) {
+               log_debug("Lease file test successful,"
+                         " removing temp lease file: %s",
+                         newfname);
+               (void)unlink (newfname);
+               return (1);
+       }
+
 #if defined (TRACING)
        if (!trace_playback ()) {
 #endif
index f45637a69c7dd863a74809c3c0660a33e563cbc4..0d0901c3c6582d2798c8ad7b095f0e289b5fb6f5 100644 (file)
@@ -269,8 +269,11 @@ automatically before installing it.
 .BI \-T
 Test the lease file.  The server tests the lease file
 for correct syntax, but will not attempt to perform any network
-operations.  This can be used to test a new lease file
-automatically before installing it.
+operations.  In addition to reading the lease file it will also
+write the leases to a temporary lease file.  The current lease
+file will not be modified and the temporary lease file will be
+removed upon completion of the test. This can be used to test a
+new lease file automatically before installing it.
 .TP
 .BI \-user \ user
 Setuid to user after completing privileged operations,