r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / privileges_basic.c
blob93ecee89dceefe6eb63fc56b7e87f439133a95fe
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 Copyright (C) Michael Adam 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Basic privileges functions (mask-operations and conversion
26 * functions between the different formats (se_priv, privset, luid)
27 * moved here * from lib/privileges.c to minimize linker deps.
29 * generally SID- and LUID-related code is left in lib/privileges.c
31 * some extra functions to hide privs array from lib/privileges.c
34 #include "includes.h"
36 const SE_PRIV se_priv_all = SE_ALL_PRIVS;
37 static const SE_PRIV se_priv_end = SE_END;
39 /* Define variables for all privileges so we can use the
40 SE_PRIV* in the various se_priv_XXX() functions */
42 const SE_PRIV se_priv_none = SE_NONE;
43 const SE_PRIV se_machine_account = SE_MACHINE_ACCOUNT;
44 const SE_PRIV se_print_operator = SE_PRINT_OPERATOR;
45 const SE_PRIV se_add_users = SE_ADD_USERS;
46 const SE_PRIV se_disk_operators = SE_DISK_OPERATOR;
47 const SE_PRIV se_remote_shutdown = SE_REMOTE_SHUTDOWN;
48 const SE_PRIV se_restore = SE_RESTORE;
49 const SE_PRIV se_take_ownership = SE_TAKE_OWNERSHIP;
51 /********************************************************************
52 This is a list of privileges reported by a WIndows 2000 SP4 AD DC
53 just for reference purposes (and I know the LUID is not guaranteed
54 across reboots):
56 SeCreateTokenPrivilege Create a token object ( 0x0, 0x2 )
57 SeAssignPrimaryTokenPrivilege Replace a process level token ( 0x0, 0x3 )
58 SeLockMemoryPrivilege Lock pages in memory ( 0x0, 0x4 )
59 SeIncreaseQuotaPrivilege Increase quotas ( 0x0, 0x5 )
60 SeMachineAccountPrivilege Add workstations to domain ( 0x0, 0x6 )
61 SeTcbPrivilege Act as part of the operating system ( 0x0, 0x7 )
62 SeSecurityPrivilege Manage auditing and security log ( 0x0, 0x8 )
63 SeTakeOwnershipPrivilege Take ownership of files or other objects ( 0x0, 0x9 )
64 SeLoadDriverPrivilege Load and unload device drivers ( 0x0, 0xa )
65 SeSystemProfilePrivilege Profile system performance ( 0x0, 0xb )
66 SeSystemtimePrivilege Change the system time ( 0x0, 0xc )
67 SeProfileSingleProcessPrivilege Profile single process ( 0x0, 0xd )
68 SeIncreaseBasePriorityPrivilege Increase scheduling priority ( 0x0, 0xe )
69 SeCreatePagefilePrivilege Create a pagefile ( 0x0, 0xf )
70 SeCreatePermanentPrivilege Create permanent shared objects ( 0x0, 0x10 )
71 SeBackupPrivilege Back up files and directories ( 0x0, 0x11 )
72 SeRestorePrivilege Restore files and directories ( 0x0, 0x12 )
73 SeShutdownPrivilege Shut down the system ( 0x0, 0x13 )
74 SeDebugPrivilege Debug programs ( 0x0, 0x14 )
75 SeAuditPrivilege Generate security audits ( 0x0, 0x15 )
76 SeSystemEnvironmentPrivilege Modify firmware environment values ( 0x0, 0x16 )
77 SeChangeNotifyPrivilege Bypass traverse checking ( 0x0, 0x17 )
78 SeRemoteShutdownPrivilege Force shutdown from a remote system ( 0x0, 0x18 )
79 SeUndockPrivilege Remove computer from docking station ( 0x0, 0x19 )
80 SeSyncAgentPrivilege Synchronize directory service data ( 0x0, 0x1a )
81 SeEnableDelegationPrivilege Enable computer and user accounts to be trusted for delegation ( 0x0, 0x1b )
82 SeManageVolumePrivilege Perform volume maintenance tasks ( 0x0, 0x1c )
83 SeImpersonatePrivilege Impersonate a client after authentication ( 0x0, 0x1d )
84 SeCreateGlobalPrivilege Create global objects ( 0x0, 0x1e )
86 ********************************************************************/
88 /* we have to define the LUID here due to a horrible check by printmig.exe
89 that requires the SeBackupPrivilege match what is in Windows. So match
90 those that we implement and start Samba privileges at 0x1001 */
92 PRIVS privs[] = {
93 #if 0 /* usrmgr will display these twice if you include them. We don't
94 use them but we'll keep the bitmasks reserved in privileges.h anyways */
96 {SE_NETWORK_LOGON, "SeNetworkLogonRight", "Access this computer from network", { 0x0, 0x0 }},
97 {SE_INTERACTIVE_LOGON, "SeInteractiveLogonRight", "Log on locally", { 0x0, 0x0 }},
98 {SE_BATCH_LOGON, "SeBatchLogonRight", "Log on as a batch job", { 0x0, 0x0 }},
99 {SE_SERVICE_LOGON, "SeServiceLogonRight", "Log on as a service", { 0x0, 0x0 }},
100 #endif
101 {SE_MACHINE_ACCOUNT, "SeMachineAccountPrivilege", "Add machines to domain", { 0x0, 0x0006 }},
102 {SE_TAKE_OWNERSHIP, "SeTakeOwnershipPrivilege", "Take ownership of files or other objects",{ 0x0, 0x0009 }},
103 {SE_BACKUP, "SeBackupPrivilege", "Back up files and directories", { 0x0, 0x0011 }},
104 {SE_RESTORE, "SeRestorePrivilege", "Restore files and directories", { 0x0, 0x0012 }},
105 {SE_REMOTE_SHUTDOWN, "SeRemoteShutdownPrivilege", "Force shutdown from a remote system", { 0x0, 0x0018 }},
107 {SE_PRINT_OPERATOR, "SePrintOperatorPrivilege", "Manage printers", { 0x0, 0x1001 }},
108 {SE_ADD_USERS, "SeAddUsersPrivilege", "Add users and groups to the domain", { 0x0, 0x1002 }},
109 {SE_DISK_OPERATOR, "SeDiskOperatorPrivilege", "Manage disk shares", { 0x0, 0x1003 }},
111 {SE_END, "", "", { 0x0, 0x0 }}
114 /***************************************************************************
115 copy an SE_PRIV structure
116 ****************************************************************************/
118 BOOL se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
120 if ( !dst || !src )
121 return False;
123 memcpy( dst, src, sizeof(SE_PRIV) );
125 return True;
128 /***************************************************************************
129 put all privileges into a mask
130 ****************************************************************************/
132 BOOL se_priv_put_all_privileges(SE_PRIV *mask)
134 int i;
135 uint32 num_privs = count_all_privileges();
137 if (!se_priv_copy(mask, &se_priv_none)) {
138 return False;
140 for ( i=0; i<num_privs; i++ ) {
141 se_priv_add(mask, &privs[i].se_priv);
143 return True;
146 /***************************************************************************
147 combine 2 SE_PRIV structures and store the resulting set in mew_mask
148 ****************************************************************************/
150 void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
152 int i;
154 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
155 mask->mask[i] |= addpriv->mask[i];
159 /***************************************************************************
160 remove one SE_PRIV sytucture from another and store the resulting set
161 in mew_mask
162 ****************************************************************************/
164 void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
166 int i;
168 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
169 mask->mask[i] &= ~removepriv->mask[i];
173 /***************************************************************************
174 invert a given SE_PRIV and store the set in new_mask
175 ****************************************************************************/
177 static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
179 SE_PRIV allprivs;
181 se_priv_copy( &allprivs, &se_priv_all );
182 se_priv_remove( &allprivs, mask );
183 se_priv_copy( new_mask, &allprivs );
186 /***************************************************************************
187 check if 2 SE_PRIV structure are equal
188 ****************************************************************************/
190 BOOL se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
192 return ( memcmp(mask1, mask2, sizeof(SE_PRIV)) == 0 );
195 /***************************************************************************
196 check if a SE_PRIV has any assigned privileges
197 ****************************************************************************/
199 static BOOL se_priv_empty( const SE_PRIV *mask )
201 SE_PRIV p1;
202 int i;
204 se_priv_copy( &p1, mask );
206 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
207 p1.mask[i] &= se_priv_all.mask[i];
210 return se_priv_equal( &p1, &se_priv_none );
213 /*********************************************************************
214 Lookup the SE_PRIV value for a privilege name
215 *********************************************************************/
217 BOOL se_priv_from_name( const char *name, SE_PRIV *mask )
219 int i;
221 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
222 if ( strequal( privs[i].name, name ) ) {
223 se_priv_copy( mask, &privs[i].se_priv );
224 return True;
228 return False;
231 /***************************************************************************
232 dump an SE_PRIV structure to the log files
233 ****************************************************************************/
235 void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
237 int i;
239 DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV "));
241 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
242 DEBUGADDC( dbg_cl, dbg_lvl,(" 0x%x", mask->mask[i] ));
245 DEBUGADDC( dbg_cl, dbg_lvl, ("\n"));
248 /****************************************************************************
249 check if the privilege is in the privilege list
250 ****************************************************************************/
252 BOOL is_privilege_assigned(const SE_PRIV *privileges,
253 const SE_PRIV *check)
255 SE_PRIV p1, p2;
257 if ( !privileges || !check )
258 return False;
260 /* everyone has privileges if you aren't checking for any */
262 if ( se_priv_empty( check ) ) {
263 DEBUG(1,("is_privilege_assigned: no privileges in check_mask!\n"));
264 return True;
267 se_priv_copy( &p1, check );
269 /* invert the SE_PRIV we want to check for and remove that from the
270 original set. If we are left with the SE_PRIV we are checking
271 for then return True */
273 se_priv_invert( &p1, check );
274 se_priv_copy( &p2, privileges );
275 se_priv_remove( &p2, &p1 );
277 return se_priv_equal( &p2, check );
280 /****************************************************************************
281 check if the privilege is in the privilege list
282 ****************************************************************************/
284 static BOOL is_any_privilege_assigned( SE_PRIV *privileges, const SE_PRIV *check )
286 SE_PRIV p1, p2;
288 if ( !privileges || !check )
289 return False;
291 /* everyone has privileges if you aren't checking for any */
293 if ( se_priv_empty( check ) ) {
294 DEBUG(1,("is_any_privilege_assigned: no privileges in check_mask!\n"));
295 return True;
298 se_priv_copy( &p1, check );
300 /* invert the SE_PRIV we want to check for and remove that from the
301 original set. If we are left with the SE_PRIV we are checking
302 for then return True */
304 se_priv_invert( &p1, check );
305 se_priv_copy( &p2, privileges );
306 se_priv_remove( &p2, &p1 );
308 /* see if we have any bits left */
310 return !se_priv_empty( &p2 );
313 /*********************************************************************
314 Generate the LUID_ATTR structure based on a bitmask
315 *********************************************************************/
317 const char* get_privilege_dispname( const char *name )
319 int i;
321 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
323 if ( strequal( privs[i].name, name ) ) {
324 return privs[i].description;
328 return NULL;
331 /****************************************************************************
332 initialise a privilege list and set the talloc context
333 ****************************************************************************/
335 /****************************************************************************
336 Does the user have the specified privilege ? We only deal with one privilege
337 at a time here.
338 *****************************************************************************/
340 BOOL user_has_privileges(const NT_USER_TOKEN *token, const SE_PRIV *privilege)
342 if ( !token )
343 return False;
345 return is_privilege_assigned( &token->privileges, privilege );
348 /****************************************************************************
349 Does the user have any of the specified privileges ? We only deal with one privilege
350 at a time here.
351 *****************************************************************************/
353 BOOL user_has_any_privilege(NT_USER_TOKEN *token, const SE_PRIV *privilege)
355 if ( !token )
356 return False;
358 return is_any_privilege_assigned( &token->privileges, privilege );
361 /*******************************************************************
362 return the number of elements in the privlege array
363 *******************************************************************/
365 int count_all_privileges( void )
368 * The -1 is due to the weird SE_END record...
370 return (sizeof(privs) / sizeof(privs[0])) - 1;
374 /*********************************************************************
375 Generate the LUID_ATTR structure based on a bitmask
376 The assumption here is that the privilege has already been validated
377 so we are guaranteed to find it in the list.
378 *********************************************************************/
380 LUID_ATTR get_privilege_luid( SE_PRIV *mask )
382 LUID_ATTR priv_luid;
383 int i;
385 ZERO_STRUCT( priv_luid );
387 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
389 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
390 priv_luid.luid = privs[i].luid;
391 break;
395 return priv_luid;
398 /****************************************************************************
399 Convert a LUID to a named string
400 ****************************************************************************/
402 const char *luid_to_privilege_name(const LUID *set)
404 int i;
406 if (set->high != 0)
407 return NULL;
409 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
410 if ( set->low == privs[i].luid.low ) {
411 return privs[i].name;
415 return NULL;
419 /****************************************************************************
420 add a privilege to a privilege array
421 ****************************************************************************/
423 static BOOL privilege_set_add(PRIVILEGE_SET *priv_set, LUID_ATTR set)
425 LUID_ATTR *new_set;
427 /* we can allocate memory to add the new privilege */
429 new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
430 if ( !new_set ) {
431 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
432 return False;
435 new_set[priv_set->count].luid.high = set.luid.high;
436 new_set[priv_set->count].luid.low = set.luid.low;
437 new_set[priv_set->count].attr = set.attr;
439 priv_set->count++;
440 priv_set->set = new_set;
442 return True;
445 /*******************************************************************
446 *******************************************************************/
448 BOOL se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
450 int i;
451 uint32 num_privs = count_all_privileges();
452 LUID_ATTR luid;
454 luid.attr = 0;
455 luid.luid.high = 0;
457 for ( i=0; i<num_privs; i++ ) {
458 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
459 continue;
461 luid.luid = privs[i].luid;
463 if ( !privilege_set_add( set, luid ) )
464 return False;
467 return True;
470 /*******************************************************************
471 *******************************************************************/
473 static BOOL luid_to_se_priv( LUID *luid, SE_PRIV *mask )
475 int i;
476 uint32 num_privs = count_all_privileges();
478 for ( i=0; i<num_privs; i++ ) {
479 if ( luid->low == privs[i].luid.low ) {
480 se_priv_copy( mask, &privs[i].se_priv );
481 return True;
485 return False;
488 /*******************************************************************
489 *******************************************************************/
491 BOOL privilege_set_to_se_priv( SE_PRIV *mask, PRIVILEGE_SET *privset )
493 int i;
495 ZERO_STRUCTP( mask );
497 for ( i=0; i<privset->count; i++ ) {
498 SE_PRIV r;
500 /* sanity check for invalid privilege. we really
501 only care about the low 32 bits */
503 if ( privset->set[i].luid.high != 0 )
504 return False;
506 if ( luid_to_se_priv( &privset->set[i].luid, &r ) )
507 se_priv_add( mask, &r );
510 return True;