s3: use getgrset() when it is available
authorChristian Ambach <christian.ambach@de.ibm.com>
Mon, 14 Mar 2011 12:08:58 +0000 (08:08 -0400)
committerVolker Lendecke <vlendec@samba.org>
Sat, 19 Mar 2011 09:09:38 +0000 (10:09 +0100)
When getgrouplist() is not defined, use getgrset() if it is defined
instead of using the initgroups() + getgroups() combo

Major contributions from Yannick Bergeron <yaberger@ca.ibm.com>

Autobuild-User: Volker Lendecke <vlendec@samba.org>
Autobuild-Date: Sat Mar 19 10:09:38 CET 2011 on sn-devel-104

source3/configure.in
source3/lib/system_smbd.c
source3/wscript

index 74df9d4836e9b084348eae301ca4634142e56f47..6033b0b13be7ed70569dfb65dc19baf444c371cc 100644 (file)
@@ -1070,6 +1070,7 @@ AC_CHECK_FUNCS(fdatasync,,[AC_CHECK_LIB_EXT(rt, LIBS, fdatasync)])
 AC_CHECK_FUNCS(setsid glob strpbrk crypt16 getauthuid)
 AC_CHECK_FUNCS(sigprocmask sigblock sigaction sigset innetgr setnetgrent getnetgrent endnetgrent)
 AC_CHECK_FUNCS(initgroups select rdchk getgrnam getgrent pathconf)
+AC_CHECK_FUNCS(getgrset)
 AC_CHECK_FUNCS(setpriv setgidx setuidx setgroups sysconf stat64 fstat64)
 AC_CHECK_FUNCS(lstat64 fopen64 atexit grantpt lseek64 ftruncate64 posix_fallocate posix_fallocate64)
 AC_CHECK_FUNCS(fallocate fallocate64)
index 3bb2c4240b44c40209fd6259036647619799653e..12538c4434fed89a9cdaaa6688d1d43c583823f6 100644 (file)
 
 #ifndef HAVE_GETGROUPLIST
 
+#ifdef HAVE_GETGRSET
+static int getgrouplist_getgrset(const char *user, gid_t gid, gid_t *groups,
+                                int *grpcnt)
+{
+       char *grplist;
+       char *grp;
+       gid_t temp_gid;
+       int num_gids = 1;
+       int ret = 0;
+       long l;
+
+       grplist = getgrset(user);
+
+       DEBUG(10, ("getgrset returned %s\n", grplist));
+
+       if (grplist == NULL) {
+               return -1;
+       }
+
+       if (*grpcnt > 0) {
+               groups[0] = gid;
+       }
+
+       while ((grp = strsep(&grplist, ",")) != NULL) {
+               l = strtol(grp, NULL, 10);
+               temp_gid = (gid_t) l;
+               if (temp_gid == gid) {
+                       continue;
+               }
+
+               if (num_gids + 1 > *grpcnt) {
+                       num_gids++;
+                       continue;
+               }
+               groups[num_gids++] = temp_gid;
+       }
+       free(grplist);
+
+       if (num_gids > *grpcnt) {
+               ret = -1;
+       }
+       *grpcnt = num_gids;
+
+       DEBUG(10, ("Found %d groups for user %s\n", *grpcnt, user));
+
+       return ret;
+}
+
+#else /* HAVE_GETGRSET */
+
 /*
   This is a *much* faster way of getting the list of groups for a user
   without changing the current supplementary group list. The old
@@ -113,7 +163,8 @@ static int getgrouplist_internals(const char *user, gid_t gid, gid_t *groups,
        free(gids_saved);
        return ret;
 }
-#endif
+#endif /* HAVE_GETGRSET */
+#endif /* HAVE_GETGROUPLIST */
 
 static int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
 {
@@ -130,11 +181,15 @@ static int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grp
 
 #ifdef HAVE_GETGROUPLIST
        retval = getgrouplist(user, gid, groups, grpcnt);
+#else
+#ifdef HAVE_GETGRSET
+       retval = getgrouplist_getgrset(user, gid, groups, grpcnt);
 #else
        become_root();
        retval = getgrouplist_internals(user, gid, groups, grpcnt);
        unbecome_root();
-#endif
+#endif /* HAVE_GETGRSET */
+#endif /* HAVE_GETGROUPLIST */
 
        /* allow winbindd lookups, but only if they were not already disabled */
        if (!winbind_env) {
index 9fcf173065265dec067e20f27a5bf7139d142f54..e8bd62598b14e72dcb4b84854e0def0f11f86af4 100644 (file)
@@ -269,7 +269,7 @@ _fork __fork fremoveea fremovexattr fseek64 fseeko64 fsetea
 fsetproplist fsetxattr _fstat __fstat fstat64 _fstat64 __fstat64 fsync
 ftell64 ftello64 ftruncate64 futimens futimes __fxstat getauthuid
 getcwd _getcwd __getcwd getdents __getdents getdents64 getdirentries
-getgrent getgrnam getgrouplist getmntent getpagesize
+getgrent getgrnam getgrouplist getgrset getmntent getpagesize
 getproplist get_proplist_entry getpwanam getpwent_r getrlimit gettext
 glob grantpt hstrerror initgroups innetgr
 inotify_init lgetea lgetxattr listea listxattr llistea llistxattr