xref: /dragonfly/lib/libc/sys/getlogin.2 (revision dc71b7ab)
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. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"	@(#)getlogin.2	8.1 (Berkeley) 6/9/93
29.\" $FreeBSD: src/lib/libc/sys/getlogin.2,v 1.14.2.6 2001/12/14 18:34:00 ru Exp $
30.\" $DragonFly: src/lib/libc/sys/getlogin.2,v 1.5 2007/07/30 22:11:33 swildner Exp $
31.\"
32.Dd June 9, 1993
33.Dt GETLOGIN 2
34.Os
35.Sh NAME
36.Nm getlogin ,
37.Nm getlogin_r ,
38.Nm setlogin
39.Nd get/set login name
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In unistd.h
44.Ft char *
45.Fn getlogin void
46.In sys/param.h
47.Ft int
48.Fn getlogin_r "char *name" "int len"
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 getlogin_r
67provides the same service as
68.Fn getlogin
69except the caller must provide the buffer
70.Fa name
71with length
72.Fa len
73bytes
74to hold the result.  The buffer should be at least
75.Dv MAXLOGNAME
76bytes in length.
77.Pp
78.Fn Setlogin
79sets the login name of the user associated with the current session to
80.Fa name .
81This call is restricted to the super-user, and
82is normally used only when a new session is being created on behalf
83of the named user
84(for example, at login time, or when a remote shell is invoked).
85.Pp
86.Em NOTE :
87There is only one login name per session.
88.Pp
89It is
90.Em CRITICALLY
91important to ensure that
92.Fn setlogin
93is only ever called after the process has taken adequate steps to ensure
94that it is detached from its parent's session.
95Making a
96.Fn setsid
97system call is the
98.Em ONLY
99way to do this.  The
100.Fn daemon
101library call calls
102.Fn setsid
103which is an ideal way of detaching from a controlling terminal and
104forking into the background.
105.Pp
106In particular, doing a
107.Fn ioctl ttyfd TIOCNOTTY ...\&
108or
109.Fn setpgrp ...\&
110is
111.Em NOT
112sufficient.
113.Pp
114Once a parent process does a
115.Fn setsid
116call, it is acceptable for some child of that process to then do a
117.Fn setlogin
118even though it is not the session leader, but beware that ALL processes
119in the session will change their login name at the same time, even the
120parent.
121.Pp
122This is not the same as the traditional
123.Ux
124behavior of inheriting privilege.
125.Pp
126Since the
127.Fn setlogin
128system call is restricted to the super-user, it is assumed that (like
129all other privileged programs) the programmer has taken adequate
130precautions to prevent security violations.
131.Sh RETURN VALUES
132If a call to
133.Fn getlogin
134succeeds, it returns a pointer to a null-terminated string in a static buffer,
135or
136.Dv NULL
137if the name has not been set.
138.Fn getlogin_r
139returns zero if successful, or the error number upon failure.
140.Pp
141.Rv -std setlogin
142.Sh ERRORS
143The following errors may be returned by these calls:
144.Bl -tag -width Er
145.It Bq Er EFAULT
146The
147.Fa name
148parameter gave an
149invalid address.
150.It Bq Er EINVAL
151The
152.Fa name
153parameter
154pointed to a string that was too long.
155Login names are limited to
156.Dv MAXLOGNAME
157(from
158.In sys/param.h )
159characters, currently 17 including null.
160.It Bq Er EPERM
161The caller tried to set the login name and was not the super-user.
162.It Bq Er ERANGE
163The size of the buffer is smaller than the result to be returned.
164.El
165.Sh SEE ALSO
166.Xr setsid 2 ,
167.Xr daemon 3
168.Sh STANDARDS
169.Fn getlogin
170and
171.Fn getlogin_r
172conform to
173.St -p1003.1-96 .
174.Sh HISTORY
175The
176.Fn getlogin
177function first appeared in
178.Bx 4.4 .
179The return value of
180.Fn getlogin_r
181was changed from earlier versions of
182.Fx
183to be conformant with
184.St -p1003.1-96 .
185.Sh BUGS
186In earlier versions of the system,
187.Fn getlogin
188failed unless the process was associated with a login terminal.
189The current implementation (using
190.Fn setlogin )
191allows getlogin to succeed even when the process has no controlling terminal.
192In earlier versions of the system, the value returned by
193.Fn getlogin
194could not be trusted without checking the user ID.
195Portable programs should probably still make this check.
196