libcli/security Move 'private' privileges functions to another header
[amitay/samba.git] / libcli / security / privileges.c
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
10
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.
15
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.
20
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/>.
23 */
24
25 /*
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.
29  *
30  * generally SID- and LUID-related code is left in lib/privileges.c
31  *
32  * some extra functions to hide privs array from lib/privileges.c
33  */
34
35 #include "includes.h"
36 #include "libcli/security/privileges.h"
37 #include "libcli/security/privileges_private.h"
38 #include "librpc/gen_ndr/security.h"
39
40 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
41 #undef strcasecmp
42
43 #define NUM_SHORT_LIST_PRIVS 8
44
45 static const struct {
46         enum sec_privilege luid;
47         uint64_t privilege_mask;
48         const char *name;
49         const char *description;
50 } privs[] = {
51
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"},
57
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
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
66          * 2008 R2.
67
68            Finally, the parameter 'short_list' determines if the short
69            or full list (including many other privileges) is used */
70
71         {SEC_PRIV_SECURITY,
72          SEC_PRIV_SECURITY_BIT,
73          "SeSecurityPrivilege",
74         "System security"},
75
76         {SEC_PRIV_SYSTEMTIME,
77          SEC_PRIV_SYSTEMTIME_BIT,
78          "SeSystemtimePrivilege",
79         "Set the system clock"},
80
81         {SEC_PRIV_SHUTDOWN,
82          SEC_PRIV_SHUTDOWN_BIT,
83          "SeShutdownPrivilege",
84         "Shutdown the system"},
85
86         {SEC_PRIV_DEBUG,
87          SEC_PRIV_DEBUG_BIT,
88          "SeDebugPrivilege",
89         "Debug processes"},
90
91         {SEC_PRIV_SYSTEM_ENVIRONMENT,
92          SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
93          "SeSystemEnvironmentPrivilege",
94         "Modify system environment"},
95
96         {SEC_PRIV_SYSTEM_PROFILE,
97          SEC_PRIV_SYSTEM_PROFILE_BIT,
98          "SeSystemProfilePrivilege",
99         "Profile the system"},
100
101         {SEC_PRIV_PROFILE_SINGLE_PROCESS,
102          SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
103          "SeProfileSingleProcessPrivilege",
104         "Profile one process"},
105
106         {SEC_PRIV_INCREASE_BASE_PRIORITY,
107          SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
108          "SeIncreaseBasePriorityPrivilege",
109          "Increase base priority"},
110
111         {SEC_PRIV_LOAD_DRIVER,
112          SEC_PRIV_LOAD_DRIVER_BIT,
113          "SeLoadDriverPrivilege",
114         "Load drivers"},
115
116         {SEC_PRIV_CREATE_PAGEFILE,
117          SEC_PRIV_CREATE_PAGEFILE_BIT,
118          "SeCreatePagefilePrivilege",
119         "Create page files"},
120
121         {SEC_PRIV_INCREASE_QUOTA,
122          SEC_PRIV_INCREASE_QUOTA_BIT,
123          "SeIncreaseQuotaPrivilege",
124         "Increase quota"},
125
126         {SEC_PRIV_CHANGE_NOTIFY,
127          SEC_PRIV_CHANGE_NOTIFY_BIT,
128          "SeChangeNotifyPrivilege",
129         "Register for change notify"},
130
131         {SEC_PRIV_UNDOCK,
132          SEC_PRIV_UNDOCK_BIT,
133          "SeUndockPrivilege",
134         "Undock devices"},
135
136         {SEC_PRIV_MANAGE_VOLUME,
137          SEC_PRIV_MANAGE_VOLUME_BIT,
138          "SeManageVolumePrivilege",
139         "Manage system volumes"},
140
141         {SEC_PRIV_IMPERSONATE,
142          SEC_PRIV_IMPERSONATE_BIT,
143          "SeImpersonatePrivilege",
144         "Impersonate users"},
145
146         {SEC_PRIV_CREATE_GLOBAL,
147          SEC_PRIV_CREATE_GLOBAL_BIT,
148          "SeCreateGlobalPrivilege",
149         "Create global"},
150
151         {SEC_PRIV_ENABLE_DELEGATION,
152          SEC_PRIV_ENABLE_DELEGATION_BIT,
153          "SeEnableDelegationPrivilege",
154         "Enable Delegation"},
155
156         {SEC_PRIV_INTERACTIVE_LOGON,
157          SEC_PRIV_INTERACTIVE_LOGON_BIT,
158          "SeInteractiveLogonRight",
159         "Interactive logon"},
160
161         {SEC_PRIV_NETWORK_LOGON,
162          SEC_PRIV_NETWORK_LOGON_BIT,
163          "SeNetworkLogonRight",
164         "Network logon"},
165
166         {SEC_PRIV_REMOTE_INTERACTIVE_LOGON,
167          SEC_PRIV_REMOTE_INTERACTIVE_LOGON_BIT,
168          "SeRemoteInteractiveLogonRight",
169         "Remote Interactive logon"}
170 };
171
172 /*
173   return a privilege mask given a privilege id
174 */
175 uint64_t sec_privilege_mask(enum sec_privilege privilege)
176 {
177         int i;
178         for (i=0;i<ARRAY_SIZE(privs);i++) {
179                 if (privs[i].luid == privilege) {
180                         return privs[i].privilege_mask;
181                 }
182         }
183
184         return 0;
185 }
186
187 /***************************************************************************
188  put all valid privileges into a mask
189 ****************************************************************************/
190
191 void se_priv_put_all_privileges(uint64_t *privilege_mask)
192 {
193         int i;
194         uint32_t num_privs = ARRAY_SIZE(privs);
195
196         *privilege_mask = 0;
197         for ( i=0; i<num_privs; i++ ) {
198                 *privilege_mask |= privs[i].privilege_mask;
199         }
200 }
201
202 /*********************************************************************
203  Lookup the uint64_t bitmask value for a privilege name
204 *********************************************************************/
205
206 bool se_priv_from_name( const char *name, uint64_t *privilege_mask )
207 {
208         int i;
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;
213                         return true;
214                 }
215         }
216
217         return false;
218 }
219
220 const char* get_privilege_dispname( const char *name )
221 {
222         int i;
223
224         uint32_t num_privs = ARRAY_SIZE(privs);
225
226         if (!name) {
227                 return NULL;
228         }
229
230         for ( i=0; i<num_privs; i++ ) {
231                 if ( strequal( privs[i].name, name ) ) {
232                         return privs[i].description;
233                 }
234         }
235
236         return NULL;
237 }
238
239 /*******************************************************************
240  return the number of elements in the 'short' privlege array (traditional source3 behaviour)
241 *******************************************************************/
242
243 int num_privileges_in_short_list( void )
244 {
245         return NUM_SHORT_LIST_PRIVS;
246 }
247
248 /****************************************************************************
249  add a privilege to a privilege array
250  ****************************************************************************/
251
252 static bool privilege_set_add(PRIVILEGE_SET *priv_set, struct lsa_LUIDAttribute set)
253 {
254         struct lsa_LUIDAttribute *new_set;
255
256         /* we can allocate memory to add the new privilege */
257
258         new_set = talloc_realloc(priv_set->mem_ctx, priv_set->set, struct lsa_LUIDAttribute, priv_set->count + 1);
259         if ( !new_set ) {
260                 DEBUG(0,("privilege_set_add: failed to allocate memory!\n"));
261                 return false;
262         }
263
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;
267
268         priv_set->count++;
269         priv_set->set = new_set;
270
271         return true;
272 }
273
274 /*******************************************************************
275 *******************************************************************/
276
277 bool se_priv_to_privilege_set( PRIVILEGE_SET *set, uint64_t privilege_mask )
278 {
279         int i;
280         uint32_t num_privs = ARRAY_SIZE(privs);
281         struct lsa_LUIDAttribute luid;
282
283         luid.attribute = 0;
284         luid.luid.high = 0;
285
286         for ( i=0; i<num_privs; i++ ) {
287                 if ((privilege_mask & privs[i].privilege_mask) == 0)
288                         continue;
289
290                 luid.luid.high = 0;
291                 luid.luid.low = privs[i].luid;
292
293                 if ( !privilege_set_add( set, luid ) )
294                         return false;
295         }
296
297         return true;
298 }
299
300 /*******************************************************************
301 *******************************************************************/
302
303 bool privilege_set_to_se_priv( uint64_t *privilege_mask, struct lsa_PrivilegeSet *privset )
304 {
305         int i;
306
307         ZERO_STRUCTP( privilege_mask );
308
309         for ( i=0; i<privset->count; i++ ) {
310                 uint64_t r;
311
312                 /* sanity check for invalid privilege.  we really
313                    only care about the low 32 bits */
314
315                 if ( privset->set[i].luid.high != 0 )
316                         return false;
317
318                 r = sec_privilege_mask(privset->set[i].luid.low);
319                 if (r) {
320                         *privilege_mask |= r;
321                 }
322         }
323
324         return true;
325 }
326
327 /*
328   map a privilege id to the wire string constant
329 */
330 const char *sec_privilege_name(enum sec_privilege privilege)
331 {
332         int i;
333         for (i=0;i<ARRAY_SIZE(privs);i++) {
334                 if (privs[i].luid == privilege) {
335                         return privs[i].name;
336                 }
337         }
338         return NULL;
339 }
340
341 /*
342   map a privilege id to a privilege display name. Return NULL if not found
343
344   TODO: this should use language mappings
345 */
346 const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
347 {
348         int i;
349         for (i=0;i<ARRAY_SIZE(privs);i++) {
350                 if (privs[i].luid == privilege) {
351                         return privs[i].description;
352                 }
353         }
354         return NULL;
355 }
356
357 /*
358   map a privilege name to a privilege id. Return -1 if not found
359 */
360 enum sec_privilege sec_privilege_id(const char *name)
361 {
362         int i;
363         for (i=0;i<ARRAY_SIZE(privs);i++) {
364                 if (strcasecmp(privs[i].name, name) == 0) {
365                         return privs[i].luid;
366                 }
367         }
368         return SEC_PRIV_INVALID;
369 }
370
371 /*
372   assist in walking the table of privileges - return the LUID (low 32 bits) by index
373 */
374 enum sec_privilege sec_privilege_from_index(int idx)
375 {
376         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
377                 return privs[idx].luid;
378         }
379         return SEC_PRIV_INVALID;
380 }
381
382 /*
383   assist in walking the table of privileges - return the string constant by index
384 */
385 const char *sec_privilege_name_from_index(int idx)
386 {
387         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
388                 return privs[idx].name;
389         }
390         return NULL;
391 }
392
393
394
395 /*
396   return true if a security_token has a particular privilege bit set
397 */
398 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
399 {
400         uint64_t mask;
401
402         mask = sec_privilege_mask(privilege);
403         if (mask == 0) {
404                 return false;
405         }
406
407         if (token->privilege_mask & mask) {
408                 return true;
409         }
410         return false;
411 }
412
413 /*
414   set a bit in the privilege mask
415 */
416 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
417 {
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);
420 }
421
422 void security_token_debug_privileges(int dbg_lev, const struct security_token *token)
423 {
424         DEBUGADD(dbg_lev, (" Privileges (0x%16llX):\n",
425                             (unsigned long long) token->privilege_mask));
426
427         if (token->privilege_mask) {
428                 int idx = 0;
429                 int i = 0;
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++,
433                                                    privs[idx].name));
434                         }
435                 }
436         }
437 }