Skip to content

Commit af2eb43

Browse files
committed
sysusers: handle NSS errors gracefully
If the io.systemd.DynamicUser or io.systemd.Machine files exist, but nothing is listening on them, the nss-systemd module returns ECONNREFUSED and systemd-sysusers fails to creat the user/group. This is problematic when ran by packaging scripts, as the package assumes that after this has run, the user/group exist and can be used. adduser does not fail in the same situation. Change sysusers to print a loud warning but otherwise continue when NSS returns an error. (cherry picked from commit fc9938d) (cherry picked from commit abba1e6) (cherry picked from commit 0f51875) (cherry picked from commit dffa62c)
1 parent 0c4605e commit af2eb43

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/sysusers/sysusers.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ static int uid_is_ok(uid_t uid, const char *name, bool check_with_gid) {
989989
if (p)
990990
return 0;
991991
if (!IN_SET(errno, 0, ENOENT))
992-
return -errno;
992+
log_warning_errno(errno, "Unexpected failure while looking up UID '" UID_FMT "' via NSS, assuming it doesn't exist: %m", uid);
993993

994994
if (check_with_gid) {
995995
errno = 0;
@@ -998,7 +998,7 @@ static int uid_is_ok(uid_t uid, const char *name, bool check_with_gid) {
998998
if (!streq(g->gr_name, name))
999999
return 0;
10001000
} else if (!IN_SET(errno, 0, ENOENT))
1001-
return -errno;
1001+
log_warning_errno(errno, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", uid);
10021002
}
10031003
}
10041004

@@ -1103,7 +1103,7 @@ static int add_user(Item *i) {
11031103
return 0;
11041104
}
11051105
if (!errno_is_not_exists(errno))
1106-
return log_error_errno(errno, "Failed to check if user %s already exists: %m", i->name);
1106+
log_warning_errno(errno, "Unexpected failure while looking up user '%s' via NSS, assuming it doesn't exist: %m", i->name);
11071107
}
11081108

11091109
/* Try to use the suggested numeric UID */
@@ -1219,15 +1219,15 @@ static int gid_is_ok(gid_t gid, const char *groupname, bool check_with_uid) {
12191219
if (g)
12201220
return 0;
12211221
if (!IN_SET(errno, 0, ENOENT))
1222-
return -errno;
1222+
log_warning_errno(errno, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", gid);
12231223

12241224
if (check_with_uid) {
12251225
errno = 0;
12261226
p = getpwuid((uid_t) gid);
12271227
if (p)
12281228
return 0;
12291229
if (!IN_SET(errno, 0, ENOENT))
1230-
return -errno;
1230+
log_warning_errno(errno, "Unexpected failure while looking up GID '" GID_FMT "' via NSS, assuming it doesn't exist: %m", gid);
12311231
}
12321232
}
12331233

@@ -1257,7 +1257,7 @@ static int get_gid_by_name(const char *name, gid_t *gid) {
12571257
return 0;
12581258
}
12591259
if (!errno_is_not_exists(errno))
1260-
return log_error_errno(errno, "Failed to check if group %s already exists: %m", name);
1260+
log_warning_errno(errno, "Unexpected failure while looking up group '%s' via NSS, assuming it doesn't exist: %m", name);
12611261
}
12621262

12631263
return -ENOENT;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env bash
2+
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
set -eux
4+
set -o pipefail
5+
6+
# shellcheck source=test/units/util.sh
7+
. "$(dirname "$0")"/util.sh
8+
9+
at_exit() {
10+
set +e
11+
userdel -r foobarbaz
12+
umount /run/systemd/userdb/
13+
}
14+
15+
# Check that we indeed run under root to make the rest of the test work
16+
[[ "$(id -u)" -eq 0 ]]
17+
18+
trap at_exit EXIT
19+
20+
# Ensure that a non-responsive NSS socket doesn't make sysusers fail
21+
mount -t tmpfs tmpfs /run/systemd/userdb/
22+
touch /run/systemd/userdb/io.systemd.DynamicUser
23+
echo 'u foobarbaz' | SYSTEMD_LOG_LEVEL=debug systemd-sysusers -
24+
grep -q foobarbaz /etc/passwd

0 commit comments

Comments
 (0)