Skip to content

Commit

Permalink
sysusers: add minimal tool to reconstruct /etc/passwd and /etc/group …
Browse files Browse the repository at this point in the history
…from static files

systemd-sysusers is a tool to reconstruct /etc/passwd and /etc/group
from static definition files that take a lot of inspiration from
tmpfiles snippets. These snippets should carry information about system
users only. To make sure it is not misused for normal users these
snippets only allow configuring UID and gecos field for each user, but
do not allow configuration of the home directory or shell, which is
necessary for real login users.

The purpose of this tool is to enable state-less systems that can
populate /etc with the minimal files necessary, solely from static data
in /usr. systemd-sysuser is additive only, and will never override
existing users.

This tool will create these files directly, and not via some user
database abtsraction layer. This is appropriate as this tool is supposed
to run really early at boot, and is only useful for creating system
users, and system users cannot be stored in remote databases anyway.

The tool is also useful to be invoked from RPM scriptlets, instead of
useradd. This allows moving from imperative user descriptions in RPM to
declarative descriptions.

The UID/GID for a user/group to be created can either be chosen dynamic,
or fixed, or be read from the owner of a file in the file system, in
order to support reconstructing the correct IDs for files that shall be
owned by them.

This also adds a minimal user definition file, that should be
sufficient for most basic systems. Distributions are expected to patch
these files and augment the contents, for example with fixed UIDs for
the users where that's necessary.
  • Loading branch information
poettering committed Jun 12, 2014
1 parent 0138a2d commit 1b99214
Show file tree
Hide file tree
Showing 11 changed files with 1,476 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -101,6 +101,7 @@
/systemd-socket-proxyd
/systemd-sysctl
/systemd-system-update-generator
/systemd-sysusers
/systemd-sysv-generator
/systemd-timedated
/systemd-timesyncd
Expand Down
24 changes: 24 additions & 0 deletions Makefile.am
Expand Up @@ -88,6 +88,7 @@ pkgsysconfdir=$(sysconfdir)/systemd
userunitdir=$(prefix)/lib/systemd/user
userpresetdir=$(prefix)/lib/systemd/user-preset
tmpfilesdir=$(prefix)/lib/tmpfiles.d
sysusersdir=$(prefix)/lib/sysusers.d
sysctldir=$(prefix)/lib/sysctl.d
networkdir=$(rootprefix)/lib/systemd/network
pkgincludedir=$(includedir)/systemd
Expand Down Expand Up @@ -1756,6 +1757,28 @@ EXTRA_DIST += \
units/systemd-tmpfiles-setup.service.in \
units/systemd-tmpfiles-clean.service.in

# ------------------------------------------------------------------------------
if ENABLE_SYSUSERS
systemd_sysusers_SOURCES = \
src/sysusers/sysusers.c

systemd_sysusers_LDADD = \
libsystemd-units.la \
libsystemd-label.la \
libsystemd-capability.la \
libsystemd-internal.la \
libsystemd-shared.la

rootbin_PROGRAMS += \
systemd-sysusers

dist_sysusers_DATA = \
sysusers.d/systemd.conf

INSTALL_DIRS += \
$(sysusersdir)
endif

# ------------------------------------------------------------------------------
systemd_machine_id_setup_SOURCES = \
src/machine-id-setup/machine-id-setup-main.c \
Expand Down Expand Up @@ -4879,6 +4902,7 @@ substitutions = \
'|udevrulesdir=$(udevrulesdir)|' \
'|catalogdir=$(catalogdir)|' \
'|tmpfilesdir=$(tmpfilesdir)|' \
'|sysusersdir=$(sysusersdir)|' \
'|sysctldir=$(sysctldir)|' \
'|systemgeneratordir=$(systemgeneratordir)|' \
'|usergeneratordir=$(usergeneratordir)|' \
Expand Down
9 changes: 9 additions & 0 deletions configure.ac
Expand Up @@ -722,6 +722,14 @@ if test "x$enable_tmpfiles" != "xno"; then
fi
AM_CONDITIONAL(ENABLE_TMPFILES, [test "$have_tmpfiles" = "yes"])

