Delete unused label to fix compiler warning.
[ira/wip.git] / source / lib / privileges.c
1 /*
2    Unix SMB/CIFS implementation.
3    Privileges handling functions
4    Copyright (C) Jean François Micouleau        1998-2001
5    Copyright (C) Simo Sorce                     2002-2003
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 /* defines */
25
26 #define ALLOC_CHECK(ptr, err, label, str) do { if ((ptr) == NULL) { DEBUG(0, ("%s: out of memory!\n", str)); err = NT_STATUS_NO_MEMORY; goto label; } } while(0)
27 #define NTSTATUS_CHECK(err, label, str1, str2) do { if (!NT_STATUS_IS_OK(err)) { DEBUG(0, ("%s: %s failed!\n", str1, str2)); } } while(0)
28
29 /****************************************************************************
30  Check if a user is a mapped group.
31
32    This function will check if the group SID is mapped onto a
33    system managed gid or onto a winbind manged sid.
34    In the first case it will be threated like a mapped group
35    and the backend should take the member list with a getgrgid
36    and ignore any user that have been possibly set into the group
37    object.
38
39    In the second case, the group is a fully SAM managed group
40    served back to the system through winbind. In this case the
41    members of a Local group are "unrolled" to cope with the fact
42    that unix cannot contain groups inside groups.
43    The backend MUST never call any getgr* / getpw* function or
44    loops with winbind may happen. 
45  ****************************************************************************/
46
47 #if 0
48 NTSTATUS is_mapped_group(BOOL *mapped, const DOM_SID *sid)
49 {
50         NTSTATUS result;
51         gid_t id;
52
53         /* look if mapping exist, do not make idmap alloc an uid if SID is not found */
54         result = idmap_get_gid_from_sid(&id, sid, False);
55         if (NT_STATUS_IS_OK(result)) {
56                 *mapped = gid_is_in_winbind_range(id);
57         } else {
58                 *mapped = False;
59         }
60
61         return result;
62 }
63 #endif
64
65 /****************************************************************************
66  duplicate alloc luid_attr
67  ****************************************************************************/
68 NTSTATUS dupalloc_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la)
69 {
70         NTSTATUS ret;
71
72         *new_la = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR));
73         ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
74
75         (*new_la)->luid.high = old_la->luid.high;
76         (*new_la)->luid.low = old_la->luid.low;
77         (*new_la)->attr = old_la->attr;
78         
79         ret = NT_STATUS_OK;
80
81 done:
82         return ret;
83 }
84
85 /****************************************************************************
86  initialise a privilege list
87  ****************************************************************************/
88 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
89 {
90         NTSTATUS ret;
91         TALLOC_CTX *mem_ctx = talloc_init("privilege set");
92         ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
93
94         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
95         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
96
97         (*priv_set)->mem_ctx = mem_ctx;
98
99         ret = NT_STATUS_OK;
100
101 done:
102         return ret;
103 }
104
105 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
106 {
107         NTSTATUS ret;
108
109         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
110         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
111
112         (*priv_set)->mem_ctx = mem_ctx;
113         (*priv_set)->ext_ctx = True;
114
115         ret = NT_STATUS_OK;
116
117 done:
118         return ret;
119 }
120
121 void reset_privilege(PRIVILEGE_SET *priv_set)
122 {
123         priv_set->count = 0;
124         priv_set->control = 0;
125         priv_set->set = NULL;
126 }
127
128 void destroy_privilege(PRIVILEGE_SET **priv_set)
129 {
130         reset_privilege(*priv_set);
131         if (!((*priv_set)->ext_ctx))
132                 /* mem_ctx is local, destroy it */
133                 talloc_destroy((*priv_set)->mem_ctx);
134         *priv_set = NULL;
135 }
136
137 /****************************************************************************
138  add a privilege to a privilege array
139  ****************************************************************************/
140 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
141 {
142         NTSTATUS ret;
143         LUID_ATTR *new_set;
144
145         /* check if the privilege is not already in the list */
146         if (NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
147                 return NT_STATUS_UNSUCCESSFUL;
148
149         /* we can allocate memory to add the new privilege */
150
151         new_set = (LUID_ATTR *)talloc_realloc(priv_set->mem_ctx, priv_set->set, (priv_set->count + 1) * (sizeof(LUID_ATTR)));
152         ALLOC_CHECK(new_set, ret, done, "add_privilege");
153
154         new_set[priv_set->count].luid.high = set.luid.high;
155         new_set[priv_set->count].luid.low = set.luid.low;
156         new_set[priv_set->count].attr = set.attr;
157
158         priv_set->count++;
159         priv_set->set = new_set;
160
161         ret = NT_STATUS_OK;
162
163 done:
164         return ret;
165 }
166
167 /****************************************************************************
168  add all the privileges to a privilege array
169  ****************************************************************************/
170 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
171 {
172         NTSTATUS result = NT_STATUS_OK;
173         LUID_ATTR set;
174
175         set.attr = 0;
176         set.luid.high = 0;
177
178         /* TODO: set a proper list of privileges */
179         set.luid.low = SE_PRIV_ADD_USERS;
180         result = add_privilege(priv_set, set);
181         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
182
183         set.luid.low = SE_PRIV_ADD_MACHINES;
184         result = add_privilege(priv_set, set);
185         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
186
187         set.luid.low = SE_PRIV_PRINT_OPERATOR;
188         result = add_privilege(priv_set, set);
189         NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
190
191         return result;
192 }
193
194 /****************************************************************************
195  check if the privilege list is empty
196  ****************************************************************************/
197 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
198 {
199         if (!priv_set)
200                 return NT_STATUS_INVALID_PARAMETER;
201
202         if (priv_set->count == 0)
203                 return NT_STATUS_OK;
204
205         return NT_STATUS_UNSUCCESSFUL;
206 }
207
208 /****************************************************************************
209  check if the privilege is in the privilege list
210  ****************************************************************************/
211 NTSTATUS check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
212 {
213         int i;
214
215         if (!priv_set)
216                 return NT_STATUS_INVALID_PARAMETER;
217
218         /* if the list is empty, obviously we can't have it */
219         if (NT_STATUS_IS_OK(check_empty_privilege(priv_set)))
220                 return NT_STATUS_UNSUCCESSFUL;
221
222         for (i = 0; i < priv_set->count; i++) {
223                 LUID_ATTR *cur_set;
224
225                 cur_set = &priv_set->set[i];
226                 /* check only the low and high part. Checking the attr field has no meaning */
227                 if (    (cur_set->luid.low == set.luid.low) &&
228                         (cur_set->luid.high == set.luid.high)   ) {
229                         return NT_STATUS_OK;
230                 }
231         }
232
233         return NT_STATUS_UNSUCCESSFUL;
234 }
235
236 /****************************************************************************
237  remove a privilege from a privilege array
238  ****************************************************************************/
239 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
240 {
241         NTSTATUS ret;
242         LUID_ATTR *new_set;
243         LUID_ATTR *old_set;
244         int i,j;
245
246         if (!priv_set)
247                 return NT_STATUS_INVALID_PARAMETER;
248
249         /* check if the privilege is in the list */
250         if (!NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
251                 return NT_STATUS_UNSUCCESSFUL;
252
253         /* special case if it's the only privilege in the list */
254         if (priv_set->count == 1) {
255                 reset_privilege(priv_set);      
256                 return NT_STATUS_OK;
257         }
258
259         /* 
260          * the privilege is there, create a new list,
261          * and copy the other privileges
262          */
263
264         old_set = priv_set->set;
265
266         new_set = (LUID_ATTR *)talloc(priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
267         ALLOC_CHECK(new_set, ret, done, "remove_privilege");
268
269         for (i=0, j=0; i < priv_set->count; i++) {
270                 if (    (old_set[i].luid.low == set.luid.low) && 
271                         (old_set[i].luid.high == set.luid.high) ) {
272                         continue;
273                 }
274                 
275                 new_set[j].luid.low = old_set[i].luid.low;
276                 new_set[j].luid.high = old_set[i].luid.high;
277                 new_set[j].attr = old_set[i].attr;
278
279                 j++;
280         }
281         
282         if (j != priv_set->count - 1) {
283                 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
284                 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
285                 return NT_STATUS_INTERNAL_ERROR;
286         }
287                 
288         /* ok everything is fine */
289         
290         priv_set->count--;
291         priv_set->set = new_set;
292         
293         ret = NT_STATUS_OK;
294
295 done:
296         return ret;
297 }
298
299 /****************************************************************************
300  duplicates a privilege array
301  the new privilege set must be passed inited
302  (use init_privilege or init_priv_with_ctx)
303  ****************************************************************************/
304 NTSTATUS dup_priv_set(PRIVILEGE_SET *new_priv_set, PRIVILEGE_SET *priv_set)
305 {
306         NTSTATUS ret;
307         LUID_ATTR *new_set;
308         LUID_ATTR *old_set;
309         int i;
310
311         if (!new_priv_set || !priv_set)
312                 return NT_STATUS_INVALID_PARAMETER;
313
314         /* special case if there are no privileges in the list */
315         if (priv_set->count == 0) {
316                 return NT_STATUS_OK;
317         }
318
319         /* 
320          * create a new list,
321          * and copy the other privileges
322          */
323
324         old_set = priv_set->set;
325
326         new_set = (LUID_ATTR *)talloc(new_priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
327         ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
328
329         for (i=0; i < priv_set->count; i++) {
330                 
331                 new_set[i].luid.low = old_set[i].luid.low;
332                 new_set[i].luid.high = old_set[i].luid.high;
333                 new_set[i].attr = old_set[i].attr;
334         }
335                         
336         new_priv_set->count = priv_set->count;
337         new_priv_set->control = priv_set->control;
338         new_priv_set->set = new_set;
339         
340         ret = NT_STATUS_OK;
341
342 done:
343         return ret;
344 }