]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/sys/getlogin.2
This commit was generated by cvs2svn to compensate for changes in r52874,
[FreeBSD/FreeBSD.git] / lib / libc / sys / getlogin.2
1 .\" Copyright (c) 1989, 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)getlogin.2  8.1 (Berkeley) 6/9/93
33 .\" $FreeBSD$
34 .\"
35 .Dd June 9, 1993
36 .Dt GETLOGIN 2
37 .Os BSD 4.4
38 .Sh NAME
39 .Nm getlogin ,
40 .Nm setlogin
41 .Nd get/set login name
42 .Sh SYNOPSIS
43 .Fd #include <unistd.h>
44 .Ft char *
45 .Fn getlogin void
46 .Ft int
47 .Fn setlogin "const char *name"
48 .Sh DESCRIPTION
49 The
50 .Fn getlogin
51 routine
52 returns the login name of the user associated with the current session,
53 as previously set by
54 .Fn setlogin .
55 The name is normally associated with a login shell
56 at the time a session is created,
57 and is inherited by all processes descended from the login shell.
58 (This is true even if some of those processes assume another user ID,
59 for example when
60 .Xr su 1
61 is used).
62 .Pp
63 .Fn Setlogin
64 sets the login name of the user associated with the current session to
65 .Fa name .
66 This call is restricted to the super-user, and
67 is normally used only when a new session is being created on behalf
68 of the named user
69 (for example, at login time, or when a remote shell is invoked).
70 .Pp
71 .Em NOTE:
72 There is only one `login name' per `session .
73 .Pp
74 It is
75 .Em CRITICALLY
76 important to ensure that
77 .Fn setlogin
78 is only ever called after the process has taken adequate steps to ensure
79 that it is detached from its parent's session.
80 Making a
81 .Fn setsid
82 system call is the
83 .Em ONLY
84 way to do this.  The
85 .Fn daemon
86 library call calls
87 .Fn setsid
88 which is an ideal way of detaching from a controlling terminal and
89 forking into the background.
90 .Pp
91 In particular, doing a
92 .Fn ioctl ttyfd TIOCNOTTY ...
93 or
94 .Fn setpgrp ...
95 is
96 .Em NOT
97 sufficient.
98 .Pp
99 Once a parent process does a
100 .Fn setsid
101 call, it is acceptable for some child of that process to then do a
102 .Fn setlogin
103 even though it is not the session leader, but beware that ALL processes
104 in the session will change their login name at the same time, even the
105 parent.
106 .Pp
107 This is not the same as the traditional UNIX behavior of inheriting privilege.
108 .Pp
109 Since the
110 .Fn setlogin
111 system call is restricted to the super-user, it is assumed that (like
112 all other privileged programs) the programmer has taken adequate
113 precautions to prevent security violations.
114 .Sh RETURN VALUES
115 If a call to
116 .Fn getlogin
117 succeeds, it returns a pointer to a null-terminated string in a static buffer.
118 If the name has not been set, it returns
119 .Dv NULL .
120 If a call to
121 .Fn setlogin
122 succeeds, a value of 0 is returned.  If
123 .Fn setlogin
124 fails, a value of -1 is returned and an error code is
125 placed in the global location
126 .Va errno .
127 .Sh ERRORS
128 The following errors may be returned by these calls:
129 .Bl -tag -width Er
130 .It Bq Er EFAULT
131 The
132 .Fa name
133 parameter gave an
134 invalid address.
135 .It Bq Er EINVAL
136 The
137 .Fa name
138 parameter
139 pointed to a string that was too long.
140 Login names are limited to
141 .Dv MAXLOGNAME
142 (from
143 .Ao Pa sys/param.h Ac )
144 characters, currently 17 including null.
145 .It Bq Er EPERM
146 The caller tried to set the login name and was not the super-user.
147 .El
148 .Sh SEE ALSO
149 .Xr setsid 2 ,
150 .Xr daemon 3
151 .Sh BUGS
152 In earlier versions of the system,
153 .Fn getlogin
154 failed unless the process was associated with a login terminal.
155 The current implementation (using
156 .Fn setlogin )
157 allows getlogin to succeed even when the process has no controlling terminal.
158 In earlier versions of the system, the value returned by
159 .Fn getlogin
160 could not be trusted without checking the user ID.
161 Portable programs should probably still make this check.
162 .Sh HISTORY
163 The
164 .Fn getlogin
165 function first appeared in
166 .Bx 4.4 .