xref: /openbsd-src/lib/libc/sys/setuid.2 (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1.\"	$OpenBSD: setuid.2,v 1.20 2008/08/04 07:08:23 jmc Exp $
2.\"	$NetBSD: setuid.2,v 1.3 1995/02/27 12:37:06 cgd Exp $
3.\"
4.\" Copyright (c) 1983, 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.\"     @(#)setuid.2	8.1 (Berkeley) 6/4/93
32.\"
33.Dd $Mdocdate: August 4 2008 $
34.Dt SETUID 2
35.Os
36.Sh NAME
37.Nm setuid ,
38.Nm seteuid ,
39.Nm setgid ,
40.Nm setegid
41.Nd set user and group ID
42.Sh SYNOPSIS
43.Fd #include <sys/types.h>
44.Fd #include <unistd.h>
45.Ft int
46.Fn setuid "uid_t uid"
47.Ft int
48.Fn seteuid "uid_t euid"
49.Ft int
50.Fn setgid "gid_t gid"
51.Ft int
52.Fn setegid "gid_t egid"
53.Sh DESCRIPTION
54The
55.Fn setuid
56function sets the real and effective user IDs and the saved set-user-ID
57of the current process to the specified value.
58The
59.Fn setuid
60function is permitted if the effective user ID is that of the superuser,
61or if the specified user ID is the same as the effective user ID.
62If not, but the specified user ID is the same as the real user ID,
63.Fn setuid
64will set the effective user ID to the real user ID.
65.Pp
66The
67.Fn setgid
68function sets the real and effective group IDs and the saved set-group-ID
69of the current process to the specified value.
70The
71.Fn setgid
72function is permitted if the effective user ID is that of the superuser,
73or if the specified group ID is the same as the effective group ID.
74If not, but the specified group ID is the same as the real group ID,
75.Fn setgid
76will set the effective group ID to the real group ID.
77Supplementary group IDs remain unchanged.
78.Pp
79The
80.Fn seteuid
81function
82.Pq Fn setegid
83sets the effective user ID (group ID) of the current process.
84The effective user ID may be set to the value
85of the real user ID or the saved set-user-ID (see
86.Xr intro 2
87and
88.Xr execve 2 ) ;
89in this way, the effective user ID of a set-user-ID executable
90may be toggled by switching to the real user ID, then re-enabled
91by reverting to the set-user-ID value.
92Similarly, the effective group ID may be set to the value
93of the real group ID or the saved set-group-ID.
94.Sh RETURN VALUES
95Upon success, these functions return 0;
96otherwise \-1 is returned.
97.Pp
98If the user is not the superuser, or the UID
99specified is not the real, effective, or saved ID,
100these functions return \-1.
101.Sh SEE ALSO
102.Xr getgid 2 ,
103.Xr getuid 2 ,
104.Xr issetugid 2 ,
105.Xr setgroups 2 ,
106.Xr setregid 2 ,
107.Xr setresgid 2 ,
108.Xr setresuid 2 ,
109.Xr setreuid 2
110.Sh STANDARDS
111The
112.Fn setuid
113and
114.Fn setgid
115functions are compliant with the
116.St -p1003.1-90
117specification with
118.Li _POSIX_SAVED_IDS
119defined, with the extensions allowed in section B.4.2.2.
120The
121.Fn seteuid
122and
123.Fn setegid
124functions are extensions based on the
125.Tn POSIX
126concept of
127.Li _POSIX_SAVED_IDS ,
128and have been proposed for a future revision of the standard.
129.Sh HISTORY
130A
131.Fn setuid
132function appeared in
133.At v2 .
134