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.

134 lines
3.4 KiB

  1. #!/bin/sh
  2. # Converts a uci config file into an appropriate mosquitto.conf snippet
  3. # expected to be used in an init file to generate a config file to run from
  4. # Karl Palsson <karlp@remake.is> 2012.
  5. # Considered to be released into the public domain
  6. [ -f $IPKG_INSTROOT/lib/functions.sh ] && . $IPKG_INSTROOT/lib/functions.sh
  7. TCONF=/tmp/mosquitto.generated.$$.conf
  8. while getopts "f:" o; do
  9. case $o in
  10. f)
  11. TCONF=$OPTARG
  12. ;;
  13. esac
  14. done
  15. if [ -e $TCONF ]; then
  16. echo "Odd, same temporary generated config file already existed: $TCONF"
  17. exit 1
  18. fi
  19. echo "Generating mosquitto config file in $TCONF"
  20. NOW=$(date)
  21. echo "# mosquitto.conf file generated from UCI config." >>$TCONF
  22. echo "# Config snippet generated by $0 on $NOW" >>$TCONF
  23. echo "#" >> $TCONF
  24. # Usage: append_if cfg uci_name output_name
  25. # add a config line of the form "output_name <value>"
  26. # if the "uci_name" was found.
  27. # output_name defaults to uci_name if not specified.
  28. append_if() {
  29. local cfg="$1"
  30. local uci_name="$2"
  31. local out_name="$3"
  32. if [ -z "$out_name" ]; then
  33. out_name=$uci_name
  34. fi
  35. config_get val $cfg $uci_name
  36. if [ -n "$val" ]; then
  37. echo "$out_name $val" >> $TCONF
  38. fi
  39. }
  40. # mosquitto uses true/false, uci uses 1/0
  41. # note that this is not shell truthy, but equality with 1!
  42. append_bool() {
  43. if [ $2 -eq 1 ]; then
  44. echo "$1 true" >> $TCONF
  45. else
  46. echo "$1 false" >> $TCONF
  47. fi
  48. }
  49. # as per append_if, but gets the value as a uci bool, not raw
  50. append_optional_bool() {
  51. local cfg="$1"
  52. local uci_name="$2"
  53. local out_name="$3"
  54. config_get val $cfg $uci_name
  55. if [ -n "$val" ]; then
  56. config_get_bool real $cfg $uci_name
  57. append_bool $out_name $real
  58. fi
  59. }
  60. mosq_general() {
  61. config_get destinations "$1" log_dest
  62. for dest in $destinations; do
  63. echo "log_dest $dest" >> $TCONF
  64. done
  65. config_get_bool no_remote "$1" no_remote_access 0
  66. if [ "$no_remote" -eq 1 ]; then
  67. echo "bind_address 127.0.0.1" >> $TCONF
  68. fi
  69. config_get port "$1" port 1883
  70. echo "port $port" >> $TCONF
  71. append_if "$1" protocol
  72. append_if "$1" max_inflight_messages
  73. append_if "$1" max_queued_messages
  74. }
  75. add_listener() {
  76. echo "" >> $TCONF
  77. config_get port "$1" port
  78. if [ -z "$port" ]; then
  79. echo "Ignoring listener section without port"
  80. return
  81. fi
  82. config_get_bool no_remote "$1" no_remote_access 0
  83. if [ "$no_remote" -eq 1 ]; then
  84. echo "listener $port 127.0.0.1" >> $TCONF
  85. else
  86. echo "listener $port" >> $TCONF
  87. fi
  88. append_if "$1" protocol
  89. }
  90. add_topic() {
  91. echo "topic $1" >> $TCONF
  92. }
  93. add_bridge() {
  94. config_get conn "$1" connection
  95. config_get addr "$1" address
  96. if [ -z "$conn" -o -z "$addr" ]; then
  97. echo "Ignoring bridge section, misisng connection/address"
  98. return
  99. fi
  100. echo "" >> $TCONF
  101. echo "# Bridge connection from UCI section" >> $TCONF
  102. append_if "$1" connection
  103. append_if "$1" address
  104. config_list_foreach "$1" topic add_topic
  105. append_optional_bool "$1" cleansession cleansession
  106. append_optional_bool "$1" try_private try_private
  107. append_if "$1" clientid
  108. append_if "$1" identity bridge_identity
  109. append_if "$1" psk bridge_psk
  110. append_if "$1" tls_version bridge_tls_version
  111. }
  112. config_load "mosquitto"
  113. config_foreach mosq_general "mosquitto"
  114. config_foreach add_listener "listener"
  115. config_foreach add_bridge "bridge"