From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <96075317506475.tru64-unix-managers.marc@archive.invalid> From: haymanR () icefog ! uacn ! alaska ! edu Date: Fri, 10 Feb 1995 01:35:38 +0000 To: tru64-unix-managers Subject: getgroups(2) of another user X-MARC-Message: https://marc.info/?l=tru64-unix-managers&m=96075317506475 Greetings - I'm having a slight bit of difficulty getting the grouplist of another user (except when using groups(1)). Below is a code fragment which returns primary group of the other user, but then displays the current user's grouplist. What am I missing? The man pages are not all that clear, and W.R. Stevens book on Advanced Programming in the UNIX Environment (p151) states "In addition to finding all the groups that 'username' is a member of in the group file, 'initgroups' also includes 'basegid' in the supplementary group ID list." I'm confused. ---code fragment--- ... struct pr_passwd *pr; struct passwd *upr; struct group *gpr; int num_groups; gid_t grouplist[NGROUPS_MAX]; int i; /* grouplist[] index */ ... char name[16]; ... /* /etc/group entries */ if (NULL == (gpr = getgrgid(upr->pw_gid)) ) { fprintf(stderr, "\n**Error getting primary /etc/group entry for %s.\n", pr->ufld.fd_name); } else { fprintf(stdout, "\n\tprimary group:\t\t%s", gpr->gr_name); } if (-1 == setreuid(pr->ufld.fd_uid, pr->ufld.fd_uid)) /* real and effective uids to 'name' context */ { fprintf(stderr, "\nCan not seuid there\n"); } if (NULL == initgroups(name, gpr->gr_gid) ) { fprintf(stderr, "\n**Error initializing /etc/group entries for %s.\n", pr->ufld.fd_name); } else { if (-1 == (num_groups = getgroups(NGROUPS_MAX, grouplist)) ) { fprintf(stderr, "\n**Error getting /etc/group entries for %s.\n", pr->ufld.fd_name); } else { i = 0; /* grouplist[0] is the primary group for this user */ for (fprintf(stdout, "\n\tgroup membership (%u):\t", num_groups) ; i < num_groups ; i++, (0 == (i % 5))?fprintf(stdout, "\n\t\t\t\t"):NULL ) { if (NULL == (gpr = getgrgid(grouplist[i])) ) { fprintf(stderr, "\n**Error getting additional groups for %s.\n", pr->ufld.fd_name); } else { fprintf(stdout, "%s (%u) ", gpr->gr_name, grouplist[i]); } } } } ... output ends up being of the following format: ... primary group: users group membership (10): system (0) daemon (1) mem (3) bin (4) sec (5) auth (11) users (15) sysadmin (16) tcb (18) adm (19) ... Where primary group is correct for the user inquired about, but the group membership is correct for the user actually running the program. Any assistance would be greatly appreciated. Thanks. Randy M. Hayman haymanr@icefog.alaska.edu