Skip to content

Commit ec83616

Browse files
committed
Extend and fix BIND script
1 parent aac4475 commit ec83616

File tree

1 file changed

+122
-3
lines changed

1 file changed

+122
-3
lines changed

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

Lines changed: 122 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,79 @@ fi
3030

3131
# Generate named.conf from template
3232
echo "Generating $NAMED_CONF from $NAMED_CONF_TEMPLATE"
33+
34+
# Check if 127.0.0.1 is available and decide on listen address
35+
echo "Debug: Testing network connectivity for BIND address selection..."
36+
37+
IPV4_OK=false
38+
IPV6_OK=false
39+
40+
# Test IPv4 connectivity
41+
if ping -c 1 127.0.0.1 >/dev/null 2>&1; then
42+
IPV4_OK=true
43+
echo "Debug: IPv4 (127.0.0.1) is reachable"
44+
else
45+
echo "Debug: IPv4 (127.0.0.1) is NOT reachable"
46+
fi
47+
48+
# Test IPv6 connectivity
49+
if command -v ping6 >/dev/null 2>&1; then
50+
if ping6 -c 1 ::1 >/dev/null 2>&1; then
51+
IPV6_OK=true
52+
echo "Debug: IPv6 (::1) is reachable"
53+
fi
54+
else
55+
if ping -6 -c 1 ::1 >/dev/null 2>&1; then
56+
IPV6_OK=true
57+
echo "Debug: IPv6 (::1) is reachable via ping -6"
58+
fi
59+
fi
60+
61+
if ! $IPV6_OK; then
62+
echo "Debug: IPv6 (::1) is NOT reachable"
63+
fi
64+
65+
# Choose the listen address
66+
if $IPV4_OK; then
67+
LISTEN_ADDRESS="127.0.0.1"
68+
echo "Debug: Using IPv4 (127.0.0.1) for BIND"
69+
elif $IPV6_OK; then
70+
LISTEN_ADDRESS="::1"
71+
echo "Debug: Using IPv6 (::1) for BIND"
72+
else
73+
echo "Debug: Neither 127.0.0.1 nor ::1 is available!"
74+
echo "Debug: Falling back to 127.0.0.1 anyway"
75+
LISTEN_ADDRESS="127.0.0.1"
76+
fi
77+
3378
sed -e "s|@ZONES_DIR@|$ZONES_DIR|g" \
3479
-e "s|@PID_FILE@|$PID_FILE|g" \
3580
-e "s|@SCRIPT_DIR@|$SCRIPT_DIR|g" \
81+
-e "s|@LISTEN_ADDRESS@|$LISTEN_ADDRESS|g" \
3682
"$NAMED_CONF_TEMPLATE" > "$NAMED_CONF"
3783

84+
# Determine the best user to run BIND as (do this early)
85+
echo "Debug: Determining user for BIND..."
86+
87+
# Get the owner of the script directory
88+
SCRIPT_OWNER=$(stat -c '%U' "$SCRIPT_DIR")
89+
SCRIPT_GROUP=$(stat -c '%G' "$SCRIPT_DIR")
90+
91+
echo "Debug: Script directory owned by: $SCRIPT_OWNER:$SCRIPT_GROUP"
92+
echo "Debug: Current user: $(whoami)"
93+
94+
# Use the script owner if it's not root, otherwise use current user
95+
if [[ "$SCRIPT_OWNER" != "root" ]] && id "$SCRIPT_OWNER" >/dev/null 2>&1; then
96+
BIND_USER="$SCRIPT_OWNER"
97+
echo "Debug: Will run BIND as script owner: $BIND_USER"
98+
else
99+
BIND_USER="$(whoami)"
100+
echo "Debug: Will run BIND as current user: $BIND_USER"
101+
fi
102+
103+
echo "Debug: Generated named.conf contents:"
104+
cat "$NAMED_CONF"
105+
38106
# Clean up any leftover journal or PID files
39107
rm -f "$ZONES_DIR"/*.jnl "$PID_FILE"
40108

@@ -43,10 +111,59 @@ echo "Starting BIND from $SCRIPT_DIR"
43111

44112
if $FOREGROUND; then
45113
echo "(running in foreground)"
46-
exec named -c "$NAMED_CONF" -p 53 -u "$(whoami)" -g -d 1
114+
echo "Debug: About to exec: named -c $NAMED_CONF -p 53 -u $BIND_USER -g -d 1"
115+
exec named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" -g -d 1
47116
else
48117
echo "(running in background)"
49-
named -c "$NAMED_CONF" -p 53 -u "$(whoami)"
118+
echo "Debug: About to run: named -c $NAMED_CONF -p 53 -u $BIND_USER"
119+
120+
# Test configuration first
121+
echo "Debug: Testing BIND configuration..."
122+
if named-checkconf "$NAMED_CONF"; then
123+
echo "Debug: Configuration check passed"
124+
else
125+
echo "Debug: Configuration check failed"
126+
exit 1
127+
fi
128+
129+
# Check if zone files exist
130+
echo "Debug: Checking zone files..."
131+
if [[ -f "$ZONES_DIR/basic.dnstest.php.net.zone" ]]; then
132+
echo "Debug: Zone file exists"
133+
echo "Debug: Zone file contents:"
134+
cat "$ZONES_DIR/basic.dnstest.php.net.zone"
135+
else
136+
echo "Debug: Zone file missing: $ZONES_DIR/basic.dnstest.php.net.zone"
137+
ls -la "$ZONES_DIR/"
138+
exit 1
139+
fi
140+
141+
# Set up permissions for the chosen user
142+
echo "Debug: Setting up permissions for user: $BIND_USER..."
143+
144+
# Ensure files are readable by the chosen user
145+
if [[ "$BIND_USER" != "$(whoami)" ]]; then
146+
# If we're running as a different user, ensure group/other permissions
147+
chmod 644 "$NAMED_CONF" "$ZONES_DIR"/*.zone
148+
chmod 755 "$SCRIPT_DIR" "$ZONES_DIR"
149+
fi
150+
151+
# Run named and capture both stdout and stderr separately
152+
echo "Debug: Starting named as user: $BIND_USER..."
153+
if named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" > "$LOG_FILE" 2>&1; then
154+
echo "Debug: named command succeeded"
155+
else
156+
NAMED_EXIT_CODE=$?
157+
echo "Debug: named command failed with exit code: $NAMED_EXIT_CODE"
158+
echo "Debug: Log file contents:"
159+
cat "$LOG_FILE" 2>/dev/null || echo "No log file found"
160+
161+
# Try to run named with more verbose output
162+
echo "Debug: Trying to run named in foreground for better error output:"
163+
timeout 5 named -c "$NAMED_CONF" -p 53 -u "$BIND_USER" -g -d 1 || echo "Foreground attempt timed out or failed"
164+
165+
exit $NAMED_EXIT_CODE
166+
fi
50167

51168
# Wait for BIND to start with periodic checks
52169
MAX_WAIT=20 # Maximum wait time in attempts (20 * 0.5s = 10s)
@@ -76,7 +193,9 @@ else
76193
if [[ -f "$LOG_FILE" ]]; then
77194
echo "Last few lines from log:"
78195
tail -5 "$LOG_FILE"
196+
else
197+
echo "No log file found at $LOG_FILE"
79198
fi
80199

81200
exit 1
82-
fi
201+
fi

0 commit comments

Comments
 (0)