Skip to content
This repository was archived by the owner on Dec 30, 2025. It is now read-only.

Commit 80e62e1

Browse files
committed
fix: add automatic permission handling to SSL script
- Add fix_letsencrypt_permissions() function to handle Docker permission issues - Automatically fix ownership after certbot Docker operations - Apply permission fixes to certificate issuance, renewal, and copying - Update documentation to reflect automatic permission handling - Resolves permission denied errors that new users would encounter This ensures new users don't need to manually fix Docker permission issues that commonly occur when certbot creates files with different ownership.
1 parent 64e2df4 commit 80e62e1

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

docs/SSL.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ The system is built with **enterprise-grade reliability**:
5656
### 🛡️ Safety Features
5757
- **Input validation**: Domain and email format checking
5858
- **File permission checks**: Ensures proper access rights
59+
- **Automatic permission fixes**: Handles Docker permission issues automatically
5960
- **Docker availability**: Validates Docker environment
6061
- **Confirmation prompts**: Prevents accidental certificate deletion
6162
- **Graceful degradation**: Continues working despite minor issues
@@ -134,14 +135,15 @@ cat cloudflare-credentials.ini
134135
# Expected: dns_cloudflare_api_token = YOUR_TOKEN_HERE
135136
```
136137

137-
#### ❌ "Certificate file is not readable"
138+
#### ❌ "Certificate file is not readable" or "Permission denied"
138139
```bash
139-
# Check file permissions
140+
# The script now automatically fixes Docker permission issues
141+
# But if you encounter problems, check file permissions:
140142
ls -la src/backend/unrealircd/conf/tls/
141143

142-
# Fix permissions if needed
143-
chmod 644 src/backend/unrealircd/conf/tls/server.cert.pem
144-
chmod 644 src/backend/unrealircd/conf/tls/server.key.pem
144+
# If needed, the script will automatically fix Let's Encrypt permissions
145+
# Manual fix (usually not needed):
146+
sudo chown -R $(id -u):$(id -g) data/letsencrypt/
145147
```
146148

147149
#### ❌ "Domain validation failed"

scripts/ssl-manager.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ issue_certificates() {
246246

247247
log_verbose "Docker is available"
248248

249+
# Fix Let's Encrypt permissions after Docker operations
250+
fix_letsencrypt_permissions
251+
249252
# Build the certbot command
250253
local certbot_cmd=(
251254
docker run --rm
@@ -272,6 +275,10 @@ issue_certificates() {
272275
if certbot_output=$("${certbot_cmd[@]}" 2>&1); then
273276
log_info "Certificates issued successfully"
274277
log_verbose "Certbot output: $certbot_output"
278+
279+
# Fix permissions after Docker operations
280+
fix_letsencrypt_permissions
281+
275282
copy_certificates
276283
return 0
277284
else
@@ -333,6 +340,9 @@ renew_certificates() {
333340
log_debug "Running renewal command:"
334341
log_debug " ${renew_cmd[*]}"
335342

343+
# Fix Let's Encrypt permissions before renewal
344+
fix_letsencrypt_permissions
345+
336346
# Run renewal with error capture
337347
local renew_output
338348
local renew_exit_code
@@ -342,6 +352,10 @@ renew_certificates() {
342352
if $VERBOSE; then
343353
log_verbose "Renewal output: $renew_output"
344354
fi
355+
356+
# Fix permissions after Docker operations
357+
fix_letsencrypt_permissions
358+
345359
copy_certificates
346360
restart_services
347361
return 0
@@ -372,12 +386,66 @@ renew_certificates() {
372386
fi
373387
}
374388

389+
# Fix Let's Encrypt directory permissions
390+
# This is needed because Docker creates files with different ownership
391+
fix_letsencrypt_permissions() {
392+
log_debug "Fixing Let's Encrypt directory permissions..."
393+
394+
# Check if Let's Encrypt directory exists
395+
if [[ ! -d $LETSENCRYPT_DIR ]]; then
396+
log_verbose "Let's Encrypt directory doesn't exist yet, skipping permission fix"
397+
return 0
398+
fi
399+
400+
# Get current user and group
401+
local current_user current_group
402+
current_user=$(id -u)
403+
current_group=$(id -g)
404+
405+
log_debug "Setting ownership to user $current_user, group $current_group"
406+
407+
# Fix ownership recursively
408+
if ! chown -R "$current_user:$current_group" "$LETSENCRYPT_DIR" 2>/dev/null; then
409+
log_verbose "Permission fix attempted (may require sudo for existing files)"
410+
411+
# Try with sudo if available
412+
if command -v sudo > /dev/null 2>&1; then
413+
log_debug "Attempting permission fix with sudo..."
414+
if sudo chown -R "$current_user:$current_group" "$LETSENCRYPT_DIR" 2>/dev/null; then
415+
log_verbose "Permission fix successful with sudo"
416+
else
417+
log_warn "Could not fix permissions with sudo - some operations may fail"
418+
log_warn "You may need to manually run: sudo chown -R \$(id -u):\$(id -g) $LETSENCRYPT_DIR"
419+
fi
420+
else
421+
log_warn "Could not fix permissions - sudo not available"
422+
log_warn "You may need to manually run: sudo chown -R \$(id -u):\$(id -g) $LETSENCRYPT_DIR"
423+
fi
424+
else
425+
log_verbose "Permission fix successful"
426+
fi
427+
428+
# Ensure proper directory permissions
429+
if [[ -d $LETSENCRYPT_DIR ]]; then
430+
chmod 755 "$LETSENCRYPT_DIR" 2>/dev/null || true
431+
if [[ -d "$LETSENCRYPT_DIR/live" ]]; then
432+
chmod 755 "$LETSENCRYPT_DIR/live" 2>/dev/null || true
433+
fi
434+
if [[ -d "$LETSENCRYPT_DIR/archive" ]]; then
435+
chmod 755 "$LETSENCRYPT_DIR/archive" 2>/dev/null || true
436+
fi
437+
fi
438+
}
439+
375440
# Copy certificates to UnrealIRCd
376441
copy_certificates() {
377442
log_info "Copying certificates to UnrealIRCd..."
378443
log_debug "Source directory: $LETSENCRYPT_DIR/live/$DOMAIN"
379444
log_debug "Target directory: $TLS_DIR"
380445

446+
# Fix permissions before attempting to copy
447+
fix_letsencrypt_permissions
448+
381449
# Check if source certificates exist
382450
local cert_source="$LETSENCRYPT_DIR/live/$DOMAIN/fullchain.pem"
383451
local key_source="$LETSENCRYPT_DIR/live/$DOMAIN/privkey.pem"

0 commit comments

Comments
 (0)