Score:1

htaccess - Generate ALLOW rule dynamically from external file

ru flag

Can Htaccess set variables by external files as values?

I would like to ALLOW all IPs that are listed in a txt file located on the server.

This txt file is auto generated via script by our systems that resolve DYNDNS adresses to IPs.

I can format this external file in any way But I would need HTACCESS to INCLUDE it

So that the htaccess fill the allow section dynamically:

# allow IP range by CIDR number
order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
allow from yyy.yyy.yyy.yyy
allow from zzz.zzz.zzz.zzz

Any tip for that? Thank you

kz flag
What version of Apache are you using? How many IPs are you expecting this file to contain?
MastaP avatar
ru flag
Server version: Apache/2.4.57 (cPanel). There will be about 10 IPs. But they change often (our team working from home). All have a dyndns.org hostname assigned. I would have preferred to have htaccess to whitelist hostnames, but it does not do the resolver work it seems.
kz flag
"I would have preferred to have htaccess to whitelist hostnames, but it does not do the resolver work it seems." - It can, but it depends on the server config (and is not particularly efficient). Do you have access to the server config?
MastaP avatar
ru flag
Yes, I have root access. But like you say for efficiency as it only changes every 2-3 days, I would rather cache that as static values.
Score:2
kz flag

Since you have access to the server config then you can create a RewriteMap (part of mod_rewrite) that references your text file of IP addresses. You can then lookup IP addresses in this rewrite map using mod_rewrite. The RewriteMap itself needs to be defined in the server config, but it can be called from anywhere (eg. .htaccess).

For example:

Your text file of IP addresses... key/value pairs separated by a space. You don't need to include the actual team members name (it's not used in the lookup), but it might be useful. Just some text value that is not "DENY" (since this is used later as the default value in the lookup). All values could be the same if you want.

# /path/to/file/allowedips.txt

# IP addresses to allow access
1.1.1.1 Bob
2.2.2.2 Alice
3.3.3.3 Frank
4.4.4.4 Joe

In the server config you define the RewriteMap:

RewriteMap allowedips "txt:/path/to/file/allowedips.txt"

Call the rewrite map in .htaccess (or server config) to block access:

RewriteEngine On
RewriteCond ${allowedips:%{REMOTE_ADDR}|DENY} =DENY
RewriteRule ^ - [F]

DENY is simply the default value returned when the IP address is not found in the rewrite map.

The F flag triggers a 403 Forbidden response.

You need to restart Apache after making changes to the server config (ie. when you define the RewriteMap), but you do not need to restart the server when you simply update the text file containing the list of IP addresses.


order deny,allow
deny from all
allow from xxx.xxx.xxx.xxx
allow from yyy.yyy.yyy.yyy
allow from zzz.zzz.zzz.zzz

Order, Deny and Allow directives are formerly deprecated on Apache 2.4. The Apache 2.4 equivalent is Require. However, this does not have the ability to read IP addresses from a file. Although you can read the entire contents of a file using an Apache expression and parse this against a regex, but a RewriteMap (as mentioned above) would be more efficient. Although this would still be a solution if you didn't have access to the server config.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.