From 980152fd53fc3cf488c8987cdc21d9851c1957cd Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Tue, 30 Nov 2021 11:44:22 +0100 Subject: [PATCH] Add support for non-root users to use initctl, e.g. group wheel Signed-off-by: Joachim Wiberg --- configure.ac | 10 ++++++++++ src/api.c | 13 ++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 8c166f10..8d85546c 100644 --- a/configure.ac +++ b/configure.ac @@ -114,6 +114,10 @@ AC_ARG_WITH(sysconfig, AS_HELP_STRING([--with-sysconfig=DIR], [System environment directory, sourced by .conf files, default /etc/default & /etc/conf.d]), [sysconfig=$withval], [sysconfig=default]) +AC_ARG_WITH(group, + AS_HELP_STRING([--with-group=NAME], [Group for /run/finit/socket (initctl), default: root]), + [group=$withval], [group=root]) + AC_ARG_WITH(hostname, AS_HELP_STRING([--with-hostname=NAME], [If /etc/hostname is missing, default: noname]), [hostname=$withval], [hostname=yes]) @@ -188,6 +192,11 @@ AS_IF([test "x$with_heading" != "xno"], [ AS_IF([test "x$heading" = "xyes"], [heading=""]) AC_DEFINE_UNQUOTED(INIT_OSHEADING, "$heading", [Displayed by Finit at boot, defaults to /etc/os-release PRETTY_NAME])]) +AS_IF([test "x$with_group" != "xno"], [ + AS_IF([test "x$group" = "xyes"], [ + group=root])]) +AC_DEFINE_UNQUOTED(DEFGROUP, "$group", [For /run/finit/socket]) + AS_IF([test "x$with_hostname" != "xno"], [ AS_IF([test "x$hostname" = "xyes"], [ hostname=noname])]) @@ -273,6 +282,7 @@ Optional features: Run fsck fix mode.....: $enable_fsckfix Redirect output.......: $enable_redirect Default hostname......: $hostname + Default group.........: $group Default runlevel......: $runlevel ------------- Compiler version -------------- diff --git a/src/api.c b/src/api.c index 4365bb03..f9276f6b 100644 --- a/src/api.c +++ b/src/api.c @@ -466,12 +466,13 @@ error: int api_init(uev_ctx_t *ctx) { - int sd; - mode_t oldmask; struct sockaddr_un sun = { .sun_family = AF_UNIX, .sun_path = INIT_SOCKET, }; + mode_t oldmask; + int uid, gid; + int sd; _d("Setting up external API socket ..."); sd = socket(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0); @@ -480,14 +481,20 @@ int api_init(uev_ctx_t *ctx) return 1; } + uid = geteuid(); + gid = getgroup(DEFGROUP); + erase(INIT_SOCKET); - oldmask = umask(0077); + oldmask = umask(0117); if (-1 == bind(sd, (struct sockaddr*)&sun, sizeof(sun))) goto error; if (-1 == listen(sd, 10)) goto error; + if (chown(INIT_SOCKET, uid, gid)) + _pe("Failed setting group %s on %s", DEFGROUP, INIT_SOCKET); + umask(oldmask); if (!uev_io_init(ctx, &api_watcher, api_cb, NULL, sd, UEV_READ)) return 0;