Posted in Apache Howto Linux Security

Apache : conditional http authentication

January 27, 2007 - 6 comments

This is what I needed to do :
I have a virtual host (say sub.domain.be) running under Apache web server at work that should be accessible for everybody on the local network but as well for a bunch of people outside of it. The main concern is *security*, we would consider the local network as safe while anything else is not.

Thus, the condition is this :
- local network : unrestricted access
- foreign network (in other words “web users”) : http authentication

Here’s the configuration I used on that virtual host :

AuthType        basic
AuthName        "Sub Domain authentication"
AuthUserFile    /var/www/virtual/.htmaster/.sub.domain.be
Require         valid-user

Satisfy         any
Order           deny,allow
Deny            from all
Allow from      192.168.100.0/24

What is interesting is the “Satisfy any” line.

http://httpd.apache.org/docs/2.0/mod/core.html#satisfy says this about it :
Access policy if both Allow and Require used. The parameter can be either All or Any. This directive is only useful if access to a particular area is being restricted by both username/password and client host address. In this case the default behavior (All) is to require that the client passes the address access restriction and enters a valid username and password. With the Any option the client will be granted access if they either pass the host restriction or enter a valid username and password. This can be used to password restrict an area, but to let clients from particular addresses in without prompting for a password.

So either the user is in the 192.168.100.0/24 range and gets unrestricted access to the virtual host, or he isn’t and is asked for a username and password.

Keep in mind http authentication credentials are sent in the clear ! Force SSL encryption if you want the credentials to be encrypted.

Fore more info about “order directive”, check this link

Comments

The Adminblogger

January 27, 2007 - 12:18

Hi Sebastien,

you’re right, satisfy any is a nice feature in apache.

you could still save one line if using “order” the other way around:

order allow,deny
allow from 192.168.100.0/24

then there is no “deny from all” needed, since deny is the default policy (because it’s at the end in the “order” line) which will hit all not covered by an “allow”.

Wish you a nice weekend,
Marcel.

Sébastien Wains

January 27, 2007 - 13:20

Thanks again Marcel for your always useful comments !

marco

November 20, 2008 - 16:29

Sebastien,

maybe a stupid question. I have a linux box running apache2 behind a router. There I have installed some web applications (sql-ledger) that I would like to access from a computer not in the LAN. For this I need to open a port on the router. At this point everybody could connect.
Could be possible to have an ACL like in SSH so that only certain ip addresses can access the server?
If possible, is this a secure option?
Thanks!

Cheers

Marco

Sébastien Wains

November 23, 2008 - 0:25

Just set up authentication as explained in the article..

For more info see http://www.yolinux.com/TUTORIALS/LinuxTutorialApacheAddingLoginSiteProtection.html

Cheers

Ramen

January 14, 2010 - 1:07

Hi Sebastien

Im a developer, but am feeling my way around more sysadmin type stuff. I use my home server for managing projects, etc that I have started to make selectively available to clients. I force access to the projects, repositories, source, etc. through ssl, authenticating against an ldap server. This was proving painful for me when working on my private network and your post has saved me a lot of time.

Thanks

Sébastien Wains

January 14, 2010 - 18:00

You’re welcome :)

Leave Comment

Please consider visiting the partners below if you enjoyed this article :

If this post saved you time and money, please consider checking my Amazon wishlist.

Before submitting, some rules :
- Is your comment related to the article ?
- You're having a problem ? Have you checked Google, other howtos, docs, manpages ?
- You're still having the problem ? Have you raised log verbosity, checked traces, ran tcpdump ?
- Have you checked your configuratoin for typo ?
Unless your comment is providing additional info or respect the rules above, DON'T comment.
If you don't understand what you are doing, I urge you to read the documentation, I'm not your free Level 1 helpdesk guy.