Skip to content

Commit c1a4f81

Browse files
committed
ensure the user/group exists
1 parent 12d1572 commit c1a4f81

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

debian/cnb-cache-mysql.postinst

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,68 @@ case "$1" in
1111
configure)
1212

1313

14+
# Sane defaults:
15+
16+
[ -z "$SERVER_HOME" ] && SERVER_HOME=/var/lib/cnb-cache
17+
[ -z "$SERVER_USER" ] && SERVER_USER=cnb-cache
18+
[ -z "$SERVER_NAME" ] && SERVER_NAME="cnb-cache"
19+
[ -z "$SERVER_GROUP" ] && SERVER_GROUP=cnb-cache
20+
21+
# Groups that the user will be added to, if undefined, then none.
22+
ADDGROUP="cnb-cache"
23+
24+
# create user to avoid running server as root
25+
# 1. create group if not existing
26+
if ! getent group | grep -q "^$SERVER_GROUP:"; then
27+
echo -n "Adding group $SERVER_GROUP.."
28+
addgroup --quiet --system "$SERVER_GROUP" 2>/dev/null || true
29+
echo "..done"
30+
fi
31+
# 2. create homedir if not existing
32+
test -d "$SERVER_HOME" || mkdir "$SERVER_HOME"
33+
# 3. create user if not existing
34+
if ! getent passwd | grep -q "^$SERVER_USER:"; then
35+
echo -n "Adding system user $SERVER_USER.."
36+
adduser --quiet \
37+
--system \
38+
--ingroup "$SERVER_GROUP" \
39+
--home /var/lib/cnb-cache \
40+
--disabled-password \
41+
"$SERVER_USER" 2>/dev/null || true
42+
echo "..done"
43+
fi
44+
# 4. adjust passwd entry
45+
usermod -c "$SERVER_NAME" \
46+
-d "$SERVER_HOME" \
47+
-g "$SERVER_GROUP" \
48+
"$SERVER_USER"
49+
# 5. adjust file and directory permissions
50+
if ! dpkg-statoverride --list "$SERVER_HOME" >/dev/null; then
51+
chown -R "$SERVER_USER":adm "$SERVER_HOME"
52+
chmod u=rwx,g=rxs,o= "$SERVER_HOME"
53+
fi
54+
# 6. Add the user to the ADDGROUP group
55+
if test -n $ADDGROUP; then
56+
if ! groups "$SERVER_USER" | cut -d: -f2 |
57+
grep -qw $ADDGROUP; then
58+
adduser "$SERVER_USER" $ADDGROUP
59+
fi
60+
fi
61+
62+
63+
1464
if [ -f /usr/share/dbconfig-common/dpkg/postinst.mysql ]; then
1565
. /usr/share/dbconfig-common/dpkg/postinst.mysql
1666
# shellcheck disable=SC2034
1767
dbc_generate_include_args="-U -o template_infile=/usr/lib/cnb-cache/.env.template"
1868
# shellcheck disable=SC2034
1969
dbc_generate_include=template:/etc/cnb-cache/cnb-cache.env
2070
# shellcheck disable=SC2034
21-
dbc_generate_include_owner="root:www-data"
71+
dbc_generate_include_owner="root:cnb-cache"
2272
# shellcheck disable=SC2034
2373
dbc_generate_include_perms="664"
2474
# shellcheck disable=SC2034
25-
dbc_dbfile_owner="www-data:www-data"
75+
dbc_dbfile_owner="cnb-cache:cnb-cache"
2676
# shellcheck disable=SC2034
2777
dbc_dbfile_perms="0664"
2878
# shellcheck disable=SC2034
@@ -39,7 +89,7 @@ case "$1" in
3989
phinx migrate -c /usr/lib/cnb-cache/phinx-adapter.php
4090

4191
if [ -f /var/lib/cnb-cache/cnb-cache ]; then
42-
chown root:www-data /var/lib/cnb-cache
92+
chown root:cnb-cache /var/lib/cnb-cache
4393
chmod ug+rw /var/lib/cnb-cache
4494
fi
4595

debian/cnb-cache-sqlite.postinst

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,56 @@ configure)
1212
. /usr/share/debconf/confmodule
1313
. /usr/share/dbconfig-common/dpkg/postinst.sqlite3
1414

15+
16+
# Sane defaults:
17+
18+
[ -z "$SERVER_HOME" ] && SERVER_HOME=/var/lib/cnb-cache
19+
[ -z "$SERVER_USER" ] && SERVER_USER=cnb-cache
20+
[ -z "$SERVER_NAME" ] && SERVER_NAME="cnb-cache"
21+
[ -z "$SERVER_GROUP" ] && SERVER_GROUP=cnb-cache
22+
23+
# Groups that the user will be added to, if undefined, then none.
24+
ADDGROUP="cnb-cache"
25+
26+
# create user to avoid running server as root
27+
# 1. create group if not existing
28+
if ! getent group | grep -q "^$SERVER_GROUP:"; then
29+
echo -n "Adding group $SERVER_GROUP.."
30+
addgroup --quiet --system "$SERVER_GROUP" 2>/dev/null || true
31+
echo "..done"
32+
fi
33+
# 2. create homedir if not existing
34+
test -d "$SERVER_HOME" || mkdir "$SERVER_HOME"
35+
# 3. create user if not existing
36+
if ! getent passwd | grep -q "^$SERVER_USER:"; then
37+
echo -n "Adding system user $SERVER_USER.."
38+
adduser --quiet \
39+
--system \
40+
--ingroup "$SERVER_GROUP" \
41+
--home /var/lib/cnb-cache \
42+
--disabled-password \
43+
"$SERVER_USER" 2>/dev/null || true
44+
echo "..done"
45+
fi
46+
# 4. adjust passwd entry
47+
usermod -c "$SERVER_NAME" \
48+
-d "$SERVER_HOME" \
49+
-g "$SERVER_GROUP" \
50+
"$SERVER_USER"
51+
# 5. adjust file and directory permissions
52+
if ! dpkg-statoverride --list "$SERVER_HOME" >/dev/null; then
53+
chown -R "$SERVER_USER":adm "$SERVER_HOME"
54+
chmod u=rwx,g=rxs,o= "$SERVER_HOME"
55+
fi
56+
# 6. Add the user to the ADDGROUP group
57+
if test -n $ADDGROUP; then
58+
if ! groups "$SERVER_USER" | cut -d: -f2 |
59+
grep -qw $ADDGROUP; then
60+
adduser "$SERVER_USER" $ADDGROUP
61+
fi
62+
fi
63+
64+
1565
# shellcheck disable=SC2034
1666
dbc_generate_include_args="-U -o template_infile=/usr/lib/cnb-cache/.env.template"
1767
# shellcheck disable=SC2034

0 commit comments

Comments
 (0)