;
foreach my $line (@file_lines) {
if ($line =~ /$username/) {
$user_exists = 1;
$htpasswd{"${rep}_write"} = 1;
print p("DEBUG:: UserExists: found $username in ${rep}_write
\n") if $debug;
$htpasswd{"entry"} = "$line";
}
}
}
print p("DEBUG:: ...completed UserExists, returning to calling sub
\n") if $debug;
return($user_exists, \%htpasswd, \@repos);
}
sub CommitChanges {
# I should get username, passnew, passnew2
my $username = param("username");
my $passnew = param("passnew");
my $passnew2 = param("passnew2");
print ("\n") if $debug;
print p("DEBUG:: CommitChanges: \$username: $username\n") if $debug;
print p("DEBUG:: CommitChanges: \$passnew: $passnew\n") if $debug;
print ("
\n") if $debug;
# passed to this would be the list of repositories and values assigned...
# list fo repos...
my ($user_exists, $ref_htpasswdhash, $ref_reposarray) = &UserExists($username);
my @repos = @$ref_reposarray; #dereference
my %htpasswd = %$ref_htpasswdhash;
my $message;
# ChangeUser MAY set password, may not...
# untaint passwd passnew and username
my $set_user = &UnTaintMe("$username");
my $set_passwd = &UnTaintMe("$passnew") if $passnew;
# if no passwd to reset (in ChangeUser) then we'll be appending an existing line...
# UserExists captures $htpasswd{entry}, the line for the user from any one of the files
my ($htpassline, $set_line);
unless ($passnew) {
$htpassline = "$htpasswd{entry}";
$set_line = &UnTaintMe($htpassline);
unless ($set_line) {
&ErrorPage("ERROR:: CommitChanges: \$passnew not found AND \$set_line (line from htpasswd) not found...");
# if this isn't set we sure as hell don't want to tdelete the user from the files, which is the next step...
}
}
# set the user to zero, deleting from files...
my $at_least_one = 0; # user must remain part of at least on repository...
# check through repositories for at least one remaining...
foreach my $check (@repos) {
my $rep_access = param("${check}_access");
if ($rep_access) {
$at_least_one = 1;
}
}
unless ($at_least_one) {
&ErrorPage("ERROR:: CommitChanges: I failed to find at least one repository for this user to remain on
Please restart and remove user if this is what you intended...\n");
}
foreach my $rep_zero (@repos) {
my $set_rep_zero = &UnTaintMe("$rep_zero");
my $result = `$HTPASSWD -D $HTDIR/${set_rep_zero}_read $set_user`;
$result = `$HTPASSWD -D $HTDIR/${set_rep_zero}_write $set_user`;
}
$message = "$message
| ...removed $set_user from all repositories (zero out)
\n";
# this moves to the loop and gets issued for each repos set as "true" and r or rw
foreach my $rep (@repos) {
#get params...
my $rep_access = param("${rep}_access");
my $rep_type = param("${rep}_rw");
if ($rep_access) { # repository access checked (value set at 1)
# untaint this
my $set_rep = &UnTaintMe("$rep");
#what kind of access? "read" | "readwrite"
if ($rep_type eq "read") {
if ($passnew) {
my $res = `$HTPASSWD -bm $HTDIR/${set_rep}_read $set_user $set_passwd`; # set user and passwd in read htpasswd
$message = "$message | ...added $set_user to $HTDIR/${rep}_read
\n";
} else {
# add to the file by appending the line
`echo '${set_line}' >> $HTDIR/${set_rep}_read`;
print ("\n") if $debug;
print p("DEBUG:: CommitChanges: hit echo line to ${set_rep}_read...\n") if $debug;
print ("\n
\n") if $debug;
}
} elsif ($rep_type eq "read_write") {
if ($passnew) {
my $res = `$HTPASSWD -bm $HTDIR/${set_rep}_read $set_user $set_passwd`;
$res = `$HTPASSWD -bm $HTDIR/${set_rep}_write $set_user $set_passwd`;
$message = "$message
| ...added $set_user to $HTDIR/${rep}_read & ${rep}_write
\n";
} else {
`echo '${set_line}' >> $HTDIR/${set_rep}_read`;
`echo '${set_line}' >> $HTDIR/${set_rep}_write`;
}
} else {
&ErrorPage("ERROR:: failed to find ${rep}:: \$rep_type (kind of access): $rep_type");
}
}
}
$message = "$message
| ...completed placing changes for user $username
\n";
print p("DEBUG:: ...I am at the end of CommitChanges
\n") if $debug;
&Message("$message");
}
sub UnTaintMe {
my $tainted = shift;
print ("\n") if $debug;
print p("DEBUG:: UnTaintMe: string passed: \$tainted: $tainted\n") if $debug;
print ("
\n") if $debug;
my $untainted;
if ($tainted =~ /^([ &:#-\@\w.]+)$/) {
$untainted = "$1";
} else {
&ErrorPage("ERROR:: UnTaintMe: Tainted input: bad string: \$tainted: [$tainted]\n");
}
return $untainted;
}
sub CheckForDelete {
# I should get username
my $username = param("username");
print hidden("username"); # preserve to pass on
unless ($username) {
&ErrorPage("ERROR:: CheckForDelete: I failed to be passed a username...\n");
}
my ($user_exists, $ref_htpasswdhash, $ref_reposarray) = &UserExists($username);
unless ($user_exists) {
&ErrorPage ("ERROR:: CheckForDelete: \$username: $username | no such user found");
} else {
print ("\n") if $debug;
print p("DEBUG:: CheckForDelete: user found to exist: $username\n") if $debug;
print ("\n
\n") if $debug;
}
my $set_user = &UnTaintMe("$username"); #untaint the username here
my %htpasswd = %$ref_htpasswdhash; # get back the hash
my @repos = @$ref_reposarray; # get back the repos array
print p("I will be removing $username from the following files:\n");
print ("\n");
foreach my $rep (@repos) {
if ($htpasswd{"${rep}_read"}) {
print ("- ${rep}_read
\n");
}
if ($htpasswd{"${rep}_write"}) {
print ("- ${rep}_write
\n");
}
}
print ("
\n");
print p("If this is what you intend for this user click \"Delete This User\" below\n");
print ("\n");
print ("
\n");
}
sub RemoveUser {
my $msg;
# I should get username, passnew, passnew2 - need username...
my $username = param("username");
unless ($username) {
&ErrorPage("ERROR:: RemoveUser: I failed to be passed a username...\n");
}
# does this user exist?
my ($user_exists, $ref_htpasswdhash, $ref_reposarray) = &UserExists($username);
unless ($user_exists) {
&ErrorPage ("ERROR:: RemoveUser: $username: no such user found, exiting...");
} else {
print ("\n") if $debug;
print h2("DEBUG:: RemoveUser: user found to exist: $username\n") if $debug;
print ("\n
\n") if $debug;
}
my $set_user = &UnTaintMe("$username"); #untaint the username here
my %htpasswd = %$ref_htpasswdhash; # get back the hash
my @repos = @$ref_reposarray; # get back the repos array
foreach my $rep (@repos) {
my $set_rep = &UnTaintMe("$rep");
# if the user exists in a file delete - %htpasswd contains that data
# add to $MSG and then pass to &Message
if ($htpasswd{"${rep}_read"}) {
my $res = `$HTPASSWD -D $HTDIR/${set_rep}_read $set_user`;
$msg = "$msg | found and removed $set_user from ${set_rep}_read htpasswd file...
";
} else {
print ("\n") if $debug;
print p("DEBUG:: RemoveUser: user $set_user not found in ${set_rep}_read\n") if $debug;
print ("\n
\n") if $debug;
}
if ($htpasswd{"${set_rep}_write"}) {
my $res = `$HTPASSWD -D $HTDIR/${set_rep}_write $set_user`;
$msg = "$msg | found and removed $set_user from ${set_rep}_write htpasswd file...
";
} else {
print ("\n") if $debug;
print p("DEBUG:: RemoveUser: user $set_user not found in ${set_rep}_write\n") if $debug;
print ("\n
\n") if $debug;
}
}
&Message("$msg");
}
#messages - final page on success
sub Message {
my $msg = shift;
print ("\n");
print p(" \n");
print p("$msg\n");
print p(" \n");
print h3("Successfully completed\n");
print p(" \n");
print ("
\n");
print ("\n");
print submit(-NAME => ".State", -VALUE => "Run Script Again");
print ("
\n");
}
sub CommitNew {
# I should get username, passnew, passnew2
my $username = param("username");
my $passnew = param("passnew");
my $passnew2 = param("passnew2");
print ("\n") if $debug;
print p("DEBUG:: CommitNew: \$username: $username\n") if $debug;
print p("DEBUG:: CommitNew: \$passnew: $passnew\n") if $debug;
print ("
\n") if $debug;
# passed to this would be the list of repositories and values assigned...
# list fo repos...
my ($user_exists, $ref_htpasswdhash, $ref_reposarray) = &UserExists($username);
my @repos = @$ref_reposarray; #dereference
my %htpasswd = %$ref_htpasswdhash;
my $message;
# ChangeUser MAY set password, may not...
# untaint passwd passnew and username
my $set_user = &UnTaintMe("$username");
my $set_passwd = &UnTaintMe("$passnew");
# this moves to the loop and gets issued for each repos set as "true" and r or rw
my $at_least_one = 0; # at least one repository must be selected...
foreach my $rep (@repos) {
#get params...
my $rep_access = param("${rep}_access");
my $rep_type = param("${rep}_rw");
print ("\n") if $debug;
print p("DEBUG:: CommitNew: ${rep}:: \$rep_access: $rep_access\t\$rep_type: $rep_type\n") if $debug;
print ("
\n") if $debug;
if ($rep_access) { # repository access checked (value set at 1)
# untaint this
my $set_rep = &UnTaintMe("$rep");
#what kind of access? "read" | "readwrite"
if ($rep_type eq "read") {
my $res = `$HTPASSWD -bm $HTDIR/${set_rep}_read $set_user $set_passwd`; # set user and passwd in read htpasswd
$message = "$message | ...added $set_user to $HTDIR/${rep}_read
\n";
$at_least_one = 1;
} elsif ($rep_type eq "read_write") {
my $res = `$HTPASSWD -bm $HTDIR/${set_rep}_read $set_user $set_passwd`;
$res = `$HTPASSWD -bm $HTDIR/${set_rep}_write $set_user $set_passwd`;
$message = "$message
| ...added $set_user to $HTDIR/${rep}_read & ${rep}_write
\n";
$at_least_one = 1;
} else {
&ErrorPage("ERROR:: failed to find ${rep}:: \$rep_type (kind of access): $rep_type");
}
}
}
# check for at least one repo added for this user...
unless ($at_least_one) {
&ErrorPage("ERROR:: CommitNew: You must add $username to at least ONE repository
I find the user added to no repositories...\n");
}
$message = "$message
| ...completed add new user $username
\n";
print ("\n") if $debug;
print p("DEBUG:: I am at CommitNew\n") if $debug;
print ("
\n") if $debug;
&Message("$message");
}