Node:Setting Groups, Next:Enable/Disable Setuid, Previous:Setting User ID, Up:Users and Groups
This section describes the functions for altering the group IDs (real
and effective) of a process. To use these facilities, you must include
the header files sys/types.h
and unistd.h
.
int setegid (gid_t newgid) | Function |
This function sets the effective group ID of the process to
newgid, provided that the process is allowed to change its group
ID. Just as with seteuid , if the process is privileged it may
change its effective group ID to any value; if it isn't, but it has a
file group ID, then it may change to its real group ID or file group ID;
otherwise it may not change its effective group ID.
Note that a process is only privileged if its effective user ID is zero. The effective group ID only affects access permissions. The return values and error conditions for This function is only present if |
int setgid (gid_t newgid) | Function |
This function sets both the real and effective group ID of the process
to newgid, provided that the process is privileged. It also
deletes the file group ID, if any.
If the process is not privileged, then The return values and error conditions for |
int setregid (gid_t rgid, gid_t egid) | Function |
This function sets the real group ID of the process to rgid and
the effective group ID to egid. If rgid is -1 , it
means not to change the real group ID; likewise if egid is
-1 , it means not to change the effective group ID.
The The return values and error conditions for |
setuid
and setgid
behave differently depending on whether
the effective user ID at the time is zero. If it is not zero, they
behave like seteuid
and setegid
. If it is, they change
both effective and real IDs and delete the file ID. To avoid confusion,
we recommend you always use seteuid
and setegid
except
when you know the effective user ID is zero and your intent is to change
the persona permanently. This case is rare--most of the programs that
need it, such as login
and su
, have already been written.
Note that if your program is setuid to some user other than root
,
there is no way to drop privileges permanently.
The system also lets privileged processes change their supplementary
group IDs. To use setgroups
or initgroups
, your programs
should include the header file grp.h
.
int setgroups (size_t count, gid_t *groups) | Function |
This function sets the process's supplementary group IDs. It can only
be called from privileged processes. The count argument specifies
the number of group IDs in the array groups.
This function returns
|
int initgroups (const char *user, gid_t group) | Function |
The initgroups function sets the process's supplementary group
IDs to be the normal default for the user name user. The group
group is automatically included.
This function works by scanning the group database for all the groups
user belongs to. It then calls The return values and error conditions are the same as for
|
If you are interested in the groups a particular user belongs to, but do
not want to change the process's supplementary group IDs, you can use
getgrouplist
. To use getgrouplist
, your programs should
include the header file grp.h
.
int getgrouplist (const char *user, gid_t group, gid_t *groups, int *ngroups) | Function |
The getgrouplist function scans the group database for all the
groups user belongs to. Up to *ngroups group IDs
corresponding to these groups are stored in the array groups; the
return value from the function is the number of group IDs actually
stored. If *ngroups is smaller than the total number of groups
found, then getgrouplist returns a value of -1 and stores
the actual number of groups in *ngroups. The group group is
automatically included in the list of groups returned by
getgrouplist .
Here's how to use gid_t * supplementary_groups (char *user) { int ngroups = 16; gid_t *groups = (gid_t *) xmalloc (ngroups * sizeof (gid_t)); struct passwd *pw = getpwnam (user); if (pw == NULL) return NULL; if (getgrouplist (pw->pw_name, pw->pw_gid, groups, &ngroups) < 0) { groups = xrealloc (ngroups * sizeof (gid_t)); getgrouplist (pw->pw_name, pw->pw_gid, groups, &ngroups); } return groups; } |