# ------------------------------------------------------------------------------
have_sysusers=no
AC_ARG_ENABLE(sysusers, AS_HELP_STRING([--disable-sysusers], [disable sysusers support]))
if test "x$enable_sysusers" != "xno"; then
have_sysusers=yes
fi
AM_CONDITIONAL(ENABLE_SYSUSERS, [test "$have_sysusers" = "yes"])

# ------------------------------------------------------------------------------
have_randomseed=no
AC_ARG_ENABLE(randomseed, AS_HELP_STRING([--disable-randomseed], [disable randomseed tools]))
Expand Down Expand Up @@ -1166,6 +1174,7 @@ AC_MSG_RESULT([
bootchart: ${have_bootchart}
quotacheck: ${have_quotacheck}
tmpfiles: ${have_tmpfiles}
sysusers: ${have_sysusers}
randomseed: ${have_randomseed}
backlight: ${have_backlight}
rfkill: ${have_rfkill}
Expand Down
6 changes: 3 additions & 3 deletions src/shared/copy.c
Expand Up @@ -22,7 +22,7 @@
#include "util.h"
#include "copy.h"

static int stream_bytes(int fdf, int fdt) {
int copy_bytes(int fdf, int fdt) {
assert(fdf >= 0);
assert(fdt >= 0);

Expand Down Expand Up @@ -92,7 +92,7 @@ static int fd_copy_regular(int df, const char *from, const struct stat *st, int
return -errno;
}

r = stream_bytes(fdf, fdt);
r = copy_bytes(fdf, fdt);
if (r < 0) {
unlinkat(dt, to, 0);
return r;
Expand Down Expand Up @@ -273,7 +273,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode) {
if (fdt < 0)
return -errno;

r = stream_bytes(fdf, fdt);
r = copy_bytes(fdf, fdt);
if (r < 0) {
unlink(to);
return r;
Expand Down
1 change: 1 addition & 0 deletions src/shared/copy.h
Expand Up @@ -23,3 +23,4 @@

int copy_file(const char *from, const char *to, int flags, mode_t mode);
int copy_tree(const char *from, const char *to);
int copy_bytes(int fdf, int fdt);
21 changes: 12 additions & 9 deletions src/shared/util.c
Expand Up @@ -4007,24 +4007,16 @@ int fd_wait_for_event(int fd, int event, usec_t t) {
int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
FILE *f;
char *t;
const char *fn;
size_t k;
int fd;

assert(path);
assert(_f);
assert(_temp_path);

t = new(char, strlen(path) + 1 + 6 + 1);
t = strappend(path, ".XXXXXX");
if (!t)
return -ENOMEM;

fn = basename(path);
k = fn - path;
memcpy(t, path, k);
t[k] = '.';
stpcpy(stpcpy(t+k+1, fn), "XXXXXX");

fd = mkostemp_safe(t, O_WRONLY|O_CLOEXEC);
if (fd < 0) {
free(t);
Expand Down Expand Up @@ -6665,3 +6657,14 @@ int bind_remount_recursive(const char *prefix, bool ro) {
}
}
}

int fflush_and_check(FILE *f) {

errno = 0;
fflush(f);

if (ferror(f))
return errno ? -errno : -EIO;

return 0;
}
2 changes: 2 additions & 0 deletions src/shared/util.h
Expand Up @@ -946,3 +946,5 @@ int update_reboot_param_file(const char *param);
int umount_recursive(const char *target, int flags);

int bind_remount_recursive(const char *prefix, bool ro);

int fflush_and_check(FILE *f);
1 change: 1 addition & 0 deletions src/sysusers/Makefile

0 comments on commit 1b99214

Please sign in to comment.