3cd04e3263
- Added app icon - added certificates and user management in app config - added user customization files in `/share`
97 lines
2.3 KiB
Bash
97 lines
2.3 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
|
|
CONFIG=/config
|
|
SHARE=/share/visquitto
|
|
PASSWD=/data/passwd
|
|
FINAL=/tmp/mosquitto.conf
|
|
|
|
mkdir -p "$SHARE" /data/mosquitto
|
|
chown -R mosquitto:mosquitto /data/mosquitto
|
|
|
|
CAFILE=$(jq -r '.cafile' /data/options.json)
|
|
CERTFILE=$(jq -r '.certfile' /data/options.json)
|
|
KEYFILE=$(jq -r '.keyfile' /data/options.json)
|
|
|
|
for f in "$CAFILE" "$CERTFILE" "$KEYFILE"; do
|
|
[ -f "/ssl/$f" ] || { echo "Missing /ssl/$f" >&2; exit 1; }
|
|
done
|
|
|
|
# ---- Defaults in /config (regenerated at every start) ----
|
|
|
|
cat > "$CONFIG/mosquitto.conf" <<EOF
|
|
allow_anonymous false
|
|
password_file $PASSWD
|
|
persistence true
|
|
persistence_location /data/mosquitto/
|
|
|
|
listener 1993
|
|
|
|
listener 9993
|
|
cafile /ssl/$CAFILE
|
|
certfile /ssl/$CERTFILE
|
|
keyfile /ssl/$KEYFILE
|
|
tls_version tlsv1.2
|
|
EOF
|
|
|
|
# Default ACL file: used only if the user's acl file doesn't set acl_file
|
|
cat > "$CONFIG/acl" <<EOF
|
|
acl_file $CONFIG/accesscontrollist
|
|
EOF
|
|
|
|
# Default access control list: grants nothing
|
|
cat > "$CONFIG/accesscontrollist" <<EOF
|
|
# Default ACL: no rules, all access denied
|
|
EOF
|
|
|
|
# ---- User customization in /share (created once, never overwritten) ----
|
|
|
|
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
|
cat > "$SHARE/mosquitto.conf" <<EOF
|
|
# Extra Mosquitto options, appended to the default config
|
|
EOF
|
|
fi
|
|
|
|
if [ ! -f "$SHARE/acl" ]; then
|
|
cat > "$SHARE/acl" <<EOF
|
|
acl_file /share/visquitto/accesscontrollist
|
|
EOF
|
|
fi
|
|
|
|
if [ ! -f "$SHARE/accesscontrollist" ]; then
|
|
cat > "$SHARE/accesscontrollist" <<EOF
|
|
# Visquitto ACL rules, for example:
|
|
# user myuser
|
|
# topic readwrite myprivate/#
|
|
EOF
|
|
fi
|
|
|
|
chown mosquitto:mosquitto "$SHARE/mosquitto.conf" "$SHARE/acl" "$SHARE/accesscontrollist"
|
|
chown mosquitto:mosquitto "$CONFIG/acl" "$CONFIG/accesscontrollist"
|
|
|
|
# ---- Users from the panel ----
|
|
|
|
rm -f "$PASSWD"
|
|
touch "$PASSWD"
|
|
jq -r '.users[] | [.username, .password] | @tsv' /data/options.json |
|
|
while IFS="$(printf '\t')" read -r user pass; do
|
|
mosquitto_passwd -b "$PASSWD" "$user" "$pass"
|
|
done
|
|
chown mosquitto:mosquitto "$PASSWD"
|
|
chmod 600 "$PASSWD"
|
|
|
|
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in" >&2
|
|
|
|
# ---- Build the final config: defaults + user config + ACL ----
|
|
|
|
cat "$CONFIG/mosquitto.conf" > "$FINAL"
|
|
echo >> "$FINAL"
|
|
cat "$SHARE/mosquitto.conf" >> "$FINAL"
|
|
echo >> "$FINAL"
|
|
if grep -q '^[[:space:]]*acl_file' "$SHARE/acl"; then
|
|
cat "$SHARE/acl" >> "$FINAL"
|
|
else
|
|
cat "$CONFIG/acl" >> "$FINAL"
|
|
fi
|
|
|
|
exec mosquitto -c "$FINAL" |