]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#805, !6-p] check for null or empty hwaddr before writing to memfile (v4)
authorRazvan Becheriu <razvan@isc.org>
Fri, 9 Aug 2019 15:23:47 +0000 (18:23 +0300)
committerThomas Markwalder <tmark@isc.org>
Fri, 16 Aug 2019 22:30:22 +0000 (18:30 -0400)
src/lib/dhcpsrv/csv_lease_file4.cc
src/lib/dhcpsrv/tests/csv_lease_file4_unittest.cc

index 646da9f25df71c9e365d5b99e319f2d511379874..3d4c707802810a6f400561f86ed811a0d8da3839 100644 (file)
@@ -36,13 +36,22 @@ CSVLeaseFile4::append(const Lease4& lease) {
 
     CSVRow row(getColumnCount());
     row.writeAt(getColumnIndex("address"), lease.addr_.toText());
+    std::string hwaddr_text;
     if (!lease.hwaddr_) {
         // Bump the error counter
         ++write_errs_;
 
         isc_throw(BadValue, "Lease4 must have hardware address specified.");
+    } else {
+        hwaddr_text = lease.hwaddr_->toText(false);
+        if (hwaddr_text.empty() && lease.state_ != Lease::STATE_DECLINED) {
+            // Bump the error counter
+            ++write_errs_;
+
+            isc_throw(BadValue, "Lease4 must have non empty hardware address.");
+        }
     }
-    row.writeAt(getColumnIndex("hwaddr"), lease.hwaddr_->toText(false));
+    row.writeAt(getColumnIndex("hwaddr"), hwaddr_text);
     // Client id may be unset (NULL).
     if (lease.client_id_) {
         row.writeAt(getColumnIndex("client_id"), lease.client_id_->toText());
index 23bfce4ed4cba9a1b1fddd5589a63e887590f215..f85efc5f6bea886492ca38d8f98dc47763fca647 100644 (file)
@@ -462,6 +462,70 @@ TEST_F(CSVLeaseFile4Test, highLeaseLifetime) {
     EXPECT_EQ(lease->cltt_, lease_read->cltt_);
 }
 
+// Verifies that it is not possible to output a lease with empty hwaddr in other
+// than the declined state
+TEST_F(CSVLeaseFile4Test, emptyHWAddrDefaultStateOnly) {
+    CSVLeaseFile4 lf(filename_);
+    ASSERT_NO_THROW(lf.recreate());
+    ASSERT_TRUE(io_.exists());
+
+    HWAddrPtr hwaddr;
+
+    // Create lease with null hwaddr and default state
+    Lease4Ptr lease_null_hwaddr(new Lease4(IOAddress("192.0.3.2"),
+                                hwaddr,
+                                NULL, 0,
+                                0xFFFFFFFF, time(0),
+                                8, true, true,
+                                "host.example.com"));
+    // Try to write this lease out to the lease file.
+    ASSERT_THROW(lf.append(*lease_null_hwaddr), BadValue);
+
+    hwaddr.reset(new HWAddr());
+
+    //Create lease with empty hdaddr and default state
+    Lease4Ptr lease_empty_hwaddr(new Lease4(IOAddress("192.0.3.2"),
+                                 hwaddr,
+                                 NULL, 0,
+                                 0xFFFFFFFF, time(0),
+                                 8, true, true,
+                                 "host.example.com"));
+    // Try to write this lease out to the lease file.
+    ASSERT_THROW(lf.append(*lease_empty_hwaddr), BadValue);
+
+    // Create lease with hwaddr and current time.
+    Lease4Ptr lease(new Lease4(IOAddress("192.0.3.2"),
+                               hwaddr0_,
+                               NULL, 0,
+                               0xFFFFFFFF, time(0),
+                               8, true, true,
+                               "host.example.com"));
+
+    // Decline the lease
+    lease->decline(1000);
+    ASSERT_TRUE(lease->hwaddr_);
+    EXPECT_EQ(lease->hwaddr_->toText(false), "");
+
+    // Write this lease out to the lease file.
+    ASSERT_NO_THROW(lf.append(*lease));
+
+    // Close the lease file.
+    lf.close();
+
+    Lease4Ptr lease_read;
+
+    // Re-open the file for reading.
+    ASSERT_NO_THROW(lf.open());
+
+    // Read the lease and make sure it is successful.
+    EXPECT_TRUE(lf.next(lease_read));
+    ASSERT_TRUE(lease_read);
+
+    // The valid lifetime and the cltt should match with the original lease.
+    EXPECT_EQ(lease->valid_lft_, lease_read->valid_lft_);
+    EXPECT_EQ(lease->cltt_, lease_read->cltt_);
+}
+
 /// @todo Currently we don't check invalid lease attributes, such as invalid
 /// lease type, invalid preferred lifetime vs valid lifetime etc. The Lease6
 /// should be extended with the function that validates lease attributes. Once