#! /usr/bin/perl -w #A scipt to add a name and password to a .htpasswd file #GNU GPL copyleft Jonathan Riddell 2000,2001 #place this in it's own ScriptAlias directory with a .htaccess #file that uses the same .htpasswd file but with #require user myname #rather than letting anybody use it #if I've done this corrrectly you can get away with changing only #the three variables below use CGI qw(:standard); #the name of the .htpasswd file with full path $passwdfile = "/etc/httpd/htpasswd"; #location of this file relative to root of URL $control = "/controled/pass/control"; #what's the page called $title = "30th inverleith's member's area"; ######## print < Power user page for $title

Edit password file for $title

Does like it says. If you add a name which already exists it will be updated. Now deletes names as well. Please do not go and delete yourself GNU GPL Copyleft Jonathan Riddell 2000,2001

HEAD if (param()) { #if paramaters passed, check & enter them into .htpasswd file print "

Results:

\n"; if (param("name") && param("delete")) { print "
" . delete_name(param("name")) . "
"; } elsif (param("name") && param("pass")) { $name = param("name"); $pass = param("pass"); `htpasswd -b $passwdfile $name $pass`; print "

user $name added to password file

\n"; } elsif (!param("name")){ print "

No name supplied

\n"; } elsif (!param("pass")){ print "

No password supplied or delete box not checked

\n"; } } #print out the form $checked = 'checked="true"' if param("delete"); print <Form

Name: Check to delete:

Password:

Below is the output from your .htpasswd file.


FORMEND

    open(HTPASSWD, "$passwdfile");
    while () {
      /(.*):.*/;
       print $1 . "\n";
    }
    print "
"; print < FOOT #nuke them sub delete_name { my ($name) = @_; my $output; my $deleted = 0; open(HTPASSWD, "$passwdfile") || (return "Error: could not open password file to read"); while ($line = ) { if (($line =~ /$name:.*/)) { $deleted = 1; } else { $output .= $line; } } close(HTPASSWD); open(HTPASSWD, "> $passwdfile") || (return "Error: could not open password file to write"); print HTPASSWD $output; close (HTPASSWD) || (return "Error: could not close password file after write"); if ($deleted) { return "$name deleted"; } else { return "$name not found"; } }