edb2a6df1d
- separated ACL's for as plain and TLS - added logging configuration - added timestamp for logs
155 lines
4.3 KiB
Bash
155 lines
4.3 KiB
Bash
#!/bin/sh
|
|
set -e
|
|
umask 077
|
|
|
|
CONFIG=/config
|
|
SHARE=/share/visquitto
|
|
DATA=/data/mosquitto
|
|
PASSWD=/data/passwd
|
|
CERTDIR=/data/certs
|
|
|
|
mkdir -p "$SHARE" "$DATA" "$CERTDIR"
|
|
chown -R mosquitto:mosquitto "$DATA"
|
|
|
|
CAFILE=$(jq -r '.cafile' /data/options.json)
|
|
CERTFILE=$(jq -r '.certfile' /data/options.json)
|
|
KEYFILE=$(jq -r '.keyfile' /data/options.json)
|
|
|
|
# ---- Check plugins exist ----
|
|
for p in /usr/lib/mosquitto_acl_file.so /usr/lib/mosquitto_password_file.so; do
|
|
[ -f "$p" ] || { echo "Missing plugin $p" >&2; exit 1; }
|
|
done
|
|
|
|
# ---- 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
|
|
protocol mqtt
|
|
allow_anonymous false
|
|
|
|
log_dest stdout
|
|
log_type error
|
|
log_type warning
|
|
log_type notice
|
|
log_timestamp_format %Y-%m-%d %H:%M:%S
|
|
|
|
persistence true
|
|
persistence_location $DATA/
|
|
max_queued_messages 8192
|
|
|
|
plugin_load acl_file /usr/lib/mosquitto_acl_file.so
|
|
plugin_load password_file /usr/lib/mosquitto_password_file.so
|
|
|
|
include_dir $SHARE
|
|
|
|
# Plain listener (host port 1993): password users only
|
|
listener 1883
|
|
protocol mqtt
|
|
plugin_use password_file
|
|
plugin_opt_password_file $PASSWD
|
|
plugin_use acl_file
|
|
plugin_opt_acl_file $DATA/acl_plain
|
|
|
|
# TLS listener (host port 9993): client certificate required
|
|
listener 8883
|
|
protocol mqtt
|
|
cafile $CERTDIR/ca.pem
|
|
certfile $CERTDIR/cert.pem
|
|
keyfile $CERTDIR/key.pem
|
|
tls_version tlsv1.2
|
|
require_certificate true
|
|
use_identity_as_username true
|
|
plugin_use password_file
|
|
plugin_opt_password_file $PASSWD
|
|
plugin_use acl_file
|
|
plugin_opt_acl_file $DATA/acl_tls
|
|
EOF
|
|
|
|
# plain ACL
|
|
echo "# Default ACL for the plain listener: no rules, all access denied" > "$CONFIG/acl_plain"
|
|
cat > "$CONFIG/acl_plain" <<EOF
|
|
# ============================================================
|
|
# Not certified devices
|
|
# ============================================================
|
|
user hamqtt
|
|
|
|
# Test topic
|
|
topic readwrite test
|
|
# Initial enrollment
|
|
topic write cert/enroll/+/request
|
|
topic read cert/enroll/+/issued
|
|
|
|
# ============================================================
|
|
# Certificate manager
|
|
# ============================================================
|
|
user viracert
|
|
|
|
topic read cert/enroll/+/request
|
|
topic write cert/enroll/+/issued
|
|
topic read cert/enroll/+/renew
|
|
EOF
|
|
|
|
# TLS ACL
|
|
echo "# Default ACL for the TLS listener: no rules, all access denied" > "$CONFIG/acl_tls"
|
|
cat > "$CONFIG/acl_tls" <<EOF
|
|
# Default rule for every certificate client. %u is the certificate CN.
|
|
pattern readwrite clients/%u/#
|
|
EOF
|
|
|
|
# ---- User customization in /share (created once, never overwritten) ----
|
|
if [ ! -f "$SHARE/mosquitto.conf" ]; then
|
|
echo "# Extra global Mosquitto options (loaded with include_dir, before the listeners)" > "$SHARE/mosquitto.conf"
|
|
fi
|
|
|
|
if [ ! -f "$SHARE/acl_plain" ]; then
|
|
cat > "$SHARE/acl_plain" <<EOF
|
|
# Extra ACL rules for the plain listener, appended to the default, for example:
|
|
# user enroll
|
|
# topic write certs/request/#
|
|
# topic read certs/response/#
|
|
EOF
|
|
fi
|
|
|
|
if [ ! -f "$SHARE/acl_tls" ]; then
|
|
cat > "$SHARE/acl_tls" <<EOF
|
|
# Extra ACL rules for the TLS listener (user = certificate CN), appended to the default, for example:
|
|
# user hamt
|
|
# topic read broadcast/#
|
|
EOF
|
|
fi
|
|
|
|
chmod 644 "$SHARE/mosquitto.conf"
|
|
chmod 600 "$SHARE/acl_plain" "$SHARE/acl_tls"
|
|
|
|
# ---- Merge defaults + user ACLs into the files Mosquitto reads ----
|
|
{ cat "$CONFIG/acl_plain"; echo; cat "$SHARE/acl_plain"; echo; } > "$DATA/acl_plain"
|
|
{ cat "$CONFIG/acl_tls"; echo; cat "$SHARE/acl_tls"; echo; } > "$DATA/acl_tls"
|
|
|
|
for f in "$DATA/acl_plain" "$DATA/acl_tls"; do
|
|
chown mosquitto:mosquitto "$f"
|
|
chmod 600 "$f"
|
|
done
|
|
|
|
# ---- 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 0700 "$PASSWD"
|
|
|
|
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in on the plain listener" >&2
|
|
|
|
exec mosquitto -c "$CONFIG/mosquitto.conf" |