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