bea370dca2
- container listener port changed to 1883 and 8883 - acl file renamed to acl.conf
101 lines
2.8 KiB
Bash
101 lines
2.8 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
|
|
# every file the script creates is private from the start
|
|
umask 077
|
|
|
|
CONFIG=/config
|
|
SHARE=/share/visquitto
|
|
PASSWD=/data/passwd
|
|
CERTDIR=/data/certs
|
|
FINAL=/tmp/mosquitto.conf
|
|
|
|
mkdir -p "$SHARE" /data/mosquitto "$CERTDIR"
|
|
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)
|
|
|
|
# ---- Certificates: copied at every start, readable by mosquitto ----
|
|
for f in "$CAFILE" "$CERTFILE" "$KEYFILE"; do
|
|
[ -f "/ssl/$f" ] || { echo "Missing /ssl/$f" >&2; exit 1; }
|
|
done
|
|
|
|
cp "/ssl/$CAFILE" "$CERTDIR/ca.pem"
|
|
cp "/ssl/$CERTFILE" "$CERTDIR/cert.pem"
|
|
cp "/ssl/$KEYFILE" "$CERTDIR/key.pem"
|
|
chown -R mosquitto:mosquitto "$CERTDIR"
|
|
chmod 644 "$CERTDIR/ca.pem" "$CERTDIR/cert.pem"
|
|
chmod 600 "$CERTDIR/key.pem"
|
|
|
|
# ---- 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 1883
|
|
|
|
listener 8883
|
|
cafile $CERTDIR/ca.pem
|
|
certfile $CERTDIR/cert.pem
|
|
keyfile $CERTDIR/key.pem
|
|
tls_version tlsv1.2
|
|
EOF
|
|
|
|
cat > "$CONFIG/acl.conf" <<EOF
|
|
acl_file $CONFIG/accesscontrollist
|
|
EOF
|
|
|
|
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
|
|
echo "# Extra Mosquitto options, appended to the default config" > "$SHARE/mosquitto.conf"
|
|
fi
|
|
|
|
if [ ! -f "$SHARE/acl.conf" ]; then
|
|
echo "acl_file /share/visquitto/accesscontrollist" > "$SHARE/acl.conf"
|
|
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.conf" "$SHARE/accesscontrollist" \
|
|
"$CONFIG/acl.conf" "$CONFIG/accesscontrollist"
|
|
chmod 600 "$SHARE/acl.conf" "$SHARE/accesscontrollist" "$CONFIG/acl.conf" "$CONFIG/accesscontrollist"
|
|
chmod 644 "$SHARE/mosquitto.conf"
|
|
|
|
# ---- 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
|
|
|
|
# ---- 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.conf"; then
|
|
cat "$SHARE/acl.conf" >> "$FINAL"
|
|
else
|
|
cat "$CONFIG/acl.conf" >> "$FINAL"
|
|
fi
|
|
|
|
exec mosquitto -c "$FINAL" |