@@ -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
376441copy_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