Skip to content

Commit 43d1d58

Browse files
committed
Enhance add_http_auth.sh script to support IP-based access control and Apache version compatibility. Update README files to reflect new features and usage instructions.
1 parent 30d8701 commit 43d1d58

File tree

3 files changed

+147
-5
lines changed

3 files changed

+147
-5
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This repository contains a collection of bash scripts organized by category:
1010

1111
Scripts for managing HTTP authentication in web servers.
1212

13-
- [add_http_auth.sh](http-auth/add_http_auth.sh) - Add HTTP Basic authentication to an .htaccess file
13+
- [add_http_auth.sh](http-auth/add_http_auth.sh) - Add HTTP Basic authentication to an .htaccess file with support for IP-based access control and compatibility with both Apache 2.2 and 2.4
1414

1515
## Installation
1616

http-auth/README.md

+64-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ A bash script to easily add HTTP Basic authentication to an .htaccess file.
1616
- Allows customization of username
1717
- Provides interactive or command-line password entry
1818
- Supports multiple encryption methods (bcrypt, md5, sha1)
19+
- Allows specific IP addresses to bypass authentication
20+
- Compatible with both Apache 2.2 and 2.4 syntax
1921

2022
## Requirements
2123

@@ -61,6 +63,7 @@ This will:
6163
- Set up authentication with default values (username: admin)
6264
- Prompt you to enter a password
6365
- Use bcrypt encryption (most secure)
66+
- Use Apache 2.4 syntax (default)
6467

6568
### Command-line Options
6669

@@ -72,6 +75,8 @@ Options:
7275
-u, --user Username (default: 'admin')
7376
-s, --password Password (if not specified, it will be asked interactively)
7477
-e, --encrypt Encryption method: md5, bcrypt, sha1 (default: 'bcrypt')
78+
-i, --ip Comma-separated list of IP addresses allowed without authentication
79+
-a, --apache Apache version: 2.2 or 2.4 (default: '2.4')
7580
-h, --help Display this help
7681
```
7782

@@ -101,10 +106,22 @@ Options:
101106
./add_http_auth.sh -e md5
102107
```
103108

109+
#### Allow specific IP addresses to bypass authentication:
110+
111+
```bash
112+
./add_http_auth.sh -i "192.168.1.100,10.0.0.5"
113+
```
114+
115+
#### Specify Apache version (for older servers):
116+
117+
```bash
118+
./add_http_auth.sh -a 2.2
119+
```
120+
104121
#### Full example with all options:
105122

106123
```bash
107-
./add_http_auth.sh -f /var/www/html/.htaccess -p /etc/apache2/.htpasswd -u webmaster -s "my_secure_password" -e bcrypt
124+
./add_http_auth.sh -f /var/www/html/.htaccess -p /etc/apache2/.htpasswd -u webmaster -s "my_secure_password" -e bcrypt -i "192.168.1.100,10.0.0.5" -a 2.4
108125
```
109126

110127
## How It Works
@@ -113,7 +130,8 @@ Options:
113130
2. It then checks if the .htaccess file already exists and if authentication is already configured
114131
3. If a password is not provided as an argument, it prompts the user to enter one
115132
4. It creates or updates the .htpasswd file with the username and hashed password
116-
5. Finally, it adds the necessary authentication directives to the .htaccess file
133+
5. If IP addresses are specified, it adds rules to allow those IPs to bypass authentication
134+
6. Finally, it adds the necessary authentication directives to the .htaccess file
117135

118136
## Encryption Methods
119137

@@ -123,13 +141,49 @@ The script supports three encryption methods:
123141
2. **md5**: Compatible with most servers, requires the `openssl` command
124142
3. **sha1**: Stronger than md5 but less secure than bcrypt, requires the `openssl` command
125143

