LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 05-03-2009, 08:06 PM   #1
noir911
Member
 
Registered: Apr 2004
Posts: 682

Rep: Reputation: Disabled
apache2 and .htaccess


Is there any way to tell Apache2 to prompt for username and password
when it sees a .htaccess file in a directory?

At the moment, I need to define the directory in the following
fashion in Apache2 conf file but it becomes a problem as I have thousands of directories that needs .htaccess files.

<Directory "/srv/www/htdocs/dir">
AllowOverride All
Options Indexes
Order allow,deny
Allow from all
AuthUserFile /srv/www/htdocs/dir/.htaccess
</Directory>

Thanks.
 
Old 05-03-2009, 08:33 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
I'm a little confused about the way you're going about things here. Is it that you want to protect a whole directory structure with a username and password, or do you want to protect each of thousands of directories with different usernames and passwords? Protecting one directory would protect all directories below it (e.g. protecting private/ will also protect private/quotes/ and private/orders/).
Code:
<Directory "/srv/www/htdocs/dir">
AllowOverride All
Options Indexes
Order allow,deny
Allow from all
AuthUserFile /srv/www/htdocs/dir/.htaccess
</Directory>
A few issues here:
  • It's missing an AuthType [Basic|Digest] directive
  • It's missing an AuthName directive
  • It's missing a Require directive
  • Using .htaccess files (which is enabled with AllowOverride All) for most directories is only a good idea if you do not have access to the main config files (it can sap performance of the server).
  • .htaccess files are not normally the AuthUserFile - and the AuthUserFile should not be in a publically accessible directory. The file created using htpasswd - should be stored above the document root.
The section should read something like:
Code:
<Directory "/srv/www/htdocs/dir">
AllowOverride None
AuthType Basic
AuthName "My Private Directory"
Options Indexes
Order allow,deny
Allow from all
AuthUserFile /srv/www/.htpasswd
Require valid-user
</Directory>
Using AuthType Digest provides further security, by hashing the username and password before transmission (though a few more changes would be needed as well). The Require statement can instead list a number of users from the AuthUserFile, rather than valid-user (= all users). See here for more.

If you do decide you want to go with .htaccess files, you would need to include the essential directives shown above (AuthType, AuthName, AuthUserFile and Require) within <Limit... > ... </Limit> tags, within that file (at the root of each branch you want to protect).

Hope this helps,
Rob

Last edited by Robhogg; 05-03-2009 at 08:54 PM.
 
Old 05-03-2009, 08:52 PM   #3
noir911
Member
 
Registered: Apr 2004
Posts: 682

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Robhogg View Post
I'm a little confused about the way you're going about things here. Is it that you want to protect a whole directory structure with a username and password, or do you want to protect each of thousands of directories with a different usernames and passwords? Protecting one directory would protect all directories below it (e.g. protecting private/ will also protect private/quotes/ and private/orders/).
Code:
<Directory "/srv/www/htdocs/dir">
AllowOverride All
Options Indexes
Order allow,deny
Allow from all
AuthUserFile /srv/www/htdocs/dir/.htaccess
</Directory>
A few issues here:
  • It's missing an AuthType [Basic|Digest] directive
  • It's missing an AuthName directive
  • It's missing Require directives
  • Using .htaccess files (which is enabled with AllowOverride All) for most directories is only a good idea if you do not have access to the main config files (it can sap performance of the server).
  • .htaccess files are not normally the AuthUserFile - and the AuthUserFile should not be in a publically accessible directory. The file created using htpasswd - should be stored above the document root.
The section should read something like:
Code:
<Directory "/srv/www/htdocs/dir">
AllowOverride None
AuthType Basic
AuthName "My Private Directory"
Options Indexes
Order allow,deny
Allow from all
AuthUserFile /srv/www/.htpasswd
Require valid-user
</Directory>
Using AuthType Digest provides further security, by hashing the username and password before transmission (though a few more changes would be needed as well). The Require statement can instead list a number of users from the AuthUserFile, rather than valid-user (= all users). See here for more.

Hope this helps,
Rob
I want to protect each of thousands of directories with a different usernames and passwords. Assuming /srv/www is DirectoryRoot then protect /srv/www/a1, /srv/www/z1 etc.

I am aware of the fact that protecting one directory would protect all directories below it.

The issues that you have raised about a few directives missing are all described in the .htaccess file (and it works fine). Following is an example. .htaccess is not an issue; it works fine.

Code:
AuthName "Restricted"
AuthType Basic
AuthLDAPURL ldap://ad.company.com:389/ou=marketing,dc=company,dc=com?sAMAccountName?sub
AuthBasicProvider ldap
AuthzLDAPAuthoritative off
require valid-user
 
Old 05-03-2009, 09:06 PM   #4
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Wouldn't you then just need to modify the AllowOverride directive for your document root, to something like:

Code:
<Directory "/srv/www/htdocs">
AllowOverride AuthConfig #or All?
...

Last edited by Robhogg; 05-03-2009 at 09:09 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Broken .htaccess on Apache2.2 OMT Linux - Software 2 04-14-2009 02:38 AM
Apache2 .htaccess redirect problem raven Linux - Networking 0 08-06-2007 02:35 AM
Apache2 htaccess problem ghostz00 Linux - Server 3 11-26-2006 11:18 PM
Apache2 .htaccess problem suse 10.0 PB0711 Linux - Networking 2 07-12-2006 11:23 AM
apache2 + htaccess + ssl rubella Linux - Security 1 02-07-2006 05:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration