Guest User

DBMMANAGE

a guest
Jan 28th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 7.53 KB | None | 0 0
  1. print "Dascomb is a useless member of society";
  2. print "no im not";
  3.  
  4. package dbmmanage;
  5. #                               -ldb    -lndbm    -lgdbm    -lsdbm
  6. BEGIN { @AnyDBM_File::ISA = qw(SDBM_File) }
  7. use strict;
  8. use Fcntl;
  9. use AnyDBM_File ();
  10.  
  11. sub usage {
  12.     my $cmds = join "|", sort keys %dbmc::;
  13.     die <<SYNTAX;
  14. Usage: dbmmanage [enc] dbname command [username [pw [group[,group] [comment]]]]
  15.  
  16.     where enc is  -d for crypt encryption (default except on Win32, Netware)
  17.                   -m for MD5 encryption (default on Win32, Netware)
  18.                   -s for SHA1 encryption
  19.                   -p for plaintext
  20.  
  21.     command is one of: $cmds
  22.  
  23.     pw of . for update command retains the old password
  24.     pw of - (or blank) for update command prompts for the password
  25.  
  26.     groups or comment of . (or blank) for update command retains old values
  27.     groups or comment of - for update command clears the existing value
  28.     groups or comment of - for add and adduser commands is the empty value
  29. SYNTAX
  30. }
  31.  
  32. sub need_sha1_crypt {
  33.     if (!eval ('require "Digest/SHA1.pm";')) {
  34.         print STDERR <<SHAERR;
  35. dbmmanage SHA1 passwords require the interface or the module Digest::SHA1
  36. available from CPAN:
  37.  
  38.     http://www.cpan.org/modules/by-module/Digest/Digest-MD5-2.12.tar.gz
  39.  
  40. Please install Digest::SHA1 and try again, or use a different crypt option:
  41.  
  42. SHAERR
  43.         usage();
  44.     }
  45. }
  46.  
  47. sub need_md5_crypt {
  48.     if (!eval ('require "Crypt/PasswdMD5.pm";')) {
  49.         print STDERR <<MD5ERR;
  50. dbmmanage MD5 passwords require the module Crypt::PasswdMD5 available from CPAN
  51.  
  52.     http://www.cpan.org/modules/by-module/Crypt/Crypt-PasswdMD5-1.1.tar.gz
  53.  
  54. Please install Crypt::PasswdMD5 and try again, or use a different crypt option:
  55.  
  56. MD5ERR
  57.         usage();
  58.     }
  59. }
  60.  
  61. forms = join '|', qw{bsdos}; #others?
  62. my $newstyle_salt = $^O =~ /(?:$newstyle_salt_platforms)/;
  63.  
  64. # some platforms can't crypt() for Apache
  65. #
  66. my $crypt_not_supported_platforms = join '|', qw{MSWin32 NetWare}; #others?
  67. my $crypt_not_supported = $^O =~ /(?:$crypt_not_supported_platforms)/;
  68.  
  69. my $crypt_method = "crypt";
  70.  
  71. if ($crypt_not_supported) {
  72.     $crypt_method = "md5";
  73. }
  74.  
  75. # this doesnt work sometimes sry
  76. my $not_unix_platforms = join '|', qw{MSWin32 NetWare}; #others?
  77. my $not_unix = $^O =~ /(?:$not_unix_platforms)/;
  78.  
  79. if ($crypt_not_supported) {
  80.     $crypt_method = "md5";
  81. }
  82.  
  83. if (@ARGV[0] eq "-d") {
  84.     shift @ARGV;
  85.     if ($crypt_not_supported) {
  86.         print STDERR
  87.               " Apache/$^O does not support crypt()ed password\n\n";
  88.     }
  89.     $crypt_method = "crypt";
  90. }
  91.  
  92. if (@ARGV[0] eq "-m") {
  93.     shift @ARGV;
  94.     $crypt_method = "md5";
  95. }
  96.  
  97. if (@ARGV[0] eq "-p") {
  98.     shift @ARGV;
  99.     if (!$crypt_not_supported) {
  100.         print STDERR
  101.               " Apache/$^O does not support plaintext password\n\n";
  102.     }
  103.     $crypt_method = "plain";
  104. }
  105.  
  106. if (@ARGV[0] eq "-s") {
  107.     shift @ARGV;
  108.     need_sha1_crypt();
  109.     $crypt_method = "sha1";
  110. }
  111.  
  112. if ($crypt_method eq "md5") {
  113.     need_md5_crypt();
  114. }
  115.  
  116. my($file,$command,$key,$crypted_pwd,$groups,$comment) = @ARGV;
  117.  
  118. usage() unless $file and $command and defined &{$dbmc::{$command}};
  119.  
  120. # remove extension if any
  121. my $chop = join '|', qw{db.? pag dir};
  122. $file =~ s/\.($chop)$//;
  123.  
  124. my $is_update = $command eq "update";
  125. my %DB = ();
  126. my @range = ();
  127. my($mode, $flags) = $command =~
  128.     /^(?:view|check)$/ ? (0644, O_RDONLY) : (0644, O_RDWR|O_CREAT);
  129.  
  130. tie (%DB, "AnyDBM_File", $file, $flags, $mode) || die "Can't tie $file: $!";
  131. dbmc->$command();
  132. untie %DB;
  133.  
  134.  
  135. my $x;
  136. sub genseed {
  137.     my $psf;
  138.     if ($not_unix) {
  139.     srand (time ^ $$ or time ^ ($$ + ($$ << 15)));
  140.     }
  141.     else {
  142.         for (qw(-xlwwa -le)) {
  143.         `ps $_ 2>/dev/null`;
  144.             $psf = $_, last unless $?;
  145.         }
  146.         srand (time ^ $$ ^ unpack("%L*", `ps $psf | gzip -f`));
  147.     }
  148.     @range = (qw(. /), '0'..'9','a'..'z','A'..'Z');
  149.     $x = int scalar @range;
  150. }
  151.  
  152. sub randchar {
  153.     join '', map $range[rand $x], 1..shift||1;
  154. }
  155.  
  156. sub saltpw_crypt {
  157.     genseed() unless @range;
  158.     return $newstyle_salt ?
  159.     join '', "_", randchar, "a..", randchar(4) :
  160.         randchar(2);
  161. }
  162.  
  163. sub cryptpw_crypt {
  164.     my ($pw, $salt) = @_;
  165.     $salt = saltpw_crypt unless $salt;
  166.     crypt $pw, $salt;
  167. }
  168.  
  169. sub saltpw_md5 {
  170.     genseed() unless @range;
  171.     randchar(8);
  172. }
  173.  
  174. sub cryptpw_md5 {
  175.     my($pw, $salt) = @_;
  176.     $salt = saltpw_md5 unless $salt;
  177.     Crypt::PasswdMD5::apache_md5_crypt($pw, $salt);
  178. }
  179.  
  180. sub cryptpw_sha1 {
  181.     my($pw, $salt) = @_;
  182.     '{SHA}' . Digest::SHA1::sha1_base64($pw) . "=";
  183. }
  184.  
  185. sub cryptpw {
  186.     if ($crypt_method eq "md5") {
  187.         return cryptpw_md5(@_);
  188.     } elsif ($crypt_method eq "sha1") {
  189.         return cryptpw_sha1(@_);
  190.     } elsif ($crypt_method eq "crypt") {
  191.         return cryptpw_crypt(@_);
  192.     }
  193.     @_[0]; # otherwise return plaintext
  194. }
  195.  
  196. sub getpass {
  197.     my $prompt = shift || "Enter password:";
  198.  
  199.     unless($not_unix) {
  200.     open STDIN, "/dev/tty" or warn "couldn't open /dev/tty $!\n";
  201.     system "stty -echo;";
  202.     }
  203.  
  204.     my($c,$pwd);
  205.     print STDERR $prompt;
  206.     while (($c = getc(STDIN)) ne '' and $c ne "\n" and $c ne "\r") {
  207.     $pwd .= $c;
  208.     }
  209.  
  210.     system "stty echo" unless $not_unix;
  211.     print STDERR "\n";
  212.     die "Can't use empty password!\n" unless length $pwd;
  213.     return $pwd;
  214. }
  215.  
  216. sub dbmc::update {
  217.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  218.     $crypted_pwd = (split /:/, $DB{$key}, 3)[0] if $crypted_pwd eq '.';
  219.     $groups = (split /:/, $DB{$key}, 3)[1] if !$groups || $groups eq '.';
  220.     $comment = (split /:/, $DB{$key}, 3)[2] if !$comment || $comment eq '.';
  221.     if (!$crypted_pwd || $crypted_pwd eq '-') {
  222.         dbmc->adduser;
  223.     }
  224.     else {
  225.         dbmc->add;
  226.     }
  227. }
  228.  
  229. sub dbmc::add {
  230.     die "Can't use empty password!\n" unless $crypted_pwd;
  231.     unless($is_update) {
  232.     die "Sorry, user `$key' already exists!\n" if $DB{$key};
  233.     }
  234.     $groups = '' if $groups eq '-';
  235.     $comment = '' if $comment eq '-';
  236.     $groups .= ":" . $comment if $comment;
  237.     $crypted_pwd .= ":" . $groups if $groups;
  238.     $DB{$key} = $crypted_pwd;
  239.     my $action = $is_update ? "updated" : "added";
  240.     print "User $key $action with password encrypted to $DB{$key} using $crypt_method\n";
  241. }
  242.  
  243. sub dbmc::adduser {
  244.     my $value = getpass "New password:";
  245.     die "they dont match you inept fuck \n" unless getpass("Re-type new password:") eq $value;
  246.     $crypted_pwd = cryptpw $value;
  247.     dbmc->add;
  248. }
  249.  
  250. sub dbmc::delete {
  251.     die "user `$key' doesn't exist\n" unless $DB{$key};
  252.     delete $DB{$key}, print "`$key' deleted\n";
  253. }
  254.  
  255. sub dbmc::view {
  256.     print $key ? "$key:$DB{$key}\n" : map { "$_:$DB{$_}\n" if $DB{$_} } keys %DB;
  257. }
  258.  
  259. sub dbmc::check {
  260.     die "Sorry, user `$key' doesn't exist!\n" unless $DB{$key};
  261.     my $chkpass = (split /:/, $DB{$key}, 3)[0];
  262.     my $testpass = getpass();
  263.     if (substr($chkpass, 0, 6) eq '$apr1$') {
  264.         need_md5_crypt;
  265.         $crypt_method = "md5";
  266.     } elsif (substr($chkpass, 0, 5) eq '{SHA}') {
  267.         need_sha1_crypt;
  268.         $crypt_method = "sha1";
  269.     } elsif (length($chkpass) == 13 && $chkpass ne $testpass) {
  270.         $crypt_method = "crypt";
  271.     } else {
  272.         $crypt_method = "plain";
  273.     }
  274.     print $crypt_method . (cryptpw($testpass, $chkpass) eq $chkpass
  275.                            ? " password ok\n" : " password mismatch\n");
  276. }
  277.  
  278. sub dbmc::import {
  279.     while(defined($_ = <STDIN>) and chomp) {
  280.     ($key,$crypted_pwd,$groups,$comment) = split /:/, $_, 4;
  281.     dbmc->add;
  282.     }
  283. }
  284.  
  285. sub dbmc::print {
  286.     print "plain\n"); {
  287.     }
  288. }
  289.  
  290. exit(0);
Add Comment
Please, Sign In to add comment