144+
## IP-Based Access
145+
146+
When you specify IP addresses with the `-i` option, the script adds rules to the .htaccess file that allow those IPs to access the protected content without authentication. This is useful for:
147+
148+
- Office networks where you want to allow access without prompting for credentials
149+
- Development or staging environments where you want to restrict access but allow certain IPs
150+
- Monitoring services that need to access the site without authentication
151+
152+
The IP addresses should be provided as a comma-separated list without spaces, for example: `192.168.1.100,10.0.0.5`
153+
154+
## Apache Version Compatibility
155+
156+
The script supports both Apache 2.2 and Apache 2.4 syntax for access control:
157+
158+
### Apache 2.2 Syntax
159+
160+
```apache
161+
SetEnvIf Remote_Addr "^(192\.168\.1\.100|10\.0\.0\.5)$" ALLOW_ACCESS
162+
Order deny,allow
163+
Deny from all
164+
Allow from env=ALLOW_ACCESS
165+
Satisfy any
166+
```
167+
168+
### Apache 2.4 Syntax
169+
170+
```apache
171+
<RequireAny>
172+
Require ip 192.168.1.100 10.0.0.5
173+
Require valid-user
174+
</RequireAny>
175+
```
176+
177+
By default, the script uses Apache 2.4 syntax. If you're using an older Apache server (version 2.2), specify `-a 2.2` when running the script.
178+
126179
## Security Considerations
127180

128181
- When using the `-s` option to specify a password on the command line, be aware that the password may be visible in the process list or command history
129182
- For production environments, it's recommended to use the interactive password prompt
130183
- Make sure the .htpasswd file is stored in a location not accessible from the web
131184
- Ensure proper file permissions are set on both .htaccess and .htpasswd files
132185
- Use bcrypt encryption when possible for better security
186+
- Be careful when allowing IP addresses to bypass authentication, as IP addresses can be spoofed
133187

134188
## Troubleshooting
135189

@@ -149,6 +203,14 @@ The script supports three encryption methods:
149203
- Ensure your web server is configured to allow .htaccess overrides
150204
- Check that the path to the .htpasswd file in the .htaccess is correct and accessible by the web server
151205

206+
5. **IP-based access not working**
207+
- Make sure your Apache server has the required modules enabled:
208+
- For Apache 2.2: `mod_setenvif`, `mod_authz_host`
209+
- For Apache 2.4: `mod_authz_core`, `mod_authz_host`
210+
- Check that you're using the correct IP address format
211+
- Verify that your server is properly detecting the client's IP address
212+
- Ensure you're using the correct Apache version syntax (`-a 2.2` or `-a 2.4`)
213+
152214
## License
153215

154216
This script is released under the MIT License. See the LICENSE file for details.

http-auth/add_http_auth.sh

+82-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ show_help() {
1212
echo " -u, --user Username (default: 'admin')"
1313
echo " -s, --password Password (if not specified, it will be asked interactively)"
1414
echo " -e, --encrypt Encryption method: md5, bcrypt, sha1 (default: 'bcrypt')"
15+
echo " -i, --ip Comma-separated list of IP addresses allowed without authentication"
16+
echo " -a, --apache Apache version: 2.2 or 2.4 (default: '2.4')"
1517
echo " -h, --help Display this help"
1618
exit 0
1719
}
@@ -74,12 +76,53 @@ generate_htpasswd_entry() {
7476
esac
7577
}
7678

