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.

218 lines
6.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. # Generate configuration. See: https://github.com/getdnsapi/stubby/blob/develop/stubby.yml.example
  37. echo "# Autogenerated configuration from uci data" > "$config_file"
  38. echo "resolution_type: GETDNS_RESOLUTION_STUB" >> "$config_file"
  39. config_get round_robin "global" round_robin_upstreams "1"
  40. echo "round_robin_upstreams: $round_robin" >> "$config_file"
  41. config_get appdata_dir "global" appdata_dir "/var/lib/stubby"
  42. echo "appdata_dir: \"$appdata_dir\"" >> "$config_file"
  43. config_get trust_anchors_backoff_time "global" trust_anchors_backoff_time "2500"
  44. echo "trust_anchors_backoff_time: $trust_anchors_backoff_time" >> "$config_file"
  45. config_get tls_connection_retries "global" tls_connection_retries ""
  46. if [ -n "$tls_connection_retries" ]; then
  47. echo "tls_connection_retries: $tls_connection_retries" >> "$config_file"
  48. fi
  49. config_get tls_backoff_time "global" tls_backoff_time ""
  50. if [ -n "$tls_backoff_time" ]; then
  51. echo "tls_backoff_time: $tls_backoff_time" >> "$config_file"
  52. fi
  53. config_get timeout "global" timeout ""
  54. if [ -n "$timeout" ]; then
  55. echo "timeout: $timeout" >> "$config_file"
  56. fi
  57. config_get_bool tls_authentication "global" tls_authentication "1"
  58. if [ "$tls_authentication" = "1" ]; then
  59. echo "tls_authentication: GETDNS_AUTHENTICATION_REQUIRED" >> "$config_file"
  60. else
  61. echo "tls_authentication: GETDNS_AUTHENTICATION_NONE" >> "$config_file"
  62. fi
  63. config_get_bool dnssec_return_status "global" dnssec_return_status "0"
  64. if [ "$dnssec_return_status" = "1" ]; then
  65. echo "dnssec_return_status: GETDNS_EXTENSION_TRUE" >> "$config_file"
  66. fi
  67. config_get dnssec_trust_anchors "global" dnssec_trust_anchors ""
  68. if [ -n "$dnssec_trust_anchors" ]; then
  69. echo "dnssec_trust_anchors: \"$dnssec_trust_anchors\"" >> "$config_file"
  70. fi
  71. config_get tls_query_padding_blocksize "global" tls_query_padding_blocksize "128"
  72. echo "tls_query_padding_blocksize: $tls_query_padding_blocksize" >> "$config_file"
  73. config_get_bool edns_client_subnet_private "global" edns_client_subnet_private "1"
  74. echo "edns_client_subnet_private: $edns_client_subnet_private" >> "$config_file"
  75. config_get idle_timeout "global" idle_timeout "10000"
  76. echo "idle_timeout: $idle_timeout" >> "$config_file"
  77. handle_listen_address_value()
  78. {
  79. local value="$1"
  80. if [ "$listen_addresses_section" = 0 ]; then
  81. echo "listen_addresses:" >> "$config_file"
  82. listen_addresses_section=1
  83. fi
  84. echo " - $value" >> "$config_file"
  85. }
  86. config_list_foreach "global" listen_address handle_listen_address_value
  87. handle_dns_transport_list_value()
  88. {
  89. local value="$1"
  90. if [ "$dns_transport_list_section" = 0 ]; then
  91. echo "dns_transport_list:" >> "$config_file"
  92. dns_transport_list_section=1
  93. fi
  94. echo " - $value" >> "$config_file"
  95. }
  96. config_list_foreach "global" dns_transport handle_dns_transport_list_value
  97. handle_resolver()
  98. {
  99. local config=$1
  100. local address
  101. local tls_auth_name
  102. local tls_pubkey_pinset_section=0
  103. if [ "$upstream_recursive_servers_section" = 0 ]; then
  104. echo "upstream_recursive_servers:" >> "$config_file"
  105. upstream_recursive_servers_section=1
  106. fi
  107. config_get address "$config" address
  108. config_get tls_auth_name "$config" tls_auth_name
  109. echo " - address_data: $address" >> "$config_file"
  110. echo " tls_auth_name: \"$tls_auth_name\"" >> "$config_file"
  111. handle_resolver_spki()
  112. {
  113. local val="$1"
  114. local digest="${val%/*}"
  115. local value="${val#*/}"
  116. if [ "$tls_pubkey_pinset_section" = 0 ]; then
  117. echo " tls_pubkey_pinset:" >> "$config_file"
  118. tls_pubkey_pinset_section=1
  119. fi
  120. echo " - digest: \"$digest\"" >> "$config_file"
  121. echo " value: $value" >> "$config_file"
  122. }
  123. config_list_foreach "$config" spki handle_resolver_spki
  124. }
  125. config_foreach handle_resolver resolver
  126. }
  127. start_service() {
  128. local config_file_tmp
  129. local manual
  130. local log_level
  131. local command_line_arguments
  132. mkdir -p "$stubby_config_dir"
  133. config_load "stubby"
  134. config_get_bool manual "global" manual "0"
  135. if [ "$manual" = "1" ]; then
  136. cp "$stubby_manual_config" "$stubby_config"
  137. else
  138. config_file_tmp="$stubby_config.$$"
  139. generate_config "$config_file_tmp"
  140. mv "$config_file_tmp" "$stubby_config"
  141. fi
  142. config_get command_line_arguments "global" command_line_arguments ""
  143. config_get log_level "global" log_level ""
  144. if [ "$("$stubby_init" enabled; printf "%u" $?)" -eq 0 ]; then
  145. if [ -n "$stubby_boot" ]; then
  146. local trigger
  147. trigger="$(uci_get stubby global trigger)"
  148. if [ "$trigger" != "timed" ]; then
  149. return 0
  150. fi
  151. fi
  152. procd_open_instance "stubby"
  153. procd_set_param command "$stubby" -C "$stubby_config"
  154. if [ -n "$log_level" ]; then
  155. procd_append_param command -v "$log_level"
  156. fi
  157. if [ -n "$command_line_arguments" ]; then
  158. procd_append_param command "$command_line_arguments"
  159. fi
  160. procd_set_param respawn
  161. procd_set_param file "$stubby_config"
  162. procd_set_param stdout 1
  163. procd_set_param stderr 1
  164. procd_set_param pidfile "$stubby_pid_file"
  165. procd_set_param user stubby
  166. procd_close_instance
  167. fi
  168. }
  169. service_triggers()
  170. {
  171. local trigger
  172. local delay
  173. trigger="$(uci_get stubby global trigger)"
  174. delay="$(uci_get stubby global triggerdelay "2")"
  175. if [ "$trigger" != "none" ] && [ "$trigger" != "timed" ]; then
  176. PROCD_RELOAD_DELAY=$((${delay:-2} * 1000))
  177. procd_add_interface_trigger "interface.*.up" "$trigger" "$stubby_init" start
  178. fi
  179. procd_add_reload_trigger "stubby"
  180. }