home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Online / Apache / bin / dbmmanage < prev    next >
Text File  |  1999-11-11  |  6KB  |  190 lines

  1. #!/bin/perl
  2.  
  3. # ====================================================================
  4. # Copyright (c) 1995-1999 The Apache Group.  All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. #    notice, this list of conditions and the following disclaimer. 
  12. #
  13. # 2. Redistributions in binary form must reproduce the above copyright
  14. #    notice, this list of conditions and the following disclaimer in
  15. #    the documentation and/or other materials provided with the
  16. #    distribution.
  17. #
  18. # 3. All advertising materials mentioning features or use of this
  19. #    software must display the following acknowledgment:
  20. #    "This product includes software developed by the Apache Group
  21. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  22. #
  23. # 4. The names "Apache Server" and "Apache Group" must not be used to
  24. #    endorse or promote products derived from this software without
  25. #    prior written permission. For written permission, please contact
  26. #    apache@apache.org.
  27. #
  28. # 5. Products derived from this software may not be called "Apache"
  29. #    nor may "Apache" appear in their names without prior written
  30. #    permission of the Apache Group.
  31. #
  32. # 6. Redistributions of any form whatsoever must retain the following
  33. #    acknowledgment:
  34. #    "This product includes software developed by the Apache Group
  35. #    for use in the Apache HTTP server project (http://www.apache.org/)."
  36. #
  37. # THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  38. # EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  39. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  40. # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE APACHE GROUP OR
  41. # ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  48. # OF THE POSSIBILITY OF SUCH DAMAGE.
  49. # ====================================================================
  50. #
  51. # This software consists of voluntary contributions made by many
  52. # individuals on behalf of the Apache Group and was originally based
  53. # on public domain software written at the National Center for
  54. # Supercomputing Applications, University of Illinois, Urbana-Champaign.
  55. # For more information on the Apache Group and the Apache HTTP server
  56. # project, please see <http://www.apache.org/>.
  57.  
  58. #for more functionality see the HTTPD::UserAdmin module:
  59. # http://www.perl.com/CPAN/modules/by-module/HTTPD/HTTPD-Tools-x.xx.tar.gz
  60. #
  61. # usage: dbmmanage <DBMfile> <command> <key> <value>
  62.  
  63. package dbmmanage;
  64. #                               -ldb    -lndbm    -lgdbm
  65. BEGIN { @AnyDBM_File::ISA = qw(DB_File NDBM_File GDBM_File) }
  66. use strict;
  67. use Fcntl;
  68. use AnyDBM_File ();
  69.  
  70. my($file,$command,$key,$crypted_pwd) = @ARGV;
  71.  
  72. usage() unless $file and $command and defined &{$dbmc::{$command}};
  73.  
  74. # if your osname is in $newstyle_salt, then use new style salt (starts with '_' and contains
  75. # four bytes of iteration count and four bytes of salt).  Otherwise, just use
  76. # the traditional two-byte salt.
  77. # see the man page on your system to decide if you have a newer crypt() lib.
  78. # I believe that 4.4BSD derived systems do (at least BSD/OS 2.0 does).
  79. # The new style crypt() allows up to 20 characters of the password to be
  80. # significant rather than only 8.
  81. my $newstyle_salt = join '|', qw{bsdos}; #others?
  82.  
  83. # remove extension if any
  84. my $chop = join '|', qw{db.? pag dir};
  85. $file =~ s/\.($chop)$//;
  86.  
  87. my $is_update = $command eq "update";
  88. my $Is_Win32  = $^O eq "MSWin32"; 
  89. my %DB = ();
  90. my @range = ();
  91. my($mode, $flags) = $command =~ 
  92.     /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
  93.  
  94. tie %DB, "AnyDBM_File", $file, $flags, $mode || die "Can't tie $file: $!";
  95. dbmc->$command();
  96. untie %DB;
  97.  
  98. sub usage {
  99.     my $cmds = join "|", sort keys %dbmc::;
  100.     die "usage: $0 filename [$cmds] [username]\n";
  101. }
  102.  
  103. my $x;
  104. sub genseed {
  105.     my $psf;
  106.     for (qw(-xlwwa -le)) { 
  107.     `ps $_ 2>/dev/null`;
  108.     $psf = $_, last unless $?;
  109.     }
  110.     srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
  111.     @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
  112.     $x = int scalar @range;
  113. }
  114.  
  115. sub randchar { 
  116.     join '', map $range[rand $x], 1..shift||1;
  117. }
  118.  
  119. sub salt {
  120.     my $newstyle = $^O =~ /(?:$newstyle_salt)/;
  121.     genseed() unless @range; 
  122.     return $newstyle ? 
  123.     join '', "_", randchar, "a..", randchar(4) :
  124.         randchar(2);
  125. }
  126.  
  127. sub getpass {
  128.     my $prompt = shift || "Enter password:";
  129.  
  130.     unless($Is_Win32) { 
  131.     open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
  132.     system "stty -echo;";
  133.     }
  134.  
  135.     my($c,$pwd);
  136.     print STDERR $prompt;
  137.     while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
  138.     $pwd .= $c;
  139.     }
  140.  
  141.     system "stty echo" unless $Is_Win32;
  142.     print STDERR "\n";
  143.     die "Can't use empty password!\n" unless length $pwd;
  144.     return $pwd;
  145. }
  146.  
  147. sub dbmc::update {
  148.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  149.     dbmc->adduser;
  150. }
  151.  
  152. sub dbmc::add {
  153.     die "Can't use empty password!\n" unless $crypted_pwd;
  154.     unless($is_update) {
  155.     die "Sorry, user `$key' already exists!\n" if $DB{$key};
  156.     }
  157.     $DB{$key} = $crypted_pwd;
  158.     my $action = $is_update ? "updated" : "added";
  159.     print "User $key $action with password encrypted to $DB{$key}\n";
  160. }
  161.  
  162. sub dbmc::adduser {
  163.     my $value = getpass "New password:";
  164.     die "They don't match, sorry.\n" unless getpass("Re-type new password:") eq $value;
  165.     $crypted_pwd = crypt $value, caller->salt;
  166.     dbmc->add;
  167. }
  168.  
  169. sub dbmc::delete {
  170.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  171.     delete $DB{$key}, print "`$key' deleted\n";
  172. }
  173.  
  174. sub dbmc::view {
  175.     print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
  176. }
  177.  
  178. sub dbmc::check {
  179.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  180.     print crypt(getpass(), $DB{$key}) eq $DB{$key} ? "password ok\n" : "password mismatch\n";
  181. }
  182.  
  183. sub dbmc::import {
  184.     while(defined($_ = <STDIN>) and chomp) {
  185.     ($key,$crypted_pwd) = split /:/, $_, 2;
  186.     dbmc->add;
  187.     }
  188. }
  189.  
  190.