r4904: sync up with 3.0 for 3.0.11pre2
[Samba.git] / source / lib / privileges.c
blob628b2dd32516e368c42c754cb45e0ff529071c96
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
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 #define GENERATE_LUID_LOW(x) (x)+1;
30 static SE_PRIV se_priv_all = SE_ALL_PRIVS;
31 static SE_PRIV se_priv_end = SE_END;
32 static SE_PRIV se_priv_none = SE_NONE;
34 /* Define variables for all privileges so we can use the
35 SE_PRIV* in the various se_priv_XXX() functions */
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;
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 */
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"},
58 {SE_END, "", ""}
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, "", ""}
91 #endif
93 typedef struct priv_sid_list {
94 SE_PRIV privilege;
95 SID_LIST sids;
96 } PRIV_SID_LIST;
99 /***************************************************************************
100 copy an SE_PRIV structure
101 ****************************************************************************/
103 BOOL se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
105 if ( !dst || !src )
106 return False;
108 memcpy( dst, src, sizeof(SE_PRIV) );
110 return True;
113 /***************************************************************************
114 combine 2 SE_PRIV structures and store the resulting set in mew_mask
115 ****************************************************************************/
117 static void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
119 int i;
121 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
122 mask->mask[i] |= addpriv->mask[i];
126 /***************************************************************************
127 remove one SE_PRIV sytucture from another and store the resulting set
128 in mew_mask
129 ****************************************************************************/
131 static void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
133 int i;
135 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
136 mask->mask[i] &= ~removepriv->mask[i];
140 /***************************************************************************
141 invert a given SE_PRIV and store the set in new_mask
142 ****************************************************************************/
144 static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
146 SE_PRIV allprivs;
148 se_priv_copy( &allprivs, &se_priv_all );
149 se_priv_remove( &allprivs, mask );
150 se_priv_copy( new_mask, &allprivs );
153 /***************************************************************************
154 check if 2 SE_PRIV structure are equal
155 ****************************************************************************/
157 static BOOL se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
159 return ( memcmp(mask1, mask2, sizeof(SE_PRIV)) == 0 );
163 /***************************************************************************
164 dump an SE_PRIV structure to the log files
165 ****************************************************************************/
167 void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
169 int i;
171 DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV "));
173 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
174 DEBUGADDC( dbg_cl, dbg_lvl,(" 0x%x", mask->mask[i] ));
177 DEBUGADDC( dbg_cl, dbg_lvl, ("\n"));
180 /***************************************************************************
181 Retrieve the privilege mask (set) for a given SID
182 ****************************************************************************/
184 static BOOL get_privileges( const DOM_SID *sid, SE_PRIV *mask )
186 TDB_CONTEXT *tdb = get_account_pol_tdb();
187 fstring keystr;
188 TDB_DATA key, data;
190 /* Fail if the admin has not enable privileges */
192 if ( !lp_enable_privileges() ) {
193 return False;
196 if ( !tdb )
197 return False;
199 /* PRIV_<SID> (NULL terminated) as the key */
201 fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
202 key.dptr = keystr;
203 key.dsize = strlen(keystr) + 1;
205 data = tdb_fetch( tdb, key );
207 if ( !data.dptr ) {
208 DEBUG(3,("get_privileges: No privileges assigned to SID [%s]\n",
209 sid_string_static(sid)));
210 return False;
213 SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
215 se_priv_copy( mask, (SE_PRIV*)data.dptr );
218 return True;
221 /***************************************************************************
222 Store the privilege mask (set) for a given SID
223 ****************************************************************************/
225 static BOOL set_privileges( const DOM_SID *sid, SE_PRIV *mask )
227 TDB_CONTEXT *tdb = get_account_pol_tdb();
228 fstring keystr;
229 TDB_DATA key, data;
231 if ( !tdb )
232 return False;
234 /* PRIV_<SID> (NULL terminated) as the key */
236 fstr_sprintf( keystr, "%s%s", PRIVPREFIX, sid_string_static(sid) );
237 key.dptr = keystr;
238 key.dsize = strlen(keystr) + 1;
240 /* no packing. static size structure, just write it out */
242 data.dptr = (char*)mask;
243 data.dsize = sizeof(SE_PRIV);
245 return ( tdb_store(tdb, key, data, TDB_REPLACE) != -1 );
248 /****************************************************************************
249 check if the privilege is in the privilege list
250 ****************************************************************************/
252 static BOOL is_privilege_assigned( SE_PRIV *privileges, SE_PRIV *check )
254 SE_PRIV p1, p2;
256 if ( !privileges || !check )
257 return False;
259 se_priv_copy( &p1, check );
261 /* invert the SE_PRIV we want to check for and remove that from the
262 original set. If we are left with the SE_PRIV we are checking
263 for then return True */
265 se_priv_invert( &p1, check );
266 se_priv_copy( &p2, privileges );
267 se_priv_remove( &p2, &p1 );
269 return se_priv_equal( &p2, check );
272 /****************************************************************************
273 add a privilege to a privilege array
274 ****************************************************************************/
276 static BOOL privilege_set_add(PRIVILEGE_SET *priv_set, LUID_ATTR set)
278 LUID_ATTR *new_set;
280 /* we can allocate memory to add the new privilege */
282 new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
283 if ( !new_set ) {
284 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
285 return False;
288 new_set[priv_set->count].luid.high = set.luid.high;
289 new_set[priv_set->count].luid.low = set.luid.low;
290 new_set[priv_set->count].attr = set.attr;
292 priv_set->count++;
293 priv_set->set = new_set;
295 return True;
298 /*********************************************************************
299 Generate the LUID_ATTR structure based on a bitmask
300 *********************************************************************/
302 LUID_ATTR get_privilege_luid( SE_PRIV *mask )
304 LUID_ATTR priv_luid;
305 int i;
307 priv_luid.attr = 0;
308 priv_luid.luid.high = 0;
310 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
312 /* just use the index+1 (so its non-zero) into the
313 array as the lower portion of the LUID */
315 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
316 priv_luid.luid.low = GENERATE_LUID_LOW(i);
320 return priv_luid;
323 /*********************************************************************
324 Generate the LUID_ATTR structure based on a bitmask
325 *********************************************************************/
327 const char* get_privilege_dispname( const char *name )
329 int i;
331 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
333 if ( strequal( privs[i].name, name ) ) {
334 return privs[i].description;
338 return NULL;
341 /*********************************************************************
342 get a list of all privleges for all sids the in list
343 *********************************************************************/
345 BOOL get_privileges_for_sids(SE_PRIV *privileges, DOM_SID *slist, int scount)
347 SE_PRIV mask;
348 int i;
349 BOOL found = False;
351 se_priv_copy( privileges, &se_priv_none );
353 for ( i=0; i<scount; i++ ) {
354 /* don't add unless we actually have a privilege assigned */
356 if ( !get_privileges( &slist[i], &mask ) )
357 continue;
359 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege set:\n",
360 sid_string_static(&slist[i])));
361 dump_se_priv( DBGC_ALL, 5, &mask );
363 se_priv_add( privileges, &mask );
364 found = True;
367 return found;
371 /*********************************************************************
372 travseral functions for privilege_enumerate_accounts
373 *********************************************************************/
375 static int priv_traverse_fn(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void *state)
377 PRIV_SID_LIST *priv = state;
378 int prefixlen = strlen(PRIVPREFIX);
379 DOM_SID sid;
380 fstring sid_string;
382 /* easy check first */
384 if ( data.dsize != sizeof(SE_PRIV) )
385 return 0;
387 /* check we have a PRIV_+SID entry */
389 if ( strncmp(key.dptr, PRIVPREFIX, prefixlen) != 0)
390 return 0;
392 /* check to see if we are looking for a particular privilege */
394 if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
395 SE_PRIV mask;
397 se_priv_copy( &mask, (SE_PRIV*)data.dptr );
399 /* if the SID does not have the specified privilege
400 then just return */
402 if ( !is_privilege_assigned( &mask, &priv->privilege) )
403 return 0;
406 fstrcpy( sid_string, &key.dptr[strlen(PRIVPREFIX)] );
408 if ( !string_to_sid(&sid, sid_string) ) {
409 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
410 sid_string));
411 return 0;
414 add_sid_to_array( &sid, &priv->sids.list, &priv->sids.count );
416 return 0;
419 /*********************************************************************
420 Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
421 *********************************************************************/
423 NTSTATUS privilege_enumerate_accounts(DOM_SID **sids, int *num_sids)
425 TDB_CONTEXT *tdb = get_account_pol_tdb();
426 PRIV_SID_LIST priv;
428 ZERO_STRUCT(priv);
430 se_priv_copy( &priv.privilege, &se_priv_none );
432 tdb_traverse( tdb, priv_traverse_fn, &priv);
434 /* give the memory away; caller will free */
436 *sids = priv.sids.list;
437 *num_sids = priv.sids.count;
439 return NT_STATUS_OK;
442 /***************************************************************************
443 Add privilege to sid
444 ****************************************************************************/
446 BOOL grant_privilege(const DOM_SID *sid, SE_PRIV *priv_mask)
448 SE_PRIV old_mask, new_mask;
450 if ( get_privileges( sid, &old_mask ) )
451 se_priv_copy( &new_mask, &old_mask );
452 else
453 se_priv_copy( &new_mask, &se_priv_none );
455 se_priv_add( &new_mask, priv_mask );
457 DEBUG(10,("grant_privilege: %s\n", sid_string_static(sid)));
459 DEBUGADD( 10, ("original privilege mask:\n"));
460 dump_se_priv( DBGC_ALL, 10, &old_mask );
462 DEBUGADD( 10, ("new privilege mask:\n"));
463 dump_se_priv( DBGC_ALL, 10, &new_mask );
465 return set_privileges( sid, &new_mask );
468 /*********************************************************************
469 Add a privilege based on its name
470 *********************************************************************/
472 BOOL grant_privilege_by_name(DOM_SID *sid, const char *name)
474 int i;
476 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
477 if ( strequal(privs[i].name, name) ) {
478 return grant_privilege( sid, &privs[i].se_priv );
482 DEBUG(3, ("grant_privilege_by_name: No Such Privilege Found (%s)\n", name));
484 return False;
487 /***************************************************************************
488 Remove privilege from sid
489 ****************************************************************************/
491 BOOL revoke_privilege(const DOM_SID *sid, SE_PRIV *priv_mask)
493 SE_PRIV mask;
495 /* if the user has no privileges, then we can't revoke any */
497 if ( !get_privileges( sid, &mask ) )
498 return True;
500 DEBUG(10,("revoke_privilege: %s\n", sid_string_static(sid)));
502 DEBUGADD( 10, ("original privilege mask:\n"));
503 dump_se_priv( DBGC_ALL, 10, &mask );
505 se_priv_remove( &mask, priv_mask );
507 DEBUGADD( 10, ("new privilege mask:\n"));
508 dump_se_priv( DBGC_ALL, 10, &mask );
510 return set_privileges( sid, &mask );
513 /*********************************************************************
514 Revoke all privileges
515 *********************************************************************/
517 BOOL revoke_all_privileges( DOM_SID *sid )
519 return revoke_privilege( sid, &se_priv_all );
522 /*********************************************************************
523 Add a privilege based on its name
524 *********************************************************************/
526 BOOL revoke_privilege_by_name(DOM_SID *sid, const char *name)
528 int i;
530 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
531 if ( strequal(privs[i].name, name) ) {
532 return revoke_privilege( sid, &privs[i].se_priv );
536 DEBUG(3, ("revoke_privilege_by_name: No Such Privilege Found (%s)\n", name));
538 return False;
541 /***************************************************************************
542 Retrieve the SIDs assigned to a given privilege
543 ****************************************************************************/
545 NTSTATUS privilege_create_account(const DOM_SID *sid )
547 return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
550 /****************************************************************************
551 initialise a privilege list and set the talloc context
552 ****************************************************************************/
553 NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
555 TALLOC_CTX *mem_ctx;
557 ZERO_STRUCTP( priv_set );
559 mem_ctx = talloc_init("privilege set");
560 if ( !mem_ctx ) {
561 DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
562 return NT_STATUS_NO_MEMORY;
565 priv_set->mem_ctx = mem_ctx;
567 return NT_STATUS_OK;
570 /****************************************************************************
571 initialise a privilege list and with someone else's talloc context
572 ****************************************************************************/
574 NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
576 ZERO_STRUCTP( priv_set );
578 priv_set->mem_ctx = mem_ctx;
579 priv_set->ext_ctx = True;
581 return NT_STATUS_OK;
584 /****************************************************************************
585 Free all memory used by a PRIVILEGE_SET
586 ****************************************************************************/
588 void privilege_set_free(PRIVILEGE_SET *priv_set)
590 if ( !priv_set )
591 return;
593 if ( !( priv_set->ext_ctx ) )
594 talloc_destroy( priv_set->mem_ctx );
596 ZERO_STRUCTP( priv_set );
599 /****************************************************************************
600 duplicate alloc luid_attr
601 ****************************************************************************/
603 NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
605 int i;
607 /* don't crash if the source pointer is NULL (since we don't
608 do priviledges now anyways) */
610 if ( !old_la )
611 return NT_STATUS_OK;
613 *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
614 if ( !*new_la ) {
615 DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
616 return NT_STATUS_NO_MEMORY;
619 for (i=0; i<count; i++) {
620 (*new_la)[i].luid.high = old_la[i].luid.high;
621 (*new_la)[i].luid.low = old_la[i].luid.low;
622 (*new_la)[i].attr = old_la[i].attr;
625 return NT_STATUS_OK;
628 /****************************************************************************
629 Does the user have the specified privilege ? We only deal with one privilege
630 at a time here.
631 *****************************************************************************/
633 BOOL user_has_privileges(NT_USER_TOKEN *token, SE_PRIV *privilege)
635 if ( !token )
636 return False;
638 return is_privilege_assigned( &token->privileges, privilege );
641 /****************************************************************************
642 Convert a LUID to a named string
643 ****************************************************************************/
645 char* luid_to_privilege_name(const LUID *set)
647 static fstring name;
648 int max = count_all_privileges();
650 if (set->high != 0)
651 return NULL;
653 if ( set->low > max )
654 return NULL;
656 fstrcpy( name, privs[set->low - 1].name );
658 return name;
661 /****************************************************************************
662 Convert an LUID to a 32-bit mask
663 ****************************************************************************/
665 SE_PRIV* luid_to_privilege_mask(const LUID *set)
667 static SE_PRIV mask;
668 int max = count_all_privileges();
670 if (set->high != 0)
671 return NULL;
673 if ( set->low > max )
674 return NULL;
676 se_priv_copy( &mask, &privs[set->low - 1].se_priv );
678 return &mask;
681 /*******************************************************************
682 return the number of elements in the privlege array
683 *******************************************************************/
685 int count_all_privileges( void )
687 static int count;
689 if ( count )
690 return count;
692 /* loop over the array and count it */
693 for ( count=0; !se_priv_equal(&privs[count].se_priv, &se_priv_end); count++ ) ;
695 return count;
698 /*******************************************************************
699 *******************************************************************/
701 BOOL se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
703 int i;
704 uint32 num_privs = count_all_privileges();
705 LUID_ATTR luid;
707 luid.attr = 0;
708 luid.luid.high = 0;
710 for ( i=0; i<num_privs; i++ ) {
711 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
712 continue;
714 luid.luid.low = GENERATE_LUID_LOW(i);
716 if ( !privilege_set_add( set, luid ) )
717 return False;
720 return True;
723 /*******************************************************************
724 *******************************************************************/
726 BOOL privilege_set_to_se_priv( SE_PRIV *mask, PRIVILEGE_SET *privset )
728 int i;
729 uint32 num_privs = count_all_privileges();
731 ZERO_STRUCTP( mask );
733 for ( i=0; i<privset->count; i++ ) {
734 SE_PRIV r;
736 /* sanity check for invalid privilege. we really
737 only care about the low 32 bits */
739 if ( privset->set[i].luid.high != 0 )
740 return False;
742 /* make sure :LUID.low is in range */
743 if ( privset->set[i].luid.low == 0 || privset->set[i].luid.low > num_privs )
744 return False;
746 r = privs[privset->set[i].luid.low - 1].se_priv;
747 se_priv_add( mask, &r );
750 return True;
753 /*******************************************************************
754 *******************************************************************/
756 BOOL is_privileged_sid( DOM_SID *sid )
758 SE_PRIV mask;
760 return get_privileges( sid, &mask );