r23485: This checkin consists mostly of refactorings in preparation of the
[ira/wip.git] / source3 / 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    Copyright (C) Gerald (Jerry) Carter          2005
7    Copyright (C) Michael Adam                   2007
8    
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 2 of the License, or
12    (at your option) any later version.
13    
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.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23
24  
25 #include "includes.h"
26
27 #define PRIVPREFIX              "PRIV_"
28
29 typedef struct {
30         size_t count;
31         DOM_SID *list;
32 } SID_LIST;
33
34 typedef struct {
35         SE_PRIV privilege;
36         SID_LIST sids;
37 } PRIV_SID_LIST;
38
39
40 static BOOL get_privileges( const DOM_SID *sid, SE_PRIV *mask )
41 {
42         TDB_CONTEXT *tdb = get_account_pol_tdb();
43         fstring keystr;
44         TDB_DATA data;
45
46         /* Fail if the admin has not enable privileges */
47         
48         if ( !lp_enable_privileges() ) {
49                 return False;
50         }
51         
52         if ( !tdb )
53                 return False;
54
55         /* PRIV_<SID> (NULL terminated) as the key */
56         
57         fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
58
59         data = tdb_fetch_bystring( tdb, keystr );
60         
61         if ( !data.dptr ) {
62                 DEBUG(3,("get_privileges: No privileges assigned to SID [%s]\n",
63                         sid_string_static(sid)));
64                 return False;
65         }
66         
67         SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
68         
69         se_priv_copy( mask, (SE_PRIV*)data.dptr );
70         SAFE_FREE(data.dptr);
71
72         return True;
73 }
74
75 /***************************************************************************
76  Store the privilege mask (set) for a given SID
77 ****************************************************************************/
78
79 static BOOL set_privileges( const DOM_SID *sid, SE_PRIV *mask )
80 {
81         TDB_CONTEXT *tdb = get_account_pol_tdb();
82         fstring keystr;
83         TDB_DATA data;
84         
85         if ( !lp_enable_privileges() )
86                 return False;
87
88         if ( !tdb )
89                 return False;
90
91         if ( !sid || (sid->num_auths == 0) ) {
92                 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
93                 return False;
94         }
95
96         /* PRIV_<SID> (NULL terminated) as the key */
97         
98         fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
99         
100         /* no packing.  static size structure, just write it out */
101         
102         data.dptr  = (uint8 *)mask;
103         data.dsize = sizeof(SE_PRIV);
104
105         return ( tdb_store_bystring(tdb, keystr, data, TDB_REPLACE) != -1 );
106 }
107
108 /*********************************************************************
109  get a list of all privleges for all sids the in list
110 *********************************************************************/
111
112 BOOL get_privileges_for_sids(SE_PRIV *privileges, DOM_SID *slist, int scount)
113 {
114         SE_PRIV mask;
115         int i;
116         BOOL found = False;
117
118         se_priv_copy( privileges, &se_priv_none );
119         
120         for ( i=0; i<scount; i++ ) {
121                 /* don't add unless we actually have a privilege assigned */
122
123                 if ( !get_privileges( &slist[i], &mask ) )
124                         continue;
125
126                 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege set:\n", 
127                         sid_string_static(&slist[i])));
128                 dump_se_priv( DBGC_ALL, 5, &mask );
129                         
130                 se_priv_add( privileges, &mask );
131                 found = True;
132         }
133
134         return found;
135 }
136
137
138 /*********************************************************************
139  travseral functions for privilege_enumerate_accounts
140 *********************************************************************/
141
142 static int priv_traverse_fn(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
143 {
144         PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
145         int  prefixlen = strlen(PRIVPREFIX);
146         DOM_SID sid;
147         fstring sid_string;
148         
149         /* easy check first */
150         
151         if ( data.dsize != sizeof(SE_PRIV) )
152                 return 0;
153
154         /* check we have a PRIV_+SID entry */
155
156         if ( strncmp((const char *)key.dptr, PRIVPREFIX, prefixlen) != 0)
157                 return 0;
158                 
159         /* check to see if we are looking for a particular privilege */
160
161         if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
162                 SE_PRIV mask;
163                 
164                 se_priv_copy( &mask, (SE_PRIV*)data.dptr );
165                 
166                 /* if the SID does not have the specified privilege 
167                    then just return */
168                    
169                 if ( !is_privilege_assigned( &mask, &priv->privilege) )
170                         return 0;
171         }
172                 
173         fstrcpy( sid_string, (const char *)&key.dptr[strlen(PRIVPREFIX)] );
174
175         /* this is a last ditch safety check to preventing returning
176            and invalid SID (i've somehow run into this on development branches) */
177
178         if ( strcmp( "S-0-0", sid_string ) == 0 )
179                 return 0;
180
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;
185         }
186
187         if (!add_sid_to_array( NULL, &sid, &priv->sids.list, &priv->sids.count )) {
188                 return 0;
189         }
190         
191         return 0;
192 }
193
194 /*********************************************************************
195  Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
196 *********************************************************************/
197
198 NTSTATUS privilege_enumerate_accounts(DOM_SID **sids, int *num_sids)
199 {
200         TDB_CONTEXT *tdb = get_account_pol_tdb();
201         PRIV_SID_LIST priv;
202         
203         if (!tdb) {
204                 return NT_STATUS_ACCESS_DENIED;
205         }
206
207         ZERO_STRUCT(priv);
208
209         se_priv_copy( &priv.privilege, &se_priv_none );
210
211         tdb_traverse( tdb, priv_traverse_fn, &priv);
212
213         /* give the memory away; caller will free */
214         
215         *sids      = priv.sids.list;
216         *num_sids  = priv.sids.count;
217
218         return NT_STATUS_OK;
219 }
220
221 /***************************************************************************
222  Add privilege to sid
223 ****************************************************************************/
224
225 BOOL grant_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
226 {
227         SE_PRIV old_mask, new_mask;
228         
229         ZERO_STRUCT( old_mask );
230         ZERO_STRUCT( new_mask );
231
232         if ( get_privileges( sid, &old_mask ) )
233                 se_priv_copy( &new_mask, &old_mask );
234         else
235                 se_priv_copy( &new_mask, &se_priv_none );
236
237         se_priv_add( &new_mask, priv_mask );
238
239         DEBUG(10,("grant_privilege: %s\n", sid_string_static(sid)));
240         
241         DEBUGADD( 10, ("original privilege mask:\n"));
242         dump_se_priv( DBGC_ALL, 10, &old_mask );
243         
244         DEBUGADD( 10, ("new privilege mask:\n"));
245         dump_se_priv( DBGC_ALL, 10, &new_mask );
246         
247         return set_privileges( sid, &new_mask );
248 }
249
250 /*********************************************************************
251  Add a privilege based on its name
252 *********************************************************************/
253
254 BOOL grant_privilege_by_name(DOM_SID *sid, const char *name)
255 {
256         SE_PRIV mask;
257
258         if (! se_priv_from_name(name, &mask)) {
259                 DEBUG(3, ("grant_privilege_by_name: "
260                           "No Such Privilege Found (%s)\n", name));
261                 return False;
262         }
263
264         return grant_privilege( sid, &mask );
265 }
266
267 /***************************************************************************
268  Remove privilege from sid
269 ****************************************************************************/
270
271 BOOL revoke_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
272 {
273         SE_PRIV mask;
274         
275         /* if the user has no privileges, then we can't revoke any */
276         
277         if ( !get_privileges( sid, &mask ) )
278                 return True;
279         
280         DEBUG(10,("revoke_privilege: %s\n", sid_string_static(sid)));
281         
282         DEBUGADD( 10, ("original privilege mask:\n"));
283         dump_se_priv( DBGC_ALL, 10, &mask );
284
285         se_priv_remove( &mask, priv_mask );
286         
287         DEBUGADD( 10, ("new privilege mask:\n"));
288         dump_se_priv( DBGC_ALL, 10, &mask );
289         
290         return set_privileges( sid, &mask );
291 }
292
293 /*********************************************************************
294  Revoke all privileges
295 *********************************************************************/
296
297 BOOL revoke_all_privileges( DOM_SID *sid )
298 {
299         return revoke_privilege( sid, &se_priv_all );
300 }
301
302 /*********************************************************************
303  Add a privilege based on its name
304 *********************************************************************/
305
306 BOOL revoke_privilege_by_name(DOM_SID *sid, const char *name)
307 {
308         SE_PRIV mask;
309
310         if (! se_priv_from_name(name, &mask)) {
311                 DEBUG(3, ("revoke_privilege_by_name: "
312                           "No Such Privilege Found (%s)\n", name));
313                 return False;
314         }
315
316         return revoke_privilege(sid, &mask);
317
318 }
319
320 /***************************************************************************
321  Retrieve the SIDs assigned to a given privilege
322 ****************************************************************************/
323
324 NTSTATUS privilege_create_account(const DOM_SID *sid )
325 {
326         return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
327 }
328
329 /****************************************************************************
330  initialise a privilege list and set the talloc context 
331  ****************************************************************************/
332  
333 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
334 {
335         TALLOC_CTX *mem_ctx;
336         
337         ZERO_STRUCTP( priv_set );
338
339         mem_ctx = talloc_init("privilege set");
340         if ( !mem_ctx ) {
341                 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
342                 return NT_STATUS_NO_MEMORY;
343         }
344
345         priv_set->mem_ctx = mem_ctx;
346
347         return NT_STATUS_OK;
348 }
349
350 /****************************************************************************
351   initialise a privilege list and with someone else's talloc context 
352 ****************************************************************************/
353
354 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
355 {
356         ZERO_STRUCTP( priv_set );
357         
358         priv_set->mem_ctx = mem_ctx;
359         priv_set->ext_ctx = True;
360
361         return NT_STATUS_OK;
362 }
363
364 /****************************************************************************
365  Free all memory used by a PRIVILEGE_SET
366 ****************************************************************************/
367
368 void privilege_set_free(PRIVILEGE_SET *priv_set)
369 {
370         if ( !priv_set )
371                 return;
372
373         if ( !( priv_set->ext_ctx ) )
374                 talloc_destroy( priv_set->mem_ctx );
375
376         ZERO_STRUCTP( priv_set );
377 }
378
379 /****************************************************************************
380  duplicate alloc luid_attr
381  ****************************************************************************/
382
383 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
384 {
385         int i;
386
387         if ( !old_la )
388                 return NT_STATUS_OK;
389
390         if (count) {
391                 *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
392                 if ( !*new_la ) {
393                         DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
394                         return NT_STATUS_NO_MEMORY;
395                 }
396         } else {
397                 *new_la = NULL;
398         }
399
400         for (i=0; i<count; i++) {
401                 (*new_la)[i].luid.high = old_la[i].luid.high;
402                 (*new_la)[i].luid.low = old_la[i].luid.low;
403                 (*new_la)[i].attr = old_la[i].attr;
404         }
405         
406         return NT_STATUS_OK;
407 }
408
409 /*******************************************************************
410 *******************************************************************/
411
412 BOOL is_privileged_sid( const DOM_SID *sid )
413 {
414         SE_PRIV mask;
415         
416         return get_privileges( sid, &mask );
417 }
418
419 /*******************************************************************
420 *******************************************************************/
421
422 BOOL grant_all_privileges( const DOM_SID *sid )
423 {
424         SE_PRIV mask;
425
426         if (!se_priv_put_all_privileges(&mask)) {
427                 return False;
428         }
429         
430         return grant_privilege( sid, &mask );
431 }