#!/usr/bin/perl
use strict;
use warnings;
use File::Temp qw(tempfile);
use CGI qw/:standard :cgi/;
$CGI::POST_MAX = 1024 * 100; # max 100K posts
$CGI::DISABLE_UPLOADS = 1; # no uploads
## This script gets data from a html form and uses it to create a password file
## that can be used by Apache to autheticate users. It creates a new file for
## each new user and mails the system administrator. It is his job to use that
## file (created with File::Temp) to add to the list of authorized users.
##
## Notes: htpasswd is run in batch mode
################################################################################
## Options
################################################################################
my $pass_min = 4; # Minimum password length
my $user_min = 4; # Minimum username length
my $htpasswd = "/usr/bin/htpasswd"; # Absolute path fpr htpasswd
my $encryption = "-m"; # See man htpasswd(1)
my $tmp_dir = "/var/www/svn/temp/"; # Directory to store the temporary files
################################################################################
## Get values from form (field name is argument for param)
################################################################################
my $repo = param("repository");
my $username = param("username");
my $pass_1 = param("password1");
my $pass_2 = param("password2");
my $name = param("real_name");
################################################################################
## No user configuration beyond this point
################################################################################
## Create temporary file to store user information
my (undef, $passwd_file) = tempfile(DIR => $tmp_dir);
## Server side to check some conditions
if ($pass_1 ne $pass_2) {
reply_error("The passwords were different.");
} elsif (length($pass_1) < $pass_min) {
reply_error("The passwords were smaller than 4 characters.");
} elsif (length($username) < $user_min) {
reply_error("The username had less than 4 characters.");
}
## Create command and run htpasswd
my @htpasswd_command = (
$htpasswd,
"-b",
"-c",
$encryption,
$passwd_file,
$username,
$pass_1,
);
system(@htpasswd_command);
if ($? != 0) {
reply_error(sprintf "Error when registering user. Error value is %d\n", $? >> 8);
} else {
reply_success();
}
################################################################################
## Subroutines
################################################################################
sub reply_error {
my $msg = $_[0];
print
header,
h4("$msg"),
"You should mention this to the system administrator",
end_html;
exit;
}
sub reply_success {
print
header,
h4("Contact the system administrator to activate your account."),
"User $username was successfully registered.",
end_html;
exit;
}