Blame support/dbmmanage.in

Packit 90a5c9
#!@perlbin@
Packit 90a5c9
#
Packit 90a5c9
# Licensed to the Apache Software Foundation (ASF) under one or more
Packit 90a5c9
# contributor license agreements.  See the NOTICE file distributed with
Packit 90a5c9
# this work for additional information regarding copyright ownership.
Packit 90a5c9
# The ASF licenses this file to You under the Apache License, Version 2.0
Packit 90a5c9
# (the "License"); you may not use this file except in compliance with
Packit 90a5c9
# the License.  You may obtain a copy of the License at
Packit 90a5c9
#
Packit 90a5c9
#     http://www.apache.org/licenses/LICENSE-2.0
Packit 90a5c9
#
Packit 90a5c9
# Unless required by applicable law or agreed to in writing, software
Packit 90a5c9
# distributed under the License is distributed on an "AS IS" BASIS,
Packit 90a5c9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Packit 90a5c9
# See the License for the specific language governing permissions and
Packit 90a5c9
# limitations under the License.
Packit 90a5c9
Packit 90a5c9
#for more functionality see the HTTPD::UserAdmin module:
Packit 90a5c9
# http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
Packit 90a5c9
#
Packit 90a5c9
# usage: dbmmanage <DBMfile> <command> <user> <password> <groups> <comment>
Packit 90a5c9
Packit 90a5c9
package dbmmanage;
Packit 90a5c9
#                               -ldb    -lndbm    -lgdbm    -lsdbm
Packit 90a5c9
BEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File SDBM_File) }
Packit 90a5c9
use strict;
Packit 90a5c9
use Fcntl;
Packit 90a5c9
use AnyDBM_File ();
Packit 90a5c9
Packit 90a5c9
sub usage {
Packit 90a5c9
    my $cmds = join "|", sort keys %dbmc::;
Packit 90a5c9
    die <
Packit 90a5c9
Usage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
Packit 90a5c9
Packit 90a5c9
    where enc is  -d for crypt encryption (default except on Win32, Netware)
Packit 90a5c9
                  -m for MD5 encryption (default on Win32, Netware)
Packit 90a5c9
                  -s for SHA1 encryption
Packit 90a5c9
                  -p for plaintext
Packit 90a5c9
Packit 90a5c9
    command is one of: $cmds
Packit 90a5c9
Packit 90a5c9
    pw of . for update command retains the old password
Packit 90a5c9
    pw of - (or blank) for update command prompts for the password
Packit 90a5c9
Packit 90a5c9
    groups or comment of . (or blank) for update command retains old values
Packit 90a5c9
    groups or comment of - for update command clears the existing value
Packit 90a5c9
    groups or comment of - for add and adduser commands is the empty value
Packit 90a5c9
SYNTAX
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub need_sha1_crypt {
Packit 90a5c9
    if (!eval ('require "Digest/SHA1.pm";')) {
Packit 90a5c9
        print STDERR <
Packit 90a5c9
dbmmanage SHA1 passwords require the interface or the module Digest::SHA1
Packit 90a5c9
available from CPAN:
Packit 90a5c9
Packit 90a5c9
    http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
Packit 90a5c9
Packit 90a5c9
Please install Digest::SHA1 and try again, or use a different crypt option:
Packit 90a5c9
Packit 90a5c9
SHAERR
Packit 90a5c9
        usage();
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub need_md5_crypt {
Packit 90a5c9
    if (!eval ('require "Crypt/PasswdMD5.pm";')) {
Packit 90a5c9
        print STDERR <
Packit 90a5c9
dbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
Packit 90a5c9
Packit 90a5c9
    http://www.cpan.org/modules/by-module/Crypt/Crypt-PasswdMD5-1.1.tar.gz
Packit 90a5c9
Packit 90a5c9
Please install Crypt::PasswdMD5 and try again, or use a different crypt option:
Packit 90a5c9
Packit 90a5c9
MD5ERR
Packit 90a5c9
        usage();
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
# if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
Packit 90a5c9
# four bytes of iteration count and four bytes of salt).  Otherwise, just use
Packit 90a5c9
# the traditional two-byte salt.
Packit 90a5c9
# see the man page on your system to decide if you have a newer crypt() lib.
Packit 90a5c9
# I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
Packit 90a5c9
# The new style crypt() allows up to 20 characters of the password to be
Packit 90a5c9
# significant rather than only 8.
Packit 90a5c9
#
Packit 90a5c9
my $newstyle_salt_platforms = join '|', qw{bsdos}; #others?
Packit 90a5c9
my $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
Packit 90a5c9
Packit 90a5c9
# Some platforms just can't crypt() for Apache
Packit 90a5c9
#
Packit 90a5c9
my $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
Packit 90a5c9
my $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
Packit 90a5c9
Packit 90a5c9
my $crypt_method = "crypt";
Packit 90a5c9
Packit 90a5c9
if ($crypt_not_supported) {
Packit 90a5c9
    $crypt_method = "md5";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
# Some platforms won't jump through our favorite hoops
Packit 90a5c9
#
Packit 90a5c9
my $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
Packit 90a5c9
my $not_unix = $^O =~ /(?:$not_unix_platforms)/;
Packit 90a5c9
Packit 90a5c9
if ($crypt_not_supported) {
Packit 90a5c9
    $crypt_method = "md5";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if (@ARGV[0] eq "-d") {
Packit 90a5c9
    shift @ARGV;
Packit 90a5c9
    if ($crypt_not_supported) {
Packit 90a5c9
        print STDERR
Packit 90a5c9
              "Warning: Apache/$^O does not support crypt()ed passwords!\n\n";
Packit 90a5c9
    }
Packit 90a5c9
    $crypt_method = "crypt";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if (@ARGV[0] eq "-m") {
Packit 90a5c9
    shift @ARGV;
Packit 90a5c9
    $crypt_method = "md5";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if (@ARGV[0] eq "-p") {
Packit 90a5c9
    shift @ARGV;
Packit 90a5c9
    if (!$crypt_not_supported) {
Packit 90a5c9
        print STDERR
Packit 90a5c9
              "Warning: Apache/$^O does not support plaintext passwords!\n\n";
Packit 90a5c9
    }
Packit 90a5c9
    $crypt_method = "plain";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if (@ARGV[0] eq "-s") {
Packit 90a5c9
    shift @ARGV;
Packit 90a5c9
    need_sha1_crypt();
Packit 90a5c9
    $crypt_method = "sha1";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
if ($crypt_method eq "md5") {
Packit 90a5c9
    need_md5_crypt();
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
my($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
Packit 90a5c9
Packit 90a5c9
usage() unless $file and $command and defined &{$dbmc::{$command}};
Packit 90a5c9
Packit 90a5c9
# remove extension if any
Packit 90a5c9
my $chop = join '|', qw{db.? pag dir};
Packit 90a5c9
$file =~ s/\.($chop)$//;
Packit 90a5c9
Packit 90a5c9
my $is_update = $command eq "update";
Packit 90a5c9
my %DB = ();
Packit 90a5c9
my @range = ();
Packit 90a5c9
my($mode, $flags) = $command =~
Packit 90a5c9
    /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
Packit 90a5c9
Packit 90a5c9
tie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
Packit 90a5c9
dbmc->$command();
Packit 90a5c9
untie %DB;
Packit 90a5c9
Packit 90a5c9
Packit 90a5c9
my $x;
Packit 90a5c9
sub genseed {
Packit 90a5c9
    my $psf;
Packit 90a5c9
    if ($not_unix) {
Packit 90a5c9
        srand (time ^ $$ or time ^ ($$ + ($$ << 15)));
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        for (qw(-xlwwa -le)) {
Packit 90a5c9
            `ps $_ 2>/dev/null`;
Packit 90a5c9
            $psf = $_, last unless $?;
Packit 90a5c9
        }
Packit 90a5c9
        srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
Packit 90a5c9
    }
Packit 90a5c9
    @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
Packit 90a5c9
    $x = int scalar @range;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub randchar {
Packit 90a5c9
    join '', map $range[rand $x], 1..shift||1;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub saltpw_crypt {
Packit 90a5c9
    genseed() unless @range;
Packit 90a5c9
    return $newstyle_salt ?
Packit 90a5c9
        join '', "_", randchar, "a..", randchar(4) :
Packit 90a5c9
        randchar(2);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub cryptpw_crypt {
Packit 90a5c9
    my ($pw, $salt) = @_;
Packit 90a5c9
    $salt = saltpw_crypt unless $salt;
Packit 90a5c9
    crypt $pw, $salt;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub saltpw_md5 {
Packit 90a5c9
    genseed() unless @range;
Packit 90a5c9
    randchar(8);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub cryptpw_md5 {
Packit 90a5c9
    my($pw, $salt) = @_;
Packit 90a5c9
    $salt = saltpw_md5 unless $salt;
Packit 90a5c9
    Crypt::PasswdMD5::apache_md5_crypt($pw, $salt);
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub cryptpw_sha1 {
Packit 90a5c9
    my($pw, $salt) = @_;
Packit 90a5c9
    '{SHA}' . Digest::SHA1::sha1_base64($pw) . "=";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub cryptpw {
Packit 90a5c9
    if ($crypt_method eq "md5") {
Packit 90a5c9
        return cryptpw_md5(@_);
Packit 90a5c9
    } elsif ($crypt_method eq "sha1") {
Packit 90a5c9
        return cryptpw_sha1(@_);
Packit 90a5c9
    } elsif ($crypt_method eq "crypt") {
Packit 90a5c9
        return cryptpw_crypt(@_);
Packit 90a5c9
    }
Packit 90a5c9
    @_[0]; # otherwise return plaintext
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub getpass {
Packit 90a5c9
    my $prompt = shift || "Enter password:";
Packit 90a5c9
Packit 90a5c9
    unless($not_unix) {
Packit 90a5c9
        open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
Packit 90a5c9
        system "stty -echo;";
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    my($c,$pwd);
Packit 90a5c9
    print STDERR $prompt;
Packit 90a5c9
    while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
Packit 90a5c9
        $pwd .= $c;
Packit 90a5c9
    }
Packit 90a5c9
Packit 90a5c9
    system "stty echo" unless $not_unix;
Packit 90a5c9
    print STDERR "\n";
Packit 90a5c9
    die "Can't use empty password!\n" unless length $pwd;
Packit 90a5c9
    return $pwd;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::update {
Packit 90a5c9
    die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
Packit 90a5c9
    $crypted_pwd = (split /:/, $DB{$key}, 3)[0] if $crypted_pwd eq '.';
Packit 90a5c9
    $groups = (split /:/, $DB{$key}, 3)[1] if !$groups || $groups eq '.';
Packit 90a5c9
    $comment = (split /:/, $DB{$key}, 3)[2] if !$comment || $comment eq '.';
Packit 90a5c9
    if (!$crypted_pwd || $crypted_pwd eq '-') {
Packit 90a5c9
        dbmc->adduser;
Packit 90a5c9
    }
Packit 90a5c9
    else {
Packit 90a5c9
        dbmc->add;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::add {
Packit 90a5c9
    die "Can't use empty password!\n" unless $crypted_pwd;
Packit 90a5c9
    unless($is_update) {
Packit 90a5c9
        die "Sorry, user `$key' already exists!\n" if $DB{$key};
Packit 90a5c9
    }
Packit 90a5c9
    $groups = '' if $groups eq '-';
Packit 90a5c9
    $comment = '' if $comment eq '-';
Packit 90a5c9
    $groups .= ":" . $comment if $comment;
Packit 90a5c9
    $crypted_pwd .= ":" . $groups if $groups;
Packit 90a5c9
    $DB{$key} = $crypted_pwd;
Packit 90a5c9
    my $action = $is_update ? "updated" : "added";
Packit 90a5c9
    print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::adduser {
Packit 90a5c9
    my $value = getpass "New password:";
Packit 90a5c9
    die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
Packit 90a5c9
    $crypted_pwd = cryptpw $value;
Packit 90a5c9
    dbmc->add;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::delete {
Packit 90a5c9
    die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
Packit 90a5c9
    delete $DB{$key}, print "`$key' deleted\n";
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::view {
Packit 90a5c9
    print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::check {
Packit 90a5c9
    die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
Packit 90a5c9
    my $chkpass = (split /:/, $DB{$key}, 3)[0];
Packit 90a5c9
    my $testpass = getpass();
Packit 90a5c9
    if (substr($chkpass, 0, 6) eq '$apr1$') {
Packit 90a5c9
        need_md5_crypt;
Packit 90a5c9
        $crypt_method = "md5";
Packit 90a5c9
    } elsif (substr($chkpass, 0, 5) eq '{SHA}') {
Packit 90a5c9
        need_sha1_crypt;
Packit 90a5c9
        $crypt_method = "sha1";
Packit 90a5c9
    } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
Packit 90a5c9
        $crypt_method = "crypt";
Packit 90a5c9
    } else {
Packit 90a5c9
        $crypt_method = "plain";
Packit 90a5c9
    }
Packit 90a5c9
    print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass
Packit 90a5c9
                           ? " password ok\n" : " password mismatch\n");
Packit 90a5c9
}
Packit 90a5c9
Packit 90a5c9
sub dbmc::import {
Packit 90a5c9
    while(defined($_ = <STDIN>) and chomp) {
Packit 90a5c9
        ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;
Packit 90a5c9
        dbmc->add;
Packit 90a5c9
    }
Packit 90a5c9
}
Packit 90a5c9