WHATSNEW: Update changed parameters.
[Samba/gebeck_regimport.git] / libcli / security / privileges.c
blobe215b5f43abf8d6a7865cb7fc13e66d55e0eacf6
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
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
35 #include "includes.h"
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 */
41 #undef strcasecmp
43 #define NUM_SHORT_LIST_PRIVS 9
45 static const struct {
46 enum sec_privilege luid;
47 uint64_t privilege_mask;
48 const char *name;
49 const char *description;
50 } privs[] = {
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"},
61 {SEC_PRIV_SECURITY, SEC_PRIV_SECURITY_BIT, "SeSecurityPrivilege", "System security"},
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 */
68 {SEC_PRIV_SYSTEMTIME,
69 SEC_PRIV_SYSTEMTIME_BIT,
70 "SeSystemtimePrivilege",
71 "Set the system clock"},
73 {SEC_PRIV_SHUTDOWN,
74 SEC_PRIV_SHUTDOWN_BIT,
75 "SeShutdownPrivilege",
76 "Shutdown the system"},
78 {SEC_PRIV_DEBUG,
79 SEC_PRIV_DEBUG_BIT,
80 "SeDebugPrivilege",
81 "Debug processes"},
83 {SEC_PRIV_SYSTEM_ENVIRONMENT,
84 SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
85 "SeSystemEnvironmentPrivilege",
86 "Modify system environment"},
88 {SEC_PRIV_SYSTEM_PROFILE,
89 SEC_PRIV_SYSTEM_PROFILE_BIT,
90 "SeSystemProfilePrivilege",
91 "Profile the system"},
93 {SEC_PRIV_PROFILE_SINGLE_PROCESS,
94 SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
95 "SeProfileSingleProcessPrivilege",
96 "Profile one process"},
98 {SEC_PRIV_INCREASE_BASE_PRIORITY,
99 SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
100 "SeIncreaseBasePriorityPrivilege",
101 "Increase base priority"},
103 {SEC_PRIV_LOAD_DRIVER,
104 SEC_PRIV_LOAD_DRIVER_BIT,
105 "SeLoadDriverPrivilege",
106 "Load drivers"},
108 {SEC_PRIV_CREATE_PAGEFILE,
109 SEC_PRIV_CREATE_PAGEFILE_BIT,
110 "SeCreatePagefilePrivilege",
111 "Create page files"},
113 {SEC_PRIV_INCREASE_QUOTA,
114 SEC_PRIV_INCREASE_QUOTA_BIT,
115 "SeIncreaseQuotaPrivilege",
116 "Increase quota"},
118 {SEC_PRIV_CHANGE_NOTIFY,
119 SEC_PRIV_CHANGE_NOTIFY_BIT,
120 "SeChangeNotifyPrivilege",
121 "Register for change notify"},
123 {SEC_PRIV_UNDOCK,
124 SEC_PRIV_UNDOCK_BIT,
125 "SeUndockPrivilege",
126 "Undock devices"},
128 {SEC_PRIV_MANAGE_VOLUME,
129 SEC_PRIV_MANAGE_VOLUME_BIT,
130 "SeManageVolumePrivilege",
131 "Manage system volumes"},
133 {SEC_PRIV_IMPERSONATE,
134 SEC_PRIV_IMPERSONATE_BIT,
135 "SeImpersonatePrivilege",
136 "Impersonate users"},
138 {SEC_PRIV_CREATE_GLOBAL,
139 SEC_PRIV_CREATE_GLOBAL_BIT,
140 "SeCreateGlobalPrivilege",
141 "Create global"},
143 {SEC_PRIV_ENABLE_DELEGATION,
144 SEC_PRIV_ENABLE_DELEGATION_BIT,
145 "SeEnableDelegationPrivilege",
146 "Enable Delegation"},
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"},
162 {LSA_POLICY_MODE_NETWORK,
163 "SeNetworkLogonRight",
164 "Network logon"},
166 {LSA_POLICY_MODE_REMOTE_INTERACTIVE,
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)
176 int i;
177 for (i=0;i<ARRAY_SIZE(privs);i++) {
178 if (privs[i].luid == privilege) {
179 return privs[i].privilege_mask;
183 return 0;
186 /***************************************************************************
187 put all valid privileges into a mask
188 ****************************************************************************/
190 void se_priv_put_all_privileges(uint64_t *privilege_mask)
192 int i;
193 uint32_t num_privs = ARRAY_SIZE(privs);
195 *privilege_mask = 0;
196 for ( i=0; i<num_privs; i++ ) {
197 *privilege_mask |= privs[i].privilege_mask;
201 /*********************************************************************
202 Lookup the uint64_t bitmask value for a privilege name
203 *********************************************************************/
205 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
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;
216 return false;
219 const char* get_privilege_dispname( const char *name )
221 int i;
223 uint32_t num_privs = ARRAY_SIZE(privs);
225 if (!name) {
226 return NULL;
229 for ( i=0; i<num_privs; i++ ) {
230 if ( strequal( privs[i].name, name ) ) {
231 return privs[i].description;
235 return NULL;
238 /*******************************************************************
239 return the number of elements in the 'short' privlege array (traditional source3 behaviour)
240 *******************************************************************/
242 int num_privileges_in_short_list( void )
244 return NUM_SHORT_LIST_PRIVS;
247 /****************************************************************************
248 add a privilege to a privilege array
249 ****************************************************************************/
251 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
253 struct lsa_LUIDAttribute *new_set;
255 /* we can allocate memory to add the new privilege */
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;
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;
267 priv_set->count++;
268 priv_set->set = new_set;
270 return true;
273 /*******************************************************************
274 *******************************************************************/
276 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
278 int i;
279 uint32_t num_privs = ARRAY_SIZE(privs);
280 struct lsa_LUIDAttribute luid;
282 luid.attribute = 0;
283 luid.luid.high = 0;
285 for ( i=0; i<num_privs; i++ ) {
286 if ((privilege_mask & privs[i].privilege_mask) == 0)
287 continue;
289 luid.luid.high = 0;
290 luid.luid.low = privs[i].luid;
292 if ( !privilege_set_add( set, luid ) )
293 return false;
296 return true;
299 /*******************************************************************
300 *******************************************************************/
302 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
304 int i;
306 ZERO_STRUCTP( privilege_mask );
308 for ( i=0; i<privset->count; i++ ) {
309 uint64_t r;
311 /* sanity check for invalid privilege. we really
312 only care about the low 32 bits */
314 if ( privset->set[i].luid.high != 0 )
315 return false;
317 r = sec_privilege_mask(privset->set[i].luid.low);
318 if (r) {
319 *privilege_mask |= r;
323 return true;
327 map a privilege id to the wire string constant
329 const char *sec_privilege_name(enum sec_privilege privilege)
331 int i;
332 for (i=0;i<ARRAY_SIZE(privs);i++) {
333 if (privs[i].luid == privilege) {
334 return privs[i].name;
337 return NULL;
341 map a privilege id to a privilege display name. Return NULL if not found
343 TODO: this should use language mappings
345 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
347 int i;
348 for (i=0;i<ARRAY_SIZE(privs);i++) {
349 if (privs[i].luid == privilege) {
350 return privs[i].description;
353 return NULL;
357 map a privilege name to a privilege id. Return SEC_PRIV_INVALID if not found
359 enum sec_privilege sec_privilege_id(const char *name)
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;
367 return SEC_PRIV_INVALID;
371 map a 'right' name to it's bitmap value. Return 0 if not found
373 uint32_t sec_right_bit(const char *name)
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;
381 return 0;
385 assist in walking the table of privileges - return the LUID (low 32 bits) by index
387 enum sec_privilege sec_privilege_from_index(int idx)
389 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
390 return privs[idx].luid;
392 return SEC_PRIV_INVALID;
396 assist in walking the table of privileges - return the string constant by index
398 const char *sec_privilege_name_from_index(int idx)
400 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
401 return privs[idx].name;
403 return NULL;
409 return true if a security_token has a particular privilege bit set
411 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
413 uint64_t mask;
415 if (!token) {
416 return false;
419 mask = sec_privilege_mask(privilege);
420 if (mask == 0) {
421 return false;
424 if (token->privilege_mask & mask) {
425 return true;
427 return false;
431 set a bit in the privilege mask
433 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
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);
440 set a bit in the rights mask
442 void security_token_set_right_bit(struct security_token *token, uint32_t right_bit)
444 token->rights_mask |= right_bit;
447 void security_token_debug_privileges(int dbg_class, int dbg_lev, const struct security_token *token)
449 DEBUGADDC(dbg_class, dbg_lev, (" Privileges (0x%16llX):\n",
450 (unsigned long long) token->privilege_mask));
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));
463 DEBUGADDC(dbg_class, dbg_lev, (" Rights (0x%16lX):\n",
464 (unsigned long) token->rights_mask));
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));