79+
# Function to generate IP-based access rules
80+
generate_ip_rules() {
81+
local ip_list="$1"
82+
local apache_version="$2"
83+
local rules=""
84+
85+
# If IP list is empty, return empty rules
86+
if [ -z "$ip_list" ]; then
87+
echo ""
88+
return
89+
fi
90+
91+
# Format the IP list for use in the rules
92+
# Replace commas with spaces for Apache 2.4 Require ip directive
93+
local formatted_ip_list=${ip_list//,/ }
94+
95+
# Start the rules block
96+
rules="# Allow access from specific IP addresses without authentication\n"
97+
98+
if [ "$apache_version" = "2.2" ]; then
99+
# Apache 2.2 syntax
100+
# Replace spaces with | for regex OR
101+
local regex_ip_list=${ip_list//,/|}
102+
rules="${rules}SetEnvIf Remote_Addr \"^(${regex_ip_list})$\" ALLOW_ACCESS\n"
103+
rules="${rules}Order deny,allow\n"
104+
rules="${rules}Deny from all\n"
105+
rules="${rules}Allow from env=ALLOW_ACCESS\n"
106+
rules="${rules}Satisfy any\n\n"
107+
else
108+
# Apache 2.4 syntax using RequireAny
109+
rules="${rules}<RequireAny>\n"
110+
rules="${rules} Require ip ${formatted_ip_list}\n"
111+
rules="${rules} Require valid-user\n"
112+
rules="${rules}</RequireAny>\n\n"
113+
fi
114+
115+
echo -e "$rules"
116+
}
117+
77118
# Default values
78119
HTACCESS_FILE="./.htaccess"
79120
HTPASSWD_FILE="./.htpasswd"
80121
USERNAME="admin"
81122
PASSWORD=""
82123
ENCRYPT_METHOD="bcrypt"
124+
ALLOWED_IPS=""
125+
APACHE_VERSION="2.4"
83126

84127
# Process arguments
85128
while [[ $# -gt 0 ]]; do
@@ -104,6 +147,14 @@ while [[ $# -gt 0 ]]; do
104147
ENCRYPT_METHOD="$2"
105148
shift 2
106149
;;
150+
-i|--ip)
151+
ALLOWED_IPS="$2"
152+
shift 2
153+
;;
154+
-a|--apache)
155+
APACHE_VERSION="$2"
156+
shift 2
157+
;;
107158
-h|--help)
108159
show_help
109160
;;
@@ -120,6 +171,12 @@ if [[ ! "$ENCRYPT_METHOD" =~ ^(md5|bcrypt|sha1)$ ]]; then
120171
exit 1
121172
fi
122173

174+
# Validate Apache version
175+
if [[ ! "$APACHE_VERSION" =~ ^(2.2|2.4)$ ]]; then
176+
echo "Error: Invalid Apache version. Valid options are: 2.2, 2.4"
177+
exit 1
178+
fi
179+
123180
# Convert to absolute paths without using realpath
124181
HTACCESS_PATH=$(get_absolute_path "$HTACCESS_FILE")
125182
HTPASSWD_PATH=$(get_absolute_path "$HTPASSWD_FILE")
@@ -176,20 +233,43 @@ fi
176233
echo "Generating entry for user '$USERNAME' in the .htpasswd file using $ENCRYPT_METHOD encryption."
177234
generate_htpasswd_entry "$USERNAME" "$PASSWORD" "$ENCRYPT_METHOD" > "$HTPASSWD_PATH"
178235

236+
# Generate IP-based access rules if IPs are provided
237+
IP_RULES=""
238+
if [ -n "$ALLOWED_IPS" ]; then
239+
echo "Configuring access without authentication for IPs: $ALLOWED_IPS (Apache $APACHE_VERSION syntax)"
240+
IP_RULES=$(generate_ip_rules "$ALLOWED_IPS" "$APACHE_VERSION")
241+
fi
242+
179243
# Add authentication configuration to the .htaccess file
180244
cat << EOF >> "$HTACCESS_PATH"
181245
182246
# HTTP Basic Authentication Configuration
183247
AuthType Basic
184248
AuthName "Restricted Area"
185249
AuthUserFile "$HTPASSWD_PATH"
186-
Require valid-user
187-
188250
EOF
189251

252+
# For Apache 2.4 without IP rules or Apache 2.2, we need to add Require valid-user
253+
if [ -z "$ALLOWED_IPS" ]; then
254+
if [ "$APACHE_VERSION" = "2.4" ]; then
255+
echo "Require valid-user" >> "$HTACCESS_PATH"
256+
elif [ "$APACHE_VERSION" = "2.2" ]; then
257+
echo "Require valid-user" >> "$HTACCESS_PATH"
258+
fi
259+
fi
260+
261+
# Add IP rules if any
262+
if [ -n "$IP_RULES" ]; then
263+
echo -e "$IP_RULES" >> "$HTACCESS_PATH"
264+
fi
265+
190266
echo "HTTP Basic authentication successfully added to the .htaccess file."
191267
echo "Username: $USERNAME"
192268
echo "Encryption method: $ENCRYPT_METHOD"
193269
echo "Password file: $HTPASSWD_PATH"
270+
echo "Apache version: $APACHE_VERSION"
271+
if [ -n "$ALLOWED_IPS" ]; then
272+
echo "IPs allowed without authentication: $ALLOWED_IPS"
273+
fi
194274

195275
exit 0

0 commit comments

Comments
 (0)