s3-build: use dbwrap.h only where needed.
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include "includes.h"
25 #include "dbwrap.h"
26
27 #define PRIVPREFIX              "PRIV_"
28
29 typedef struct {
30         size_t count;
31         struct dom_sid *list;
32 } SID_LIST;
33
34 typedef struct {
35         TALLOC_CTX *mem_ctx;
36         SE_PRIV privilege;
37         SID_LIST sids;
38 } PRIV_SID_LIST;
39
40
41 static bool get_privileges( const struct dom_sid *sid, SE_PRIV *mask )
42 {
43         struct db_context *db = get_account_pol_db();
44         fstring tmp, keystr;
45         TDB_DATA data;
46
47         /* Fail if the admin has not enable privileges */
48
49         if ( !lp_enable_privileges() ) {
50                 return False;
51         }
52
53         if ( db == NULL )
54                 return False;
55
56         /* PRIV_<SID> (NULL terminated) as the key */
57
58         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
59
60         data = dbwrap_fetch_bystring( db, talloc_tos(), keystr );
61
62         if ( !data.dptr ) {
63                 DEBUG(3, ("get_privileges: No privileges assigned to SID "
64                           "[%s]\n", sid_string_dbg(sid)));
65                 return False;
66         }
67
68         SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
69
70         se_priv_copy( mask, (SE_PRIV*)data.dptr );
71         TALLOC_FREE(data.dptr);
72
73         return True;
74 }
75
76 /***************************************************************************
77  Store the privilege mask (set) for a given SID
78 ****************************************************************************/
79
80 static bool set_privileges( const struct dom_sid *sid, SE_PRIV *mask )
81 {
82         struct db_context *db = get_account_pol_db();
83         fstring tmp, keystr;
84         TDB_DATA data;
85
86         if ( !lp_enable_privileges() )
87                 return False;
88
89         if ( db == NULL )
90                 return False;
91
92         if ( !sid || (sid->num_auths == 0) ) {
93                 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
94                 return False;
95         }
96
97         /* PRIV_<SID> (NULL terminated) as the key */
98
99         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
100
101         /* no packing.  static size structure, just write it out */
102
103         data.dptr  = (uint8 *)mask;
104         data.dsize = sizeof(SE_PRIV);
105
106         return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
107                                                      TDB_REPLACE));
108 }
109
110 /*********************************************************************
111  get a list of all privileges for all sids in the list
112 *********************************************************************/
113
114 bool get_privileges_for_sids(SE_PRIV *privileges, struct dom_sid *slist, int scount)
115 {
116         SE_PRIV mask;
117         int i;
118         bool found = False;
119
120         se_priv_copy( privileges, &se_priv_none );
121
122         for ( i=0; i<scount; i++ ) {
123                 /* don't add unless we actually have a privilege assigned */
124
125                 if ( !get_privileges( &slist[i], &mask ) )
126                         continue;
127
128                 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
129                          "set:\n", sid_string_dbg(&slist[i])));
130                 dump_se_priv( DBGC_ALL, 5, &mask );
131
132                 se_priv_add( privileges, &mask );
133                 found = True;
134         }
135
136         return found;
137 }
138
139
140 /*********************************************************************
141  traversal functions for privilege_enumerate_accounts
142 *********************************************************************/
143
144 static int priv_traverse_fn(struct db_record *rec, void *state)
145 {
146         PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
147         int  prefixlen = strlen(PRIVPREFIX);
148         struct dom_sid sid;
149         fstring sid_string;
150
151         /* easy check first */
152
153         if (rec->value.dsize != sizeof(SE_PRIV) )
154                 return 0;
155
156         /* check we have a PRIV_+SID entry */
157
158         if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
159                 return 0;
160
161         /* check to see if we are looking for a particular privilege */
162
163         if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
164                 SE_PRIV mask;
165
166                 se_priv_copy( &mask, (SE_PRIV*)rec->value.dptr );
167
168                 /* if the SID does not have the specified privilege
169                    then just return */
170
171                 if ( !is_privilege_assigned( &mask, &priv->privilege) )
172                         return 0;
173         }
174
175         fstrcpy( sid_string, (char *)&(rec->key.dptr[strlen(PRIVPREFIX)]) );
176
177         /* this is a last ditch safety check to preventing returning
178            and invalid SID (i've somehow run into this on development branches) */
179
180         if ( strcmp( "S-0-0", sid_string ) == 0 )
181                 return 0;
182
183         if ( !string_to_sid(&sid, sid_string) ) {
184                 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
185                         sid_string));
186                 return 0;
187         }
188
189         if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
190                                               &priv->sids.list,
191                                               &priv->sids.count)))
192         {
193                 return 0;
194         }
195
196         return 0;
197 }
198
199 /*********************************************************************
200  Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
201 *********************************************************************/
202
203 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
204 {
205         struct db_context *db = get_account_pol_db();
206         PRIV_SID_LIST priv;
207
208         if (db == NULL) {
209                 return NT_STATUS_ACCESS_DENIED;
210         }
211
212         ZERO_STRUCT(priv);
213
214         se_priv_copy( &priv.privilege, &se_priv_none );
215
216         db->traverse_read(db, priv_traverse_fn, &priv);
217
218         /* give the memory away; caller will free */
219
220         *sids      = priv.sids.list;
221         *num_sids  = priv.sids.count;
222
223         return NT_STATUS_OK;
224 }
225
226 /*********************************************************************
227  Retrieve list of SIDs granted a particular privilege
228 *********************************************************************/
229
230 NTSTATUS privilege_enum_sids(const SE_PRIV *mask, TALLOC_CTX *mem_ctx,
231                              struct dom_sid **sids, int *num_sids)
232 {
233         struct db_context *db = get_account_pol_db();
234         PRIV_SID_LIST priv;
235
236         if (db == NULL) {
237                 return NT_STATUS_ACCESS_DENIED;
238         }
239
240         ZERO_STRUCT(priv);
241
242         se_priv_copy(&priv.privilege, mask);
243         priv.mem_ctx = mem_ctx;
244
245         db->traverse_read(db, priv_traverse_fn, &priv);
246
247         /* give the memory away; caller will free */
248
249         *sids      = priv.sids.list;
250         *num_sids  = priv.sids.count;
251
252         return NT_STATUS_OK;
253 }
254
255 /***************************************************************************
256  Add privilege to sid
257 ****************************************************************************/
258
259 bool grant_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
260 {
261         SE_PRIV old_mask, new_mask;
262
263         ZERO_STRUCT( old_mask );
264         ZERO_STRUCT( new_mask );
265
266         if ( get_privileges( sid, &old_mask ) )
267                 se_priv_copy( &new_mask, &old_mask );
268         else
269                 se_priv_copy( &new_mask, &se_priv_none );
270
271         se_priv_add( &new_mask, priv_mask );
272
273         DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
274
275         DEBUGADD( 10, ("original privilege mask:\n"));
276         dump_se_priv( DBGC_ALL, 10, &old_mask );
277
278         DEBUGADD( 10, ("new privilege mask:\n"));
279         dump_se_priv( DBGC_ALL, 10, &new_mask );
280
281         return set_privileges( sid, &new_mask );
282 }
283
284 /*********************************************************************
285  Add a privilege based on its name
286 *********************************************************************/
287
288 bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
289 {
290         SE_PRIV mask;
291
292         if (! se_priv_from_name(name, &mask)) {
293                 DEBUG(3, ("grant_privilege_by_name: "
294                           "No Such Privilege Found (%s)\n", name));
295                 return False;
296         }
297
298         return grant_privilege( sid, &mask );
299 }
300
301 /***************************************************************************
302  Remove privilege from sid
303 ****************************************************************************/
304
305 bool revoke_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
306 {
307         SE_PRIV mask;
308
309         /* if the user has no privileges, then we can't revoke any */
310
311         if ( !get_privileges( sid, &mask ) )
312                 return True;
313
314         DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
315
316         DEBUGADD( 10, ("original privilege mask:\n"));
317         dump_se_priv( DBGC_ALL, 10, &mask );
318
319         se_priv_remove( &mask, priv_mask );
320
321         DEBUGADD( 10, ("new privilege mask:\n"));
322         dump_se_priv( DBGC_ALL, 10, &mask );
323
324         return set_privileges( sid, &mask );
325 }
326
327 /*********************************************************************
328  Revoke all privileges
329 *********************************************************************/
330
331 bool revoke_all_privileges( struct dom_sid *sid )
332 {
333         return revoke_privilege( sid, &se_priv_all );
334 }
335
336 /*********************************************************************
337  Add a privilege based on its name
338 *********************************************************************/
339
340 bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
341 {
342         SE_PRIV mask;
343
344         if (! se_priv_from_name(name, &mask)) {
345                 DEBUG(3, ("revoke_privilege_by_name: "
346                           "No Such Privilege Found (%s)\n", name));
347                 return False;
348         }
349
350         return revoke_privilege(sid, &mask);
351
352 }
353
354 /***************************************************************************
355  Retrieve the SIDs assigned to a given privilege
356 ****************************************************************************/
357
358 NTSTATUS privilege_create_account(const struct dom_sid *sid )
359 {
360         return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
361 }
362
363 /***************************************************************************
364  Delete a privileged account
365 ****************************************************************************/
366
367 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
368 {
369         struct db_context *db = get_account_pol_db();
370         fstring tmp, keystr;
371
372         if (!lp_enable_privileges()) {
373                 return NT_STATUS_OK;
374         }
375
376         if (!db) {
377                 return NT_STATUS_INVALID_HANDLE;
378         }
379
380         if (!sid || (sid->num_auths == 0)) {
381                 return NT_STATUS_INVALID_SID;
382         }
383
384         /* PRIV_<SID> (NULL terminated) as the key */
385
386         fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
387
388         return dbwrap_delete_bystring(db, keystr);
389 }
390
391 /****************************************************************************
392  initialise a privilege list and set the talloc context
393  ****************************************************************************/
394
395 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
396 {
397         TALLOC_CTX *mem_ctx;
398
399         ZERO_STRUCTP( priv_set );
400
401         mem_ctx = talloc_init("privilege set");
402         if ( !mem_ctx ) {
403                 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
404                 return NT_STATUS_NO_MEMORY;
405         }
406
407         priv_set->mem_ctx = mem_ctx;
408
409         return NT_STATUS_OK;
410 }
411
412 /****************************************************************************
413   initialise a privilege list and with someone else's talloc context
414 ****************************************************************************/
415
416 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
417 {
418         ZERO_STRUCTP( priv_set );
419
420         priv_set->mem_ctx = mem_ctx;
421         priv_set->ext_ctx = True;
422
423         return NT_STATUS_OK;
424 }
425
426 /****************************************************************************
427  Free all memory used by a PRIVILEGE_SET
428 ****************************************************************************/
429
430 void privilege_set_free(PRIVILEGE_SET *priv_set)
431 {
432         if ( !priv_set )
433                 return;
434
435         if ( !( priv_set->ext_ctx ) )
436                 talloc_destroy( priv_set->mem_ctx );
437
438         ZERO_STRUCTP( priv_set );
439 }
440
441 /****************************************************************************
442  duplicate alloc luid_attr
443  ****************************************************************************/
444
445 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, struct lsa_LUIDAttribute **new_la, struct lsa_LUIDAttribute *old_la, int count)
446 {
447         int i;
448
449         if ( !old_la )
450                 return NT_STATUS_OK;
451
452         if (count) {
453                 *new_la = TALLOC_ARRAY(mem_ctx, struct lsa_LUIDAttribute, count);
454                 if ( !*new_la ) {
455                         DEBUG(0,("dup_luid_attr: failed to alloc new struct lsa_LUIDAttribute array [%d]\n", count));
456                         return NT_STATUS_NO_MEMORY;
457                 }
458         } else {
459                 *new_la = NULL;
460         }
461
462         for (i=0; i<count; i++) {
463                 (*new_la)[i].luid.high = old_la[i].luid.high;
464                 (*new_la)[i].luid.low = old_la[i].luid.low;
465                 (*new_la)[i].attribute = old_la[i].attribute;
466         }
467
468         return NT_STATUS_OK;
469 }
470
471 /*******************************************************************
472 *******************************************************************/
473
474 bool is_privileged_sid( const struct dom_sid *sid )
475 {
476         SE_PRIV mask;
477
478         return get_privileges( sid, &mask );
479 }
480
481 /*******************************************************************
482 *******************************************************************/
483
484 bool grant_all_privileges( const struct dom_sid *sid )
485 {
486         SE_PRIV mask;
487
488         if (!se_priv_put_all_privileges(&mask)) {
489                 return False;
490         }
491
492         return grant_privilege( sid, &mask );
493 }