Skip to content

Commit dd81a6f

Browse files
authored
Merge pull request #13 from Al-Muhandis/codex/configure-tgadmin.service-for-non-root-user
Configure tgadmin service account during .deb installation
2 parents 6cde727 + 122bc7d commit dd81a6f

5 files changed

Lines changed: 94 additions & 2 deletions

File tree

debian/DEBIAN/conffiles

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/etc/tgadmin/tgadmin.json
2+
/etc/default/tgadmin

debian/DEBIAN/postinst

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ set -euo pipefail
44
PACKAGE_NAME="tgadmin"
55
CONFIG_FILE="/etc/${PACKAGE_NAME}/tgadmin.json"
66
SCHEMA_FILE="/usr/share/${PACKAGE_NAME}/db_schema.sql"
7+
DEFAULTS_FILE="/etc/default/${PACKAGE_NAME}"
8+
SYSTEMD_DROPIN_DIR="/etc/systemd/system/${PACKAGE_NAME}.service.d"
9+
SYSTEMD_DROPIN_FILE="${SYSTEMD_DROPIN_DIR}/10-run-as.conf"
710

811
DB_NAME="tgadmin"
912
DB_USER="tgadmin"
@@ -24,6 +27,63 @@ json_set() {
2427
jq "$1" "${CONFIG_FILE}" > "${tmp}" && mv "${tmp}" "${CONFIG_FILE}"
2528
}
2629

30+
resolve_service_identity() {
31+
SERVICE_USER="${PACKAGE_NAME}"
32+
SERVICE_GROUP="${PACKAGE_NAME}"
33+
34+
if [ -f "${DEFAULTS_FILE}" ]; then
35+
# shellcheck disable=SC1090
36+
. "${DEFAULTS_FILE}"
37+
38+
if [ -n "${TGADMIN_SERVICE_USER:-}" ]; then
39+
SERVICE_USER="${TGADMIN_SERVICE_USER}"
40+
fi
41+
if [ -n "${TGADMIN_SERVICE_GROUP:-}" ]; then
42+
SERVICE_GROUP="${TGADMIN_SERVICE_GROUP}"
43+
fi
44+
fi
45+
46+
if [ -z "${SERVICE_USER}" ] || [ -z "${SERVICE_GROUP}" ]; then
47+
echo "ERROR: TGADMIN_SERVICE_USER and TGADMIN_SERVICE_GROUP must be non-empty." >&2
48+
exit 1
49+
fi
50+
}
51+
52+
ensure_service_account() {
53+
if ! getent group "${SERVICE_GROUP}" >/dev/null; then
54+
groupadd --system "${SERVICE_GROUP}"
55+
fi
56+
57+
if ! id -u "${SERVICE_USER}" >/dev/null 2>&1; then
58+
useradd \
59+
--system \
60+
--gid "${SERVICE_GROUP}" \
61+
--no-create-home \
62+
--home-dir /nonexistent \
63+
--shell /usr/sbin/nologin \
64+
"${SERVICE_USER}"
65+
fi
66+
}
67+
68+
configure_systemd_service_user() {
69+
if ! command -v systemctl >/dev/null 2>&1; then
70+
return
71+
fi
72+
73+
if ! systemctl list-unit-files "${PACKAGE_NAME}.service" >/dev/null 2>&1; then
74+
return
75+
fi
76+
77+
mkdir -p "${SYSTEMD_DROPIN_DIR}"
78+
cat > "${SYSTEMD_DROPIN_FILE}" <<DROPIN
79+
[Service]
80+
User=${SERVICE_USER}
81+
Group=${SERVICE_GROUP}
82+
DROPIN
83+
84+
systemctl daemon-reload || true
85+
}
86+
2787
case "$1" in
2888
configure)
2989
# ----------------------------------------------------------------
@@ -43,6 +103,10 @@ case "$1" in
43103
exit 1
44104
fi
45105

106+
resolve_service_identity
107+
ensure_service_account
108+
configure_systemd_service_user
109+
46110
# ----------------------------------------------------------------
47111
# Data base
48112
# ----------------------------------------------------------------
@@ -83,8 +147,8 @@ SQL
83147
echo " Install it, then run: sudo dpkg-reconfigure ${PACKAGE_NAME}" >&2
84148
fi
85149

86-
# Configuration rights — only root and the tgadmin group
87-
chown root:${PACKAGE_NAME} "${CONFIG_FILE}" 2>/dev/null || chown root:root "${CONFIG_FILE}"
150+
# Configuration rights — only root and the service group
151+
chown "root:${SERVICE_GROUP}" "${CONFIG_FILE}" 2>/dev/null || chown root:root "${CONFIG_FILE}"
88152
chmod 0640 "${CONFIG_FILE}"
89153
;;
90154
esac

debian/DEBIAN/postrm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ FLUSH PRIVILEGES;
2323
SQL
2424
fi
2525
rm -f "${CONFIG_FILE}"
26+
rm -f "/etc/systemd/system/${PACKAGE_NAME}.service.d/10-run-as.conf"
27+
rmdir --ignore-fail-on-non-empty "/etc/systemd/system/${PACKAGE_NAME}.service.d"
2628
rmdir --ignore-fail-on-non-empty "/etc/${PACKAGE_NAME}"
2729
;;
2830
remove)

debian/etc/default/tgadmin

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Service identity used by postinst when configuring tgadmin.service.
2+
#
3+
# By default package runs daemon as the system account "tgadmin".
4+
# Override only if you need a different existing or system account.
5+
#
6+
# Example:
7+
# TGADMIN_SERVICE_USER=mybot
8+
# TGADMIN_SERVICE_GROUP=mybot
9+
10+
TGADMIN_SERVICE_USER=tgadmin
11+
TGADMIN_SERVICE_GROUP=tgadmin
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
Description=TGAdmin Telegram moderation daemon
3+
After=network-online.target mariadb.service mysql.service
4+
Wants=network-online.target
5+
6+
[Service]
7+
Type=simple
8+
ExecStart=/usr/bin/adminhelperd
9+
Restart=on-failure
10+
RestartSec=5
11+
NoNewPrivileges=true
12+
13+
[Install]
14+
WantedBy=multi-user.target

0 commit comments

Comments
 (0)