#include "includes.h"
#ifndef HAVE_GETGROUPLIST
static int getgrouplist_internals(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
{
gid_t *gids_saved;
int ret, ngrp_saved, num_gids;
if (non_root_mode()) {
*grpcnt = 0;
return 0;
}
ngrp_saved = getgroups(0, NULL);
if (ngrp_saved == -1) {
return -1;
}
gids_saved = SMB_MALLOC_ARRAY(gid_t, ngrp_saved+1);
if (!gids_saved) {
errno = ENOMEM;
return -1;
}
ngrp_saved = getgroups(ngrp_saved, gids_saved);
if (ngrp_saved == -1) {
SAFE_FREE(gids_saved);
return -1;
}
if (initgroups(user, gid) != 0) {
DEBUG(0, ("getgrouplist_internals: initgroups() failed!\n"));
SAFE_FREE(gids_saved);
return -1;
}
save_re_gid();
set_effective_gid(gid);
setgid(gid);
num_gids = getgroups(0, NULL);
if (num_gids + 1 > *grpcnt) {
*grpcnt = num_gids + 1;
ret = -1;
} else {
ret = getgroups(*grpcnt - 1, &groups[1]);
if (ret >= 0) {
groups[0] = gid;
*grpcnt = ret + 1;
}
remove_duplicate_gids( grpcnt, groups );
}
restore_re_gid();
if (sys_setgroups(ngrp_saved, gids_saved) != 0) {
DEBUG(0,("ERROR: getgrouplist: failed to reset group list!\n"));
smb_panic("getgrouplist: failed to reset group list!\n");
free(gids_saved);
return -1;
}
free(gids_saved);
return ret;
}
#endif
int sys_getgrouplist(const char *user, gid_t gid, gid_t *groups, int *grpcnt)
{
char *p;
int retval;
DEBUG(10,("sys_getgrouplist: user [%s]\n", user));
if ( (p = strchr(user, *lp_winbind_separator())) == NULL ) {
if ( !winbind_off() )
DEBUG(0,("sys_getgroup_list: Insufficient environment space for %s\n",
WINBINDD_DONT_ENV));
else
DEBUG(10,("sys_getgrouplist(): disabled winbindd for group lookup [user == %s]\n",
user));
}
#ifdef HAVE_GETGROUPLIST
retval = getgrouplist(user, gid, groups, grpcnt);
#else
become_root();
retval = getgrouplist_internals(user, gid, groups, grpcnt);
unbecome_root();
#endif
winbind_on();
return retval;
}