Advertisement
Guest User

carandraug

a guest
Sep 7th, 2011
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.11 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Temp qw(tempfile);
  5. use CGI qw/:standard :cgi/;
  6.  
  7. $CGI::POST_MAX        = 1024 * 100; # max 100K posts
  8. $CGI::DISABLE_UPLOADS = 1;          # no uploads
  9.  
  10. ## This script gets data from a html form and uses it to create a password file
  11. ## that can be used by Apache to autheticate users. It creates a new file for
  12. ## each new user and mails the system administrator. It is his job to use that
  13. ## file (created with File::Temp) to add to the list of authorized users.
  14. ##
  15. ## Notes: htpasswd is run in batch mode
  16.  
  17. ################################################################################
  18. ## Options
  19. ################################################################################
  20.  
  21. my $pass_min    = 4;                    # Minimum password length
  22. my $user_min    = 4;                    # Minimum username length
  23.  
  24. my $htpasswd    = "/usr/bin/htpasswd";  # Absolute path fpr htpasswd
  25. my $encryption  = "-m";                 # See man htpasswd(1)
  26.  
  27. my $tmp_dir     = "/var/www/svn/temp/"; # Directory to store the temporary files
  28.  
  29. ################################################################################
  30. ## Get values from form (field name is argument for param)
  31. ################################################################################
  32.  
  33. my $repo      = param("repository");
  34. my $username  = param("username");
  35. my $pass_1    = param("password1");
  36. my $pass_2    = param("password2");
  37. my $name      = param("real_name");
  38.  
  39. ################################################################################
  40. ## No user configuration beyond this point
  41. ################################################################################
  42.  
  43. ## Create temporary file to store user information
  44. my (undef, $passwd_file) = tempfile(DIR => $tmp_dir);
  45.  
  46. ## Server side to check some conditions
  47. if ($pass_1 ne $pass_2) {
  48.   reply_error("The passwords were different.");
  49. } elsif (length($pass_1) < $pass_min) {
  50.   reply_error("The passwords were smaller than 4 characters.");
  51. } elsif (length($username) < $user_min) {
  52.   reply_error("The username had less than 4 characters.");
  53. }
  54.  
  55. ## Create command and run htpasswd
  56. my @htpasswd_command = (
  57.                      $htpasswd,
  58.                      "-b",
  59.                      "-c",
  60.                      $encryption,
  61.                      $passwd_file,
  62.                      $username,
  63.                      $pass_1,
  64.                      );
  65.  
  66. system(@htpasswd_command);
  67. if ($? != 0) {
  68.   reply_error(sprintf "Error when registering user. Error value is %d\n", $? >> 8);
  69. } else {
  70.   reply_success();
  71. }
  72.  
  73. ################################################################################
  74. ## Subroutines
  75. ################################################################################
  76.  
  77. sub reply_error {
  78.   my $msg = $_[0];
  79.   print
  80.     header,
  81.     h4("$msg"),
  82.     "You should mention this to the system administrator",
  83.     end_html;
  84.   exit;
  85. }
  86.  
  87. sub reply_success {
  88.   print
  89.     header,
  90.     h4("Contact the system administrator to activate your account."),
  91.     "User $username was successfully registered.",
  92.     end_html;
  93.   exit;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement