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