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
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.
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.
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/>.
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.
30 * generally SID- and LUID-related code is left in lib/privileges.c
32 * some extra functions to hide privs array from lib/privileges.c
36 #include "libcli/security/privileges.h"
37 #include "librpc/gen_ndr/security.h"
39 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
42 #define NUM_SHORT_LIST_PRIVS 8
45 enum sec_privilege luid;
46 uint64_t privilege_mask;
48 const char *description;
51 {SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_MACHINE_ACCOUNT_BIT, "SeMachineAccountPrivilege", "Add machines to domain"},
52 {SEC_PRIV_TAKE_OWNERSHIP, SEC_PRIV_TAKE_OWNERSHIP_BIT, "SeTakeOwnershipPrivilege", "Take ownership of files or other objects"},
53 {SEC_PRIV_BACKUP, SEC_PRIV_BACKUP_BIT, "SeBackupPrivilege", "Back up files and directories"},
54 {SEC_PRIV_RESTORE, SEC_PRIV_RESTORE_BIT, "SeRestorePrivilege", "Restore files and directories"},
55 {SEC_PRIV_REMOTE_SHUTDOWN, SEC_PRIV_REMOTE_SHUTDOWN_BIT, "SeRemoteShutdownPrivilege", "Force shutdown from a remote system"},
57 {SEC_PRIV_PRINT_OPERATOR, SEC_PRIV_PRINT_OPERATOR_BIT, "SePrintOperatorPrivilege", "Manage printers"},
58 {SEC_PRIV_ADD_USERS, SEC_PRIV_ADD_USERS_BIT, "SeAddUsersPrivilege", "Add users and groups to the domain"},
59 {SEC_PRIV_DISK_OPERATOR, SEC_PRIV_DISK_OPERATOR_BIT, "SeDiskOperatorPrivilege", "Manage disk shares"},
61 /* The list from here on was not displayed in the code from
62 * source3/ with the comment that usrmgr will display these
63 * next 2 twice if you include them. The source4/ code has
64 * always included them, but they do not appear in Windows
67 Finally, the parameter 'short_list' determines if the short
68 or full list (including many other privileges) is used */
71 SEC_PRIV_SECURITY_BIT,
72 "SeSecurityPrivilege",
76 SEC_PRIV_SYSTEMTIME_BIT,
77 "SeSystemtimePrivilege",
78 "Set the system clock"},
81 SEC_PRIV_SHUTDOWN_BIT,
82 "SeShutdownPrivilege",
83 "Shutdown the system"},
90 {SEC_PRIV_SYSTEM_ENVIRONMENT,
91 SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
92 "SeSystemEnvironmentPrivilege",
93 "Modify system environment"},
95 {SEC_PRIV_SYSTEM_PROFILE,
96 SEC_PRIV_SYSTEM_PROFILE_BIT,
97 "SeSystemProfilePrivilege",
98 "Profile the system"},
100 {SEC_PRIV_PROFILE_SINGLE_PROCESS,
101 SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
102 "SeProfileSingleProcessPrivilege",
103 "Profile one process"},
105 {SEC_PRIV_INCREASE_BASE_PRIORITY,
106 SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
107 "SeIncreaseBasePriorityPrivilege",
108 "Increase base priority"},
110 {SEC_PRIV_LOAD_DRIVER,
111 SEC_PRIV_LOAD_DRIVER_BIT,
112 "SeLoadDriverPrivilege",
115 {SEC_PRIV_CREATE_PAGEFILE,
116 SEC_PRIV_CREATE_PAGEFILE_BIT,
117 "SeCreatePagefilePrivilege",
118 "Create page files"},
120 {SEC_PRIV_INCREASE_QUOTA,
121 SEC_PRIV_INCREASE_QUOTA_BIT,
122 "SeIncreaseQuotaPrivilege",
125 {SEC_PRIV_CHANGE_NOTIFY,
126 SEC_PRIV_CHANGE_NOTIFY_BIT,
127 "SeChangeNotifyPrivilege",
128 "Register for change notify"},
135 {SEC_PRIV_MANAGE_VOLUME,
136 SEC_PRIV_MANAGE_VOLUME_BIT,
137 "SeManageVolumePrivilege",
138 "Manage system volumes"},
140 {SEC_PRIV_IMPERSONATE,
141 SEC_PRIV_IMPERSONATE_BIT,
142 "SeImpersonatePrivilege",
143 "Impersonate users"},
145 {SEC_PRIV_CREATE_GLOBAL,
146 SEC_PRIV_CREATE_GLOBAL_BIT,
147 "SeCreateGlobalPrivilege",
150 {SEC_PRIV_ENABLE_DELEGATION,
151 SEC_PRIV_ENABLE_DELEGATION_BIT,
152 "SeEnableDelegationPrivilege",
153 "Enable Delegation"},
155 {SEC_PRIV_INTERACTIVE_LOGON,
156 SEC_PRIV_INTERACTIVE_LOGON_BIT,
157 "SeInteractiveLogonRight",
158 "Interactive logon"},
160 {SEC_PRIV_NETWORK_LOGON,
161 SEC_PRIV_NETWORK_LOGON_BIT,
162 "SeNetworkLogonRight",
165 {SEC_PRIV_REMOTE_INTERACTIVE_LOGON,
166 SEC_PRIV_REMOTE_INTERACTIVE_LOGON_BIT,
167 "SeRemoteInteractiveLogonRight",
168 "Remote Interactive logon"}
172 return a privilege mask given a privilege id
174 uint64_t sec_privilege_mask(enum sec_privilege privilege)
177 for (i=0;i<ARRAY_SIZE(privs);i++) {
178 if (privs[i].luid == privilege) {
179 return privs[i].privilege_mask;
186 /***************************************************************************
187 put all valid privileges into a mask
188 ****************************************************************************/
190 bool se_priv_put_all_privileges(uint64_t *privilege_mask)
193 uint32_t num_privs = ARRAY_SIZE(privs);
196 for ( i=0; i<num_privs; i++ ) {
197 *privilege_mask |= privs[i].privilege_mask;
202 /*********************************************************************
203 Lookup the uint64_t bitmask value for a privilege name
204 *********************************************************************/
206 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
209 uint32_t num_privs = ARRAY_SIZE(privs);
210 for ( i=0; i<num_privs; i++ ) {
211 if ( strequal( privs[i].name, name ) ) {
212 *privilege_mask = privs[i].privilege_mask;
220 const char* get_privilege_dispname( const char *name )
224 uint32_t num_privs = ARRAY_SIZE(privs);
230 for ( i=0; i<num_privs; i++ ) {
231 if ( strequal( privs[i].name, name ) ) {
232 return privs[i].description;
239 /*******************************************************************
240 return the number of elements in the 'short' privlege array (traditional source3 behaviour)
241 *******************************************************************/
243 int num_privileges_in_short_list( void )
245 return NUM_SHORT_LIST_PRIVS;
248 /****************************************************************************
249 add a privilege to a privilege array
250 ****************************************************************************/
252 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
254 struct lsa_LUIDAttribute *new_set;
256 /* we can allocate memory to add the new privilege */
258 new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
260 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
264 new_set[priv_set->count].luid.high = set.luid.high;
265 new_set[priv_set->count].luid.low = set.luid.low;
266 new_set[priv_set->count].attribute = set.attribute;
269 priv_set->set = new_set;
274 /*******************************************************************
275 *******************************************************************/
277 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
280 uint32_t num_privs = ARRAY_SIZE(privs);
281 struct lsa_LUIDAttribute luid;
286 for ( i=0; i<num_privs; i++ ) {
287 if ((privilege_mask & privs[i].privilege_mask) == 0)
291 luid.luid.low = privs[i].luid;
293 if ( !privilege_set_add( set, luid ) )
300 /*******************************************************************
301 *******************************************************************/
303 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
307 ZERO_STRUCTP( privilege_mask );
309 for ( i=0; i<privset->count; i++ ) {
312 /* sanity check for invalid privilege. we really
313 only care about the low 32 bits */
315 if ( privset->set[i].luid.high != 0 )
318 r = sec_privilege_mask(privset->set[i].luid.low);
320 *privilege_mask |= r;
328 map a privilege id to the wire string constant
330 const char *sec_privilege_name(enum sec_privilege privilege)
333 for (i=0;i<ARRAY_SIZE(privs);i++) {
334 if (privs[i].luid == privilege) {
335 return privs[i].name;
342 map a privilege id to a privilege display name. Return NULL if not found
344 TODO: this should use language mappings
346 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
349 if (privilege < 1 || privilege > 64) {
352 for (i=0;i<ARRAY_SIZE(privs);i++) {
353 if (privs[i].luid == privilege) {
354 return privs[i].description;
361 map a privilege name to a privilege id. Return -1 if not found
363 enum sec_privilege sec_privilege_id(const char *name)
366 for (i=0;i<ARRAY_SIZE(privs);i++) {
367 if (strcasecmp(privs[i].name, name) == 0) {
368 return privs[i].luid;
371 return SEC_PRIV_INVALID;
375 assist in walking the table of privileges - return the LUID (low 32 bits) by index
377 enum sec_privilege sec_privilege_from_index(int idx)
379 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
380 return privs[idx].luid;
382 return SEC_PRIV_INVALID;
386 assist in walking the table of privileges - return the string constant by index
388 const char *sec_privilege_name_from_index(int idx)
390 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
391 return privs[idx].name;
399 return true if a security_token has a particular privilege bit set
401 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
405 mask = sec_privilege_mask(privilege);
410 if (token->privilege_mask & mask) {
417 set a bit in the privilege mask
419 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
421 /* Relies on the fact that an invalid privilage will return 0, so won't change this */
422 token->privilege_mask |= sec_privilege_mask(privilege);
425 void security_token_debug_privileges(int dbg_lev, const struct security_token *token)
427 DEBUGADD(dbg_lev, (" Privileges (0x%16llX):\n",
428 (unsigned long long) token->privilege_mask));
430 if (token->privilege_mask) {
433 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
434 if (token->privilege_mask & privs[idx].privilege_mask) {
435 DEBUGADD(dbg_lev, (" Privilege[%3lu]: %s\n", (unsigned long)i++,