2b241c91aade549cadece0dcf415cac4abf0c68c
[abartlet/samba.git/.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 "librpc/gen_ndr/security.h"
38
39 /* The use of strcasecmp here is safe, all the comparison strings are ASCII */
40 #undef strcasecmp
41
42 #define NUM_SHORT_LIST_PRIVS 8
43
44 static const struct {
45         enum sec_privilege luid;
46         uint64_t privilege_mask;
47         const char *name;
48         const char *description;
49 } privs[] = {
50
51         {SEC_PRIV_MACHINE_ACCOUNT, SEC_PRIV_MACHINE_ACCOUNT_BIT,   "SeMachineAccountPrivilege", "Add machines to domain"},
52         {SEC_PRIV_TAKE_OWNERSHIP,  SEC_PRIV_TAKE_OWNERSHIP_BIT,    "SeTakeOwnershipPrivilege",    "Take ownership of files or other objects"},
53         {SEC_PRIV_BACKUP,          SEC_PRIV_BACKUP_BIT,            "SeBackupPrivilege",           "Back up files and directories"},
54         {SEC_PRIV_RESTORE,         SEC_PRIV_RESTORE_BIT,           "SeRestorePrivilege",          "Restore files and directories"},
55         {SEC_PRIV_REMOTE_SHUTDOWN, SEC_PRIV_REMOTE_SHUTDOWN_BIT,   "SeRemoteShutdownPrivilege", "Force shutdown from a remote system"},
56
57         {SEC_PRIV_PRINT_OPERATOR,  SEC_PRIV_PRINT_OPERATOR_BIT,  "SePrintOperatorPrivilege",    "Manage printers"},
58         {SEC_PRIV_ADD_USERS,       SEC_PRIV_ADD_USERS_BIT,       "SeAddUsersPrivilege",         "Add users and groups to the domain"},
59         {SEC_PRIV_DISK_OPERATOR,   SEC_PRIV_DISK_OPERATOR_BIT,   "SeDiskOperatorPrivilege",     "Manage disk shares"},
60
61         /* The list from here on was not displayed in the code from
62          * source3/ with the comment that usrmgr will display these
63          * next 2 twice if you include them.  The source4/ code has
64          * always included them, but they do not appear in Windows
65          * 2008 R2.
66
67            Finally, the parameter 'short_list' determines if the short
68            or full list (including many other privileges) is used */
69
70         {SEC_PRIV_SECURITY,
71          SEC_PRIV_SECURITY_BIT,
72          "SeSecurityPrivilege",
73         "System security"},
74
75         {SEC_PRIV_SYSTEMTIME,
76          SEC_PRIV_SYSTEMTIME_BIT,
77          "SeSystemtimePrivilege",
78         "Set the system clock"},
79
80         {SEC_PRIV_SHUTDOWN,
81          SEC_PRIV_SHUTDOWN_BIT,
82          "SeShutdownPrivilege",
83         "Shutdown the system"},
84
85         {SEC_PRIV_DEBUG,
86          SEC_PRIV_DEBUG_BIT,
87          "SeDebugPrivilege",
88         "Debug processes"},
89
90         {SEC_PRIV_SYSTEM_ENVIRONMENT,
91          SEC_PRIV_SYSTEM_ENVIRONMENT_BIT,
92          "SeSystemEnvironmentPrivilege",
93         "Modify system environment"},
94
95         {SEC_PRIV_SYSTEM_PROFILE,
96          SEC_PRIV_SYSTEM_PROFILE_BIT,
97          "SeSystemProfilePrivilege",
98         "Profile the system"},
99
100         {SEC_PRIV_PROFILE_SINGLE_PROCESS,
101          SEC_PRIV_PROFILE_SINGLE_PROCESS_BIT,
102          "SeProfileSingleProcessPrivilege",
103         "Profile one process"},
104
105         {SEC_PRIV_INCREASE_BASE_PRIORITY,
106          SEC_PRIV_INCREASE_BASE_PRIORITY_BIT,
107          "SeIncreaseBasePriorityPrivilege",
108          "Increase base priority"},
109
110         {SEC_PRIV_LOAD_DRIVER,
111          SEC_PRIV_LOAD_DRIVER_BIT,
112          "SeLoadDriverPrivilege",
113         "Load drivers"},
114
115         {SEC_PRIV_CREATE_PAGEFILE,
116          SEC_PRIV_CREATE_PAGEFILE_BIT,
117          "SeCreatePagefilePrivilege",
118         "Create page files"},
119
120         {SEC_PRIV_INCREASE_QUOTA,
121          SEC_PRIV_INCREASE_QUOTA_BIT,
122          "SeIncreaseQuotaPrivilege",
123         "Increase quota"},
124
125         {SEC_PRIV_CHANGE_NOTIFY,
126          SEC_PRIV_CHANGE_NOTIFY_BIT,
127          "SeChangeNotifyPrivilege",
128         "Register for change notify"},
129
130         {SEC_PRIV_UNDOCK,
131          SEC_PRIV_UNDOCK_BIT,
132          "SeUndockPrivilege",
133         "Undock devices"},
134
135         {SEC_PRIV_MANAGE_VOLUME,
136          SEC_PRIV_MANAGE_VOLUME_BIT,
137          "SeManageVolumePrivilege",
138         "Manage system volumes"},
139
140         {SEC_PRIV_IMPERSONATE,
141          SEC_PRIV_IMPERSONATE_BIT,
142          "SeImpersonatePrivilege",
143         "Impersonate users"},
144
145         {SEC_PRIV_CREATE_GLOBAL,
146          SEC_PRIV_CREATE_GLOBAL_BIT,
147          "SeCreateGlobalPrivilege",
148         "Create global"},
149
150         {SEC_PRIV_ENABLE_DELEGATION,
151          SEC_PRIV_ENABLE_DELEGATION_BIT,
152          "SeEnableDelegationPrivilege",
153         "Enable Delegation"},
154
155         {SEC_PRIV_INTERACTIVE_LOGON,
156          SEC_PRIV_INTERACTIVE_LOGON_BIT,
157          "SeInteractiveLogonRight",
158         "Interactive logon"},
159
160         {SEC_PRIV_NETWORK_LOGON,
161          SEC_PRIV_NETWORK_LOGON_BIT,
162          "SeNetworkLogonRight",
163         "Network logon"},
164
165         {SEC_PRIV_REMOTE_INTERACTIVE_LOGON,
166          SEC_PRIV_REMOTE_INTERACTIVE_LOGON_BIT,
167          "SeRemoteInteractiveLogonRight",
168         "Remote Interactive logon"}
169 };
170
171 /*
172   return a privilege mask given a privilege id
173 */
174 static uint64_t sec_privilege_mask(enum sec_privilege privilege)
175 {
176         int i;
177         for (i=0;i<ARRAY_SIZE(privs);i++) {
178                 if (privs[i].luid == privilege) {
179                         return privs[i].privilege_mask;
180                 }
181         }
182
183         return 0;
184 }
185
186 /***************************************************************************
187  put all valid privileges into a mask
188 ****************************************************************************/
189
190 bool se_priv_put_all_privileges(uint64_t *privilege_mask)
191 {
192         int i;
193         uint32_t num_privs = ARRAY_SIZE(privs);
194
195         *privilege_mask = 0;
196         for ( i=0; i<num_privs; i++ ) {
197                 *privilege_mask |= privs[i].privilege_mask;
198         }
199         return true;
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         if (privilege < 1 || privilege > 64) {
350                 return NULL;
351         }
352         for (i=0;i<ARRAY_SIZE(privs);i++) {
353                 if (privs[i].luid == privilege) {
354                         return privs[i].description;
355                 }
356         }
357         return NULL;
358 }
359
360 /*
361   map a privilege name to a privilege id. Return -1 if not found
362 */
363 enum sec_privilege sec_privilege_id(const char *name)
364 {
365         int i;
366         for (i=0;i<ARRAY_SIZE(privs);i++) {
367                 if (strcasecmp(privs[i].name, name) == 0) {
368                         return privs[i].luid;
369                 }
370         }
371         return -1;
372 }
373
374 /*
375   assist in walking the table of privileges - return the LUID (low 32 bits) by index
376 */
377 enum sec_privilege sec_privilege_from_index(int idx)
378 {
379         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
380                 return privs[idx].luid;
381         }
382         return -1;
383 }
384
385 /*
386   assist in walking the table of privileges - return the string constant by index
387 */
388 const char *sec_privilege_name_from_index(int idx)
389 {
390         if (idx >= 0 && idx<ARRAY_SIZE(privs)) {
391                 return privs[idx].name;
392         }
393         return NULL;
394 }
395
396
397
398 /*
399   return true if a security_token has a particular privilege bit set
400 */
401 bool security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
402 {
403         uint64_t mask;
404
405         mask = sec_privilege_mask(privilege);
406         if (mask == 0) {
407                 return false;
408         }
409
410         if (token->privilege_mask & mask) {
411                 return true;
412         }
413         return false;
414 }
415
416 /*
417   set a bit in the privilege mask
418 */
419 void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
420 {
421         /* Relies on the fact that an invalid privilage will return 0, so won't change this */
422         token->privilege_mask |= sec_privilege_mask(privilege);
423 }
424
425 void security_token_debug_privileges(int dbg_lev, const struct security_token *token)
426 {
427         DEBUGADD(dbg_lev, (" Privileges (0x%16llX):\n",
428                             (unsigned long long) token->privilege_mask));
429
430         if (token->privilege_mask) {
431                 int idx = 0;
432                 int i = 0;
433                 for (idx = 0; idx<ARRAY_SIZE(privs); idx++) {
434                         if (token->privilege_mask & privs[idx].privilege_mask) {
435                                 DEBUGADD(dbg_lev, ("  Privilege[%3lu]: %s\n", (unsigned long)i++,
436                                                    privs[idx].name));
437                         }
438                 }
439         }
440 }