1 #define NGROUPS_MAX 1000
11 #ifndef HAVE_GETGROUPLIST
13 This is a *much* faster way of getting the list of groups for a user
14 without changing the current supplemenrary group list. The old
15 method user getgrent() which could take 20 minutes on a really big
16 network with hundeds of thousands of groups and users. The new method
17 takes a couple of seconds. (tridge@samba.org)
19 unfortunately it only works if we are root!
22 getgrouplist(uname, agroup, groups, grpcnt)
25 register gid_t *groups;
28 gid_t gids_saved[NGROUPS_MAX + 1];
31 ngrp_saved = getgroups(NGROUPS_MAX, gids_saved);
32 if (ngrp_saved == -1) {
36 if (initgroups(uname, agroup) != 0) {
40 ret = getgroups(*grpcnt, groups);
45 if (setgroups(ngrp_saved, gids_saved) != 0) {
47 fprintf(stderr,"getgrouplist: failed to reset group list!\n");
55 int main(int argc, char *argv[])
59 gid_t gids[NGROUPS_MAX + 1];
65 printf("Unknown user '%s'\n", user);
71 ret = getgrouplist(user, pwd->pw_gid, gids, &count);
73 printf("ret=%d\n", ret);
76 printf("getgrouplist failed\n");
80 printf("Got %d groups\n", ret);
82 for (i=0;i<count;i++) {
83 printf("%u ", (unsigned)gids[i]);