makeuserdb.in
上传用户:s81996212
上传日期:2007-01-04
资源大小:722k
文件大小:3k
- #! @PERL@
- #
- # Create userdb database
- #
- # Usage: makeuserdb
- #
- # $Id: makeuserdb.in,v 1.6 2000/02/23 05:11:13 mrsam Exp $
- #
- # Copyright 1998 - 1999 Double Precision, Inc. See COPYING for
- # distribution information.
- use Fcntl ':flock';
- $prefix="@prefix@";
- $exec_prefix="@exec_prefix@";
- $bindir="@bindir@";
- $dbfile="@userdb@";
- $datfile="@userdb@.dat";
- $lockfile="@userdb@.lock";
- $shadowfile="@userdb@shadow.dat";
- $tmpdir="@tmpdir@";
- $tmpdatfile="$tmpdir/userdb.tmp";
- $tmpshadowfile="$tmpdir/userdbshadow.tmp";
- $makedat="@makedat@";
- $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 "SYMLINKn" if -l $dbfile;
- };
- die "ERROR: Wrong makeuserdb command.n ($dbfile is a symbolic link)n"
- if $@ eq "SYMLINKn";
- eval {
- die "SYMLINKn" if -l $datfile;
- };
- die "ERROR: Wrong makeuserdb command.n ($datfile is a symbolic link)n"
- if $@ eq "SYMLINKn";
- eval {
- die "SYMLINKn" if -l $shadowfile;
- };
- die "ERROR: Wrong makeuserdb command.n ($shadowfile is a symbolic link)n"
- if $@ eq "SYMLINKn";
- 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=$@;
- close(DBPIPE) || die "$!n";
- exit (1) if $?;
- close(SHADOWPIPE) || die "$!n";
- exit (1) if $?;
- if ($err)
- {
- print $err;
- exit (1);
- }
- exit (0);
- sub do_file {
- my ($filename)=@_;
- my ($addr, $fields);
- my (@nonshadow, @shadow);
- open (F, $filename) || die "$!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, $_;
- }
- }
- ( print DBPIPE "$addrt" . join("|", @nonshadow) . "n"
- || die "$!n" ) if $#nonshadow >= 0;
- ( print SHADOWPIPE "$addrt" . join("|", @shadow) . "n"
- || die "$!n" ) if $#shadow >= 0;
- }
- }