Merge of 64-bit printf warning fixes.
[Samba/gebeck_regimport.git] / source3 / lib / privileges.c
blob1ed583382de1565f816732c31dbe49c5861a5db1
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 *new_la = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR));
73 ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
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;
79 ret = NT_STATUS_OK;
81 done:
82 return ret;
85 /****************************************************************************
86 initialise a privilege list
87 ****************************************************************************/
88 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
90 NTSTATUS ret;
91 TALLOC_CTX *mem_ctx = talloc_init("privilege set");
92 ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
94 *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
95 ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
97 (*priv_set)->mem_ctx = mem_ctx;
99 ret = NT_STATUS_OK;
101 done:
102 return ret;
105 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
107 NTSTATUS ret;
109 *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
110 ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
112 (*priv_set)->mem_ctx = mem_ctx;
113 (*priv_set)->ext_ctx = True;
115 ret = NT_STATUS_OK;
117 done:
118 return ret;
121 void reset_privilege(PRIVILEGE_SET *priv_set)
123 priv_set->count = 0;
124 priv_set->control = 0;
125 priv_set->set = NULL;
128 void destroy_privilege(PRIVILEGE_SET **priv_set)
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;
137 /****************************************************************************
138 add a privilege to a privilege array
139 ****************************************************************************/
140 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
142 NTSTATUS ret;
143 LUID_ATTR *new_set;
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;
149 /* we can allocate memory to add the new privilege */
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");
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;
158 priv_set->count++;
159 priv_set->set = new_set;
161 ret = NT_STATUS_OK;
163 done:
164 return ret;
167 /****************************************************************************
168 add all the privileges to a privilege array
169 ****************************************************************************/
170 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
172 NTSTATUS result = NT_STATUS_OK;
173 LUID_ATTR set;
175 set.attr = 0;
176 set.luid.high = 0;
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");
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");
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");
191 return result;
194 /****************************************************************************
195 check if the privilege list is empty
196 ****************************************************************************/
197 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
199 if (!priv_set)
200 return NT_STATUS_INVALID_PARAMETER;
202 if (priv_set->count == 0)
203 return NT_STATUS_OK;
205 return NT_STATUS_UNSUCCESSFUL;
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)
213 int i;
215 if (!priv_set)
216 return NT_STATUS_INVALID_PARAMETER;
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;
222 for (i = 0; i < priv_set->count; i++) {
223 LUID_ATTR *cur_set;
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;
233 return NT_STATUS_UNSUCCESSFUL;
236 /****************************************************************************
237 remove a privilege from a privilege array
238 ****************************************************************************/
239 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
241 NTSTATUS ret;
242 LUID_ATTR *new_set;
243 LUID_ATTR *old_set;
244 int i,j;
246 if (!priv_set)
247 return NT_STATUS_INVALID_PARAMETER;
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;
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;
260 * the privilege is there, create a new list,
261 * and copy the other privileges
264 old_set = priv_set->set;
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");
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;
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;
279 j++;
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;
288 /* ok everything is fine */
290 priv_set->count--;
291 priv_set->set = new_set;
293 ret = NT_STATUS_OK;
295 done:
296 return ret;
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)
306 NTSTATUS ret;
307 LUID_ATTR *new_set;
308 LUID_ATTR *old_set;
309 int i;
311 if (!new_priv_set || !priv_set)
312 return NT_STATUS_INVALID_PARAMETER;
314 /* special case if there are no privileges in the list */
315 if (priv_set->count == 0) {
316 return NT_STATUS_OK;
320 * create a new list,
321 * and copy the other privileges
324 old_set = priv_set->set;
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");
329 for (i=0; i < priv_set->count; i++) {
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;
336 new_priv_set->count = priv_set->count;
337 new_priv_set->control = priv_set->control;
338 new_priv_set->set = new_set;
340 ret = NT_STATUS_OK;
342 done:
343 return ret;