mosquitto 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/bin/sh /etc/rc.common
  2. # Basic init script for mosquitto
  3. # April 2012, OpenWrt.org
  4. # Provides support for the luci-app-mosquitto package, if installed
  5. START=80
  6. USE_PROCD=1
  7. TCONF=/tmp/mosquitto.generated.conf
  8. # Usage: append_if cfg uci_name output_name
  9. # add a config line of the form "output_name <value>"
  10. # if the "uci_name" was found.
  11. # output_name defaults to uci_name if not specified.
  12. append_if() {
  13. local cfg="$1"
  14. local uci_name="$2"
  15. local out_name="$3"
  16. if [ -z "$out_name" ]; then
  17. out_name=$uci_name
  18. fi
  19. config_get val $cfg $uci_name
  20. if [ -n "$val" ]; then
  21. echo "$out_name $val" >> $TCONF
  22. fi
  23. }
  24. # mosquitto uses true/false, uci uses 1/0
  25. # note that this is not shell truthy, but equality with 1!
  26. append_bool() {
  27. if [ $2 -eq 1 ]; then
  28. echo "$1 true" >> $TCONF
  29. else
  30. echo "$1 false" >> $TCONF
  31. fi
  32. }
  33. # as per append_if, but gets the value as a uci bool, not raw
  34. append_optional_bool() {
  35. local cfg="$1"
  36. local uci_name="$2"
  37. local out_name="$3"
  38. config_get val $cfg $uci_name
  39. if [ -n "$val" ]; then
  40. config_get_bool real $cfg $uci_name
  41. append_bool $out_name $real
  42. fi
  43. }
  44. convert_mosq_general() {
  45. local cfg="$1"
  46. config_get destinations "$1" log_dest
  47. for dest in $destinations; do
  48. echo "log_dest $dest" >> $TCONF
  49. done
  50. config_get_bool no_remote "$1" no_remote_access 0
  51. if [ "$no_remote" -eq 1 ]; then
  52. echo "bind_address localhost" >> $TCONF
  53. fi
  54. config_get port "$1" port 1883
  55. echo "port $port" >> $TCONF
  56. append_if "$1" protocol
  57. append_if "$1" max_inflight_messages
  58. append_if "$1" max_queued_messages
  59. append_if "$1" sys_interval
  60. }
  61. convert_persistence() {
  62. local cfg="$1"
  63. append_if "$cfg" client_expiration persistent_client_expiration
  64. append_if "$cfg" autosave_interval
  65. append_optional_bool "$cfg" autosave_on_changes autosave_on_changes
  66. append_optional_bool "$cfg" persistence persistence
  67. append_if "$cfg" file persistence_file
  68. config_get loc "$cfg" location
  69. if [ -n "$loc" ]; then
  70. [ -d "$loc" ] || {
  71. mkdir -p "$loc";
  72. chown mosquitto "$loc";
  73. }
  74. echo "persistence_location $loc" >> $TCONF
  75. fi
  76. }
  77. add_listener() {
  78. echo "" >> $TCONF
  79. config_get port "$1" port
  80. if [ -z "$port" ]; then
  81. echo "Ignoring listener section without port"
  82. return
  83. fi
  84. config_get_bool no_remote "$1" no_remote_access 0
  85. if [ "$no_remote" -eq 1 ]; then
  86. echo "listener $port 127.0.0.1" >> $TCONF
  87. else
  88. echo "listener $port" >> $TCONF
  89. fi
  90. append_if "$1" protocol
  91. append_if "$1" http_dir
  92. append_optional_bool "$1" use_username_as_clientid use_username_as_clientid
  93. append_if "$1" cafile
  94. append_if "$1" capath
  95. append_if "$1" certfile
  96. append_if "$1" keyfile
  97. append_if "$1" tls_version
  98. append_optional_bool "$1" require_certificate require_certificate
  99. append_optional_bool "$1" use_identity_as_username use_identity_as_username
  100. append_if "$1" crlfile
  101. append_if "$1" ciphers
  102. append_if "$1" psk_hint
  103. }
  104. add_topic() {
  105. echo "topic $1" >> $TCONF
  106. }
  107. add_bridge() {
  108. config_get conn "$1" connection
  109. config_get addr "$1" address
  110. if [ -z "$conn" -o -z "$addr" ]; then
  111. echo "Ignoring bridge section, misisng connection/address"
  112. return
  113. fi
  114. echo "" >> $TCONF
  115. echo "# Bridge connection from UCI section" >> $TCONF
  116. append_if "$1" connection
  117. append_if "$1" address
  118. config_list_foreach "$1" topic add_topic
  119. append_optional_bool "$1" cleansession cleansession
  120. append_optional_bool "$1" try_private try_private
  121. append_optional_bool "$1" notifications notifications
  122. append_optional_bool "$1" round_robin round_robin
  123. # Note, deprecated upstream, preserve old uci configs
  124. append_if "$1" clientid remote_clientid
  125. append_if "$1" remote_clientid
  126. append_if "$1" local_clientid
  127. append_if "$1" notification_topic
  128. append_if "$1" keepalive_interval
  129. append_if "$1" start_type
  130. append_if "$1" restart_timeout
  131. append_if "$1" idle_timeout
  132. append_if "$1" threshold
  133. append_if "$1" protocol_version bridge_protocol_version
  134. append_optional_bool "$1" attempt_unsubscribe bridge_attempt_unsubscribe
  135. append_if "$1" identity bridge_identity
  136. append_if "$1" psk bridge_psk
  137. append_if "$1" tls_version bridge_tls_version
  138. append_if "$1" restart_timeout
  139. append_if "$1" capath bridge_capath
  140. append_if "$1" cafile bridge_cafile
  141. append_if "$1" certfile bridge_certfile
  142. append_if "$1" keyfile bridge_keyfile
  143. # Note, deprecated upstream, preserve old uci configs
  144. append_if "$1" username remote_username
  145. # Note, deprecated upstream, preserve old uci configs
  146. append_if "$1" password remote_password
  147. append_if "$1" remote_username
  148. append_if "$1" remote_password
  149. }
  150. convert_uci() {
  151. rm -rf $TCONF
  152. echo "Generating mosquitto config file in $TCONF"
  153. echo "# mosquitto.conf file generated from UCI config." >>$TCONF
  154. # Don't include a timestamp, it makes md5sum compares fail
  155. config_load mosquitto
  156. config_foreach convert_mosq_general "mosquitto"
  157. config_foreach convert_persistence "persistence"
  158. config_foreach add_listener "listener"
  159. config_foreach add_bridge "bridge"
  160. }
  161. start_service_real() {
  162. local cfg="$1"
  163. local use_uci write_pid
  164. config_get use_uci "$cfg" use_uci
  165. if [ "$use_uci" -eq 1 ]; then
  166. CONF=$TCONF
  167. convert_uci
  168. else
  169. CONF=/etc/mosquitto/mosquitto.conf
  170. fi
  171. config_get write_pid "$cfg" write_pid 0
  172. procd_open_instance
  173. procd_set_param command mosquitto
  174. procd_append_param command -c $CONF
  175. # Makes /etc/init.d/mosquitto reload work if you edit the final file.
  176. procd_set_param file $CONF
  177. [ "$write_pid" -eq 1 ] && procd_set_param pidfile /var/run/mosquitto.pid
  178. procd_close_instance
  179. }
  180. start_service() {
  181. config_load mosquitto
  182. config_foreach start_service_real owrt
  183. }
  184. service_triggers() {
  185. # Makes "reload_config" work
  186. procd_add_reload_trigger "mosquitto"
  187. }