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.

273 lines
8.8 KiB

  1. #!/bin/sh /etc/rc.common
  2. USE_PROCD=1
  3. START=30
  4. STOP=51
  5. stubby="/usr/sbin/stubby"
  6. stubby_init="/etc/init.d/stubby"
  7. stubby_config_dir="/var/etc/stubby"
  8. stubby_config="$stubby_config_dir/stubby.yml"
  9. stubby_pid_file="/var/run/stubby.pid"
  10. stubby_manual_config="/etc/stubby/stubby.yml"
  11. boot()
  12. {
  13. stubby_boot=1
  14. rc_procd start_service
  15. }
  16. generate_config()
  17. {
  18. local config_file="$1"
  19. local round_robin
  20. local tls_authentication
  21. local tls_query_padding_blocksize
  22. local edns_client_subnet_private
  23. local idle_timeout
  24. local appdata_dir
  25. local trust_anchors_backoff_time
  26. local tls_connection_retries
  27. local tls_backoff_time
  28. local timeout
  29. local dnssec_return_status
  30. local dnssec_trust_anchors
  31. local listen_addresses_section=0
  32. local dns_transport_list_section=0
  33. local upstream_recursive_servers_section=0
  34. local command_line_arguments
  35. local log_level
  36. local tls_cipher_list
  37. local tls_ciphersuites
  38. local tls_min_version
  39. local tls_max_version
  40. # Generate configuration. See: https://github.com/getdnsapi/stubby/blob/develop/stubby.yml.example
  41. echo "# Autogenerated configuration from uci data" > "$config_file"
  42. echo "resolution_type: GETDNS_RESOLUTION_STUB" >> "$config_file"
  43. config_get round_robin "global" round_robin_upstreams "1"
  44. echo "round_robin_upstreams: $round_robin" >> "$config_file"
  45. config_get appdata_dir "global" appdata_dir "/var/lib/stubby"
  46. echo "appdata_dir: \"$appdata_dir\"" >> "$config_file"
  47. config_get trust_anchors_backoff_time "global" trust_anchors_backoff_time "2500"
  48. echo "trust_anchors_backoff_time: $trust_anchors_backoff_time" >> "$config_file"
  49. config_get tls_connection_retries "global" tls_connection_retries ""
  50. if [ -n "$tls_connection_retries" ]; then
  51. echo "tls_connection_retries: $tls_connection_retries" >> "$config_file"
  52. fi
  53. config_get tls_backoff_time "global" tls_backoff_time ""
  54. if [ -n "$tls_backoff_time" ]; then
  55. echo "tls_backoff_time: $tls_backoff_time" >> "$config_file"
  56. fi
  57. config_get timeout "global" timeout ""
  58. if [ -n "$timeout" ]; then
  59. echo "timeout: $timeout" >> "$config_file"
  60. fi
  61. config_get_bool tls_authentication "global" tls_authentication "1"
  62. if [ "$tls_authentication" = "1" ]; then
  63. echo "tls_authentication: GETDNS_AUTHENTICATION_REQUIRED" >> "$config_file"
  64. else
  65. echo "tls_authentication: GETDNS_AUTHENTICATION_NONE" >> "$config_file"
  66. fi
  67. config_get_bool dnssec_return_status "global" dnssec_return_status "0"
  68. if [ "$dnssec_return_status" = "1" ]; then
  69. echo "dnssec_return_status: GETDNS_EXTENSION_TRUE" >> "$config_file"
  70. fi
  71. config_get dnssec_trust_anchors "global" dnssec_trust_anchors ""
  72. if [ -n "$dnssec_trust_anchors" ]; then
  73. echo "dnssec_trust_anchors: \"$dnssec_trust_anchors\"" >> "$config_file"
  74. fi
  75. config_get tls_query_padding_blocksize "global" tls_query_padding_blocksize "128"
  76. echo "tls_query_padding_blocksize: $tls_query_padding_blocksize" >> "$config_file"
  77. config_get_bool edns_client_subnet_private "global" edns_client_subnet_private "1"
  78. echo "edns_client_subnet_private: $edns_client_subnet_private" >> "$config_file"
  79. config_get idle_timeout "global" idle_timeout "10000"
  80. echo "idle_timeout: $idle_timeout" >> "$config_file"
  81. config_get tls_cipher_list "global" tls_cipher_list ""
  82. if [ -n "$tls_cipher_list" ]; then
  83. echo "tls_cipher_list: \"$tls_cipher_list\"" >> "$config_file"
  84. fi
  85. config_get tls_ciphersuites "global" tls_ciphersuites ""
  86. if [ -n "$tls_ciphersuites" ]; then
  87. echo "tls_ciphersuites: \"$tls_ciphersuites\"" >> "$config_file"
  88. fi
  89. config_get tls_min_version "global" tls_min_version ""
  90. if [ -n "$tls_min_version" ]; then
  91. echo "tls_min_version: GETDNS_TLS${tls_min_version/\./_}" >> "$config_file"
  92. fi
  93. config_get tls_max_version "global" tls_max_version ""
  94. if [ -n "$tls_max_version" ]; then
  95. echo "tls_max_version: GETDNS_TLS${tls_max_version/\./_}" >> "$config_file"
  96. fi
  97. handle_listen_address_value()
  98. {
  99. local value="$1"
  100. if [ "$listen_addresses_section" = 0 ]; then
  101. echo "listen_addresses:" >> "$config_file"
  102. listen_addresses_section=1
  103. fi
  104. echo " - $value" >> "$config_file"
  105. }
  106. config_list_foreach "global" listen_address handle_listen_address_value
  107. handle_dns_transport_list_value()
  108. {
  109. local value="$1"
  110. if [ "$dns_transport_list_section" = 0 ]; then
  111. echo "dns_transport_list:" >> "$config_file"
  112. dns_transport_list_section=1
  113. fi
  114. echo " - $value" >> "$config_file"
  115. }
  116. config_list_foreach "global" dns_transport handle_dns_transport_list_value
  117. handle_resolver()
  118. {
  119. local config=$1
  120. local address
  121. local tls_auth_name
  122. local tls_port
  123. local tls_pubkey_pinset_section=0
  124. local tls_cipher_list
  125. local tls_ciphersuites
  126. local tls_min_version
  127. local tls_max_version
  128. if [ "$upstream_recursive_servers_section" = 0 ]; then
  129. echo "upstream_recursive_servers:" >> "$config_file"
  130. upstream_recursive_servers_section=1
  131. fi
  132. config_get address "$config" address
  133. echo " - address_data: $address" >> "$config_file"
  134. config_get tls_auth_name "$config" tls_auth_name
  135. echo " tls_auth_name: \"$tls_auth_name\"" >> "$config_file"
  136. config_get tls_auth_port "$config" tls_port ""
  137. if [ -n "$tls_port" ]; then
  138. echo " tls_port: $tls_port" >> "$config_file"
  139. fi
  140. config_get tls_cipher_list "$config" tls_cipher_list ""
  141. if [ -n "$tls_cipher_list" ]; then
  142. echo " tls_cipher_list: \"$tls_cipher_list\"" >> "$config_file"
  143. fi
  144. config_get tls_ciphersuites "$config" tls_ciphersuites ""
  145. if [ -n "$tls_ciphersuites" ]; then
  146. echo " tls_ciphersuites: \"$tls_ciphersuites\"" >> "$config_file"
  147. fi
  148. config_get tls_min_version "$config" tls_min_version ""
  149. if [ -n "$tls_min_version" ]; then
  150. echo " tls_min_version: GETDNS_TLS${tls_min_version/\./_}" >> "$config_file"
  151. fi
  152. config_get tls_max_version "$config" tls_max_version ""
  153. if [ -n "$tls_max_version" ]; then
  154. echo " tls_max_version: GETDNS_TLS${tls_max_version/\./_}" >> "$config_file"
  155. fi
  156. handle_resolver_spki()
  157. {
  158. local val="$1"
  159. local digest="${val%%/*}"
  160. local value="${val#*/}"
  161. if [ "$tls_pubkey_pinset_section" = 0 ]; then
  162. echo " tls_pubkey_pinset:" >> "$config_file"
  163. tls_pubkey_pinset_section=1
  164. fi
  165. echo " - digest: \"$digest\"" >> "$config_file"
  166. echo " value: $value" >> "$config_file"
  167. }
  168. config_list_foreach "$config" spki handle_resolver_spki
  169. }
  170. config_foreach handle_resolver resolver
  171. }
  172. start_service() {
  173. local config_file_tmp
  174. local manual
  175. local log_level
  176. local command_line_arguments
  177. mkdir -p "$stubby_config_dir"
  178. config_load "stubby"
  179. config_get_bool manual "global" manual "0"
  180. if [ "$manual" = "1" ]; then
  181. cp "$stubby_manual_config" "$stubby_config"
  182. else
  183. config_file_tmp="$stubby_config.$$"
  184. generate_config "$config_file_tmp"
  185. mv "$config_file_tmp" "$stubby_config"
  186. fi
  187. config_get command_line_arguments "global" command_line_arguments ""
  188. config_get log_level "global" log_level ""
  189. if [ "$("$stubby_init" enabled; printf "%u" $?)" -eq 0 ]; then
  190. if [ -n "$stubby_boot" ]; then
  191. local trigger
  192. trigger="$(uci_get stubby global trigger)"
  193. if [ "$trigger" != "timed" ]; then
  194. return 0
  195. fi
  196. fi
  197. procd_open_instance "stubby"
  198. procd_set_param command "$stubby" -C "$stubby_config"
  199. if [ -n "$log_level" ]; then
  200. procd_append_param command -v "$log_level"
  201. fi
  202. if [ -n "$command_line_arguments" ]; then
  203. procd_append_param command "$command_line_arguments"
  204. fi
  205. procd_set_param respawn
  206. procd_set_param file "$stubby_config"
  207. procd_set_param stdout 1
  208. procd_set_param stderr 1
  209. procd_set_param pidfile "$stubby_pid_file"
  210. procd_set_param user stubby
  211. procd_close_instance
  212. fi
  213. }
  214. service_triggers()
  215. {
  216. local trigger
  217. local delay
  218. trigger="$(uci_get stubby global trigger)"
  219. delay="$(uci_get stubby global triggerdelay "2")"
  220. if [ "$trigger" != "none" ] && [ "$trigger" != "timed" ]; then
  221. PROCD_RELOAD_DELAY=$((${delay:-2} * 1000))
  222. procd_add_interface_trigger "interface.*.up" "$trigger" "$stubby_init" start
  223. fi
  224. procd_add_reload_trigger "stubby"
  225. }