7ddfcd34e52c8a22472a8517ffb46208cd679107
[platform/upstream/glibc.git] / hurd / hurdauth.c
1 /* Copyright (C) 1991, 92, 93, 94, 95, 97 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
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
12    Library General Public License for more details.
13
14    You should have received a copy of the GNU Library General Public
15    License along with the GNU C Library; see the file COPYING.LIB.  If not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <hurd.h>
20 #include <hurd/msg_server.h>
21 #include <hurd/id.h>
22 #include <string.h>
23
24 int
25 _hurd_refport_secure_p (mach_port_t ref)
26 {
27   if (ref == __mach_task_self ())
28     return 1;
29   if (__USEPORT (AUTH, ref == port))
30     return 1;
31   return 0;
32 }
33
34 kern_return_t
35 _S_msg_add_auth (mach_port_t me,
36                  auth_t addauth)
37 {
38   error_t err;
39   auth_t newauth;
40
41   if (err = __USEPORT (AUTH,
42                        __auth_makeauth (port,
43                                         &addauth, 1, MACH_MSG_TYPE_MOVE_SEND,
44                                         NULL, 0,
45                                         NULL, 0,
46                                         NULL, 0,
47                                         NULL, 0,
48                                         &newauth)))
49     return err;
50
51   err = __setauth (newauth);
52   __mach_port_deallocate (__mach_task_self (), newauth);
53   if (err)
54     return errno;
55
56   return 0;
57 }
58
59 kern_return_t
60 _S_msg_del_auth (mach_port_t me,
61                  task_t task,
62                  intarray_t uids, mach_msg_type_number_t nuids,
63                  intarray_t gids, mach_msg_type_number_t ngids)
64 {
65   error_t err;
66   auth_t newauth;
67
68   if (!_hurd_refport_secure_p (task))
69     return EPERM;
70
71   HURD_CRITICAL_BEGIN;
72   __mutex_lock (&_hurd_id.lock);
73   err = _hurd_check_ids ();
74
75   if (!err)
76     {
77       size_t i, j;
78       size_t nu = _hurd_id.gen.nuids, ng = _hurd_id.gen.ngids;
79       uid_t newu[nu];
80       gid_t newg[ng];
81
82       memcpy (newu, _hurd_id.gen.uids, nu * sizeof (uid_t));
83       memcpy (newg, _hurd_id.gen.gids, ng * sizeof (gid_t));
84
85       for (j = 0; j < nuids; ++j)
86         {
87           const uid_t uid = uids[j];
88           for (i = 0; i < nu; ++i)
89             if (newu[i] == uid)
90               /* Move the last uid into this slot, and decrease the
91                  number of uids so the last slot is no longer used.  */
92               newu[i] = newu[--nu];
93         }
94       __vm_deallocate (__mach_task_self (),
95                        (vm_address_t) uids, nuids * sizeof (uid_t));
96
97       for (j = 0; j < ngids; ++j)
98         {
99           const gid_t gid = gids[j];
100           for (i = 0; i < nu; ++i)
101             if (newu[i] == gid)
102               /* Move the last gid into this slot, and decrease the
103                  number of gids so the last slot is no longer used.  */
104               newu[i] = newu[--nu];
105         }
106       __vm_deallocate (__mach_task_self (),
107                        (vm_address_t) gids, ngids * sizeof (gid_t));
108
109       err = __USEPORT (AUTH, __auth_makeauth
110                        (port,
111                         NULL, 0, MACH_MSG_TYPE_COPY_SEND,
112                         newu, nu,
113                         _hurd_id.aux.uids, _hurd_id.aux.nuids,
114                         newg, ng,
115                         _hurd_id.aux.uids, _hurd_id.aux.ngids,
116                         &newauth));
117     }
118   __mutex_unlock (&_hurd_id.lock);
119   HURD_CRITICAL_END;
120
121   if (err)
122     return err;
123
124   err = __setauth (newauth);
125   __mach_port_deallocate (__mach_task_self (), newauth);
126   if (err)
127     return errno;
128
129   return 0;
130 }