users.texi 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765
  1. @node Users and Groups, System Management, Name Service Switch, Top
  2. @c %MENU% How users are identified and classified
  3. @chapter Users and Groups
  4. Every user who can log in on the system is identified by a unique number
  5. called the @dfn{user ID}. Each process has an effective user ID which
  6. says which user's access permissions it has.
  7. Users are classified into @dfn{groups} for access control purposes. Each
  8. process has one or more @dfn{group ID values} which say which groups the
  9. process can use for access to files.
  10. The effective user and group IDs of a process collectively form its
  11. @dfn{persona}. This determines which files the process can access.
  12. Normally, a process inherits its persona from the parent process, but
  13. under special circumstances a process can change its persona and thus
  14. change its access permissions.
  15. Each file in the system also has a user ID and a group ID. Access
  16. control works by comparing the user and group IDs of the file with those
  17. of the running process.
  18. The system keeps a database of all the registered users, and another
  19. database of all the defined groups. There are library functions you
  20. can use to examine these databases.
  21. @menu
  22. * User and Group IDs:: Each user has a unique numeric ID;
  23. likewise for groups.
  24. * Process Persona:: The user IDs and group IDs of a process.
  25. * Why Change Persona:: Why a program might need to change
  26. its user and/or group IDs.
  27. * How Change Persona:: Changing the user and group IDs.
  28. * Reading Persona:: How to examine the user and group IDs.
  29. * Setting User ID:: Functions for setting the user ID.
  30. * Setting Groups:: Functions for setting the group IDs.
  31. * Enable/Disable Setuid:: Turning setuid access on and off.
  32. * Setuid Program Example:: The pertinent parts of one sample program.
  33. * Tips for Setuid:: How to avoid granting unlimited access.
  34. * Who Logged In:: Getting the name of the user who logged in,
  35. or of the real user ID of the current process.
  36. * User Accounting Database:: Keeping information about users and various
  37. actions in databases.
  38. * User Database:: Functions and data structures for
  39. accessing the user database.
  40. * Group Database:: Functions and data structures for
  41. accessing the group database.
  42. * Database Example:: Example program showing the use of database
  43. inquiry functions.
  44. * Netgroup Database:: Functions for accessing the netgroup database.
  45. @end menu
  46. @node User and Group IDs
  47. @section User and Group IDs
  48. @cindex login name
  49. @cindex user name
  50. @cindex user ID
  51. Each user account on a computer system is identified by a @dfn{user
  52. name} (or @dfn{login name}) and @dfn{user ID}. Normally, each user name
  53. has a unique user ID, but it is possible for several login names to have
  54. the same user ID. The user names and corresponding user IDs are stored
  55. in a data base which you can access as described in @ref{User Database}.
  56. @cindex group name
  57. @cindex group ID
  58. Users are classified in @dfn{groups}. Each user name belongs to one
  59. @dfn{default group} and may also belong to any number of
  60. @dfn{supplementary groups}. Users who are members of the same group can
  61. share resources (such as files) that are not accessible to users who are
  62. not a member of that group. Each group has a @dfn{group name} and
  63. @dfn{group ID}. @xref{Group Database}, for how to find information
  64. about a group ID or group name.
  65. @node Process Persona
  66. @section The Persona of a Process
  67. @cindex persona
  68. @cindex effective user ID
  69. @cindex effective group ID
  70. @cindex supplementary group IDs
  71. @c When Hurd is more widely used, explain multiple effective user IDs
  72. @c here. -zw
  73. At any time, each process has an @dfn{effective user ID}, a @dfn{effective
  74. group ID}, and a set of @dfn{supplementary group IDs}. These IDs
  75. determine the privileges of the process. They are collectively
  76. called the @dfn{persona} of the process, because they determine ``who it
  77. is'' for purposes of access control.
  78. Your login shell starts out with a persona which consists of your user
  79. ID, your default group ID, and your supplementary group IDs (if you are
  80. in more than one group). In normal circumstances, all your other processes
  81. inherit these values.
  82. @cindex real user ID
  83. @cindex real group ID
  84. A process also has a @dfn{real user ID} which identifies the user who
  85. created the process, and a @dfn{real group ID} which identifies that
  86. user's default group. These values do not play a role in access
  87. control, so we do not consider them part of the persona. But they are
  88. also important.
  89. Both the real and effective user ID can be changed during the lifetime
  90. of a process. @xref{Why Change Persona}.
  91. For details on how a process's effective user ID and group IDs affect
  92. its permission to access files, see @ref{Access Permission}.
  93. The effective user ID of a process also controls permissions for sending
  94. signals using the @code{kill} function. @xref{Signaling Another
  95. Process}.
  96. Finally, there are many operations which can only be performed by a
  97. process whose effective user ID is zero. A process with this user ID is
  98. a @dfn{privileged process}. Commonly the user name @code{root} is
  99. associated with user ID 0, but there may be other user names with this
  100. ID.
  101. @c !!! should mention POSIX capabilities here.
  102. @node Why Change Persona
  103. @section Why Change the Persona of a Process?
  104. The most obvious situation where it is necessary for a process to change
  105. its user and/or group IDs is the @code{login} program. When
  106. @code{login} starts running, its user ID is @code{root}. Its job is to
  107. start a shell whose user and group IDs are those of the user who is
  108. logging in. (To accomplish this fully, @code{login} must set the real
  109. user and group IDs as well as its persona. But this is a special case.)
  110. The more common case of changing persona is when an ordinary user
  111. program needs access to a resource that wouldn't ordinarily be
  112. accessible to the user actually running it.
  113. For example, you may have a file that is controlled by your program but
  114. that shouldn't be read or modified directly by other users, either
  115. because it implements some kind of locking protocol, or because you want
  116. to preserve the integrity or privacy of the information it contains.
  117. This kind of restricted access can be implemented by having the program
  118. change its effective user or group ID to match that of the resource.
  119. Thus, imagine a game program that saves scores in a file. The game
  120. program itself needs to be able to update this file no matter who is
  121. running it, but if users can write the file without going through the
  122. game, they can give themselves any scores they like. Some people
  123. consider this undesirable, or even reprehensible. It can be prevented
  124. by creating a new user ID and login name (say, @code{games}) to own the
  125. scores file, and make the file writable only by this user. Then, when
  126. the game program wants to update this file, it can change its effective
  127. user ID to be that for @code{games}. In effect, the program must
  128. adopt the persona of @code{games} so it can write to the scores file.
  129. @node How Change Persona
  130. @section How an Application Can Change Persona
  131. @cindex @code{setuid} programs
  132. @cindex saved set-user-ID
  133. @cindex saved set-group-ID
  134. @cindex @code{_POSIX_SAVED_IDS}
  135. The ability to change the persona of a process can be a source of
  136. unintentional privacy violations, or even intentional abuse. Because of
  137. the potential for problems, changing persona is restricted to special
  138. circumstances.
  139. You can't arbitrarily set your user ID or group ID to anything you want;
  140. only privileged processes can do that. Instead, the normal way for a
  141. program to change its persona is that it has been set up in advance to
  142. change to a particular user or group. This is the function of the setuid
  143. and setgid bits of a file's access mode. @xref{Permission Bits}.
  144. When the setuid bit of an executable file is on, executing that file
  145. gives the process a third user ID: the @dfn{file user ID}. This ID is
  146. set to the owner ID of the file. The system then changes the effective
  147. user ID to the file user ID. The real user ID remains as it was.
  148. Likewise, if the setgid bit is on, the process is given a @dfn{file
  149. group ID} equal to the group ID of the file, and its effective group ID
  150. is changed to the file group ID.
  151. If a process has a file ID (user or group), then it can at any time
  152. change its effective ID to its real ID and back to its file ID.
  153. Programs use this feature to relinquish their special privileges except
  154. when they actually need them. This makes it less likely that they can
  155. be tricked into doing something inappropriate with their privileges.
  156. @strong{Portability Note:} Older systems do not have file IDs.
  157. To determine if a system has this feature, you can test the compiler
  158. define @code{_POSIX_SAVED_IDS}. (In the POSIX standard, file IDs are
  159. known as saved IDs.)
  160. @xref{File Attributes}, for a more general discussion of file modes and
  161. accessibility.
  162. @node Reading Persona
  163. @section Reading the Persona of a Process
  164. Here are detailed descriptions of the functions for reading the user and
  165. group IDs of a process, both real and effective. To use these
  166. facilities, you must include the header files @file{sys/types.h} and
  167. @file{unistd.h}.
  168. @pindex unistd.h
  169. @pindex sys/types.h
  170. @deftp {Data Type} uid_t
  171. @standards{POSIX.1, sys/types.h}
  172. This is an integer data type used to represent user IDs. In
  173. @theglibc{}, this is an alias for @code{unsigned int}.
  174. @end deftp
  175. @deftp {Data Type} gid_t
  176. @standards{POSIX.1, sys/types.h}
  177. This is an integer data type used to represent group IDs. In
  178. @theglibc{}, this is an alias for @code{unsigned int}.
  179. @end deftp
  180. @deftypefun uid_t getuid (void)
  181. @standards{POSIX.1, unistd.h}
  182. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  183. @c Atomic syscall, except on hurd, where it takes a lock within a hurd
  184. @c critical section.
  185. The @code{getuid} function returns the real user ID of the process.
  186. @end deftypefun
  187. @deftypefun gid_t getgid (void)
  188. @standards{POSIX.1, unistd.h}
  189. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  190. The @code{getgid} function returns the real group ID of the process.
  191. @end deftypefun
  192. @deftypefun uid_t geteuid (void)
  193. @standards{POSIX.1, unistd.h}
  194. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  195. The @code{geteuid} function returns the effective user ID of the process.
  196. @end deftypefun
  197. @deftypefun gid_t getegid (void)
  198. @standards{POSIX.1, unistd.h}
  199. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  200. The @code{getegid} function returns the effective group ID of the process.
  201. @end deftypefun
  202. @deftypefun int getgroups (int @var{count}, gid_t *@var{groups})
  203. @standards{POSIX.1, unistd.h}
  204. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  205. The @code{getgroups} function is used to inquire about the supplementary
  206. group IDs of the process. Up to @var{count} of these group IDs are
  207. stored in the array @var{groups}; the return value from the function is
  208. the number of group IDs actually stored. If @var{count} is smaller than
  209. the total number of supplementary group IDs, then @code{getgroups}
  210. returns a value of @code{-1} and @code{errno} is set to @code{EINVAL}.
  211. If @var{count} is zero, then @code{getgroups} just returns the total
  212. number of supplementary group IDs. On systems that do not support
  213. supplementary groups, this will always be zero.
  214. Here's how to use @code{getgroups} to read all the supplementary group
  215. IDs:
  216. @smallexample
  217. @group
  218. gid_t *
  219. read_all_groups (void)
  220. @{
  221. int ngroups = getgroups (0, NULL);
  222. gid_t *groups
  223. = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
  224. int val = getgroups (ngroups, groups);
  225. if (val < 0)
  226. @{
  227. free (groups);
  228. return NULL;
  229. @}
  230. return groups;
  231. @}
  232. @end group
  233. @end smallexample
  234. @end deftypefun
  235. @node Setting User ID
  236. @section Setting the User ID
  237. This section describes the functions for altering the user ID (real
  238. and/or effective) of a process. To use these facilities, you must
  239. include the header files @file{sys/types.h} and @file{unistd.h}.
  240. @pindex unistd.h
  241. @pindex sys/types.h
  242. @deftypefun int seteuid (uid_t @var{neweuid})
  243. @standards{POSIX.1, unistd.h}
  244. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  245. @c seteuid @asulock @aculock
  246. @c INLINE_SETXID_SYSCALL @asulock @aculock
  247. @c This may be just a unix syscall, or the ugliness below used by
  248. @c nptl to propagate the syscall to all cloned processes used to
  249. @c implement threads.
  250. @c nptl_setxid @asulock @aculock
  251. @c while holding the stack_alloc_lock, mark with SETXID_BITMASK all
  252. @c threads that are not exiting, signal them until no thread remains
  253. @c marked, clear the marks and run the syscall, then release the lock.
  254. @c lll_lock @asulock @aculock
  255. @c list_for_each ok
  256. @c list_entry ok
  257. @c setxid_mark_thread ok
  258. @c if a thread is initializing, wait for it to be cloned.
  259. @c mark it with SETXID_BITMASK if it's not exiting
  260. @c setxid_signal_thread ok
  261. @c if a thread is marked with SETXID_BITMASK,
  262. @c send it the SIGSETXID signal
  263. @c setxid_unmark_thread ok
  264. @c clear SETXID_BITMASK and release the futex if SETXID_BITMASK is
  265. @c set.
  266. @c <syscall> ok
  267. @c lll_unlock @aculock
  268. @c
  269. @c sighandler_setxid ok
  270. @c issue the syscall, clear SETXID_BITMASK, release the futex, and
  271. @c wake up the signaller loop if the counter reached zero.
  272. This function sets the effective user ID of a process to @var{neweuid},
  273. provided that the process is allowed to change its effective user ID. A
  274. privileged process (effective user ID zero) can change its effective
  275. user ID to any legal value. An unprivileged process with a file user ID
  276. can change its effective user ID to its real user ID or to its file user
  277. ID. Otherwise, a process may not change its effective user ID at all.
  278. The @code{seteuid} function returns a value of @code{0} to indicate
  279. successful completion, and a value of @code{-1} to indicate an error.
  280. The following @code{errno} error conditions are defined for this
  281. function:
  282. @table @code
  283. @item EINVAL
  284. The value of the @var{neweuid} argument is invalid.
  285. @item EPERM
  286. The process may not change to the specified ID.
  287. @end table
  288. Older systems (those without the @code{_POSIX_SAVED_IDS} feature) do not
  289. have this function.
  290. @end deftypefun
  291. @deftypefun int setuid (uid_t @var{newuid})
  292. @standards{POSIX.1, unistd.h}
  293. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  294. @c setuid @asulock @aculock
  295. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  296. If the calling process is privileged, this function sets both the real
  297. and effective user IDs of the process to @var{newuid}. It also deletes
  298. the file user ID of the process, if any. @var{newuid} may be any
  299. legal value. (Once this has been done, there is no way to recover the
  300. old effective user ID.)
  301. If the process is not privileged, and the system supports the
  302. @code{_POSIX_SAVED_IDS} feature, then this function behaves like
  303. @code{seteuid}.
  304. The return values and error conditions are the same as for @code{seteuid}.
  305. @end deftypefun
  306. @deftypefun int setreuid (uid_t @var{ruid}, uid_t @var{euid})
  307. @standards{BSD, unistd.h}
  308. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  309. @c setreuid @asulock @aculock
  310. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  311. This function sets the real user ID of the process to @var{ruid} and the
  312. effective user ID to @var{euid}. If @var{ruid} is @code{-1}, it means
  313. not to change the real user ID; likewise if @var{euid} is @code{-1}, it
  314. means not to change the effective user ID.
  315. The @code{setreuid} function exists for compatibility with 4.3 BSD Unix,
  316. which does not support file IDs. You can use this function to swap the
  317. effective and real user IDs of the process. (Privileged processes are
  318. not limited to this particular usage.) If file IDs are supported, you
  319. should use that feature instead of this function. @xref{Enable/Disable
  320. Setuid}.
  321. The return value is @code{0} on success and @code{-1} on failure.
  322. The following @code{errno} error conditions are defined for this
  323. function:
  324. @table @code
  325. @item EPERM
  326. The process does not have the appropriate privileges; you do not
  327. have permission to change to the specified ID.
  328. @end table
  329. @end deftypefun
  330. @node Setting Groups
  331. @section Setting the Group IDs
  332. This section describes the functions for altering the group IDs (real
  333. and effective) of a process. To use these facilities, you must include
  334. the header files @file{sys/types.h} and @file{unistd.h}.
  335. @pindex unistd.h
  336. @pindex sys/types.h
  337. @deftypefun int setegid (gid_t @var{newgid})
  338. @standards{POSIX.1, unistd.h}
  339. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  340. @c setegid @asulock @aculock
  341. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  342. This function sets the effective group ID of the process to
  343. @var{newgid}, provided that the process is allowed to change its group
  344. ID. Just as with @code{seteuid}, if the process is privileged it may
  345. change its effective group ID to any value; if it isn't, but it has a
  346. file group ID, then it may change to its real group ID or file group ID;
  347. otherwise it may not change its effective group ID.
  348. Note that a process is only privileged if its effective @emph{user} ID
  349. is zero. The effective group ID only affects access permissions.
  350. The return values and error conditions for @code{setegid} are the same
  351. as those for @code{seteuid}.
  352. This function is only present if @code{_POSIX_SAVED_IDS} is defined.
  353. @end deftypefun
  354. @deftypefun int setgid (gid_t @var{newgid})
  355. @standards{POSIX.1, unistd.h}
  356. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  357. @c setgid @asulock @aculock
  358. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  359. This function sets both the real and effective group ID of the process
  360. to @var{newgid}, provided that the process is privileged. It also
  361. deletes the file group ID, if any.
  362. If the process is not privileged, then @code{setgid} behaves like
  363. @code{setegid}.
  364. The return values and error conditions for @code{setgid} are the same
  365. as those for @code{seteuid}.
  366. @end deftypefun
  367. @deftypefun int setregid (gid_t @var{rgid}, gid_t @var{egid})
  368. @standards{BSD, unistd.h}
  369. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  370. @c setregid @asulock @aculock
  371. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  372. This function sets the real group ID of the process to @var{rgid} and
  373. the effective group ID to @var{egid}. If @var{rgid} is @code{-1}, it
  374. means not to change the real group ID; likewise if @var{egid} is
  375. @code{-1}, it means not to change the effective group ID.
  376. The @code{setregid} function is provided for compatibility with 4.3 BSD
  377. Unix, which does not support file IDs. You can use this function to
  378. swap the effective and real group IDs of the process. (Privileged
  379. processes are not limited to this usage.) If file IDs are supported,
  380. you should use that feature instead of using this function.
  381. @xref{Enable/Disable Setuid}.
  382. The return values and error conditions for @code{setregid} are the same
  383. as those for @code{setreuid}.
  384. @end deftypefun
  385. @code{setuid} and @code{setgid} behave differently depending on whether
  386. the effective user ID at the time is zero. If it is not zero, they
  387. behave like @code{seteuid} and @code{setegid}. If it is, they change
  388. both effective and real IDs and delete the file ID. To avoid confusion,
  389. we recommend you always use @code{seteuid} and @code{setegid} except
  390. when you know the effective user ID is zero and your intent is to change
  391. the persona permanently. This case is rare---most of the programs that
  392. need it, such as @code{login} and @code{su}, have already been written.
  393. Note that if your program is setuid to some user other than @code{root},
  394. there is no way to drop privileges permanently.
  395. The system also lets privileged processes change their supplementary
  396. group IDs. To use @code{setgroups} or @code{initgroups}, your programs
  397. should include the header file @file{grp.h}.
  398. @pindex grp.h
  399. @deftypefun int setgroups (size_t @var{count}, const gid_t *@var{groups})
  400. @standards{BSD, grp.h}
  401. @safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  402. @c setgroups @asulock @aculock
  403. @c INLINE_SETXID_SYSCALL dup @asulock @aculock
  404. This function sets the process's supplementary group IDs. It can only
  405. be called from privileged processes. The @var{count} argument specifies
  406. the number of group IDs in the array @var{groups}.
  407. This function returns @code{0} if successful and @code{-1} on error.
  408. The following @code{errno} error conditions are defined for this
  409. function:
  410. @table @code
  411. @item EPERM
  412. The calling process is not privileged.
  413. @end table
  414. @end deftypefun
  415. @deftypefun int initgroups (const char *@var{user}, gid_t @var{group})
  416. @standards{BSD, grp.h}
  417. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @acsmem{} @acsfd{} @aculock{}}}
  418. @c initgroups @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  419. @c sysconf(_SC_NGROUPS_MAX) dup @acsfd
  420. @c MIN dup ok
  421. @c malloc @ascuheap @acsmem
  422. @c internal_getgrouplist @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  423. @c nscd_getgrouplist @ascuheap @acsfd @acsmem
  424. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  425. @c nscd_cache_search dup ok
  426. @c nscd_open_socket dup @acsfd
  427. @c realloc dup @ascuheap @acsmem
  428. @c readall dup ok
  429. @c memcpy dup ok
  430. @c close_not_cancel_no_status dup @acsfd
  431. @c nscd_drop_map_ref dup @ascuheap @acsmem
  432. @c nscd_unmap dup @ascuheap @acsmem
  433. @c nss_database_lookup dup @mtslocale @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  434. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  435. @c compat_call @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  436. @c sysconf(_SC_GETGR_R_SIZE_MAX) ok
  437. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  438. @c *getgrent_fct @ascuplugin
  439. @c *setgrent_fct @ascuplugin
  440. @c *endgrent_fct @ascuplugin
  441. @c realloc dup @ascuheap @acsmem
  442. @c free dup @ascuheap @acsmem
  443. @c *initgroups_dyn_fct @ascuplugin
  444. @c nss_next_action dup ok
  445. @c setgroups dup @asulock @aculock
  446. @c free dup @ascuheap @acsmem
  447. The @code{initgroups} function sets the process's supplementary group
  448. IDs to be the normal default for the user name @var{user}. The group
  449. @var{group} is automatically included.
  450. This function works by scanning the group database for all the groups
  451. @var{user} belongs to. It then calls @code{setgroups} with the list it
  452. has constructed.
  453. The return values and error conditions are the same as for
  454. @code{setgroups}.
  455. @end deftypefun
  456. If you are interested in the groups a particular user belongs to, but do
  457. not want to change the process's supplementary group IDs, you can use
  458. @code{getgrouplist}. To use @code{getgrouplist}, your programs should
  459. include the header file @file{grp.h}.
  460. @pindex grp.h
  461. @deftypefun int getgrouplist (const char *@var{user}, gid_t @var{group}, gid_t *@var{groups}, int *@var{ngroups})
  462. @standards{BSD, grp.h}
  463. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @acsmem{} @acsfd{} @aculock{}}}
  464. @c getgrouplist @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  465. @c MAX dup ok
  466. @c malloc dup @ascuheap @acsmem
  467. @c internal_getgrouplist dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  468. @c memcpy dup ok
  469. @c free dup @ascuheap @acsmem
  470. The @code{getgrouplist} function scans the group database for all the
  471. groups @var{user} belongs to. Up to *@var{ngroups} group IDs
  472. corresponding to these groups are stored in the array @var{groups}; the
  473. return value from the function is the number of group IDs actually
  474. stored. If *@var{ngroups} is smaller than the total number of groups
  475. found, then @code{getgrouplist} returns a value of @code{-1} and stores
  476. the actual number of groups in *@var{ngroups}. The group @var{group} is
  477. automatically included in the list of groups returned by
  478. @code{getgrouplist}.
  479. Here's how to use @code{getgrouplist} to read all supplementary groups
  480. for @var{user}:
  481. @smallexample
  482. @group
  483. gid_t *
  484. supplementary_groups (char *user)
  485. @{
  486. int ngroups = 16;
  487. gid_t *groups
  488. = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
  489. struct passwd *pw = getpwnam (user);
  490. if (pw == NULL)
  491. return NULL;
  492. if (getgrouplist (pw->pw_name, pw->pw_gid, groups, &ngroups) < 0)
  493. @{
  494. groups = xrealloc (ngroups * sizeof (gid_t));
  495. getgrouplist (pw->pw_name, pw->pw_gid, groups, &ngroups);
  496. @}
  497. return groups;
  498. @}
  499. @end group
  500. @end smallexample
  501. @end deftypefun
  502. @node Enable/Disable Setuid
  503. @section Enabling and Disabling Setuid Access
  504. A typical setuid program does not need its special access all of the
  505. time. It's a good idea to turn off this access when it isn't needed,
  506. so it can't possibly give unintended access.
  507. If the system supports the @code{_POSIX_SAVED_IDS} feature, you can
  508. accomplish this with @code{seteuid}. When the game program starts, its
  509. real user ID is @code{jdoe}, its effective user ID is @code{games}, and
  510. its saved user ID is also @code{games}. The program should record both
  511. user ID values once at the beginning, like this:
  512. @smallexample
  513. user_user_id = getuid ();
  514. game_user_id = geteuid ();
  515. @end smallexample
  516. Then it can turn off game file access with
  517. @smallexample
  518. seteuid (user_user_id);
  519. @end smallexample
  520. @noindent
  521. and turn it on with
  522. @smallexample
  523. seteuid (game_user_id);
  524. @end smallexample
  525. @noindent
  526. Throughout this process, the real user ID remains @code{jdoe} and the
  527. file user ID remains @code{games}, so the program can always set its
  528. effective user ID to either one.
  529. On other systems that don't support file user IDs, you can
  530. turn setuid access on and off by using @code{setreuid} to swap the real
  531. and effective user IDs of the process, as follows:
  532. @smallexample
  533. setreuid (geteuid (), getuid ());
  534. @end smallexample
  535. @noindent
  536. This special case is always allowed---it cannot fail.
  537. Why does this have the effect of toggling the setuid access? Suppose a
  538. game program has just started, and its real user ID is @code{jdoe} while
  539. its effective user ID is @code{games}. In this state, the game can
  540. write the scores file. If it swaps the two uids, the real becomes
  541. @code{games} and the effective becomes @code{jdoe}; now the program has
  542. only @code{jdoe} access. Another swap brings @code{games} back to
  543. the effective user ID and restores access to the scores file.
  544. In order to handle both kinds of systems, test for the saved user ID
  545. feature with a preprocessor conditional, like this:
  546. @smallexample
  547. #ifdef _POSIX_SAVED_IDS
  548. seteuid (user_user_id);
  549. #else
  550. setreuid (geteuid (), getuid ());
  551. #endif
  552. @end smallexample
  553. @node Setuid Program Example
  554. @section Setuid Program Example
  555. Here's an example showing how to set up a program that changes its
  556. effective user ID.
  557. This is part of a game program called @code{caber-toss} that manipulates
  558. a file @file{scores} that should be writable only by the game program
  559. itself. The program assumes that its executable file will be installed
  560. with the setuid bit set and owned by the same user as the @file{scores}
  561. file. Typically, a system administrator will set up an account like
  562. @code{games} for this purpose.
  563. The executable file is given mode @code{4755}, so that doing an
  564. @samp{ls -l} on it produces output like:
  565. @smallexample
  566. -rwsr-xr-x 1 games 184422 Jul 30 15:17 caber-toss
  567. @end smallexample
  568. @noindent
  569. The setuid bit shows up in the file modes as the @samp{s}.
  570. The scores file is given mode @code{644}, and doing an @samp{ls -l} on
  571. it shows:
  572. @smallexample
  573. -rw-r--r-- 1 games 0 Jul 31 15:33 scores
  574. @end smallexample
  575. Here are the parts of the program that show how to set up the changed
  576. user ID. This program is conditionalized so that it makes use of the
  577. file IDs feature if it is supported, and otherwise uses @code{setreuid}
  578. to swap the effective and real user IDs.
  579. @smallexample
  580. #include <stdio.h>
  581. #include <sys/types.h>
  582. #include <unistd.h>
  583. #include <stdlib.h>
  584. /* @r{Remember the effective and real UIDs.} */
  585. static uid_t euid, ruid;
  586. /* @r{Restore the effective UID to its original value.} */
  587. void
  588. do_setuid (void)
  589. @{
  590. int status;
  591. #ifdef _POSIX_SAVED_IDS
  592. status = seteuid (euid);
  593. #else
  594. status = setreuid (ruid, euid);
  595. #endif
  596. if (status < 0) @{
  597. fprintf (stderr, "Couldn't set uid.\n");
  598. exit (status);
  599. @}
  600. @}
  601. @group
  602. /* @r{Set the effective UID to the real UID.} */
  603. void
  604. undo_setuid (void)
  605. @{
  606. int status;
  607. #ifdef _POSIX_SAVED_IDS
  608. status = seteuid (ruid);
  609. #else
  610. status = setreuid (euid, ruid);
  611. #endif
  612. if (status < 0) @{
  613. fprintf (stderr, "Couldn't set uid.\n");
  614. exit (status);
  615. @}
  616. @}
  617. @end group
  618. /* @r{Main program.} */
  619. int
  620. main (void)
  621. @{
  622. /* @r{Remember the real and effective user IDs.} */
  623. ruid = getuid ();
  624. euid = geteuid ();
  625. undo_setuid ();
  626. /* @r{Do the game and record the score.} */
  627. @dots{}
  628. @}
  629. @end smallexample
  630. Notice how the first thing the @code{main} function does is to set the
  631. effective user ID back to the real user ID. This is so that any other
  632. file accesses that are performed while the user is playing the game use
  633. the real user ID for determining permissions. Only when the program
  634. needs to open the scores file does it switch back to the file user ID,
  635. like this:
  636. @smallexample
  637. /* @r{Record the score.} */
  638. int
  639. record_score (int score)
  640. @{
  641. FILE *stream;
  642. char *myname;
  643. /* @r{Open the scores file.} */
  644. do_setuid ();
  645. stream = fopen (SCORES_FILE, "a");
  646. undo_setuid ();
  647. @group
  648. /* @r{Write the score to the file.} */
  649. if (stream)
  650. @{
  651. myname = cuserid (NULL);
  652. if (score < 0)
  653. fprintf (stream, "%10s: Couldn't lift the caber.\n", myname);
  654. else
  655. fprintf (stream, "%10s: %d feet.\n", myname, score);
  656. fclose (stream);
  657. return 0;
  658. @}
  659. else
  660. return -1;
  661. @}
  662. @end group
  663. @end smallexample
  664. @node Tips for Setuid
  665. @section Tips for Writing Setuid Programs
  666. It is easy for setuid programs to give the user access that isn't
  667. intended---in fact, if you want to avoid this, you need to be careful.
  668. Here are some guidelines for preventing unintended access and
  669. minimizing its consequences when it does occur:
  670. @itemize @bullet
  671. @item
  672. Don't have @code{setuid} programs with privileged user IDs such as
  673. @code{root} unless it is absolutely necessary. If the resource is
  674. specific to your particular program, it's better to define a new,
  675. nonprivileged user ID or group ID just to manage that resource.
  676. It's better if you can write your program to use a special group than a
  677. special user.
  678. @item
  679. Be cautious about using the @code{exec} functions in combination with
  680. changing the effective user ID. Don't let users of your program execute
  681. arbitrary programs under a changed user ID. Executing a shell is
  682. especially bad news. Less obviously, the @code{execlp} and @code{execvp}
  683. functions are a potential risk (since the program they execute depends
  684. on the user's @code{PATH} environment variable).
  685. If you must @code{exec} another program under a changed ID, specify an
  686. absolute file name (@pxref{File Name Resolution}) for the executable,
  687. and make sure that the protections on that executable and @emph{all}
  688. containing directories are such that ordinary users cannot replace it
  689. with some other program.
  690. You should also check the arguments passed to the program to make sure
  691. they do not have unexpected effects. Likewise, you should examine the
  692. environment variables. Decide which arguments and variables are safe,
  693. and reject all others.
  694. You should never use @code{system} in a privileged program, because it
  695. invokes a shell.
  696. @item
  697. Only use the user ID controlling the resource in the part of the program
  698. that actually uses that resource. When you're finished with it, restore
  699. the effective user ID back to the actual user's user ID.
  700. @xref{Enable/Disable Setuid}.
  701. @item
  702. If the @code{setuid} part of your program needs to access other files
  703. besides the controlled resource, it should verify that the real user
  704. would ordinarily have permission to access those files. You can use the
  705. @code{access} function (@pxref{Access Permission}) to check this; it
  706. uses the real user and group IDs, rather than the effective IDs.
  707. @end itemize
  708. @node Who Logged In
  709. @section Identifying Who Logged In
  710. @cindex login name, determining
  711. @cindex user ID, determining
  712. You can use the functions listed in this section to determine the login
  713. name of the user who is running a process, and the name of the user who
  714. logged in the current session. See also the function @code{getuid} and
  715. friends (@pxref{Reading Persona}). How this information is collected by
  716. the system and how to control/add/remove information from the background
  717. storage is described in @ref{User Accounting Database}.
  718. The @code{getlogin} function is declared in @file{unistd.h}, while
  719. @code{cuserid} and @code{L_cuserid} are declared in @file{stdio.h}.
  720. @pindex stdio.h
  721. @pindex unistd.h
  722. @deftypefun {char *} getlogin (void)
  723. @standards{POSIX.1, unistd.h}
  724. @safety{@prelim{}@mtunsafe{@mtasurace{:getlogin} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  725. @c getlogin (linux) @mtasurace:getlogin @mtasurace:utent @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  726. @c getlogin_r_loginuid dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  727. @c getlogin_fd0 (unix) @mtasurace:getlogin @mtasurace:utent @mtascusig:ALRM @mtascutimer @ascuheap @asulock @aculock @acsfd @acsmem
  728. @c uses static buffer name => @mtasurace:getlogin
  729. @c ttyname_r dup @ascuheap @acsmem @acsfd
  730. @c strncpy dup ok
  731. @c setutent dup @mtasurace:utent @asulock @aculock @acsfd
  732. @c getutline_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  733. @c endutent dup @mtasurace:utent @asulock @aculock
  734. @c libc_lock_unlock dup ok
  735. @c strlen dup ok
  736. @c memcpy dup ok
  737. @c
  738. @c getlogin_r (linux) @mtasurace:utent @mtascusig:ALRM @mtascutimer @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  739. @c getlogin_r_loginuid @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  740. @c open_not_cancel_2 dup @acsfd
  741. @c read_not_cancel dup ok
  742. @c close_not_cancel_no_status dup @acsfd
  743. @c strtoul @mtslocale
  744. @c getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  745. @c realloc dup @asulock @aculock @acsfd @acsmem
  746. @c strlen dup ok
  747. @c memcpy dup ok
  748. @c free dup @asulock @aculock @acsfd @acsmem
  749. @c getlogin_r_fd0 (unix) @mtasurace:utent @mtascusig:ALRM @mtascutimer @ascuheap @asulock @aculock @acsmem @acsfd
  750. @c ttyname_r dup @ascuheap @acsmem @acsfd
  751. @c strncpy dup ok
  752. @c libc_lock_lock dup @asulock @aculock
  753. @c *libc_utmp_jump_table->setutent dup @mtasurace:utent @acsfd
  754. @c *libc_utmp_jump_table->getutline_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer
  755. @c *libc_utmp_jump_table->endutent dup @mtasurace:utent @asulock @aculock
  756. @c libc_lock_unlock dup ok
  757. @c strlen dup ok
  758. @c memcpy dup ok
  759. The @code{getlogin} function returns a pointer to a string containing the
  760. name of the user logged in on the controlling terminal of the process,
  761. or a null pointer if this information cannot be determined. The string
  762. is statically allocated and might be overwritten on subsequent calls to
  763. this function or to @code{cuserid}.
  764. @end deftypefun
  765. @deftypefun {char *} cuserid (char *@var{string})
  766. @standards{POSIX.1, stdio.h}
  767. @safety{@prelim{}@mtunsafe{@mtasurace{:cuserid/!string} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  768. @c cuserid @mtasurace:cuserid/!string @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  769. @c if string is NULL, cuserid will overwrite and return a static buffer
  770. @c geteuid dup ok
  771. @c getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  772. @c strncpy dup ok
  773. The @code{cuserid} function returns a pointer to a string containing a
  774. user name associated with the effective ID of the process. If
  775. @var{string} is not a null pointer, it should be an array that can hold
  776. at least @code{L_cuserid} characters; the string is returned in this
  777. array. Otherwise, a pointer to a string in a static area is returned.
  778. This string is statically allocated and might be overwritten on
  779. subsequent calls to this function or to @code{getlogin}.
  780. The use of this function is deprecated since it is marked to be
  781. withdrawn in XPG4.2 and has already been removed from newer revisions of
  782. POSIX.1.
  783. @end deftypefun
  784. @deftypevr Macro int L_cuserid
  785. @standards{POSIX.1, stdio.h}
  786. An integer constant that indicates how long an array you might need to
  787. store a user name.
  788. @end deftypevr
  789. These functions let your program identify positively the user who is
  790. running or the user who logged in this session. (These can differ when
  791. setuid programs are involved; see @ref{Process Persona}.) The user cannot
  792. do anything to fool these functions.
  793. For most purposes, it is more useful to use the environment variable
  794. @code{LOGNAME} to find out who the user is. This is more flexible
  795. precisely because the user can set @code{LOGNAME} arbitrarily.
  796. @xref{Standard Environment}.
  797. @node User Accounting Database
  798. @section The User Accounting Database
  799. @cindex user accounting database
  800. Most Unix-like operating systems keep track of logged in users by
  801. maintaining a user accounting database. This user accounting database
  802. stores for each terminal, who has logged on, at what time, the process
  803. ID of the user's login shell, etc., etc., but also stores information
  804. about the run level of the system, the time of the last system reboot,
  805. and possibly more.
  806. The user accounting database typically lives in @file{/etc/utmp},
  807. @file{/var/adm/utmp} or @file{/var/run/utmp}. However, these files
  808. should @strong{never} be accessed directly. For reading information
  809. from and writing information to the user accounting database, the
  810. functions described in this section should be used.
  811. @menu
  812. * Manipulating the Database:: Scanning and modifying the user
  813. accounting database.
  814. * XPG Functions:: A standardized way for doing the same thing.
  815. * Logging In and Out:: Functions from BSD that modify the user
  816. accounting database.
  817. @end menu
  818. @node Manipulating the Database
  819. @subsection Manipulating the User Accounting Database
  820. These functions and the corresponding data structures are declared in
  821. the header file @file{utmp.h}.
  822. @pindex utmp.h
  823. @deftp {Data Type} {struct exit_status}
  824. @standards{SVID, utmp.h}
  825. The @code{exit_status} data structure is used to hold information about
  826. the exit status of processes marked as @code{DEAD_PROCESS} in the user
  827. accounting database.
  828. @table @code
  829. @item short int e_termination
  830. The exit status of the process.
  831. @item short int e_exit
  832. The exit status of the process.
  833. @end table
  834. @end deftp
  835. @deftp {Data Type} {struct utmp}
  836. The @code{utmp} data structure is used to hold information about entries
  837. in the user accounting database. On @gnusystems{} it has the following
  838. members:
  839. @table @code
  840. @item short int ut_type
  841. Specifies the type of login; one of @code{EMPTY}, @code{RUN_LVL},
  842. @code{BOOT_TIME}, @code{OLD_TIME}, @code{NEW_TIME}, @code{INIT_PROCESS},
  843. @code{LOGIN_PROCESS}, @code{USER_PROCESS}, @code{DEAD_PROCESS} or
  844. @code{ACCOUNTING}.
  845. @item pid_t ut_pid
  846. The process ID number of the login process.
  847. @item char ut_line[]
  848. The device name of the tty (without @file{/dev/}).
  849. @item char ut_id[]
  850. The inittab ID of the process.
  851. @item char ut_user[]
  852. The user's login name.
  853. @item char ut_host[]
  854. The name of the host from which the user logged in.
  855. @item struct exit_status ut_exit
  856. The exit status of a process marked as @code{DEAD_PROCESS}.
  857. @item long ut_session
  858. The Session ID, used for windowing.
  859. @item struct timeval ut_tv
  860. Time the entry was made. For entries of type @code{OLD_TIME} this is
  861. the time when the system clock changed, and for entries of type
  862. @code{NEW_TIME} this is the time the system clock was set to.
  863. @item int32_t ut_addr_v6[4]
  864. The Internet address of a remote host.
  865. @end table
  866. @end deftp
  867. The @code{ut_type}, @code{ut_pid}, @code{ut_id}, @code{ut_tv}, and
  868. @code{ut_host} fields are not available on all systems. Portable
  869. applications therefore should be prepared for these situations. To help
  870. do this the @file{utmp.h} header provides macros
  871. @code{_HAVE_UT_TYPE}, @code{_HAVE_UT_PID}, @code{_HAVE_UT_ID},
  872. @code{_HAVE_UT_TV}, and @code{_HAVE_UT_HOST} if the respective field is
  873. available. The programmer can handle the situations by using
  874. @code{#ifdef} in the program code.
  875. The following macros are defined for use as values for the
  876. @code{ut_type} member of the @code{utmp} structure. The values are
  877. integer constants.
  878. @vtable @code
  879. @item EMPTY
  880. @standards{SVID, utmp.h}
  881. This macro is used to indicate that the entry contains no valid user
  882. accounting information.
  883. @item RUN_LVL
  884. @standards{SVID, utmp.h}
  885. This macro is used to identify the system's runlevel.
  886. @item BOOT_TIME
  887. @standards{SVID, utmp.h}
  888. This macro is used to identify the time of system boot.
  889. @item OLD_TIME
  890. @standards{SVID, utmp.h}
  891. This macro is used to identify the time when the system clock changed.
  892. @item NEW_TIME
  893. @standards{SVID, utmp.h}
  894. This macro is used to identify the time after the system clock changed.
  895. @item INIT_PROCESS
  896. @standards{SVID, utmp.h}
  897. This macro is used to identify a process spawned by the init process.
  898. @item LOGIN_PROCESS
  899. @standards{SVID, utmp.h}
  900. This macro is used to identify the session leader of a logged in user.
  901. @item USER_PROCESS
  902. @standards{SVID, utmp.h}
  903. This macro is used to identify a user process.
  904. @item DEAD_PROCESS
  905. @standards{SVID, utmp.h}
  906. This macro is used to identify a terminated process.
  907. @item ACCOUNTING
  908. @standards{SVID, utmp.h}
  909. ???
  910. @end vtable
  911. The size of the @code{ut_line}, @code{ut_id}, @code{ut_user} and
  912. @code{ut_host} arrays can be found using the @code{sizeof} operator.
  913. Many older systems have, instead of an @code{ut_tv} member, an
  914. @code{ut_time} member, usually of type @code{time_t}, for representing
  915. the time associated with the entry. Therefore, for backwards
  916. compatibility only, @file{utmp.h} defines @code{ut_time} as an alias for
  917. @code{ut_tv.tv_sec}.
  918. @deftypefun void setutent (void)
  919. @standards{SVID, utmp.h}
  920. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  921. @c Besides the static variables in utmp_file.c, there's the jump_table.
  922. @c They're both modified while holding a lock, but other threads may
  923. @c cause the variables to be modified between calling this function and
  924. @c others that rely on the internal state it sets up.
  925. @c setutent @mtasurace:utent @asulock @aculock @acsfd
  926. @c libc_lock_lock dup @asulock @aculock
  927. @c *libc_utmp_jump_table->setutent @mtasurace:utent @acsfd
  928. @c setutent_unknown @mtasurace:utent @acsfd
  929. @c *libc_utmp_file_functions.setutent = setutent_file @mtasurace:utent @acsfd
  930. @c open_not_cancel_2 dup @acsfd
  931. @c fcntl_not_cancel dup ok
  932. @c close_not_cancel_no_status dup @acsfd
  933. @c lseek64 dup ok
  934. @c libc_lock_unlock dup ok
  935. This function opens the user accounting database to begin scanning it.
  936. You can then call @code{getutent}, @code{getutid} or @code{getutline} to
  937. read entries and @code{pututline} to write entries.
  938. If the database is already open, it resets the input to the beginning of
  939. the database.
  940. @end deftypefun
  941. @deftypefun {struct utmp *} getutent (void)
  942. @standards{SVID, utmp.h}
  943. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtasurace{:utentbuf} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  944. @c The static buffer that holds results is allocated with malloc at
  945. @c the first call; the test is not thread-safe, so multiple concurrent
  946. @c calls could malloc multiple buffers.
  947. @c getutent @mtuinit @mtasurace:utent @mtasurace:utentbuf @mtascusig:ALRM @mtascutimer @ascuheap @asulock @aculock @acsfd @acsmem
  948. @c malloc @asulock @aculock @acsfd @acsmem
  949. @c getutent_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  950. The @code{getutent} function reads the next entry from the user
  951. accounting database. It returns a pointer to the entry, which is
  952. statically allocated and may be overwritten by subsequent calls to
  953. @code{getutent}. You must copy the contents of the structure if you
  954. wish to save the information or you can use the @code{getutent_r}
  955. function which stores the data in a user-provided buffer.
  956. A null pointer is returned in case no further entry is available.
  957. @end deftypefun
  958. @deftypefun void endutent (void)
  959. @standards{SVID, utmp.h}
  960. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  961. @c endutent @mtasurace:utent @asulock @aculock @acsfd
  962. @c libc_lock_lock dup @asulock @aculock
  963. @c *libc_utmp_jump_table->endutent @mtasurace:utent @acsfd
  964. @c endutent_unknown ok
  965. @c endutent_file @mtasurace:utent @acsfd
  966. @c close_not_cancel_no_status dup @acsfd
  967. @c libc_lock_unlock dup ok
  968. This function closes the user accounting database.
  969. @end deftypefun
  970. @deftypefun {struct utmp *} getutid (const struct utmp *@var{id})
  971. @standards{SVID, utmp.h}
  972. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
  973. @c Same caveats as getutline.
  974. @c
  975. @c getutid @mtuinit @mtasurace:utent @mtascusig:ALRM @mtascutimer @ascuheap @asulock @aculock @acsmem @acsfd
  976. @c uses a static buffer malloced on the first call
  977. @c malloc dup @ascuheap @acsmem
  978. @c getutid_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  979. This function searches forward from the current point in the database
  980. for an entry that matches @var{id}. If the @code{ut_type} member of the
  981. @var{id} structure is one of @code{RUN_LVL}, @code{BOOT_TIME},
  982. @code{OLD_TIME} or @code{NEW_TIME} the entries match if the
  983. @code{ut_type} members are identical. If the @code{ut_type} member of
  984. the @var{id} structure is @code{INIT_PROCESS}, @code{LOGIN_PROCESS},
  985. @code{USER_PROCESS} or @code{DEAD_PROCESS}, the entries match if the
  986. @code{ut_type} member of the entry read from the database is one of
  987. these four, and the @code{ut_id} members match. However if the
  988. @code{ut_id} member of either the @var{id} structure or the entry read
  989. from the database is empty it checks if the @code{ut_line} members match
  990. instead. If a matching entry is found, @code{getutid} returns a pointer
  991. to the entry, which is statically allocated, and may be overwritten by a
  992. subsequent call to @code{getutent}, @code{getutid} or @code{getutline}.
  993. You must copy the contents of the structure if you wish to save the
  994. information.
  995. A null pointer is returned in case the end of the database is reached
  996. without a match.
  997. The @code{getutid} function may cache the last read entry. Therefore,
  998. if you are using @code{getutid} to search for multiple occurrences, it
  999. is necessary to zero out the static data after each call. Otherwise
  1000. @code{getutid} could just return a pointer to the same entry over and
  1001. over again.
  1002. @end deftypefun
  1003. @deftypefun {struct utmp *} getutline (const struct utmp *@var{line})
  1004. @standards{SVID, utmp.h}
  1005. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  1006. @c The static buffer that holds results is allocated with malloc at
  1007. @c the first call; the test is not thread-safe, so multiple concurrent
  1008. @c calls could malloc multiple buffers.
  1009. @c getutline @mtuinit @mtasurace:utent @mtascusig:ALRM @mtascutimer @ascuheap @asulock @aculock @acsfd @acsmem
  1010. @c malloc @asulock @aculock @acsfd @acsmem
  1011. @c getutline_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1012. This function searches forward from the current point in the database
  1013. until it finds an entry whose @code{ut_type} value is
  1014. @code{LOGIN_PROCESS} or @code{USER_PROCESS}, and whose @code{ut_line}
  1015. member matches the @code{ut_line} member of the @var{line} structure.
  1016. If it finds such an entry, it returns a pointer to the entry which is
  1017. statically allocated, and may be overwritten by a subsequent call to
  1018. @code{getutent}, @code{getutid} or @code{getutline}. You must copy the
  1019. contents of the structure if you wish to save the information.
  1020. A null pointer is returned in case the end of the database is reached
  1021. without a match.
  1022. The @code{getutline} function may cache the last read entry. Therefore
  1023. if you are using @code{getutline} to search for multiple occurrences, it
  1024. is necessary to zero out the static data after each call. Otherwise
  1025. @code{getutline} could just return a pointer to the same entry over and
  1026. over again.
  1027. @end deftypefun
  1028. @deftypefun {struct utmp *} pututline (const struct utmp *@var{utmp})
  1029. @standards{SVID, utmp.h}
  1030. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1031. @c pututline @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1032. @c libc_lock_lock dup @asulock @aculock
  1033. @c *libc_utmp_jump_table->pututline @mtasurace:utent @mtascusig:ALRM @mtascutimer @acsfd
  1034. @c pututline_unknown @mtasurace:utent @acsfd
  1035. @c setutent_unknown dup @mtasurace:utent @acsfd
  1036. @c pututline_file @mtascusig:ALRM @mtascutimer @acsfd
  1037. @c TRANSFORM_UTMP_FILE_NAME ok
  1038. @c strcmp dup ok
  1039. @c acesss dup ok
  1040. @c open_not_cancel_2 dup @acsfd
  1041. @c fcntl_not_cancel dup ok
  1042. @c close_not_cancel_no_status dup @acsfd
  1043. @c llseek dup ok
  1044. @c dup2 dup ok
  1045. @c utmp_equal dup ok
  1046. @c internal_getut_r dup @mtascusig:ALRM @mtascutimer
  1047. @c LOCK_FILE dup @mtascusig:ALRM @mtasctimer
  1048. @c LOCKING_FAILED dup ok
  1049. @c ftruncate64 dup ok
  1050. @c write_not_cancel dup ok
  1051. @c UNLOCK_FILE dup @mtasctimer
  1052. @c libc_lock_unlock dup @aculock
  1053. The @code{pututline} function inserts the entry @code{*@var{utmp}} at
  1054. the appropriate place in the user accounting database. If it finds that
  1055. it is not already at the correct place in the database, it uses
  1056. @code{getutid} to search for the position to insert the entry, however
  1057. this will not modify the static structure returned by @code{getutent},
  1058. @code{getutid} and @code{getutline}. If this search fails, the entry
  1059. is appended to the database.
  1060. The @code{pututline} function returns a pointer to a copy of the entry
  1061. inserted in the user accounting database, or a null pointer if the entry
  1062. could not be added. The following @code{errno} error conditions are
  1063. defined for this function:
  1064. @table @code
  1065. @item EPERM
  1066. The process does not have the appropriate privileges; you cannot modify
  1067. the user accounting database.
  1068. @end table
  1069. @end deftypefun
  1070. All the @code{get*} functions mentioned before store the information
  1071. they return in a static buffer. This can be a problem in multi-threaded
  1072. programs since the data returned for the request is overwritten by the
  1073. return value data in another thread. Therefore @theglibc{}
  1074. provides as extensions three more functions which return the data in a
  1075. user-provided buffer.
  1076. @deftypefun int getutent_r (struct utmp *@var{buffer}, struct utmp **@var{result})
  1077. @standards{GNU, utmp.h}
  1078. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1079. @c getutent_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1080. @c libc_lock_lock dup @asulock @aculock
  1081. @c *libc_utmp_jump_table->getutent_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @acsfd
  1082. @c getutent_r_unknown @mtasurace:utent @acsfd
  1083. @c setutent_unknown dup @mtasurace:utent @acsfd
  1084. @c getutent_r_file @mtasurace:utent @mtascusig:ALRM @mtascutimer
  1085. @c LOCK_FILE @mtascusig:ALRM @mtascutimer
  1086. @c alarm dup @mtascutimer
  1087. @c sigemptyset dup ok
  1088. @c sigaction dup ok
  1089. @c memset dup ok
  1090. @c fcntl_not_cancel dup ok
  1091. @c LOCKING_FAILED ok
  1092. @c read_not_cancel dup ok
  1093. @c UNLOCK_FILE @mtascutimer
  1094. @c fcntl_not_cancel dup ok
  1095. @c alarm dup @mtascutimer
  1096. @c sigaction dup ok
  1097. @c memcpy dup ok
  1098. @c libc_lock_unlock dup ok
  1099. The @code{getutent_r} is equivalent to the @code{getutent} function. It
  1100. returns the next entry from the database. But instead of storing the
  1101. information in a static buffer it stores it in the buffer pointed to by
  1102. the parameter @var{buffer}.
  1103. If the call was successful, the function returns @code{0} and the
  1104. pointer variable pointed to by the parameter @var{result} contains a
  1105. pointer to the buffer which contains the result (this is most probably
  1106. the same value as @var{buffer}). If something went wrong during the
  1107. execution of @code{getutent_r} the function returns @code{-1}.
  1108. This function is a GNU extension.
  1109. @end deftypefun
  1110. @deftypefun int getutid_r (const struct utmp *@var{id}, struct utmp *@var{buffer}, struct utmp **@var{result})
  1111. @standards{GNU, utmp.h}
  1112. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1113. @c getutid_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1114. @c libc_lock_lock dup @asulock @aculock
  1115. @c *libc_utmp_jump_table->getutid_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @acsfd
  1116. @c getutid_r_unknown @mtasurace:utent @acsfd
  1117. @c setutent_unknown dup @mtasurace:utent @acsfd
  1118. @c getutid_r_file @mtascusig:ALRM @mtascutimer
  1119. @c internal_getut_r @mtascusig:ALRM @mtascutimer
  1120. @c LOCK_FILE dup @mtascusig:ALRM @mtascutimer
  1121. @c LOCKING_FAILED dup ok
  1122. @c read_not_cancel dup ok
  1123. @c utmp_equal ok
  1124. @c strncmp dup ok
  1125. @c UNLOCK_FILE dup @mtascutimer
  1126. @c memcpy dup ok
  1127. @c libc_lock_unlock dup @aculock
  1128. This function retrieves just like @code{getutid} the next entry matching
  1129. the information stored in @var{id}. But the result is stored in the
  1130. buffer pointed to by the parameter @var{buffer}.
  1131. If successful the function returns @code{0} and the pointer variable
  1132. pointed to by the parameter @var{result} contains a pointer to the
  1133. buffer with the result (probably the same as @var{result}. If not
  1134. successful the function return @code{-1}.
  1135. This function is a GNU extension.
  1136. @end deftypefun
  1137. @deftypefun int getutline_r (const struct utmp *@var{line}, struct utmp *@var{buffer}, struct utmp **@var{result})
  1138. @standards{GNU, utmp.h}
  1139. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1140. @c getutline_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1141. @c libc_lock_lock dup @asulock @aculock
  1142. @c *libc_utmp_jump_table->getutline_r @mtasurace:utent @mtascusig:ALRM @mtascutimer @acsfd
  1143. @c getutline_r_unknown @mtasurace:utent @acsfd
  1144. @c setutent_unknown dup @mtasurace:utent @acsfd
  1145. @c getutline_r_file @mtasurace:utent @mtascusig:ALRM @mtascutimer
  1146. @c LOCK_FILE @mtascusig:ALRM @mtascutimer
  1147. @c alarm dup @mtascutimer
  1148. @c sigemptyset dup ok
  1149. @c sigaction dup ok
  1150. @c memset dup ok
  1151. @c fcntl_not_cancel dup ok
  1152. @c LOCKING_FAILED ok
  1153. @c read_not_cancel dup ok
  1154. @c strncmp dup ok
  1155. @c UNLOCK_FILE @mtascutimer
  1156. @c fcntl_not_cancel dup ok
  1157. @c alarm dup @mtascutimer
  1158. @c sigaction dup ok
  1159. @c memcpy dup ok
  1160. @c libc_lock_unlock dup ok
  1161. This function retrieves just like @code{getutline} the next entry
  1162. matching the information stored in @var{line}. But the result is stored
  1163. in the buffer pointed to by the parameter @var{buffer}.
  1164. If successful the function returns @code{0} and the pointer variable
  1165. pointed to by the parameter @var{result} contains a pointer to the
  1166. buffer with the result (probably the same as @var{result}. If not
  1167. successful the function return @code{-1}.
  1168. This function is a GNU extension.
  1169. @end deftypefun
  1170. In addition to the user accounting database, most systems keep a number
  1171. of similar databases. For example most systems keep a log file with all
  1172. previous logins (usually in @file{/etc/wtmp} or @file{/var/log/wtmp}).
  1173. For specifying which database to examine, the following function should
  1174. be used.
  1175. @deftypefun int utmpname (const char *@var{file})
  1176. @standards{SVID, utmp.h}
  1177. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acsmem{}}}
  1178. @c utmpname @mtasurace:utent @asulock @ascuheap @aculock @acsmem
  1179. @c libc_lock_lock dup @asulock @aculock
  1180. @c *libc_utmp_jump_table->endutent dup @mtasurace:utent
  1181. @c strcmp dup ok
  1182. @c free dup @ascuheap @acsmem
  1183. @c strdup dup @ascuheap @acsmem
  1184. @c libc_lock_unlock dup @aculock
  1185. The @code{utmpname} function changes the name of the database to be
  1186. examined to @var{file}, and closes any previously opened database. By
  1187. default @code{getutent}, @code{getutid}, @code{getutline} and
  1188. @code{pututline} read from and write to the user accounting database.
  1189. The following macros are defined for use as the @var{file} argument:
  1190. @deftypevr Macro {char *} _PATH_UTMP
  1191. This macro is used to specify the user accounting database.
  1192. @end deftypevr
  1193. @deftypevr Macro {char *} _PATH_WTMP
  1194. This macro is used to specify the user accounting log file.
  1195. @end deftypevr
  1196. The @code{utmpname} function returns a value of @code{0} if the new name
  1197. was successfully stored, and a value of @code{-1} to indicate an error.
  1198. Note that @code{utmpname} does not try to open the database, and that
  1199. therefore the return value does not say anything about whether the
  1200. database can be successfully opened.
  1201. @end deftypefun
  1202. Specially for maintaining log-like databases @theglibc{} provides
  1203. the following function:
  1204. @deftypefun void updwtmp (const char *@var{wtmp_file}, const struct utmp *@var{utmp})
  1205. @standards{SVID, utmp.h}
  1206. @safety{@prelim{}@mtunsafe{@mtascusig{:ALRM} @mtascutimer{}}@asunsafe{}@acunsafe{@acsfd{}}}
  1207. @c updwtmp @mtascusig:ALRM @mtascutimer @acsfd
  1208. @c TRANSFORM_UTMP_FILE_NAME dup ok
  1209. @c *libc_utmp_file_functions->updwtmp = updwtmp_file @mtascusig:ALRM @mtascutimer @acsfd
  1210. @c open_not_cancel_2 dup @acsfd
  1211. @c LOCK_FILE dup @mtascusig:ALRM @mtascutimer
  1212. @c LOCKING_FAILED dup ok
  1213. @c lseek64 dup ok
  1214. @c ftruncate64 dup ok
  1215. @c write_not_cancel dup ok
  1216. @c UNLOCK_FILE dup @mtascutimer
  1217. @c close_not_cancel_no_status dup @acsfd
  1218. The @code{updwtmp} function appends the entry *@var{utmp} to the
  1219. database specified by @var{wtmp_file}. For possible values for the
  1220. @var{wtmp_file} argument see the @code{utmpname} function.
  1221. @end deftypefun
  1222. @strong{Portability Note:} Although many operating systems provide a
  1223. subset of these functions, they are not standardized. There are often
  1224. subtle differences in the return types, and there are considerable
  1225. differences between the various definitions of @code{struct utmp}. When
  1226. programming for @theglibc{}, it is probably best to stick
  1227. with the functions described in this section. If however, you want your
  1228. program to be portable, consider using the XPG functions described in
  1229. @ref{XPG Functions}, or take a look at the BSD compatible functions in
  1230. @ref{Logging In and Out}.
  1231. @node XPG Functions
  1232. @subsection XPG User Accounting Database Functions
  1233. These functions, described in the X/Open Portability Guide, are declared
  1234. in the header file @file{utmpx.h}.
  1235. @pindex utmpx.h
  1236. @deftp {Data Type} {struct utmpx}
  1237. The @code{utmpx} data structure contains at least the following members:
  1238. @table @code
  1239. @item short int ut_type
  1240. Specifies the type of login; one of @code{EMPTY}, @code{RUN_LVL},
  1241. @code{BOOT_TIME}, @code{OLD_TIME}, @code{NEW_TIME}, @code{INIT_PROCESS},
  1242. @code{LOGIN_PROCESS}, @code{USER_PROCESS} or @code{DEAD_PROCESS}.
  1243. @item pid_t ut_pid
  1244. The process ID number of the login process.
  1245. @item char ut_line[]
  1246. The device name of the tty (without @file{/dev/}).
  1247. @item char ut_id[]
  1248. The inittab ID of the process.
  1249. @item char ut_user[]
  1250. The user's login name.
  1251. @item struct timeval ut_tv
  1252. Time the entry was made. For entries of type @code{OLD_TIME} this is
  1253. the time when the system clock changed, and for entries of type
  1254. @code{NEW_TIME} this is the time the system clock was set to.
  1255. @end table
  1256. In @theglibc{}, @code{struct utmpx} is identical to @code{struct
  1257. utmp} except for the fact that including @file{utmpx.h} does not make
  1258. visible the declaration of @code{struct exit_status}.
  1259. @end deftp
  1260. The following macros are defined for use as values for the
  1261. @code{ut_type} member of the @code{utmpx} structure. The values are
  1262. integer constants and are, in @theglibc{}, identical to the
  1263. definitions in @file{utmp.h}.
  1264. @vtable @code
  1265. @item EMPTY
  1266. @standards{XPG4.2, utmpx.h}
  1267. This macro is used to indicate that the entry contains no valid user
  1268. accounting information.
  1269. @item RUN_LVL
  1270. @standards{XPG4.2, utmpx.h}
  1271. This macro is used to identify the system's runlevel.
  1272. @item BOOT_TIME
  1273. @standards{XPG4.2, utmpx.h}
  1274. This macro is used to identify the time of system boot.
  1275. @item OLD_TIME
  1276. @standards{XPG4.2, utmpx.h}
  1277. This macro is used to identify the time when the system clock changed.
  1278. @item NEW_TIME
  1279. @standards{XPG4.2, utmpx.h}
  1280. This macro is used to identify the time after the system clock changed.
  1281. @item INIT_PROCESS
  1282. @standards{XPG4.2, utmpx.h}
  1283. This macro is used to identify a process spawned by the init process.
  1284. @item LOGIN_PROCESS
  1285. @standards{XPG4.2, utmpx.h}
  1286. This macro is used to identify the session leader of a logged in user.
  1287. @item USER_PROCESS
  1288. @standards{XPG4.2, utmpx.h}
  1289. This macro is used to identify a user process.
  1290. @item DEAD_PROCESS
  1291. @standards{XPG4.2, utmpx.h}
  1292. This macro is used to identify a terminated process.
  1293. @end vtable
  1294. The size of the @code{ut_line}, @code{ut_id} and @code{ut_user} arrays
  1295. can be found using the @code{sizeof} operator.
  1296. @deftypefun void setutxent (void)
  1297. @standards{XPG4.2, utmpx.h}
  1298. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1299. This function is similar to @code{setutent}. In @theglibc{} it is
  1300. simply an alias for @code{setutent}.
  1301. @end deftypefun
  1302. @deftypefun {struct utmpx *} getutxent (void)
  1303. @standards{XPG4.2, utmpx.h}
  1304. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  1305. The @code{getutxent} function is similar to @code{getutent}, but returns
  1306. a pointer to a @code{struct utmpx} instead of @code{struct utmp}. In
  1307. @theglibc{} it simply is an alias for @code{getutent}.
  1308. @end deftypefun
  1309. @deftypefun void endutxent (void)
  1310. @standards{XPG4.2, utmpx.h}
  1311. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{}}@acunsafe{@aculock{}}}
  1312. This function is similar to @code{endutent}. In @theglibc{} it is
  1313. simply an alias for @code{endutent}.
  1314. @end deftypefun
  1315. @deftypefun {struct utmpx *} getutxid (const struct utmpx *@var{id})
  1316. @standards{XPG4.2, utmpx.h}
  1317. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
  1318. This function is similar to @code{getutid}, but uses @code{struct utmpx}
  1319. instead of @code{struct utmp}. In @theglibc{} it is simply an alias
  1320. for @code{getutid}.
  1321. @end deftypefun
  1322. @deftypefun {struct utmpx *} getutxline (const struct utmpx *@var{line})
  1323. @standards{XPG4.2, utmpx.h}
  1324. @safety{@prelim{}@mtunsafe{@mtuinit{} @mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  1325. This function is similar to @code{getutid}, but uses @code{struct utmpx}
  1326. instead of @code{struct utmp}. In @theglibc{} it is simply an alias
  1327. for @code{getutline}.
  1328. @end deftypefun
  1329. @deftypefun {struct utmpx *} pututxline (const struct utmpx *@var{utmp})
  1330. @standards{XPG4.2, utmpx.h}
  1331. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{}}@acunsafe{@aculock{} @acsfd{}}}
  1332. The @code{pututxline} function is functionally identical to
  1333. @code{pututline}, but uses @code{struct utmpx} instead of @code{struct
  1334. utmp}. In @theglibc{}, @code{pututxline} is simply an alias for
  1335. @code{pututline}.
  1336. @end deftypefun
  1337. @deftypefun int utmpxname (const char *@var{file})
  1338. @standards{XPG4.2, utmpx.h}
  1339. @safety{@prelim{}@mtunsafe{@mtasurace{:utent}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acsmem{}}}
  1340. The @code{utmpxname} function is functionally identical to
  1341. @code{utmpname}. In @theglibc{}, @code{utmpxname} is simply an
  1342. alias for @code{utmpname}.
  1343. @end deftypefun
  1344. You can translate between a traditional @code{struct utmp} and an XPG
  1345. @code{struct utmpx} with the following functions. In @theglibc{},
  1346. these functions are merely copies, since the two structures are
  1347. identical.
  1348. @deftypefun int getutmp (const struct utmpx *@var{utmpx}, struct utmp *@var{utmp})
  1349. @standards{GNU, utmp.h}
  1350. @standards{GNU, utmpx.h}
  1351. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1352. @code{getutmp} copies the information, insofar as the structures are
  1353. compatible, from @var{utmpx} to @var{utmp}.
  1354. @end deftypefun
  1355. @deftypefun int getutmpx (const struct utmp *@var{utmp}, struct utmpx *@var{utmpx})
  1356. @standards{GNU, utmp.h}
  1357. @standards{GNU, utmpx.h}
  1358. @safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
  1359. @code{getutmpx} copies the information, insofar as the structures are
  1360. compatible, from @var{utmp} to @var{utmpx}.
  1361. @end deftypefun
  1362. @node Logging In and Out
  1363. @subsection Logging In and Out
  1364. These functions, derived from BSD, are available in the separate
  1365. @file{libutil} library, and declared in @file{utmp.h}.
  1366. @pindex utmp.h
  1367. Note that the @code{ut_user} member of @code{struct utmp} is called
  1368. @code{ut_name} in BSD. Therefore, @code{ut_name} is defined as an alias
  1369. for @code{ut_user} in @file{utmp.h}.
  1370. @deftypefun int login_tty (int @var{filedes})
  1371. @standards{BSD, utmp.h}
  1372. @safety{@prelim{}@mtunsafe{@mtasurace{:ttyname}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  1373. @c If this function is canceled, it may have succeeded in redirecting
  1374. @c only some of the standard streams to the newly opened terminal.
  1375. @c Should there be a safety annotation for this?
  1376. @c login_tty @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
  1377. @c setsid dup ok
  1378. @c ioctl dup ok
  1379. @c ttyname dup @mtasurace:ttyname @ascuheap @asulock @aculock @acsmem @acsfd
  1380. @c close dup @acsfd
  1381. @c open dup @acsfd
  1382. @c dup2 dup ok
  1383. This function makes @var{filedes} the controlling terminal of the
  1384. current process, redirects standard input, standard output and
  1385. standard error output to this terminal, and closes @var{filedes}.
  1386. This function returns @code{0} on successful completion, and @code{-1}
  1387. on error.
  1388. @end deftypefun
  1389. @deftypefun void login (const struct utmp *@var{entry})
  1390. @standards{BSD, utmp.h}
  1391. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsfd{} @acsmem{}}}
  1392. @c login @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @ascuheap @aculock @acucorrupt @acsfd @acsmem
  1393. @c getpid dup ok
  1394. @c tty_name @ascuheap @acucorrupt @acsmem @acsfd
  1395. @c ttyname_r dup @ascuheap @acsmem @acsfd
  1396. @c memchr dup ok
  1397. @c realloc dup @ascuheap @acsmem
  1398. @c malloc dup @ascuheap @acsmem
  1399. @c free dup @ascuheap @acsmem
  1400. @c strncmp dup ok
  1401. @c basename dup ok
  1402. @c strncpy dup ok
  1403. @c utmpname dup @mtasurace:utent @asulock @ascuheap @aculock @acsmem
  1404. @c setutent dup @mtasurace:utent @asulock @aculock @acsfd
  1405. @c pututline dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1406. @c endutent dup @mtasurace:utent @asulock @aculock
  1407. @c free dup @ascuheap @acsmem
  1408. @c updwtmp dup @mtascusig:ALRM @mtascutimer @acsfd
  1409. The @code{login} functions inserts an entry into the user accounting
  1410. database. The @code{ut_line} member is set to the name of the terminal
  1411. on standard input. If standard input is not a terminal @code{login}
  1412. uses standard output or standard error output to determine the name of
  1413. the terminal. If @code{struct utmp} has a @code{ut_type} member,
  1414. @code{login} sets it to @code{USER_PROCESS}, and if there is an
  1415. @code{ut_pid} member, it will be set to the process ID of the current
  1416. process. The remaining entries are copied from @var{entry}.
  1417. A copy of the entry is written to the user accounting log file.
  1418. @end deftypefun
  1419. @deftypefun int logout (const char *@var{ut_line})
  1420. @standards{BSD, utmp.h}
  1421. @safety{@prelim{}@mtunsafe{@mtasurace{:utent} @mtascusig{:ALRM} @mtascutimer{}}@asunsafe{@asulock{} @ascuheap{}}@acunsafe{@aculock{} @acsfd{} @acsmem{}}}
  1422. @c logout @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @ascuheap @aculock @acsfd @acsmem
  1423. @c utmpname dup @mtasurace:utent @asulock @ascuheap @aculock @acsmem
  1424. @c setutent dup @mtasurace:utent @asulock @aculock @acsfd
  1425. @c strncpy dup ok
  1426. @c getutline_r dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1427. @c bzero dup ok
  1428. @c gettimeofday dup ok
  1429. @c time dup ok
  1430. @c pututline dup @mtasurace:utent @mtascusig:ALRM @mtascutimer @asulock @aculock @acsfd
  1431. @c endutent dup @mtasurace:utent @asulock @aculock
  1432. This function modifies the user accounting database to indicate that the
  1433. user on @var{ut_line} has logged out.
  1434. The @code{logout} function returns @code{1} if the entry was successfully
  1435. written to the database, or @code{0} on error.
  1436. @end deftypefun
  1437. @deftypefun void logwtmp (const char *@var{ut_line}, const char *@var{ut_name}, const char *@var{ut_host})
  1438. @standards{BSD, utmp.h}
  1439. @safety{@prelim{}@mtunsafe{@mtascusig{:ALRM} @mtascutimer{}}@asunsafe{}@acunsafe{@acsfd{}}}
  1440. @c logwtmp @mtascusig:ALRM @mtascutimer @acsfd
  1441. @c memset dup ok
  1442. @c getpid dup ok
  1443. @c strncpy dup ok
  1444. @c gettimeofday dup ok
  1445. @c time dup ok
  1446. @c updwtmp dup @mtascusig:ALRM @mtascutimer @acsfd
  1447. The @code{logwtmp} function appends an entry to the user accounting log
  1448. file, for the current time and the information provided in the
  1449. @var{ut_line}, @var{ut_name} and @var{ut_host} arguments.
  1450. @end deftypefun
  1451. @strong{Portability Note:} The BSD @code{struct utmp} only has the
  1452. @code{ut_line}, @code{ut_name}, @code{ut_host} and @code{ut_time}
  1453. members. Older systems do not even have the @code{ut_host} member.
  1454. @node User Database
  1455. @section User Database
  1456. @cindex user database
  1457. @cindex password database
  1458. @pindex /etc/passwd
  1459. This section describes how to search and scan the database of registered
  1460. users. The database itself is kept in the file @file{/etc/passwd} on
  1461. most systems, but on some systems a special network server gives access
  1462. to it.
  1463. Historically, this database included one-way hashes of user
  1464. passphrases (@pxref{Passphrase Storage}) as well as public information
  1465. about each user (such as their user ID and full name). Many of the
  1466. functions and data structures associated with this database, and the
  1467. filename @file{/etc/passwd} itself, reflect this history. However,
  1468. the information in this database is available to all users, and it is
  1469. no longer considered safe to make passphrase hashes available to all
  1470. users, so they have been moved to a ``shadow'' database that can only
  1471. be accessed with special privileges.
  1472. @menu
  1473. * User Data Structure:: What each user record contains.
  1474. * Lookup User:: How to look for a particular user.
  1475. * Scanning All Users:: Scanning the list of all users, one by one.
  1476. * Writing a User Entry:: How a program can rewrite a user's record.
  1477. @end menu
  1478. @node User Data Structure
  1479. @subsection The Data Structure that Describes a User
  1480. The functions and data structures for accessing the system user database
  1481. are declared in the header file @file{pwd.h}.
  1482. @pindex pwd.h
  1483. @deftp {Data Type} {struct passwd}
  1484. @standards{POSIX.1, pwd.h}
  1485. The @code{passwd} data structure is used to hold information about
  1486. entries in the system user data base. It has at least the following members:
  1487. @table @code
  1488. @item char *pw_name
  1489. The user's login name.
  1490. @item char *pw_passwd
  1491. Historically, this field would hold the one-way hash of the user's
  1492. passphrase. Nowadays, it will almost always be the single character
  1493. @samp{x}, indicating that the hash is in the shadow database.
  1494. @item uid_t pw_uid
  1495. The user ID number.
  1496. @item gid_t pw_gid
  1497. The user's default group ID number.
  1498. @item char *pw_gecos
  1499. A string typically containing the user's real name, and possibly other
  1500. information such as a phone number.
  1501. @item char *pw_dir
  1502. The user's home directory, or initial working directory. This might be
  1503. a null pointer, in which case the interpretation is system-dependent.
  1504. @item char *pw_shell
  1505. The user's default shell, or the initial program run when the user logs in.
  1506. This might be a null pointer, indicating that the system default should
  1507. be used.
  1508. @end table
  1509. @end deftp
  1510. @node Lookup User
  1511. @subsection Looking Up One User
  1512. @cindex converting user ID to user name
  1513. @cindex converting user name to user ID
  1514. You can search the system user database for information about a
  1515. specific user using @code{getpwuid} or @code{getpwnam}. These
  1516. functions are declared in @file{pwd.h}.
  1517. @deftypefun {struct passwd *} getpwuid (uid_t @var{uid})
  1518. @standards{POSIX.1, pwd.h}
  1519. @safety{@prelim{}@mtunsafe{@mtasurace{:pwuid} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1520. @c getpwuid @mtasurace:pwuid @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1521. @c libc_lock_lock dup @asulock @aculock
  1522. @c malloc dup @ascuheap @acsmem
  1523. @c getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1524. @c realloc dup @ascuheap @acsmem
  1525. @c free dup @ascuheap @acsmem
  1526. @c libc_lock_unlock dup @aculock
  1527. This function returns a pointer to a statically-allocated structure
  1528. containing information about the user whose user ID is @var{uid}. This
  1529. structure may be overwritten on subsequent calls to @code{getpwuid}.
  1530. A null pointer value indicates there is no user in the data base with
  1531. user ID @var{uid}.
  1532. @end deftypefun
  1533. @deftypefun int getpwuid_r (uid_t @var{uid}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
  1534. @standards{POSIX.1c, pwd.h}
  1535. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1536. @c getpwuid_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1537. @c nscd_getpwuid_r @ascuheap @acsfd @acsmem
  1538. @c itoa_word dup ok
  1539. @c nscd_getpw_r @ascuheap @acsfd @acsmem
  1540. @c nscd_get_map_ref @ascuheap @acsfd @acsmem
  1541. @c nscd_acquire_maplock ok
  1542. @c nscd_get_mapping @ascuheap @acsfd @acsmem
  1543. @c open_socket dup @acsfd
  1544. @c memset dup ok
  1545. @c wait_on_socket dup ok
  1546. @c recvmsg dup ok
  1547. @c strcmp dup ok
  1548. @c fstat64 dup ok
  1549. @c mmap dup @acsmem
  1550. @c munmap dup @acsmem
  1551. @c malloc dup @ascuheap @acsmem
  1552. @c close dup ok
  1553. @c nscd_unmap dup @ascuheap @acsmem
  1554. @c nscd_cache_search ok
  1555. @c nis_hash ok
  1556. @c memcmp dup ok
  1557. @c nscd_open_socket @acsfd
  1558. @c open_socket @acsfd
  1559. @c socket dup @acsfd
  1560. @c fcntl dup ok
  1561. @c strcpy dup ok
  1562. @c connect dup ok
  1563. @c send dup ok
  1564. @c gettimeofday dup ok
  1565. @c poll dup ok
  1566. @c close_not_cancel_no_status dup @acsfd
  1567. @c wait_on_socket dup ok
  1568. @c read dup ok
  1569. @c close_not_cancel_no_status dup @acsfd
  1570. @c readall ok
  1571. @c read dup ok
  1572. @c wait_on_socket ok
  1573. @c poll dup ok
  1574. @c gettimeofday dup ok
  1575. @c memcpy dup ok
  1576. @c close_not_cancel_no_status dup @acsfd
  1577. @c nscd_drop_map_ref @ascuheap @acsmem
  1578. @c nscd_unmap dup @ascuheap @acsmem
  1579. @c nscd_unmap @ascuheap @acsmem
  1580. @c munmap dup ok
  1581. @c free dup @ascuheap @acsmem
  1582. @c nss_passwd_lookup2 @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1583. @c nss_database_lookup @mtslocale @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  1584. @c libc_lock_lock @asulock @aculock
  1585. @c libc_lock_unlock @aculock
  1586. @c nss_parse_file @mtslocale @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  1587. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  1588. @c fsetlocking dup ok [no concurrent uses]
  1589. @c malloc dup @asulock @aculock @acsfd @acsmem
  1590. @c fclose dup @ascuheap @asulock @acsmem @acsfd @aculock
  1591. @c getline dup @ascuheap @aculock @acucorrupt @acsmem
  1592. @c strchrnul dup ok
  1593. @c nss_getline @mtslocale @ascuheap @acsmem
  1594. @c isspace @mtslocale^^
  1595. @c strlen dup ok
  1596. @c malloc dup @asulock @aculock @acsfd @acsmem
  1597. @c memcpy dup ok
  1598. @c nss_parse_service_list dup @mtslocale^, @ascuheap @acsmem
  1599. @c feof_unlocked dup ok
  1600. @c free dup @asulock @aculock @acsfd @acsmem
  1601. @c strcmp dup ok
  1602. @c nss_parse_service_list @mtslocale^, @ascuheap @acsmem
  1603. @c isspace @mtslocale^^
  1604. @c malloc dup @asulock @aculock @acsfd @acsmem
  1605. @c mempcpy dup ok
  1606. @c strncasecmp dup ok
  1607. @c free dup @asulock @aculock @acsfd @acsmem
  1608. @c malloc dup @asulock @aculock @acsfd @acsmem
  1609. @c nss_lookup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1610. @c nss_lookup_function @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1611. @c libc_lock_lock @asulock @aculock
  1612. @c tsearch @ascuheap @acucorrupt @acsmem [no @mtsrace or @asucorrupt due to locking]
  1613. @c known_compare ok
  1614. @c strcmp dup ok
  1615. @c malloc dup @ascuheap @acsmem
  1616. @c tdelete @ascuheap @acucorrupt @acsmem [no @mtsrace or @asucorrupt due to locking]
  1617. @c free dup @ascuheap @acsmem
  1618. @c nss_load_library @ascudlopen @ascuplugin @ascuheap @asulock @aculock @acsfd @acsmem
  1619. @c nss_new_service @ascuheap @acsmem
  1620. @c strcmp dup ok
  1621. @c malloc dup @ascuheap @acsmem
  1622. @c strlen dup ok
  1623. @c stpcpy dup ok
  1624. @c libc_dlopen @ascudlopen @ascuheap @asulock @aculock @acsfd @acsmem
  1625. @c libc_dlsym dup @asulock @aculock @acsfd @acsmem
  1626. @c *ifct(*nscd_init_cb) @ascuplugin
  1627. @c stpcpy dup ok
  1628. @c libc_dlsym dup @asulock @aculock @acsfd @acsmem
  1629. @c libc_lock_unlock dup ok
  1630. @c nss_next_action ok
  1631. @c *fct.l -> _nss_*_getpwuid_r @ascuplugin
  1632. @c nss_next2 @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1633. @c nss_next_action dup ok
  1634. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1635. @c _nss_files_getpwuid_r @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1636. @c libc_lock_lock dup @asulock @aculock
  1637. @c internal_setent @ascuheap @asulock @aculock @acsmem @acsfd
  1638. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  1639. @c fileno dup ok
  1640. @c fcntl dup ok
  1641. @c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
  1642. @c rewind dup @aculock [stream guarded by non-recursive pwent lock]
  1643. @c internal_getent @mtslocale^
  1644. @c fgets_unlocked dup ok [stream guarded by non-recursive pwent lock]
  1645. @c isspace dup @mtslocale^^
  1646. @c _nss_files_parse_pwent = parse_line ok
  1647. @c strpbrk dup ok
  1648. @c internal_endent @ascuheap @asulock @aculock @acsmem @acsfd
  1649. @c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
  1650. @c libc_lock_unlock dup @aculock
  1651. @c _nss_nis_getpwuid_r ... not fully reviewed (assumed) @asuinit @asulock @acucorrupt @aculock
  1652. @c yp_get_default_domain @asulock @aculock
  1653. @c libc_lock_lock dup @asulock @aculock
  1654. @c getdomainname dup ok
  1655. @c strcmp dup ok
  1656. @c libc_lock_unlock dup @aculock
  1657. @c snprintf dup @ascuheap @acsmem
  1658. @c yp_match
  1659. @c do_ypcall_tr(xdr_ypreq_key,xdr_ypresp_val)
  1660. @c do_ypcall(xdr_ypreq_key,xdr_ypresp_val)
  1661. @c libc_lock_lock @asulock @aculock
  1662. @c strcmp
  1663. @c yp_bind
  1664. @c ypclnt_call
  1665. @c clnt_call
  1666. @c clnt_perror
  1667. @c libc_lock_unlock @aculock
  1668. @c yp_unbind_locked
  1669. @c yp_unbind
  1670. @c strcmp dup ok
  1671. @c calloc dup @asulock @aculock @acsfd @acsmem
  1672. @c yp_bind_file
  1673. @c strlen dup ok
  1674. @c snprintf dup @ascuheap @acsmem
  1675. @c open dup @acsfd [cancelpt]
  1676. @c pread dup [cancelpt]
  1677. @c yp_bind_client_create
  1678. @c close dup @acsfd [cancelpt]
  1679. @c yp_bind_ypbindprog
  1680. @c clnttcp_create
  1681. @c clnt_destroy
  1682. @c clnt_call(xdr_domainname,xdr_ypbind_resp)
  1683. @c memset dup ok
  1684. @c yp_bind_client_create
  1685. @c free dup @asulock @aculock @acsfd @acsmem
  1686. @c calloc dup @asulock @aculock @acsfd @acsmem
  1687. @c free dup @asulock @aculock @acsfd @acsmem
  1688. @c ypprot_err
  1689. @c memcpy dup ok
  1690. @c xdr_free(xdr_ypresp_val)
  1691. @c xdr_ypresp_val
  1692. @c xdr_ypstat
  1693. @c xdr_enum
  1694. @c XDR_PUTLONG
  1695. @c *x_putlong
  1696. @c XDR_GETLONG
  1697. @c *x_getlong
  1698. @c xdr_long
  1699. @c XDR_PUTLONG dup
  1700. @c XDR_GETLONG dup
  1701. @c xdr_short
  1702. @c XDR_PUTLONG dup
  1703. @c XDR_GETLONG dup
  1704. @c xdr_valdat
  1705. @c xdr_bytes
  1706. @c xdr_u_int
  1707. @c XDR_PUTLONG dup
  1708. @c XDR_GETLONG dup
  1709. @c mem_alloc @ascuheap @acsmem
  1710. @c malloc dup @ascuheap @acsmem
  1711. @c xdr_opaque
  1712. @c XDR_GETBYTES
  1713. @c *x_getbytes
  1714. @c XDR_PUTBYTES
  1715. @c *x_putbytes
  1716. @c mem_free @ascuheap @acsmem
  1717. @c free dup @ascuheap @acsmem
  1718. @c yperr2nss ok
  1719. @c strchr dup ok
  1720. @c _nls_default_nss @asuinit @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  1721. @c init @asuinit^, @ascuheap @asulock @acucorrupt @acsmem @acsfd @aculock
  1722. @c fopen dup @ascuheap @asulock @acsmem @acsfd @aculock
  1723. @c fsetlocking ok [no concurrent uses]
  1724. @c feof_unlocked dup ok
  1725. @c getline dup @ascuheap @aculock @acucorrupt @acsmem
  1726. @c isspace dup @mtslocale^^
  1727. @c strncmp dup ok
  1728. @c free dup @asulock @acsmem @acsfd @aculock
  1729. @c fclose dup @ascuheap @asulock @aculock @acsmem @acsfd
  1730. @c free dup @asulock @acsmem @acsfd @aculock
  1731. @c mempcpy dup ok
  1732. @c strncpy dup ok
  1733. @c isspace dup @mtslocale^^
  1734. @c _nss_files_parse_pwent ok
  1735. This function is similar to @code{getpwuid} in that it returns
  1736. information about the user whose user ID is @var{uid}. However, it
  1737. fills the user supplied structure pointed to by @var{result_buf} with
  1738. the information instead of using a static buffer. The first
  1739. @var{buflen} bytes of the additional buffer pointed to by @var{buffer}
  1740. are used to contain additional information, normally strings which are
  1741. pointed to by the elements of the result structure.
  1742. If a user with ID @var{uid} is found, the pointer returned in
  1743. @var{result} points to the record which contains the wanted data (i.e.,
  1744. @var{result} contains the value @var{result_buf}). If no user is found
  1745. or if an error occurred, the pointer returned in @var{result} is a null
  1746. pointer. The function returns zero or an error code. If the buffer
  1747. @var{buffer} is too small to contain all the needed information, the
  1748. error code @code{ERANGE} is returned and @code{errno} is set to
  1749. @code{ERANGE}.
  1750. @end deftypefun
  1751. @deftypefun {struct passwd *} getpwnam (const char *@var{name})
  1752. @standards{POSIX.1, pwd.h}
  1753. @safety{@prelim{}@mtunsafe{@mtasurace{:pwnam} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1754. @c getpwnam @mtasurace:pwnam @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1755. @c libc_lock_lock dup @asulock @aculock
  1756. @c malloc dup @ascuheap @acsmem
  1757. @c getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1758. @c realloc dup @ascuheap @acsmem
  1759. @c free dup @ascuheap @acsmem
  1760. @c libc_lock_unlock dup @aculock
  1761. This function returns a pointer to a statically-allocated structure
  1762. containing information about the user whose user name is @var{name}.
  1763. This structure may be overwritten on subsequent calls to
  1764. @code{getpwnam}.
  1765. A null pointer return indicates there is no user named @var{name}.
  1766. @end deftypefun
  1767. @deftypefun int getpwnam_r (const char *@var{name}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
  1768. @standards{POSIX.1c, pwd.h}
  1769. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1770. @c getpwnam_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1771. @c nscd_getpwnam_r @ascuheap @asulock @aculock @acsfd @acsmem
  1772. @c strlen dup ok
  1773. @c nscd_getpw_r dup @ascuheap @asulock @aculock @acsfd @acsmem
  1774. @c nss_passwd_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1775. @c *fct.l @ascuplugin
  1776. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1777. @c
  1778. @c _nss_files_getpwnam_r @mtslocale @ascuheap @asulock @aculock @acsmem @acsfd
  1779. @c libc_lock_lock dup @asulock @aculock
  1780. @c internal_setent dup @ascuheap @asulock @aculock @acsmem @acsfd
  1781. @c internal_getent dup @mtslocale^
  1782. @c strcmp dup ok
  1783. @c internal_endent dup @ascuheap @asulock @aculock @acsmem @acsfd
  1784. @c libc_lock_unlock dup @aculock
  1785. @c
  1786. @c _nss_*_getpwnam_r (assumed) @asuinit @asulock @acucorrupt @aculock
  1787. This function is similar to @code{getpwnam} in that it returns
  1788. information about the user whose user name is @var{name}. However, like
  1789. @code{getpwuid_r}, it fills the user supplied buffers in
  1790. @var{result_buf} and @var{buffer} with the information instead of using
  1791. a static buffer.
  1792. The return values are the same as for @code{getpwuid_r}.
  1793. @end deftypefun
  1794. @node Scanning All Users
  1795. @subsection Scanning the List of All Users
  1796. @cindex scanning the user list
  1797. This section explains how a program can read the list of all users in
  1798. the system, one user at a time. The functions described here are
  1799. declared in @file{pwd.h}.
  1800. You can use the @code{fgetpwent} function to read user entries from a
  1801. particular file.
  1802. @deftypefun {struct passwd *} fgetpwent (FILE *@var{stream})
  1803. @standards{SVID, pwd.h}
  1804. @safety{@prelim{}@mtunsafe{@mtasurace{:fpwent}}@asunsafe{@asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{}}}
  1805. @c fgetpwent @mtasurace:fpwent @asucorrupt @asulock @acucorrupt @aculock
  1806. @c fgetpos dup @asucorrupt @aculock @acucorrupt
  1807. @c libc_lock_lock dup @asulock @aculock
  1808. @c malloc dup @ascuheap @acsmem
  1809. @c fgetpwent_r dup @asucorrupt @acucorrupt @aculock
  1810. @c realloc dup @ascuheap @acsmem
  1811. @c free dup @ascuheap @acsmem
  1812. @c fsetpos dup @asucorrupt @aculock @acucorrupt
  1813. @c libc_lock_unlock dup @aculock
  1814. This function reads the next user entry from @var{stream} and returns a
  1815. pointer to the entry. The structure is statically allocated and is
  1816. rewritten on subsequent calls to @code{fgetpwent}. You must copy the
  1817. contents of the structure if you wish to save the information.
  1818. The stream must correspond to a file in the same format as the standard
  1819. user database file.
  1820. @end deftypefun
  1821. @deftypefun int fgetpwent_r (FILE *@var{stream}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
  1822. @standards{GNU, pwd.h}
  1823. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  1824. @c fgetpwent_r @asucorrupt @acucorrupt @aculock
  1825. @c flockfile dup @aculock
  1826. @c fgets_unlocked @asucorrupt @acucorrupt [no @mtsrace due to explicit locking]
  1827. @c feof_unlocked dup ok
  1828. @c funlockfile dup @aculock
  1829. @c isspace dup @mtslocale^^
  1830. @c parse_line dup ok
  1831. This function is similar to @code{fgetpwent} in that it reads the next
  1832. user entry from @var{stream}. But the result is returned in the
  1833. structure pointed to by @var{result_buf}. The
  1834. first @var{buflen} bytes of the additional buffer pointed to by
  1835. @var{buffer} are used to contain additional information, normally
  1836. strings which are pointed to by the elements of the result structure.
  1837. The stream must correspond to a file in the same format as the standard
  1838. user database file.
  1839. If the function returns zero @var{result} points to the structure with
  1840. the wanted data (normally this is in @var{result_buf}). If errors
  1841. occurred the return value is nonzero and @var{result} contains a null
  1842. pointer.
  1843. @end deftypefun
  1844. The way to scan all the entries in the user database is with
  1845. @code{setpwent}, @code{getpwent}, and @code{endpwent}.
  1846. @deftypefun void setpwent (void)
  1847. @standards{SVID, pwd.h}
  1848. @standards{BSD, pwd.h}
  1849. @safety{@prelim{}@mtunsafe{@mtasurace{:pwent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1850. @c setpwent @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1851. @c libc_lock_lock @asulock @aculock
  1852. @c nss_setent(nss_passwd_lookup2) @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1853. @c ** resolv's res_maybe_init not called here
  1854. @c setup(nss_passwd_lookup2) @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1855. @c *lookup_fct = nss_passwd_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1856. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1857. @c *fct.f @mtasurace:pwent @ascuplugin
  1858. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1859. @c libc_lock_unlock @aculock
  1860. This function initializes a stream which @code{getpwent} and
  1861. @code{getpwent_r} use to read the user database.
  1862. @end deftypefun
  1863. @deftypefun {struct passwd *} getpwent (void)
  1864. @standards{POSIX.1, pwd.h}
  1865. @safety{@prelim{}@mtunsafe{@mtasurace{:pwent} @mtasurace{:pwentbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1866. @c getpwent @mtasurace:pwent @mtasurace:pwentbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1867. @c libc_lock_lock dup @asulock @aculock
  1868. @c nss_getent(getpwent_r) @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1869. @c malloc dup @ascuheap @acsmem
  1870. @c *func = getpwent_r dup @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1871. @c realloc dup @ascuheap @acsmem
  1872. @c free dup @ascuheap @acsmem
  1873. @c libc_lock_unlock dup @aculock
  1874. The @code{getpwent} function reads the next entry from the stream
  1875. initialized by @code{setpwent}. It returns a pointer to the entry. The
  1876. structure is statically allocated and is rewritten on subsequent calls
  1877. to @code{getpwent}. You must copy the contents of the structure if you
  1878. wish to save the information.
  1879. A null pointer is returned when no more entries are available.
  1880. @end deftypefun
  1881. @deftypefun int getpwent_r (struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
  1882. @standards{GNU, pwd.h}
  1883. @safety{@prelim{}@mtunsafe{@mtasurace{:pwent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1884. @c The static buffer here is not the result_buf, but rather the
  1885. @c variables that keep track of what nss backend we've last used, and
  1886. @c whatever internal state the nss backend uses to keep track of the
  1887. @c last read entry.
  1888. @c getpwent_r @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1889. @c libc_lock_lock dup @asulock @aculock
  1890. @c nss_getent_r(nss_passwd_lookup2) @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1891. @c setup(nss_passwd_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1892. @c *fct.f @mtasurace:pwent @ascuplugin
  1893. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1894. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1895. @c *sfct.f @mtasurace:pwent @ascuplugin
  1896. @c libc_lock_unlock dup @aculock
  1897. This function is similar to @code{getpwent} in that it returns the next
  1898. entry from the stream initialized by @code{setpwent}. Like
  1899. @code{fgetpwent_r}, it uses the user-supplied buffers in
  1900. @var{result_buf} and @var{buffer} to return the information requested.
  1901. The return values are the same as for @code{fgetpwent_r}.
  1902. @end deftypefun
  1903. @deftypefun void endpwent (void)
  1904. @standards{SVID, pwd.h}
  1905. @standards{BSD, pwd.h}
  1906. @safety{@prelim{}@mtunsafe{@mtasurace{:pwent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1907. @c endpwent @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1908. @c libc_lock_lock @asulock @aculock
  1909. @c nss_endent(nss_passwd_lookup2) @mtasurace:pwent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1910. @c ** resolv's res_maybe_init not called here
  1911. @c setup(nss_passwd_lookup2) dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1912. @c *fct.f @mtasurace:pwent @ascuplugin
  1913. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1914. @c libc_lock_unlock @aculock
  1915. This function closes the internal stream used by @code{getpwent} or
  1916. @code{getpwent_r}.
  1917. @end deftypefun
  1918. @node Writing a User Entry
  1919. @subsection Writing a User Entry
  1920. @deftypefun int putpwent (const struct passwd *@var{p}, FILE *@var{stream})
  1921. @standards{SVID, pwd.h}
  1922. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
  1923. @c putpwent @mtslocale @asucorrupt @aculock @acucorrupt
  1924. @c fprintf dup @mtslocale @asucorrupt @aculock @acucorrupt [no @ascuheap @acsmem]
  1925. This function writes the user entry @code{*@var{p}} to the stream
  1926. @var{stream}, in the format used for the standard user database
  1927. file. The return value is zero on success and nonzero on failure.
  1928. This function exists for compatibility with SVID. We recommend that you
  1929. avoid using it, because it makes sense only on the assumption that the
  1930. @code{struct passwd} structure has no members except the standard ones;
  1931. on a system which merges the traditional Unix data base with other
  1932. extended information about users, adding an entry using this function
  1933. would inevitably leave out much of the important information.
  1934. The group and user ID fields are left empty if the group or user name
  1935. starts with a - or +.
  1936. The function @code{putpwent} is declared in @file{pwd.h}.
  1937. @end deftypefun
  1938. @node Group Database
  1939. @section Group Database
  1940. @cindex group database
  1941. @pindex /etc/group
  1942. This section describes how to search and scan the database of
  1943. registered groups. The database itself is kept in the file
  1944. @file{/etc/group} on most systems, but on some systems a special network
  1945. service provides access to it.
  1946. @menu
  1947. * Group Data Structure:: What each group record contains.
  1948. * Lookup Group:: How to look for a particular group.
  1949. * Scanning All Groups:: Scanning the list of all groups.
  1950. @end menu
  1951. @node Group Data Structure
  1952. @subsection The Data Structure for a Group
  1953. The functions and data structures for accessing the system group
  1954. database are declared in the header file @file{grp.h}.
  1955. @pindex grp.h
  1956. @deftp {Data Type} {struct group}
  1957. @standards{POSIX.1, grp.h}
  1958. The @code{group} structure is used to hold information about an entry in
  1959. the system group database. It has at least the following members:
  1960. @table @code
  1961. @item char *gr_name
  1962. The name of the group.
  1963. @item gid_t gr_gid
  1964. The group ID of the group.
  1965. @item char **gr_mem
  1966. A vector of pointers to the names of users in the group. Each user name
  1967. is a null-terminated string, and the vector itself is terminated by a
  1968. null pointer.
  1969. @end table
  1970. @end deftp
  1971. @node Lookup Group
  1972. @subsection Looking Up One Group
  1973. @cindex converting group name to group ID
  1974. @cindex converting group ID to group name
  1975. You can search the group database for information about a specific
  1976. group using @code{getgrgid} or @code{getgrnam}. These functions are
  1977. declared in @file{grp.h}.
  1978. @deftypefun {struct group *} getgrgid (gid_t @var{gid})
  1979. @standards{POSIX.1, grp.h}
  1980. @safety{@prelim{}@mtunsafe{@mtasurace{:grgid} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1981. @c getgrgid =~ getpwuid dup @mtasurace:grgid @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1982. @c getgrgid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1983. This function returns a pointer to a statically-allocated structure
  1984. containing information about the group whose group ID is @var{gid}.
  1985. This structure may be overwritten by subsequent calls to
  1986. @code{getgrgid}.
  1987. A null pointer indicates there is no group with ID @var{gid}.
  1988. @end deftypefun
  1989. @deftypefun int getgrgid_r (gid_t @var{gid}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
  1990. @standards{POSIX.1c, grp.h}
  1991. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  1992. @c getgrgid_r =~ getpwuid_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  1993. @c nscd_getgrgid_r @ascuheap @acsfd @acsmem
  1994. @c itoa_word dup ok
  1995. @c nscd_getgr_r @ascuheap @acsfd @acsmem
  1996. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  1997. @c nscd_cache_search dup ok
  1998. @c nscd_open_socket dup @acsfd
  1999. @c readvall ok
  2000. @c readv dup ok
  2001. @c memcpy dup ok
  2002. @c wait_on_socket dup ok
  2003. @c memcpy dup ok
  2004. @c readall dup ok
  2005. @c close_not_cancel_no_status dup @acsfd
  2006. @c nscd_drop_map_ref dup @ascuheap @acsmem
  2007. @c nscd_unmap dup @ascuheap @acsmem
  2008. @c nss_group_lookup2 =~ nss_passwd_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2009. @c *fct.l -> _nss_*_getgrgid_r @ascuplugin
  2010. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2011. This function is similar to @code{getgrgid} in that it returns
  2012. information about the group whose group ID is @var{gid}. However, it
  2013. fills the user supplied structure pointed to by @var{result_buf} with
  2014. the information instead of using a static buffer. The first
  2015. @var{buflen} bytes of the additional buffer pointed to by @var{buffer}
  2016. are used to contain additional information, normally strings which are
  2017. pointed to by the elements of the result structure.
  2018. If a group with ID @var{gid} is found, the pointer returned in
  2019. @var{result} points to the record which contains the wanted data (i.e.,
  2020. @var{result} contains the value @var{result_buf}). If no group is found
  2021. or if an error occurred, the pointer returned in @var{result} is a null
  2022. pointer. The function returns zero or an error code. If the buffer
  2023. @var{buffer} is too small to contain all the needed information, the
  2024. error code @code{ERANGE} is returned and @code{errno} is set to
  2025. @code{ERANGE}.
  2026. @end deftypefun
  2027. @deftypefun {struct group *} getgrnam (const char *@var{name})
  2028. @standards{SVID, grp.h}
  2029. @standards{BSD, grp.h}
  2030. @safety{@prelim{}@mtunsafe{@mtasurace{:grnam} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2031. @c getgrnam =~ getpwnam dup @mtasurace:grnam @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2032. @c getgrnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2033. This function returns a pointer to a statically-allocated structure
  2034. containing information about the group whose group name is @var{name}.
  2035. This structure may be overwritten by subsequent calls to
  2036. @code{getgrnam}.
  2037. A null pointer indicates there is no group named @var{name}.
  2038. @end deftypefun
  2039. @deftypefun int getgrnam_r (const char *@var{name}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
  2040. @standards{POSIX.1c, grp.h}
  2041. @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2042. @c getgrnam_r =~ getpwnam_r dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2043. @c nscd_getgrnam_r @ascuheap @asulock @aculock @acsfd @acsmem
  2044. @c strlen dup ok
  2045. @c nscd_getgr_r dup @ascuheap @asulock @aculock @acsfd @acsmem
  2046. @c nss_group_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2047. @c *fct.l @ascuplugin
  2048. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2049. This function is similar to @code{getgrnam} in that it returns
  2050. information about the group whose group name is @var{name}. Like
  2051. @code{getgrgid_r}, it uses the user supplied buffers in
  2052. @var{result_buf} and @var{buffer}, not a static buffer.
  2053. The return values are the same as for @code{getgrgid_r}.
  2054. @end deftypefun
  2055. @node Scanning All Groups
  2056. @subsection Scanning the List of All Groups
  2057. @cindex scanning the group list
  2058. This section explains how a program can read the list of all groups in
  2059. the system, one group at a time. The functions described here are
  2060. declared in @file{grp.h}.
  2061. You can use the @code{fgetgrent} function to read group entries from a
  2062. particular file.
  2063. @deftypefun {struct group *} fgetgrent (FILE *@var{stream})
  2064. @standards{SVID, grp.h}
  2065. @safety{@prelim{}@mtunsafe{@mtasurace{:fgrent}}@asunsafe{@asucorrupt{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{}}}
  2066. @c fgetgrent @mtasurace:fgrent @asucorrupt @asulock @acucorrupt @aculock
  2067. @c fgetpos dup @asucorrupt @aculock @acucorrupt
  2068. @c libc_lock_lock dup @asulock @aculock
  2069. @c malloc dup @ascuheap @acsmem
  2070. @c fgetgrent_r dup @asucorrupt @acucorrupt @aculock
  2071. @c realloc dup @ascuheap @acsmem
  2072. @c free dup @ascuheap @acsmem
  2073. @c fsetpos dup @asucorrupt @aculock @acucorrupt
  2074. @c libc_lock_unlock dup @aculock
  2075. The @code{fgetgrent} function reads the next entry from @var{stream}.
  2076. It returns a pointer to the entry. The structure is statically
  2077. allocated and is overwritten on subsequent calls to @code{fgetgrent}. You
  2078. must copy the contents of the structure if you wish to save the
  2079. information.
  2080. The stream must correspond to a file in the same format as the standard
  2081. group database file.
  2082. @end deftypefun
  2083. @deftypefun int fgetgrent_r (FILE *@var{stream}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
  2084. @standards{GNU, grp.h}
  2085. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
  2086. @c fgetgrent_r @asucorrupt @acucorrupt @aculock
  2087. @c flockfile dup @aculock
  2088. @c fgets_unlocked @asucorrupt @acucorrupt [no @mtsrace due to explicit locking]
  2089. @c feof_unlocked dup ok
  2090. @c funlockfile dup @aculock
  2091. @c isspace dup @mtslocale^^
  2092. @c parse_line dup ok
  2093. This function is similar to @code{fgetgrent} in that it reads the next
  2094. user entry from @var{stream}. But the result is returned in the
  2095. structure pointed to by @var{result_buf}. The first @var{buflen} bytes
  2096. of the additional buffer pointed to by @var{buffer} are used to contain
  2097. additional information, normally strings which are pointed to by the
  2098. elements of the result structure.
  2099. This stream must correspond to a file in the same format as the standard
  2100. group database file.
  2101. If the function returns zero @var{result} points to the structure with
  2102. the wanted data (normally this is in @var{result_buf}). If errors
  2103. occurred the return value is non-zero and @var{result} contains a null
  2104. pointer.
  2105. @end deftypefun
  2106. The way to scan all the entries in the group database is with
  2107. @code{setgrent}, @code{getgrent}, and @code{endgrent}.
  2108. @deftypefun void setgrent (void)
  2109. @standards{SVID, grp.h}
  2110. @standards{BSD, grp.h}
  2111. @safety{@prelim{}@mtunsafe{@mtasurace{:grent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2112. @c setgrent =~ setpwent dup @mtasurace:grent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2113. @c ...*lookup_fct = nss_group_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2114. This function initializes a stream for reading from the group data base.
  2115. You use this stream by calling @code{getgrent} or @code{getgrent_r}.
  2116. @end deftypefun
  2117. @deftypefun {struct group *} getgrent (void)
  2118. @standards{SVID, grp.h}
  2119. @standards{BSD, grp.h}
  2120. @safety{@prelim{}@mtunsafe{@mtasurace{:grent} @mtasurace{:grentbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2121. @c getgrent =~ getpwent dup @mtasurace:grent @mtasurace:grentbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2122. @c *func = getgrent_r dup @mtasurace:grent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2123. The @code{getgrent} function reads the next entry from the stream
  2124. initialized by @code{setgrent}. It returns a pointer to the entry. The
  2125. structure is statically allocated and is overwritten on subsequent calls
  2126. to @code{getgrent}. You must copy the contents of the structure if you
  2127. wish to save the information.
  2128. @end deftypefun
  2129. @deftypefun int getgrent_r (struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
  2130. @standards{GNU, grp.h}
  2131. @safety{@prelim{}@mtunsafe{@mtasurace{:grent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2132. @c getgrent_r =~ getpwent_r dup @mtasurace:grent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2133. This function is similar to @code{getgrent} in that it returns the next
  2134. entry from the stream initialized by @code{setgrent}. Like
  2135. @code{fgetgrent_r}, it places the result in user-supplied buffers
  2136. pointed to by @var{result_buf} and @var{buffer}.
  2137. If the function returns zero @var{result} contains a pointer to the data
  2138. (normally equal to @var{result_buf}). If errors occurred the return
  2139. value is non-zero and @var{result} contains a null pointer.
  2140. @end deftypefun
  2141. @deftypefun void endgrent (void)
  2142. @standards{SVID, grp.h}
  2143. @standards{BSD, grp.h}
  2144. @safety{@prelim{}@mtunsafe{@mtasurace{:grent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2145. @c endgrent =~ endpwent dup @mtasurace:grent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2146. This function closes the internal stream used by @code{getgrent} or
  2147. @code{getgrent_r}.
  2148. @end deftypefun
  2149. @node Database Example
  2150. @section User and Group Database Example
  2151. Here is an example program showing the use of the system database inquiry
  2152. functions. The program prints some information about the user running
  2153. the program.
  2154. @smallexample
  2155. @include db.c.texi
  2156. @end smallexample
  2157. Here is some output from this program:
  2158. @smallexample
  2159. I am Throckmorton Snurd.
  2160. My login name is snurd.
  2161. My uid is 31093.
  2162. My home directory is /home/fsg/snurd.
  2163. My default shell is /bin/sh.
  2164. My default group is guest (12).
  2165. The members of this group are:
  2166. friedman
  2167. tami
  2168. @end smallexample
  2169. @node Netgroup Database
  2170. @section Netgroup Database
  2171. @menu
  2172. * Netgroup Data:: Data in the Netgroup database and where
  2173. it comes from.
  2174. * Lookup Netgroup:: How to look for a particular netgroup.
  2175. * Netgroup Membership:: How to test for netgroup membership.
  2176. @end menu
  2177. @node Netgroup Data
  2178. @subsection Netgroup Data
  2179. @cindex Netgroup
  2180. Sometimes it is useful to group users according to other criteria
  2181. (@pxref{Group Database}). E.g., it is useful to associate a certain
  2182. group of users with a certain machine. On the other hand grouping of
  2183. host names is not supported so far.
  2184. In Sun Microsystems' SunOS appeared a new kind of database, the netgroup
  2185. database. It allows grouping hosts, users, and domains freely, giving
  2186. them individual names. To be more concrete, a netgroup is a list of triples
  2187. consisting of a host name, a user name, and a domain name where any of
  2188. the entries can be a wildcard entry matching all inputs. A last
  2189. possibility is that names of other netgroups can also be given in the
  2190. list specifying a netgroup. So one can construct arbitrary hierarchies
  2191. without loops.
  2192. Sun's implementation allows netgroups only for the @code{nis} or
  2193. @code{nisplus} service, @pxref{Services in the NSS configuration}. The
  2194. implementation in @theglibc{} has no such restriction. An entry
  2195. in either of the input services must have the following form:
  2196. @smallexample
  2197. @var{groupname} ( @var{groupname} | @code{(}@var{hostname}@code{,}@var{username}@code{,}@code{domainname}@code{)} )+
  2198. @end smallexample
  2199. Any of the fields in the triple can be empty which means anything
  2200. matches. While describing the functions we will see that the opposite
  2201. case is useful as well. I.e., there may be entries which will not
  2202. match any input. For entries like this, a name consisting of the single
  2203. character @code{-} shall be used.
  2204. @node Lookup Netgroup
  2205. @subsection Looking up one Netgroup
  2206. The lookup functions for netgroups are a bit different than all other
  2207. system database handling functions. Since a single netgroup can contain
  2208. many entries a two-step process is needed. First a single netgroup is
  2209. selected and then one can iterate over all entries in this netgroup.
  2210. These functions are declared in @file{netdb.h}.
  2211. @deftypefun int setnetgrent (const char *@var{netgroup})
  2212. @standards{BSD, netdb.h}
  2213. @safety{@prelim{}@mtunsafe{@mtasurace{:netgrent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2214. @c setnetgrent @mtasurace:netgrent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2215. @c libc_lock_lock dup @asulock @aculock
  2216. @c nscd_setnetgrent @ascuheap @acsfd @acsmem
  2217. @c __nscd_setnetgrent @ascuheap @acsfd @acsmem
  2218. @c strlen dup ok
  2219. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  2220. @c nscd_cache_search dup ok
  2221. @c nscd_open_socket dup @acsfd
  2222. @c malloc dup @ascuheap @acsmem
  2223. @c readall dup ok
  2224. @c free dup @ascuheap @acsmem
  2225. @c close_not_cancel_no_status dup @acsfd
  2226. @c nscd_drop_map_ref dup @ascuheap @acsmem
  2227. @c nscd_unmap dup @ascuheap @acsmem
  2228. @c internal_setnetgrent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2229. @c free_memory dup @ascuheap @acsmem
  2230. @c free dup @ascuheap @acsmem
  2231. @c internal_setnetgrent_reuse @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2232. @c endnetgrent_hook dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2233. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2234. @c *endfct @ascuplugin
  2235. @c (netgroup::)setup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2236. @c nss_netgroup_lookup dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2237. @c nss_netgroup_lookup2 =~ nss_passwd_lookup2 dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2238. @c nss_lookup dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2239. @c *fct.f @ascuplugin
  2240. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2241. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2242. @c *endfct @ascuplugin
  2243. @c strlen dup ok
  2244. @c malloc dup @ascuheap @acsmem
  2245. @c memcpy dup ok
  2246. @c libc_lock_unlock dup @aculock
  2247. A call to this function initializes the internal state of the library to
  2248. allow following calls of @code{getnetgrent} to iterate over all entries
  2249. in the netgroup with name @var{netgroup}.
  2250. When the call is successful (i.e., when a netgroup with this name exists)
  2251. the return value is @code{1}. When the return value is @code{0} no
  2252. netgroup of this name is known or some other error occurred.
  2253. @end deftypefun
  2254. It is important to remember that there is only one single state for
  2255. iterating the netgroups. Even if the programmer uses the
  2256. @code{getnetgrent_r} function the result is not really reentrant since
  2257. always only one single netgroup at a time can be processed. If the
  2258. program needs to process more than one netgroup simultaneously she
  2259. must protect this by using external locking. This problem was
  2260. introduced in the original netgroups implementation in SunOS and since
  2261. we must stay compatible it is not possible to change this.
  2262. Some other functions also use the netgroups state. Currently these are
  2263. the @code{innetgr} function and parts of the implementation of the
  2264. @code{compat} service part of the NSS implementation.
  2265. @deftypefun int getnetgrent (char **@var{hostp}, char **@var{userp}, char **@var{domainp})
  2266. @standards{BSD, netdb.h}
  2267. @safety{@prelim{}@mtunsafe{@mtasurace{:netgrent} @mtasurace{:netgrentbuf} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2268. @c getnetgrent @mtasurace:netgrent @mtasurace:netgrentbuf @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2269. @c uses unsafely a static buffer allocated within a libc_once call
  2270. @c allocate (libc_once) @ascuheap @acsmem
  2271. @c malloc dup @ascuheap @acsmem
  2272. @c getnetgrent_r dup @mtasurace:netgrent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2273. This function returns the next unprocessed entry of the currently
  2274. selected netgroup. The string pointers, in which addresses are passed in
  2275. the arguments @var{hostp}, @var{userp}, and @var{domainp}, will contain
  2276. after a successful call pointers to appropriate strings. If the string
  2277. in the next entry is empty the pointer has the value @code{NULL}.
  2278. The returned string pointers are only valid if none of the netgroup
  2279. related functions are called.
  2280. The return value is @code{1} if the next entry was successfully read. A
  2281. value of @code{0} means no further entries exist or internal errors occurred.
  2282. @end deftypefun
  2283. @deftypefun int getnetgrent_r (char **@var{hostp}, char **@var{userp}, char **@var{domainp}, char *@var{buffer}, size_t @var{buflen})
  2284. @standards{GNU, netdb.h}
  2285. @safety{@prelim{}@mtunsafe{@mtasurace{:netgrent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2286. @c getnetgrent_r @mtasurace:netgrent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2287. @c libc_lock_lock dup @asulock @aculock
  2288. @c internal_getnetgrent_r @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2289. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2290. @c *fct @ascuplugin
  2291. @c nscd_getnetgrent ok
  2292. @c rawmemchr dup ok
  2293. @c internal_setnetgrent_reuse dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2294. @c strcmp dup ok
  2295. @c malloc dup @ascuheap @acsmem
  2296. @c memcpy dup ok
  2297. @c libc_lock_unlock dup @aculock
  2298. This function is similar to @code{getnetgrent} with only one exception:
  2299. the strings the three string pointers @var{hostp}, @var{userp}, and
  2300. @var{domainp} point to, are placed in the buffer of @var{buflen} bytes
  2301. starting at @var{buffer}. This means the returned values are valid
  2302. even after other netgroup related functions are called.
  2303. The return value is @code{1} if the next entry was successfully read and
  2304. the buffer contains enough room to place the strings in it. @code{0} is
  2305. returned in case no more entries are found, the buffer is too small, or
  2306. internal errors occurred.
  2307. This function is a GNU extension. The original implementation in the
  2308. SunOS libc does not provide this function.
  2309. @end deftypefun
  2310. @deftypefun void endnetgrent (void)
  2311. @standards{BSD, netdb.h}
  2312. @safety{@prelim{}@mtunsafe{@mtasurace{:netgrent}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2313. @c endnetgrent @mtasurace:netgrent @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2314. @c libc_lock_lock dup @asulock @aculock
  2315. @c internal_endnetgrent @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2316. @c endnetgrent_hook dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2317. @c free_memory dup @ascuheap @acsmem
  2318. @c libc_lock_unlock dup @aculock
  2319. This function frees all buffers which were allocated to process the last
  2320. selected netgroup. As a result all string pointers returned by calls
  2321. to @code{getnetgrent} are invalid afterwards.
  2322. @end deftypefun
  2323. @node Netgroup Membership
  2324. @subsection Testing for Netgroup Membership
  2325. It is often not necessary to scan the whole netgroup since often the
  2326. only interesting question is whether a given entry is part of the
  2327. selected netgroup.
  2328. @deftypefun int innetgr (const char *@var{netgroup}, const char *@var{host}, const char *@var{user}, const char *@var{domain})
  2329. @standards{BSD, netdb.h}
  2330. @safety{@prelim{}@mtunsafe{@mtasurace{:netgrent} @mtslocale{}}@asunsafe{@ascudlopen{} @ascuplugin{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsfd{} @acsmem{}}}
  2331. @c This function does not use the static data structure that the
  2332. @c *netgrent* ones do, but since each nss must maintains internal state
  2333. @c to support iteration and concurrent iteration will interfere
  2334. @c destructively, we regard this internal state as a static buffer.
  2335. @c getnetgrent_r iteration in each nss backend.
  2336. @c innetgr @mtasurace:netgrent @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2337. @c nscd_innetgr @ascuheap @acsfd @acsmem
  2338. @c strlen dup ok
  2339. @c malloc dup @ascuheap @acsmem
  2340. @c stpcpy dup ok
  2341. @c nscd_get_map_ref dup @ascuheap @acsfd @acsmem
  2342. @c nscd_cache_search dup ok
  2343. @c nscd_open_socket dup @acsfd
  2344. @c close_not_cancel_no_status dup @acsfd
  2345. @c nscd_drop_map_ref dup @ascuheap @acsmem
  2346. @c nscd_unmap dup @ascuheap @acsmem
  2347. @c free dup @ascuheap @acsmem
  2348. @c memset dup ok
  2349. @c (netgroup::)setup dup @mtslocale @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2350. @c *setfct.f @ascuplugin
  2351. @c nss_lookup_function dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2352. @c *getfct @ascuplugin
  2353. @c strcmp dup ok
  2354. @c strlen dup ok
  2355. @c malloc dup @ascuheap @acsmem
  2356. @c memcpy dup ok
  2357. @c strcasecmp dup
  2358. @c *endfct @ascuplugin
  2359. @c nss_next2 dup @ascudlopen @ascuplugin @ascuheap @asulock @acucorrupt @aculock @acsfd @acsmem
  2360. @c free_memory dup @ascuheap @acsmem
  2361. This function tests whether the triple specified by the parameters
  2362. @var{host}, @var{user}, and @var{domain} is part of the netgroup
  2363. @var{netgroup}. Using this function has the advantage that
  2364. @enumerate
  2365. @item
  2366. no other netgroup function can use the global netgroup state since
  2367. internal locking is used and
  2368. @item
  2369. the function is implemented more efficiently than successive calls
  2370. to the other @code{set}/@code{get}/@code{endnetgrent} functions.
  2371. @end enumerate
  2372. Any of the pointers @var{host}, @var{user}, or @var{domain} can be
  2373. @code{NULL} which means any value is accepted in this position. This is
  2374. also true for the name @code{-} which should not match any other string
  2375. otherwise.
  2376. The return value is @code{1} if an entry matching the given triple is
  2377. found in the netgroup. The return value is @code{0} if the netgroup
  2378. itself is not found, the netgroup does not contain the triple or
  2379. internal errors occurred.
  2380. @end deftypefun
  2381. @c FIXME these are undocumented:
  2382. @c setresgid
  2383. @c setresuid