📄 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 numbercalled the <DFN>user ID</DFN>. Each process has an effective user ID whichsays which user's access permissions it has.<P>Users are classified into <DFN>groups</DFN> for access control purposes. Eachprocess has one or more <DFN>group ID values</DFN> which say which groups theprocess 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, butunder special circumstances a process can change its persona and thuschange its access permissions.<P>Each file in the system also has a user ID and a group ID. Accesscontrol works by comparing the user and group IDs of the file with thoseof the running process.<P>The system keeps a database of all the registered users, and anotherdatabase of all the defined groups. There are library functions youcan 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>username</DFN> (or <DFN>login name</DFN>) and <DFN>user ID</DFN>. Normally, each user namehas a unique user ID, but it is possible for several login names to havethe same user ID. The user names and corresponding user IDs are storedin 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 toone or more groups, and has one <DFN>default group</DFN>. Users who aremembers of the same group can share resources (such as files) that arenot accessible to users who are not a member of that group. Each grouphas 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 whichdetermine the privileges of the process. These are collectively calledthe <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 userID 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 whocreated the process, and a <DFN>real group ID</DFN> which identifies thatuser's default group. These values do not play a role in accesscontrol, so we do not consider them part of the persona. But they arealso important.<P>Both the real and effective user ID can be changed during the lifetimeof 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 personaincludes <DFN>supplementary group IDs</DFN> that also contribute to accesspermission.<P>For details on how a process's effective user IDs and group IDs affectits 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 signalsusing 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 changeits 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 tostart a shell whose user and group IDs are those of the user who islogging in. (To accomplish this fully, <CODE>login</CODE> must set the realuser 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 userprogram needs access to a resource that wouldn't ordinarily beaccessible to the user actually running it.<P>For example, you may have a file that is controlled by your program butthat shouldn't be read or modified directly by other users, eitherbecause it implements some kind of locking protocol, or because you wantto preserve the integrity or privacy of the information it contains.This kind of restricted access can be implemented by having the programchange 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 gameprogram itself needs to be able to update this file no matter who isrunning it, but if users can write the file without going through thegame, they can give themselves any scores they like. Some peopleconsider this undesirable, or even reprehensible. It can be preventedby creating a new user ID and login name (say, <CODE>games</CODE>) to own thescores file, and make the file writable only by this user. Then, whenthe game program wants to update this file, it can change its effectiveuser ID to be that for <CODE>games</CODE>. In effect, the program mustadopt 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 ofunintentional privacy violations, or even intentional abuse. Because ofthe potential for problems, changing persona is restricted to specialcircumstances.<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 aprogram to change its persona is that it has been set up in advance tochange to a particular user or group. This is the function of the setuidand 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 fileautomatically changes the effective user ID to the user that owns thefile. Likewise, executing a file whose setgid bit is set changes theeffective 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 thusrequires 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 andaccessibility.<P>A process can always change its effective user (or group) ID back to itsreal ID. Programs do this so as to turn off their special privilegeswhen 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 andgroup IDs of a process, both real and effective. To use thesefacilities, 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 GNUlibrary, 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 GNUlibrary, 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 supplementarygroup IDs of the process. Up to <VAR>count</VAR> of these group IDs arestored in the array <VAR>groups</VAR>; the return value from the function isthe number of group IDs actually stored. If <VAR>count</VAR> is smaller thanthe 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 totalnumber of supplementary group IDs. On systems that do not supportsupplementary groups, this will always be zero.<P>Here's how to use <CODE>getgroups</CODE> to read all the supplementary groupIDs:<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 (realand/or effective) of a process. To use these facilities, you mustinclude 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 processto <VAR>newuid</VAR>, provided that the process has appropriate privileges.<P>If the process is not privileged, then <VAR>newuid</VAR> must either be equalto 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 onlythe effective user ID and not the real user ID.<P>The <CODE>setuid</CODE> function returns a value of <CODE>0</CODE> to indicatesuccessful completion, and a value of <CODE>-1</CODE> to indicate an error.The following <CODE>errno</CODE> error conditions are defined for thisfunction:<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 nothave 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> andthe 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 theeffective and real user IDs of the process. (Privileged processes arenot limited to this particular usage.) If saved IDs are supported, youshould 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 thisfunction:<P><DL COMPACT><DT><CODE>EPERM</CODE><DD>The process does not have the appropriate privileges; you do nothave 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 (realand effective) of a process. To use these facilities, you must includethe 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 processto <VAR>newgid</VAR>, provided that the process has appropriate privileges.<P>If the process is not privileged, then <VAR>newgid</VAR> must either be equalto 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 sameas 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> andthe effective group ID to <VAR>egid</VAR>.<P>The <CODE>setregid</CODE> function is provided for compatibility with 4.3 BSDUnix, which does not support saved IDs. You can use this function toswap the effective and real group IDs of the process. (Privilegedprocesses 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 sameas 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 programsshould 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 onlybe called from privileged processes. The <VAR>count</VAR> argument specifies
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -