You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.3 KiB

  1. #!/bin/sh
  2. PRINT_PASSED=2
  3. NGINX_UTIL="/usr/bin/nginx-util"
  4. __esc_newlines() {
  5. echo "${1}" | sed -E 's/$/\\n/' | tr -d '\n' | sed -E 's/\\n$/\n/'
  6. }
  7. __esc_sed_rhs() {
  8. __esc_newlines "${1}" | sed -E 's/[&/\]/\\&/g'
  9. }
  10. _sed_rhs() {
  11. __esc_sed_rhs "$(echo "${1}" | sed -E "s/[$]/$(__esc_sed_rhs "${2}")/g")"
  12. }
  13. __esc_regex() {
  14. __esc_newlines "${1}" | sed -E 's/[^^_a-zA-Z0-9-]/[&]/g; s/\^/\\^/g'
  15. }
  16. _regex() {
  17. __esc_regex "${1}" | sed -E -e 's/^(\[\s])*/^\\s*/' \
  18. -e 's/(\[\s])+\[[*]]/(\\s.*)?/g' \
  19. -e 's/(\[\s])+/\\s+/g' \
  20. -e 's/(\[\s])*\[[;]]/\\s*;/g' \
  21. -e "s/\[['\"]]/['\"]?/g" \
  22. -e "s/\[[$]]/$(__esc_sed_rhs "$(__esc_regex "${2}")")/g"
  23. }
  24. _echo_sed() {
  25. echo "" | sed -E "c${1}"
  26. }
  27. setpoint_add_ssl() {
  28. local indent="\n$1"
  29. local name="$2"
  30. local default=""
  31. [ "${name}" = "${LAN_NAME}" ] && default=".default"
  32. local prefix="${CONF_DIR}${name}"
  33. local CONF="$(grep -vE "$(_regex "${NGX_INCLUDE}" \
  34. "${LAN_LISTEN}${default}")" "${prefix}.sans" 2>/dev/null)"
  35. local ADDS=""
  36. echo "${CONF}" \
  37. | grep -qE "$(_regex "${NGX_INCLUDE}" "${LAN_SSL_LISTEN}${default}")" \
  38. || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_INCLUDE}" \
  39. "${LAN_SSL_LISTEN}${default}")"
  40. echo "${CONF}" | grep -qE "$(_regex "${NGX_SSL_CRT}" "${prefix}")" \
  41. || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_CRT}" "${prefix}")"
  42. echo "${CONF}" | grep -qE "$(_regex "${NGX_SSL_KEY}" "${prefix}")" \
  43. || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_KEY}" "${prefix}")"
  44. echo "${CONF}" | grep -qE "^\s*ssl_session_cache\s" \
  45. || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_SESSION_CACHE}" "${name}")"
  46. echo "${CONF}" | grep -qE "^\s*ssl_session_timeout\s" \
  47. || ADDS="${ADDS}${indent}$(_sed_rhs "${NGX_SSL_SESSION_TIMEOUT}" "")"
  48. if [ -n "${ADDS}" ]
  49. then
  50. ADDS="$(echo "${ADDS}" | sed -E 's/^\\n//')"
  51. echo "${CONF}" | grep -qE "$(_regex "${NGX_SERVER_NAME}" "${name}")" \
  52. && echo "${CONF}" \
  53. | sed -E "/$(_regex "${NGX_SERVER_NAME}" "${name}")/a\\${ADDS}" \
  54. > "${prefix}.with" \
  55. && _echo_sed "Added directives to ${prefix}.with:\n${ADDS}" \
  56. && return 0 \
  57. || _echo_sed "Cannot add directives to ${prefix}.sans, missing:\
  58. \n$(_sed_rhs "${NGX_SERVER_NAME}" "${name}")\n${ADDS}"
  59. return 1
  60. fi
  61. return 0
  62. }
  63. # ----------------------------------------------------------------------------
  64. test_setpoint() {
  65. [ "$(cat "$1")" = "$2" ] && return
  66. echo "$1:"; cat "$1"
  67. echo "differs from setpoint:"; echo "$2"
  68. [ "${PRINT_PASSED}" -gt 1 ] && pst_exit 1
  69. }
  70. test() {
  71. eval "$1 2>/dev/null >/dev/null"
  72. if [ "$?" -eq "$2" ]
  73. then
  74. [ "${PRINT_PASSED}" -gt 0 ] \
  75. && printf "%-72s%-1s\n" "$1" "2>/dev/null >/dev/null (-> $2?) passed."
  76. else
  77. printf "%-72s%-1s\n" "$1" "2>/dev/null >/dev/null (-> $2?) failed!!!"
  78. [ "${PRINT_PASSED}" -gt 1 ] && exit 1
  79. fi
  80. }
  81. [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s get_env ...\n" "${NGINX_UTIL}"
  82. eval $("${NGINX_UTIL}" get_env)
  83. test '[ -n "${NGINX_CONF}" ]' 0
  84. test '[ -n "${CONF_DIR}" ]' 0
  85. test '[ -n "${LAN_NAME}" ]' 0
  86. test '[ -n "${LAN_LISTEN}" ]' 0
  87. test '[ -n "${LAN_SSL_LISTEN}" ]' 0
  88. test '[ -n "${SSL_SESSION_CACHE_ARG}" ]' 0
  89. test '[ -n "${SSL_SESSION_TIMEOUT_ARG}" ]' 0
  90. test '[ -n "${ADD_SSL_FCT}" ]' 0
  91. [ "$PRINT_PASSED" -gt 0 ] && printf "\nPrepare files in %s ...\n" "${CONF_DIR}"
  92. mkdir -p "${CONF_DIR}"
  93. cd "${CONF_DIR}" || exit 2
  94. NGX_INCLUDE="include '\$';"
  95. NGX_SERVER_NAME="server_name * '\$' *;"
  96. NGX_SSL_CRT="ssl_certificate '\$.crt';"
  97. NGX_SSL_KEY="ssl_certificate_key '\$.key';"
  98. NGX_SSL_SESSION_CACHE="ssl_session_cache '$(echo "${SSL_SESSION_CACHE_ARG}" \
  99. | sed -E "s/$(__esc_regex "${LAN_NAME}")/\$/")';"
  100. NGX_SSL_SESSION_TIMEOUT="ssl_session_timeout '${SSL_SESSION_TIMEOUT_ARG}';"
  101. cat > "${LAN_NAME}.sans" <<EOF
  102. # default_server for the LAN addresses getting the IPs by:
  103. # ifstatus lan | jsonfilter -e '@["ipv4-address","ipv6-address"].*.address'
  104. server {
  105. include '${LAN_LISTEN}.default';
  106. server_name ${LAN_NAME};
  107. include conf.d/*.locations;
  108. }
  109. EOF
  110. CONFS="${CONFS} ${LAN_NAME}:0"
  111. cat > minimal.sans <<EOF
  112. server {
  113. server_name minimal;
  114. }
  115. EOF
  116. CONFS="${CONFS} minimal:0"
  117. cat > normal.sans <<EOF
  118. server {
  119. include '${LAN_LISTEN}';
  120. server_name normal;
  121. }
  122. EOF
  123. CONFS="${CONFS} normal:0"
  124. cat > more_server.sans <<EOF
  125. server {
  126. # include '${LAN_LISTEN}';
  127. server_name normal;
  128. }
  129. server {
  130. include '${LAN_LISTEN}';
  131. server_name more_server;
  132. }
  133. EOF
  134. CONFS="${CONFS} more_server:0"
  135. cat > more_names.sans <<EOF
  136. server {
  137. include '${LAN_LISTEN}';
  138. server_name example.com more_names example.org;
  139. }
  140. EOF
  141. CONFS="${CONFS} more_names:0"
  142. cat > different_name.sans <<EOF
  143. server {
  144. include '${LAN_LISTEN}';
  145. server_name minimal;
  146. }
  147. EOF
  148. CONFS="${CONFS} different_name:1"
  149. cat > comments.sans <<EOF
  150. server { # comment1
  151. # comment2
  152. include '${LAN_LISTEN}';
  153. server_name comments;
  154. # comment3
  155. } # comment4
  156. EOF
  157. CONFS="${CONFS} comments:0"
  158. cat > name_comment.sans <<EOF
  159. server {
  160. include '${LAN_LISTEN}';
  161. server_name name_comment; # comment
  162. }
  163. EOF
  164. CONFS="${CONFS} name_comment:0"
  165. cat > tab.sans <<EOF
  166. server {
  167. include '${LAN_LISTEN}';
  168. server_name tab;
  169. }
  170. EOF
  171. CONFS="${CONFS} tab:0"
  172. [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s init_lan ...\n" "${NGINX_UTIL}"
  173. mkdir -p "$(dirname "${LAN_LISTEN}")"
  174. cp "${LAN_NAME}.sans" "${LAN_NAME}.conf"
  175. test '"${NGINX_UTIL}" init_lan' 0
  176. [ "$PRINT_PASSED" -gt 0 ] && printf "\nSetup files in %s ...\n" "${CONF_DIR}"
  177. for conf in ${CONFS}
  178. do test 'setpoint_add_ssl " " '"${conf%:*}" "${conf#*:}"
  179. done
  180. test 'setpoint_add_ssl "\t" tab' 0 # fixes wrong indentation.
  181. [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s add_ssl ...\n" "${NGINX_UTIL}"
  182. cp different_name.sans different_name.with
  183. test '[ "${ADD_SSL_FCT}" = "add_ssl" ] ' 0
  184. for conf in ${CONFS}; do
  185. name="${conf%:*}"
  186. cp "${name}.sans" "${name}.conf"
  187. test '"${NGINX_UTIL}" add_ssl '"${name}" "${conf#*:}"
  188. test_setpoint "${name}.conf" "$(cat "${name}.with")"
  189. done
  190. [ "$PRINT_PASSED" -gt 0 ] && printf "\nTesting %s del_ssl ...\n" "${NGINX_UTIL}"
  191. sed -i "/server {/a\\ include '${LAN_LISTEN}';" minimal.sans
  192. for conf in ${CONFS}; do
  193. name="${conf%:*}"
  194. cp "${name}.with" "${name}.conf"
  195. test '"${NGINX_UTIL}" del_ssl '"${name}" "${conf#*:}"
  196. test_setpoint "${name}.conf" "$(cat "${name}.sans")"
  197. done