getgrouplist(3) Library Functions Manual getgrouplist(3) getgrouplist - , C (libc, -lc) #include int getgrouplist(const char *user, gid_t group, gid_t *groups, int *ngroups); glibc (. feature_test_macros(7)): getgrouplist(): glibc 2.19: _DEFAULT_SOURCE glibc 2.19 : _BSD_SOURCE getgrouplist() ( group(5)) , user. groups *ngroups . , user, , getgrouplist(), group; , ID user. ngroups -: , user group; , , groups. , user , *ngroups, *ngroups. If the user is a member of more than *ngroups groups, then getgrouplist() returns -1. In this case, the value returned in *ngroups can be used to resize the buffer passed to a further call to getgrouplist(). attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |getgrouplist() | | MT-Safe locale | +----------------------------+----------------------------------------------------------+--------------------------+ . glibc 2.2.4. Before glibc 2.3.3, the implementation of this function contains a buffer-overrun bug: it returns the complete list of groups for user in the array groups, even when the number of groups exceeds *ngroups. , , . ngroups, getgrouplist(). : $ ./a.out cecilia 0 getgrouplist() -1; ngroups = 3 $ ./a.out cecilia 3 ngroups = 3 16 (dialout) 33 (video) 100 (users) #include #include #include #include #include int main(int argc, char *argv[]) { int ngroups; gid_t *groups; struct group *gr; struct passwd *pw; if (argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); exit(EXIT_FAILURE); } ngroups = atoi(argv[2]); groups = malloc(sizeof(*groups) * ngroups); if (groups == NULL) { perror("malloc"); exit(EXIT_FAILURE); } /* Fetch passwd structure (contains first group ID for user). */ errno = 0; pw = getpwnam(argv[1]); if (pw == NULL) { if (errno) perror("getpwnam"); else fprintf(stderr, "no such user\n"); exit(EXIT_FAILURE); } /* Retrieve group list. */ if (getgrouplist(argv[1], pw->pw_gid, groups, &ngroups) == -1) { fprintf(stderr, "getgrouplist() returned -1; ngroups = %d\n", ngroups); exit(EXIT_FAILURE); } /* Display list of retrieved groups, along with group names. */ fprintf(stderr, "ngroups = %d\n", ngroups); for (int j = 0; j < ngroups; j++) { printf("%d", groups[j]); gr = getgrgid(groups[j]); if (gr != NULL) printf(" (%s)", gr->gr_name); printf("\n"); } exit(EXIT_SUCCESS); } getgroups(2), setgroups(2), getgrent(3), group_member(3), group(5), passwd(5) () Azamat Hackimov , Dmitry Bolkhovskikh , Vladislav , Yuri Kozlov , Kirill Rekhov ; GNU (GNU General Public License - GPL, 3 ) , - . - , , () () () <>. Linux 6.9.1 15 2024 . getgrouplist(3)