2 # ====================================================================
3 # The Apache Software License, Version 1.1
5 # Copyright (c) 2000 The Apache Software Foundation. All rights
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
12 # 1. Redistributions of source code must retain the above copyright
13 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in
17 # the documentation and/or other materials provided with the
20 # 3. The end-user documentation included with the redistribution,
21 # if any, must include the following acknowledgment:
22 # "This product includes software developed by the
23 # Apache Software Foundation (http://www.apache.org/)."
24 # Alternately, this acknowledgment may appear in the software itself,
25 # if and wherever such third-party acknowledgments normally appear.
27 # 4. The names "Apache" and "Apache Software Foundation" must
28 # not be used to endorse or promote products derived from this
29 # software without prior written permission. For written
30 # permission, please contact apache@apache.org.
32 # 5. Products derived from this software may not be called "Apache",
33 # nor may "Apache" appear in their name, without prior written
34 # permission of the Apache Software Foundation.
36 # THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37 # WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 # DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40 # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 # ====================================================================
50 # This software consists of voluntary contributions made by many
51 # individuals on behalf of the Apache Software Foundation. For more
52 # information on the Apache Software Foundation, please see
53 # <http://www.apache.org/>.
55 # Portions of this software are based upon public domain software
56 # originally written at the National Center for Supercomputing Applications,
57 # University of Illinois, Urbana-Champaign.
59 #for more functionality see the HTTPD::UserAdmin module:
60 # http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
62 # usage: dbmmanage <DBMfile> <command> <key> <value>
66 BEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File) }
71 my($file,$command,$key,$crypted_pwd) = @ARGV;
73 usage() unless $file and $command and defined &{$dbmc::{$command}};
75 # if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
76 # four bytes of iteration count and four bytes of salt). Otherwise, just use
77 # the traditional two-byte salt.
78 # see the man page on your system to decide if you have a newer crypt() lib.
79 # I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
80 # The new style crypt() allows up to 20 characters of the password to be
81 # significant rather than only 8.
82 my $newstyle_salt = join '|', qw{bsdos}; #others?
84 # remove extension if any
85 my $chop = join '|', qw{db.? pag dir};
86 $file =~ s/\.($chop)$//;
88 my $is_update = $command eq "update";
89 my $Is_Win32 = $^O eq "MSWin32";
92 my($mode, $flags) = $command =~
93 /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
95 tie %DB, "AnyDBM_File", $file, $flags, $mode || die "Can't tie $file: $!";
100 my $cmds = join "|", sort keys %dbmc::;
101 die "usage: $0 filename [$cmds] [username]\n";
107 for (qw(-xlwwa -le)) {
109 $psf = $_, last unless $?;
111 srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
112 @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
113 $x = int scalar @range;
117 join '', map $range[rand $x], 1..shift||1;
121 my $newstyle = $^O =~ /(?:$newstyle_salt)/;
122 genseed() unless @range;
124 join '', "_", randchar, "a..", randchar(4) :
129 my $prompt = shift || "Enter password:";
132 open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
133 system "stty -echo;";
137 print STDERR $prompt;
138 while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
142 system "stty echo" unless $Is_Win32;
144 die "Can't use empty password!\n" unless length $pwd;
149 die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
154 die "Can't use empty password!\n" unless $crypted_pwd;
156 die "Sorry, user `$key' already exists!\n" if $DB{$key};
158 $DB{$key} = $crypted_pwd;
159 my $action = $is_update ? "updated" : "added";
160 print "User $key $action with password encrypted to $DB{$key}\n";
164 my $value = getpass "New password:";
165 die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
166 $crypted_pwd = crypt $value, caller->salt;
171 die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
172 delete $DB{$key}, print "`$key' deleted\n";
176 print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
180 die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
181 print crypt(getpass(), $DB{$key}) eq $DB{$key} ? "password ok\n" : "password mismatch\n";
185 while(defined($_ = <STDIN>) and chomp) {
186 ($key,$crypted_pwd) = split /:/, $_, 2;