Skip to content

Commit 7b35d37

Browse files
committed
Use dir user for bind
1 parent 89e548c commit 7b35d37

File tree

1 file changed

+58
-52
lines changed

1 file changed

+58
-52
lines changed

ext/standard/tests/dns/bind-start.sh

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,25 @@ else
100100
exit 1
101101
fi
102102

103+
# Determine the best user to run BIND as (do this early)
104+
echo "Debug: Determining user for BIND..."
105+
106+
# Get the owner of the script directory
107+
SCRIPT_OWNER=$(stat -c '%U' "$SCRIPT_DIR")
108+
SCRIPT_GROUP=$(stat -c '%G' "$SCRIPT_DIR")
109+
110+
echo "Debug: Script directory owned by: $SCRIPT_OWNER:$SCRIPT_GROUP"
111+
echo "Debug: Current user: $(whoami)"
112+
113+
# Use the script owner if it's not root, otherwise use current user
114+
if [[ "$SCRIPT_OWNER" != "root" ]] && id "$SCRIPT_OWNER" >/dev/null 2>&1; then
115+
BIND_USER="$SCRIPT_OWNER"
116+
echo "Debug: Will run BIND as script owner: $BIND_USER"
117+
else
118+
BIND_USER="$(whoami)"
119+
echo "Debug: Will run BIND as current user: $BIND_USER"
120+
fi
121+
103122
# Enhanced AppArmor handling
104123
if [[ -f /etc/apparmor.d/usr.sbin.named ]]; then
105124
echo "Debug: AppArmor profile detected, attempting comprehensive bypass..."
@@ -159,11 +178,11 @@ echo "Starting BIND from $SCRIPT_DIR"
159178

160179
if $FOREGROUND; then
161180
echo "(running in foreground)"
162-
echo "Debug: About to exec: named -c $NAMED_CONF -p 53 -u $(whoami) -g -d 1"
163-
exec named -c "$NAMED_CONF" -p 53 -u "$(whoami)" -g -d 1
181+
echo "Debug: About to exec: named -c $NAMED_CONF -p 53 -u $BIND_USER -g -d 1"
182+
exec named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" -g -d 1
164183
else
165184
echo "(running in background)"
166-
echo "Debug: About to run: named -c $NAMED_CONF -p 53 -u $(whoami)"
185+
echo "Debug: About to run: named -c $NAMED_CONF -p 53 -u $BIND_USER"
167186

168187
# Test configuration first
169188
echo "Debug: Testing BIND configuration..."
@@ -186,49 +205,40 @@ else
186205
exit 1
187206
fi
188207

