| 1 | #!/usr/bin/perl | 
|---|
| 2 | use strict; | 
|---|
| 3 |  | 
|---|
| 4 | my ($op, $username) = @ARGV; | 
|---|
| 5 |  | 
|---|
| 6 | if(defined $op and $op eq "reset") { | 
|---|
| 7 |         system("rm -f .htaccess .htpasswd"); | 
|---|
| 8 |     print "\nDone.  All access restrictions removed.\n\n"; | 
|---|
| 9 |         exit(0); | 
|---|
| 10 | } | 
|---|
| 11 |  | 
|---|
| 12 | if(!defined $op or !defined $username or | 
|---|
| 13 |         ($op ne "allow" and $op ne "remove")) { | 
|---|
| 14 |         print "Usage: webaccess [allow username] [remove username] [reset]\n"; | 
|---|
| 15 |         exit(0); | 
|---|
| 16 | } | 
|---|
| 17 |  | 
|---|
| 18 | if($op eq "allow" or $op eq "remove") { | 
|---|
| 19 |         open(HTPASSWD, ".htpasswd"); | 
|---|
| 20 |         open(TMP, ">.htpasswd_tmp"); | 
|---|
| 21 |         while(my $line = <HTPASSWD>) { | 
|---|
| 22 |                 print TMP "$line" unless($line =~ /$username\:/); | 
|---|
| 23 |         } | 
|---|
| 24 |         close(TMP); | 
|---|
| 25 |         close(HTPASSWD); | 
|---|
| 26 |         system("mv .htpasswd_tmp .htpasswd"); | 
|---|
| 27 | } | 
|---|
| 28 |  | 
|---|
| 29 | if($op eq "allow") { | 
|---|
| 30 |         my $password; | 
|---|
| 31 |         print "Enter new password for $username: "; | 
|---|
| 32 |         system("stty -echo"); | 
|---|
| 33 |         chop($password = <STDIN>); | 
|---|
| 34 |         system("stty echo"); | 
|---|
| 35 |         print "\n"; | 
|---|
| 36 |  | 
|---|
| 37 |         open(HTACCESS, ">.htaccess"); | 
|---|
| 38 |         print HTACCESS <<ENDFILE; | 
|---|
| 39 | AuthUserFile $ENV{PWD}/.htpasswd | 
|---|
| 40 | AuthName Private | 
|---|
| 41 | AuthType Basic | 
|---|
| 42 | <Limit GET> | 
|---|
| 43 | require valid-user | 
|---|
| 44 | </Limit> | 
|---|
| 45 | ENDFILE | 
|---|
| 46 |         close(HTACCESS); | 
|---|
| 47 |         chmod(0777, ".htaccess"); | 
|---|
| 48 |  | 
|---|
| 49 |         my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z') [rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64]; | 
|---|
| 50 |         $password = crypt($password, '$1$'.$salt); | 
|---|
| 51 |  | 
|---|
| 52 |         open(HTPASSWD, ">>.htpasswd"); | 
|---|
| 53 |         print HTPASSWD "$username\:$password\n"; | 
|---|
| 54 |         close(HTPASSWD); | 
|---|
| 55 |         chmod(0777, ".htpasswd"); | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | print "\nDone.  New list of valid usernames:\n"; | 
|---|
| 59 | open(HTPASSWD, ".htpasswd"); | 
|---|
| 60 | while(my $line = <HTPASSWD>) { | 
|---|
| 61 |         $line =~ /(.*):/; | 
|---|
| 62 |         print "$1\n"; | 
|---|
| 63 | } | 
|---|
| 64 | close(HTPASSWD); | 
|---|
| 65 | print "\n"; | 
|---|