0

I have read the answer to this question:

https://stackoverflow.com/questions/4102763/apache-basic-authentication-except-for-those-allowed

It helped me understand how to not authenticate some users (according to the IP):

<Directory /var/www/files/>
    Order deny,allow
    Deny from all
    Allow from 192.168.1.2
    Satisfy Any
    AuthUserFile /etc/apache2/basic.pwd 
    AuthName "Please enter username and password" 
    AuthType Basic 
    Require valid-user 
</Directory>

Imagine I have this DB (Different from the DB used for authentication):

User        IP 
Mark        192.168.1.2
Mike        192.168.1.3
Karl        192.168.1.4

1- can I allow all the IP addresses stored in the DB using a configuration in Apache?

2- another problem is the authorization of the allowed IP is lost, can Apache use this DB for authorization, if the user is allowed to get the pages without authentication?

Update:

to be clear:

1- I don't want a static solution, I want Apache to Allow all IPs from the mentioned table in the DB ( the DB is changing dynamically).

2- We know when Apache authenticate users, it knows the user name from authentication credentials, but with the Allowing, the user name will be lost, I want Apache to extract the user name of the IP its allowing from the same table it extract the IP address?

8
  • I don't follow "the user name will be lost". Can you explain? Also, a common practice is to generate these config files dynamically in a case like this. Can you not create this directive whenever the DB is updated? Jul 22, 2014 at 14:30
  • "the user name will be lost" means that Apache can follow the users as it does when it use Authentication info, "Can you not create this directive whenever the DB is updated?" this will be very bad to performance (restarting httpd after every change in DB). plus my DB is changing a lot@CharlieS
    – Nidal
    Jul 22, 2014 at 14:33
  • Don't restart it, just reload the conf. Jul 22, 2014 at 14:38
  • ever reloading is not good enough, this is not a good solution
    – Nidal
    Jul 22, 2014 at 14:39
  • The answer at stackoverflow.com/a/4677061/630614 may solve part of this. But if you want to use Auth it may require other binding or a conf reload (which is not harmful to running user sessions). Jul 22, 2014 at 14:41

2 Answers 2

1

You can try the answer suggested here of using mod_rewrite to blacklist from a file:

## WHITELIST IPS ##
RewriteMap ipslist txt:/path/to/whitelist.txt
RewriteCond %{REMOTE_ADDR} ^(.*)$
RewriteCond ${ipslist:%1|black} ^black$ [NC]
RewriteRule (.*) - [F]

You can try and use something dynamic like mod_authn_dbd to select the username and password from a database:

# mod_dbd configuration
DBDriver pgsql
DBDParams "dbname=apacheauth user=apache password=xxxxxx"

DBDMin  4
DBDKeep 8
DBDMax  20
DBDExptime 300

<Directory /usr/www/myhost/private>
  # core authentication and mod_auth_basic configuration
  # for mod_authn_dbd
  AuthType Basic
  AuthName "My Server"
  AuthBasicProvider dbd

  # core authorization configuration
  Require valid-user

  # mod_authn_dbd SQL query to authenticate a user
  AuthDBDUserPWQuery \
    "SELECT password FROM authn WHERE user = %s"
</Directory>
4
  • +1, because it makes sense, but it doesn't solve my second problem
    – Nidal
    Jul 22, 2014 at 14:53
  • Sorry but I keep re-reading the second problem and I don't understand what you're asking. What do you mean by "the user is lost", "authenticate but does not authorize"? Jul 22, 2014 at 14:54
  • authentication: who you are , authorization: what you can Do? so I want the users who didn't authenticate to be authorized according to there IP or the DB
    – Nidal
    Jul 22, 2014 at 14:57
  • 1
    I personally would implement that at the application level, as it would become more easily portable and testable. Jul 22, 2014 at 14:59
1

In answer to #1 you can have as many IP's allowed as you want... examples:

Allow from 192.168.1.2
Allow from 192.168.1.3
Allow from 192.168.1.4

or even

Allow from 192.168.1.2 192.168.1.3 192.168.1.4

and many others including ranges.

for #2, sorry but I'm not understanding what you're asking, perhaps an example?

3
  • I don't want a static solution, I want Apache to Allow all IPs from the mentioned table in the DB, see the Updates
    – Nidal
    Jul 20, 2014 at 17:39
  • 1
    @Networker I held off another reply til I had a chance to surf the apache docs. I'm not sure what good supplying a 'user' is for access granted by IP. Short of that name being used for lookup in the .htpasswd file I don't see a way to grab the value that was used from another application. Httpclient authentication is also not very good security, passwords are sent to the server in clear text. If you want user based logins for an application use a more sophisticated system within that app. What httpclient Auth IS good for is keeping search bots, nosy users, etc out of a dev or test site.
    – Tyson
    Jul 23, 2014 at 19:05
  • @Networker That said... You could have a cron job that queried the database and re-generated an .htaccess file every X minutes, to handle the need for updating authorized IP's.
    – Tyson
    Jul 23, 2014 at 19:07

Not the answer you're looking for? Browse other questions tagged .