2008-06-30 14:44:09 +00:00
|
|
|
.\" Copyright (C) 2008, Linux Foundation, written by Michael Kerrisk
|
|
|
|
.\" <mtk.manpages@gmail.com>
|
2004-11-03 13:51:07 +00:00
|
|
|
.\"
|
2008-06-30 14:44:09 +00:00
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
|
|
|
.\" A few pieces remain from an earlier version written in
|
|
|
|
.\" 2002 by Walter Harms (walter.harms@informatik.uni-oldenburg.de)
|
|
|
|
.\"
|
2008-07-03 14:52:47 +00:00
|
|
|
.TH GETGROUPLIST 3 2008-07-03 "GNU" "Linux Programmer's Manual"
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH NAME
|
2008-06-30 14:44:09 +00:00
|
|
|
getgrouplist \- get list of groups to which a user belongs
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH SYNOPSIS
|
|
|
|
.B #include <grp.h>
|
|
|
|
.sp
|
2007-04-03 15:32:52 +00:00
|
|
|
.BI "int getgrouplist(const char *" user ", gid_t " group ,
|
2007-07-08 12:11:40 +00:00
|
|
|
.br
|
|
|
|
.BI " gid_t *" groups ", int *" ngroups );
|
|
|
|
.sp
|
|
|
|
.in -4n
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
|
|
.BR feature_test_macros (7)):
|
|
|
|
.in
|
|
|
|
.sp
|
|
|
|
.BR getgrouplist ():
|
|
|
|
_BSD_SOURCE
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH DESCRIPTION
|
2007-04-12 22:42:49 +00:00
|
|
|
The
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getgrouplist ()
|
2008-06-30 14:44:09 +00:00
|
|
|
function scans the group database (see
|
|
|
|
.BR group (5))
|
|
|
|
to obtain the list of groups that
|
2004-11-03 13:51:07 +00:00
|
|
|
.I user
|
2007-04-12 22:42:49 +00:00
|
|
|
belongs to.
|
|
|
|
Up to
|
2007-12-22 22:10:40 +00:00
|
|
|
.I *ngroups
|
2008-06-30 14:44:09 +00:00
|
|
|
of these groups are returned in the array
|
|
|
|
.IR groups .
|
|
|
|
|
|
|
|
If it was not among the groups defined for
|
|
|
|
.I user
|
|
|
|
in the group database, then
|
2004-11-03 13:51:07 +00:00
|
|
|
.I group
|
2008-06-30 14:44:09 +00:00
|
|
|
is included in the list of groups returned by
|
|
|
|
.BR getgrouplist ();
|
2008-07-03 14:47:08 +00:00
|
|
|
typically this argument is specified as the group ID from
|
2008-06-30 14:44:09 +00:00
|
|
|
the password record for
|
|
|
|
.IR user .
|
|
|
|
|
|
|
|
The
|
|
|
|
.I ngroups
|
|
|
|
argument is a value-result argument:
|
|
|
|
on return it always contains the number of groups found for
|
|
|
|
.IR user ,
|
|
|
|
including
|
|
|
|
.IR group ;
|
|
|
|
this value may be greater than the number of groups stored in
|
|
|
|
.IR groups .
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH "RETURN VALUE"
|
2008-06-30 14:44:09 +00:00
|
|
|
If the number of groups of which
|
|
|
|
.I user
|
|
|
|
is a member is less than or equal to
|
|
|
|
.IR *ngroups ,
|
|
|
|
then the value
|
2007-12-22 22:10:40 +00:00
|
|
|
.I *ngroups
|
2008-06-30 14:44:09 +00:00
|
|
|
is returned.
|
|
|
|
|
|
|
|
If the user is a member of more than
|
|
|
|
.I *ngroups
|
|
|
|
groups, then
|
2005-10-19 07:07:02 +00:00
|
|
|
.BR getgrouplist ()
|
2007-06-12 21:19:16 +00:00
|
|
|
returns \-1.
|
2008-06-30 14:44:09 +00:00
|
|
|
In this case the value returned in
|
|
|
|
.IR *ngroups
|
|
|
|
can be used to resize the buffer passed to a further call
|
|
|
|
.BR getgrouplist ().
|
2007-05-19 04:30:20 +00:00
|
|
|
.SH "VERSIONS"
|
|
|
|
This function is present since glibc 2.2.4.
|
|
|
|
.SH "CONFORMING TO"
|
accept.2, execve.2, futimesat.2, getresuid.2, getrlimit.2, madvise.2, mq_getsetattr.2, msgctl.2, nice.2, open.2, poll.2, setresuid.2, shmctl.2, sigaction.2, times.2, utimensat.2, __setfpucw.3, atoi.3, endian.3, err.3, euidaccess.3, fenv.3, fopencookie.3, fpurge.3, getgrouplist.3, getutmp.3, j0.3, lgamma.3, netlink.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_cleanup_push_defer_np.3, pthread_getattr_np.3, pthread_kill_other_threads_np.3, pthread_setaffinity_np.3, pthread_tryjoin_np.3, pthread_yield.3, random_r.3, readdir.3, rtnetlink.3, scanf.3, setenv.3, significand.3, sigsetops.3, strerror.3, strstr.3, sysv_signal.3, termios.3, timegm.3, unlocked_stdio.3, y0.3, locale.5, bootparam.7, feature_test_macros.7, ip.7, locale.7, pthreads.7, rtld-audit.7, nscd.8: Global fix: s/non-standard/nonstandard/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:37:52 +00:00
|
|
|
This function is nonstandard; it appears on most BSDs.
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH BUGS
|
2008-06-30 14:44:09 +00:00
|
|
|
In glibc versions before 2.3.3,
|
|
|
|
the implementation of this function contains a buffer-overrun bug:
|
|
|
|
it returns the complete list of groups for
|
2008-07-02 13:43:42 +00:00
|
|
|
.IR user
|
2008-06-30 14:44:09 +00:00
|
|
|
in the array
|
|
|
|
.IR groups ,
|
|
|
|
even when the number of groups exceeds
|
2007-12-22 22:10:40 +00:00
|
|
|
.IR *ngroups .
|
2004-11-03 13:51:07 +00:00
|
|
|
.SH EXAMPLE
|
2008-06-30 14:44:09 +00:00
|
|
|
.PP
|
|
|
|
The program below displays the group list for the user named in its
|
|
|
|
first command-line argument.
|
|
|
|
The second command-line argument specifies the
|
|
|
|
.I ngroups
|
|
|
|
value to be supplied to
|
2008-07-04 15:16:12 +00:00
|
|
|
.BR getgrouplist ().
|
2008-06-30 14:44:09 +00:00
|
|
|
The following shell session shows examples of the use of this program:
|
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
|
eventfd.2, execve.2, getdents.2, ioprio_set.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, malloc.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5, proc.5, cpuset.7, mq_overview.7: Global fix: Format user input in shell sessions in boldface
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:37:22 +00:00
|
|
|
.RB "$" " ./a.out cecilia 0"
|
2008-06-30 14:44:09 +00:00
|
|
|
getgrouplist() returned -1; ngroups = 3
|
eventfd.2, execve.2, getdents.2, ioprio_set.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, malloc.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5, proc.5, cpuset.7, mq_overview.7: Global fix: Format user input in shell sessions in boldface
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:37:22 +00:00
|
|
|
.RB "$" " ./a.out cecilia 3"
|
2008-06-30 14:44:09 +00:00
|
|
|
ngroups = 3
|
|
|
|
16 (dialout)
|
|
|
|
33 (video)
|
|
|
|
100 (users)
|
|
|
|
.fi
|
|
|
|
.in
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strtol.3, core.5: global fix: Add ".SS Program source" to EXAMPLE
Add ".SS Program source" to clearly distinguish shell session and
descriptive text from actual program code.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-10-28 15:51:05 +00:00
|
|
|
.SS Program source
|
eventfd.2, getdents.2, mprotect.2, signalfd.2, timerfd_create.2, wait.2, backtrace.3, clock_getcpuclockid.3, end.3, fmemopen.3, fopencookie.3, frexp.3, getaddrinfo.3, getdate.3, getgrouplist.3, getprotoent_r.3, getservent_r.3, gnu_get_libc_version.3, inet.3, inet_pton.3, makecontext.3, matherr.3, offsetof.3, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, sem_wait.3, strftime.3, strtok.3, strtol.3, core.5: srcfix
s/\.R " "/\\\&/ as a way of getting a blank line after a .SS heading.
(Suggested by Sam Varshavchik <mrsam@courier-mta.com>)
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2008-11-05 01:42:45 +00:00
|
|
|
\&
|
2004-11-03 13:51:07 +00:00
|
|
|
.nf
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
|
2007-04-12 22:42:49 +00:00
|
|
|
int
|
2008-06-30 14:44:09 +00:00
|
|
|
main(int argc, char *argv[])
|
2007-04-05 12:36:57 +00:00
|
|
|
{
|
2008-06-30 14:44:09 +00:00
|
|
|
int j, ngroups;
|
|
|
|
gid_t *groups;
|
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
fprintf(stderr, "Usage: %s <user> <ngroups>\\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2008-07-02 13:43:42 +00:00
|
|
|
ngroups = atoi(argv[2]);
|
2008-06-30 14:44:09 +00:00
|
|
|
|
|
|
|
groups = malloc(ngroups * sizeof (gid_t));
|
|
|
|
if (groups == NULL) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fetch passwd structure (contains first group ID for user) */
|
|
|
|
|
|
|
|
pw = getpwnam(argv[1]);
|
|
|
|
if (pw == NULL) {
|
|
|
|
perror("getpwnam");
|
2007-05-16 02:09:09 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2008-06-30 14:44:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Retrieve group list */
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2008-06-30 14:44:09 +00:00
|
|
|
if (getgrouplist(argv[1], pw\->pw_gid, groups, &ngroups) == \-1) {
|
|
|
|
fprintf(stderr, "getgrouplist() returned \-1; ngroups = %d\\n",
|
|
|
|
ngroups);
|
|
|
|
exit(EXIT_FAILURE);
|
2007-04-05 12:36:57 +00:00
|
|
|
}
|
2004-11-03 13:51:07 +00:00
|
|
|
|
2008-06-30 14:44:09 +00:00
|
|
|
/* Display list of retrieved groups, along with group names */
|
|
|
|
|
|
|
|
fprintf(stderr, "ngroups = %d\\n", ngroups);
|
|
|
|
for (j = 0; j < ngroups; j++) {
|
|
|
|
printf("%d", groups[j]);
|
|
|
|
gr = getgrgid(groups[j]);
|
|
|
|
if (gr != NULL)
|
|
|
|
printf(" (%s)", gr\->gr_name);
|
|
|
|
printf("\\n");
|
2008-07-02 13:43:42 +00:00
|
|
|
}
|
2007-04-05 12:36:57 +00:00
|
|
|
|
2007-05-16 02:09:09 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2004-11-03 13:51:07 +00:00
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH "SEE ALSO"
|
2007-06-01 06:26:42 +00:00
|
|
|
.BR getgroups (2),
|
2008-06-30 14:44:09 +00:00
|
|
|
.BR setgroups (2),
|
|
|
|
.BR getgrent (3),
|
2008-07-03 15:20:50 +00:00
|
|
|
.BR group (5),
|
|
|
|
.BR passwd (5)
|