summaryrefslogtreecommitdiffstats
path: root/userdb/makeuserdb.in
diff options
context:
space:
mode:
authorSam Varshavchik2013-08-19 16:39:41 -0400
committerSam Varshavchik2013-08-25 14:43:51 -0400
commit9c45d9ad13fdf439d44d7443ae75da15ea0223ed (patch)
tree7a81a04cb51efb078ee350859a64be2ebc6b8813 /userdb/makeuserdb.in
parenta9520698b770168d1f33d6301463bb70a19655ec (diff)
downloadcourier-libs-9c45d9ad13fdf439d44d7443ae75da15ea0223ed.tar.bz2
Initial checkin
Imported from subversion report, converted to git. Updated all paths in scripts and makefiles, reflecting the new directory hierarchy.
Diffstat (limited to 'userdb/makeuserdb.in')
-rw-r--r--userdb/makeuserdb.in171
1 files changed, 171 insertions, 0 deletions
diff --git a/userdb/makeuserdb.in b/userdb/makeuserdb.in
new file mode 100644
index 0000000..4765309
--- /dev/null
+++ b/userdb/makeuserdb.in
@@ -0,0 +1,171 @@
+#! @PERL@
+#
+# Create userdb database
+#
+# Usage: makeuserdb
+#
+#
+# Copyright 1998 - 2006 Double Precision, Inc. See COPYING for
+# distribution information.
+
+use Fcntl ':flock';
+
+$prefix="@prefix@";
+$exec_prefix="@exec_prefix@";
+$bindir="@bindir@";
+
+$ENV{'PATH'}="@bindir@:/usr/bin:/usr/local/bin:/bin";
+
+$dbfile="@userdb@";
+
+$makedat="@makedat@";
+
+$name=shift @ARGV;
+if ($name eq "-f") {
+ $dbfile=shift @ARGV;
+ $dbfile=~s/\/$//;
+}
+
+$datfile=$dbfile.".dat";
+# XXX the lock file here is etc/userdb.lock but the userdb command uses etc/.lock.userdb
+$lockfile=$dbfile.".lock";
+$shadowfile=$dbfile."shadow.dat";
+$tmpdatfile=$dbfile.".tmp";
+$tmpshadowfile=$dbfile."shadow.tmp";
+
+$mode=(stat($dbfile))[2];
+die "$dbfile: not found.\n" unless defined $mode;
+
+die "$dbfile: MAY NOT HAVE GROUP OR WORLD PERMISSIONS!!\n"
+ if ( $mode & 077);
+
+eval {
+ die "SYMLINK\n" if -l $dbfile;
+};
+
+die "ERROR: Wrong makeuserdb command.\n ($dbfile is a symbolic link)\n"
+ if $@ eq "SYMLINK\n";
+
+eval {
+ die "SYMLINK\n" if -l $datfile;
+};
+
+die "ERROR: Wrong makeuserdb command.\n ($datfile is a symbolic link)\n"
+ if $@ eq "SYMLINK\n";
+
+eval {
+ die "SYMLINK\n" if -l $shadowfile;
+};
+
+die "ERROR: Wrong makeuserdb command.\n ($shadowfile is a symbolic link)\n"
+ if $@ eq "SYMLINK\n";
+
+umask (022);
+open(LOCK, ">$lockfile") or die "Can't open $lockfile: $!";
+flock(LOCK,LOCK_EX) || die "Can't lock $lockfile: $!";
+
+open (DBPIPE, "| ${makedat} - $tmpdatfile $datfile") || die "$!\n";
+umask (066);
+open (SHADOWPIPE, "| ${makedat} - $tmpshadowfile $shadowfile")
+ || die "$!\n";
+
+eval {
+
+ if ( -d $dbfile )
+ {
+ my (@dirs);
+ my (@files);
+
+ push @dirs, $dbfile;
+ while ( $#dirs >= 0 )
+ {
+ $dir=shift @dirs;
+ opendir(DIR, $dir) || die "$!\n";
+ while ( defined($filename=readdir(DIR)))
+ {
+ next if $filename =~ /^\./;
+ if ( -d "$dir/$filename" )
+ {
+ push @dirs, "$dir/$filename";
+ }
+ else
+ {
+ push @files, "$dir/$filename";
+ }
+ }
+ closedir(DIR);
+ }
+
+ while (defined ($filename=shift @files))
+ {
+ &do_file( $filename );
+ }
+ }
+ else
+ {
+ &do_file( $dbfile );
+ }
+
+ print DBPIPE ".\n" || die "$!\n";
+ print SHADOWPIPE ".\n" || die "$!\n";
+} ;
+
+$err=$@;
+if ($err)
+{
+ print "$err";
+ exit (1);
+}
+
+close(DBPIPE) || die "$!\n";
+exit (1) if $?;
+close(SHADOWPIPE) || die "$!\n";
+exit (1) if $?;
+
+exit (0);
+
+sub do_file {
+my ($filename)=@_;
+my ($addr, $fields);
+my (@nonshadow, @shadow);
+
+my $location=substr($filename, length("@userdb@"));
+
+ $location =~ s/^\///;
+ $location =~ s/\/$//;
+ $location .= "/" if $location ne "";
+
+ open (F, $filename) || die "$filename: $!\n";
+ while (<F>)
+ {
+ if ( /^[\n#]/ || ! /^([^\t]*)\t(.*)/ )
+ {
+ print DBPIPE;
+ print SHADOWPIPE;
+ next;
+ }
+ ($addr,$fields)=($1,$2);
+ undef @nonshadow;
+ undef @shadow;
+
+ foreach ( split (/\|/, $fields ) )
+ {
+ if ( /^[^=]*pw=/ )
+ {
+ push @shadow, $_;
+ }
+ else
+ {
+ push @nonshadow, $_;
+ }
+ }
+
+ push @nonshadow, "_=$location";
+ ( print DBPIPE "$addr\t" . join("|", @nonshadow) . "\n"
+ || die "$!\n" ) if $#nonshadow >= 0;
+ ( print SHADOWPIPE "$addr\t" . join("|", @shadow) . "\n"
+ || die "$!\n" ) if $#shadow >= 0;
+ }
+ print DBPIPE "\n";
+ print SHADOWPIPE "\n";
+}