Update.
[glibc.git] / manual / users.texi
blob5ffd5fd99037d65e2cc7965724f9f787fe0a5e00
1 @node Users and Groups
2 @chapter Users and Groups
4 Every user who can log in on the system is identified by a unique number
5 called the @dfn{user ID}.  Each process has an effective user ID which
6 says which user's access permissions it has.
8 Users are classified into @dfn{groups} for access control purposes.  Each
9 process has one or more @dfn{group ID values} which say which groups the
10 process can use for access to files.
12 The effective user and group IDs of a process collectively form its
13 @dfn{persona}.  This determines which files the process can access.
14 Normally, a process inherits its persona from the parent process, but
15 under special circumstances a process can change its persona and thus
16 change its access permissions.
18 Each file in the system also has a user ID and a group ID.  Access
19 control works by comparing the user and group IDs of the file with those
20 of the running process.
22 The system keeps a database of all the registered users, and another
23 database of all the defined groups.  There are library functions you
24 can use to examine these databases.
26 @menu
27 * User and Group IDs::          Each user has a unique numeric ID;
28                                  likewise for groups.
29 * Process Persona::             The user IDs and group IDs of a process.
30 * Why Change Persona::          Why a program might need to change
31                                  its user and/or group IDs.
32 * How Change Persona::          Changing the user and group IDs.
33 * Reading Persona::             How to examine the user and group IDs.
35 * Setting User ID::             Functions for setting the user ID.
36 * Setting Groups::              Functions for setting the group IDs.
38 * Enable/Disable Setuid::       Turning setuid access on and off.
39 * Setuid Program Example::      The pertinent parts of one sample program.
40 * Tips for Setuid::             How to avoid granting unlimited access.
42 * Who Logged In::               Getting the name of the user who logged in,
43                                  or of the real user ID of the current process.
45 * User Accounting Database::    Keeping information about users and various
46                                  actions in databases.
48 * User Database::               Functions and data structures for
49                                  accessing the user database.
50 * Group Database::              Functions and data structures for
51                                  accessing the group database.
52 * Database Example::            Example program showing the use of database
53                                  inquiry functions.
54 * Netgroup Database::           Functions for accessing the netgroup database.
55 @end menu
57 @node User and Group IDs
58 @section User and Group IDs
60 @cindex login name
61 @cindex user name
62 @cindex user ID
63 Each user account on a computer system is identified by a @dfn{user
64 name} (or @dfn{login name}) and @dfn{user ID}.  Normally, each user name
65 has a unique user ID, but it is possible for several login names to have
66 the same user ID.  The user names and corresponding user IDs are stored
67 in a data base which you can access as described in @ref{User Database}.
69 @cindex group name
70 @cindex group ID
71 Users are classified in @dfn{groups}.  Each user name belongs to one
72 @dfn{default group} and may also belong to any number of
73 @dfn{supplementary groups}. Users who are members of the same group can
74 share resources (such as files) that are not accessible to users who are
75 not a member of that group.  Each group has a @dfn{group name} and
76 @dfn{group ID}.  @xref{Group Database}, for how to find information
77 about a group ID or group name.
79 @node Process Persona
80 @section The Persona of a Process
81 @cindex persona
82 @cindex effective user ID
83 @cindex effective group ID
84 @cindex supplementary group IDs
86 @c When Hurd is more widely used, explain multiple effective user IDs
87 @c here. -zw
88 At any time, each process has an @dfn{effective user ID}, a @dfn{effective
89 group ID}, and a set of @dfn{supplementary group IDs}.  These IDs
90 determine the privileges of the process.  They are collectively
91 called the @dfn{persona} of the process, because they determine ``who it
92 is'' for purposes of access control.
94 Your login shell starts out with a persona which consists of your user
95 ID, your default group ID, and your supplementary group IDs (if you are
96 in more than one group).  In normal circumstances, all your other processes
97 inherit these values.
99 @cindex real user ID
100 @cindex real group ID
101 A process also has a @dfn{real user ID} which identifies the user who
102 created the process, and a @dfn{real group ID} which identifies that
103 user's default group.  These values do not play a role in access
104 control, so we do not consider them part of the persona.  But they are
105 also important.
107 Both the real and effective user ID can be changed during the lifetime
108 of a process.  @xref{Why Change Persona}.
110 For details on how a process's effective user ID and group IDs affect
111 its permission to access files, see @ref{Access Permission}.
113 The effective user ID of a process also controls permissions for sending
114 signals using the @code{kill} function.  @xref{Signaling Another
115 Process}.
117 Finally, there are many operations which can only be performed by a
118 process whose effective user ID is zero.  A process with this user ID is
119 a @dfn{privileged process}.  Commonly the user name @code{root} is
120 associated with user ID 0, but there may be other user names with this
122 @c !!! should mention POSIX capabilities here.
124 @node Why Change Persona
125 @section Why Change the Persona of a Process?
127 The most obvious situation where it is necessary for a process to change
128 its user and/or group IDs is the @code{login} program.  When
129 @code{login} starts running, its user ID is @code{root}.  Its job is to
130 start a shell whose user and group IDs are those of the user who is
131 logging in.  (To accomplish this fully, @code{login} must set the real
132 user and group IDs as well as its persona.  But this is a special case.)
134 The more common case of changing persona is when an ordinary user
135 program needs access to a resource that wouldn't ordinarily be
136 accessible to the user actually running it.
138 For example, you may have a file that is controlled by your program but
139 that shouldn't be read or modified directly by other users, either
140 because it implements some kind of locking protocol, or because you want
141 to preserve the integrity or privacy of the information it contains.
142 This kind of restricted access can be implemented by having the program
143 change its effective user or group ID to match that of the resource.
145 Thus, imagine a game program that saves scores in a file.  The game
146 program itself needs to be able to update this file no matter who is
147 running it, but if users can write the file without going through the
148 game, they can give themselves any scores they like.  Some people
149 consider this undesirable, or even reprehensible.  It can be prevented
150 by creating a new user ID and login name (say, @code{games}) to own the
151 scores file, and make the file writable only by this user.  Then, when
152 the game program wants to update this file, it can change its effective
153 user ID to be that for @code{games}.  In effect, the program must
154 adopt the persona of @code{games} so it can write the scores file.
156 @node How Change Persona
157 @section How an Application Can Change Persona
158 @cindex @code{setuid} programs
159 @cindex saved set-user-ID
160 @cindex saved set-group-ID
161 @cindex @code{_POSIX_SAVED_IDS}
163 The ability to change the persona of a process can be a source of
164 unintentional privacy violations, or even intentional abuse.  Because of
165 the potential for problems, changing persona is restricted to special
166 circumstances.
168 You can't arbitrarily set your user ID or group ID to anything you want;
169 only privileged processes can do that.  Instead, the normal way for a
170 program to change its persona is that it has been set up in advance to
171 change to a particular user or group.  This is the function of the setuid
172 and setgid bits of a file's access mode.  @xref{Permission Bits}.
174 When the setuid bit of an executable file is on, executing that file
175 gives the process a third user ID: the @dfn{file user ID}.  This ID is
176 set to the owner ID of the file.  The system then changes the effective
177 user ID to the file user ID.  The real user ID remains as it was.
178 Likewise, if the setgid bit is on, the process is given a @dfn{file
179 group ID} equal to the group ID of the file, and its effective group ID
180 is changed to the file group ID.
182 If a process has a file ID (user or group), then it can at any time
183 change its effective ID to its real ID and back to its file ID.
184 Programs use this feature to relinquish their special privileges except
185 when they actually need them.  This makes it less likely that they can
186 be tricked into doing something inappropriate with their privileges.
188 @strong{Portability Note:} Older systems do not have file IDs.
189 To determine if a system has this feature, you can test the compiler
190 define @code{_POSIX_SAVED_IDS}.  (In the POSIX standard, file IDs are
191 known as saved IDs.)
193 @xref{File Attributes}, for a more general discussion of file modes and
194 accessibility.
196 @node Reading Persona
197 @section Reading the Persona of a Process
199 Here are detailed descriptions of the functions for reading the user and
200 group IDs of a process, both real and effective.  To use these
201 facilities, you must include the header files @file{sys/types.h} and
202 @file{unistd.h}.
203 @pindex unistd.h
204 @pindex sys/types.h
206 @comment sys/types.h
207 @comment POSIX.1
208 @deftp {Data Type} uid_t
209 This is an integer data type used to represent user IDs.  In the GNU
210 library, this is an alias for @code{unsigned int}.
211 @end deftp
213 @comment sys/types.h
214 @comment POSIX.1
215 @deftp {Data Type} gid_t
216 This is an integer data type used to represent group IDs.  In the GNU
217 library, this is an alias for @code{unsigned int}.
218 @end deftp
220 @comment unistd.h
221 @comment POSIX.1
222 @deftypefun uid_t getuid (void)
223 The @code{getuid} function returns the real user ID of the process.
224 @end deftypefun
226 @comment unistd.h
227 @comment POSIX.1
228 @deftypefun gid_t getgid (void)
229 The @code{getgid} function returns the real group ID of the process.
230 @end deftypefun
232 @comment unistd.h
233 @comment POSIX.1
234 @deftypefun uid_t geteuid (void)
235 The @code{geteuid} function returns the effective user ID of the process.
236 @end deftypefun
238 @comment unistd.h
239 @comment POSIX.1
240 @deftypefun gid_t getegid (void)
241 The @code{getegid} function returns the effective group ID of the process.
242 @end deftypefun
244 @comment unistd.h
245 @comment POSIX.1
246 @deftypefun int getgroups (int @var{count}, gid_t *@var{groups})
247 The @code{getgroups} function is used to inquire about the supplementary
248 group IDs of the process.  Up to @var{count} of these group IDs are
249 stored in the array @var{groups}; the return value from the function is
250 the number of group IDs actually stored.  If @var{count} is smaller than
251 the total number of supplementary group IDs, then @code{getgroups}
252 returns a value of @code{-1} and @code{errno} is set to @code{EINVAL}.
254 If @var{count} is zero, then @code{getgroups} just returns the total
255 number of supplementary group IDs.  On systems that do not support
256 supplementary groups, this will always be zero.
258 Here's how to use @code{getgroups} to read all the supplementary group
259 IDs:
261 @smallexample
262 @group
263 gid_t *
264 read_all_groups (void)
266   int ngroups = getgroups (0, NULL);
267   gid_t *groups
268     = (gid_t *) xmalloc (ngroups * sizeof (gid_t));
269   int val = getgroups (ngroups, groups);
270   if (val < 0)
271     @{
272       free (groups);
273       return NULL;
274     @}
275   return groups;
277 @end group
278 @end smallexample
279 @end deftypefun
281 @node Setting User ID
282 @section Setting the User ID
284 This section describes the functions for altering the user ID (real
285 and/or effective) of a process.  To use these facilities, you must
286 include the header files @file{sys/types.h} and @file{unistd.h}.
287 @pindex unistd.h
288 @pindex sys/types.h
290 @comment unistd.h
291 @comment POSIX.1
292 @deftypefun int seteuid (uid_t @var{neweuid})
293 This function sets the effective user ID of a process to @var{newuid},
294 provided that the process is allowed to change its effective user ID.  A
295 privileged process (effective user ID zero) can change its effective
296 user ID to any legal value.  An unprivileged process with a file user ID
297 can change its effective user ID to its real user ID or to its file user
298 ID.  Otherwise, a process may not change its effective user ID at all.
300 The @code{seteuid} function returns a value of @code{0} to indicate
301 successful completion, and a value of @code{-1} to indicate an error.
302 The following @code{errno} error conditions are defined for this
303 function:
305 @table @code
306 @item EINVAL
307 The value of the @var{newuid} argument is invalid.
309 @item EPERM
310 The process may not change to the specified ID.
311 @end table
313 Older systems (those without the @code{_POSIX_SAVED_IDS} feature) do not
314 have this function.
315 @end deftypefun
317 @comment unistd.h
318 @comment POSIX.1
319 @deftypefun int setuid (uid_t @var{newuid})
320 If the calling process is privileged, this function sets both the real
321 and effective user ID of the process to @var{newuid}.  It also deletes
322 the file user ID of the process, if any.  @var{newuid} may be any
323 legal value.  (Once this has been done, there is no way to recover the
324 old effective user ID.)
326 If the process is not privileged, and the system supports the
327 @code{_POSIX_SAVED_IDS} feature, then this function behaves like
328 @code{seteuid}.
330 The return values and error conditions are the same as for @code{seteuid}.
331 @end deftypefun
333 @comment unistd.h
334 @comment BSD
335 @deftypefun int setreuid (uid_t @var{ruid}, uid_t @var{euid})
336 This function sets the real user ID of the process to @var{ruid} and the
337 effective user ID to @var{euid}.  If @var{ruid} is @code{-1}, it means
338 not to change the real user ID; likewise if @var{euid} is @code{-1}, it
339 means not to change the effective user ID.
341 The @code{setreuid} function exists for compatibility with 4.3 BSD Unix,
342 which does not support file IDs.  You can use this function to swap the
343 effective and real user IDs of the process.  (Privileged processes are
344 not limited to this particular usage.)  If file IDs are supported, you
345 should use that feature instead of this function.  @xref{Enable/Disable
346 Setuid}.
348 The return value is @code{0} on success and @code{-1} on failure.
349 The following @code{errno} error conditions are defined for this
350 function:
352 @table @code
353 @item EPERM
354 The process does not have the appropriate privileges; you do not
355 have permission to change to the specified ID.
356 @end table
357 @end deftypefun
359 @node Setting Groups
360 @section Setting the Group IDs
362 This section describes the functions for altering the group IDs (real
363 and effective) of a process.  To use these facilities, you must include
364 the header files @file{sys/types.h} and @file{unistd.h}.
365 @pindex unistd.h
366 @pindex sys/types.h
368 @comment unistd.h
369 @comment POSIX.1
370 @deftypefun int setegid (gid_t @var{newgid})
371 This function sets the effective group ID of the process to
372 @var{newgid}, provided that the process is allowed to change its group
373 ID.  Just as with @code{seteuid}, if the process is privileged it may
374 change its effective group ID to any value; if it isn't, but it has a
375 file group ID, then it may change to its real group ID or file group ID;
376 otherwise it may not change its effective group ID.
378 Note that a process is only privileged if its effective @emph{user} ID
379 is zero.  The effective group ID only affects access permissions.
381 The return values and error conditions for @code{setegid} are the same
382 as those for @code{seteuid}.
384 This function is only present if @code{_POSIX_SAVED_IDS} is defined.
385 @end deftypefun
387 @comment unistd.h
388 @comment POSIX.1
389 @deftypefun int setgid (gid_t @var{newgid})
390 This function sets both the real and effective group ID of the process
391 to @var{newgid}, provided that the process is privileged.  It also
392 deletes the file group ID, if any.
394 If the process is not privileged, then @code{setgid} behaves like
395 @code{setegid}.
397 The return values and error conditions for @code{setgid} are the same
398 as those for @code{seteuid}.
399 @end deftypefun
401 @comment unistd.h
402 @comment BSD
403 @deftypefun int setregid (gid_t @var{rgid}, gid_t @var{egid})
404 This function sets the real group ID of the process to @var{rgid} and
405 the effective group ID to @var{egid}.  If @var{rgid} is @code{-1}, it
406 means not to change the real group ID; likewise if @var{egid} is
407 @code{-1}, it means not to change the effective group ID.
409 The @code{setregid} function is provided for compatibility with 4.3 BSD
410 Unix, which does not support file IDs.  You can use this function to
411 swap the effective and real group IDs of the process.  (Privileged
412 processes are not limited to this usage.)  If file IDs are supported,
413 you should use that feature instead of using this function.
414 @xref{Enable/Disable Setuid}.
416 The return values and error conditions for @code{setregid} are the same
417 as those for @code{setreuid}.
418 @end deftypefun
420 @code{setuid} and @code{setgid} behave differently depending on whether
421 the effective user ID at the time is zero.  If it is not zero, they
422 behave like @code{seteuid} and @code{setegid}.  If it is, they change
423 both effective and real IDs and delete the file ID.  To avoid confusion,
424 we recommend you always use @code{seteuid} and @code{setegid} except
425 when you know the effective user ID is zero and your intent is to change
426 the persona permanently.  This case is rare---most of the programs that
427 need it, such as @code{login} and @code{su}, have already been written.
429 Note that if your program is setuid to some user other than @code{root},
430 there is no way to drop privileges permanently.
432 The system also lets privileged processes change their supplementary
433 group IDs.  To use @code{setgroups} or @code{initgroups}, your programs
434 should include the header file @file{grp.h}.
435 @pindex grp.h
437 @comment grp.h
438 @comment BSD
439 @deftypefun int setgroups (size_t @var{count}, gid_t *@var{groups})
440 This function sets the process's supplementary group IDs.  It can only
441 be called from privileged processes.  The @var{count} argument specifies
442 the number of group IDs in the array @var{groups}.
444 This function returns @code{0} if successful and @code{-1} on error.
445 The following @code{errno} error conditions are defined for this
446 function:
448 @table @code
449 @item EPERM
450 The calling process is not privileged.
451 @end table
452 @end deftypefun
454 @comment grp.h
455 @comment BSD
456 @deftypefun int initgroups (const char *@var{user}, gid_t @var{gid})
457 The @code{initgroups} function sets the process's supplementary group
458 IDs to be the normal default for the user name @var{user}. If @var{gid}
459 is not -1, it includes that group also.
461 This function works by scanning the group database for all the groups
462 @var{user} belongs to.  It then calls @code{setgroups} with the list it
463 has constructed.
465 The return values and error conditions are the same as for
466 @code{setgroups}.
467 @end deftypefun
469 @node Enable/Disable Setuid
470 @section Enabling and Disabling Setuid Access
472 A typical setuid program does not need its special access all of the
473 time.  It's a good idea to turn off this access when it isn't needed,
474 so it can't possibly give unintended access.
476 If the system supports the @code{_POSIX_SAVED_IDS} feature, you can
477 accomplish this with @code{seteuid}.  When the game program starts, its
478 real user ID is @code{jdoe}, its effective user ID is @code{games}, and
479 its saved user ID is also @code{games}.  The program should record both
480 user ID values once at the beginning, like this:
482 @smallexample
483 user_user_id = getuid ();
484 game_user_id = geteuid ();
485 @end smallexample
487 Then it can turn off game file access with
489 @smallexample
490 seteuid (user_user_id);
491 @end smallexample
493 @noindent
494 and turn it on with
496 @smallexample
497 seteuid (game_user_id);
498 @end smallexample
500 @noindent
501 Throughout this process, the real user ID remains @code{jdoe} and the
502 file user ID remains @code{games}, so the program can always set its
503 effective user ID to either one.
505 On other systems that don't support file user IDs, you can
506 turn setuid access on and off by using @code{setreuid} to swap the real
507 and effective user IDs of the process, as follows:
509 @smallexample
510 setreuid (geteuid (), getuid ());
511 @end smallexample
513 @noindent
514 This special case is always allowed---it cannot fail.
516 Why does this have the effect of toggling the setuid access?  Suppose a
517 game program has just started, and its real user ID is @code{jdoe} while
518 its effective user ID is @code{games}.  In this state, the game can
519 write the scores file.  If it swaps the two uids, the real becomes
520 @code{games} and the effective becomes @code{jdoe}; now the program has
521 only @code{jdoe} access.  Another swap brings @code{games} back to
522 the effective user ID and restores access to the scores file.
524 In order to handle both kinds of systems, test for the saved user ID
525 feature with a preprocessor conditional, like this:
527 @smallexample
528 #ifdef _POSIX_SAVED_IDS
529   setuid (user_user_id);
530 #else
531   setreuid (geteuid (), getuid ());
532 #endif
533 @end smallexample
535 @node Setuid Program Example
536 @section Setuid Program Example
538 Here's an example showing how to set up a program that changes its
539 effective user ID.
541 This is part of a game program called @code{caber-toss} that manipulates
542 a file @file{scores} that should be writable only by the game program
543 itself.  The program assumes that its executable file will be installed
544 with the setuid bit set and owned by the same user as the @file{scores}
545 file.  Typically, a system administrator will set up an account like
546 @code{games} for this purpose.
548 The executable file is given mode @code{4755}, so that doing an
549 @samp{ls -l} on it produces output like:
551 @smallexample
552 -rwsr-xr-x   1 games    184422 Jul 30 15:17 caber-toss
553 @end smallexample
555 @noindent
556 The setuid bit shows up in the file modes as the @samp{s}.
558 The scores file is given mode @code{644}, and doing an @samp{ls -l} on
559 it shows:
561 @smallexample
562 -rw-r--r--  1 games           0 Jul 31 15:33 scores
563 @end smallexample
565 Here are the parts of the program that show how to set up the changed
566 user ID.  This program is conditionalized so that it makes use of the
567 file IDs feature if it is supported, and otherwise uses @code{setreuid}
568 to swap the effective and real user IDs.
570 @smallexample
571 #include <stdio.h>
572 #include <sys/types.h>
573 #include <unistd.h>
574 #include <stdlib.h>
577 /* @r{Remember the effective and real UIDs.} */
579 static uid_t euid, ruid;
582 /* @r{Restore the effective UID to its original value.} */
584 void
585 do_setuid (void)
587   int status;
589 #ifdef _POSIX_SAVED_IDS
590   status = seteuid (euid);
591 #else
592   status = setreuid (ruid, euid);
593 #endif
594   if (status < 0) @{
595     fprintf (stderr, "Couldn't set uid.\n");
596     exit (status);
597     @}
601 @group
602 /* @r{Set the effective UID to the real UID.} */
604 void
605 undo_setuid (void)
607   int status;
609 #ifdef _POSIX_SAVED_IDS
610   status = seteuid (ruid);
611 #else
612   status = setreuid (euid, ruid);
613 #endif
614   if (status < 0) @{
615     fprintf (stderr, "Couldn't set uid.\n");
616     exit (status);
617     @}
619 @end group
621 /* @r{Main program.} */
624 main (void)
626   /* @r{Remember the real and effective user IDs.}  */
627   ruid = getuid ();
628   euid = geteuid ();
629   undo_setuid ();
631   /* @r{Do the game and record the score.}  */
632   @dots{}
634 @end smallexample
636 Notice how the first thing the @code{main} function does is to set the
637 effective user ID back to the real user ID.  This is so that any other
638 file accesses that are performed while the user is playing the game use
639 the real user ID for determining permissions.  Only when the program
640 needs to open the scores file does it switch back to the file user ID,
641 like this:
643 @smallexample
644 /* @r{Record the score.} */
647 record_score (int score)
649   FILE *stream;
650   char *myname;
652   /* @r{Open the scores file.} */
653   do_setuid ();
654   stream = fopen (SCORES_FILE, "a");
655   undo_setuid ();
657 @group
658   /* @r{Write the score to the file.} */
659   if (stream)
660     @{
661       myname = cuserid (NULL);
662       if (score < 0)
663         fprintf (stream, "%10s: Couldn't lift the caber.\n", myname);
664       else
665         fprintf (stream, "%10s: %d feet.\n", myname, score);
666       fclose (stream);
667       return 0;
668     @}
669   else
670     return -1;
672 @end group
673 @end smallexample
675 @node Tips for Setuid
676 @section Tips for Writing Setuid Programs
678 It is easy for setuid programs to give the user access that isn't
679 intended---in fact, if you want to avoid this, you need to be careful.
680 Here are some guidelines for preventing unintended access and
681 minimizing its consequences when it does occur:
683 @itemize @bullet
684 @item
685 Don't have @code{setuid} programs with privileged user IDs such as
686 @code{root} unless it is absolutely necessary.  If the resource is
687 specific to your particular program, it's better to define a new,
688 nonprivileged user ID or group ID just to manage that resource.
689 It's better if you can write your program to use a special group than a
690 special user.
692 @item
693 Be cautious about using the @code{exec} functions in combination with
694 changing the effective user ID.  Don't let users of your program execute
695 arbitrary programs under a changed user ID.  Executing a shell is
696 especially bad news. Less obviously, the @code{execlp} and @code{execvp}
697 functions are a potential risk (since the program they execute depends
698 on the user's @code{PATH} environment variable).
700 If you must @code{exec} another program under a changed ID, specify an
701 absolute file name (@pxref{File Name Resolution}) for the executable,
702 and make sure that the protections on that executable and @emph{all}
703 containing directories are such that ordinary users cannot replace it
704 with some other program.
706 You should also check the arguments passed to the program to make sure
707 they do not have unexpected effects.  Likewise, you should examine the
708 environment variables.  Decide which arguments and variables are safe,
709 and reject all others.
711 You should never use @code{system} in a privileged program, because it
712 invokes a shell.
714 @item
715 Only use the user ID controlling the resource in the part of the program
716 that actually uses that resource.  When you're finished with it, restore
717 the effective user ID back to the actual user's user ID.
718 @xref{Enable/Disable Setuid}.
720 @item
721 If the @code{setuid} part of your program needs to access other files
722 besides the controlled resource, it should verify that the real user
723 would ordinarily have permission to access those files.  You can use the
724 @code{access} function (@pxref{Access Permission}) to check this; it
725 uses the real user and group IDs, rather than the effective IDs.
726 @end itemize
728 @node Who Logged In
729 @section Identifying Who Logged In
730 @cindex login name, determining
731 @cindex user ID, determining
733 You can use the functions listed in this section to determine the login
734 name of the user who is running a process, and the name of the user who
735 logged in the current session.  See also the function @code{getuid} and
736 friends (@pxref{Reading Persona}).  How this information is collected by
737 the system and how to control/add/remove information from the background
738 storage is described in @ref{User Accounting Database}.
740 The @code{getlogin} function is declared in @file{unistd.h}, while
741 @code{cuserid} and @code{L_cuserid} are declared in @file{stdio.h}.
742 @pindex stdio.h
743 @pindex unistd.h
745 @comment unistd.h
746 @comment POSIX.1
747 @deftypefun {char *} getlogin (void)
748 The @code{getlogin} function returns a pointer to a string containing the
749 name of the user logged in on the controlling terminal of the process,
750 or a null pointer if this information cannot be determined.  The string
751 is statically allocated and might be overwritten on subsequent calls to
752 this function or to @code{cuserid}.
753 @end deftypefun
755 @comment stdio.h
756 @comment POSIX.1
757 @deftypefun {char *} cuserid (char *@var{string})
758 The @code{cuserid} function returns a pointer to a string containing a
759 user name associated with the effective ID of the process.  If
760 @var{string} is not a null pointer, it should be an array that can hold
761 at least @code{L_cuserid} characters; the string is returned in this
762 array.  Otherwise, a pointer to a string in a static area is returned.
763 This string is statically allocated and might be overwritten on
764 subsequent calls to this function or to @code{getlogin}.
766 The use of this function is deprecated since it is marked to be
767 withdrawn in XPG4.2 and has already been removed from newer revisions of
768 POSIX.1.
769 @end deftypefun
771 @comment stdio.h
772 @comment POSIX.1
773 @deftypevr Macro int L_cuserid
774 An integer constant that indicates how long an array you might need to
775 store a user name.
776 @end deftypevr
778 These functions let your program identify positively the user who is
779 running or the user who logged in this session.  (These can differ when
780 setuid programs are involved; @xref{Process Persona}.)  The user cannot
781 do anything to fool these functions.
783 For most purposes, it is more useful to use the environment variable
784 @code{LOGNAME} to find out who the user is.  This is more flexible
785 precisely because the user can set @code{LOGNAME} arbitrarily.
786 @xref{Standard Environment}.
789 @node User Accounting Database
790 @section The User Accounting Database
791 @cindex user accounting database
793 Most Unix-like operating systems keep track of logged in users by
794 maintaining a user accounting database.  This user accounting database
795 stores for each terminal, who has logged on, at what time, the process
796 ID of the user's login shell, etc., etc., but also stores information
797 about the run level of the system, the time of the last system reboot,
798 and possibly more.
800 The user accounting database typically lives in @file{/etc/utmp},
801 @file{/var/adm/utmp} or @file{/var/run/utmp}.  However, these files
802 should @strong{never} be accessed directly.  For reading information
803 from and writing information to the user accounting database, the
804 functions described in this section should be used.
807 @menu
808 * Manipulating the Database::   Scanning and modifying the user
809                                  accounting database.
810 * XPG Functions::               A standardized way for doing the same thing.
811 * Logging In and Out::          Functions from BSD that modify the user
812                                  accounting database.
813 @end menu
815 @node Manipulating the Database
816 @subsection Manipulating the User Accounting Database
818 These functions and the corresponding data structures are declared in
819 the header file @file{utmp.h}.
820 @pindex utmp.h
822 @comment utmp.h
823 @comment SVID
824 @deftp {Data Type} {struct exit_status}
825 The @code{exit_status} data structure is used to hold information about
826 the exit status of processes marked as @code{DEAD_PROCESS} in the user
827 accounting database.
829 @table @code
830 @item short int e_termination
831 The exit status of the process.
833 @item short int e_exit
834 The exit status of the process.
835 @end table
836 @end deftp
838 @deftp {Data Type} {struct utmp}
839 The @code{utmp} data structure is used to hold information about entries
840 in the user accounting database.  On the GNU system it has the following
841 members:
843 @table @code
844 @item short int ut_type
845 Specifies the type of login; one of @code{EMPTY}, @code{RUN_LVL},
846 @code{BOOT_TIME}, @code{OLD_TIME}, @code{NEW_TIME}, @code{INIT_PROCESS},
847 @code{LOGIN_PROCESS}, @code{USER_PROCESS}, @code{DEAD_PROCESS} or
848 @code{ACCOUNTING}.
850 @item pid_t ut_pid
851 The process ID number of the login process.
853 @item char ut_line[]
854 The device name of the tty (without @file{/dev/}).
856 @item char ut_id[]
857 The inittab ID of the process.
859 @item char ut_user[]
860 The user's login name.
862 @item char ut_host[]
863 The name of the host from which the user logged in.
865 @item struct exit_status ut_exit
866 The exit status of a process marked as @code{DEAD_PROCESS}.
868 @item long ut_session
869 The Session ID, used for windowing.
871 @item struct timeval ut_tv
872 Time the entry was made.  For entries of type @code{OLD_TIME} this is
873 the time when the system clock changed, and for entries of type
874 @code{NEW_TIME} this is the time the system clock was set to.
876 @item int32_t ut_addr_v6[4]
877 The Internet address of a remote host.
878 @end table
879 @end deftp
881 The @code{ut_type}, @code{ut_pid}, @code{ut_id}, @code{ut_tv}, and
882 @code{ut_host} fields are not available on all systems.  Portable
883 applications therefore should be prepared for these situations.  To help
884 doing this the @file{utmp.h} header provides macros
885 @code{_HAVE_UT_TYPE}, @code{_HAVE_UT_PID}, @code{_HAVE_UT_ID},
886 @code{_HAVE_UT_TV}, and @code{_HAVE_UT_HOST} if the respective field is
887 available.  The programmer can handle the situations by using
888 @code{#ifdef} in the program code.
890 The following macros are defined for use as values for the
891 @code{ut_type} member of the @code{utmp} structure.  The values are
892 integer constants.
894 @table @code
895 @comment utmp.h
896 @comment SVID
897 @vindex EMPTY
898 @item EMPTY
899 This macro is used to indicate that the entry contains no valid user
900 accounting information.
902 @comment utmp.h
903 @comment SVID
904 @vindex RUN_LVL
905 @item RUN_LVL
906 This macro is used to identify the systems runlevel.
908 @comment utmp.h
909 @comment SVID
910 @vindex BOOT_TIME
911 @item BOOT_TIME
912 This macro is used to identify the time of system boot.
914 @comment utmp.h
915 @comment SVID
916 @vindex OLD_TIME
917 @item OLD_TIME
918 This macro is used to identify the time when the system clock changed.
920 @comment utmp.h
921 @comment SVID
922 @vindex NEW_TIME
923 @item NEW_TIME
924 This macro is used to identify the time after the system changed.
926 @comment utmp.h
927 @comment SVID
928 @vindex INIT_PROCESS
929 @item INIT_PROCESS
930 This macro is used to identify a process spawned by the init process.
932 @comment utmp.h
933 @comment SVID
934 @vindex LOGIN_PROCESS
935 @item LOGIN_PROCESS
936 This macro is used to identify the session leader of a logged in user.
938 @comment utmp.h
939 @comment SVID
940 @vindex USER_PROCESS
941 @item USER_PROCESS
942 This macro is used to identify a user process.
944 @comment utmp.h
945 @comment SVID
946 @vindex DEAD_PROCESS
947 @item DEAD_PROCESS
948 This macro is used to identify a terminated process.
950 @comment utmp.h
951 @comment SVID
952 @vindex ACCOUNTING
953 @item ACCOUNTING
955 @end table
957 The size of the @code{ut_line}, @code{ut_id}, @code{ut_user} and
958 @code{ut_host} arrays can be found using the @code{sizeof} operator.
960 Many older systems have, instead of an @code{ut_tv} member, an
961 @code{ut_time} member, usually of type @code{time_t}, for representing
962 the time associated with the entry.  Therefore, for backwards
963 compatibility only, @file{utmp.h} defines @code{ut_time} as an alias for
964 @code{ut_tv.tv_sec}.
966 @comment utmp.h
967 @comment SVID
968 @deftypefun void setutent (void)
969 This function opens the user accounting database to begin scanning it.
970 You can then call @code{getutent}, @code{getutid} or @code{getutline} to
971 read entries and @code{pututline} to write entries.
973 If the database is already open, it resets the input to the beginning of
974 the database.
975 @end deftypefun
977 @comment utmp.h
978 @comment SVID
979 @deftypefun {struct utmp *} getutent (void)
980 The @code{getutent} function reads the next entry from the user
981 accounting database.  It returns a pointer to the entry, which is
982 statically allocated and may be overwritten by subsequent calls to
983 @code{getutent}.  You must copy the contents of the structure if you
984 wish to save the information or you can use the @code{getutent_r}
985 function which stores the data in a user-provided buffer.
987 A null pointer is returned in case no further entry is available.
988 @end deftypefun
990 @comment utmp.h
991 @comment SVID
992 @deftypefun void endutent (void)
993 This function closes the user accounting database.
994 @end deftypefun
996 @comment utmp.h
997 @comment SVID
998 @deftypefun {struct utmp *} getutid (const struct utmp *@var{id})
999 This function searches forward from the current point in the database
1000 for an entry that matches @var{id}.  If the @code{ut_type} member of the
1001 @var{id} structure is one of @code{RUN_LVL}, @code{BOOT_TIME},
1002 @code{OLD_TIME} or @code{NEW_TIME} the entries match if the
1003 @code{ut_type} members are identical.  If the @code{ut_type} member of
1004 the @var{id} structure is @code{INIT_PROCESS}, @code{LOGIN_PROCESS},
1005 @code{USER_PROCESS} or @code{DEAD_PROCESS}, the entries match if the
1006 @code{ut_type} member of the entry read from the database is one of
1007 these four, and the @code{ut_id} members match.  However if the
1008 @code{ut_id} member of either the @var{id} structure or the entry read
1009 from the database is empty it checks if the @code{ut_line} members match
1010 instead.  If a matching entry is found, @code{getutid} returns a pointer
1011 to the entry, which is statically allocated, and may be overwritten by a
1012 subsequent call to @code{getutent}, @code{getutid} or @code{getutline}.
1013 You must copy the contents of the structure if you wish to save the
1014 information.
1016 A null pointer is returned in case the end of the database is reached
1017 without a match.
1019 The @code{getutid} function may cache the last read entry.  Therefore,
1020 if you are using @code{getutid} to search for multiple occurrences, it
1021 is necessary to zero out the static data after each call.  Otherwise
1022 @code{getutid} could just return a pointer to the same entry over and
1023 over again.
1024 @end deftypefun
1026 @comment utmp.h
1027 @comment SVID
1028 @deftypefun {struct utmp *} getutline (const struct utmp *@var{line})
1029 This function searches forward from the current point in the database
1030 until it finds an entry whose @code{ut_type} value is
1031 @code{LOGIN_PROCESS} or @code{USER_PROCESS}, and whose @code{ut_line}
1032 member matches the @code{ut_line} member of the @var{line} structure.
1033 If it finds such an entry, it returns a pointer to the entry which is
1034 statically allocated, and may be overwritten by a subsequent call to
1035 @code{getutent}, @code{getutid} or @code{getutline}.  You must copy the
1036 contents of the structure if you wish to save the information.
1038 A null pointer is returned in case the end of the database is reached
1039 without a match.
1041 The @code{getutline} function may cache the last read entry.  Therefore
1042 if you are using @code{getutline} to search for multiple occurrences, it
1043 is necessary to zero out the static data after each call.  Otherwise
1044 @code{getutline} could just return a pointer to the same entry over and
1045 over again.
1046 @end deftypefun
1048 @comment utmp.h
1049 @comment SVID
1050 @deftypefun {struct utmp *} pututline (const struct utmp *@var{utmp})
1051 The @code{pututline} function inserts the entry @code{*@var{utmp}} at
1052 the appropriate place in the user accounting database.  If it finds that
1053 it is not already at the correct place in the database, it uses
1054 @code{getutid} to search for the position to insert the entry, however
1055 this will not modify the static structure returned by @code{getutent},
1056 @code{getutid} and @code{getutline}.  If this search fails, the entry
1057 is appended to the database.
1059 The @code{pututline} function returns a pointer to a copy of the entry
1060 inserted in the user accounting database, or a null pointer if the entry
1061 could not be added.  The following @code{errno} error conditions are
1062 defined for this function:
1064 @table @code
1065 @item EPERM
1066 The process does not have the appropriate privileges; you cannot modify
1067 the user accounting database.
1068 @end table
1069 @end deftypefun
1071 All the @code{get*} functions mentioned before store the information
1072 they return in a static buffer.  This can be a problem in multi-threaded
1073 programs since the data return for the request is overwritten be the
1074 return value data in another thread.  Therefore the GNU C Library
1075 provides as extensions three more functions which return the data in a
1076 user-provided buffer.
1078 @comment utmp.h
1079 @comment GNU
1080 @deftypefun int getutent_r (struct utmp *@var{buffer}, struct utmp **@var{result})
1081 The @code{getutent_r} is equivalent to the @code{getutent} function.  It
1082 returns the next entry from the database.  But instead of storing the
1083 information in a static buffer it stores it in the buffer pointed to by
1084 the parameter @var{buffer}.
1086 If the call was successful, the function returns @code{0} and the
1087 pointer variable pointed to by the parameter @var{result} contains a
1088 pointer to the buffer which contains the result (this is most probably
1089 the same value as @var{buffer}).  If something went wrong during the
1090 execution of @code{getutent_r} the function returns @code{-1}.
1092 This function is a GNU extension.
1093 @end deftypefun
1095 @comment utmp.h
1096 @comment GNU
1097 @deftypefun int getutid_r (const struct utmp *@var{id}, struct utmp *@var{buffer}, struct utmp **@var{result})
1098 This function retrieves just like @code{getutid} the next entry matching
1099 the information stored in @var{id}.  But the result is stored in the
1100 buffer pointed to by the parameter @var{buffer}.
1102 If successful the function returns @code{0} and the pointer variable
1103 pointed to by the parameter @var{result} contains a pointer to the
1104 buffer with the result (probably the same as @var{result}.  If not
1105 successful the function return @code{-1}.
1107 This function is a GNU extension.
1108 @end deftypefun
1110 @comment utmp.h
1111 @comment GNU
1112 @deftypefun int getutline_r (const struct utmp *@var{line}, struct utmp *@var{buffer}, struct utmp **@var{result})
1113 This function retrieves just like @code{getutline} the next entry
1114 matching the information stored in @var{line}.  But the result is stored
1115 in the buffer pointed to by the parameter @var{buffer}.
1117 If successful the function returns @code{0} and the pointer variable
1118 pointed to by the parameter @var{result} contains a pointer to the
1119 buffer with the result (probably the same as @var{result}.  If not
1120 successful the function return @code{-1}.
1122 This function is a GNU extension.
1123 @end deftypefun
1126 In addition to the user accounting database, most systems keep a number
1127 of similar databases.  For example most systems keep a log file with all
1128 previous logins (usually in @file{/etc/wtmp} or @file{/var/log/wtmp}).
1130 For specifying which database to examine, the following function should
1131 be used.
1133 @comment utmp.h
1134 @comment SVID
1135 @deftypefun int utmpname (const char *@var{file})
1136 The @code{utmpname} function changes the name of the database to be
1137 examined to @var{file}, and closes any previously opened database.  By
1138 default @code{getutent}, @code{getutid}, @code{getutline} and
1139 @code{pututline} read from and write to the user accounting database.
1141 The following macros are defined for use as the @var{file} argument:
1143 @deftypevr Macro {char *} _PATH_UTMP
1144 This macro is used to specify the user accounting database.
1145 @end deftypevr
1147 @deftypevr Macro {char *} _PATH_WTMP
1148 This macro is used to specify the user accounting log file.
1149 @end deftypevr
1151 The @code{utmpname} function returns a value of @code{0} if the new name
1152 was successfully stored, and a value of @code{-1} to indicate an error.
1153 Note that @code{utmpname} does not try to open the database, and that
1154 therefore the return value does not say anything about whether the
1155 database can be successfully opened.
1156 @end deftypefun
1158 Specially for maintaining log-like databases the GNU C Library provides
1159 the following function:
1161 @comment utmp.h
1162 @comment SVID
1163 @deftypefun void updwtmp (const char *@var{wtmp_file}, const struct utmp *@var{utmp})
1164 The @code{updwtmp} function appends the entry *@var{utmp} to the
1165 database specified by @var{wtmp_file}.  For possible values for the
1166 @var{wtmp_file} argument see the @code{utmpname} function.
1167 @end deftypefun
1169 @strong{Portability Note:} Although many operating systems provide a
1170 subset of these functions, they are not standardized.  There are often
1171 subtle differences in the return types, and there are considerable
1172 differences between the various definitions of @code{struct utmp}.  When
1173 programming for the GNU system, it is probably best to stick
1174 with the functions described in this section.  If however, you want your
1175 program to be portable, consider using the XPG functions described in
1176 @ref{XPG Functions}, or take a look at the BSD compatible functions in
1177 @ref{Logging In and Out}.
1180 @node XPG Functions
1181 @subsection XPG User Accounting Database Functions
1183 These functions, described in the X/Open Portability Guide, are declared
1184 in the header file @file{utmpx.h}.
1185 @pindex utmpx.h
1187 @deftp {Data Type} {struct utmpx}
1188 The @code{utmpx} data structure contains at least the following members:
1190 @table @code
1191 @item short int ut_type
1192 Specifies the type of login; one of @code{EMPTY}, @code{RUN_LVL},
1193 @code{BOOT_TIME}, @code{OLD_TIME}, @code{NEW_TIME}, @code{INIT_PROCESS},
1194 @code{LOGIN_PROCESS}, @code{USER_PROCESS} or @code{DEAD_PROCESS}.
1196 @item pid_t ut_pid
1197 The process ID number of the login process.
1199 @item char ut_line[]
1200 The device name of the tty (without @file{/dev/}).
1202 @item char ut_id[]
1203 The inittab ID of the process.
1205 @item char ut_user[]
1206 The user's login name.
1208 @item struct timeval ut_tv
1209 Time the entry was made.  For entries of type @code{OLD_TIME} this is
1210 the time when the system clock changed, and for entries of type
1211 @code{NEW_TIME} this is the time the system clock was set to.
1212 @end table
1213 On the GNU system, @code{struct utmpx} is identical to @code{struct
1214 utmp} except for the fact that including @file{utmpx.h} does not make
1215 visible the declaration of @code{struct exit_status}.
1216 @end deftp
1218 The following macros are defined for use as values for the
1219 @code{ut_type} member of the @code{utmpx} structure.  The values are
1220 integer constants and are, on the GNU system, identical to the
1221 definitions in @file{utmp.h}.
1223 @table @code
1224 @comment utmpx.h
1225 @comment XPG4.2
1226 @vindex EMPTY
1227 @item EMPTY
1228 This macro is used to indicate that the entry contains no valid user
1229 accounting information.
1231 @comment utmpx.h
1232 @comment XPG4.2
1233 @vindex RUN_LVL
1234 @item RUN_LVL
1235 This macro is used to identify the systems runlevel.
1237 @comment utmpx.h
1238 @comment XPG4.2
1239 @vindex BOOT_TIME
1240 @item BOOT_TIME
1241 This macro is used to identify the time of system boot.
1243 @comment utmpx.h
1244 @comment XPG4.2
1245 @vindex OLD_TIME
1246 @item OLD_TIME
1247 This macro is used to identify the time when the system clock changed.
1249 @comment utmpx.h
1250 @comment XPG4.2
1251 @vindex NEW_TIME
1252 @item NEW_TIME
1253 This macro is used to identify the time after the system changed.
1255 @comment utmpx.h
1256 @comment XPG4.2
1257 @vindex INIT_PROCESS
1258 @item INIT_PROCESS
1259 This macro is used to identify a process spawned by the init process.
1261 @comment utmpx.h
1262 @comment XPG4.2
1263 @vindex LOGIN_PROCESS
1264 @item LOGIN_PROCESS
1265 This macro is used to identify the session leader of a logged in user.
1267 @comment utmpx.h
1268 @comment XPG4.2
1269 @vindex USER_PROCESS
1270 @item USER_PROCESS
1271 This macro is used to identify a user process.
1273 @comment utmpx.h
1274 @comment XPG4.2
1275 @vindex DEAD_PROCESS
1276 @item DEAD_PROCESS
1277 This macro is used to identify a terminated process.
1278 @end table
1280 The size of the @code{ut_line}, @code{ut_id} and @code{ut_user} arrays
1281 can be found using the @code{sizeof} operator.
1283 @comment utmpx.h
1284 @comment XPG4.2
1285 @deftypefun void setutxent (void)
1286 This function is similar to @code{setutent}.  On the GNU system it is
1287 simply an alias for @code{setutent}.
1288 @end deftypefun
1290 @comment utmpx.h
1291 @comment XPG4.2
1292 @deftypefun {struct utmpx *} getutxent (void)
1293 The @code{getutxent} function is similar to @code{getutent}, but returns
1294 a pointer to a @code{struct utmpx} instead of @code{struct utmp}.  On
1295 the GNU system it simply is an alias for @code{getutent}.
1296 @end deftypefun
1298 @comment utmpx.h
1299 @comment XPG4.2
1300 @deftypefun void endutxent (void)
1301 This function is similar to @code{endutent}.  On the GNU system it is
1302 simply an alias for @code{endutent}.
1303 @end deftypefun
1305 @comment utmpx.h
1306 @comment XPG4.2
1307 @deftypefun {struct utmpx *} getutxid (const struct utmpx *@var{id})
1308 This function is similar to @code{getutid}, but uses @code{struct utmpx}
1309 instead of @code{struct utmp}.  On the GNU system it is simply an alias
1310 for @code{getutid}.
1311 @end deftypefun
1313 @comment utmpx.h
1314 @comment XPG4.2
1315 @deftypefun {struct utmpx *} getutxline (const struct utmpx *@var{line})
1316 This function is similar to @code{getutid}, but uses @code{struct utmpx}
1317 instead of @code{struct utmp}.  On the GNU system it is simply an alias
1318 for @code{getutline}.
1319 @end deftypefun
1321 @comment utmpx.h
1322 @comment XPG4.2
1323 @deftypefun {struct utmpx *} pututxline (const struct utmpx *@var{utmp})
1324 The @code{pututxline} function provides functionality identical to
1325 @code{pututline}, but uses @code{struct utmpx} instead of @code{struct
1326 utmp}.  On the GNU system @code{pututxline} is simply an alias for
1327 @code{pututline}.
1328 @end deftypefun
1331 @node Logging In and Out
1332 @subsection Logging In and Out
1334 These functions, derived from BSD, are available in the separate
1335 @file{libutil} library, and declared in @file{utmp.h}.
1336 @pindex utmp.h
1338 Note that the @code{ut_user} member of @code{struct utmp} is called
1339 @code{ut_name} in BSD.  Therefore, @code{ut_name} is defined as an alias
1340 for @code{ut_user} in @file{utmp.h}.
1342 @comment utmp.h
1343 @comment BSD
1344 @deftypefun int login_tty (int @var{filedes})
1345 This function makes @var{filedes} the controlling terminal of the
1346 current process, redirects standard input, standard output and
1347 standard error output to this terminal, and closes @var{filedes}.
1349 This function returns @code{0} on successful completion, and @code{-1}
1350 on error.
1351 @end deftypefun
1353 @comment utmp.h
1354 @comment BSD
1355 @deftypefun void login (const struct utmp *@var{entry})
1356 The @code{login} functions inserts an entry into the user accounting
1357 database.  The @code{ut_line} member is set to the name of the terminal
1358 on standard input.  If standard input is not a terminal @code{login}
1359 uses standard output or standard error output to determine the name of
1360 the terminal.  If @code{struct utmp} has a @code{ut_type} member,
1361 @code{login} sets it to @code{USER_PROCESS}, and if there is an
1362 @code{ut_pid} member, it will be set to the process ID of the current
1363 process.  The remaining entries are copied from @var{entry}.
1365 A copy of the entry is written to the user accounting log file.
1366 @end deftypefun
1368 @comment utmp.h
1369 @comment BSD
1370 @deftypefun int logout (const char *@var{ut_line})
1371 This function modifies the user accounting database to indicate that the
1372 user on @var{ut_line} has logged out.
1374 The @code{logout} function returns @code{1} if the entry was successfully
1375 written to the database, or @code{0} on error.
1376 @end deftypefun
1378 @comment utmp.h
1379 @comment BSD
1380 @deftypefun void logwtmp (const char *@var{ut_line}, const char *@var{ut_name}, const char *@var{ut_host})
1381 The @code{logwtmp} function appends an entry to the user accounting log
1382 file, for the current time and the information provided in the
1383 @var{ut_line}, @var{ut_name} and @var{ut_host} arguments.
1384 @end deftypefun
1386 @strong{Portability Note:} The BSD @code{struct utmp} only has the
1387 @code{ut_line}, @code{ut_name}, @code{ut_host} and @code{ut_time}
1388 members.  Older systems do not even have the @code{ut_host} member.
1391 @node User Database
1392 @section User Database
1393 @cindex user database
1394 @cindex password database
1395 @pindex /etc/passwd
1397 This section describes how to search and scan the database of registered
1398 users.  The database itself is kept in the file @file{/etc/passwd} on
1399 most systems, but on some systems a special network server gives access
1400 to it.
1402 @menu
1403 * User Data Structure::         What each user record contains.
1404 * Lookup User::                 How to look for a particular user.
1405 * Scanning All Users::          Scanning the list of all users, one by one.
1406 * Writing a User Entry::        How a program can rewrite a user's record.
1407 @end menu
1409 @node User Data Structure
1410 @subsection The Data Structure that Describes a User
1412 The functions and data structures for accessing the system user database
1413 are declared in the header file @file{pwd.h}.
1414 @pindex pwd.h
1416 @comment pwd.h
1417 @comment POSIX.1
1418 @deftp {Data Type} {struct passwd}
1419 The @code{passwd} data structure is used to hold information about
1420 entries in the system user data base.  It has at least the following members:
1422 @table @code
1423 @item char *pw_name
1424 The user's login name.
1426 @item char *pw_passwd.
1427 The encrypted password string.
1429 @item uid_t pw_uid
1430 The user ID number.
1432 @item gid_t pw_gid
1433 The user's default group ID number.
1435 @item char *pw_gecos
1436 A string typically containing the user's real name, and possibly other
1437 information such as a phone number.
1439 @item char *pw_dir
1440 The user's home directory, or initial working directory.  This might be
1441 a null pointer, in which case the interpretation is system-dependent.
1443 @item char *pw_shell
1444 The user's default shell, or the initial program run when the user logs in.
1445 This might be a null pointer, indicating that the system default should
1446 be used.
1447 @end table
1448 @end deftp
1450 @node Lookup User
1451 @subsection Looking Up One User
1452 @cindex converting user ID to user name
1453 @cindex converting user name to user ID
1455 You can search the system user database for information about a
1456 specific user using @code{getpwuid} or @code{getpwnam}.  These
1457 functions are declared in @file{pwd.h}.
1459 @comment pwd.h
1460 @comment POSIX.1
1461 @deftypefun {struct passwd *} getpwuid (uid_t @var{uid})
1462 This function returns a pointer to a statically-allocated structure
1463 containing information about the user whose user ID is @var{uid}.  This
1464 structure may be overwritten on subsequent calls to @code{getpwuid}.
1466 A null pointer value indicates there is no user in the data base with
1467 user ID @var{uid}.
1468 @end deftypefun
1470 @comment pwd.h
1471 @comment POSIX.1c
1472 @deftypefun int getpwuid_r (uid_t @var{uid}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
1473 This function is similar to @code{getpwuid} in that it returns
1474 information about the user whose user ID is @var{uid}.  However, it
1475 fills the user supplied structure pointed to by @var{result_buf} with
1476 the information instead of using a static buffer.  The first
1477 @var{buflen} bytes of the additional buffer pointed to by @var{buffer}
1478 are used to contain additional information, normally strings which are
1479 pointed to by the elements of the result structure.
1481 If the return value is @code{0} the pointer returned in @var{result}
1482 points to the record which contains the wanted data (i.e., @var{result}
1483 contains the value @var{result_buf}).  If it is nonzero, there is no
1484 user in the data base with user ID @var{uid}, or the buffer @var{buffer}
1485 is too small to contain all the needed information.  In the latter case,
1486 @var{errno} is set to @code{ERANGE}.
1487 @end deftypefun
1490 @comment pwd.h
1491 @comment POSIX.1
1492 @deftypefun {struct passwd *} getpwnam (const char *@var{name})
1493 This function returns a pointer to a statically-allocated structure
1494 containing information about the user whose user name is @var{name}.
1495 This structure may be overwritten on subsequent calls to
1496 @code{getpwnam}.
1498 A null pointer return indicates there is no user named @var{name}.
1499 @end deftypefun
1501 @comment pwd.h
1502 @comment POSIX.1c
1503 @deftypefun int getpwnam_r (const char *@var{name}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
1504 This function is similar to @code{getpwnam} in that is returns
1505 information about the user whose user name is @var{name}.  However, like
1506 @code{getpwuid_r}, it fills the user supplied buffers in
1507 @var{result_buf} and @var{buffer} with the information instead of using
1508 a static buffer.
1510 The return values are the same as for @code{getpwuid_r}.
1511 @end deftypefun
1514 @node Scanning All Users
1515 @subsection Scanning the List of All Users
1516 @cindex scanning the user list
1518 This section explains how a program can read the list of all users in
1519 the system, one user at a time.  The functions described here are
1520 declared in @file{pwd.h}.
1522 You can use the @code{fgetpwent} function to read user entries from a
1523 particular file.
1525 @comment pwd.h
1526 @comment SVID
1527 @deftypefun {struct passwd *} fgetpwent (FILE *@var{stream})
1528 This function reads the next user entry from @var{stream} and returns a
1529 pointer to the entry.  The structure is statically allocated and is
1530 rewritten on subsequent calls to @code{fgetpwent}.  You must copy the
1531 contents of the structure if you wish to save the information.
1533 The stream must correspond to a file in the same format as the standard
1534 password database file.
1535 @end deftypefun
1537 @comment pwd.h
1538 @comment GNU
1539 @deftypefun int fgetpwent_r (FILE *@var{stream}, struct passwd *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct passwd **@var{result})
1540 This function is similar to @code{fgetpwent} in that it reads the next
1541 user entry from @var{stream}.  But the result is returned in the
1542 structure pointed to by @var{result_buf}.  The
1543 first @var{buflen} bytes of the additional buffer pointed to by
1544 @var{buffer} are used to contain additional information, normally
1545 strings which are pointed to by the elements of the result structure.
1547 The stream must correspond to a file in the same format as the standard
1548 password database file.
1550 If the function returns zero @var{result} points to the structure with
1551 the wanted data (normally this is in @var{result_buf}).  If errors
1552 occurred the return value is nonzero and @var{result} contains a null
1553 pointer.
1554 @end deftypefun
1556 The way to scan all the entries in the user database is with
1557 @code{setpwent}, @code{getpwent}, and @code{endpwent}.
1559 @comment pwd.h
1560 @comment SVID, BSD
1561 @deftypefun void setpwent (void)
1562 This function initializes a stream which @code{getpwent} and
1563 @code{getpwent_r} use to read the user database.
1564 @end deftypefun
1566 @comment pwd.h
1567 @comment POSIX.1
1568 @deftypefun {struct passwd *} getpwent (void)
1569 The @code{getpwent} function reads the next entry from the stream
1570 initialized by @code{setpwent}.  It returns a pointer to the entry.  The
1571 structure is statically allocated and is rewritten on subsequent calls
1572 to @code{getpwent}.  You must copy the contents of the structure if you
1573 wish to save the information.
1575 A null pointer is returned when no more entries are available.
1576 @end deftypefun
1578 @comment pwd.h
1579 @comment GNU
1580 @deftypefun int getpwent_r (struct passwd *@var{result_buf}, char *@var{buffer}, int @var{buflen}, struct passwd **@var{result})
1581 This function is similar to @code{getpwent} in that it returns the next
1582 entry from the stream initialized by @code{setpwent}.  Like
1583 @code{fgetpwent_r}, it uses the user-supplied buffers in
1584 @var{result_buf} and @var{buffer} to return the information requested.
1586 The return values are the same as for @code{fgetpwent_r}.
1588 @end deftypefun
1590 @comment pwd.h
1591 @comment SVID, BSD
1592 @deftypefun void endpwent (void)
1593 This function closes the internal stream used by @code{getpwent} or
1594 @code{getpwent_r}.
1595 @end deftypefun
1597 @node Writing a User Entry
1598 @subsection Writing a User Entry
1600 @comment pwd.h
1601 @comment SVID
1602 @deftypefun int putpwent (const struct passwd *@var{p}, FILE *@var{stream})
1603 This function writes the user entry @code{*@var{p}} to the stream
1604 @var{stream}, in the format used for the standard user database
1605 file.  The return value is zero on success and nonzero on failure.
1607 This function exists for compatibility with SVID.  We recommend that you
1608 avoid using it, because it makes sense only on the assumption that the
1609 @code{struct passwd} structure has no members except the standard ones;
1610 on a system which merges the traditional Unix data base with other
1611 extended information about users, adding an entry using this function
1612 would inevitably leave out much of the important information.
1613 @c Then how are programmers to modify the password file? -zw
1615 The function @code{putpwent} is declared in @file{pwd.h}.
1616 @end deftypefun
1618 @node Group Database
1619 @section Group Database
1620 @cindex group database
1621 @pindex /etc/group
1623 This section describes how to search and scan the database of
1624 registered groups.  The database itself is kept in the file
1625 @file{/etc/group} on most systems, but on some systems a special network
1626 service provides access to it.
1628 @menu
1629 * Group Data Structure::        What each group record contains.
1630 * Lookup Group::                How to look for a particular group.
1631 * Scanning All Groups::         Scanning the list of all groups.
1632 @end menu
1634 @node Group Data Structure
1635 @subsection The Data Structure for a Group
1637 The functions and data structures for accessing the system group
1638 database are declared in the header file @file{grp.h}.
1639 @pindex grp.h
1641 @comment grp.h
1642 @comment POSIX.1
1643 @deftp {Data Type} {struct group}
1644 The @code{group} structure is used to hold information about an entry in
1645 the system group database.  It has at least the following members:
1647 @table @code
1648 @item char *gr_name
1649 The name of the group.
1651 @item gid_t gr_gid
1652 The group ID of the group.
1654 @item char **gr_mem
1655 A vector of pointers to the names of users in the group.  Each user name
1656 is a null-terminated string, and the vector itself is terminated by a
1657 null pointer.
1658 @end table
1659 @end deftp
1661 @node Lookup Group
1662 @subsection Looking Up One Group
1663 @cindex converting group name to group ID
1664 @cindex converting group ID to group name
1666 You can search the group database for information about a specific
1667 group using @code{getgrgid} or @code{getgrnam}.  These functions are
1668 declared in @file{grp.h}.
1670 @comment grp.h
1671 @comment POSIX.1
1672 @deftypefun {struct group *} getgrgid (gid_t @var{gid})
1673 This function returns a pointer to a statically-allocated structure
1674 containing information about the group whose group ID is @var{gid}.
1675 This structure may be overwritten by subsequent calls to
1676 @code{getgrgid}.
1678 A null pointer indicates there is no group with ID @var{gid}.
1679 @end deftypefun
1681 @comment grp.h
1682 @comment POSIX.1c
1683 @deftypefun int getgrgid_r (gid_t @var{gid}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
1684 This function is similar to @code{getgrgid} in that it returns
1685 information about the group whose group ID is @var{gid}.  However, it
1686 fills the user supplied structure pointed to by @var{result_buf} with
1687 the information instead of using a static buffer.  The first
1688 @var{buflen} bytes of the additional buffer pointed to by @var{buffer}
1689 are used to contain additional information, normally strings which are
1690 pointed to by the elements of the result structure.
1692 If the return value is @code{0} the pointer returned in @var{result}
1693 points to the requested data (i.e., @var{result} contains the value
1694 @var{result_buf}).  If it is nonzero, there is no group in the data base
1695 with group ID @var{gid}, or the buffer @var{buffer} is too small to
1696 contain all the needed information.  In the latter case, @var{errno} is
1697 set to @code{ERANGE}.
1698 @end deftypefun
1700 @comment grp.h
1701 @comment SVID, BSD
1702 @deftypefun {struct group *} getgrnam (const char *@var{name})
1703 This function returns a pointer to a statically-allocated structure
1704 containing information about the group whose group name is @var{name}.
1705 This structure may be overwritten by subsequent calls to
1706 @code{getgrnam}.
1708 A null pointer indicates there is no group named @var{name}.
1709 @end deftypefun
1711 @comment grp.h
1712 @comment POSIX.1c
1713 @deftypefun int getgrnam_r (const char *@var{name}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
1714 This function is similar to @code{getgrnam} in that is returns
1715 information about the group whose group name is @var{name}.  Like
1716 @code{getgrgid_r}, it uses the user supplied buffers in
1717 @var{result_buf} and @var{buffer}, not a static buffer.
1719 The return values are the same as for @code{getgrgid_r}
1720 @code{ERANGE}.
1721 @end deftypefun
1723 @node Scanning All Groups
1724 @subsection Scanning the List of All Groups
1725 @cindex scanning the group list
1727 This section explains how a program can read the list of all groups in
1728 the system, one group at a time.  The functions described here are
1729 declared in @file{grp.h}.
1731 You can use the @code{fgetgrent} function to read group entries from a
1732 particular file.
1734 @comment grp.h
1735 @comment SVID
1736 @deftypefun {struct group *} fgetgrent (FILE *@var{stream})
1737 The @code{fgetgrent} function reads the next entry from @var{stream}.
1738 It returns a pointer to the entry.  The structure is statically
1739 allocated and is overwritten on subsequent calls to @code{fgetgrent}.  You
1740 must copy the contents of the structure if you wish to save the
1741 information.
1743 The stream must correspond to a file in the same format as the standard
1744 group database file.
1745 @end deftypefun
1747 @comment grp.h
1748 @comment GNU
1749 @deftypefun int fgetgrent_r (FILE *@var{stream}, struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
1750 This function is similar to @code{fgetgrent} in that it reads the next
1751 user entry from @var{stream}.  But the result is returned in the
1752 structure pointed to by @var{result_buf}.  The first @var{buflen} bytes
1753 of the additional buffer pointed to by @var{buffer} are used to contain
1754 additional information, normally strings which are pointed to by the
1755 elements of the result structure.
1757 This stream must correspond to a file in the same format as the standard
1758 group database file.
1760 If the function returns zero @var{result} points to the structure with
1761 the wanted data (normally this is in @var{result_buf}).  If errors
1762 occurred the return value is non-zero and @var{result} contains a null
1763 pointer.
1764 @end deftypefun
1766 The way to scan all the entries in the group database is with
1767 @code{setgrent}, @code{getgrent}, and @code{endgrent}.
1769 @comment grp.h
1770 @comment SVID, BSD
1771 @deftypefun void setgrent (void)
1772 This function initializes a stream for reading from the group data base.
1773 You use this stream by calling @code{getgrent} or @code{getgrent_r}.
1774 @end deftypefun
1776 @comment grp.h
1777 @comment SVID, BSD
1778 @deftypefun {struct group *} getgrent (void)
1779 The @code{getgrent} function reads the next entry from the stream
1780 initialized by @code{setgrent}.  It returns a pointer to the entry.  The
1781 structure is statically allocated and is overwritten on subsequent calls
1782 to @code{getgrent}.  You must copy the contents of the structure if you
1783 wish to save the information.
1784 @end deftypefun
1786 @comment grp.h
1787 @comment GNU
1788 @deftypefun int getgrent_r (struct group *@var{result_buf}, char *@var{buffer}, size_t @var{buflen}, struct group **@var{result})
1789 This function is similar to @code{getgrent} in that it returns the next
1790 entry from the stream initialized by @code{setgrent}.  Like
1791 @code{fgetgrent_r}, it places the result in user-supplied buffers
1792 pointed to @var{result_buf} and @var{buffer}.
1794 If the function returns zero @var{result} contains a pointer to the data
1795 (normally equal to @var{result_buf}).  If errors occurred the return
1796 value is non-zero and @var{result} contains a null pointer.
1797 @end deftypefun
1799 @comment grp.h
1800 @comment SVID, BSD
1801 @deftypefun void endgrent (void)
1802 This function closes the internal stream used by @code{getgrent} or
1803 @code{getgrent_r}.
1804 @end deftypefun
1806 @node Database Example
1807 @section User and Group Database Example
1809 Here is an example program showing the use of the system database inquiry
1810 functions.  The program prints some information about the user running
1811 the program.
1813 @smallexample
1814 @include db.c.texi
1815 @end smallexample
1817 Here is some output from this program:
1819 @smallexample
1820 I am Throckmorton Snurd.
1821 My login name is snurd.
1822 My uid is 31093.
1823 My home directory is /home/fsg/snurd.
1824 My default shell is /bin/sh.
1825 My default group is guest (12).
1826 The members of this group are:
1827   friedman
1828   tami
1829 @end smallexample
1831 @node Netgroup Database
1832 @section Netgroup Database
1834 @menu
1835 * Netgroup Data::                  Data in the Netgroup database and where
1836                                    it comes from.
1837 * Lookup Netgroup::                How to look for a particular netgroup.
1838 * Netgroup Membership::            How to test for netgroup membership.
1839 @end menu
1841 @node Netgroup Data
1842 @subsection Netgroup Data
1844 @cindex Netgroup
1845 Sometimes it is useful group users according to other criteria like the
1846 ones used in the @xref{Group Database}.  E.g., it is useful to associate
1847 a certain group of users with a certain machine.  On the other hand
1848 grouping of host names is not supported so far.
1850 In Sun Microsystems SunOS appeared a new kind of database, the netgroup
1851 database.  It allows to group hosts, users, and domain freely, giving
1852 them individual names.  More concrete: a netgroup is a list of triples
1853 consisting of a host name, a user name, and a domain name, where any of
1854 the entries can be a wildcard entry, matching all inputs.  A last
1855 possibility is that names of other netgroups can also be given in the
1856 list specifying a netgroup.  So one can construct arbitrary hierarchies
1857 without loops.
1859 Sun's implementation allows netgroups only for the @code{nis} or
1860 @code{nisplus} service @pxref{Services in the NSS configuration}.  The
1861 implementation in the GNU C library has no such restriction.  An entry
1862 in either of the input services must have the following form:
1864 @smallexample
1865 @var{groupname} ( @var{groupname} | @code{(}@var{hostname}@code{,}@var{username}@code{,}@code{domainname}@code{)} )+
1866 @end smallexample
1868 Any of the fields in the triple can be empty which means anything
1869 matches.  While describing the functions we will see that the opposite
1870 case is useful as well.  I.e., there may be entries which will not
1871 match any input.  For entries like a name consisting of the single
1872 character @code{-} shall be used.
1874 @node Lookup Netgroup
1875 @subsection Looking up one Netgroup
1877 The lookup functions for netgroups are a bit different to all other
1878 system database handling functions.  Since a single netgroup can contain
1879 many entries a two-step process is needed.  First a single netgroup is
1880 selected and then one can iterate over all entries in this netgroup.
1881 These functions are declared in @file{netdb.h}.
1883 @comment netdb.h
1884 @deftypefun int setnetgrent (const char *@var{netgroup})
1885 A call to this function initializes the internal state of the library to
1886 allow following calls of the @code{getnetgrent} iterate over all entries
1887 in the netgroup with name @var{netgroup}.
1889 When the call is successful (i.e., when a netgroup with this name exist)
1890 the return value is @code{1}.  When the return value is @code{0} no
1891 netgroup of this name is known or some other error occurred.
1892 @end deftypefun
1894 It is important to remember that there is only one single state for
1895 iterating the netgroups.  Even if the programmer uses the
1896 @code{getnetgrent_r} function the result is not really reentrant since
1897 always only one single netgroup at a time can be processed.  If the
1898 program needs to process more than one netgroup simultaneously she
1899 must protect this by using external locking.  This problem was
1900 introduced in the original netgroups implementation in SunOS and since
1901 we must stay compatible it is not possible to change this.
1903 Some other functions also use the netgroups state.  Currently these are
1904 the @code{innetgr} function and parts of the implementation of the
1905 @code{compat} service part of the NSS implementation.
1907 @comment netdb.h
1908 @deftypefun int getnetgrent (char **@var{hostp}, char **@var{userp}, char **@var{domainp})
1909 This function returns the next unprocessed entry of the currently
1910 selected netgroup.  The string pointers, which addresses are passed in
1911 the arguments @var{hostp}, @var{userp}, and @var{domainp}, will contain
1912 after a successful call pointers to appropriate strings.  If the string
1913 in the next entry is empty the pointer has the value @code{NULL}.
1914 The returned string pointers are only valid unless no of the netgroup
1915 related functions are called.
1917 The return value is @code{1} if the next entry was successfully read.  A
1918 value of @code{0} means no further entries exist or internal errors occurred.
1919 @end deftypefun
1921 @comment netdb.h
1922 @deftypefun int getnetgrent_r (char **@var{hostp}, char **@var{userp}, char **@var{domainp}, char *@var{buffer}, int @var{buflen})
1923 This function is similar to @code{getnetgrent} with only one exception:
1924 the strings the three string pointers @var{hostp}, @var{userp}, and
1925 @var{domainp} point to, are placed in the buffer of @var{buflen} bytes
1926 starting at @var{buffer}.  This means the returned values are valid
1927 even after other netgroup related functions are called.
1929 The return value is @code{1} if the next entry was successfully read and
1930 the buffer contains enough room to place the strings in it.  @code{0} is
1931 returned in case no more entries are found, the buffer is too small, or
1932 internal errors occurred.
1934 This function is a GNU extension.  The original implementation in the
1935 SunOS libc does not provide this function.
1936 @end deftypefun
1938 @comment netdb.h
1939 @deftypefun void endnetgrent (void)
1940 This function free all buffers which were allocated to process the last
1941 selected netgroup.  As a result all string pointers returned by calls
1942 to @code{getnetgrent} are invalid afterwards.
1943 @end deftypefun
1945 @node Netgroup Membership
1946 @subsection Testing for Netgroup Membership
1948 It is often not necessary to scan the whole netgroup since often the
1949 only interesting question is whether a given entry is part of the
1950 selected netgroup.
1952 @comment netdb.h
1953 @deftypefun int innetgr (const char *@var{netgroup}, const char *@var{host}, const char *@var{user}, const char *@var{domain})
1954 This function tests whether the triple specified by the parameters
1955 @var{hostp}, @var{userp}, and @var{domainp} is part of the netgroup
1956 @var{netgroup}.  Using this function has the advantage that
1958 @enumerate
1959 @item
1960 no other netgroup function can use the global netgroup state since
1961 internal locking is used and
1962 @item
1963 the function is implemented more efficiently than successive calls
1964 to the other @code{set}/@code{get}/@code{endnetgrent} functions.
1965 @end enumerate
1967 Any of the pointers @var{hostp}, @var{userp}, and @var{domainp} can be
1968 @code{NULL} which means any value is excepted in this position.  This is
1969 also true for the name @code{-} which should not match any other string
1970 otherwise.
1972 The return value is @code{1} if an entry matching the given triple is
1973 found in the netgroup.  The return value is @code{0} if the netgroup
1974 itself is not found, the netgroup does not contain the triple or
1975 internal errors occurred.
1976 @end deftypefun