Zero out the out policy handler in lsa_Close
[Samba/gebeck_regimport.git] / source3 / lib / privileges.c
blob509da80785334cf6b4bc0d9d5ea7c829443f4c7b
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 Copyright (C) Gerald (Jerry) Carter 2005
7 Copyright (C) Michael Adam 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "includes.h"
26 #define PRIVPREFIX "PRIV_"
28 typedef struct {
29 size_t count;
30 DOM_SID *list;
31 } SID_LIST;
33 typedef struct {
34 TALLOC_CTX *mem_ctx;
35 SE_PRIV privilege;
36 SID_LIST sids;
37 } PRIV_SID_LIST;
40 static bool get_privileges( const DOM_SID *sid, SE_PRIV *mask )
42 TDB_CONTEXT *tdb = get_account_pol_tdb();
43 fstring tmp, keystr;
44 TDB_DATA data;
46 /* Fail if the admin has not enable privileges */
48 if ( !lp_enable_privileges() ) {
49 return False;
52 if ( !tdb )
53 return False;
55 /* PRIV_<SID> (NULL terminated) as the key */
57 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
59 data = tdb_fetch_bystring( tdb, keystr );
61 if ( !data.dptr ) {
62 DEBUG(3, ("get_privileges: No privileges assigned to SID "
63 "[%s]\n", sid_string_dbg(sid)));
64 return False;
67 SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
69 se_priv_copy( mask, (SE_PRIV*)data.dptr );
70 SAFE_FREE(data.dptr);
72 return True;
75 /***************************************************************************
76 Store the privilege mask (set) for a given SID
77 ****************************************************************************/
79 static bool set_privileges( const DOM_SID *sid, SE_PRIV *mask )
81 TDB_CONTEXT *tdb = get_account_pol_tdb();
82 fstring tmp, keystr;
83 TDB_DATA data;
85 if ( !lp_enable_privileges() )
86 return False;
88 if ( !tdb )
89 return False;
91 if ( !sid || (sid->num_auths == 0) ) {
92 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
93 return False;
96 /* PRIV_<SID> (NULL terminated) as the key */
98 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
100 /* no packing. static size structure, just write it out */
102 data.dptr = (uint8 *)mask;
103 data.dsize = sizeof(SE_PRIV);
105 return ( tdb_store_bystring(tdb, keystr, data, TDB_REPLACE) != -1 );
108 /*********************************************************************
109 get a list of all privileges for all sids in the list
110 *********************************************************************/
112 bool get_privileges_for_sids(SE_PRIV *privileges, DOM_SID *slist, int scount)
114 SE_PRIV mask;
115 int i;
116 bool found = False;
118 se_priv_copy( privileges, &se_priv_none );
120 for ( i=0; i<scount; i++ ) {
121 /* don't add unless we actually have a privilege assigned */
123 if ( !get_privileges( &slist[i], &mask ) )
124 continue;
126 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
127 "set:\n", sid_string_dbg(&slist[i])));
128 dump_se_priv( DBGC_ALL, 5, &mask );
130 se_priv_add( privileges, &mask );
131 found = True;
134 return found;
138 /*********************************************************************
139 travseral functions for privilege_enumerate_accounts
140 *********************************************************************/
142 static int priv_traverse_fn(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
144 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
145 int prefixlen = strlen(PRIVPREFIX);
146 DOM_SID sid;
147 fstring sid_string;
149 /* easy check first */
151 if ( data.dsize != sizeof(SE_PRIV) )
152 return 0;
154 /* check we have a PRIV_+SID entry */
156 if ( strncmp((const char *)key.dptr, PRIVPREFIX, prefixlen) != 0)
157 return 0;
159 /* check to see if we are looking for a particular privilege */
161 if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
162 SE_PRIV mask;
164 se_priv_copy( &mask, (SE_PRIV*)data.dptr );
166 /* if the SID does not have the specified privilege
167 then just return */
169 if ( !is_privilege_assigned( &mask, &priv->privilege) )
170 return 0;
173 fstrcpy( sid_string, (const char *)&key.dptr[strlen(PRIVPREFIX)] );
175 /* this is a last ditch safety check to preventing returning
176 and invalid SID (i've somehow run into this on development branches) */
178 if ( strcmp( "S-0-0", sid_string ) == 0 )
179 return 0;
181 if ( !string_to_sid(&sid, sid_string) ) {
182 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
183 sid_string));
184 return 0;
187 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
188 &priv->sids.list,
189 &priv->sids.count)))
191 return 0;
194 return 0;
197 /*********************************************************************
198 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
199 *********************************************************************/
201 NTSTATUS privilege_enumerate_accounts(DOM_SID **sids, int *num_sids)
203 TDB_CONTEXT *tdb = get_account_pol_tdb();
204 PRIV_SID_LIST priv;
206 if (!tdb) {
207 return NT_STATUS_ACCESS_DENIED;
210 ZERO_STRUCT(priv);
212 se_priv_copy( &priv.privilege, &se_priv_none );
214 tdb_traverse( tdb, priv_traverse_fn, &priv);
216 /* give the memory away; caller will free */
218 *sids = priv.sids.list;
219 *num_sids = priv.sids.count;
221 return NT_STATUS_OK;
224 /*********************************************************************
225 Retrieve list of SIDs granted a particular privilege
226 *********************************************************************/
228 NTSTATUS privilege_enum_sids(const SE_PRIV *mask, TALLOC_CTX *mem_ctx,
229 DOM_SID **sids, int *num_sids)
231 TDB_CONTEXT *tdb = get_account_pol_tdb();
232 PRIV_SID_LIST priv;
234 if (!tdb) {
235 return NT_STATUS_ACCESS_DENIED;
238 ZERO_STRUCT(priv);
240 se_priv_copy(&priv.privilege, mask);
241 priv.mem_ctx = mem_ctx;
243 tdb_traverse( tdb, priv_traverse_fn, &priv);
245 /* give the memory away; caller will free */
247 *sids = priv.sids.list;
248 *num_sids = priv.sids.count;
250 return NT_STATUS_OK;
253 /***************************************************************************
254 Add privilege to sid
255 ****************************************************************************/
257 bool grant_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
259 SE_PRIV old_mask, new_mask;
261 ZERO_STRUCT( old_mask );
262 ZERO_STRUCT( new_mask );
264 if ( get_privileges( sid, &old_mask ) )
265 se_priv_copy( &new_mask, &old_mask );
266 else
267 se_priv_copy( &new_mask, &se_priv_none );
269 se_priv_add( &new_mask, priv_mask );
271 DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
273 DEBUGADD( 10, ("original privilege mask:\n"));
274 dump_se_priv( DBGC_ALL, 10, &old_mask );
276 DEBUGADD( 10, ("new privilege mask:\n"));
277 dump_se_priv( DBGC_ALL, 10, &new_mask );
279 return set_privileges( sid, &new_mask );
282 /*********************************************************************
283 Add a privilege based on its name
284 *********************************************************************/
286 bool grant_privilege_by_name(DOM_SID *sid, const char *name)
288 SE_PRIV mask;
290 if (! se_priv_from_name(name, &mask)) {
291 DEBUG(3, ("grant_privilege_by_name: "
292 "No Such Privilege Found (%s)\n", name));
293 return False;
296 return grant_privilege( sid, &mask );
299 /***************************************************************************
300 Remove privilege from sid
301 ****************************************************************************/
303 bool revoke_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
305 SE_PRIV mask;
307 /* if the user has no privileges, then we can't revoke any */
309 if ( !get_privileges( sid, &mask ) )
310 return True;
312 DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
314 DEBUGADD( 10, ("original privilege mask:\n"));
315 dump_se_priv( DBGC_ALL, 10, &mask );
317 se_priv_remove( &mask, priv_mask );
319 DEBUGADD( 10, ("new privilege mask:\n"));
320 dump_se_priv( DBGC_ALL, 10, &mask );
322 return set_privileges( sid, &mask );
325 /*********************************************************************
326 Revoke all privileges
327 *********************************************************************/
329 bool revoke_all_privileges( DOM_SID *sid )
331 return revoke_privilege( sid, &se_priv_all );
334 /*********************************************************************
335 Add a privilege based on its name
336 *********************************************************************/
338 bool revoke_privilege_by_name(DOM_SID *sid, const char *name)
340 SE_PRIV mask;
342 if (! se_priv_from_name(name, &mask)) {
343 DEBUG(3, ("revoke_privilege_by_name: "
344 "No Such Privilege Found (%s)\n", name));
345 return False;
348 return revoke_privilege(sid, &mask);
352 /***************************************************************************
353 Retrieve the SIDs assigned to a given privilege
354 ****************************************************************************/
356 NTSTATUS privilege_create_account(const DOM_SID *sid )
358 return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
361 /****************************************************************************
362 initialise a privilege list and set the talloc context
363 ****************************************************************************/
365 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
367 TALLOC_CTX *mem_ctx;
369 ZERO_STRUCTP( priv_set );
371 mem_ctx = talloc_init("privilege set");
372 if ( !mem_ctx ) {
373 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
374 return NT_STATUS_NO_MEMORY;
377 priv_set->mem_ctx = mem_ctx;
379 return NT_STATUS_OK;
382 /****************************************************************************
383 initialise a privilege list and with someone else's talloc context
384 ****************************************************************************/
386 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
388 ZERO_STRUCTP( priv_set );
390 priv_set->mem_ctx = mem_ctx;
391 priv_set->ext_ctx = True;
393 return NT_STATUS_OK;
396 /****************************************************************************
397 Free all memory used by a PRIVILEGE_SET
398 ****************************************************************************/
400 void privilege_set_free(PRIVILEGE_SET *priv_set)
402 if ( !priv_set )
403 return;
405 if ( !( priv_set->ext_ctx ) )
406 talloc_destroy( priv_set->mem_ctx );
408 ZERO_STRUCTP( priv_set );
411 /****************************************************************************
412 duplicate alloc luid_attr
413 ****************************************************************************/
415 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
417 int i;
419 if ( !old_la )
420 return NT_STATUS_OK;
422 if (count) {
423 *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
424 if ( !*new_la ) {
425 DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
426 return NT_STATUS_NO_MEMORY;
428 } else {
429 *new_la = NULL;
432 for (i=0; i<count; i++) {
433 (*new_la)[i].luid.high = old_la[i].luid.high;
434 (*new_la)[i].luid.low = old_la[i].luid.low;
435 (*new_la)[i].attr = old_la[i].attr;
438 return NT_STATUS_OK;
441 /*******************************************************************
442 *******************************************************************/
444 bool is_privileged_sid( const DOM_SID *sid )
446 SE_PRIV mask;
448 return get_privileges( sid, &mask );
451 /*******************************************************************
452 *******************************************************************/
454 bool grant_all_privileges( const DOM_SID *sid )
456 SE_PRIV mask;
458 if (!se_priv_put_all_privileges(&mask)) {
459 return False;
462 return grant_privilege( sid, &mask );