]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/setresuid.c
aa9cc0fa72c61c3333f01d824fef7258bbaa9d5f
[thirdparty/glibc.git] / sysdeps / mach / hurd / setresuid.c
1 /* setresuid -- set real user ID, effective user ID, and saved-set user ID
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <errno.h>
20 #include <unistd.h>
21 #include <hurd.h>
22 #include <hurd/id.h>
23
24 /* Set the real user ID, effective user ID, and saved-set user ID,
25 of the calling process to RUID, EUID, and SUID, respectively. */
26 int
27 __setresuid (uid_t ruid, uid_t euid, uid_t suid)
28 {
29 auth_t newauth;
30 error_t err;
31
32 HURD_CRITICAL_BEGIN;
33 __mutex_lock (&_hurd_id.lock);
34 err = _hurd_check_ids ();
35
36 if (!err)
37 {
38 /* Make a new auth handle which has EUID as the first element in the
39 list of effective uids. */
40
41 uid_t *newgen, *newaux;
42 uid_t auxs[2] = { ruid, suid };
43 size_t ngen, naux;
44
45 newgen = _hurd_id.gen.uids;
46 ngen = _hurd_id.gen.nuids;
47 if (euid != -1)
48 {
49 if (_hurd_id.gen.nuids == 0)
50 {
51 /* No effective uids now. The new set will be just UID. */
52 newgen = &euid;
53 ngen = 1;
54 }
55 else
56 {
57 _hurd_id.gen.uids[0] = euid;
58 _hurd_id.valid = 0;
59 }
60 }
61
62 newaux = _hurd_id.aux.uids;
63 naux = _hurd_id.aux.nuids;
64 if (ruid != -1)
65 {
66 if (_hurd_id.aux.nuids == 0)
67 {
68 newaux = &ruid;
69 naux = 1;
70 }
71 else
72 {
73 _hurd_id.aux.uids[0] = ruid;
74 _hurd_id.valid = 0;
75 }
76 }
77
78 if (suid != -1)
79 {
80 if (ruid == -1)
81 {
82 if (_hurd_id.aux.nuids >= 1)
83 auxs[0] = _hurd_id.aux.uids[0];
84 else if (_hurd_id.gen.nuids >= 1)
85 auxs[0] = _hurd_id.gen.uids[0];
86 else
87 /* Not even an effective UID.
88 Fall back to the only UID we have. */
89 auxs[0] = suid;
90 }
91 if (_hurd_id.aux.nuids <= 1)
92 {
93 /* No saved uids now. The new set will be just UID. */
94 newaux = auxs;
95 naux = 2;
96 }
97 else
98 {
99 _hurd_id.aux.uids[1] = suid;
100 _hurd_id.valid = 0;
101 }
102 }
103
104 err = __USEPORT (AUTH, __auth_makeauth
105 (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0,
106 newgen, ngen, newaux, naux,
107 _hurd_id.gen.gids, _hurd_id.gen.ngids,
108 _hurd_id.aux.gids, _hurd_id.aux.ngids,
109 &newauth));
110 }
111
112 __mutex_unlock (&_hurd_id.lock);
113 HURD_CRITICAL_END;
114
115 if (err)
116 return __hurd_fail (err);
117
118 /* Install the new handle and reauthenticate everything. */
119 err = __setauth (newauth);
120 __mach_port_deallocate (__mach_task_self (), newauth);
121 return err;
122 }
123 libc_hidden_def (__setresuid)
124 weak_alias (__setresuid, setresuid)