mosquitto: support anonymous user per listener
[feed/packages.git] / net / mosquitto / files / etc / init.d / mosquitto
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
6 START=80
7 USE_PROCD=1
8 TCONF=/tmp/mosquitto.generated.conf
9 CONF_WATCH=/etc/config/mosquitto
10
11 # Usage: append_if cfg uci_name output_name
12 # add a config line of the form "output_name <value>"
13 # if the "uci_name" was found.
14 # output_name defaults to uci_name if not specified.
15 append_if() {
16 local cfg="$1"
17 local uci_name="$2"
18 local out_name="$3"
19 if [ -z "$out_name" ]; then
20 out_name=$uci_name
21 fi
22 config_get val $cfg $uci_name
23 if [ -n "$val" ]; then
24 echo "$out_name $val" >> $TCONF
25 fi
26 }
27
28 # mosquitto uses true/false, uci uses 1/0
29 # note that this is not shell truthy, but equality with 1!
30 append_bool() {
31 if [ $2 -eq 1 ]; then
32 echo "$1 true" >> $TCONF
33 else
34 echo "$1 false" >> $TCONF
35 fi
36 }
37
38 # as per append_if, but gets the value as a uci bool, not raw
39 append_optional_bool() {
40 local cfg="$1"
41 local uci_name="$2"
42 local out_name="$3"
43 if [ -z "$out_name" ]; then
44 out_name=$uci_name
45 fi
46 config_get val $cfg $uci_name
47 if [ -n "$val" ]; then
48 config_get_bool real $cfg $uci_name
49 append_bool $out_name $real
50 fi
51 }
52
53
54 convert_mosq_general() {
55 local cfg="$1"
56 config_get destinations "$1" log_dest
57 for dest in $destinations; do
58 echo "log_dest $dest" >> $TCONF
59 done
60
61 config_get_bool no_remote "$1" no_remote_access 0
62 if [ "$no_remote" -eq 1 ]; then
63 echo "bind_address localhost" >> $TCONF
64 fi
65
66 append_if "$1" port
67 # per listener settings must be set before any potential security settings
68 append_optional_bool "$1" per_listener_settings
69 append_if "$1" acl_file
70 append_optional_bool "$1" allow_anonymous
71 append_optional_bool "$1" allow_duplicate_messages
72 append_if "$1" clientid_prefixes
73 append_optional_bool "$1" connection_messages
74 append_if "$1" include_dir
75 append_if "$1" log_facility
76 append_optional_bool "$1" log_timestamp
77 config_get log_types "$1" log_types
78 for log_type in $log_types; do
79 echo "log_type $log_type" >> $TCONF
80 done
81 append_if "$1" max_inflight_bytes
82 append_if "$1" max_inflight_messages
83 append_if "$1" max_queued_bytes
84 append_if "$1" max_queued_messages
85 append_if "$1" message_size_limit
86 append_if "$1" password_file
87 append_if "$1" pid_file
88 append_if "$1" psk_file
89 append_optional_bool "$1" queue_qos0_messages
90 append_optional_bool "$1" retain_available
91 append_optional_bool "$1" set_tcp_nodelay
92 append_if "$1" protocol
93 append_if "$1" sys_interval
94 append_optional_bool "$1" upgrade_outgoing_qos
95 append_if "$1" user
96 append_if "$1" websockets_log_level
97 append_if "$1" websockets_headers_size
98 # can be general or per listener, see per_listener_settings
99 append_if "$1" auth_plugin
100 # Careful, this relies on internal behaviour of the cfg_load functions!
101 set | grep "CONFIG_$1_auth_opt_" | sed "s/^.*_\(auth_opt_.*\)='\(.*\)'/\1 \2/" >> $TCONF
102 }
103
104 convert_persistence() {
105 local cfg="$1"
106
107 append_if "$cfg" client_expiration persistent_client_expiration
108 append_if "$cfg" autosave_interval
109 append_optional_bool "$cfg" autosave_on_changes
110 append_optional_bool "$cfg" persistence
111 append_if "$cfg" file persistence_file
112 append_if "$cfg" store_clean_interval
113 config_get loc "$cfg" location
114 if [ -n "$loc" ]; then
115 [ -d "$loc" ] || {
116 mkdir -p "$loc";
117 chown mosquitto "$loc";
118 }
119 echo "persistence_location $loc" >> $TCONF
120 fi
121 }
122
123 add_listener() {
124 echo "" >> $TCONF
125 config_get port "$1" port
126 if [ -z "$port" ]; then
127 echo "Ignoring listener section without port"
128 return
129 fi
130 # "no_remote" is a non-standard shortcut option
131 local bind_address=""
132 config_get_bool no_remote "$1" no_remote_access 0
133 [ "$no_remote" -eq 1 ] && bind_address="127.0.0.1"
134 config_get bind_address "$1" bind_address $bind_address
135 echo "listener $port $bind_address" >> $TCONF
136
137 append_if "$1" http_dir
138 append_if "$1" max_connections
139 append_if "$1" max_qos
140 append_if "$1" max_topic_alias
141 append_if "$1" mount_point
142 append_if "$1" protocol
143 append_if "$1" socket_domain
144 append_optional_bool "$1" use_username_as_clientid
145
146 append_if "$1" cafile
147 append_if "$1" capath
148 append_if "$1" certfile
149 append_if "$1" ciphers
150 append_if "$1" ciphers_tls1.3
151 append_if "$1" crlfile
152 append_if "$1" dhparamfile
153 append_if "$1" keyfile
154 append_optional_bool "$1" require_certificate
155 append_if "$1" tls_engine
156 append_if "$1" tls_engine_kpass_sha1
157 append_if "$1" tls_keyform
158 append_if "$1" tls_version
159 append_if "$1" allow_anonymous
160 append_optional_bool "$1" use_identity_as_username
161 append_optional_bool "$1" use_subject_as_username
162 append_if "$1" psk_hint
163 # can be general or per listener, see per_listener_settings
164 append_if "$1" auth_plugin
165 # Careful, this relies on internal behaviour of the cfg_load functions!
166 set | grep "CONFIG_$1_auth_opt_" | sed "s/^.*_\(auth_opt_.*\)='\(.*\)'/\1 \2/" >> $TCONF
167 }
168
169 add_topic() {
170 echo "topic $1" >> $TCONF
171 }
172
173 add_bridge() {
174 config_get conn "$1" connection
175 config_get addr "$1" address
176 if [ -z "$conn" -o -z "$addr" ]; then
177 echo "Ignoring bridge section with missing connection/address"
178 return
179 fi
180 # Also invalid if no topics are defined, mosquitto will not start!
181 config_get tlen "$1" "topic_LENGTH"
182 if [ -z "$tlen" ]; then
183 echo "Ignoring bridge section with no topics defined"
184 return
185 fi
186
187 echo "" >> $TCONF
188 echo "# Bridge connection from UCI section" >> $TCONF
189 append_if "$1" connection
190 append_if "$1" address
191
192 append_optional_bool "$1" attempt_unsubscribe bridge_attempt_unsubscribe
193 append_if "$1" bind_address bridge_bind_address
194 append_if "$1" max_packet_size bridge_max_packet_size
195 append_optional_bool "$1" outgoing_retain bridge_outgoing_retain
196 append_if "$1" protocol_version bridge_protocol_version
197 append_optional_bool "$1" cleansession
198 append_optional_bool "$1" local_cleansession
199 append_if "$1" keepalive_interval
200 append_if "$1" idle_timeout
201 append_if "$1" local_clientid
202 append_if "$1" local_password
203 append_if "$1" local_username
204 append_optional_bool "$1" notifications
205 append_optional_bool "$1" notifications_local_only
206 append_if "$1" notification_topic
207 # Note, deprecated upstream, preserve old uci configs
208 append_if "$1" clientid remote_clientid
209 append_if "$1" remote_clientid
210 # Note, deprecated upstream, preserve old uci configs
211 append_if "$1" password remote_password
212 append_if "$1" remote_password
213 # Note, deprecated upstream, preserve old uci configs
214 append_if "$1" username remote_username
215 append_if "$1" remote_username
216 append_if "$1" restart_timeout
217 append_optional_bool "$1" round_robin
218 append_if "$1" start_type
219 append_if "$1" threshold
220 config_list_foreach "$1" topic add_topic
221 append_optional_bool "$1" try_private
222
223 append_if "$1" alpn bridge_alpn
224 append_if "$1" cafile bridge_cafile
225 append_if "$1" capath bridge_capath
226 append_if "$1" certfile bridge_certfile
227 append_if "$1" identity bridge_identity
228 append_optional_bool "$1" insecure bridge_insecure
229 append_if "$1" keyfile bridge_keyfile
230 append_if "$1" psk bridge_psk
231 append_optional_bool "$1" require_ocsp bridge_require_ocsp
232 append_if "$1" tls_version bridge_tls_version
233 }
234
235
236 convert_uci() {
237 rm -rf $TCONF
238 echo "Generating mosquitto config file in $TCONF"
239 echo "# mosquitto.conf file generated from UCI config." >>$TCONF
240 # Don't include a timestamp, it makes md5sum compares fail
241
242 config_load mosquitto
243 config_foreach convert_mosq_general "mosquitto"
244 config_foreach convert_persistence "persistence"
245 config_foreach add_listener "listener"
246 config_foreach add_bridge "bridge"
247 }
248
249 start_service_real() {
250 local cfg="$1"
251 local use_uci write_pid
252 config_get use_uci "$cfg" use_uci
253 if [ "$use_uci" -eq 1 ]; then
254 CONF=$TCONF
255 convert_uci
256 else
257 CONF=/etc/mosquitto/mosquitto.conf
258 CONF_WATCH=$CONF
259 fi
260 config_get write_pid "$cfg" write_pid 0
261
262 procd_open_instance
263 procd_set_param command mosquitto
264 procd_append_param command -c $CONF
265 # Makes /etc/init.d/mosquitto reload work if you edit the final file.
266 procd_set_param file $CONF_WATCH
267 [ "$write_pid" -eq 1 ] && procd_set_param pidfile /var/run/mosquitto.pid
268 procd_set_param respawn
269 procd_close_instance
270 }
271
272 start_service() {
273 config_load mosquitto
274 config_foreach start_service_real owrt
275 }
276
277 service_triggers() {
278 # Makes "reload_config" work
279 procd_add_reload_trigger "mosquitto"
280 }