]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
perl: Verify database when it is being opened
authorStefan Schantl <stefan.schantl@ipfire.org>
Fri, 29 Nov 2019 19:01:45 +0000 (19:01 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Nov 2019 19:04:21 +0000 (19:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/perl/Location.xs
src/perl/t/Location.t

index b181e4cc39f553ff7ceabcc6eae4d4a8d80007f7..2c83b3ded0b5f623b6342b067fed2021e2d52c60 100644 (file)
@@ -209,7 +209,8 @@ build-perl:
 
 .PHONY: check-perl
 check-perl: testdata.db
-       cd $(builddir)/src/perl && $(MAKE) LD_LIBRARY_PATH="$(abs_builddir)/src/.libs" test database="../../$<"
+       cd $(builddir)/src/perl && $(MAKE) LD_LIBRARY_PATH="$(abs_builddir)/src/.libs" test \
+               database="../../$<" keyfile="$(abs_srcdir)/examples/public-key.pem"
 
 .PHONY: install-perl
 install-perl:
index b6c4dc52c53796b7b18d91b2ca8c35bb1d43e6b5..5071f85bcffd23236bf16027208be75de35a1b00 100644 (file)
@@ -15,8 +15,9 @@
 MODULE = Location              PACKAGE = Location
 
 struct loc_database *
-init(file)
+init(file, keyfile)
        char* file;
+       char* keyfile;
 
        CODE:
                struct loc_ctx* ctx = NULL;
@@ -49,6 +50,29 @@ init(file)
                        croak("Could not read database: %s\n", file);
                }
 
+               // Try to open the keyfile
+               f = fopen(keyfile, "r");
+               if (!f) {
+                       loc_database_unref(db);
+                       loc_unref(ctx);
+
+                       croak("Could not open keyfile %s: %s\n",
+                               keyfile, strerror(errno));
+               }
+
+               // Verify the database
+               int status = loc_database_verify(db, f);
+               if (status) {
+                       loc_database_unref(db);
+                       loc_unref(ctx);
+                       fclose(f);
+
+                       croak("Could not verify the database signature\n");
+               }
+
+               // Close the keyfile
+               fclose(f);
+
                // Cleanup
                loc_unref(ctx);
 
index fd43946deb447b4775dae771b9e1895be2d9f12a..71cdce8badb07d24991292de8a9d08d8b72acb9e 100644 (file)
@@ -10,6 +10,7 @@ use warnings;
 
 # Where to find the test database.
 my $testdb = $ENV{'database'};
+my $keyfile = $ENV{'keyfile'};
 
 use Test::More tests => 6;
 BEGIN { use_ok('Location') };
@@ -23,7 +24,7 @@ BEGIN { use_ok('Location') };
 my $address = "2a07:1c44:5800::1";
 
 # Connect to the database.
-my $db = &Location::init("$testdb");
+my $db = &Location::init("$testdb", "$keyfile");
 
 my $vendor = &Location::get_vendor($db);
 ok($vendor eq "IPFire Project", "Test 1 - Get Database Vendor");