acl, log
- separated ACL's for as plain and TLS - added logging configuration - added timestamp for logs
This commit is contained in:
+92
-38
@@ -1,22 +1,25 @@
|
||||
#!/bin/sh
|
||||
set -e
|
||||
|
||||
# every file the script creates is private from the start
|
||||
umask 077
|
||||
|
||||
CONFIG=/config
|
||||
SHARE=/share/visquitto
|
||||
DATA=/data/mosquitto
|
||||
PASSWD=/data/passwd
|
||||
CERTDIR=/data/certs
|
||||
FINAL=/tmp/mosquitto.conf
|
||||
|
||||
mkdir -p "$SHARE" /data/mosquitto "$CERTDIR"
|
||||
chown -R mosquitto:mosquitto /data/mosquitto
|
||||
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; }
|
||||
@@ -31,49 +34,111 @@ chmod 600 "$CERTDIR/key.pem"
|
||||
|
||||
# ---- Defaults in /config (regenerated at every start) ----
|
||||
cat > "$CONFIG/mosquitto.conf" <<EOF
|
||||
protocol mqtt
|
||||
allow_anonymous false
|
||||
password_file $PASSWD
|
||||
|
||||
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/mosquitto/
|
||||
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
|
||||
|
||||
cat > "$CONFIG/acl.conf" <<EOF
|
||||
acl_file $CONFIG/accesscontrollist
|
||||
# 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
|
||||
|
||||
cat > "$CONFIG/accesscontrollist" <<EOF
|
||||
# Default ACL: no rules, all access denied
|
||||
# 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 Mosquitto options, appended to the default config" > "$SHARE/mosquitto.conf"
|
||||
echo "# Extra global Mosquitto options (loaded with include_dir, before the listeners)" > "$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/#
|
||||
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
|
||||
|
||||
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"
|
||||
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"
|
||||
@@ -83,19 +148,8 @@ while IFS="$(printf '\t')" read -r user pass; do
|
||||
mosquitto_passwd -b "$PASSWD" "$user" "$pass"
|
||||
done
|
||||
chown mosquitto:mosquitto "$PASSWD"
|
||||
chmod 600 "$PASSWD"
|
||||
chmod 0700 "$PASSWD"
|
||||
|
||||
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in" >&2
|
||||
[ -s "$PASSWD" ] || echo "Warning: no users defined, nobody can log in on the plain listener" >&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"
|
||||
exec mosquitto -c "$CONFIG/mosquitto.conf"
|
||||
Reference in New Issue
Block a user