s3:cleanupd: use MSG_SMB_BRL_VALIDATE to signal cleanupd unclean process shutdown
[Samba.git] / libcli / security / privileges.c
blobd2731c3dd05845151d298996740d14aae56a2fab
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;
194 *privilege_mask = 0;
195 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
196 *privilege_mask |= privs[i].privilege_mask;
200 /*********************************************************************
201 Lookup the uint64_t bitmask value for a privilege name
202 *********************************************************************/
204 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
206 int i;
207 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
208 if ( strequal( privs[i].name, name ) ) {
209 *privilege_mask = privs[i].privilege_mask;
210 return true;
214 return false;
217 const char* get_privilege_dispname( const char *name )
219 int i;
221 if (!name) {
222 return NULL;
225 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
226 if ( strequal( privs[i].name, name ) ) {
227 return privs[i].description;
231 return NULL;
234 /*******************************************************************
235 return the number of elements in the 'short' privlege array (traditional source3 behaviour)
236 *******************************************************************/
238 int num_privileges_in_short_list( void )
240 return NUM_SHORT_LIST_PRIVS;
243 /****************************************************************************
244 add a privilege to a privilege array
245 ****************************************************************************/
247 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
249 struct lsa_LUIDAttribute *new_set;
251 /* we can allocate memory to add the new privilege */
253 new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
254 if ( !new_set ) {
255 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
256 return false;
259 new_set[priv_set->count].luid.high = set.luid.high;
260 new_set[priv_set->count].luid.low = set.luid.low;
261 new_set[priv_set->count].attribute = set.attribute;
263 priv_set->count++;
264 priv_set->set = new_set;
266 return true;
269 /*******************************************************************
270 *******************************************************************/
272 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
274 int i;
275 struct lsa_LUIDAttribute luid;
277 luid.attribute = 0;
278 luid.luid.high = 0;
280 for ( i=0; i<ARRAY_SIZE(privs); i++ ) {
281 if ((privilege_mask & privs[i].privilege_mask) == 0)
282 continue;
284 luid.luid.high = 0;
285 luid.luid.low = privs[i].luid;
287 if ( !privilege_set_add( set, luid ) )
288 return false;
291 return true;
294 /*******************************************************************
295 *******************************************************************/
297 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
299 uint32_t i;
301 ZERO_STRUCTP( privilege_mask );
303 for ( i=0; i<privset->count; i++ ) {
304 uint64_t r;
306 /* sanity check for invalid privilege. we really
307 only care about the low 32 bits */
309 if ( privset->set[i].luid.high != 0 )
310 return false;
312 r = sec_privilege_mask(privset->set[i].luid.low);
313 if (r) {
314 *privilege_mask |= r;
318 return true;
322 map a privilege id to the wire string constant
324 const char *sec_privilege_name(enum sec_privilege privilege)
326 int i;
327 for (i=0;i<ARRAY_SIZE(privs);i++) {
328 if (privs[i].luid == privilege) {
329 return privs[i].name;
332 return NULL;
336 map a privilege id to a privilege display name. Return NULL if not found
338 TODO: this should use language mappings
340 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
342 int i;
343 for (i=0;i<ARRAY_SIZE(privs);i++) {
344 if (privs[i].luid == privilege) {
345 return privs[i].description;
348 return NULL;
352 map a privilege name to a privilege id. Return SEC_PRIV_INVALID if not found
354 enum sec_privilege sec_privilege_id(const char *name)
356 int i;
357 for (i=0;i<ARRAY_SIZE(privs);i++) {
358 if (strcasecmp(privs[i].name, name) == 0) {
359 return privs[i].luid;
362 return SEC_PRIV_INVALID;
366 map a 'right' name to it's bitmap value. Return 0 if not found
368 uint32_t sec_right_bit(const char *name)
370 int i;
371 for (i=0;i<ARRAY_SIZE(rights);i++) {
372 if (strcasecmp(rights[i].name, name) == 0) {
373 return rights[i].right_mask;
376 return 0;
380 assist in walking the table of privileges - return the LUID (low 32 bits) by index
382 enum sec_privilege sec_privilege_from_index(int idx)
384 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
385 return privs[idx].luid;
387 return SEC_PRIV_INVALID;
391 assist in walking the table of privileges - return the string constant by index
393 const char *sec_privilege_name_from_index(int idx)
395 if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
396 return privs[idx].name;
398 return NULL;
404 return true if a security_token has a particular privilege bit set
406 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
408 uint64_t mask;
410 if (!token) {
411 return false;
414 mask = sec_privilege_mask(privilege);
415 if (mask == 0) {
416 return false;
419 if (token->privilege_mask & mask) {
420 return true;
422 return false;
425 bool security_token_system_privilege(const struct security_token *token)
427 if (token == NULL) {
428 return false;
431 if (token->privilege_mask == (uint64_t)~0) {
432 return true;
435 return false;
439 set a bit in the privilege mask
441 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
443 /* Relies on the fact that an invalid privilage will return 0, so won't change this */
444 token->privilege_mask |= sec_privilege_mask(privilege);
448 set a bit in the rights mask
450 void security_token_set_right_bit(struct security_token *token, uint32_t right_bit)
452 token->rights_mask |= right_bit;
455 void security_token_debug_privileges(int dbg_class, int dbg_lev, const struct security_token *token)
457 DEBUGADDC(dbg_class, dbg_lev, (" Privileges (0x%16llX):\n",
458 (unsigned long long) token->privilege_mask));
460 if (token->privilege_mask) {
461 int idx = 0;
462 int i = 0;
463 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
464 if (token->privilege_mask & privs[idx].privilege_mask) {
465 DEBUGADDC(dbg_class, dbg_lev,
466 (" Privilege[%3lu]: %s\n", (unsigned long)i++,
467 privs[idx].name));
471 DEBUGADDC(dbg_class, dbg_lev, (" Rights (0x%16lX):\n",
472 (unsigned long) token->rights_mask));
474 if (token->rights_mask) {
475 int idx = 0;
476 int i = 0;
477 for (idx = 0; idx<ARRAY_SIZE(rights); idx++) {
478 if (token->rights_mask & rights[idx].right_mask) {
479 DEBUGADDC(dbg_class, dbg_lev,
480 (" Right[%3lu]: %s\n", (unsigned long)i++,
481 rights[idx].name));