dbmmanage revision 09fe0b69d3d1e8c8041c9ce99ee77b8b44b5e3b1
1N/A#!/usr/local/bin/perl
1N/A
1N/A# ====================================================================
1N/A# Copyright (c) 1995-1999 The Apache Group. All rights reserved.
1N/A#
1N/A# Redistribution and use in source and binary forms, with or without
1N/A# modification, are permitted provided that the following conditions
1N/A# are met:
1N/A#
1N/A# 1. Redistributions of source code must retain the above copyright
1N/A# notice, this list of conditions and the following disclaimer.
1N/A#
1N/A# 2. Redistributions in binary form must reproduce the above copyright
1N/A# notice, this list of conditions and the following disclaimer in
1N/A# the documentation and/or other materials provided with the
1N/A# distribution.
1N/A#
1N/A# 3. All advertising materials mentioning features or use of this
1N/A# software must display the following acknowledgment:
1N/A# "This product includes software developed by the Apache Group
1N/A# for use in the Apache HTTP server project (http://www.apache.org/)."
1N/A#
1N/A# 4. The names "Apache Server" and "Apache Group" must not be used to
1N/A# endorse or promote products derived from this software without
1N/A# prior written permission. For written permission, please contact
1N/A# apache@apache.org.
1N/A#
1N/A# 5. Products derived from this software may not be called "Apache"
1N/A# nor may "Apache" appear in their names without prior written
1N/A# permission of the Apache Group.
1N/A#
1N/A# 6. Redistributions of any form whatsoever must retain the following
1N/A# acknowledgment:
1N/A# "This product includes software developed by the Apache Group
1N/A# for use in the Apache HTTP server project (http://www.apache.org/)."
1N/A#
1N/A# THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
1N/A# EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1N/A# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1N/A# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
1N/A# ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1N/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1N/A# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1N/A# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1N/A# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1N/A# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1N/A# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1N/A# OF THE POSSIBILITY OF SUCH DAMAGE.
1N/A# ====================================================================
1N/A#
1N/A# This software consists of voluntary contributions made by many
1N/A# individuals on behalf of the Apache Group and was originally based
1N/A# on public domain software written at the National Center for
1N/A# Supercomputing Applications, University of Illinois, Urbana-Champaign.
1N/A# For more information on the Apache Group and the Apache HTTP server
1N/A# project, please see <http://www.apache.org/>.
1N/A
1N/A#for more functionality see the HTTPD::UserAdmin module:
1N/A# http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
1N/A#
1N/A# usage: dbmmanage <DBMfile> <command> <key> <value>
1N/A
1N/Apackage dbmmanage;
1N/A# -ldb -lndbm -lgdbm
1N/ABEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File) }
1N/Ause strict;
1N/Ause Fcntl;
1N/Ause AnyDBM_File ();
1N/A
1N/Amy($file,$command,$key,$crypted_pwd) = @ARGV;
1N/A
1N/Ausage() unless $file and $command and defined &{$dbmc::{$command}};
1N/A
1N/A# if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
1N/A# four bytes of iteration count and four bytes of salt). Otherwise, just use
1N/A# the traditional two-byte salt.
1N/A# see the man page on your system to decide if you have a newer crypt() lib.
1N/A# I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
1N/A# The new style crypt() allows up to 20 characters of the password to be
1N/A# significant rather than only 8.
1N/Amy $newstyle_salt = join '|', qw{bsdos}; #others?
1N/A
1N/A# remove extension if any
1N/Amy $chop = join '|', qw{db.? pag dir};
1N/A$file =~ s/\.($chop)$//;
1N/A
1N/Amy $is_update = $command eq "update";
1N/Amy $Is_Win32 = $^O eq "MSWin32";
1N/Amy %DB = ();
1N/Amy @range = ();
1N/Amy($mode, $flags) = $command =~
1N/A /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
1N/A
1N/Atie %DB, "AnyDBM_File", $file, $flags, $mode || die "Can't tie $file: $!";
1N/Adbmc->$command();
1N/Auntie %DB;
1N/A
1N/Asub usage {
1N/A my $cmds = join "|", sort keys %dbmc::;
1N/A die "usage: $0 filename [$cmds] [username]\n";
1N/A}
1N/A
1N/Amy $x;
1N/Asub genseed {
1N/A my $psf;
1N/A for (qw(-xlwwa -le)) {
1N/A `ps $_ 2>/dev/null`;
1N/A $psf = $_, last unless $?;
1N/A }
1N/A srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
1N/A @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
1N/A $x = int scalar @range;
1N/A}
1N/A
1N/Asub randchar {
1N/A join '', map $range[rand $x], 1..shift||1;
1N/A}
1N/A
1N/Asub salt {
1N/A my $newstyle = $^O =~ /(?:$newstyle_salt)/;
1N/A genseed() unless @range;
1N/A return $newstyle ?
1N/A join '', "_", randchar, "a..", randchar(4) :
1N/A randchar(2);
1N/A}
1N/A
1N/Asub getpass {
1N/A my $prompt = shift || "Enter password:";
1N/A
1N/A unless($Is_Win32) {
1N/A open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
1N/A system "stty -echo;";
1N/A }
my($c,$pwd);
print STDERR $prompt;
while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
$pwd .= $c;
}
system "stty echo" unless $Is_Win32;
print STDERR "\n";
die "Can't use empty password!\n" unless length $pwd;
return $pwd;
}
sub dbmc::update {
die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
dbmc->adduser;
}
sub dbmc::add {
die "Can't use empty password!\n" unless $crypted_pwd;
unless($is_update) {
die "Sorry, user `$key' already exists!\n" if $DB{$key};
}
$DB{$key} = $crypted_pwd;
my $action = $is_update ? "updated" : "added";
print "User $key $action with password encrypted to $DB{$key}\n";
}
sub dbmc::adduser {
my $value = getpass "New password:";
die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
$crypted_pwd = crypt $value, caller->salt;
dbmc->add;
}
sub dbmc::delete {
die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
delete $DB{$key}, print "`$key' deleted\n";
}
sub dbmc::view {
print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
}
sub dbmc::check {
die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
print crypt(getpass(), $DB{$key}) eq $DB{$key} ? "password ok\n" : "password mismatch\n";
}
sub dbmc::import {
while(defined($_ = <STDIN>) and chomp) {
($key,$crypted_pwd) = split /:/, $_, 2;
dbmc->add;
}
}