diff options
author | Andreas Schneider <asn@samba.org> | 2017-08-01 08:14:30 +0200 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2017-08-01 15:13:07 +0200 |
commit | 1c793e19f1cb8cb33defb171c9ae719ec8a820da (patch) | |
tree | 3ffb9e71c72ac94a64ccaa5e75e5c88e1671dbb1 | |
parent | 7d12d4eb01ee0d0d1bd3ee1d02f20d99009c4c73 (diff) | |
download | uid_wrapper-1c793e19f1cb8cb33defb171c9ae719ec8a820da.tar.gz uid_wrapper-1c793e19f1cb8cb33defb171c9ae719ec8a820da.tar.xz uid_wrapper-1c793e19f1cb8cb33defb171c9ae719ec8a820da.zip |
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
-rw-r--r-- | src/uid_wrapper.c | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/src/uid_wrapper.c b/src/uid_wrapper.c index 9bd154a..9c4b0ee 100644 --- a/src/uid_wrapper.c +++ b/src/uid_wrapper.c @@ -34,6 +34,7 @@ #include <syscall.h> #endif #include <dlfcn.h> +#include <limits.h> #include <pthread.h> @@ -972,6 +973,31 @@ static void uwrap_thread_child(void) UWRAP_UNLOCK_ALL; } +static unsigned long uwrap_get_xid_from_env(const char *envname) +{ + unsigned long xid; + const char *env = NULL; + char *endp = NULL; + + env = getenv(envname); + if (env == NULL) { + return ULONG_MAX; + } + + if (env[0] == '\0') { + unsetenv("UID_WRAPPER_INITIAL_RUID"); + return ULONG_MAX; + } + + xid = strtoul(env, &endp, 10); + unsetenv("UID_WRAPPER_INITIAL_RUID"); + if (env == endp) { + return ULONG_MAX; + } + + return xid; +} + /* * This initializes uid_wrapper with the IDs exported to the environment. Those * are normally set after we forked and executed. @@ -980,47 +1006,38 @@ static void uwrap_init_env(struct uwrap_thread *id) { const char *env; int ngroups = 0; + unsigned long xid; - env = getenv("UID_WRAPPER_INITIAL_RUID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env); - id->ruid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_RUID"); + /* UIDs */ + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_RUID"); + if (xid != ULONG_MAX) { + id->ruid = (uid_t)xid; } - env = getenv("UID_WRAPPER_INITIAL_EUID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize euid with %s", env); - id->euid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_EUID"); + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_EUID"); + if (xid != ULONG_MAX) { + id->euid = (uid_t)xid; } - env = getenv("UID_WRAPPER_INITIAL_SUID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize suid with %s", env); - id->suid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_SUID"); + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_SUID"); + if (xid != ULONG_MAX) { + id->suid = (uid_t)xid; } - env = getenv("UID_WRAPPER_INITIAL_RGID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initialize ruid with %s", env); - id->rgid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_RGID"); + /* GIDs */ + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_RGID"); + if (xid != ULONG_MAX) { + id->rgid = (gid_t)xid; } - env = getenv("UID_WRAPPER_INITIAL_EGID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize egid with %s", env); - id->egid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_EGID"); + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_EGID"); + if (xid != ULONG_MAX) { + id->egid = (gid_t)xid; } - env = getenv("UID_WRAPPER_INITIAL_SGID"); - if (env != NULL && env[0] != '\0') { - UWRAP_LOG(UWRAP_LOG_DEBUG, "Initalize sgid with %s", env); - id->sgid = strtoul(env, (char **)NULL, 10); - unsetenv("UID_WRAPPER_INITIAL_SGID"); + xid = uwrap_get_xid_from_env("UID_WRAPPER_INITIAL_SGID"); + if (xid != ULONG_MAX) { + id->sgid = (gid_t)xid; } env = getenv("UID_WRAPPER_INITIAL_GROUPS_COUNT"); |