⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 users.texi

📁 一个C源代码分析器
💻 TEXI
📖 第 1 页 / 共 3 页
字号:
@node Users and Groups, System Information, Job Control, Top@chapter Users and GroupsEvery user who can log in on the system is identified by a unique numbercalled the @dfn{user ID}.  Each process has an effective user ID whichsays which user's access permissions it has.Users are classified into @dfn{groups} for access control purposes.  Eachprocess has one or more @dfn{group ID values} which say which groups theprocess can use for access to files.The effective user and group IDs of a process collectively form its@dfn{persona}.  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.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.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.@menu* User and Group IDs::          Each user has a unique numeric ID;				 likewise for groups.* Process Persona::             The user IDs and group IDs of a process.* Why Change Persona::          Why a program might need to change				 its user and/or group IDs.* How Change Persona::          Changing the user and group IDs.* Reading Persona::             How to examine the user and group IDs.* Setting User ID::             Functions for setting the user ID.* Setting Groups::              Functions for setting the group IDs.* Enable/Disable Setuid::       Turning setuid access on and off.* Setuid Program Example::      The pertinent parts of one sample program.* Tips for Setuid::             How to avoid granting unlimited access.* Who Logged In::               Getting the name of the user who logged in,				 or of the real user ID of the current process.* User Database::               Functions and data structures for                        	 accessing the user database.* Group Database::              Functions and data structures for                        	 accessing the group database.* Database Example::            Example program showing use of database				 inquiry functions.@end menu@node User and Group IDs@section User and Group IDs@cindex login name@cindex user name@cindex user IDEach user account on a computer system is identified by a @dfn{username} (or @dfn{login name}) and @dfn{user ID}.  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 @ref{User Database}.@cindex group name@cindex group IDUsers are classified in @dfn{groups}.  Each user name also belongs toone or more groups, and has one @dfn{default group}.  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} and @dfn{group ID}.  @xref{Group Database},for how to find information about a group ID or group name.@node Process Persona@section The Persona of a Process@cindex persona@cindex effective user ID@cindex effective group ID@c !!! bogus; not single ID.  set of effective group IDs (and, in GNU,@c set of effective UIDs) determines privilege.  lying here and then@c telling the truth below is confusing.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} of the process, because they determine ``who it is''for purposes of access control.  These IDs are also called the@dfn{effective user ID} and @dfn{effective group ID} of the process.Your login shell starts out with a persona which consists of your userID and your default group ID.  @c !!! also supplementary group IDs.In normal circumstances, all your other processes inherit these values.@cindex real user ID@cindex real group IDA process also has a @dfn{real user ID} which identifies the user whocreated the process, and a @dfn{real group ID} 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.Both the real and effective user ID can be changed during the lifetimeof a process.  @xref{Why Change Persona}.@cindex supplementary group IDsIn addition, a user can belong to multiple groups, so the personaincludes @dfn{supplementary group IDs} that also contribute to accesspermission.For details on how a process's effective user IDs and group IDs affectits permission to access files, see @ref{Access Permission}.The user ID of a process also controls permissions for sending signalsusing the @code{kill} function.  @xref{Signaling Another Process}.@node Why Change Persona@section Why Change the Persona of a Process?The most obvious situation where it is necessary for a process to changeits user and/or group IDs is the @code{login} program.  When@code{login} starts running, its user ID is @code{root}.  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} must set the realuser and group IDs as well as its persona.  But this is a special case.)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.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.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}) 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}.  In effect, the program mustadopt the persona of @code{games} so it can write the scores file.@node How Change Persona@section How an Application Can Change Persona@cindex @code{setuid} programsThe 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.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.  @xref{Permission Bits}.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.  @xref{Executing a File}.Creating a file that changes to a particular user or group ID thusrequires full access to that user or group ID.@xref{File Attributes}, for a more general discussion of file modes andaccessibility.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.@c !!! talk about _POSIX_SAVED_IDS@node Reading Persona@section Reading the Persona of a ProcessHere 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 @file{sys/types.h} and@file{unistd.h}.@pindex unistd.h@pindex sys/types.h@comment sys/types.h@comment POSIX.1@deftp {Data Type} uid_tThis is an integer data type used to represent user IDs.  In the GNUlibrary, this is an alias for @code{unsigned int}.@end deftp@comment sys/types.h@comment POSIX.1@deftp {Data Type} gid_tThis is an integer data type used to represent group IDs.  In the GNUlibrary, this is an alias for @code{unsigned int}.@end deftp@comment unistd.h@comment POSIX.1@deftypefun uid_t getuid (void)The @code{getuid} function returns the real user ID of the process.@end deftypefun@comment unistd.h@comment POSIX.1@deftypefun gid_t getgid (void)The @code{getgid} function returns the real group ID of the process.@end deftypefun@comment unistd.h@comment POSIX.1@deftypefun uid_t geteuid (void)The @code{geteuid} function returns the effective user ID of the process.@end deftypefun@comment unistd.h@comment POSIX.1@deftypefun gid_t getegid (void)The @code{getegid} function returns the effective group ID of the process.@end deftypefun@comment unistd.h@comment POSIX.1@deftypefun int getgroups (int @var{count}, gid_t *@var{groups})The @code{getgroups} function is used to inquire about the supplementarygroup IDs of the process.  Up to @var{count} of these group IDs arestored in the array @var{groups}; the return value from the function isthe number of group IDs actually stored.  If @var{count} is smaller thanthe total number of supplementary group IDs, then @code{getgroups}returns a value of @code{-1} and @code{errno} is set to @code{EINVAL}.If @var{count} is zero, then @code{getgroups} just returns the totalnumber of supplementary group IDs.  On systems that do not supportsupplementary groups, this will always be zero.Here's how to use @code{getgroups} to read all the supplementary groupIDs:@smallexample@groupgid_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;@}@end group@end smallexample@end deftypefun@node Setting User ID@section Setting the User IDThis section describes the functions for altering the user ID (realand/or effective) of a process.  To use these facilities, you mustinclude the header files @file{sys/types.h} and @file{unistd.h}.@pindex unistd.h@pindex sys/types.h@comment unistd.h@comment POSIX.1@deftypefun int setuid (uid_t @var{newuid})This function sets both the real and effective user ID of the processto @var{newuid}, provided that the process has appropriate privileges.@c !!! also sets saved-idIf the process is not privileged, then @var{newuid} must either be equalto the real user ID or the saved user ID (if the system supports the@code{_POSIX_SAVED_IDS} feature).  In this case, @code{setuid} sets onlythe effective user ID and not the real user ID.@c !!! xref to discussion of _POSIX_SAVED_IDSThe @code{setuid} function returns a value of @code{0} to indicatesuccessful completion, and a value of @code{-1} to indicate an error.The following @code{errno} error conditions are defined for thisfunction:@table @code@item EINVALThe value of the @var{newuid} argument is invalid.@item EPERMThe process does not have the appropriate privileges; you do nothave permission to change to the specified ID.@end table@end deftypefun@comment unistd.h@comment BSD@deftypefun int setreuid (uid_t @var{ruid}, uid_t @var{euid})This function sets the real user ID of the process to @var{ruid} and theeffective user ID to @var{euid}.  If @var{ruid} is @code{-1}, it meansnot to change the real user ID; likewise if @var{euid} is @code{-1}, itmeans not to change the effective user ID.The @code{setreuid} 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.  @xref{Enable/DisableSetuid}.The return value is @code{0} on success and @code{-1} on failure.The following @code{errno} error conditions are defined for thisfunction:@table @code@item EPERMThe process does not have the appropriate privileges; you do nothave permission to change to the specified ID.@end table@end deftypefun@node Setting Groups@section Setting the Group IDsThis section describes the functions for altering the group IDs (realand effective) of a process.  To use these facilities, you must includethe header files @file{sys/types.h} and @file{unistd.h}.@pindex unistd.h@pindex sys/types.h@comment unistd.h@comment POSIX.1@deftypefun int setgid (gid_t @var{newgid})

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -