Skip to content

Commit 6e303bd

Browse files
committed
Use dir user for bind
1 parent 89e548c commit 6e303bd

File tree

1 file changed

+56
-50
lines changed

1 file changed

+56
-50
lines changed

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

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ echo "Starting BIND from $SCRIPT_DIR"
159159

160160
if $FOREGROUND; then
161161
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
162+
echo "Debug: About to exec: named -c $NAMED_CONF -p 53 -u $BIND_USER -g -d 1"
163+
exec named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" -g -d 1
164164
else
165165
echo "(running in background)"
166-
echo "Debug: About to run: named -c $NAMED_CONF -p 53 -u $(whoami)"
166+
echo "Debug: About to run: named -c $NAMED_CONF -p 53 -u $BIND_USER"
167167

168168
# Test configuration first
169169
echo "Debug: Testing BIND configuration..."
@@ -186,49 +186,59 @@ else
186186
exit 1
187187
fi
188188

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"
189+
# Determine the best user to run BIND as
190+
echo "Debug: Determining user for BIND..."
191+
192+
# Get the owner of the script directory
193+
SCRIPT_OWNER=$(stat -c '%U' "$SCRIPT_DIR")
194+
SCRIPT_GROUP=$(stat -c '%G' "$SCRIPT_DIR")
195+
196+
echo "Debug: Script directory owned by: $SCRIPT_OWNER:$SCRIPT_GROUP"
197+
echo "Debug: Current user: $(whoami)"
198+
199+
# Use the script owner if it's not root, otherwise use current user
200+
if [[ "$SCRIPT_OWNER" != "root" ]] && id "$SCRIPT_OWNER" >/dev/null 2>&1; then
201+
BIND_USER="$SCRIPT_OWNER"
202+
echo "Debug: Will run BIND as script owner: $BIND_USER"
203+
else
204+
BIND_USER="$(whoami)"
205+
echo "Debug: Will run BIND as current user: $BIND_USER"
206+
fi
207+
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
@@ -272,15 +282,11 @@ else
272282
(timeout 15 tail -f /var/log/syslog 2>/dev/null | grep "apparmor.*DENIED" | head -10 &) || echo "Could not start syslog monitoring"
273283

274284
# 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
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

0 commit comments

Comments
 (0)