source: branches/samba-3.5.x/source3/lib/privileges_basic.c

Last change on this file was 596, checked in by Herwig Bauernfeind, 14 years ago

Samba 3.5: Update trunk to 3.5.8

File size: 17.2 KB
Line 
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
8
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 3 of the License, or
12 (at your option) any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23/*
24 * Basic privileges functions (mask-operations and conversion
25 * functions between the different formats (se_priv, privset, luid)
26 * moved here * from lib/privileges.c to minimize linker deps.
27 *
28 * generally SID- and LUID-related code is left in lib/privileges.c
29 *
30 * some extra functions to hide privs array from lib/privileges.c
31 */
32
33#include "includes.h"
34
35const SE_PRIV se_priv_all = SE_ALL_PRIVS;
36static const SE_PRIV se_priv_end = SE_END;
37
38/* Define variables for all privileges so we can use the
39 SE_PRIV* in the various se_priv_XXX() functions */
40
41const SE_PRIV se_priv_none = SE_NONE;
42const SE_PRIV se_machine_account = SE_MACHINE_ACCOUNT;
43const SE_PRIV se_print_operator = SE_PRINT_OPERATOR;
44const SE_PRIV se_add_users = SE_ADD_USERS;
45const SE_PRIV se_disk_operators = SE_DISK_OPERATOR;
46const SE_PRIV se_remote_shutdown = SE_REMOTE_SHUTDOWN;
47const SE_PRIV se_restore = SE_RESTORE;
48const SE_PRIV se_take_ownership = SE_TAKE_OWNERSHIP;
49const SE_PRIV se_security = SE_SECURITY;
50
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):
55
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 )
85
86 ********************************************************************/
87
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 */
91
92PRIVS 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 */
95
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_SECURITY, "SeSecurityPrivilege", "Manage auditing and security log", { 0x0, 0x0008 }},
103 {SE_TAKE_OWNERSHIP, "SeTakeOwnershipPrivilege", "Take ownership of files or other objects",{ 0x0, 0x0009 }},
104 {SE_BACKUP, "SeBackupPrivilege", "Back up files and directories", { 0x0, 0x0011 }},
105 {SE_RESTORE, "SeRestorePrivilege", "Restore files and directories", { 0x0, 0x0012 }},
106 {SE_REMOTE_SHUTDOWN, "SeRemoteShutdownPrivilege", "Force shutdown from a remote system", { 0x0, 0x0018 }},
107
108 {SE_PRINT_OPERATOR, "SePrintOperatorPrivilege", "Manage printers", { 0x0, 0x1001 }},
109 {SE_ADD_USERS, "SeAddUsersPrivilege", "Add users and groups to the domain", { 0x0, 0x1002 }},
110 {SE_DISK_OPERATOR, "SeDiskOperatorPrivilege", "Manage disk shares", { 0x0, 0x1003 }},
111
112
113 {SE_END, "", "", { 0x0, 0x0 }}
114};
115
116/***************************************************************************
117 copy an SE_PRIV structure
118****************************************************************************/
119
120bool se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
121{
122 if ( !dst || !src )
123 return False;
124
125 memcpy( dst, src, sizeof(SE_PRIV) );
126
127 return True;
128}
129
130/***************************************************************************
131 put all privileges into a mask
132****************************************************************************/
133
134bool se_priv_put_all_privileges(SE_PRIV *mask)
135{
136 int i;
137 uint32 num_privs = count_all_privileges();
138
139 if (!se_priv_copy(mask, &se_priv_none)) {
140 return False;
141 }
142 for ( i=0; i<num_privs; i++ ) {
143 se_priv_add(mask, &privs[i].se_priv);
144 }
145 return True;
146}
147
148/***************************************************************************
149 combine 2 SE_PRIV structures and store the resulting set in mew_mask
150****************************************************************************/
151
152void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
153{
154 int i;
155
156 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
157 mask->mask[i] |= addpriv->mask[i];
158 }
159}
160
161/***************************************************************************
162 remove one SE_PRIV sytucture from another and store the resulting set
163 in mew_mask
164****************************************************************************/
165
166void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
167{
168 int i;
169
170 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
171 mask->mask[i] &= ~removepriv->mask[i];
172 }
173}
174
175/***************************************************************************
176 invert a given SE_PRIV and store the set in new_mask
177****************************************************************************/
178
179static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
180{
181 SE_PRIV allprivs;
182
183 se_priv_copy( &allprivs, &se_priv_all );
184 se_priv_remove( &allprivs, mask );
185 se_priv_copy( new_mask, &allprivs );
186}
187
188/***************************************************************************
189 check if 2 SE_PRIV structure are equal
190****************************************************************************/
191
192bool se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
193{
194 return ( memcmp(mask1, mask2, sizeof(SE_PRIV)) == 0 );
195}
196
197/***************************************************************************
198 check if 2 LUID's are equal.
199****************************************************************************/
200
201static bool luid_equal( const LUID *luid1, const LUID *luid2 )
202{
203 return ( luid1->low == luid2->low && luid1->high == luid2->high);
204}
205
206/***************************************************************************
207 check if a SE_PRIV has any assigned privileges
208****************************************************************************/
209
210static bool se_priv_empty( const SE_PRIV *mask )
211{
212 SE_PRIV p1;
213 int i;
214
215 se_priv_copy( &p1, mask );
216
217 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
218 p1.mask[i] &= se_priv_all.mask[i];
219 }
220
221 return se_priv_equal( &p1, &se_priv_none );
222}
223
224/*********************************************************************
225 Lookup the SE_PRIV value for a privilege name
226*********************************************************************/
227
228bool se_priv_from_name( const char *name, SE_PRIV *mask )
229{
230 int i;
231
232 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
233 if ( strequal( privs[i].name, name ) ) {
234 se_priv_copy( mask, &privs[i].se_priv );
235 return True;
236 }
237 }
238
239 return False;
240}
241
242/***************************************************************************
243 dump an SE_PRIV structure to the log files
244****************************************************************************/
245
246void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
247{
248 int i;
249
250 DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV "));
251
252 for ( i=0; i<SE_PRIV_MASKSIZE; i++ ) {
253 DEBUGADDC( dbg_cl, dbg_lvl,(" 0x%x", mask->mask[i] ));
254 }
255
256 DEBUGADDC( dbg_cl, dbg_lvl, ("\n"));
257}
258
259/****************************************************************************
260 check if the privilege is in the privilege list
261****************************************************************************/
262
263bool is_privilege_assigned(const SE_PRIV *privileges,
264 const SE_PRIV *check)
265{
266 SE_PRIV p1, p2;
267
268 if ( !privileges || !check )
269 return False;
270
271 /* everyone has privileges if you aren't checking for any */
272
273 if ( se_priv_empty( check ) ) {
274 DEBUG(1,("is_privilege_assigned: no privileges in check_mask!\n"));
275 return True;
276 }
277
278 se_priv_copy( &p1, check );
279
280 /* invert the SE_PRIV we want to check for and remove that from the
281 original set. If we are left with the SE_PRIV we are checking
282 for then return True */
283
284 se_priv_invert( &p1, check );
285 se_priv_copy( &p2, privileges );
286 se_priv_remove( &p2, &p1 );
287
288 return se_priv_equal( &p2, check );
289}
290
291/****************************************************************************
292 check if the privilege is in the privilege list
293****************************************************************************/
294
295static bool is_any_privilege_assigned( SE_PRIV *privileges, const SE_PRIV *check )
296{
297 SE_PRIV p1, p2;
298
299 if ( !privileges || !check )
300 return False;
301
302 /* everyone has privileges if you aren't checking for any */
303
304 if ( se_priv_empty( check ) ) {
305 DEBUG(1,("is_any_privilege_assigned: no privileges in check_mask!\n"));
306 return True;
307 }
308
309 se_priv_copy( &p1, check );
310
311 /* invert the SE_PRIV we want to check for and remove that from the
312 original set. If we are left with the SE_PRIV we are checking
313 for then return True */
314
315 se_priv_invert( &p1, check );
316 se_priv_copy( &p2, privileges );
317 se_priv_remove( &p2, &p1 );
318
319 /* see if we have any bits left */
320
321 return !se_priv_empty( &p2 );
322}
323
324/*********************************************************************
325 Generate the LUID_ATTR structure based on a bitmask
326*********************************************************************/
327
328const char* get_privilege_dispname( const char *name )
329{
330 int i;
331
332 if (!name) {
333 return NULL;
334 }
335
336 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
337
338 if ( strequal( privs[i].name, name ) ) {
339 return privs[i].description;
340 }
341 }
342
343 return NULL;
344}
345
346/****************************************************************************
347 initialise a privilege list and set the talloc context
348 ****************************************************************************/
349
350/****************************************************************************
351 Does the user have the specified privilege ? We only deal with one privilege
352 at a time here.
353*****************************************************************************/
354
355bool user_has_privileges(const NT_USER_TOKEN *token, const SE_PRIV *privilege)
356{
357 if ( !token )
358 return False;
359
360 return is_privilege_assigned( &token->privileges, privilege );
361}
362
363/****************************************************************************
364 Does the user have any of the specified privileges ? We only deal with one privilege
365 at a time here.
366*****************************************************************************/
367
368bool user_has_any_privilege(NT_USER_TOKEN *token, const SE_PRIV *privilege)
369{
370 if ( !token )
371 return False;
372
373 return is_any_privilege_assigned( &token->privileges, privilege );
374}
375
376/*******************************************************************
377 return the number of elements in the privlege array
378*******************************************************************/
379
380int count_all_privileges( void )
381{
382 /*
383 * The -1 is due to the weird SE_END record...
384 */
385 return (sizeof(privs) / sizeof(privs[0])) - 1;
386}
387
388
389/*********************************************************************
390 Generate the LUID_ATTR structure based on a bitmask
391 The assumption here is that the privilege has already been validated
392 so we are guaranteed to find it in the list.
393*********************************************************************/
394
395LUID_ATTR get_privilege_luid( SE_PRIV *mask )
396{
397 LUID_ATTR priv_luid;
398 int i;
399
400 ZERO_STRUCT( priv_luid );
401
402 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
403
404 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
405 priv_luid.luid = privs[i].luid;
406 break;
407 }
408 }
409
410 return priv_luid;
411}
412
413/****************************************************************************
414 Convert a LUID to a named string
415****************************************************************************/
416
417const char *luid_to_privilege_name(const LUID *set)
418{
419 int i;
420
421 for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
422 if (luid_equal(set, &privs[i].luid)) {
423 return privs[i].name;
424 }
425 }
426
427 return NULL;
428}
429
430
431/****************************************************************************
432 add a privilege to a privilege array
433 ****************************************************************************/
434
435static bool privilege_set_add(PRIVILEGE_SET *priv_set, LUID_ATTR set)
436{
437 LUID_ATTR *new_set;
438
439 /* we can allocate memory to add the new privilege */
440
441 new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, LUID_ATTR, priv_set->count + 1);
442 if ( !new_set ) {
443 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
444 return False;
445 }
446
447 new_set[priv_set->count].luid.high = set.luid.high;
448 new_set[priv_set->count].luid.low = set.luid.low;
449 new_set[priv_set->count].attr = set.attr;
450
451 priv_set->count++;
452 priv_set->set = new_set;
453
454 return True;
455}
456
457/*******************************************************************
458*******************************************************************/
459
460bool se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
461{
462 int i;
463 uint32 num_privs = count_all_privileges();
464 LUID_ATTR luid;
465
466 luid.attr = 0;
467 luid.luid.high = 0;
468
469 for ( i=0; i<num_privs; i++ ) {
470 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
471 continue;
472
473 luid.luid = privs[i].luid;
474
475 if ( !privilege_set_add( set, luid ) )
476 return False;
477 }
478
479 return True;
480}
481
482/*******************************************************************
483*******************************************************************/
484
485static bool luid_to_se_priv( struct lsa_LUID *luid, SE_PRIV *mask )
486{
487 int i;
488 uint32 num_privs = count_all_privileges();
489 LUID local_luid;
490
491 local_luid.low = luid->low;
492 local_luid.high = luid->high;
493
494 for ( i=0; i<num_privs; i++ ) {
495 if (luid_equal(&local_luid, &privs[i].luid)) {
496 se_priv_copy( mask, &privs[i].se_priv );
497 return True;
498 }
499 }
500
501 return False;
502}
503
504/*******************************************************************
505*******************************************************************/
506
507bool privilege_set_to_se_priv( SE_PRIV *mask, struct lsa_PrivilegeSet *privset )
508{
509 int i;
510
511 ZERO_STRUCTP( mask );
512
513 for ( i=0; i<privset->count; i++ ) {
514 SE_PRIV r;
515
516 if ( luid_to_se_priv( &privset->set[i].luid, &r ) )
517 se_priv_add( mask, &r );
518 }
519
520 return True;
521}
522
Note: See TracBrowser for help on using the repository browser.