189-
# Set up permissions for bind user
190-
echo "Debug: Setting up permissions for bind user..."
191-
if id bind >/dev/null 2>&1; then
192-
# The bind user needs execute permissions on all parent directories
193-
echo "Debug: Setting directory permissions for bind user access..."
194-
195-
# Make sure bind can traverse the entire path
196-
ls -la /
197-
ls -la /home
198-
ls -la /home/runner
199-
ls -la /home/runner/work
200-
ls -la /home/runner/work/php-src
201-
ls -la /home/runner/work/php-src/php-src
202-
203-
chmod o+x /home 2>/dev/null || echo "Failed to chmod /home"
204-
chmod o+x /home/runner 2>/dev/null || echo "Failed to chmod /home/runner"
205-
chmod o+x /home/runner/work 2>/dev/null || echo "Failed to chmod /home/runner/work"
206-
chmod o+x /home/runner/work/php-src 2>/dev/null || echo "Failed to chmod /home/runner/work/php-src"
207-
chmod o+x /home/runner/work/php-src/php-src 2>/dev/null || echo "Failed to chmod /home/runner/work/php-src/php-src"
208-
chmod o+x "$SCRIPT_DIR" 2>/dev/null || echo "Failed to chmod $SCRIPT_DIR"
209-
chmod o+x "$ZONES_DIR" 2>/dev/null || echo "Failed to chmod $ZONES_DIR"
210-
211-
# Set file ownership and permissions
212-
chown bind:bind "$NAMED_CONF" "$ZONES_DIR"/*.zone 2>/dev/null || echo "Failed to chown to bind user"
208+
# Set up permissions for the chosen user
209+
echo "Debug: Setting up permissions for user: $BIND_USER..."
210+
211+
# Ensure files are readable by the chosen user
212+
if [[ "$BIND_USER" != "$(whoami)" ]]; then
213+
# If we're running as a different user, ensure group/other permissions
213214
chmod 644 "$NAMED_CONF" "$ZONES_DIR"/*.zone
214-
215-
echo "Debug: File permissions after setup:"
216-
ls -la "$NAMED_CONF" "$ZONES_DIR"/*.zone
217-
218-
echo "Debug: Directory permissions check:"
219-
ls -ld "$SCRIPT_DIR" "$ZONES_DIR"
220-
221-
# Test if bind user can actually read the config file
222-
echo "Debug: Testing bind user access to config file:"
223-
if sudo -u bind test -r "$NAMED_CONF"; then
224-
echo "Debug: bind user CAN read config file"
215+
chmod 755 "$SCRIPT_DIR" "$ZONES_DIR"
216+
fi
217+
218+
echo "Debug: File permissions after setup:"
219+
ls -la "$NAMED_CONF" "$ZONES_DIR"/*.zone
220+
221+
echo "Debug: Directory permissions:"
222+
ls -ld "$SCRIPT_DIR" "$ZONES_DIR"
223+
224+
# Test if the chosen user can actually read the config file
225+
echo "Debug: Testing $BIND_USER access to config file:"
226+
if [[ "$BIND_USER" == "$(whoami)" ]]; then
227+
# Same user, test directly
228+
if test -r "$NAMED_CONF"; then
229+
echo "Debug: $BIND_USER CAN read config file"
225230
else
226-
echo "Debug: bind user CANNOT read config file - this is the problem!"
227-
echo "Debug: Let's check what bind user sees:"
228-
sudo -u bind ls -la "$NAMED_CONF" 2>&1 || echo "bind user cannot even stat the file"
231+
echo "Debug: $BIND_USER CANNOT read config file"
229232
fi
230233
else
231-
echo "Debug: bind user does not exist, keeping current permissions"
234+
# Different user, test with sudo
235+
if sudo -u "$BIND_USER" test -r "$NAMED_CONF" 2>/dev/null; then
236+
echo "Debug: $BIND_USER CAN read config file"
237+
else
238+
echo "Debug: $BIND_USER CANNOT read config file"
239+
echo "Debug: Checking what $BIND_USER sees:"
240+
sudo -u "$BIND_USER" ls -la "$NAMED_CONF" 2>&1 || echo "$BIND_USER cannot stat the file"
241+
fi
232242
fi
233243

234244
# Check IPv4/IPv6 configuration with fallbacks
@@ -271,16 +281,12 @@ else
271281
echo "Debug: Starting AppArmor denial monitoring..."
272282
(timeout 15 tail -f /var/log/syslog 2>/dev/null | grep "apparmor.*DENIED" | head -10 &) || echo "Could not start syslog monitoring"
273283

274-
# Try different user approaches
275-
NAMED_USER="$(whoami)"
276-
if id bind >/dev/null 2>&1; then
277-
echo "Debug: Trying with bind user instead of root..."
278-
NAMED_USER="bind"
279-
fi
284+
# Use the determined user
285+
echo "Debug: Using determined user: $BIND_USER"
280286

281287
# Run named and capture both stdout and stderr separately
282-
echo "Debug: Starting named as user: $NAMED_USER..."
283-
if named -c "$NAMED_CONF" -p 53 -u "$NAMED_USER" > "$LOG_FILE" 2>&1; then
288+
echo "Debug: Starting named as user: $BIND_USER..."
289+
if named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" > "$LOG_FILE" 2>&1; then
284290
echo "Debug: named command succeeded"
285291
else
286292
NAMED_EXIT_CODE=$?
@@ -298,7 +304,7 @@ else
298304

299305
# Try to run named with more verbose output
300306
echo "Debug: Trying to run named in foreground for better error output:"
301-
timeout 5 named -c "$NAMED_CONF" -p 53 -u "$NAMED_USER" -g -d 1 || echo "Foreground attempt timed out or failed"
307+
timeout 5 named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" -g -d 1 || echo "Foreground attempt timed out or failed"
302308

303309
exit $NAMED_EXIT_CODE
304310
fi
@@ -340,4 +346,4 @@ else
340346
grep "apparmor.*DENIED.*named" /var/log/syslog 2>/dev/null | tail -5 || echo "No final AppArmor denials found"
341347

342348
exit 1
343-
fi
349+
fi

0 commit comments

Comments
 (0)