libcli/security - fix two output format specifiers
[ira/wip.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 9
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         {SEC_PRIV_SECURITY,        SEC_PRIV_SECURITY_BIT,        "SeSecurityPrivilege", "System security"},
62
63
64         /* The list from here on is not displayed in the code from
65          * source3, and is after index NUM_SHORT_LIST_PRIVS for that
66          * reason */ 
67
68         {SEC_PRIV_SYSTEMTIME,
69          SEC_PRIV_SYSTEMTIME_BIT,
70          "SeSystemtimePrivilege",
71         "Set the system clock"},
72
73         {SEC_PRIV_SHUTDOWN,
74          SEC_PRIV_SHUTDOWN_BIT,
75          "SeShutdownPrivilege",
76         "Shutdown the system"},
77
78         {SEC_PRIV_DEBUG,
79          SEC_PRIV_DEBUG_BIT,
80          "SeDebugPrivilege",
81         "Debug processes"},
82
83         {SEC_PRIV_SYSTEM_ENVIRONMENT,
84          SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
85          "SeSystemEnvironmentPrivilege",
86         "Modify system environment"},
87
88         {SEC_PRIV_SYSTEM_PROFILE,
89          SEC_PRIV_SYSTEM_PROFILE_BIT,
90          "SeSystemProfilePrivilege",
91         "Profile the system"},
92
93         {SEC_PRIV_PROFILE_SINGLE_PROCESS,
94          SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
95          "SeProfileSingleProcessPrivilege",
96         "Profile one process"},
97
98         {SEC_PRIV_INCREASE_BASE_PRIORITY,
99          SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
100          "SeIncreaseBasePriorityPrivilege",
101          "Increase base priority"},
102
103         {SEC_PRIV_LOAD_DRIVER,
104          SEC_PRIV_LOAD_DRIVER_BIT,
105          "SeLoadDriverPrivilege",
106         "Load drivers"},
107
108         {SEC_PRIV_CREATE_PAGEFILE,
109          SEC_PRIV_CREATE_PAGEFILE_BIT,
110          "SeCreatePagefilePrivilege",
111         "Create page files"},
112
113         {SEC_PRIV_INCREASE_QUOTA,
114          SEC_PRIV_INCREASE_QUOTA_BIT,
115          "SeIncreaseQuotaPrivilege",
116         "Increase quota"},
117
118         {SEC_PRIV_CHANGE_NOTIFY,
119          SEC_PRIV_CHANGE_NOTIFY_BIT,
120          "SeChangeNotifyPrivilege",
121         "Register for change notify"},
122
123         {SEC_PRIV_UNDOCK,
124          SEC_PRIV_UNDOCK_BIT,
125          "SeUndockPrivilege",
126         "Undock devices"},
127
128         {SEC_PRIV_MANAGE_VOLUME,
129          SEC_PRIV_MANAGE_VOLUME_BIT,
130          "SeManageVolumePrivilege",
131         "Manage system volumes"},
132
133         {SEC_PRIV_IMPERSONATE,
134          SEC_PRIV_IMPERSONATE_BIT,
135          "SeImpersonatePrivilege",
136         "Impersonate users"},
137
138         {SEC_PRIV_CREATE_GLOBAL,
139          SEC_PRIV_CREATE_GLOBAL_BIT,
140          "SeCreateGlobalPrivilege",
141         "Create global"},
142
143         {SEC_PRIV_ENABLE_DELEGATION,
144          SEC_PRIV_ENABLE_DELEGATION_BIT,
145          "SeEnableDelegationPrivilege",
146         "Enable Delegation"},
147 };
148
149 /* These are rights, not privileges, and should not be confused.  The
150  * names are very similar, and they are quite similar in behaviour,
151  * but they are not to be enumerated as a system-wide list or have an
152  * LUID value */
153 static const struct {
154         uint32_t right_mask;
155         const char *name;
156         const char *description;
157 } rights[] = {
158         {LSA_POLICY_MODE_INTERACTIVE,
159          "SeInteractiveLogonRight",
160         "Interactive logon"},
161
162         {LSA_POLICY_MODE_NETWORK,
163          "SeNetworkLogonRight",
164         "Network logon"},
165
166         {LSA_POLICY_MODE_REMOTE_INTERACTIVE,
167          "SeRemoteInteractiveLogonRight",
168         "Remote Interactive logon"}
169 };
170
171 /*
172   return a privilege mask given a privilege id
173 */
174 uint64_t sec_privilege_mask(enum sec_privilege privilege)
175 {
176         int i;
177         for (i=0;i<ARRAY_SIZE(privs);i++) {
178                 if (privs[i].luid == privilege) {
179                         return privs[i].privilege_mask;
180                 }
181         }
182
183         return 0;
184 }
185
186 /***************************************************************************
187  put all valid privileges into a mask
188 ****************************************************************************/
189
190 void se_priv_put_all_privileges(uint64_t *privilege_mask)
191 {
192         int i;
193         uint32_t num_privs = ARRAY_SIZE(privs);
194
195         *privilege_mask = 0;
196         for ( i=0; i<num_privs; i++ ) {
197                 *privilege_mask |= privs[i].privilege_mask;
198         }
199 }
200
201 /*********************************************************************
202  Lookup the uint64_t bitmask value for a privilege name
203 *********************************************************************/
204
205 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
206 {
207         int i;
208         uint32_t num_privs = ARRAY_SIZE(privs);
209         for ( i=0; i<num_privs; i++ ) {
210                 if ( strequal( privs[i].name, name ) ) {
211                         *privilege_mask = privs[i].privilege_mask;
212                         return true;
213                 }
214         }
215
216         return false;
217 }
218
219 const char* get_privilege_dispname( const char *name )
220 {
221         int i;
222
223         uint32_t num_privs = ARRAY_SIZE(privs);
224
225         if (!name) {
226                 return NULL;
227         }
228
229         for ( i=0; i<num_privs; i++ ) {
230                 if ( strequal( privs[i].name, name ) ) {
231                         return privs[i].description;
232                 }
233         }
234
235         return NULL;
236 }
237
238 /*******************************************************************
239  return the number of elements in the 'short' privlege array (traditional source3 behaviour)
240 *******************************************************************/
241
242 int num_privileges_in_short_list( void )
243 {
244         return NUM_SHORT_LIST_PRIVS;
245 }
246
247 /****************************************************************************
248  add a privilege to a privilege array
249  ****************************************************************************/
250
251 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
252 {
253         struct lsa_LUIDAttribute *new_set;
254
255         /* we can allocate memory to add the new privilege */
256
257         new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
258         if ( !new_set ) {
259                 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
260                 return false;
261         }
262
263         new_set[priv_set->count].luid.high = set.luid.high;
264         new_set[priv_set->count].luid.low = set.luid.low;
265         new_set[priv_set->count].attribute = set.attribute;
266
267         priv_set->count++;
268         priv_set->set = new_set;
269
270         return true;
271 }
272
273 /*******************************************************************
274 *******************************************************************/
275
276 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
277 {
278         int i;
279         uint32_t num_privs = ARRAY_SIZE(privs);
280         struct lsa_LUIDAttribute luid;
281
282         luid.attribute = 0;
283         luid.luid.high = 0;
284
285         for ( i=0; i<num_privs; i++ ) {
286                 if ((privilege_mask & privs[i].privilege_mask) == 0)
287                         continue;
288
289                 luid.luid.high = 0;
290                 luid.luid.low = privs[i].luid;
291
292                 if ( !privilege_set_add( set, luid ) )
293                         return false;
294         }
295
296         return true;
297 }
298
299 /*******************************************************************
300 *******************************************************************/
301
302 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
303 {
304         int i;
305
306         ZERO_STRUCTP( privilege_mask );
307
308         for ( i=0; i<privset->count; i++ ) {
309                 uint64_t r;
310
311                 /* sanity check for invalid privilege.  we really
312                    only care about the low 32 bits */
313
314                 if ( privset->set[i].luid.high != 0 )
315                         return false;
316
317                 r = sec_privilege_mask(privset->set[i].luid.low);
318                 if (r) {
319                         *privilege_mask |= r;
320                 }
321         }
322
323         return true;
324 }
325
326 /*
327   map a privilege id to the wire string constant
328 */
329 const char *sec_privilege_name(enum sec_privilege privilege)
330 {
331         int i;
332         for (i=0;i<ARRAY_SIZE(privs);i++) {
333                 if (privs[i].luid == privilege) {
334                         return privs[i].name;
335                 }
336         }
337         return NULL;
338 }
339
340 /*
341   map a privilege id to a privilege display name. Return NULL if not found
342
343   TODO: this should use language mappings
344 */
345 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
346 {
347         int i;
348         for (i=0;i<ARRAY_SIZE(privs);i++) {
349                 if (privs[i].luid == privilege) {
350                         return privs[i].description;
351                 }
352         }
353         return NULL;
354 }
355
356 /*
357   map a privilege name to a privilege id. Return SEC_PRIV_INVALID if not found
358 */
359 enum sec_privilege sec_privilege_id(const char *name)
360 {
361         int i;
362         for (i=0;i<ARRAY_SIZE(privs);i++) {
363                 if (strcasecmp(privs[i].name, name) == 0) {
364                         return privs[i].luid;
365                 }
366         }
367         return SEC_PRIV_INVALID;
368 }
369
370 /*
371   map a 'right' name to it's bitmap value. Return 0 if not found
372 */
373 uint32_t sec_right_bit(const char *name)
374 {
375         int i;
376         for (i=0;i<ARRAY_SIZE(rights);i++) {
377                 if (strcasecmp(rights[i].name, name) == 0) {
378                         return rights[i].right_mask;
379                 }
380         }
381         return 0;
382 }
383
384 /*
385   assist in walking the table of privileges - return the LUID (low 32 bits) by index
386 */
387 enum sec_privilege sec_privilege_from_index(int idx)
388 {
389         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
390                 return privs[idx].luid;
391         }
392         return SEC_PRIV_INVALID;
393 }
394
395 /*
396   assist in walking the table of privileges - return the string constant by index
397 */
398 const char *sec_privilege_name_from_index(int idx)
399 {
400         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
401                 return privs[idx].name;
402         }
403         return NULL;
404 }
405
406
407
408 /*
409   return true if a security_token has a particular privilege bit set
410 */
411 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
412 {
413         uint64_t mask;
414
415         if (!token) {
416                 return false;
417         }
418
419         mask = sec_privilege_mask(privilege);
420         if (mask == 0) {
421                 return false;
422         }
423
424         if (token->privilege_mask & mask) {
425                 return true;
426         }
427         return false;
428 }
429
430 /*
431   set a bit in the privilege mask
432 */
433 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
434 {
435         /* Relies on the fact that an invalid privilage will return 0, so won't change this */
436         token->privilege_mask |= sec_privilege_mask(privilege);
437 }
438
439 /*
440   set a bit in the rights mask
441 */
442 void security_token_set_right_bit(struct security_token *token, uint32_t right_bit)
443 {
444         token->rights_mask |= right_bit;
445 }
446
447 void security_token_debug_privileges(int dbg_class, int dbg_lev, const struct security_token *token)
448 {
449         DEBUGADDC(dbg_class, dbg_lev, (" Privileges (0x%16llX):\n",
450                                        (unsigned long long) token->privilege_mask));
451
452         if (token->privilege_mask) {
453                 int idx = 0;
454                 int i = 0;
455                 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
456                         if (token->privilege_mask & privs[idx].privilege_mask) {
457                                 DEBUGADDC(dbg_class, dbg_lev,
458                                           ("  Privilege[%3lu]: %s\n", (unsigned long)i++,
459                                            privs[idx].name));
460                         }
461                 }
462         }
463         DEBUGADDC(dbg_class, dbg_lev, (" Rights (0x%16lX):\n",
464                                        (unsigned long) token->rights_mask));
465
466         if (token->rights_mask) {
467                 int idx = 0;
468                 int i = 0;
469                 for (idx = 0; idx<ARRAY_SIZE(rights); idx++) {
470                         if (token->rights_mask & rights[idx].right_mask) {
471                                 DEBUGADDC(dbg_class, dbg_lev,
472                                           ("  Right[%3lu]: %s\n", (unsigned long)i++,
473                                            rights[idx].name));
474                         }
475                 }
476         }
477 }