summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-07-16 15:24:33 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-07-16 15:24:33 +0000
commit42c13f4acdd1e9f6a6cbdd88cb76006768d7ff6a (patch)
treebf885d11f5a376cfa0e7604ff18cc9f058c13745 /src
parent16013c560e189bd0c4fb58e038c0f449542dba7a (diff)
downloadskalibs-42c13f4acdd1e9f6a6cbdd88cb76006768d7ff6a.tar.xz
Change setgroups() management, in particular add skalibs_setgroups()
Diffstat (limited to 'src')
-rw-r--r--src/headers/setgroups-footer2
-rw-r--r--src/headers/setgroups-header5
-rw-r--r--src/headers/setgroups-stub2
-rw-r--r--src/include/skalibs/nonposix.h9
-rw-r--r--src/include/skalibs/stddjb.h2
-rw-r--r--src/libstddjb/setgroups.c45
-rw-r--r--src/sysdeps/tryegidingroups.c39
7 files changed, 55 insertions, 49 deletions
diff --git a/src/headers/setgroups-footer b/src/headers/setgroups-footer
deleted file mode 100644
index ddd5dae..0000000
--- a/src/headers/setgroups-footer
+++ /dev/null
@@ -1,2 +0,0 @@
-
-#endif
diff --git a/src/headers/setgroups-header b/src/headers/setgroups-header
deleted file mode 100644
index 0b8697a..0000000
--- a/src/headers/setgroups-header
+++ /dev/null
@@ -1,5 +0,0 @@
-/* ISC license. */
-
-#ifndef SETGROUPS_H
-#define SETGROUPS_H
-
diff --git a/src/headers/setgroups-stub b/src/headers/setgroups-stub
deleted file mode 100644
index 3c54861..0000000
--- a/src/headers/setgroups-stub
+++ /dev/null
@@ -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
index 7f064d5..3ef4e99 100644
--- 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
index 1a2ccca..50e07c5 100644
--- 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
new file mode 100644
index 0000000..0bcda12
--- /dev/null
+++ 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
deleted file mode 100644
index 687740d..0000000
--- a/src/sysdeps/tryegidingroups.c
+++ /dev/null
@@ -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 ;
-}