[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / lib / privileges.c
blob9c60b80e451bb337e053c367660e1780290c418d
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
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.
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.
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.
24 #include "includes.h"
26 #define PRIVPREFIX "PRIV_"
28 static const SE_PRIV se_priv_all = SE_ALL_PRIVS;
29 static const SE_PRIV se_priv_end = SE_END;
31 /* Define variables for all privileges so we can use the
32 SE_PRIV* in the various se_priv_XXX() functions */
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;
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):
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 )
78 ********************************************************************/
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 */
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 */
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 }},
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 }},
103 {SE_END, "", "", { 0x0, 0x0 }}
106 typedef struct {
107 size_t count;
108 DOM_SID *list;
109 } SID_LIST;
111 typedef struct {
112 SE_PRIV privilege;
113 SID_LIST sids;
114 } PRIV_SID_LIST;
116 /***************************************************************************
117 copy an SE_PRIV structure
118 ****************************************************************************/
120 BOOL se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
122 if ( !dst || !src )
123 return False;
125 memcpy( dst, src, sizeof(SE_PRIV) );
127 return True;
130 /***************************************************************************
131 combine 2 SE_PRIV structures and store the resulting set in mew_mask
132 ****************************************************************************/
134 void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
136 int i;
138 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
139 mask->mask[i] |= addpriv->mask[i];
143 /***************************************************************************
144 remove one SE_PRIV sytucture from another and store the resulting set
145 in mew_mask
146 ****************************************************************************/
148 void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
150 int i;
152 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
153 mask->mask[i] &= ~removepriv->mask[i];
157 /***************************************************************************
158 invert a given SE_PRIV and store the set in new_mask
159 ****************************************************************************/
161 static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
163 SE_PRIV allprivs;
165 se_priv_copy( &allprivs, &se_priv_all );
166 se_priv_remove( &allprivs, mask );
167 se_priv_copy( new_mask, &allprivs );
170 /***************************************************************************
171 check if 2 SE_PRIV structure are equal
172 ****************************************************************************/
174 static BOOL se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
176 return ( memcmp(mask1, mask2, sizeof(SE_PRIV)) == 0 );
179 /***************************************************************************
180 check if a SE_PRIV has any assigned privileges
181 ****************************************************************************/
183 static BOOL se_priv_empty( const SE_PRIV *mask )
185 SE_PRIV p1;
186 int i;
188 se_priv_copy( &p1, mask );
190 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
191 p1.mask[i] &= se_priv_all.mask[i];
194 return se_priv_equal( &p1, &se_priv_none );
197 /*********************************************************************
198 Lookup the SE_PRIV value for a privilege name
199 *********************************************************************/
201 BOOL se_priv_from_name( const char *name, SE_PRIV *mask )
203 int i;
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;
212 return False;
215 /***************************************************************************
216 dump an SE_PRIV structure to the log files
217 ****************************************************************************/
219 void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
221 int i;
223 DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV "));
225 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
226 DEBUGADDC( dbg_cl, dbg_lvl,(" 0x%x", mask->mask[i] ));
229 DEBUGADDC( dbg_cl, dbg_lvl, ("\n"));
232 /***************************************************************************
233 Retrieve the privilege mask (set) for a given SID
234 ****************************************************************************/
236 static BOOL get_privileges( const DOM_SID *sid, SE_PRIV *mask )
238 TDB_CONTEXT *tdb = get_account_pol_tdb();
239 fstring keystr;
240 TDB_DATA key, data;
242 /* Fail if the admin has not enable privileges */
244 if ( !lp_enable_privileges() ) {
245 return False;
248 if ( !tdb )
249 return False;
251 /* PRIV_<SID> (NULL terminated) as the key */
253 fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
254 key.dptr = keystr;
255 key.dsize = strlen(keystr) + 1;
257 data = tdb_fetch( tdb, key );
259 if ( !data.dptr ) {
260 DEBUG(3,("get_privileges: No privileges assigned to SID [%s]\n",
261 sid_string_static(sid)));
262 return False;
265 SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
267 se_priv_copy( mask, (SE_PRIV*)data.dptr );
268 SAFE_FREE(data.dptr);
270 return True;
273 /***************************************************************************
274 Store the privilege mask (set) for a given SID
275 ****************************************************************************/
277 static BOOL set_privileges( const DOM_SID *sid, SE_PRIV *mask )
279 TDB_CONTEXT *tdb = get_account_pol_tdb();
280 fstring keystr;
281 TDB_DATA key, data;
283 if ( !lp_enable_privileges() )
284 return False;
286 if ( !tdb )
287 return False;
289 if ( !sid || (sid->num_auths == 0) ) {
290 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
291 return False;
294 /* PRIV_<SID> (NULL terminated) as the key */
296 fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
297 key.dptr = keystr;
298 key.dsize = strlen(keystr) + 1;
300 /* no packing. static size structure, just write it out */
302 data.dptr = (char*)mask;
303 data.dsize = sizeof(SE_PRIV);
305 return ( tdb_store(tdb, key, data, TDB_REPLACE) != -1 );
308 /****************************************************************************
309 check if the privilege is in the privilege list
310 ****************************************************************************/
312 static BOOL is_privilege_assigned( const SE_PRIV *privileges,
313 const SE_PRIV *check )
315 SE_PRIV p1, p2;
317 if ( !privileges || !check )
318 return False;
320 /* everyone has privileges if you aren't checking for any */
322 if ( se_priv_empty( check ) ) {
323 DEBUG(1,("is_privilege_assigned: no privileges in check_mask!\n"));
324 return True;
327 se_priv_copy( &p1, check );
329 /* invert the SE_PRIV we want to check for and remove that from the
330 original set. If we are left with the SE_PRIV we are checking
331 for then return True */
333 se_priv_invert( &p1, check );
334 se_priv_copy( &p2, privileges );
335 se_priv_remove( &p2, &p1 );
337 return se_priv_equal( &p2, check );
340 /****************************************************************************
341 check if the privilege is in the privilege list
342 ****************************************************************************/
344 static BOOL is_any_privilege_assigned( SE_PRIV *privileges, const SE_PRIV *check )
346 SE_PRIV p1, p2;
348 if ( !privileges || !check )
349 return False;
351 /* everyone has privileges if you aren't checking for any */
353 if ( se_priv_empty( check ) ) {
354 DEBUG(1,("is_any_privilege_assigned: no privileges in check_mask!\n"));
355 return True;
358 se_priv_copy( &p1, check );
360 /* invert the SE_PRIV we want to check for and remove that from the
361 original set. If we are left with the SE_PRIV we are checking
362 for then return True */
364 se_priv_invert( &p1, check );
365 se_priv_copy( &p2, privileges );
366 se_priv_remove( &p2, &p1 );
368 /* see if we have any bits left */
370 return !se_priv_empty( &p2 );
373 /****************************************************************************
374 add a privilege to a privilege array
375 ****************************************************************************/
377 static BOOL privilege_set_add(PRIVILEGE_SET *priv_set, LUID_ATTR set)
379 LUID_ATTR *new_set;
381 /* we can allocate memory to add the new privilege */
383 new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
384 if ( !new_set ) {
385 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
386 return False;
389 new_set[priv_set->count].luid.high = set.luid.high;
390 new_set[priv_set->count].luid.low = set.luid.low;
391 new_set[priv_set->count].attr = set.attr;
393 priv_set->count++;
394 priv_set->set = new_set;
396 return True;
399 /*********************************************************************
400 Generate the LUID_ATTR structure based on a bitmask
401 The assumption here is that the privilege has already been validated
402 so we are guaranteed to find it in the list.
403 *********************************************************************/
405 LUID_ATTR get_privilege_luid( SE_PRIV *mask )
407 LUID_ATTR priv_luid;
408 int i;
410 ZERO_STRUCT( priv_luid );
412 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
414 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
415 priv_luid.luid = privs[i].luid;
416 break;
420 return priv_luid;
423 /*********************************************************************
424 Generate the LUID_ATTR structure based on a bitmask
425 *********************************************************************/
427 const char* get_privilege_dispname( const char *name )
429 int i;
431 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
433 if ( strequal( privs[i].name, name ) ) {
434 return privs[i].description;
438 return NULL;
441 /*********************************************************************
442 get a list of all privleges for all sids the in list
443 *********************************************************************/
445 BOOL get_privileges_for_sids(SE_PRIV *privileges, DOM_SID *slist, int scount)
447 SE_PRIV mask;
448 int i;
449 BOOL found = False;
451 se_priv_copy( privileges, &se_priv_none );
453 for ( i=0; i<scount; i++ ) {
454 /* don't add unless we actually have a privilege assigned */
456 if ( !get_privileges( &slist[i], &mask ) )
457 continue;
459 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege set:\n",
460 sid_string_static(&slist[i])));
461 dump_se_priv( DBGC_ALL, 5, &mask );
463 se_priv_add( privileges, &mask );
464 found = True;
467 return found;
471 /*********************************************************************
472 travseral functions for privilege_enumerate_accounts
473 *********************************************************************/
475 static int priv_traverse_fn(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
477 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
478 int prefixlen = strlen(PRIVPREFIX);
479 DOM_SID sid;
480 fstring sid_string;
482 /* easy check first */
484 if ( data.dsize != sizeof(SE_PRIV) )
485 return 0;
487 /* check we have a PRIV_+SID entry */
489 if ( strncmp(key.dptr, PRIVPREFIX, prefixlen) != 0)
490 return 0;
492 /* check to see if we are looking for a particular privilege */
494 if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
495 SE_PRIV mask;
497 se_priv_copy( &mask, (SE_PRIV*)data.dptr );
499 /* if the SID does not have the specified privilege
500 then just return */
502 if ( !is_privilege_assigned( &mask, &priv->privilege) )
503 return 0;
506 fstrcpy( sid_string, &key.dptr[strlen(PRIVPREFIX)] );
508 /* this is a last ditch safety check to preventing returning
509 and invalid SID (i've somehow run into this on development branches) */
511 if ( strcmp( "S-0-0", sid_string ) == 0 )
512 return 0;
514 if ( !string_to_sid(&sid, sid_string) ) {
515 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
516 sid_string));
517 return 0;
520 if (!add_sid_to_array( NULL, &sid, &priv->sids.list, &priv->sids.count )) {
521 return 0;
524 return 0;
527 /*********************************************************************
528 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
529 *********************************************************************/
531 NTSTATUS privilege_enumerate_accounts(DOM_SID **sids, int *num_sids)
533 TDB_CONTEXT *tdb = get_account_pol_tdb();
534 PRIV_SID_LIST priv;
536 if (!tdb) {
537 return NT_STATUS_ACCESS_DENIED;
540 ZERO_STRUCT(priv);
542 se_priv_copy( &priv.privilege, &se_priv_none );
544 tdb_traverse( tdb, priv_traverse_fn, &priv);
546 /* give the memory away; caller will free */
548 *sids = priv.sids.list;
549 *num_sids = priv.sids.count;
551 return NT_STATUS_OK;
554 /***************************************************************************
555 Add privilege to sid
556 ****************************************************************************/
558 BOOL grant_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
560 SE_PRIV old_mask, new_mask;
562 ZERO_STRUCT( old_mask );
563 ZERO_STRUCT( new_mask );
565 if ( get_privileges( sid, &old_mask ) )
566 se_priv_copy( &new_mask, &old_mask );
567 else
568 se_priv_copy( &new_mask, &se_priv_none );
570 se_priv_add( &new_mask, priv_mask );
572 DEBUG(10,("grant_privilege: %s\n", sid_string_static(sid)));
574 DEBUGADD( 10, ("original privilege mask:\n"));
575 dump_se_priv( DBGC_ALL, 10, &old_mask );
577 DEBUGADD( 10, ("new privilege mask:\n"));
578 dump_se_priv( DBGC_ALL, 10, &new_mask );
580 return set_privileges( sid, &new_mask );
583 /*********************************************************************
584 Add a privilege based on its name
585 *********************************************************************/
587 BOOL grant_privilege_by_name(DOM_SID *sid, const char *name)
589 int i;
591 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
592 if ( strequal(privs[i].name, name) ) {
593 return grant_privilege( sid, &privs[i].se_priv );
597 DEBUG(3, ("grant_privilege_by_name: No Such Privilege Found (%s)\n", name));
599 return False;
602 /***************************************************************************
603 Remove privilege from sid
604 ****************************************************************************/
606 BOOL revoke_privilege(const DOM_SID *sid, const SE_PRIV *priv_mask)
608 SE_PRIV mask;
610 /* if the user has no privileges, then we can't revoke any */
612 if ( !get_privileges( sid, &mask ) )
613 return True;
615 DEBUG(10,("revoke_privilege: %s\n", sid_string_static(sid)));
617 DEBUGADD( 10, ("original privilege mask:\n"));
618 dump_se_priv( DBGC_ALL, 10, &mask );
620 se_priv_remove( &mask, priv_mask );
622 DEBUGADD( 10, ("new privilege mask:\n"));
623 dump_se_priv( DBGC_ALL, 10, &mask );
625 return set_privileges( sid, &mask );
628 /*********************************************************************
629 Revoke all privileges
630 *********************************************************************/
632 BOOL revoke_all_privileges( DOM_SID *sid )
634 return revoke_privilege( sid, &se_priv_all );
637 /*********************************************************************
638 Add a privilege based on its name
639 *********************************************************************/
641 BOOL revoke_privilege_by_name(DOM_SID *sid, const char *name)
643 int i;
645 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
646 if ( strequal(privs[i].name, name) ) {
647 return revoke_privilege( sid, &privs[i].se_priv );
651 DEBUG(3, ("revoke_privilege_by_name: No Such Privilege Found (%s)\n", name));
653 return False;
656 /***************************************************************************
657 Retrieve the SIDs assigned to a given privilege
658 ****************************************************************************/
660 NTSTATUS privilege_create_account(const DOM_SID *sid )
662 return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
665 /****************************************************************************
666 initialise a privilege list and set the talloc context
667 ****************************************************************************/
669 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
671 TALLOC_CTX *mem_ctx;
673 ZERO_STRUCTP( priv_set );
675 mem_ctx = talloc_init("privilege set");
676 if ( !mem_ctx ) {
677 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
678 return NT_STATUS_NO_MEMORY;
681 priv_set->mem_ctx = mem_ctx;
683 return NT_STATUS_OK;
686 /****************************************************************************
687 initialise a privilege list and with someone else's talloc context
688 ****************************************************************************/
690 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
692 ZERO_STRUCTP( priv_set );
694 priv_set->mem_ctx = mem_ctx;
695 priv_set->ext_ctx = True;
697 return NT_STATUS_OK;
700 /****************************************************************************
701 Free all memory used by a PRIVILEGE_SET
702 ****************************************************************************/
704 void privilege_set_free(PRIVILEGE_SET *priv_set)
706 if ( !priv_set )
707 return;
709 if ( !( priv_set->ext_ctx ) )
710 talloc_destroy( priv_set->mem_ctx );
712 ZERO_STRUCTP( priv_set );
715 /****************************************************************************
716 duplicate alloc luid_attr
717 ****************************************************************************/
719 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
721 int i;
723 if ( !old_la )
724 return NT_STATUS_OK;
726 if (count) {
727 *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
728 if ( !*new_la ) {
729 DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
730 return NT_STATUS_NO_MEMORY;
732 } else {
733 *new_la = NULL;
736 for (i=0; i<count; i++) {
737 (*new_la)[i].luid.high = old_la[i].luid.high;
738 (*new_la)[i].luid.low = old_la[i].luid.low;
739 (*new_la)[i].attr = old_la[i].attr;
742 return NT_STATUS_OK;
745 /****************************************************************************
746 Does the user have the specified privilege ? We only deal with one privilege
747 at a time here.
748 *****************************************************************************/
750 BOOL user_has_privileges(const NT_USER_TOKEN *token, const SE_PRIV *privilege)
752 if ( !token )
753 return False;
755 return is_privilege_assigned( &token->privileges, privilege );
758 /****************************************************************************
759 Does the user have any of the specified privileges ? We only deal with one privilege
760 at a time here.
761 *****************************************************************************/
763 BOOL user_has_any_privilege(NT_USER_TOKEN *token, const SE_PRIV *privilege)
765 if ( !token )
766 return False;
768 return is_any_privilege_assigned( &token->privileges, privilege );
771 /****************************************************************************
772 Convert a LUID to a named string
773 ****************************************************************************/
775 char* luid_to_privilege_name(const LUID *set)
777 static fstring name;
778 int i;
780 if (set->high != 0)
781 return NULL;
783 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
784 if ( set->low == privs[i].luid.low ) {
785 fstrcpy( name, privs[i].name );
786 return name;
790 return NULL;
793 /*******************************************************************
794 return the number of elements in the privlege array
795 *******************************************************************/
797 int count_all_privileges( void )
799 static int count;
801 if ( count )
802 return count;
804 /* loop over the array and count it */
805 for ( count=0; !se_priv_equal(&privs[count].se_priv, &se_priv_end); count++ ) ;
807 return count;
810 /*******************************************************************
811 *******************************************************************/
813 BOOL se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
815 int i;
816 uint32 num_privs = count_all_privileges();
817 LUID_ATTR luid;
819 luid.attr = 0;
820 luid.luid.high = 0;
822 for ( i=0; i<num_privs; i++ ) {
823 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
824 continue;
826 luid.luid = privs[i].luid;
828 if ( !privilege_set_add( set, luid ) )
829 return False;
832 return True;
835 /*******************************************************************
836 *******************************************************************/
838 static BOOL luid_to_se_priv( LUID *luid, SE_PRIV *mask )
840 int i;
841 uint32 num_privs = count_all_privileges();
843 for ( i=0; i<num_privs; i++ ) {
844 if ( luid->low == privs[i].luid.low ) {
845 se_priv_copy( mask, &privs[i].se_priv );
846 return True;
850 return False;
853 /*******************************************************************
854 *******************************************************************/
856 BOOL privilege_set_to_se_priv( SE_PRIV *mask, PRIVILEGE_SET *privset )
858 int i;
860 ZERO_STRUCTP( mask );
862 for ( i=0; i<privset->count; i++ ) {
863 SE_PRIV r;
865 /* sanity check for invalid privilege. we really
866 only care about the low 32 bits */
868 if ( privset->set[i].luid.high != 0 )
869 return False;
871 if ( luid_to_se_priv( &privset->set[i].luid, &r ) )
872 se_priv_add( mask, &r );
875 return True;
878 /*******************************************************************
879 *******************************************************************/
881 BOOL is_privileged_sid( const DOM_SID *sid )
883 SE_PRIV mask;
885 return get_privileges( sid, &mask );
888 /*******************************************************************
889 *******************************************************************/
891 BOOL grant_all_privileges( const DOM_SID *sid )
893 int i;
894 SE_PRIV mask;
895 uint32 num_privs = count_all_privileges();
897 se_priv_copy( &mask, &se_priv_none );
899 for ( i=0; i<num_privs; i++ ) {
900 se_priv_add(&mask, &privs[i].se_priv);
903 return grant_privilege( sid, &mask );