skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit 42c13f4acdd1e9f6a6cbdd88cb76006768d7ff6a
parent 16013c560e189bd0c4fb58e038c0f449542dba7a
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Sun, 16 Jul 2017 15:24:33 +0000

 Change setgroups() management, in particular add skalibs_setgroups()

Diffstat:
MMakefile | 12+-----------
Mconfigure | 1-
Mpackage/deps.mak | 2++
Dsrc/headers/setgroups-footer | 2--
Dsrc/headers/setgroups-header | 5-----
Dsrc/headers/setgroups-stub | 2--
Msrc/include/skalibs/nonposix.h | 9+++++++++
Msrc/include/skalibs/stddjb.h | 2+-
Asrc/libstddjb/setgroups.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Dsrc/sysdeps/tryegidingroups.c | 39---------------------------------------
10 files changed, 58 insertions(+), 61 deletions(-)

diff --git a/Makefile b/Makefile @@ -44,8 +44,7 @@ src/include/$(package)/uint32.h \ src/include/$(package)/uint64.h \ src/include/$(package)/types.h \ src/include/$(package)/error.h \ -src/include/$(package)/ip46.h \ -src/include/$(package)/setgroups.h +src/include/$(package)/ip46.h ALL_INCLUDES := $(sort $(BUILT_INCLUDES) $(wildcard src/include/$(package)/*.h)) ALL_SYSDEPS := $(wildcard $(sysdeps)/*) ALL_DATA := $(wildcard src/etc/*) @@ -167,12 +166,3 @@ src/include/$(package)/ip46.h: src/include/$(package)/fmtscan.h src/include/$(pa fi ; \ exec cat src/headers/ip46-footer ; \ } > $@ - -src/include/$(package)/setgroups.h: $(sysdeps)/sysdeps src/headers/setgroups-header src/headers/setgroups-footer src/headers/setgroups-stub - @{ \ - cat src/headers/setgroups-header ; \ - if grep -qF 'setgroups: yes' $(sysdeps)/sysdeps ; then : ; \ - else cat src/headers/setgroups-stub ; \ - fi ; \ - exec cat src/headers/setgroups-footer ; \ - } > $@ diff --git a/configure b/configure @@ -501,7 +501,6 @@ EOF choose cl revoke REVOKE 'revoke()' choose cl sendfile SENDFILE 'sendfile()' choose cl setgroups SETGROUPS 'setgroups()' - choose clr egidingroups EGIDINGROUPS 'the effective gid in the supplementary group list' choose cl settimeofday SETTIMEOFDAY 'settimeofday()' choose clr signalfd SIGNALFD 'signalfd()' choose clr splice SPLICE 'splice()' diff --git a/package/deps.mak b/package/deps.mak @@ -32,6 +32,7 @@ src/include/skalibs/lolstdio.h: src/include/skalibs/bufalloc.h src/include/skali src/include/skalibs/mininetstring.h: src/include/skalibs/stralloc.h src/include/skalibs/netstring.h: src/include/skalibs/buffer.h src/include/skalibs/stralloc.h src/include/skalibs/random.h: src/include/skalibs/stralloc.h +src/include/skalibs/setgroups.h: src/include/skalibs/sysdeps.h src/include/skalibs/sha512.h: src/include/skalibs/uint64.h src/include/skalibs/sig.h: src/include/skalibs/gccattributes.h src/include/skalibs/siovec.h: src/include/skalibs/gccattributes.h @@ -450,6 +451,7 @@ src/libstddjb/selfpipe_read.o src/libstddjb/selfpipe_read.lo: src/libstddjb/self src/libstddjb/selfpipe_trap.o src/libstddjb/selfpipe_trap.lo: src/libstddjb/selfpipe_trap.c src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/libstddjb/selfpipe_trapset.o src/libstddjb/selfpipe_trapset.lo: src/libstddjb/selfpipe_trapset.c src/include/skalibs/nonposix.h src/include/skalibs/nsig.h src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h src/libstddjb/selfpipe_untrap.o src/libstddjb/selfpipe_untrap.lo: src/libstddjb/selfpipe_untrap.c src/libstddjb/selfpipe-internal.h src/include/skalibs/selfpipe.h src/include/skalibs/sig.h src/include/skalibs/sysdeps.h +src/libstddjb/setgroups.o src/libstddjb/setgroups.lo: src/libstddjb/setgroups.c src/include/skalibs/nonposix.h src/include/skalibs/setgroups.h src/include/skalibs/sysdeps.h src/libstddjb/sgetopt.o src/libstddjb/sgetopt.lo: src/libstddjb/sgetopt.c src/include/skalibs/buffer.h src/include/skalibs/sgetopt.h src/libstddjb/sig_block.o src/libstddjb/sig_block.lo: src/libstddjb/sig_block.c src/include/skalibs/sig.h src/libstddjb/sig_blocknone.o src/libstddjb/sig_blocknone.lo: src/libstddjb/sig_blocknone.c src/include/skalibs/sig.h diff --git a/src/headers/setgroups-footer b/src/headers/setgroups-footer @@ -1,2 +0,0 @@ - -#endif diff --git a/src/headers/setgroups-header b/src/headers/setgroups-header @@ -1,5 +0,0 @@ -/* ISC license. */ - -#ifndef SETGROUPS_H -#define SETGROUPS_H - diff --git a/src/headers/setgroups-stub b/src/headers/setgroups-stub @@ -1,2 +0,0 @@ -#include <errno.h> -#define setgroups(n, s) (errno = ENOSYS, -1) diff --git a/src/include/skalibs/nonposix.h b/src/include/skalibs/nonposix.h @@ -53,6 +53,15 @@ #endif + /* Unfortunately we can't fully avoid #ifdef forests, + because the BSDs are bloody snowflakes. See: setgroups(). */ + +#undef SKALIBS_BSD_SUCKS +#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__bsdi__) +# define SKALIBS_BSD_SUCKS +#endif + + /* old versions of BSD and some broken GNU toolchains: system headers are not self-contained, starting with sys/types.h normally always works. */ diff --git a/src/include/skalibs/stddjb.h b/src/include/skalibs/stddjb.h @@ -10,7 +10,6 @@ #include <skalibs/types.h> #include <skalibs/error.h> #include <skalibs/ip46.h> -#include <skalibs/setgroups.h> #include <skalibs/alarm.h> #include <skalibs/alloc.h> @@ -45,6 +44,7 @@ #include <skalibs/nsig.h> #include <skalibs/segfault.h> #include <skalibs/selfpipe.h> +#include <skalibs/setgroups.h> #include <skalibs/sgetopt.h> #include <skalibs/sig.h> #include <skalibs/siovec.h> diff --git a/src/libstddjb/setgroups.c b/src/libstddjb/setgroups.c @@ -0,0 +1,45 @@ +/* ISC license. */ + +#include <skalibs/sysdeps.h> + +#ifdef SKALIBS_HASSETGROUPS + +#include <skalibs/nonposix.h> +#include <string.h> +#include <unistd.h> +#include <skalibs/setgroups.h> + +int setgroups_with_egid (size_t n, gid_t const *tab) +{ + size_t i = 1 ; + gid_t g = getegid() ; + if (!n) return setgroups(1, &g) ; + if (tab[0] == g) return setgroups(n, tab) ; + for (; i < n ; i++) if (tab[i] == g) break ; + if (i < n) + { + gid_t newtab[n] ; + newtab[0] = g ; + memcpy(newtab + 1, tab, i * sizeof(gid_t)) ; + memcpy(newtab + i + 1, tab + i + 1, (n - i - 1) * sizeof(gid_t)) ; + return setgroups(n, newtab) ; + } + else + { + gid_t newtab[n+1] ; + newtab[0] = g ; + memcpy(newtab + 1, tab, n * sizeof(gid_t)) ; + return setgroups(n+1, newtab) ; + } +} + +int skalibs_setgroups (size_t n, gid_t const *tab) +{ +#ifdef SKALIBS_BSD_SUCKS + return setgroups_with_egid(n, tab) ; +#else + return setgroups(n, tab) ; +#endif +} + +#endif diff --git a/src/sysdeps/tryegidingroups.c b/src/sysdeps/tryegidingroups.c @@ -1,39 +0,0 @@ -/* ISC license. */ - -#undef _POSIX_C_SOURCE -#undef _XOPEN_SOURCE - -#ifndef _BSD_SOURCE -#define _BSD_SOURCE -#endif - -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif - -#ifndef _DEFAULT_SOURCE -#define _DEFAULT_SOURCE -#endif - -#ifndef _NETBSD_SOURCE -#define _NETBSD_SOURCE -#endif - -#ifndef __EXTENSIONS__ -#define __EXTENSIONS__ -#endif - -#include <sys/types.h> -#include <unistd.h> -#include <grp.h> - -int main (void) -{ - gid_t gid = getegid() ; - gid_t list[NGROUPS_MAX] ; - int i = 0 ; - int r = getgroups(NGROUPS_MAX, list) ; - if (r < 0) return 111 ; - for (; i < r ; i++) if (list[i] == gid) return 0 ; - return 1 ; -}