d9898ee8 |
1 | #! @PERL@ |
2 | # |
3 | # Create userdb database |
4 | # |
5 | # Usage: makeuserdb |
6 | # |
7 | # $Id: makeuserdb.in,v 1.13 2006/03/25 13:31:21 mrsam Exp $ |
8 | # |
9 | # Copyright 1998 - 2006 Double Precision, Inc. See COPYING for |
10 | # distribution information. |
11 | |
12 | use Fcntl ':flock'; |
13 | |
14 | $prefix="@prefix@"; |
15 | $exec_prefix="@exec_prefix@"; |
16 | $bindir="@bindir@"; |
17 | |
18 | $ENV{'PATH'}="@bindir@:/usr/bin:/usr/local/bin:/bin"; |
19 | |
20 | $dbfile="@userdb@"; |
21 | |
22 | $makedat="@makedat@"; |
23 | |
24 | $name=shift @ARGV; |
25 | if ($name eq "-f") { |
26 | $dbfile=shift @ARGV; |
27 | $dbfile=~s/\/$//; |
28 | } |
29 | |
30 | $datfile=$dbfile.".dat"; |
31 | # XXX the lock file here is etc/userdb.lock but the userdb command uses etc/.lock.userdb |
32 | $lockfile=$dbfile.".lock"; |
33 | $shadowfile=$dbfile."shadow.dat"; |
34 | $tmpdatfile=$dbfile.".tmp"; |
35 | $tmpshadowfile=$dbfile."shadow.tmp"; |
36 | |
37 | $mode=(stat($dbfile))[2]; |
38 | die "$dbfile: not found.\n" unless defined $mode; |
39 | |
40 | die "$dbfile: MAY NOT HAVE GROUP OR WORLD PERMISSIONS!!\n" |
41 | if ( $mode & 077); |
42 | |
43 | eval { |
44 | die "SYMLINK\n" if -l $dbfile; |
45 | }; |
46 | |
47 | die "ERROR: Wrong makeuserdb command.\n ($dbfile is a symbolic link)\n" |
48 | if $@ eq "SYMLINK\n"; |
49 | |
50 | eval { |
51 | die "SYMLINK\n" if -l $datfile; |
52 | }; |
53 | |
54 | die "ERROR: Wrong makeuserdb command.\n ($datfile is a symbolic link)\n" |
55 | if $@ eq "SYMLINK\n"; |
56 | |
57 | eval { |
58 | die "SYMLINK\n" if -l $shadowfile; |
59 | }; |
60 | |
61 | die "ERROR: Wrong makeuserdb command.\n ($shadowfile is a symbolic link)\n" |
62 | if $@ eq "SYMLINK\n"; |
63 | |
64 | umask (022); |
65 | open(LOCK, ">$lockfile") or die "Can't open $lockfile: $!"; |
66 | flock(LOCK,LOCK_EX) || die "Can't lock $lockfile: $!"; |
67 | |
68 | open (DBPIPE, "| ${makedat} - $tmpdatfile $datfile") || die "$!\n"; |
69 | umask (066); |
70 | open (SHADOWPIPE, "| ${makedat} - $tmpshadowfile $shadowfile") |
71 | || die "$!\n"; |
72 | |
73 | eval { |
74 | |
75 | if ( -d $dbfile ) |
76 | { |
77 | my (@dirs); |
78 | my (@files); |
79 | |
80 | push @dirs, $dbfile; |
81 | while ( $#dirs >= 0 ) |
82 | { |
83 | $dir=shift @dirs; |
84 | opendir(DIR, $dir) || die "$!\n"; |
85 | while ( defined($filename=readdir(DIR))) |
86 | { |
87 | next if $filename =~ /^\./; |
88 | if ( -d "$dir/$filename" ) |
89 | { |
90 | push @dirs, "$dir/$filename"; |
91 | } |
92 | else |
93 | { |
94 | push @files, "$dir/$filename"; |
95 | } |
96 | } |
97 | closedir(DIR); |
98 | } |
99 | |
100 | while (defined ($filename=shift @files)) |
101 | { |
102 | &do_file( $filename ); |
103 | } |
104 | } |
105 | else |
106 | { |
107 | &do_file( $dbfile ); |
108 | } |
109 | |
110 | print DBPIPE ".\n" || die "$!\n"; |
111 | print SHADOWPIPE ".\n" || die "$!\n"; |
112 | } ; |
113 | |
114 | $err=$@; |
115 | if ($err) |
116 | { |
117 | print "$err"; |
118 | exit (1); |
119 | } |
120 | |
121 | close(DBPIPE) || die "$!\n"; |
122 | exit (1) if $?; |
123 | close(SHADOWPIPE) || die "$!\n"; |
124 | exit (1) if $?; |
125 | |
126 | exit (0); |
127 | |
128 | sub do_file { |
129 | my ($filename)=@_; |
130 | my ($addr, $fields); |
131 | my (@nonshadow, @shadow); |
132 | |
133 | my $location=substr($filename, length("@userdb@")); |
134 | |
135 | $location =~ s/^\///; |
136 | $location =~ s/\/$//; |
137 | $location .= "/" if $location ne ""; |
138 | |
139 | open (F, $filename) || die "$filename: $!\n"; |
140 | while (<F>) |
141 | { |
142 | if ( /^[\n#]/ || ! /^([^\t]*)\t(.*)/ ) |
143 | { |
144 | print DBPIPE; |
145 | print SHADOWPIPE; |
146 | next; |
147 | } |
148 | ($addr,$fields)=($1,$2); |
149 | undef @nonshadow; |
150 | undef @shadow; |
151 | |
152 | foreach ( split (/\|/, $fields ) ) |
153 | { |
154 | if ( /^[^=]*pw=/ ) |
155 | { |
156 | push @shadow, $_; |
157 | } |
158 | else |
159 | { |
160 | push @nonshadow, $_; |
161 | } |
162 | } |
163 | |
164 | push @nonshadow, "_=$location"; |
165 | ( print DBPIPE "$addr\t" . join("|", @nonshadow) . "\n" |
166 | || die "$!\n" ) if $#nonshadow >= 0; |
167 | ( print SHADOWPIPE "$addr\t" . join("|", @shadow) . "\n" |
168 | || die "$!\n" ) if $#shadow >= 0; |
169 | } |
170 | print DBPIPE "\n"; |
171 | print SHADOWPIPE "\n"; |
172 | } |