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 "libcli/security/privileges_private.h"
38 #include "librpc/gen_ndr/security.h"
40 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
43 #define NUM_SHORT_LIST_PRIVS 8
46 enum sec_privilege luid;
47 uint64_t privilege_mask;
49 const char *description;
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"},
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"},
62 /* The list from here on was not displayed in the code from
63 * source3/ with the comment that usrmgr will display these
64 * next 2 twice if you include them. The source4/ code has
65 * always included them, but they do not appear in Windows
68 Finally, the parameter 'short_list' determines if the short
69 or full list (including many other privileges) is used */
72 SEC_PRIV_SECURITY_BIT,
73 "SeSecurityPrivilege",
77 SEC_PRIV_SYSTEMTIME_BIT,
78 "SeSystemtimePrivilege",
79 "Set the system clock"},
82 SEC_PRIV_SHUTDOWN_BIT,
83 "SeShutdownPrivilege",
84 "Shutdown the system"},
91 {SEC_PRIV_SYSTEM_ENVIRONMENT,
92 SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
93 "SeSystemEnvironmentPrivilege",
94 "Modify system environment"},
96 {SEC_PRIV_SYSTEM_PROFILE,
97 SEC_PRIV_SYSTEM_PROFILE_BIT,
98 "SeSystemProfilePrivilege",
99 "Profile the system"},
101 {SEC_PRIV_PROFILE_SINGLE_PROCESS,
102 SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
103 "SeProfileSingleProcessPrivilege",
104 "Profile one process"},
106 {SEC_PRIV_INCREASE_BASE_PRIORITY,
107 SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
108 "SeIncreaseBasePriorityPrivilege",
109 "Increase base priority"},
111 {SEC_PRIV_LOAD_DRIVER,
112 SEC_PRIV_LOAD_DRIVER_BIT,
113 "SeLoadDriverPrivilege",
116 {SEC_PRIV_CREATE_PAGEFILE,
117 SEC_PRIV_CREATE_PAGEFILE_BIT,
118 "SeCreatePagefilePrivilege",
119 "Create page files"},
121 {SEC_PRIV_INCREASE_QUOTA,
122 SEC_PRIV_INCREASE_QUOTA_BIT,
123 "SeIncreaseQuotaPrivilege",
126 {SEC_PRIV_CHANGE_NOTIFY,
127 SEC_PRIV_CHANGE_NOTIFY_BIT,
128 "SeChangeNotifyPrivilege",
129 "Register for change notify"},
136 {SEC_PRIV_MANAGE_VOLUME,
137 SEC_PRIV_MANAGE_VOLUME_BIT,
138 "SeManageVolumePrivilege",
139 "Manage system volumes"},
141 {SEC_PRIV_IMPERSONATE,
142 SEC_PRIV_IMPERSONATE_BIT,
143 "SeImpersonatePrivilege",
144 "Impersonate users"},
146 {SEC_PRIV_CREATE_GLOBAL,
147 SEC_PRIV_CREATE_GLOBAL_BIT,
148 "SeCreateGlobalPrivilege",
151 {SEC_PRIV_ENABLE_DELEGATION,
152 SEC_PRIV_ENABLE_DELEGATION_BIT,
153 "SeEnableDelegationPrivilege",
154 "Enable Delegation"},
156 {SEC_PRIV_INTERACTIVE_LOGON,
157 SEC_PRIV_INTERACTIVE_LOGON_BIT,
158 "SeInteractiveLogonRight",
159 "Interactive logon"},
161 {SEC_PRIV_NETWORK_LOGON,
162 SEC_PRIV_NETWORK_LOGON_BIT,
163 "SeNetworkLogonRight",
166 {SEC_PRIV_REMOTE_INTERACTIVE_LOGON,
167 SEC_PRIV_REMOTE_INTERACTIVE_LOGON_BIT,
168 "SeRemoteInteractiveLogonRight",
169 "Remote Interactive logon"}
173 return a privilege mask given a privilege id
175 uint64_t sec_privilege_mask(enum sec_privilege privilege)
178 for (i=0;i<ARRAY_SIZE(privs);i++) {
179 if (privs[i].luid == privilege) {
180 return privs[i].privilege_mask;
187 /***************************************************************************
188 put all valid privileges into a mask
189 ****************************************************************************/
191 void se_priv_put_all_privileges(uint64_t *privilege_mask)
194 uint32_t num_privs = ARRAY_SIZE(privs);
197 for ( i=0; i<num_privs; i++ ) {
198 *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 for (i=0;i<ARRAY_SIZE(privs);i++) {
350 if (privs[i].luid == privilege) {
351 return privs[i].description;
358 map a privilege name to a privilege id. Return -1 if not found
360 enum sec_privilege sec_privilege_id(const char *name)
363 for (i=0;i<ARRAY_SIZE(privs);i++) {
364 if (strcasecmp(privs[i].name, name) == 0) {
365 return privs[i].luid;
368 return SEC_PRIV_INVALID;
372 assist in walking the table of privileges - return the LUID (low 32 bits) by index
374 enum sec_privilege sec_privilege_from_index(int idx)
376 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
377 return privs[idx].luid;
379 return SEC_PRIV_INVALID;
383 assist in walking the table of privileges - return the string constant by index
385 const char *sec_privilege_name_from_index(int idx)
387 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
388 return privs[idx].name;
396 return true if a security_token has a particular privilege bit set
398 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
402 mask = sec_privilege_mask(privilege);
407 if (token->privilege_mask & mask) {
414 set a bit in the privilege mask
416 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
418 /* Relies on the fact that an invalid privilage will return 0, so won't change this */
419 token->privilege_mask |= sec_privilege_mask(privilege);
422 void security_token_debug_privileges(int dbg_lev, const struct security_token *token)
424 DEBUGADD(dbg_lev, (" Privileges (0x%16llX):\n",
425 (unsigned long long) token->privilege_mask));
427 if (token->privilege_mask) {
430 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
431 if (token->privilege_mask & privs[idx].privilege_mask) {
432 DEBUGADD(dbg_lev, (" Privilege[%3lu]: %s\n", (unsigned long)i++,