📄 library_25.html
字号:
<!-- This HTML file has been created by texi2html 1.27
from library.texinfo on 3 March 1994 -->
<TITLE>The GNU C Library - Users and Groups</TITLE>
<P>Go to the <A HREF="library_24.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_24.html">previous</A>, <A HREF="library_26.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_26.html">next</A> section.<P>
<H1><A NAME="SEC429" HREF="library_toc.html#SEC429" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC429">Users and Groups</A></H1>
<P>
Every user who can log in on the system is identified by a unique number
called the <DFN>user ID</DFN>. Each process has an effective user ID which
says which user's access permissions it has.
<P>
Users are classified into <DFN>groups</DFN> for access control purposes. Each
process has one or more <DFN>group ID values</DFN> which say which groups the
process can use for access to files.
<P>
The effective user and group IDs of a process collectively form its
<DFN>persona</DFN>. This determines which files the process can access.
Normally, a process inherits its persona from the parent process, but
under special circumstances a process can change its persona and thus
change its access permissions.
<P>
Each file in the system also has a user ID and a group ID. Access
control works by comparing the user and group IDs of the file with those
of the running process.
<P>
The system keeps a database of all the registered users, and another
database of all the defined groups. There are library functions you
can use to examine these databases.
<P>
<H2><A NAME="SEC430" HREF="library_toc.html#SEC430" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC430">User and Group IDs</A></H2>
<A NAME="IDX1779"></A>
<A NAME="IDX1780"></A>
<A NAME="IDX1781"></A>
<P>
Each user account on a computer system is identified by a <DFN>user
name</DFN> (or <DFN>login name</DFN>) and <DFN>user ID</DFN>. Normally, each user name
has a unique user ID, but it is possible for several login names to have
the same user ID. The user names and corresponding user IDs are stored
in a data base which you can access as described in section <A HREF="library_25.html#SEC441" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC441">User Database</A>.
<A NAME="IDX1782"></A>
<A NAME="IDX1783"></A>
<P>
Users are classified in <DFN>groups</DFN>. Each user name also belongs to
one or more groups, and has one <DFN>default group</DFN>. Users who are
members of the same group can share resources (such as files) that are
not accessible to users who are not a member of that group. Each group
has a <DFN>group name</DFN> and <DFN>group ID</DFN>. See section <A HREF="library_25.html#SEC446" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC446">Group Database</A>,
for how to find information about a group ID or group name.
<P>
<A NAME="IDX1784"></A>
<A NAME="IDX1785"></A>
<A NAME="IDX1786"></A>
<H2><A NAME="SEC431" HREF="library_toc.html#SEC431" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC431">The Persona of a Process</A></H2>
<P>
At any time, each process has a single user ID and a group ID which
determine the privileges of the process. These are collectively called
the <DFN>persona</DFN> of the process, because they determine "who it is"
for purposes of access control. These IDs are also called the
<DFN>effective user ID</DFN> and <DFN>effective group ID</DFN> of the process.
<P>
Your login shell starts out with a persona which consists of your user
ID and your default group ID.
In normal circumstances, all your other processes inherit these values.
<A NAME="IDX1787"></A>
<A NAME="IDX1788"></A>
<P>
A process also has a <DFN>real user ID</DFN> which identifies the user who
created the process, and a <DFN>real group ID</DFN> which identifies that
user's default group. These values do not play a role in access
control, so we do not consider them part of the persona. But they are
also important.
<P>
Both the real and effective user ID can be changed during the lifetime
of a process. See section <A HREF="library_25.html#SEC432" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC432">Why Change the Persona of a Process?</A>.
<A NAME="IDX1789"></A>
<P>
In addition, a user can belong to multiple groups, so the persona
includes <DFN>supplementary group IDs</DFN> that also contribute to access
permission.
<P>
For details on how a process's effective user IDs and group IDs affect
its permission to access files, see section <A HREF="library_13.html#SEC206" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC206">How Your Access to a File is Decided</A>.
<P>
The user ID of a process also controls permissions for sending signals
using the <CODE>kill</CODE> function. See section <A HREF="library_21.html#SEC365" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_21.html#SEC365">Signaling Another Process</A>.
<P>
<H2><A NAME="SEC432" HREF="library_toc.html#SEC432" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC432">Why Change the Persona of a Process?</A></H2>
<P>
The most obvious situation where it is necessary for a process to change
its user and/or group IDs is the <CODE>login</CODE> program. When
<CODE>login</CODE> starts running, its user ID is <CODE>root</CODE>. Its job is to
start a shell whose user and group IDs are those of the user who is
logging in. (To accomplish this fully, <CODE>login</CODE> must set the real
user and group IDs as well as its persona. But this is a special case.)
<P>
The more common case of changing persona is when an ordinary user
program needs access to a resource that wouldn't ordinarily be
accessible to the user actually running it.
<P>
For example, you may have a file that is controlled by your program but
that shouldn't be read or modified directly by other users, either
because it implements some kind of locking protocol, or because you want
to preserve the integrity or privacy of the information it contains.
This kind of restricted access can be implemented by having the program
change its effective user or group ID to match that of the resource.
<P>
Thus, imagine a game program that saves scores in a file. The game
program itself needs to be able to update this file no matter who is
running it, but if users can write the file without going through the
game, they can give themselves any scores they like. Some people
consider this undesirable, or even reprehensible. It can be prevented
by creating a new user ID and login name (say, <CODE>games</CODE>) to own the
scores file, and make the file writable only by this user. Then, when
the game program wants to update this file, it can change its effective
user ID to be that for <CODE>games</CODE>. In effect, the program must
adopt the persona of <CODE>games</CODE> so it can write the scores file.
<P>
<A NAME="IDX1790"></A>
<H2><A NAME="SEC433" HREF="library_toc.html#SEC433" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC433">How an Application Can Change Persona</A></H2>
<P>
The ability to change the persona of a process can be a source of
unintentional privacy violations, or even intentional abuse. Because of
the potential for problems, changing persona is restricted to special
circumstances.
<P>
You can't arbitrarily set your user ID or group ID to anything you want;
only privileged processes can do that. Instead, the normal way for a
program to change its persona is that it has been set up in advance to
change to a particular user or group. This is the function of the setuid
and setgid bits of a file's access mode. See section <A HREF="library_13.html#SEC205" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC205">The Mode Bits for Access Permission</A>.
<P>
When the setuid bit of an executable file is set, executing that file
automatically changes the effective user ID to the user that owns the
file. Likewise, executing a file whose setgid bit is set changes the
effective group ID to the group of the file. See section <A HREF="library_23.html#SEC406" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC406">Executing a File</A>.
Creating a file that changes to a particular user or group ID thus
requires full access to that user or group ID.
<P>
See section <A HREF="library_13.html#SEC200" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_13.html#SEC200">File Attributes</A>, for a more general discussion of file modes and
accessibility.
<P>
A process can always change its effective user (or group) ID back to its
real ID. Programs do this so as to turn off their special privileges
when they are not needed, which makes for more robustness.
<P>
<H2><A NAME="SEC434" HREF="library_toc.html#SEC434" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC434">Reading the Persona of a Process</A></H2>
<P>
Here are detailed descriptions of the functions for reading the user and
group IDs of a process, both real and effective. To use these
facilities, you must include the header files <TT>`sys/types.h'</TT> and
<TT>`unistd.h'</TT>.
<A NAME="IDX1792"></A>
<A NAME="IDX1791"></A>
<P>
<A NAME="IDX1793"></A>
<U>Data Type:</U> <B>uid_t</B><P>
This is an integer data type used to represent user IDs. In the GNU
library, this is an alias for <CODE>unsigned int</CODE>.
<P>
<A NAME="IDX1794"></A>
<U>Data Type:</U> <B>gid_t</B><P>
This is an integer data type used to represent group IDs. In the GNU
library, this is an alias for <CODE>unsigned int</CODE>.
<P>
<A NAME="IDX1795"></A>
<U>Function:</U> uid_t <B>getuid</B> <I>(void)</I><P>
The <CODE>getuid</CODE> function returns the real user ID of the process.
<P>
<A NAME="IDX1796"></A>
<U>Function:</U> gid_t <B>getgid</B> <I>(void)</I><P>
The <CODE>getgid</CODE> function returns the real group ID of the process.
<P>
<A NAME="IDX1797"></A>
<U>Function:</U> uid_t <B>geteuid</B> <I>(void)</I><P>
The <CODE>geteuid</CODE> function returns the effective user ID of the process.
<P>
<A NAME="IDX1798"></A>
<U>Function:</U> gid_t <B>getegid</B> <I>(void)</I><P>
The <CODE>getegid</CODE> function returns the effective group ID of the process.
<P>
<A NAME="IDX1799"></A>
<U>Function:</U> int <B>getgroups</B> <I>(int <VAR>count</VAR>, gid_t *<VAR>groups</VAR>)</I><P>
The <CODE>getgroups</CODE> function is used to inquire about the supplementary
group IDs of the process. Up to <VAR>count</VAR> of these group IDs are
stored in the array <VAR>groups</VAR>; the return value from the function is
the number of group IDs actually stored. If <VAR>count</VAR> is smaller than
the total number of supplementary group IDs, then <CODE>getgroups</CODE>
returns a value of <CODE>-1</CODE> and <CODE>errno</CODE> is set to <CODE>EINVAL</CODE>.
<P>
If <VAR>count</VAR> is zero, then <CODE>getgroups</CODE> just returns the total
number of supplementary group IDs. On systems that do not support
supplementary groups, this will always be zero.
<P>
Here's how to use <CODE>getgroups</CODE> to read all the supplementary group
IDs:
<P>
<PRE>
gid_t *
read_all_groups (void)
{
int ngroups = getgroups (NULL, 0);
gid_t *groups = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
int val = getgroups (ngroups, groups);
if (val < 0)
{
free (groups);
return NULL;
}
return groups;
}
</PRE>
<P>
<H2><A NAME="SEC435" HREF="library_toc.html#SEC435" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC435">Setting the User ID</A></H2>
<P>
This section describes the functions for altering the user ID (real
and/or effective) of a process. To use these facilities, you must
include the header files <TT>`sys/types.h'</TT> and <TT>`unistd.h'</TT>.
<A NAME="IDX1801"></A>
<A NAME="IDX1800"></A>
<P>
<A NAME="IDX1802"></A>
<U>Function:</U> int <B>setuid</B> <I>(uid_t <VAR>newuid</VAR>)</I><P>
This function sets both the real and effective user ID of the process
to <VAR>newuid</VAR>, provided that the process has appropriate privileges.
<P>
If the process is not privileged, then <VAR>newuid</VAR> must either be equal
to the real user ID or the saved user ID (if the system supports the
<CODE>_POSIX_SAVED_IDS</CODE> feature). In this case, <CODE>setuid</CODE> sets only
the effective user ID and not the real user ID.
<P>
The <CODE>setuid</CODE> function returns a value of <CODE>0</CODE> to indicate
successful completion, and a value of <CODE>-1</CODE> to indicate an error.
The following <CODE>errno</CODE> error conditions are defined for this
function:
<P>
<DL COMPACT>
<DT><CODE>EINVAL</CODE>
<DD>The value of the <VAR>newuid</VAR> argument is invalid.
<P>
<DT><CODE>EPERM</CODE>
<DD>The process does not have the appropriate privileges; you do not
have permission to change to the specified ID.
</DL>
<P>
<A NAME="IDX1803"></A>
<U>Function:</U> int <B>setreuid</B> <I>(uid_t <VAR>ruid</VAR>, uid_t <VAR>euid</VAR>)</I><P>
This function sets the real user ID of the process to <VAR>ruid</VAR> and
the effective user ID to <VAR>euid</VAR>.
<P>
The <CODE>setreuid</CODE> function exists for compatibility with 4.3 BSD Unix,
which does not support saved IDs. You can use this function to swap the
effective and real user IDs of the process. (Privileged processes are
not limited to this particular usage.) If saved IDs are supported, you
should use that feature instead of this function. See section <A HREF="library_25.html#SEC437" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC437">Enabling and Disabling Setuid Access</A>.
<P>
The return value is <CODE>0</CODE> on success and <CODE>-1</CODE> on failure.
The following <CODE>errno</CODE> error conditions are defined for this
function:
<P>
<DL COMPACT>
<DT><CODE>EPERM</CODE>
<DD>The process does not have the appropriate privileges; you do not
have permission to change to the specified ID.
</DL>
<P>
<H2><A NAME="SEC436" HREF="library_toc.html#SEC436" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC436">Setting the Group IDs</A></H2>
<P>
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 <TT>`sys/types.h'</TT> and <TT>`unistd.h'</TT>.
<A NAME="IDX1805"></A>
<A NAME="IDX1804"></A>
<P>
<A NAME="IDX1806"></A>
<U>Function:</U> int <B>setgid</B> <I>(gid_t <VAR>newgid</VAR>)</I><P>
This function sets both the real and effective group ID of the process
to <VAR>newgid</VAR>, provided that the process has appropriate privileges.
<P>
If the process is not privileged, then <VAR>newgid</VAR> must either be equal
to the real group ID or the saved group ID. In this case, <CODE>setgid</CODE>
sets only the effective group ID and not the real group ID.
<P>
The return values and error conditions for <CODE>setgid</CODE> are the same
as those for <CODE>setuid</CODE>.
<P>
<A NAME="IDX1807"></A>
<U>Function:</U> int <B>setregid</B> <I>(gid_t <VAR>rgid</VAR>, fid_t <VAR>egid</VAR>)</I><P>
This function sets the real group ID of the process to <VAR>rgid</VAR> and
the effective group ID to <VAR>egid</VAR>.
<P>
The <CODE>setregid</CODE> function is provided for compatibility with 4.3 BSD
Unix, which does not support saved IDs. You can use this function to
swap the effective and real group IDs of the process. (Privileged
processes are not limited to this usage.) If saved IDs are supported,
you should use that feature instead of using this function.
See section <A HREF="library_25.html#SEC437" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_25.html#SEC437">Enabling and Disabling Setuid Access</A>.
<P>
The return values and error conditions for <CODE>setregid</CODE> are the same
as those for <CODE>setreuid</CODE>.
<P>
The GNU system also lets privileged processes change their supplementary
group IDs. To use <CODE>setgroups</CODE> or <CODE>initgroups</CODE>, your programs
should include the header file <TT>`grp.h'</TT>.
<A NAME="IDX1808"></A>
<P>
<A NAME="IDX1809"></A>
<U>Function:</U> int <B>setgroups</B> <I>(size_t <VAR>count</VAR>, gid_t *<VAR>groups</VAR>)</I><P>
This function sets the process's supplementary group IDs. It can only
be called from privileged processes. The <VAR>count</VAR> argument specifies
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -