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