r4231: commiting changes to 3.0.10
[Samba.git] / source / lib / privileges.c
blob2b8d7613c188ba2879a9cc5a05b4ddea2c7c7bbd
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
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.
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.
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.
22 #include "includes.h"
24 /* defines */
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)
29 /****************************************************************************
30 Check if a user is a mapped group.
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.
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 ****************************************************************************/
47 #if 0
48 NTSTATUS is_mapped_group(BOOL *mapped, const DOM_SID *sid)
50 NTSTATUS result;
51 gid_t id;
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;
61 return result;
63 #endif
65 /****************************************************************************
66 duplicate alloc luid_attr
67 ****************************************************************************/
68 NTSTATUS dupalloc_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la)
70 NTSTATUS ret;
72 /* don't crash if the source pointer is NULL (since we don't
73 do priviledges now anyways) */
75 if ( !old_la )
76 return NT_STATUS_OK;
78 *new_la = TALLOC_P(mem_ctx, LUID_ATTR);
79 ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
81 (*new_la)->luid.high = old_la->luid.high;
82 (*new_la)->luid.low = old_la->luid.low;
83 (*new_la)->attr = old_la->attr;
85 ret = NT_STATUS_OK;
87 done:
88 return ret;
91 /****************************************************************************
92 initialise a privilege list
93 ****************************************************************************/
94 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
96 NTSTATUS ret;
97 TALLOC_CTX *mem_ctx = talloc_init("privilege set");
98 ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
100 *priv_set = TALLOC_ZERO_P(mem_ctx, PRIVILEGE_SET);
101 ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
103 (*priv_set)->mem_ctx = mem_ctx;
105 ret = NT_STATUS_OK;
107 done:
108 return ret;
111 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
113 NTSTATUS ret;
115 *priv_set = TALLOC_ZERO_P(mem_ctx, PRIVILEGE_SET);
116 ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
118 (*priv_set)->mem_ctx = mem_ctx;
119 (*priv_set)->ext_ctx = True;
121 ret = NT_STATUS_OK;
123 done:
124 return ret;
127 void reset_privilege(PRIVILEGE_SET *priv_set)
129 priv_set->count = 0;
130 priv_set->control = 0;
131 priv_set->set = NULL;
134 void destroy_privilege(PRIVILEGE_SET **priv_set)
136 reset_privilege(*priv_set);
137 if (!((*priv_set)->ext_ctx))
138 /* mem_ctx is local, destroy it */
139 talloc_destroy((*priv_set)->mem_ctx);
140 *priv_set = NULL;
143 /****************************************************************************
144 add a privilege to a privilege array
145 ****************************************************************************/
146 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
148 NTSTATUS ret;
149 LUID_ATTR *new_set;
151 /* check if the privilege is not already in the list */
152 if (NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
153 return NT_STATUS_UNSUCCESSFUL;
155 /* we can allocate memory to add the new privilege */
157 new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
158 ALLOC_CHECK(new_set, ret, done, "add_privilege");
160 new_set[priv_set->count].luid.high = set.luid.high;
161 new_set[priv_set->count].luid.low = set.luid.low;
162 new_set[priv_set->count].attr = set.attr;
164 priv_set->count++;
165 priv_set->set = new_set;
167 ret = NT_STATUS_OK;
169 done:
170 return ret;
173 /****************************************************************************
174 add all the privileges to a privilege array
175 ****************************************************************************/
176 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
178 NTSTATUS result = NT_STATUS_OK;
179 LUID_ATTR set;
181 set.attr = 0;
182 set.luid.high = 0;
184 /* TODO: set a proper list of privileges */
185 set.luid.low = SE_PRIV_ADD_USERS;
186 result = add_privilege(priv_set, set);
187 NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
189 set.luid.low = SE_PRIV_ADD_MACHINES;
190 result = add_privilege(priv_set, set);
191 NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
193 set.luid.low = SE_PRIV_PRINT_OPERATOR;
194 result = add_privilege(priv_set, set);
195 NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
197 return result;
200 /****************************************************************************
201 check if the privilege list is empty
202 ****************************************************************************/
203 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
205 if (!priv_set)
206 return NT_STATUS_INVALID_PARAMETER;
208 if (priv_set->count == 0)
209 return NT_STATUS_OK;
211 return NT_STATUS_UNSUCCESSFUL;
214 /****************************************************************************
215 check if the privilege is in the privilege list
216 ****************************************************************************/
217 NTSTATUS check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
219 int i;
221 if (!priv_set)
222 return NT_STATUS_INVALID_PARAMETER;
224 /* if the list is empty, obviously we can't have it */
225 if (NT_STATUS_IS_OK(check_empty_privilege(priv_set)))
226 return NT_STATUS_UNSUCCESSFUL;
228 for (i = 0; i < priv_set->count; i++) {
229 LUID_ATTR *cur_set;
231 cur_set = &priv_set->set[i];
232 /* check only the low and high part. Checking the attr field has no meaning */
233 if ( (cur_set->luid.low == set.luid.low) &&
234 (cur_set->luid.high == set.luid.high) ) {
235 return NT_STATUS_OK;
239 return NT_STATUS_UNSUCCESSFUL;
242 /****************************************************************************
243 remove a privilege from a privilege array
244 ****************************************************************************/
245 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
247 NTSTATUS ret;
248 LUID_ATTR *new_set;
249 LUID_ATTR *old_set;
250 int i,j;
252 if (!priv_set)
253 return NT_STATUS_INVALID_PARAMETER;
255 /* check if the privilege is in the list */
256 if (!NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
257 return NT_STATUS_UNSUCCESSFUL;
259 /* special case if it's the only privilege in the list */
260 if (priv_set->count == 1) {
261 reset_privilege(priv_set);
262 return NT_STATUS_OK;
266 * the privilege is there, create a new list,
267 * and copy the other privileges
270 old_set = priv_set->set;
272 new_set = TALLOC_ARRAY(priv_set->mem_ctx, LUID_ATTR, priv_set->count - 1);
273 ALLOC_CHECK(new_set, ret, done, "remove_privilege");
275 for (i=0, j=0; i < priv_set->count; i++) {
276 if ( (old_set[i].luid.low == set.luid.low) &&
277 (old_set[i].luid.high == set.luid.high) ) {
278 continue;
281 new_set[j].luid.low = old_set[i].luid.low;
282 new_set[j].luid.high = old_set[i].luid.high;
283 new_set[j].attr = old_set[i].attr;
285 j++;
288 if (j != priv_set->count - 1) {
289 DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
290 DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
291 return NT_STATUS_INTERNAL_ERROR;
294 /* ok everything is fine */
296 priv_set->count--;
297 priv_set->set = new_set;
299 ret = NT_STATUS_OK;
301 done:
302 return ret;
305 /****************************************************************************
306 duplicates a privilege array
307 the new privilege set must be passed inited
308 (use init_privilege or init_priv_with_ctx)
309 ****************************************************************************/
310 NTSTATUS dup_priv_set(PRIVILEGE_SET *new_priv_set, PRIVILEGE_SET *priv_set)
312 NTSTATUS ret;
313 LUID_ATTR *new_set;
314 LUID_ATTR *old_set;
315 int i;
317 if (!new_priv_set || !priv_set)
318 return NT_STATUS_INVALID_PARAMETER;
320 /* special case if there are no privileges in the list */
321 if (priv_set->count == 0) {
322 return NT_STATUS_OK;
326 * create a new list,
327 * and copy the other privileges
330 old_set = priv_set->set;
332 new_set = TALLOC_ARRAY(new_priv_set->mem_ctx, LUID_ATTR, priv_set->count - 1);
333 ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
335 for (i=0; i < priv_set->count; i++) {
337 new_set[i].luid.low = old_set[i].luid.low;
338 new_set[i].luid.high = old_set[i].luid.high;
339 new_set[i].attr = old_set[i].attr;
342 new_priv_set->count = priv_set->count;
343 new_priv_set->control = priv_set->control;
344 new_priv_set->set = new_set;
346 ret = NT_STATUS_OK;
348 done:
349 return ret;