From 4e8f73154021a3c8e1fdb23f84bb8edad202b0d8 Mon Sep 17 00:00:00 2001 From: Carsten Mogge Date: Sun, 4 Feb 2024 17:15:36 +0100 Subject: [PATCH] Bugfix for different user than openhabian in zigbee2mqtt-installation (#1836) * Raspberry-Imager Devices tag added Raspberry Imager now needs a devices tag Signed-off-by: Carsten Mogge --- functions/nodejs-apps.bash | 23 +++++++++++++---------- functions/packages.bash | 12 ++++++++---- functions/system.bash | 2 +- includes/zigbee2mqtt/zigbee2mqtt.service | 2 +- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/functions/nodejs-apps.bash b/functions/nodejs-apps.bash index 11cd41666..f8812879c 100644 --- a/functions/nodejs-apps.bash +++ b/functions/nodejs-apps.bash @@ -41,9 +41,9 @@ nodejs_setup() { echo -n "$(timestamp) [openHABian] Installing NodeJS... " if [[ -n $PREOFFLINE ]]; then - if cond_redirect apt-get --quiet install --download-only --yes nodejs; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect apt-get --quiet install --download-only --yes nodejs npm; then echo "OK"; else echo "FAILED"; return 1; fi else - if cond_redirect apt-get install --yes -o DPkg::Lock::Timeout="$APTTIMEOUT" nodejs; then echo "OK"; else echo "FAILED"; return 1; fi + if cond_redirect apt-get install --yes -o DPkg::Lock::Timeout="$APTTIMEOUT" nodejs npm; then echo "OK"; else echo "FAILED"; return 1; fi fi fi } @@ -214,8 +214,7 @@ nodered_setup() { zigbee2mqtt_download() { echo -n "$(timestamp) [openHABian] Downloading Zigbee2MQTT... " if ! cond_redirect mkdir -p /opt/zigbee2mqtt; then echo "FAILED (mkdir -p /opt/zigbee2mqtt)"; fi - if ! cond_redirect chown openhabian /opt/zigbee2mqtt; then echo "FAILED (chown /opt/zigbee2mqtt)"; fi - if ! cond_redirect chgrp openhab /opt/zigbee2mqtt; then echo "FAILED (chgrp /opt/zigbee2mqtt)"; fi + if ! cond_redirect chown "${username:-openhabian}:openhab" /opt/zigbee2mqtt; then echo "FAILED (chown /opt/zigbee2mqtt)"; fi if ! cond_redirect sudo -u "${username:-openhabian}" git clone https://github.com/Koenkk/zigbee2mqtt.git "/opt/zigbee2mqtt"; then echo "FAILED (git clone)"; return 1; fi } @@ -234,7 +233,7 @@ zigbee2mqtt_setup() { local mqttPWText="\\nIf your MQTT-server requires a password, please enter it here:" local my_adapters local by_path_or_id - local mqttDefaultUser="openhabian" + local mqttDefaultUser="${username:-openhabian}" local mqttUser local serverIP local installSuccessText @@ -250,7 +249,7 @@ zigbee2mqtt_setup() { if ! (whiptail --title "Zigbee2MQTT Uninstall" --yes-button "Continue" --no-button "Cancel" --yesno "$uninstallText" 7 80); then echo "CANCELED"; return 0; fi fi echo -n "$(timestamp) [openHABian] Removing Zigbee2MQTT service... " - if ! cond_redirect systemctl stop zigbee2mqtt.service; then echo "FAILED (disable service)"; return 1; fi + systemctl stop zigbee2mqtt.service if ! rm -f /etc/systemd/system/zigbee2mqtt.service; then echo "FAILED (remove service)"; return 1; fi if cond_redirect systemctl -q daemon-reload; then echo "OK"; else echo "FAILED (daemon-reload)"; return 1; fi @@ -331,14 +330,15 @@ zigbee2mqtt_setup() { echo -n "$(timestamp) [openHABian] Creating log directory... " mkdir -p /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1) - chown openhabian /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1) - chgrp openhab /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1) + chown "${username:-openhabian}:openhab" /var/log/zigbee2mqtt || (echo "FAILED (create log-directory)"; return 1) echo "OK" echo -n "$(timestamp) [openHABian] Zigbee2MQTT install & config... " cd /opt/zigbee2mqtt || (echo "FAILED (cd)"; return 1) if ! cond_redirect sudo -u "${username:-openhabian}" npm ci ; then echo "FAILED (npm ci)"; return 1; fi - sed -e "s|%adapter|$by_path_or_id/$selectedAdapter|g" /opt/openhabian/includes/zigbee2mqtt/configuration.yaml | sudo -u "${username:-openhabian}" dd status=none of=/opt/zigbee2mqtt/data/configuration.yaml + + if ! cond_redirect install -o "${username:-openhabian}" -g openhab -m 644 "${BASEDIR:-/opt/openhabian}/includes/zigbee2mqtt/configuration.yaml" /opt/zigbee2mqtt/data/; then echo "FAILED (install configuration.yaml)"; return 1; fi + sed -i -e "s|%adapter|$by_path_or_id/$selectedAdapter|g" /opt/zigbee2mqtt/data/configuration.yaml sed -i -e "s|%user%|$mqttUser|g" /opt/zigbee2mqtt/data/configuration.yaml sed -i -e "s|%password%|$mqttPW|g" /opt/zigbee2mqtt/data/configuration.yaml @@ -346,7 +346,10 @@ zigbee2mqtt_setup() { echo "OK" echo -n "$(timestamp) [openHABian] Setting up Zigbee2MQTT service... " - if ! cond_redirect install -m 644 "${BASEDIR:-/opt/openhabian}"/includes/zigbee2mqtt/zigbee2mqtt.service /etc/systemd/system/; then echo "FAILED (install service)"; return 1; fi + + if ! cond_redirect install -o "${username:-openhabian}" -g openhab -m 644 "${BASEDIR:-/opt/openhabian}/includes/zigbee2mqtt/zigbee2mqtt.service" /etc/systemd/system/; then echo "FAILED (install service)"; return 1; fi + sed -i -e "s|%user%|${username:-openhabian}|g" "/etc/systemd/system/zigbee2mqtt.service" + if ! cond_redirect systemctl -q daemon-reload; then echo "FAILED (daemon-reload)"; return 1; fi if ! cond_redirect systemctl enable --now zigbee2mqtt.service; then echo "FAILED (enable service)"; return 1; fi echo "OK" diff --git a/functions/packages.bash b/functions/packages.bash index 848e6365b..5c7b7f9c2 100644 --- a/functions/packages.bash +++ b/functions/packages.bash @@ -267,9 +267,12 @@ mqtt_setup() { local mosquittoConf="/etc/mosquitto/mosquitto.conf" local mosquittoPasswd="/etc/mosquitto/passwd" local mqttPasswd - local mqttUser="openhabian" - local introText="The MQTT broker Eclipse Mosquitto will be installed from the official repository.\\n\\nIn addition, you can activate username:password authentication." - local questionText="\\nDo you want to secure your MQTT broker by a username:password combination? Every client will need to provide these upon connection.\\n\\nUsername will be '${mqttUser}', please provide a password (consisting of ASCII printable characters except space). Leave blank for no authentication, run setup again to change." + local mqttUser + local mqttDefaultUser="openhabian" + local introText="\\nThe MQTT broker Eclipse Mosquitto will be installed from the official repository." + local mqttUserText="\\nSecure your MQTT broker by a username:password combination. Every client will need to provide these upon connection.\\nPlease enter your MQTT-User (default = openhabian):" + local mqttPasswordText="\\nPlease provide a password (consisting of ASCII printable characters except space). Run setup again to change." + local successText="Setup was successful.\\n\\nEclipse Mosquitto is now up and running in the background. You should be able to make a first connection.\\n\\nTo continue your integration in openHAB, please follow the instructions under: https://www.openhab.org/addons/bindings/mqtt/" echo -n "$(timestamp) [openHABian] Beginning the MQTT broker Eclipse Mosquitto installation... " @@ -287,7 +290,8 @@ mqtt_setup() { echo -n "$(timestamp) [openHABian] Configuring MQTT... " if [[ -n $INTERACTIVE ]]; then - if ! mqttPasswd="$(whiptail --title "MQTT Authentication" --passwordbox "$questionText" 14 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi + if ! mqttUser=$(whiptail --title "MQTT User" --inputbox "$mqttUserText" 10 80 "$mqttDefaultUser" 3>&1 1>&2 2>&3); then return 0; fi + if ! mqttPasswd="$(whiptail --title "MQTT Authentication" --passwordbox "$mqttPasswordText" 14 80 3>&1 1>&2 2>&3)"; then echo "CANCELED"; return 0; fi fi if ! grep -qs "listener" ${mosquittoConf}; then printf "\\n\\nlistener 1883" >> ${mosquittoConf} diff --git a/functions/system.bash b/functions/system.bash index 7301c8770..b90dafd87 100644 --- a/functions/system.bash +++ b/functions/system.bash @@ -336,7 +336,7 @@ permissions_corrections() { if ! cond_redirect fix_permissions /var/log/mosquitto "mosquitto:${username:-openhabian}" 644 755; then echo "FAILED (mosquitto log permissions)"; retval=1; fi fi if zigbee2mqtt_is_installed; then - if ! cond_redirect fix_permissions /var/log/zigbee2mqtt "${username:-openhabian}:${username:-openhabian}" 644 755; then echo "FAILED (zigbee2mqtt log permissions)"; retval=1; fi + if ! cond_redirect fix_permissions /var/log/zigbee2mqtt "${username:-openhabian}:openhab" 644 755; then echo "FAILED (zigbee2mqtt log permissions)"; retval=1; fi fi if zram_is_installed; then if influxdb_is_installed; then diff --git a/includes/zigbee2mqtt/zigbee2mqtt.service b/includes/zigbee2mqtt/zigbee2mqtt.service index 2bcd33b3e..1e78d9914 100644 --- a/includes/zigbee2mqtt/zigbee2mqtt.service +++ b/includes/zigbee2mqtt/zigbee2mqtt.service @@ -9,7 +9,7 @@ StandardOutput=inherit # Or use StandardOutput=null if you don't want Zigbee2MQTT messages filling syslog, for more options see systemd.exec(5) StandardError=inherit Restart=always -User=openhabian +User=%user% [Install] WantedBy=multi-user.target