dbmmanage.in revision e569ae6ccf9b3a7424969289971fa4481ce6a014
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#!@perlbin@
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
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
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
ce9621257ef9e54c1bbe5ad8a5f445a1f211c2dcnd# http://www.apache.org/licenses/LICENSE-2.0
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
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
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#
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# usage: dbmmanage <DBMfile> <command> <user> <password> <groups> <comment>
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarpackage dbmmanage;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# -ldb -lndbm -lgdbm -lsdbm
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarBEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File SDBM_File) }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaruse strict;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaruse Fcntl;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaruse AnyDBM_File ();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub usage {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $cmds = join "|", sort keys %dbmc::;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die <<SYNTAX;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarUsage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
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
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar command is one of: $cmds
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar pw of . for update command retains the old password
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar pw of - (or blank) for update command prompts for the password
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
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
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarSYNTAX
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub need_sha1_crypt {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if (!eval ('require "Digest/SHA1.pm";')) {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print STDERR <<SHAERR;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoardbmmanage SHA1 passwords require the interface or the module Digest::SHA1
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaravailable from CPAN:
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarPlease install Digest::SHA1 and try again, or use a different crypt option:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarSHAERR
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar usage();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub need_md5_crypt {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if (!eval ('require "Crypt/PasswdMD5.pm";')) {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print STDERR <<MD5ERR;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoardbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar http://www.cpan.org/modules/by-module/Crypt/Crypt-PasswdMD5-1.1.tar.gz
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarPlease install Crypt::PasswdMD5 and try again, or use a different crypt option:
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarMD5ERR
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar usage();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
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.
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $newstyle_salt_platforms = join '|', qw{bsdos}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# Some platforms just can't crypt() for Apache
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $crypt_method = "crypt";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif ($crypt_not_supported) {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "md5";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# Some platforms won't jump through our favorite hoops
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar#
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $not_unix = $^O =~ /(?:$not_unix_platforms)/;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif ($crypt_not_supported) {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "md5";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif (@ARGV[0] eq "-d") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar shift @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if ($crypt_not_supported) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg print STDERR
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar "Warning: Apache/$^O does not support crypt()ed passwords!\n\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "crypt";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif (@ARGV[0] eq "-m") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar shift @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "md5";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif (@ARGV[0] eq "-p") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar shift @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if (!$crypt_not_supported) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg print STDERR
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar "Warning: Apache/$^O does not support plaintext passwords!\n\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "plain";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif (@ARGV[0] eq "-s") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar shift @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar need_sha1_crypt();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "sha1";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarif ($crypt_method eq "md5") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar need_md5_crypt();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarusage() unless $file and $command and defined &{$dbmc::{$command}};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar# remove extension if any
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $chop = join '|', qw{db.? pag dir};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar$file =~ s/\.($chop)$//;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $is_update = $command eq "update";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy %DB = ();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy @range = ();
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankgmy($mode, $flags) = $command =~
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoartie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoardbmc->$command();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoaruntie %DB;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarmy $x;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub genseed {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $psf;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if ($not_unix) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg srand (time ^ $$ or time ^ ($$ + ($$ << 15)));
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar else {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg for (qw(-xlwwa -le)) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg `ps $_ 2>/dev/null`;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $psf = $_, last unless $?;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $x = int scalar @range;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankgsub randchar {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar join '', map $range[rand $x], 1..shift||1;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub saltpw_crypt {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg genseed() unless @range;
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg return $newstyle_salt ?
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg join '', "_", randchar, "a..", randchar(4) :
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar randchar(2);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub cryptpw_crypt {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my ($pw, $salt) = @_;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $salt = saltpw_crypt unless $salt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar crypt $pw, $salt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub saltpw_md5 {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg genseed() unless @range;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar randchar(8);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub cryptpw_md5 {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my($pw, $salt) = @_;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $salt = saltpw_md5 unless $salt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar Crypt::PasswdMD5::apache_md5_crypt($pw, $salt);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub cryptpw_sha1 {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my($pw, $salt) = @_;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar '{SHA}' . Digest::SHA1::sha1_base64($pw) . "=";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub cryptpw {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if ($crypt_method eq "md5") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar return cryptpw_md5(@_);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } elsif ($crypt_method eq "sha1") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar return cryptpw_sha1(@_);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } elsif ($crypt_method eq "crypt") {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar return cryptpw_crypt(@_);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar @_[0]; # otherwise return plaintext
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub getpass {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $prompt = shift || "Enter password:";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg unless($not_unix) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg system "stty -echo;";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my($c,$pwd);
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print STDERR $prompt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg $pwd .= $c;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar system "stty echo" unless $not_unix;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print STDERR "\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Can't use empty password!\n" unless length $pwd;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar return $pwd;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::update {
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 }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar else {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar dbmc->add;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::add {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Can't use empty password!\n" unless $crypted_pwd;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar unless($is_update) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg die "Sorry, user `$key' already exists!\n" if $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $groups = '' if $groups eq '-';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $comment = '' if $comment eq '-';
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $groups .= ":" . $comment if $comment;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypted_pwd .= ":" . $groups if $groups;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $DB{$key} = $crypted_pwd;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $action = $is_update ? "updated" : "added";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::adduser {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $value = getpass "New password:";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypted_pwd = cryptpw $value;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar dbmc->add;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::delete {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar delete $DB{$key}, print "`$key' deleted\n";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::view {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::check {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $chkpass = (split /:/, $DB{$key}, 3)[0];
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar my $testpass = getpass();
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar if (substr($chkpass, 0, 6) eq '$apr1$') {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar need_md5_crypt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "md5";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } elsif (substr($chkpass, 0, 5) eq '{SHA}') {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar need_sha1_crypt;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "sha1";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "crypt";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar } else {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar $crypt_method = "plain";
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar ? " password ok\n" : " password mismatch\n");
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoarsub dbmc::import {
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar while(defined($_ = <STDIN>) and chomp) {
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;
e569ae6ccf9b3a7424969289971fa4481ce6a014fuankg dbmc->add;
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar }
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar}
650b8deadd86f9326c0d4a1bcbbb61cb907722aacoar