nginx-hardened.initd 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #!/sbin/openrc-run
  2. # Copyright 2017-2021 Hyperbola Project
  3. # Distributed under the terms of the GNU General Public License v2
  4. CHROOT=/srv/nginxchroot
  5. PIDFILE=/var/run/nginx.pid
  6. CONFFILE=/etc/nginx/nginx.conf
  7. SVCNAME=nginx
  8. extra_commands="checkconfig"
  9. extra_started_commands="reload"
  10. description="Robust, small and high performance http and reverse proxy server"
  11. description_checkconfig="Run nginx' internal config check."
  12. description_upgrade="Upgrade the nginx binary without losing connections."
  13. description_reload="Reload the nginx configuration without losing connections."
  14. depend() {
  15. use net dns logger netmount
  16. }
  17. checkconfig() {
  18. if [ ! -c ${CHROOT}/dev/random ] ; then
  19. mknod -m 666 ${CHROOT}/dev/null c 1 3
  20. mknod -m 644 ${CHROOT}/dev/random c 1 8
  21. mknod -m 644 ${CHROOT}/dev/urandom c 1 9
  22. mount -ro remount ${CHROOT}/dev
  23. fi
  24. checkpath --quiet --mode 755 --owner http:http --directory 'dirname ${CHROOT}${PIDFILE}'
  25. # now verify whether the configuration is valid
  26. /usr/sbin/${SVCNAME} -c ${CHROOT}${CONFFILE} -t -q
  27. if [ $? -eq 0 ] ; then
  28. einfo "Nginx configuration (${CHROOT}${CONFFILE}) is valid."
  29. return 0
  30. else
  31. eerror "Nginx configuration (${CHROOT}${CONFFILE}) not valid."
  32. /usr/sbin/${SVCNAME} -c ${CHROOT}${CONFFILE} -t
  33. return 1
  34. fi
  35. }
  36. start() {
  37. checkconfig || return 1
  38. ebegin "Starting chrooted Nginx"
  39. # Detect old version and upgrade
  40. Nginxchroothash=$(sha256sum ${CHROOT}/usr/sbin/nginx | awk '{print $1}')
  41. Nginxoutsidehash=$(sha256sum /usr/sbin/nginx | awk '{print $1}')
  42. if [ "$Nginxchroothash" != "$Nginxoutsidehash" ]
  43. then
  44. echo "New version of Tor detected! Updating chroot before running."
  45. umount ${CHROOT}/tmp
  46. umount ${CHROOT}/var/run
  47. rm -rf ${CHROOT}
  48. wait
  49. /usr/bin/sh -c "/usr/libexec/nginx-hardened-scripts/nginxchroot.sh"
  50. wait
  51. fi
  52. start-stop-daemon --start --pidfile "${CHROOT}${PIDFILE}" --quiet --exec chroot -- --userspec=http:http ${CHROOT} /usr/sbin/${SVCNAME} -g 'pid /var/run/nginx.pid; daemon on; master_process on;' > /dev/null 2>&1
  53. eend $?
  54. }
  55. stop() {
  56. ebegin "Stopping chrooted Nginx"
  57. start-stop-daemon --stop --pidfile "${CHROOT}${PIDFILE}"
  58. rm -f "${CHROOT}${PIDFILE}"
  59. eend $?
  60. }
  61. reload() {
  62. if [ ! -f ${CHROOT}${PIDFILE} ]; then
  63. eerror "${SVCNAME} isn't running"
  64. return 1
  65. fi
  66. checkconfig || return 1
  67. ebegin "Reloading chrooted Nginx configuration"
  68. start-stop-daemon --signal HUP --pidfile ${CHROOT}${PIDFILE}
  69. eend $? "Failed to reload chrooted Nginx"
  70. }