logo
Published on Fedora Tunisia (https://fedora-tn.org)

Protecting Apache directories

By Swobodin
Created 2006-04-18 15:32
Protecting an Apache directory with a login / password is very common and very simple. Your host may provide this feature, Webmin too. As many friends usually ask me about it, I think I should write a quick tutorial to redirect people who are asking about.
First, allow htaccess directive in the directory you want to set rules, this may be done from your httpd.conf (/etc/httpd/conf/httpd.conf) setting the AllowOverride flag to on
For example

Allow Override On

Go to the directory, and create a .htaccess file, in which set these values
AuthUserFile /var/www/html/Lab/_htpasswd # htpasswd file, in which you store logins and passwords
AuthType Basic # The only type I know
AuthName "Restricted Area. Please login to proceed" # Title of the popup
Require valid-user # "valid-user" is in lowercase

Protect your _htpasswd

Deny From All

To add a user who is allowed to access, use htpasswd command. The -c option will create the file, or erase it if exists; don't use it when appending.
htpasswd -c _htpasswd Swobodin
New password:
Re-type new password:
Adding password for user Swobodin

The passwords are encrypted in this case according to the system's crypt(), thus you may make passwords with other programming language.

PHP

$fp = fopen ("_htpasswd","a');
fputs($fp, "Swobodin:".crypt("mypass"));
fclose($fp);
?>

Perl

#!/usr/bin/perl -w
use strict;
use warnings;
use POSIX;
my $salt="Yh";
open FP,">>_htpasswd";
my $encrypted=crypt("mypass", $salt);
print FP ("Swobodin:", crypt("mypass", $salt));

C

#include
#define _XOPEN_SOURCE
#include
int
main (int argc, char *argv[])
{
  FILE *FP;
  if (argc != 4)
    {
      printf ("usage: %s File Login Password\n", argv[0]);
      return (-1);

    }
  FP = fopen (argv[1], "a+");
  char *encrypted = (char *) malloc (10);
  fprintf (FP, "%s: %s\n", argv[2], crypt (argv[3], "zz"));
  fclose (FP);
  return 0;
}
Compile it
gcc -o myhtpasswd myhpasswd.c -lcrypt
And run it
./myhtpaswd _htpasswd Swobodin mypass

Source URL:
https://fedora-tn.org/?q=node/92