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