libcli/security Add debug class to security_token_debug() et al
[samba.git] / libcli / security / privileges.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    Copyright (C) Andrew Bartlett                2010
9    Copyright (C) Andrew Tridgell                2004
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 /*
26  * Basic privileges functions (mask-operations and conversion
27  * functions between the different formats (se_priv, privset, luid)
28  * moved here * from lib/privileges.c to minimize linker deps.
29  *
30  * generally SID- and LUID-related code is left in lib/privileges.c
31  *
32  * some extra functions to hide privs array from lib/privileges.c
33  */
34
35 #include "includes.h"
36 #include "libcli/security/privileges.h"
37 #include "libcli/security/privileges_private.h"
38 #include "librpc/gen_ndr/security.h"
39
40 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
41 #undef strcasecmp
42
43 #define NUM_SHORT_LIST_PRIVS 8
44
45 static const struct {
46         enum sec_privilege luid;
47         uint64_t privilege_mask;
48         const char *name;
49         const char *description;
50 } privs[] = {
51
52         {SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_MACHINE_ACCOUNT_BIT,   "SeMachineAccountPrivilege", "Add machines to domain"},
53         {SEC_PRIV_TAKE_OWNERSHIP,  SEC_PRIV_TAKE_OWNERSHIP_BIT,    "SeTakeOwnershipPrivilege",    "Take ownership of files or other objects"},
54         {SEC_PRIV_BACKUP,          SEC_PRIV_BACKUP_BIT,            "SeBackupPrivilege",           "Back up files and directories"},
55         {SEC_PRIV_RESTORE,         SEC_PRIV_RESTORE_BIT,           "SeRestorePrivilege",          "Restore files and directories"},
56         {SEC_PRIV_REMOTE_SHUTDOWN, SEC_PRIV_REMOTE_SHUTDOWN_BIT,   "SeRemoteShutdownPrivilege", "Force shutdown from a remote system"},
57
58         {SEC_PRIV_PRINT_OPERATOR,  SEC_PRIV_PRINT_OPERATOR_BIT,  "SePrintOperatorPrivilege",    "Manage printers"},
59         {SEC_PRIV_ADD_USERS,       SEC_PRIV_ADD_USERS_BIT,       "SeAddUsersPrivilege",         "Add users and groups to the domain"},
60         {SEC_PRIV_DISK_OPERATOR,   SEC_PRIV_DISK_OPERATOR_BIT,   "SeDiskOperatorPrivilege",     "Manage disk shares"},
61
62         /* The list from here on is not displayed in the code from
63          * source3, and is after index NUM_SHORT_LIST_PRIVS for that
64          * reason */ 
65
66         {SEC_PRIV_SECURITY,
67          SEC_PRIV_SECURITY_BIT,
68          "SeSecurityPrivilege",
69         "System security"},
70
71         {SEC_PRIV_SYSTEMTIME,
72          SEC_PRIV_SYSTEMTIME_BIT,
73          "SeSystemtimePrivilege",
74         "Set the system clock"},
75
76         {SEC_PRIV_SHUTDOWN,
77          SEC_PRIV_SHUTDOWN_BIT,
78          "SeShutdownPrivilege",
79         "Shutdown the system"},
80
81         {SEC_PRIV_DEBUG,
82          SEC_PRIV_DEBUG_BIT,
83          "SeDebugPrivilege",
84         "Debug processes"},
85
86         {SEC_PRIV_SYSTEM_ENVIRONMENT,
87          SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
88          "SeSystemEnvironmentPrivilege",
89         "Modify system environment"},
90
91         {SEC_PRIV_SYSTEM_PROFILE,
92          SEC_PRIV_SYSTEM_PROFILE_BIT,
93          "SeSystemProfilePrivilege",
94         "Profile the system"},
95
96         {SEC_PRIV_PROFILE_SINGLE_PROCESS,
97          SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
98          "SeProfileSingleProcessPrivilege",
99         "Profile one process"},
100
101         {SEC_PRIV_INCREASE_BASE_PRIORITY,
102          SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
103          "SeIncreaseBasePriorityPrivilege",
104          "Increase base priority"},
105
106         {SEC_PRIV_LOAD_DRIVER,
107          SEC_PRIV_LOAD_DRIVER_BIT,
108          "SeLoadDriverPrivilege",
109         "Load drivers"},
110
111         {SEC_PRIV_CREATE_PAGEFILE,
112          SEC_PRIV_CREATE_PAGEFILE_BIT,
113          "SeCreatePagefilePrivilege",
114         "Create page files"},
115
116         {SEC_PRIV_INCREASE_QUOTA,
117          SEC_PRIV_INCREASE_QUOTA_BIT,
118          "SeIncreaseQuotaPrivilege",
119         "Increase quota"},
120
121         {SEC_PRIV_CHANGE_NOTIFY,
122          SEC_PRIV_CHANGE_NOTIFY_BIT,
123          "SeChangeNotifyPrivilege",
124         "Register for change notify"},
125
126         {SEC_PRIV_UNDOCK,
127          SEC_PRIV_UNDOCK_BIT,
128          "SeUndockPrivilege",
129         "Undock devices"},
130
131         {SEC_PRIV_MANAGE_VOLUME,
132          SEC_PRIV_MANAGE_VOLUME_BIT,
133          "SeManageVolumePrivilege",
134         "Manage system volumes"},
135
136         {SEC_PRIV_IMPERSONATE,
137          SEC_PRIV_IMPERSONATE_BIT,
138          "SeImpersonatePrivilege",
139         "Impersonate users"},
140
141         {SEC_PRIV_CREATE_GLOBAL,
142          SEC_PRIV_CREATE_GLOBAL_BIT,
143          "SeCreateGlobalPrivilege",
144         "Create global"},
145
146         {SEC_PRIV_ENABLE_DELEGATION,
147          SEC_PRIV_ENABLE_DELEGATION_BIT,
148          "SeEnableDelegationPrivilege",
149         "Enable Delegation"},
150 };
151
152 /* These are rights, not privileges, and should not be confused.  The
153  * names are very similar, and they are quite similar in behaviour,
154  * but they are not to be enumerated as a system-wide list or have an
155  * LUID value */
156 static const struct {
157         uint32_t right_mask;
158         const char *name;
159         const char *description;
160 } rights[] = {
161         {LSA_POLICY_MODE_INTERACTIVE,
162          "SeInteractiveLogonRight",
163         "Interactive logon"},
164
165         {LSA_POLICY_MODE_NETWORK,
166          "SeNetworkLogonRight",
167         "Network logon"},
168
169         {LSA_POLICY_MODE_REMOTE_INTERACTIVE,
170          "SeRemoteInteractiveLogonRight",
171         "Remote Interactive logon"}
172 };
173
174 /*
175   return a privilege mask given a privilege id
176 */
177 uint64_t sec_privilege_mask(enum sec_privilege privilege)
178 {
179         int i;
180         for (i=0;i<ARRAY_SIZE(privs);i++) {
181                 if (privs[i].luid == privilege) {
182                         return privs[i].privilege_mask;
183                 }
184         }
185
186         return 0;
187 }
188
189 /***************************************************************************
190  put all valid privileges into a mask
191 ****************************************************************************/
192
193 void se_priv_put_all_privileges(uint64_t *privilege_mask)
194 {
195         int i;
196         uint32_t num_privs = ARRAY_SIZE(privs);
197
198         *privilege_mask = 0;
199         for ( i=0; i<num_privs; i++ ) {
200                 *privilege_mask |= privs[i].privilege_mask;
201         }
202 }
203
204 /*********************************************************************
205  Lookup the uint64_t bitmask value for a privilege name
206 *********************************************************************/
207
208 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
209 {
210         int i;
211         uint32_t num_privs = ARRAY_SIZE(privs);
212         for ( i=0; i<num_privs; i++ ) {
213                 if ( strequal( privs[i].name, name ) ) {
214                         *privilege_mask = privs[i].privilege_mask;
215                         return true;
216                 }
217         }
218
219         return false;
220 }
221
222 const char* get_privilege_dispname( const char *name )
223 {
224         int i;
225
226         uint32_t num_privs = ARRAY_SIZE(privs);
227
228         if (!name) {
229                 return NULL;
230         }
231
232         for ( i=0; i<num_privs; i++ ) {
233                 if ( strequal( privs[i].name, name ) ) {
234                         return privs[i].description;
235                 }
236         }
237
238         return NULL;
239 }
240
241 /*******************************************************************
242  return the number of elements in the 'short' privlege array (traditional source3 behaviour)
243 *******************************************************************/
244
245 int num_privileges_in_short_list( void )
246 {
247         return NUM_SHORT_LIST_PRIVS;
248 }
249
250 /****************************************************************************
251  add a privilege to a privilege array
252  ****************************************************************************/
253
254 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
255 {
256         struct lsa_LUIDAttribute *new_set;
257
258         /* we can allocate memory to add the new privilege */
259
260         new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
261         if ( !new_set ) {
262                 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
263                 return false;
264         }
265
266         new_set[priv_set->count].luid.high = set.luid.high;
267         new_set[priv_set->count].luid.low = set.luid.low;
268         new_set[priv_set->count].attribute = set.attribute;
269
270         priv_set->count++;
271         priv_set->set = new_set;
272
273         return true;
274 }
275
276 /*******************************************************************
277 *******************************************************************/
278
279 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
280 {
281         int i;
282         uint32_t num_privs = ARRAY_SIZE(privs);
283         struct lsa_LUIDAttribute luid;
284
285         luid.attribute = 0;
286         luid.luid.high = 0;
287
288         for ( i=0; i<num_privs; i++ ) {
289                 if ((privilege_mask & privs[i].privilege_mask) == 0)
290                         continue;
291
292                 luid.luid.high = 0;
293                 luid.luid.low = privs[i].luid;
294
295                 if ( !privilege_set_add( set, luid ) )
296                         return false;
297         }
298
299         return true;
300 }
301
302 /*******************************************************************
303 *******************************************************************/
304
305 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
306 {
307         int i;
308
309         ZERO_STRUCTP( privilege_mask );
310
311         for ( i=0; i<privset->count; i++ ) {
312                 uint64_t r;
313
314                 /* sanity check for invalid privilege.  we really
315                    only care about the low 32 bits */
316
317                 if ( privset->set[i].luid.high != 0 )
318                         return false;
319
320                 r = sec_privilege_mask(privset->set[i].luid.low);
321                 if (r) {
322                         *privilege_mask |= r;
323                 }
324         }
325
326         return true;
327 }
328
329 /*
330   map a privilege id to the wire string constant
331 */
332 const char *sec_privilege_name(enum sec_privilege privilege)
333 {
334         int i;
335         for (i=0;i<ARRAY_SIZE(privs);i++) {
336                 if (privs[i].luid == privilege) {
337                         return privs[i].name;
338                 }
339         }
340         return NULL;
341 }
342
343 /*
344   map a privilege id to a privilege display name. Return NULL if not found
345
346   TODO: this should use language mappings
347 */
348 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
349 {
350         int i;
351         for (i=0;i<ARRAY_SIZE(privs);i++) {
352                 if (privs[i].luid == privilege) {
353                         return privs[i].description;
354                 }
355         }
356         return NULL;
357 }
358
359 /*
360   map a privilege name to a privilege id. Return SEC_PRIV_INVALID if not found
361 */
362 enum sec_privilege sec_privilege_id(const char *name)
363 {
364         int i;
365         for (i=0;i<ARRAY_SIZE(privs);i++) {
366                 if (strcasecmp(privs[i].name, name) == 0) {
367                         return privs[i].luid;
368                 }
369         }
370         return SEC_PRIV_INVALID;
371 }
372
373 /*
374   map a 'right' name to it's bitmap value. Return 0 if not found
375 */
376 uint32_t sec_right_bit(const char *name)
377 {
378         int i;
379         for (i=0;i<ARRAY_SIZE(rights);i++) {
380                 if (strcasecmp(rights[i].name, name) == 0) {
381                         return rights[i].right_mask;
382                 }
383         }
384         return 0;
385 }
386
387 /*
388   assist in walking the table of privileges - return the LUID (low 32 bits) by index
389 */
390 enum sec_privilege sec_privilege_from_index(int idx)
391 {
392         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
393                 return privs[idx].luid;
394         }
395         return SEC_PRIV_INVALID;
396 }
397
398 /*
399   assist in walking the table of privileges - return the string constant by index
400 */
401 const char *sec_privilege_name_from_index(int idx)
402 {
403         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
404                 return privs[idx].name;
405         }
406         return NULL;
407 }
408
409
410
411 /*
412   return true if a security_token has a particular privilege bit set
413 */
414 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
415 {
416         uint64_t mask;
417
418         mask = sec_privilege_mask(privilege);
419         if (mask == 0) {
420                 return false;
421         }
422
423         if (token->privilege_mask & mask) {
424                 return true;
425         }
426         return false;
427 }
428
429 /*
430   set a bit in the privilege mask
431 */
432 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
433 {
434         /* Relies on the fact that an invalid privilage will return 0, so won't change this */
435         token->privilege_mask |= sec_privilege_mask(privilege);
436 }
437
438 /*
439   set a bit in the rights mask
440 */
441 void security_token_set_right_bit(struct security_token *token, uint32_t right_bit)
442 {
443         token->rights_mask |= right_bit;
444 }
445
446 void security_token_debug_privileges(int dbg_class, int dbg_lev, const struct security_token *token)
447 {
448         DEBUGADDC(dbg_class, dbg_lev, (" Privileges (0x%16llX):\n",
449                                        (unsigned long long) token->privilege_mask));
450
451         if (token->privilege_mask) {
452                 int idx = 0;
453                 int i = 0;
454                 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
455                         if (token->privilege_mask & privs[idx].privilege_mask) {
456                                 DEBUGADDC(dbg_class, dbg_lev,
457                                           ("  Privilege[%3lu]: %s\n", (unsigned long)i++,
458                                            privs[idx].name));
459                         }
460                 }
461         }
462         DEBUGADDC(dbg_class, dbg_lev, (" Rights (0x%16lX):\n",
463                                        (unsigned long) token->rights_mask));
464
465         if (token->rights_mask) {
466                 int idx = 0;
467                 int i = 0;
468                 for (idx = 0; idx<ARRAY_SIZE(rights); idx++) {
469                         if (token->rights_mask & rights[idx].right_mask) {
470                                 DEBUGADDC(dbg_class, dbg_lev,
471                                           ("  Right[%3lu]: %s\n", (unsigned long)i++,
472                                            rights[idx].name));
473                         }
474                 }
475         }
476 }