]> git.ipfire.org Git - thirdparty/glibc.git/blame - sysdeps/mach/hurd/setgid.c
Replace FSF snail mail address with URLs.
[thirdparty/glibc.git] / sysdeps / mach / hurd / setgid.c
CommitLineData
ebbad4cc
UD
1/* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
28f540f4 3
ebbad4cc 4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
28f540f4 8
ebbad4cc
UD
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
28f540f4 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
28f540f4 17
28f540f4
RM
18#include <errno.h>
19#include <unistd.h>
20#include <sys/types.h>
21#include <hurd.h>
22#include <hurd/id.h>
23#include <string.h>
24
25/* Set the group ID of the calling process to UID.
26 If the calling process is the super-user, the real
27 and effective group IDs, and the saved set-group-ID to UID;
28 if not, the effective group ID is set to GID. */
29int
ebbad4cc
UD
30__setgid (gid)
31 gid_t gid;
28f540f4
RM
32{
33 auth_t newauth;
34 error_t err;
35
36 HURD_CRITICAL_BEGIN;
37 __mutex_lock (&_hurd_id.lock);
38 err = _hurd_check_ids ();
39
40 if (!err)
41 {
42 /* Make a new auth handle which has GID as the real gid,
43 and as the first element in the list of effective gids. */
44
97532098
RM
45 gid_t *newgen, *newaux, auxbuf[2];
46 size_t ngen, naux;
47
97532098
RM
48 if (_hurd_id.gen.ngids == 0)
49 {
50 /* No effective gids now. The new set will be just GID. */
51 newgen = &gid;
52 ngen = 1;
53 }
6df1b247 54 else
97532098 55 {
97532098
RM
56 _hurd_id.gen.gids[0] = gid;
57 _hurd_id.valid = 0;
58 newgen = _hurd_id.gen.gids;
59 ngen = _hurd_id.gen.ngids;
6df1b247
RM
60 }
61
62 newaux = _hurd_id.aux.gids;
63 naux = _hurd_id.aux.ngids;
64 if (_hurd_id.gen.nuids > 0 && _hurd_id.gen.uids[0] == 0)
65 {
66 /* We are root; set the real and saved IDs too. */
67 _hurd_id.valid = 0;
97532098
RM
68 if (_hurd_id.aux.ngids < 2)
69 {
70 newaux = auxbuf;
71 naux = 2;
72 }
2114e10a 73 newaux[0] = newaux[1] = gid;
97532098 74 }
28f540f4
RM
75
76 err = __USEPORT (AUTH, __auth_makeauth
995069ee 77 (port, NULL, MACH_MSG_TYPE_COPY_SEND, 0,
6df1b247
RM
78 _hurd_id.gen.uids, _hurd_id.gen.nuids,
79 _hurd_id.aux.uids, _hurd_id.aux.nuids,
97532098 80 newgen, ngen, newaux, naux,
28f540f4
RM
81 &newauth));
82 }
83 __mutex_unlock (&_hurd_id.lock);
84 HURD_CRITICAL_END;
85
86 if (err)
87 return __hurd_fail (err);
88
89 /* Install the new handle and reauthenticate everything. */
90 err = __setauth (newauth);
91 __mach_port_deallocate (__mach_task_self (), newauth);
92 return err;
93}
94
95weak_alias (__setgid, setgid)