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