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