962b5e8514d0ac686e67bd6508fc06d90e885efe
[abartlet/samba.git/.git] / source3 / lib / privileges_basic.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          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
35 const SE_PRIV se_priv_all         = SE_ALL_PRIVS;
36 static 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
41 const SE_PRIV se_priv_none       = SE_NONE;
42 const SE_PRIV se_machine_account = SE_MACHINE_ACCOUNT;
43 const SE_PRIV se_print_operator  = SE_PRINT_OPERATOR;
44 const SE_PRIV se_add_users       = SE_ADD_USERS;
45 const SE_PRIV se_disk_operators  = SE_DISK_OPERATOR;
46 const SE_PRIV se_remote_shutdown = SE_REMOTE_SHUTDOWN;
47 const SE_PRIV se_restore         = SE_RESTORE;
48 const SE_PRIV se_take_ownership  = SE_TAKE_OWNERSHIP;
49
50 /********************************************************************
51  This is a list of privileges reported by a WIndows 2000 SP4 AD DC
52  just for reference purposes (and I know the LUID is not guaranteed
53  across reboots):
54
55             SeCreateTokenPrivilege  Create a token object ( 0x0, 0x2 )
56      SeAssignPrimaryTokenPrivilege  Replace a process level token ( 0x0, 0x3 )
57              SeLockMemoryPrivilege  Lock pages in memory ( 0x0, 0x4 )
58           SeIncreaseQuotaPrivilege  Increase quotas ( 0x0, 0x5 )
59          SeMachineAccountPrivilege  Add workstations to domain ( 0x0, 0x6 )
60                     SeTcbPrivilege  Act as part of the operating system ( 0x0, 0x7 )
61                SeSecurityPrivilege  Manage auditing and security log ( 0x0, 0x8 )
62           SeTakeOwnershipPrivilege  Take ownership of files or other objects ( 0x0, 0x9 )
63              SeLoadDriverPrivilege  Load and unload device drivers ( 0x0, 0xa )
64           SeSystemProfilePrivilege  Profile system performance ( 0x0, 0xb )
65              SeSystemtimePrivilege  Change the system time ( 0x0, 0xc )
66    SeProfileSingleProcessPrivilege  Profile single process ( 0x0, 0xd )
67    SeIncreaseBasePriorityPrivilege  Increase scheduling priority ( 0x0, 0xe )
68          SeCreatePagefilePrivilege  Create a pagefile ( 0x0, 0xf )
69         SeCreatePermanentPrivilege  Create permanent shared objects ( 0x0, 0x10 )
70                  SeBackupPrivilege  Back up files and directories ( 0x0, 0x11 )
71                 SeRestorePrivilege  Restore files and directories ( 0x0, 0x12 )
72                SeShutdownPrivilege  Shut down the system ( 0x0, 0x13 )
73                   SeDebugPrivilege  Debug programs ( 0x0, 0x14 )
74                   SeAuditPrivilege  Generate security audits ( 0x0, 0x15 )
75       SeSystemEnvironmentPrivilege  Modify firmware environment values ( 0x0, 0x16 )
76            SeChangeNotifyPrivilege  Bypass traverse checking ( 0x0, 0x17 )
77          SeRemoteShutdownPrivilege  Force shutdown from a remote system ( 0x0, 0x18 )
78                  SeUndockPrivilege  Remove computer from docking station ( 0x0, 0x19 )
79               SeSyncAgentPrivilege  Synchronize directory service data ( 0x0, 0x1a )
80        SeEnableDelegationPrivilege  Enable computer and user accounts to be trusted for delegation ( 0x0, 0x1b )
81            SeManageVolumePrivilege  Perform volume maintenance tasks ( 0x0, 0x1c )
82             SeImpersonatePrivilege  Impersonate a client after authentication ( 0x0, 0x1d )
83            SeCreateGlobalPrivilege  Create global objects ( 0x0, 0x1e )
84
85  ********************************************************************/
86
87 /* we have to define the LUID here due to a horrible check by printmig.exe
88    that requires the SeBackupPrivilege match what is in Windows.  So match
89    those that we implement and start Samba privileges at 0x1001 */
90
91 PRIVS privs[] = {
92 #if 0   /* usrmgr will display these twice if you include them.  We don't
93            use them but we'll keep the bitmasks reserved in privileges.h anyways */
94
95         {SE_NETWORK_LOGON,      "SeNetworkLogonRight",          "Access this computer from network",       { 0x0, 0x0 }},
96         {SE_INTERACTIVE_LOGON,  "SeInteractiveLogonRight",      "Log on locally",                          { 0x0, 0x0 }},
97         {SE_BATCH_LOGON,        "SeBatchLogonRight",            "Log on as a batch job",                   { 0x0, 0x0 }},
98         {SE_SERVICE_LOGON,      "SeServiceLogonRight",          "Log on as a service",                     { 0x0, 0x0 }},
99 #endif
100         {SE_MACHINE_ACCOUNT,    "SeMachineAccountPrivilege",    "Add machines to domain",                  { 0x0006,    0x0 }},
101         {SE_TAKE_OWNERSHIP,     "SeTakeOwnershipPrivilege",     "Take ownership of files or other objects",{ 0x0009,    0x0 }},
102         {SE_BACKUP,             "SeBackupPrivilege",            "Back up files and directories",           { 0x0011,    0x0 }},
103         {SE_RESTORE,            "SeRestorePrivilege",           "Restore files and directories",           { 0x0012,    0x0 }},
104         {SE_REMOTE_SHUTDOWN,    "SeRemoteShutdownPrivilege",    "Force shutdown from a remote system",     { 0x0018,    0x0 }},
105
106         {SE_PRINT_OPERATOR,     "SePrintOperatorPrivilege",     "Manage printers",                         { 0x1001,    0x0 }},
107         {SE_ADD_USERS,          "SeAddUsersPrivilege",          "Add users and groups to the domain",      { 0x1002,    0x0 }},
108         {SE_DISK_OPERATOR,      "SeDiskOperatorPrivilege",      "Manage disk shares",                      { 0x1003,    0x0 }},
109
110         {SE_END, "", "", { 0x0, 0x0 }}
111 };
112
113 /***************************************************************************
114  copy an SE_PRIV structure
115 ****************************************************************************/
116
117 bool se_priv_copy( SE_PRIV *dst, const SE_PRIV *src )
118 {
119         if ( !dst || !src )
120                 return False;
121
122         memcpy( dst, src, sizeof(SE_PRIV) );
123
124         return True;
125 }
126
127 /***************************************************************************
128  put all privileges into a mask
129 ****************************************************************************/
130
131 bool se_priv_put_all_privileges(SE_PRIV *mask)
132 {
133         int i;
134         uint32 num_privs = count_all_privileges();
135
136         if (!se_priv_copy(mask, &se_priv_none)) {
137                 return False;
138         }
139         for ( i=0; i<num_privs; i++ ) {
140                 se_priv_add(mask, &privs[i].se_priv);
141         }
142         return True;
143 }
144
145 /***************************************************************************
146  combine 2 SE_PRIV structures and store the resulting set in mew_mask
147 ****************************************************************************/
148
149 void se_priv_add( SE_PRIV *mask, const SE_PRIV *addpriv )
150 {
151         *mask |= *addpriv;
152 }
153
154 /***************************************************************************
155  remove one SE_PRIV sytucture from another and store the resulting set
156  in mew_mask
157 ****************************************************************************/
158
159 void se_priv_remove( SE_PRIV *mask, const SE_PRIV *removepriv )
160 {
161         *mask &= ~*removepriv;
162 }
163
164 /***************************************************************************
165  invert a given SE_PRIV and store the set in new_mask
166 ****************************************************************************/
167
168 static void se_priv_invert( SE_PRIV *new_mask, const SE_PRIV *mask )
169 {
170         SE_PRIV allprivs;
171
172         se_priv_copy( &allprivs, &se_priv_all );
173         se_priv_remove( &allprivs, mask );
174         se_priv_copy( new_mask, &allprivs );
175 }
176
177 /***************************************************************************
178  check if 2 SE_PRIV structure are equal
179 ****************************************************************************/
180
181 bool se_priv_equal( const SE_PRIV *mask1, const SE_PRIV *mask2 )
182 {
183         return *mask1 == *mask2;
184 }
185
186 /***************************************************************************
187  check if a SE_PRIV has any assigned privileges
188 ****************************************************************************/
189
190 static bool se_priv_empty( const SE_PRIV *mask )
191 {
192         SE_PRIV p1;
193
194         se_priv_copy( &p1, mask );
195
196         p1 &= se_priv_all;
197
198         return se_priv_equal( &p1, &se_priv_none );
199 }
200
201 /*********************************************************************
202  Lookup the SE_PRIV value for a privilege name
203 *********************************************************************/
204
205 bool se_priv_from_name( const char *name, SE_PRIV *mask )
206 {
207         int i;
208
209         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
210                 if ( strequal( privs[i].name, name ) ) {
211                         se_priv_copy( mask, &privs[i].se_priv );
212                         return True;
213                 }
214         }
215
216         return False;
217 }
218
219 /***************************************************************************
220  dump an SE_PRIV structure to the log files
221 ****************************************************************************/
222
223 void dump_se_priv( int dbg_cl, int dbg_lvl, const SE_PRIV *mask )
224 {
225         DEBUGADDC( dbg_cl, dbg_lvl,("SE_PRIV 0x%llx\n", (unsigned long long)*mask));
226 }
227
228 /****************************************************************************
229  check if the privilege is in the privilege list
230 ****************************************************************************/
231
232 bool is_privilege_assigned(const SE_PRIV *privileges,
233                            const SE_PRIV *check)
234 {
235         SE_PRIV p1, p2;
236
237         if ( !privileges || !check )
238                 return False;
239
240         /* everyone has privileges if you aren't checking for any */
241
242         if ( se_priv_empty( check ) ) {
243                 DEBUG(1,("is_privilege_assigned: no privileges in check_mask!\n"));
244                 return True;
245         }
246
247         se_priv_copy( &p1, check );
248
249         /* invert the SE_PRIV we want to check for and remove that from the
250            original set.  If we are left with the SE_PRIV we are checking
251            for then return True */
252
253         se_priv_invert( &p1, check );
254         se_priv_copy( &p2, privileges );
255         se_priv_remove( &p2, &p1 );
256
257         return se_priv_equal( &p2, check );
258 }
259
260 /****************************************************************************
261  check if the privilege is in the privilege list
262 ****************************************************************************/
263
264 static bool is_any_privilege_assigned( SE_PRIV *privileges, 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_any_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         /* see if we have any bits left */
289
290         return !se_priv_empty( &p2 );
291 }
292
293 /*********************************************************************
294  Generate the struct lsa_LUIDAttribute structure based on a bitmask
295 *********************************************************************/
296
297 const char* get_privilege_dispname( const char *name )
298 {
299         int i;
300
301         if (!name) {
302                 return NULL;
303         }
304
305         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
306
307                 if ( strequal( privs[i].name, name ) ) {
308                         return privs[i].description;
309                 }
310         }
311
312         return NULL;
313 }
314
315 /****************************************************************************
316  initialise a privilege list and set the talloc context
317  ****************************************************************************/
318
319 /****************************************************************************
320  Does the user have the specified privilege ?  We only deal with one privilege
321  at a time here.
322 *****************************************************************************/
323
324 bool user_has_privileges(const NT_USER_TOKEN *token, const SE_PRIV *privilege)
325 {
326         if ( !token )
327                 return False;
328
329         return is_privilege_assigned( &token->privilege_mask, privilege );
330 }
331
332 /****************************************************************************
333  Does the user have any of the specified privileges ?  We only deal with one privilege
334  at a time here.
335 *****************************************************************************/
336
337 bool user_has_any_privilege(NT_USER_TOKEN *token, const SE_PRIV *privilege)
338 {
339         if ( !token )
340                 return False;
341
342         return is_any_privilege_assigned( &token->privilege_mask, privilege );
343 }
344
345 /*******************************************************************
346  return the number of elements in the privlege array
347 *******************************************************************/
348
349 int count_all_privileges( void )
350 {
351         /*
352          * The -1 is due to the weird SE_END record...
353          */
354         return (sizeof(privs) / sizeof(privs[0])) - 1;
355 }
356
357
358 /*********************************************************************
359  Generate the struct lsa_LUIDAttribute structure based on a bitmask
360  The assumption here is that the privilege has already been validated
361  so we are guaranteed to find it in the list.
362 *********************************************************************/
363
364 struct lsa_LUIDAttribute get_privilege_luid( SE_PRIV *mask )
365 {
366         struct lsa_LUIDAttribute priv_luid;
367         int i;
368
369         ZERO_STRUCT( priv_luid );
370
371         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
372
373                 if ( se_priv_equal( &privs[i].se_priv, mask ) ) {
374                         priv_luid.luid = privs[i].luid;
375                         break;
376                 }
377         }
378
379         return priv_luid;
380 }
381
382 /****************************************************************************
383  Convert a LUID to a named string
384 ****************************************************************************/
385
386 const char *luid_to_privilege_name(const struct lsa_LUID *set)
387 {
388         int i;
389
390         if (set->high != 0)
391                 return NULL;
392
393         for ( i=0; !se_priv_equal(&privs[i].se_priv, &se_priv_end); i++ ) {
394                 if ( set->low == privs[i].luid.low ) {
395                         return privs[i].name;
396                 }
397         }
398
399         return NULL;
400 }
401
402
403 /****************************************************************************
404  add a privilege to a privilege array
405  ****************************************************************************/
406
407 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
408 {
409         struct lsa_LUIDAttribute *new_set;
410
411         /* we can allocate memory to add the new privilege */
412
413         new_set = TALLOC_REALLOC_ARRAY(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
414         if ( !new_set ) {
415                 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
416                 return False;
417         }
418
419         new_set[priv_set->count].luid.high = set.luid.high;
420         new_set[priv_set->count].luid.low = set.luid.low;
421         new_set[priv_set->count].attribute = set.attribute;
422
423         priv_set->count++;
424         priv_set->set = new_set;
425
426         return True;
427 }
428
429 /*******************************************************************
430 *******************************************************************/
431
432 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, SE_PRIV *mask )
433 {
434         int i;
435         uint32 num_privs = count_all_privileges();
436         struct lsa_LUIDAttribute luid;
437
438         luid.attribute = 0;
439         luid.luid.high = 0;
440
441         for ( i=0; i<num_privs; i++ ) {
442                 if ( !is_privilege_assigned(mask, &privs[i].se_priv) )
443                         continue;
444
445                 luid.luid = privs[i].luid;
446
447                 if ( !privilege_set_add( set, luid ) )
448                         return False;
449         }
450
451         return True;
452 }
453
454 /*******************************************************************
455 *******************************************************************/
456
457 static bool luid_to_se_priv( struct lsa_LUID *luid, SE_PRIV *mask )
458 {
459         int i;
460         uint32 num_privs = count_all_privileges();
461
462         for ( i=0; i<num_privs; i++ ) {
463                 if ( luid->low == privs[i].luid.low ) {
464                         se_priv_copy( mask, &privs[i].se_priv );
465                         return True;
466                 }
467         }
468
469         return False;
470 }
471
472 /*******************************************************************
473 *******************************************************************/
474
475 bool privilege_set_to_se_priv( SE_PRIV *mask, struct lsa_PrivilegeSet *privset )
476 {
477         int i;
478
479         ZERO_STRUCTP( mask );
480
481         for ( i=0; i<privset->count; i++ ) {
482                 SE_PRIV r;
483
484                 /* sanity check for invalid privilege.  we really
485                    only care about the low 32 bits */
486
487                 if ( privset->set[i].luid.high != 0 )
488                         return False;
489
490                 if ( luid_to_se_priv( &privset->set[i].luid, &r ) )
491                         se_priv_add( mask, &r );
492         }
493
494         return True;
495 }
496