From 5894692c6a462c8d3ca79bc09d21d6ea3be0e652 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 13 Mar 2015 21:18:04 +0000 Subject: - new options to s6-envuidgid - version: rc for 2.1.3.0 --- src/daemontools-extras/s6-envuidgid.c | 89 +++++++++++++++++++++++++---------- 1 file changed, 65 insertions(+), 24 deletions(-) (limited to 'src/daemontools-extras') diff --git a/src/daemontools-extras/s6-envuidgid.c b/src/daemontools-extras/s6-envuidgid.c index 64521b0..3a58c3c 100644 --- a/src/daemontools-extras/s6-envuidgid.c +++ b/src/daemontools-extras/s6-envuidgid.c @@ -3,42 +3,83 @@ #include #include #include +#include #include -#include +#include +#include #include +#include #include #include -#define USAGE "s6-envuidgid username prog..." +#define USAGE "s6-envuidgid [ -i | -D defaultuid:defaultgid ] username prog..." +#define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv, char const *const *envp) { + struct passwd *pw ; + uint64 uid ; + gid_t gid ; + gid_t tab[NGROUPS_MAX] ; + int n = 0 ; + int insist = 1 ; PROG = "s6-envuidgid" ; - if (argc < 3) strerr_dieusage(100, USAGE) ; { - char fmt[UINT64_FMT] ; - struct passwd *pw = getpwnam(argv[1]) ; - if (!pw) strerr_dief2x(1, "unknown user: ", argv[1]) ; - fmt[gid_fmt(fmt, pw->pw_gid)] = 0 ; - if (!pathexec_env("GID", fmt)) - strerr_diefu1sys(111, "update environment") ; - fmt[uint64_fmt(fmt, pw->pw_uid)] = 0 ; - if (!pathexec_env("UID", fmt)) - strerr_diefu1sys(111, "update environment") ; + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) + { + register int opt = subgetopt_r(argc, argv, "iD:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 'i' : insist = 1 ; break ; + case 'D' : + { + unsigned int pos = uint64_scan(l.arg, &uid) ; + if (!pos) + { + if (l.arg[pos] != ':') dieusage() ; + uid = 0 ; + } + if (!l.arg[pos]) gid = 0 ; + else + { + if (l.arg[pos++] != ':') dieusage() ; + if (!l.arg[pos]) gid = 0 ; + else if (!gid0_scan(l.arg + pos, &gid)) dieusage() ; + } + insist = 0 ; + break ; + } + default : dieusage() ; + } + } + argc -= l.ind ; argv += l.ind ; } - + if (argc < 2) dieusage() ; + + pw = getpwnam(argv[0]) ; + if (pw) { - gid_t tab[NGROUPS_MAX] ; - int n = prot_readgroups(argv[1], tab, NGROUPS_MAX) ; + uid = pw->pw_uid ; + gid = pw->pw_gid ; + n = prot_readgroups(argv[0], tab, NGROUPS_MAX) ; if (n < 0) - strerr_diefu2sys(111, "get supplementary groups for ", argv[1]) ; - { - char fmt[GID_FMT * n] ; - fmt[gid_fmtlist(fmt, tab, n)] = 0 ; - if (!pathexec_env("GIDLIST", fmt)) - strerr_diefu1sys(111, "update environment") ; - } + strerr_diefu2sys(111, "get supplementary groups for ", argv[0]) ; + } + else if (insist) strerr_dief2x(1, "unknown user: ", argv[1]) ; + + { + unsigned int pos = 0 ; + char fmt[19 + UINT64_FMT + (n+1) * GID_FMT] ; + byte_copy(fmt + pos, 4, "UID=") ; pos += 4 ; + pos += uint64_fmt(fmt + pos, uid) ; + byte_copy(fmt + pos, 5, "\0GID=") ; pos += 5 ; + pos += gid_fmt(fmt + pos, gid) ; + byte_copy(fmt + pos, 9, "\0GIDLIST=") ; pos += 9 ; + pos += gid_fmtlist(fmt + pos, tab, n) ; + fmt[pos++] = 0 ; + pathexec_r(argv+1, envp, env_len(envp), fmt, pos) ; } - pathexec_fromenv(argv+2, envp, env_len(envp)) ; - strerr_dieexec(111, argv[2]) ; + strerr_dieexec(111, argv[1]) ; } -- cgit v1.2.3