dbmmanage.in revision e569ae6ccf9b3a7424969289971fa4481ce6a014
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#!@perlbin@
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# Licensed to the Apache Software Foundation (ASF) under one or more
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# contributor license agreements. See the NOTICE file distributed with
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# this work for additional information regarding copyright ownership.
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# The ASF licenses this file to You under the Apache License, Version 2.0
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# (the "License"); you may not use this file except in compliance with
db479b48bd4d75423ed4a45e15b75089d1a8ad72fielding# the License. You may obtain a copy of the License at
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# Unless required by applicable law or agreed to in writing, software
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# distributed under the License is distributed on an "AS IS" BASIS,
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# See the License for the specific language governing permissions and
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# limitations under the License.
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#for more functionality see the HTTPD::UserAdmin module:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# usage: dbmmanage <DBMfile> <command> <user> <password> <groups> <comment>
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# -ldb -lndbm -lgdbm -lsdbm
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarBEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File SDBM_File) }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarUsage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar where enc is -d for crypt encryption (default except on Win32, Netware)
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar -m for MD5 encryption (default on Win32, Netware)
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar -s for SHA1 encryption
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar -p for plaintext
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar command is one of: $cmds
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar pw of . for update command retains the old password
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar pw of - (or blank) for update command prompts for the password
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar groups or comment of . (or blank) for update command retains old values
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar groups or comment of - for update command clears the existing value
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar groups or comment of - for add and adduser commands is the empty value
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoardbmmanage SHA1 passwords require the interface or the module Digest::SHA1
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaravailable from CPAN:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarPlease install Digest::SHA1 and try again, or use a different crypt option:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoardbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar http://www.cpan.org/modules/by-module/Crypt/Crypt-PasswdMD5-1.1.tar.gz
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarPlease install Crypt::PasswdMD5 and try again, or use a different crypt option:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# four bytes of iteration count and four bytes of salt). Otherwise, just use
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# the traditional two-byte salt.
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# see the man page on your system to decide if you have a newer crypt() lib.
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# The new style crypt() allows up to 20 characters of the password to be
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# significant rather than only 8.
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $newstyle_salt_platforms = join '|', qw{bsdos}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# Some platforms just can't crypt() for Apache
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# Some platforms won't jump through our favorite hoops
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar "Warning: Apache/$^O does not support crypt()ed passwords!\n\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar "Warning: Apache/$^O does not support plaintext passwords!\n\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarusage() unless $file and $command and defined &{$dbmc::{$command}};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# remove extension if any
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoartie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypted_pwd = (split /:/, $DB{$key}, 3)[0] if $crypted_pwd eq '.';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $groups = (split /:/, $DB{$key}, 3)[1] if !$groups || $groups eq '.';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $comment = (split /:/, $DB{$key}, 3)[2] if !$comment || $comment eq '.';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if (!$crypted_pwd || $crypted_pwd eq '-') {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar dbmc->adduser;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar dbmc->add;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::add {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar unless($is_update) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg die "Sorry, user `$key' already exists!\n" if $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $groups = '' if $groups eq '-';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $comment = '' if $comment eq '-';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::adduser {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypted_pwd = cryptpw $value;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar dbmc->add;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::delete {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::view {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::check {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;