s3: lib: Add new clistr_smb2_extract_snapshot_token() function.
[Samba.git] / libcli / security / privileges.c
blob7910001c60d9a49369cfd716517e71b2508ba713
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 "replace.h"
36 #include "libcli/security/privileges.h"
37 #include "libcli/security/privileges_private.h"
38 #include "librpc/gen_ndr/security.h"
39 #include "lib/util/samba_util.h"
40 #include "lib/util/debug.h"
42 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
43 #undef strcasecmp
45 #define NUM_SHORT_LIST_PRIVS 9
47 static const struct {
48 enum sec_privilege luid;
49 uint64_t privilege_mask;
50 const char *name;
51 const char *description;
52 } privs[] = {
54 {SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_MACHINE_ACCOUNT_BIT, "SeMachineAccountPrivilege", "Add machines to domain"},
55 {SEC_PRIV_TAKE_OWNERSHIP, SEC_PRIV_TAKE_OWNERSHIP_BIT, "SeTakeOwnershipPrivilege", "Take ownership of files or other objects"},
56 {SEC_PRIV_BACKUP, SEC_PRIV_BACKUP_BIT, "SeBackupPrivilege", "Back up files and directories"},
57 {SEC_PRIV_RESTORE, SEC_PRIV_RESTORE_BIT, "SeRestorePrivilege", "Restore files and directories"},
58 {SEC_PRIV_REMOTE_SHUTDOWN, SEC_PRIV_REMOTE_SHUTDOWN_BIT, "SeRemoteShutdownPrivilege", "Force shutdown from a remote system"},
60 {SEC_PRIV_PRINT_OPERATOR, SEC_PRIV_PRINT_OPERATOR_BIT, "SePrintOperatorPrivilege", "Manage printers"},
61 {SEC_PRIV_ADD_USERS, SEC_PRIV_ADD_USERS_BIT, "SeAddUsersPrivilege", "Add users and groups to the domain"},
62 {SEC_PRIV_DISK_OPERATOR, SEC_PRIV_DISK_OPERATOR_BIT, "SeDiskOperatorPrivilege", "Manage disk shares"},
63 {SEC_PRIV_SECURITY, SEC_PRIV_SECURITY_BIT, "SeSecurityPrivilege", "System security"},
66 /* The list from here on is not displayed in the code from
67 * source3, and is after index NUM_SHORT_LIST_PRIVS for that
68 * reason */
70 {SEC_PRIV_SYSTEMTIME,
71 SEC_PRIV_SYSTEMTIME_BIT,
72 "SeSystemtimePrivilege",
73 "Set the system clock"},
75 {SEC_PRIV_SHUTDOWN,
76 SEC_PRIV_SHUTDOWN_BIT,
77 "SeShutdownPrivilege",
78 "Shutdown the system"},
80 {SEC_PRIV_DEBUG,
81 SEC_PRIV_DEBUG_BIT,
82 "SeDebugPrivilege",
83 "Debug processes"},
85 {SEC_PRIV_SYSTEM_ENVIRONMENT,
86 SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
87 "SeSystemEnvironmentPrivilege",
88 "Modify system environment"},
90 {SEC_PRIV_SYSTEM_PROFILE,
91 SEC_PRIV_SYSTEM_PROFILE_BIT,
92 "SeSystemProfilePrivilege",
93 "Profile the system"},
95 {SEC_PRIV_PROFILE_SINGLE_PROCESS,
96 SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
97 "SeProfileSingleProcessPrivilege",
98 "Profile one process"},
100 {SEC_PRIV_INCREASE_BASE_PRIORITY,
101 SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
102 "SeIncreaseBasePriorityPrivilege",
103 "Increase base priority"},
105 {SEC_PRIV_LOAD_DRIVER,
106 SEC_PRIV_LOAD_DRIVER_BIT,
107 "SeLoadDriverPrivilege",
108 "Load drivers"},
110 {SEC_PRIV_CREATE_PAGEFILE,
111 SEC_PRIV_CREATE_PAGEFILE_BIT,
112 "SeCreatePagefilePrivilege",
113 "Create page files"},
115 {SEC_PRIV_INCREASE_QUOTA,
116 SEC_PRIV_INCREASE_QUOTA_BIT,
117 "SeIncreaseQuotaPrivilege",
118 "Increase quota"},
120 {SEC_PRIV_CHANGE_NOTIFY,
121 SEC_PRIV_CHANGE_NOTIFY_BIT,
122 "SeChangeNotifyPrivilege",
123 "Register for change notify"},
125 {SEC_PRIV_UNDOCK,
126 SEC_PRIV_UNDOCK_BIT,
127 "SeUndockPrivilege",
128 "Undock devices"},
130 {SEC_PRIV_MANAGE_VOLUME,
131 SEC_PRIV_MANAGE_VOLUME_BIT,
132 "SeManageVolumePrivilege",
133 "Manage system volumes"},
135 {SEC_PRIV_IMPERSONATE,
136 SEC_PRIV_IMPERSONATE_BIT,
137 "SeImpersonatePrivilege",
138 "Impersonate users"},
140 {SEC_PRIV_CREATE_GLOBAL,
141 SEC_PRIV_CREATE_GLOBAL_BIT,
142 "SeCreateGlobalPrivilege",
143 "Create global"},
145 {SEC_PRIV_ENABLE_DELEGATION,
146 SEC_PRIV_ENABLE_DELEGATION_BIT,
147 "SeEnableDelegationPrivilege",
148 "Enable Delegation"},
151 /* These are rights, not privileges, and should not be confused. The
152 * names are very similar, and they are quite similar in behaviour,
153 * but they are not to be enumerated as a system-wide list or have an
154 * LUID value */
155 static const struct {
156 uint32_t right_mask;
157 const char *name;
158 const char *description;
159 } rights[] = {
160 {LSA_POLICY_MODE_INTERACTIVE,
161 "SeInteractiveLogonRight",
162 "Interactive logon"},
164 {LSA_POLICY_MODE_NETWORK,
165 "SeNetworkLogonRight",
166 "Network logon"},
168 {LSA_POLICY_MODE_REMOTE_INTERACTIVE,
169 "SeRemoteInteractiveLogonRight",
170 "Remote Interactive logon"}
174 return a privilege mask given a privilege id
176 uint64_t sec_privilege_mask(enum sec_privilege privilege)
178 size_t i;
179 for (i=0;i<ARRAY_SIZE(privs);i++) {
180 if (privs[i].luid == privilege) {
181 return privs[i].privilege_mask;
185 return 0;
188 /***************************************************************************
189 put all valid privileges into a mask
190 ****************************************************************************/
192 void se_priv_put_all_privileges(uint64_t *privilege_mask)
194 size_t i;
196 *privilege_mask = 0;
197 for ( i=0; i<ARRAY_SIZE(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 )
208 size_t i;
209 for ( i=0; i<ARRAY_SIZE(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 size_t i;
223 if (!name) {
224 return NULL;
227 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
228 if ( strequal( privs[i].name, name ) ) {
229 return privs[i].description;
233 return NULL;
236 /*******************************************************************
237 return the number of elements in the 'short' privlege array (traditional source3 behaviour)
238 *******************************************************************/
240 int num_privileges_in_short_list( void )
242 return NUM_SHORT_LIST_PRIVS;
245 /****************************************************************************
246 add a privilege to a privilege array
247 ****************************************************************************/
249 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
251 struct lsa_LUIDAttribute *new_set;
253 /* we can allocate memory to add the new privilege */
255 new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
256 if ( !new_set ) {
257 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
258 return false;
261 new_set[priv_set->count].luid.high = set.luid.high;
262 new_set[priv_set->count].luid.low = set.luid.low;
263 new_set[priv_set->count].attribute = set.attribute;
265 priv_set->count++;
266 priv_set->set = new_set;
268 return true;
271 /*******************************************************************
272 *******************************************************************/
274 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
276 size_t i;
277 struct lsa_LUIDAttribute luid;
279 luid.attribute = 0;
280 luid.luid.high = 0;
282 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
283 if ((privilege_mask & privs[i].privilege_mask) == 0)
284 continue;
286 luid.luid.high = 0;
287 luid.luid.low = privs[i].luid;
289 if ( !privilege_set_add( set, luid ) )
290 return false;
293 return true;
296 /*******************************************************************
297 *******************************************************************/
299 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
301 uint32_t i;
303 ZERO_STRUCTP( privilege_mask );
305 for ( i=0; i<privset->count; i++ ) {
306 uint64_t r;
308 /* sanity check for invalid privilege. we really
309 only care about the low 32 bits */
311 if ( privset->set[i].luid.high != 0 )
312 return false;
314 r = sec_privilege_mask(privset->set[i].luid.low);
315 if (r) {
316 *privilege_mask |= r;
320 return true;
324 map a privilege id to the wire string constant
326 const char *sec_privilege_name(enum sec_privilege privilege)
328 size_t i;
329 for (i=0;i<ARRAY_SIZE(privs);i++) {
330 if (privs[i].luid == privilege) {
331 return privs[i].name;
334 return NULL;
338 map a privilege id to a privilege display name. Return NULL if not found
340 TODO: this should use language mappings
342 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
344 size_t i;
345 for (i=0;i<ARRAY_SIZE(privs);i++) {
346 if (privs[i].luid == privilege) {
347 return privs[i].description;
350 return NULL;
354 map a privilege name to a privilege id. Return SEC_PRIV_INVALID if not found
356 enum sec_privilege sec_privilege_id(const char *name)
358 size_t i;
359 for (i=0;i<ARRAY_SIZE(privs);i++) {
360 if (strcasecmp(privs[i].name, name) == 0) {
361 return privs[i].luid;
364 return SEC_PRIV_INVALID;
368 map a 'right' name to it's bitmap value. Return 0 if not found
370 uint32_t sec_right_bit(const char *name)
372 size_t i;
373 for (i=0;i<ARRAY_SIZE(rights);i++) {
374 if (strcasecmp(rights[i].name, name) == 0) {
375 return rights[i].right_mask;
378 return 0;
382 assist in walking the table of privileges - return the LUID (low 32 bits) by index
384 enum sec_privilege sec_privilege_from_index(int idx)
386 if (idx >= 0 && (unsigned)idx<ARRAY_SIZE(privs)) {
387 return privs[idx].luid;
389 return SEC_PRIV_INVALID;
393 assist in walking the table of privileges - return the string constant by index
395 const char *sec_privilege_name_from_index(int idx)
397 if (idx >= 0 && (unsigned)idx<ARRAY_SIZE(privs)) {
398 return privs[idx].name;
400 return NULL;
406 return true if a security_token has a particular privilege bit set
408 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
410 uint64_t mask;
412 if (!token) {
413 return false;
416 mask = sec_privilege_mask(privilege);
417 if (mask == 0) {
418 return false;
421 if (token->privilege_mask & mask) {
422 return true;
424 return false;
427 bool security_token_system_privilege(const struct security_token *token)
429 if (token == NULL) {
430 return false;
433 if (token->privilege_mask == (uint64_t)~0) {
434 return true;
437 return false;
441 set a bit in the privilege mask
443 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
445 /* Relies on the fact that an invalid privilage will return 0, so won't change this */
446 token->privilege_mask |= sec_privilege_mask(privilege);
450 set a bit in the rights mask
452 void security_token_set_right_bit(struct security_token *token, uint32_t right_bit)
454 token->rights_mask |= right_bit;
457 void security_token_debug_privileges(int dbg_class, int dbg_lev, const struct security_token *token)
459 DEBUGADDC(dbg_class, dbg_lev, (" Privileges (0x%16llX):\n",
460 (unsigned long long) token->privilege_mask));
462 if (token->privilege_mask) {
463 size_t idx = 0;
464 int i = 0;
465 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
466 if (token->privilege_mask & privs[idx].privilege_mask) {
467 DEBUGADDC(dbg_class, dbg_lev,
468 (" Privilege[%3lu]: %s\n", (unsigned long)i++,
469 privs[idx].name));
473 DEBUGADDC(dbg_class, dbg_lev, (" Rights (0x%16lX):\n",
474 (unsigned long) token->rights_mask));
476 if (token->rights_mask) {
477 size_t idx = 0;
478 int i = 0;
479 for (idx = 0; idx<ARRAY_SIZE(rights); idx++) {
480 if (token->rights_mask & rights[idx].right_mask) {
481 DEBUGADDC(dbg_class, dbg_lev,
482 (" Right[%3lu]: %s\n", (unsigned long)i++,
483 rights[idx].name));