r487: fixing some compile issues with the IBM AIX compiler reoported on the ml -...
[tprouty/samba.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         /* don't crash if the source pointer is NULL (since we don't
73            do priviledges now anyways) */
74
75         if ( !old_la )
76                 return NT_STATUS_OK;
77
78         *new_la = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR));
79         ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
80
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;
84         
85         ret = NT_STATUS_OK;
86
87 done:
88         return ret;
89 }
90
91 /****************************************************************************
92  initialise a privilege list
93  ****************************************************************************/
94 NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
95 {
96         NTSTATUS ret;
97         TALLOC_CTX *mem_ctx = talloc_init("privilege set");
98         ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
99
100         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
101         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
102
103         (*priv_set)->mem_ctx = mem_ctx;
104
105         ret = NT_STATUS_OK;
106
107 done:
108         return ret;
109 }
110
111 NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
112 {
113         NTSTATUS ret;
114
115         *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
116         ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
117
118         (*priv_set)->mem_ctx = mem_ctx;
119         (*priv_set)->ext_ctx = True;
120
121         ret = NT_STATUS_OK;
122
123 done:
124         return ret;
125 }
126
127 void reset_privilege(PRIVILEGE_SET *priv_set)
128 {
129         priv_set->count = 0;
130         priv_set->control = 0;
131         priv_set->set = NULL;
132 }
133
134 void destroy_privilege(PRIVILEGE_SET **priv_set)
135 {
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;
141 }
142
143 /****************************************************************************
144  add a privilege to a privilege array
145  ****************************************************************************/
146 NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
147 {
148         NTSTATUS ret;
149         LUID_ATTR *new_set;
150
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;
154
155         /* we can allocate memory to add the new privilege */
156
157         new_set = (LUID_ATTR *)talloc_realloc(priv_set->mem_ctx, priv_set->set, (priv_set->count + 1) * (sizeof(LUID_ATTR)));
158         ALLOC_CHECK(new_set, ret, done, "add_privilege");
159
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;
163
164         priv_set->count++;
165         priv_set->set = new_set;
166
167         ret = NT_STATUS_OK;
168
169 done:
170         return ret;
171 }
172
173 /****************************************************************************
174  add all the privileges to a privilege array
175  ****************************************************************************/
176 NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
177 {
178         NTSTATUS result = NT_STATUS_OK;
179         LUID_ATTR set;
180
181         set.attr = 0;
182         set.luid.high = 0;
183
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");
188
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");
192
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");
196
197         return result;
198 }
199
200 /****************************************************************************
201  check if the privilege list is empty
202  ****************************************************************************/
203 NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
204 {
205         if (!priv_set)
206                 return NT_STATUS_INVALID_PARAMETER;
207
208         if (priv_set->count == 0)
209                 return NT_STATUS_OK;
210
211         return NT_STATUS_UNSUCCESSFUL;
212 }
213
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)
218 {
219         int i;
220
221         if (!priv_set)
222                 return NT_STATUS_INVALID_PARAMETER;
223
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;
227
228         for (i = 0; i < priv_set->count; i++) {
229                 LUID_ATTR *cur_set;
230
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;
236                 }
237         }
238
239         return NT_STATUS_UNSUCCESSFUL;
240 }
241
242 /****************************************************************************
243  remove a privilege from a privilege array
244  ****************************************************************************/
245 NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
246 {
247         NTSTATUS ret;
248         LUID_ATTR *new_set;
249         LUID_ATTR *old_set;
250         int i,j;
251
252         if (!priv_set)
253                 return NT_STATUS_INVALID_PARAMETER;
254
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;
258
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;
263         }
264
265         /* 
266          * the privilege is there, create a new list,
267          * and copy the other privileges
268          */
269
270         old_set = priv_set->set;
271
272         new_set = (LUID_ATTR *)talloc(priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
273         ALLOC_CHECK(new_set, ret, done, "remove_privilege");
274
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;
279                 }
280                 
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;
284
285                 j++;
286         }
287         
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;
292         }
293                 
294         /* ok everything is fine */
295         
296         priv_set->count--;
297         priv_set->set = new_set;
298         
299         ret = NT_STATUS_OK;
300
301 done:
302         return ret;
303 }
304
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)
311 {
312         NTSTATUS ret;
313         LUID_ATTR *new_set;
314         LUID_ATTR *old_set;
315         int i;
316
317         if (!new_priv_set || !priv_set)
318                 return NT_STATUS_INVALID_PARAMETER;
319
320         /* special case if there are no privileges in the list */
321         if (priv_set->count == 0) {
322                 return NT_STATUS_OK;
323         }
324
325         /* 
326          * create a new list,
327          * and copy the other privileges
328          */
329
330         old_set = priv_set->set;
331
332         new_set = (LUID_ATTR *)talloc(new_priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
333         ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
334
335         for (i=0; i < priv_set->count; i++) {
336                 
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;
340         }
341                         
342         new_priv_set->count = priv_set->count;
343         new_priv_set->control = priv_set->control;
344         new_priv_set->set = new_set;
345         
346         ret = NT_STATUS_OK;
347
348 done:
349         return ret;
350 }