r9029: Another crash bug when enumerating privileges.
[amitay/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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23  
24 #include "includes.h"
25
26 #define PRIVPREFIX              "PRIV_"
27
28 static const SE_PRIV se_priv_all  = SE_ALL_PRIVS;
29 static const SE_PRIV se_priv_end  = SE_END;
30
31 /* Define variables for all privileges so we can use the
32    SE_PRIV* in the various se_priv_XXX() functions */
33
34 const SE_PRIV se_priv_none       = SE_NONE;
35 const SE_PRIV se_machine_account = SE_MACHINE_ACCOUNT;
36 const SE_PRIV se_print_operator  = SE_PRINT_OPERATOR;
37 const SE_PRIV se_add_users       = SE_ADD_USERS;
38 const SE_PRIV se_disk_operators  = SE_DISK_OPERATOR;
39 const SE_PRIV se_remote_shutdown = SE_REMOTE_SHUTDOWN;
40 const SE_PRIV se_restore         = SE_RESTORE;
41
42 /********************************************************************
43  This is a list of privileges reported by a WIndows 2000 SP4 AD DC
44  just for reference purposes (and I know the LUID is not guaranteed 
45  across reboots):
46
47             SeCreateTokenPrivilege  Create a token object ( 0x0, 0x2 )
48      SeAssignPrimaryTokenPrivilege  Replace a process level token ( 0x0, 0x3 )
49              SeLockMemoryPrivilege  Lock pages in memory ( 0x0, 0x4 )
50           SeIncreaseQuotaPrivilege  Increase quotas ( 0x0, 0x5 )
51          SeMachineAccountPrivilege  Add workstations to domain ( 0x0, 0x6 )
52                     SeTcbPrivilege  Act as part of the operating system ( 0x0, 0x7 )
53                SeSecurityPrivilege  Manage auditing and security log ( 0x0, 0x8 )
54           SeTakeOwnershipPrivilege  Take ownership of files or other objects ( 0x0, 0x9 )
55              SeLoadDriverPrivilege  Load and unload device drivers ( 0x0, 0xa )
56           SeSystemProfilePrivilege  Profile system performance ( 0x0, 0xb )
57              SeSystemtimePrivilege  Change the system time ( 0x0, 0xc )
58    SeProfileSingleProcessPrivilege  Profile single process ( 0x0, 0xd )
59    SeIncreaseBasePriorityPrivilege  Increase scheduling priority ( 0x0, 0xe )
60          SeCreatePagefilePrivilege  Create a pagefile ( 0x0, 0xf )
61         SeCreatePermanentPrivilege  Create permanent shared objects ( 0x0, 0x10 )
62                  SeBackupPrivilege  Back up files and directories ( 0x0, 0x11 )
63                 SeRestorePrivilege  Restore files and directories ( 0x0, 0x12 )
64                SeShutdownPrivilege  Shut down the system ( 0x0, 0x13 )
65                   SeDebugPrivilege  Debug programs ( 0x0, 0x14 )
66                   SeAuditPrivilege  Generate security audits ( 0x0, 0x15 )
67       SeSystemEnvironmentPrivilege  Modify firmware environment values ( 0x0, 0x16 )
68            SeChangeNotifyPrivilege  Bypass traverse checking ( 0x0, 0x17 )
69          SeRemoteShutdownPrivilege  Force shutdown from a remote system ( 0x0, 0x18 )
70                  SeUndockPrivilege  Remove computer from docking station ( 0x0, 0x19 )
71               SeSyncAgentPrivilege  Synchronize directory service data ( 0x0, 0x1a )
72        SeEnableDelegationPrivilege  Enable computer and user accounts to be trusted for delegation ( 0x0, 0x1b )
73            SeManageVolumePrivilege  Perform volume maintenance tasks ( 0x0, 0x1c )
74             SeImpersonatePrivilege  Impersonate a client after authentication ( 0x0, 0x1d )
75            SeCreateGlobalPrivilege  Create global objects ( 0x0, 0x1e )
76
77  ********************************************************************/
78
79 /* we have to define the LUID here due to a horrible check by printmig.exe
80    that requires the SeBackupPrivilege match what is in Windows.  So match
81    those that we implement and start Samba privileges at 0x1001 */
82    
83 PRIVS privs[] = {
84 #if 0   /* usrmgr will display these twice if you include them.  We don't 
85            use them but we'll keep the bitmasks reserved in privileges.h anyways */
86            
87         {SE_NETWORK_LOGON,      "SeNetworkLogonRight",          "Access this computer from network",       { 0x0, 0x0 }},
88         {SE_INTERACTIVE_LOGON,  "SeInteractiveLogonRight",      "Log on locally",                          { 0x0, 0x0 }},
89         {SE_BATCH_LOGON,        "SeBatchLogonRight",            "Log on as a batch job",                   { 0x0, 0x0 }},
90         {SE_SERVICE_LOGON,      "SeServiceLogonRight",          "Log on as a service",                     { 0x0, 0x0 }},
91 #endif
92         {SE_MACHINE_ACCOUNT,    "SeMachineAccountPrivilege",    "Add machines to domain",                  { 0x0, 0x0006 }},
93         {SE_TAKE_OWNERSHIP,     "SeTakeOwnershipPrivilege",     "Take ownership of files or other objects",{ 0x0, 0x0009 }},
94         {SE_BACKUP,             "SeBackupPrivilege",            "Back up files and directories",           { 0x0, 0x0011 }},
95         {SE_RESTORE,            "SeRestorePrivilege",           "Restore files and directories",           { 0x0, 0x0012 }},
96         {SE_REMOTE_SHUTDOWN,    "SeRemoteShutdownPrivilege",    "Force shutdown from a remote system",     { 0x0, 0x0018 }},
97         
98         {SE_PRINT_OPERATOR,     "SePrintOperatorPrivilege",     "Manage printers",                         { 0x0, 0x1001 }},
99         {SE_ADD_USERS,          "SeAddUsersPrivilege",          "Add users and groups to the domain",      { 0x0, 0x1002 }},
100         {SE_DISK_OPERATOR,      "SeDiskOperatorPrivilege",      "Manage disk shares",                      { 0x0, 0x1003 }},
101
102         {SE_END, "", "", { 0x0, 0x0 }}
103 };
104
105 typedef struct {
106         int count;
107         DOM_SID *list;
108 } SID_LIST;
109
110 typedef struct {
111         SE_PRIV privilege;
112         SID_LIST sids;
113 } PRIV_SID_LIST;
114
115 /***************************************************************************
116  copy an SE_PRIV structure
117 ****************************************************************************/
118
119 BOOL se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
120 {
121         if ( !dst || !src )
122                 return False;
123                 
124         memcpy( dst, src, sizeof(SE_PRIV) );
125         
126         return True;
127 }
128
129 /***************************************************************************
130  combine 2 SE_PRIV structures and store the resulting set in mew_mask
131 ****************************************************************************/
132
133 void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
134 {
135         int i;
136
137         for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
138                 mask->mask[i] |= addpriv->mask[i];
139         }
140 }
141
142 /***************************************************************************
143  remove one SE_PRIV sytucture from another and store the resulting set 
144  in mew_mask
145 ****************************************************************************/
146
147 void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
148 {       
149         int i;
150
151         for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
152                 mask->mask[i] &= ~removepriv->mask[i];
153         }
154 }
155
156 /***************************************************************************
157  invert a given SE_PRIV and store the set in new_mask
158 ****************************************************************************/
159
160 static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
161 {       
162         SE_PRIV allprivs;
163         
164         se_priv_copy( &allprivs, &se_priv_all );
165         se_priv_remove( &allprivs, mask );
166         se_priv_copy( new_mask, &allprivs );
167 }
168
169 /***************************************************************************
170  check if 2 SE_PRIV structure are equal
171 ****************************************************************************/
172
173 static BOOL se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
174 {       
175         return ( memcmp(mask1, mask2, sizeof(SE_PRIV)) == 0 );
176 }
177
178 /***************************************************************************
179  check if a SE_PRIV has any assigned privileges
180 ****************************************************************************/
181
182 static BOOL se_priv_empty( const SE_PRIV *mask )
183 {
184         SE_PRIV p1;
185         int i;
186         
187         se_priv_copy( &p1, mask );
188
189         for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
190                 p1.mask[i] &= se_priv_all.mask[i];
191         }
192         
193         return se_priv_equal( &p1, &se_priv_none );
194 }
195
196 /*********************************************************************
197  Lookup the SE_PRIV value for a privilege name 
198 *********************************************************************/
199
200 BOOL se_priv_from_name( const char *name, SE_PRIV *mask )
201 {
202         int i;
203
204         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
205                 if ( strequal( privs[i].name, name ) ) {
206                         se_priv_copy( mask, &privs[i].se_priv );
207                         return True;
208                 }
209         }
210
211         return False;
212 }
213
214 /***************************************************************************
215  dump an SE_PRIV structure to the log files
216 ****************************************************************************/
217
218 void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
219 {
220         int i;
221         
222         DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV "));
223         
224         for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
225                 DEBUGADDC( dbg_cl, dbg_lvl,(" 0x%x", mask->mask[i] ));
226         }
227                 
228         DEBUGADDC( dbg_cl, dbg_lvl, ("\n"));
229 }
230
231 /***************************************************************************
232  Retrieve the privilege mask (set) for a given SID
233 ****************************************************************************/
234
235 static BOOL get_privileges( const DOM_SID *sid, SE_PRIV *mask )
236 {
237         TDB_CONTEXT *tdb = get_account_pol_tdb();
238         fstring keystr;
239         TDB_DATA key, data;
240
241         /* Fail if the admin has not enable privileges */
242         
243         if ( !lp_enable_privileges() ) {
244                 return False;
245         }
246         
247         if ( !tdb )
248                 return False;
249
250         /* PRIV_<SID> (NULL terminated) as the key */
251         
252         fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
253         key.dptr = keystr;
254         key.dsize = strlen(keystr) + 1;
255
256         data = tdb_fetch( tdb, key );
257         
258         if ( !data.dptr ) {
259                 DEBUG(3,("get_privileges: No privileges assigned to SID [%s]\n",
260                         sid_string_static(sid)));
261                 return False;
262         }
263         
264         SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
265         
266         se_priv_copy( mask, (SE_PRIV*)data.dptr );
267         SAFE_FREE(data.dptr);
268
269         return True;
270 }
271
272 /***************************************************************************
273  Store the privilege mask (set) for a given SID
274 ****************************************************************************/
275
276 static BOOL set_privileges( const DOM_SID *sid, SE_PRIV *mask )
277 {
278         TDB_CONTEXT *tdb = get_account_pol_tdb();
279         fstring keystr;
280         TDB_DATA key, data;
281         
282         if ( !lp_enable_privileges() )
283                 return False;
284
285         if ( !tdb )
286                 return False;
287
288         /* PRIV_<SID> (NULL terminated) as the key */
289         
290         fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
291         key.dptr = keystr;
292         key.dsize = strlen(keystr) + 1;
293         
294         /* no packing.  static size structure, just write it out */
295         
296         data.dptr  = (char*)mask;
297         data.dsize = sizeof(SE_PRIV);
298
299         return ( tdb_store(tdb, key, data, TDB_REPLACE) != -1 );
300 }
301
302 /****************************************************************************
303  check if the privilege is in the privilege list
304 ****************************************************************************/
305
306 static BOOL is_privilege_assigned( SE_PRIV *privileges, const SE_PRIV *check )
307 {
308         SE_PRIV p1, p2;
309
310         if ( !privileges || !check )
311                 return False;
312         
313         /* everyone has privileges if you aren't checking for any */
314         
315         if ( se_priv_empty( check ) ) {
316                 DEBUG(1,("is_privilege_assigned: no privileges in check_mask!\n"));
317                 return True;
318         }
319         
320         se_priv_copy( &p1, check );
321         
322         /* invert the SE_PRIV we want to check for and remove that from the 
323            original set.  If we are left with the SE_PRIV we are checking 
324            for then return True */
325            
326         se_priv_invert( &p1, check );
327         se_priv_copy( &p2, privileges );
328         se_priv_remove( &p2, &p1 );
329         
330         return se_priv_equal( &p2, check );
331 }
332
333 /****************************************************************************
334  check if the privilege is in the privilege list
335 ****************************************************************************/
336
337 static BOOL is_any_privilege_assigned( SE_PRIV *privileges, const SE_PRIV *check )
338 {
339         SE_PRIV p1, p2;
340
341         if ( !privileges || !check )
342                 return False;
343         
344         /* everyone has privileges if you aren't checking for any */
345         
346         if ( se_priv_empty( check ) ) {
347                 DEBUG(1,("is_any_privilege_assigned: no privileges in check_mask!\n"));
348                 return True;
349         }
350         
351         se_priv_copy( &p1, check );
352         
353         /* invert the SE_PRIV we want to check for and remove that from the 
354            original set.  If we are left with the SE_PRIV we are checking 
355            for then return True */
356            
357         se_priv_invert( &p1, check );
358         se_priv_copy( &p2, privileges );
359         se_priv_remove( &p2, &p1 );
360         
361         /* see if we have any bits left */
362         
363         return !se_priv_empty( &p2 );
364 }
365
366 /****************************************************************************
367  add a privilege to a privilege array
368  ****************************************************************************/
369
370 static BOOL privilege_set_add(PRIVILEGE_SET *priv_set, LUID_ATTR set)
371 {
372         LUID_ATTR *new_set;
373
374         /* we can allocate memory to add the new privilege */
375
376         new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
377         if ( !new_set ) {
378                 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
379                 return False;
380         }       
381
382         new_set[priv_set->count].luid.high = set.luid.high;
383         new_set[priv_set->count].luid.low = set.luid.low;
384         new_set[priv_set->count].attr = set.attr;
385
386         priv_set->count++;
387         priv_set->set = new_set;
388
389         return True;
390 }
391
392 /*********************************************************************
393  Generate the LUID_ATTR structure based on a bitmask
394 *********************************************************************/
395
396 LUID_ATTR get_privilege_luid( SE_PRIV *mask )
397 {
398         LUID_ATTR priv_luid;
399         int i;
400
401         priv_luid.attr = 0;
402         priv_luid.luid.high = 0;
403         
404         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
405         
406                 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
407                         priv_luid.luid = privs[i].luid;
408                         break;
409                 }
410         }
411
412         return priv_luid;
413 }
414
415 /*********************************************************************
416  Generate the LUID_ATTR structure based on a bitmask
417 *********************************************************************/
418
419 const char* get_privilege_dispname( const char *name )
420 {
421         int i;
422
423         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
424         
425                 if ( strequal( privs[i].name, name ) ) {
426                         return privs[i].description;
427                 }
428         }
429
430         return NULL;
431 }
432
433 /*********************************************************************
434  get a list of all privleges for all sids the in list
435 *********************************************************************/
436
437 BOOL get_privileges_for_sids(SE_PRIV *privileges, DOM_SID *slist, int scount)
438 {
439         SE_PRIV mask;
440         int i;
441         BOOL found = False;
442
443         se_priv_copy( privileges, &se_priv_none );
444         
445         for ( i=0; i<scount; i++ ) {
446                 /* don't add unless we actually have a privilege assigned */
447
448                 if ( !get_privileges( &slist[i], &mask ) )
449                         continue;
450
451                 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege set:\n", 
452                         sid_string_static(&slist[i])));
453                 dump_se_priv( DBGC_ALL, 5, &mask );
454                         
455                 se_priv_add( privileges, &mask );
456                 found = True;
457         }
458
459         return found;
460 }
461
462
463 /*********************************************************************
464  travseral functions for privilege_enumerate_accounts
465 *********************************************************************/
466
467 static int priv_traverse_fn(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
468 {
469         PRIV_SID_LIST *priv = state;
470         int  prefixlen = strlen(PRIVPREFIX);
471         DOM_SID sid;
472         fstring sid_string;
473         
474         /* easy check first */
475         
476         if ( data.dsize != sizeof(SE_PRIV) )
477                 return 0;
478
479         /* check we have a PRIV_+SID entry */
480
481         if ( strncmp(key.dptr, PRIVPREFIX, prefixlen) != 0)
482                 return 0;
483                 
484         /* check to see if we are looking for a particular privilege */
485
486         if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
487                 SE_PRIV mask;
488                 
489                 se_priv_copy( &mask, (SE_PRIV*)data.dptr );
490                 
491                 /* if the SID does not have the specified privilege 
492                    then just return */
493                    
494                 if ( !is_privilege_assigned( &mask, &priv->privilege) )
495                         return 0;
496         }
497                 
498         fstrcpy( sid_string, &key.dptr[strlen(PRIVPREFIX)] );
499
500         if ( !string_to_sid(&sid, sid_string) ) {
501                 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
502                         sid_string));
503                 return 0;
504         }
505
506         add_sid_to_array( NULL, &sid, &priv->sids.list, &priv->sids.count );
507         
508         return 0;
509 }
510
511 /*********************************************************************
512  Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
513 *********************************************************************/
514
515 NTSTATUS privilege_enumerate_accounts(DOM_SID **sids, int *num_sids)
516 {
517         TDB_CONTEXT *tdb = get_account_pol_tdb();
518         PRIV_SID_LIST priv;
519         
520         ZERO_STRUCT(priv);
521
522         se_priv_copy( &priv.privilege, &se_priv_none );
523
524         tdb_traverse( tdb, priv_traverse_fn, &priv);
525
526         /* give the memory away; caller will free */
527         
528         *sids      = priv.sids.list;
529         *num_sids  = priv.sids.count;
530
531         return NT_STATUS_OK;
532 }
533
534 /***************************************************************************
535  Add privilege to sid
536 ****************************************************************************/
537
538 BOOL grant_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
539 {
540         SE_PRIV old_mask, new_mask;
541         
542         ZERO_STRUCT( old_mask );
543         ZERO_STRUCT( new_mask );
544
545         if ( get_privileges( sid, &old_mask ) )
546                 se_priv_copy( &new_mask, &old_mask );
547         else
548                 se_priv_copy( &new_mask, &se_priv_none );
549
550         se_priv_add( &new_mask, priv_mask );
551
552         DEBUG(10,("grant_privilege: %s\n", sid_string_static(sid)));
553         
554         DEBUGADD( 10, ("original privilege mask:\n"));
555         dump_se_priv( DBGC_ALL, 10, &old_mask );
556         
557         DEBUGADD( 10, ("new privilege mask:\n"));
558         dump_se_priv( DBGC_ALL, 10, &new_mask );
559         
560         return set_privileges( sid, &new_mask );
561 }
562
563 /*********************************************************************
564  Add a privilege based on its name
565 *********************************************************************/
566
567 BOOL grant_privilege_by_name(DOM_SID *sid, const char *name)
568 {
569         int i;
570
571         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
572                 if ( strequal(privs[i].name, name) ) {
573                         return grant_privilege( sid, &privs[i].se_priv );
574                 }
575         }
576
577         DEBUG(3, ("grant_privilege_by_name: No Such Privilege Found (%s)\n", name));
578
579         return False;
580 }
581
582 /***************************************************************************
583  Remove privilege from sid
584 ****************************************************************************/
585
586 BOOL revoke_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
587 {
588         SE_PRIV mask;
589         
590         /* if the user has no privileges, then we can't revoke any */
591         
592         if ( !get_privileges( sid, &mask ) )
593                 return True;
594         
595         DEBUG(10,("revoke_privilege: %s\n", sid_string_static(sid)));
596         
597         DEBUGADD( 10, ("original privilege mask:\n"));
598         dump_se_priv( DBGC_ALL, 10, &mask );
599
600         se_priv_remove( &mask, priv_mask );
601         
602         DEBUGADD( 10, ("new privilege mask:\n"));
603         dump_se_priv( DBGC_ALL, 10, &mask );
604         
605         return set_privileges( sid, &mask );
606 }
607
608 /*********************************************************************
609  Revoke all privileges
610 *********************************************************************/
611
612 BOOL revoke_all_privileges( DOM_SID *sid )
613 {
614         return revoke_privilege( sid, &se_priv_all );
615 }
616
617 /*********************************************************************
618  Add a privilege based on its name
619 *********************************************************************/
620
621 BOOL revoke_privilege_by_name(DOM_SID *sid, const char *name)
622 {
623         int i;
624
625         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
626                 if ( strequal(privs[i].name, name) ) {
627                         return revoke_privilege( sid, &privs[i].se_priv );
628                 }
629         }
630
631         DEBUG(3, ("revoke_privilege_by_name: No Such Privilege Found (%s)\n", name));
632
633         return False;
634 }
635
636 /***************************************************************************
637  Retrieve the SIDs assigned to a given privilege
638 ****************************************************************************/
639
640 NTSTATUS privilege_create_account(const DOM_SID *sid )
641 {
642         return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
643 }
644
645 /****************************************************************************
646  initialise a privilege list and set the talloc context 
647  ****************************************************************************/
648  
649 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
650 {
651         TALLOC_CTX *mem_ctx;
652         
653         ZERO_STRUCTP( priv_set );
654
655         mem_ctx = talloc_init("privilege set");
656         if ( !mem_ctx ) {
657                 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
658                 return NT_STATUS_NO_MEMORY;
659         }
660
661         priv_set->mem_ctx = mem_ctx;
662
663         return NT_STATUS_OK;
664 }
665
666 /****************************************************************************
667   initialise a privilege list and with someone else's talloc context 
668 ****************************************************************************/
669
670 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
671 {
672         ZERO_STRUCTP( priv_set );
673         
674         priv_set->mem_ctx = mem_ctx;
675         priv_set->ext_ctx = True;
676
677         return NT_STATUS_OK;
678 }
679
680 /****************************************************************************
681  Free all memory used by a PRIVILEGE_SET
682 ****************************************************************************/
683
684 void privilege_set_free(PRIVILEGE_SET *priv_set)
685 {
686         if ( !priv_set )
687                 return;
688
689         if ( !( priv_set->ext_ctx ) )
690                 talloc_destroy( priv_set->mem_ctx );
691
692         ZERO_STRUCTP( priv_set );
693 }
694
695 /****************************************************************************
696  duplicate alloc luid_attr
697  ****************************************************************************/
698
699 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
700 {
701         int i;
702
703         if ( !old_la )
704                 return NT_STATUS_OK;
705
706         *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
707         if ( !*new_la ) {
708                 DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
709                 return NT_STATUS_NO_MEMORY;
710         }
711
712         for (i=0; i<count; i++) {
713                 (*new_la)[i].luid.high = old_la[i].luid.high;
714                 (*new_la)[i].luid.low = old_la[i].luid.low;
715                 (*new_la)[i].attr = old_la[i].attr;
716         }
717         
718         return NT_STATUS_OK;
719 }
720
721 /****************************************************************************
722  Does the user have the specified privilege ?  We only deal with one privilege
723  at a time here.
724 *****************************************************************************/
725
726 BOOL user_has_privileges(NT_USER_TOKEN *token, const SE_PRIV *privilege)
727 {
728         if ( !token )
729                 return False;
730
731         return is_privilege_assigned( &token->privileges, privilege );
732 }
733
734 /****************************************************************************
735  Does the user have any of the specified privileges ?  We only deal with one privilege
736  at a time here.
737 *****************************************************************************/
738
739 BOOL user_has_any_privilege(NT_USER_TOKEN *token, const SE_PRIV *privilege)
740 {
741         if ( !token )
742                 return False;
743
744         return is_any_privilege_assigned( &token->privileges, privilege );
745 }
746
747 /****************************************************************************
748  Convert a LUID to a named string
749 ****************************************************************************/
750
751 char* luid_to_privilege_name(const LUID *set)
752 {
753         static fstring name;
754         int i;
755
756         if (set->high != 0)
757                 return NULL;
758
759         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
760                 if ( set->low == privs[i].luid.low ) {
761                         fstrcpy( name, privs[i].name );
762                         return name;
763                 }
764         }
765         
766         return NULL;
767 }
768
769 /*******************************************************************
770  return the number of elements in the privlege array
771 *******************************************************************/
772
773 int count_all_privileges( void )
774 {
775         static int count;
776         
777         if ( count )
778                 return count;
779
780         /* loop over the array and count it */  
781         for ( count=0; !se_priv_equal(&privs[count].se_priv, &se_priv_end); count++ ) ;
782
783         return count;
784 }
785
786 /*******************************************************************
787 *******************************************************************/
788
789 BOOL se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
790 {
791         int i;
792         uint32 num_privs = count_all_privileges();
793         LUID_ATTR luid;
794         
795         luid.attr = 0;
796         luid.luid.high = 0;
797         
798         for ( i=0; i<num_privs; i++ ) {
799                 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
800                         continue;
801                 
802                 luid.luid = privs[i].luid;
803                 
804                 if ( !privilege_set_add( set, luid ) )
805                         return False;
806         }
807
808         return True;
809 }
810
811 /*******************************************************************
812 *******************************************************************/
813
814 BOOL privilege_set_to_se_priv( SE_PRIV *mask, PRIVILEGE_SET *privset )
815 {
816         int i;
817         uint32 num_privs = count_all_privileges();
818         
819         ZERO_STRUCTP( mask );
820         
821         for ( i=0; i<privset->count; i++ ) {
822                 SE_PRIV r;
823         
824                 /* sanity check for invalid privilege.  we really
825                    only care about the low 32 bits */
826                    
827                 if ( privset->set[i].luid.high != 0 )
828                         return False;
829                 
830                 /* make sure :LUID.low is in range */   
831                 if ( privset->set[i].luid.low == 0 || privset->set[i].luid.low > num_privs )
832                         return False;
833                 
834                 r = privs[privset->set[i].luid.low - 1].se_priv;
835                 se_priv_add( mask, &r );
836         }
837
838         return True;
839 }
840
841 /*******************************************************************
842 *******************************************************************/
843
844 BOOL is_privileged_sid( DOM_SID *sid )
845 {
846         SE_PRIV mask;
847         
848         return get_privileges( sid, &mask );
849 }