DB-Autocreation
authorheiko
Tue, 02 Jan 2007 21:36:00 +0000
changeset 16 b47059897e9b
parent 15 3044f5348418
child 17 43cfd824c314
DB-Autocreation
DB-Name/Location
debian/changelog
exim-exigrey.pl
--- a/debian/changelog	Tue Jan 02 20:10:41 2007 +0000
+++ b/debian/changelog	Tue Jan 02 21:36:00 2007 +0000
@@ -1,3 +1,11 @@
+exigrey (0.5-3) stable; urgency=low
+
+  * new upstream: 
+    - autocreation of files/dirs
+    - default db location/name changed
+
+ -- Heiko Schlittermann <hs@schlittermann.de>  Tue, 02 Jan 2007 22:35:21 +0100
+
 exigrey (0.4-3) stable; urgency=low
 
   * depends on exim4-daemon-heavy
--- a/exim-exigrey.pl	Tue Jan 02 20:10:41 2007 +0000
+++ b/exim-exigrey.pl	Tue Jan 02 21:36:00 2007 +0000
@@ -15,7 +15,7 @@
 
 my %DEFAULT = (
 	delay => 600,
-	db => "seen.db",
+	db => "seen",
 );
 
 sub unseen($;$$);
@@ -29,8 +29,8 @@
 # Usage:
 # 	${perl{unseen}{$sender_host_address}}
 # 	${perl{unseen}{$sender_host_address}{600}}
-# 	${perl{unseen}{$sender_host_address}{600}{seen.db}}
-# 	${perl{unseen}{$sender_host_address}{600}{/some/dir/seen.db}}
+# 	${perl{unseen}{$sender_host_address}{600}{seen}}
+# 	${perl{unseen}{$sender_host_address}{600}{$spool_directory/grey/seen}}
 #
 # record structure: key:   item\0 
 #                   value: timestamp(creation) timestamp(usage)\0
@@ -66,14 +66,23 @@
 # If we're running under exim it's easy, otherwise we've to find exim
 # and then ask...
 sub getDBDir() {
-	my $spooldir;
+	my ($spooldir, $dbdir);
 	eval { $spooldir = Exim::expand_string('$spool_directory') };
 	if (not defined $spooldir) {
 		my $exim = findExim();
 		chomp($spooldir = `$exim -be '\$spool_directory'`);
 		die "Can't find spooldir" if not defined $spooldir;
 	}
-	return "$spooldir/db";
+	-d ($dbdir = "$spooldir/grey") and return $dbdir;
+
+	my ($mode, $owner, $group) = (stat $spooldir)[2,4,5];
+	{
+	    local $) = $group;
+	    local $> = $owner;
+	    $mode &= 0777;
+	    mkdir $dbdir, $mode or die "Can't create $dbdir: $!";
+	}
+	return $dbdir;
 }
 
 sub findExim(;$) {
@@ -90,18 +99,29 @@
     my ($h, $db) = @_;
     $db = getDBDir() ."/$db" unless $db =~ /^\//;
 
+    # Creation of DB-File if it doesn't exist
+    # to avoid races we change our own uid/gid for creation of
+    # this file
+    if (!-f $db) {
+	(my $dir = $db) =~ s/^(.*)\/.*?$/$1/;
+	local ($>, $)) = (stat $dir)[4,5] or die "Can't stat $dir: $!";
+	open(X, ">>$db") or die "Can't create $db: $!";
+	close(X);
+    }
+
+    # now test which of the DB-Modules has been loaded
+
     if (exists &BerkeleyDB::Hash::TIEHASH) {
 	no strict;
 	my $umask = umask 077;
 	tie %$h, "BerkeleyDB::Hash", 
-	    -Filename => $db,
-	    -Flags => DB_CREATE
+	    -Filename => $db
 	or die "$0: $db: $!";
 	return $db;
     }
 
     if (exists &DB_File::TIEHASH) {
-	tie %$h, "DB_File", $db, undef, 0600
+	tie %$h, "DB_File", $db
 	or die "$0: $db: $!";
 	return $db;
     }