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
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/privileges.h"
26 #include "dbwrap/dbwrap.h"
27 #include "libcli/security/privileges_private.h"
28 #include "../libcli/security/security.h"
31 #define PRIVPREFIX "PRIV_"
45 interpret an old style SE_PRIV structure
47 static uint64_t map_old_SE_PRIV(unsigned char *dptr)
49 uint32_t *old_masks = (uint32_t *)dptr;
51 * the old privileges code only ever used up to 0x800, except
52 * for a special case of 'SE_ALL_PRIVS' which was 0xFFFFFFFF
54 if (old_masks[0] == 0xFFFFFFFF) {
55 /* they set all privileges */
59 /* the old code used the machine byte order, but we don't know
60 * the byte order of the machine that wrote it. However we can
61 * tell what byte order it was by taking advantage of the fact
62 * that it only ever use up to 0x800
64 if (dptr[0] || dptr[1]) {
65 /* it was little endian */
69 /* it was either zero or big-endian */
70 return RIVAL(dptr, 0);
74 static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
76 struct db_context *db = get_account_pol_db();
77 struct dom_sid_buf tmp;
82 /* Fail if the admin has not enable privileges */
84 if ( !lp_enable_privileges() ) {
91 /* PRIV_<SID> (NULL terminated) as the key */
93 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
95 status = dbwrap_fetch_bystring(db, talloc_tos(), keystr, &data);
97 if (!NT_STATUS_IS_OK(status)) {
98 DEBUG(4, ("get_privileges: No privileges assigned to SID "
99 "[%s]\n", sid_string_dbg(sid)));
103 if (data.dsize == 4*4) {
104 /* it's an old style SE_PRIV structure. */
105 *mask = map_old_SE_PRIV(data.dptr);
107 if (data.dsize != sizeof( uint64_t ) ) {
108 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
109 "[%s]\n", sid_string_dbg(sid)));
113 *mask = BVAL(data.dptr, 0);
116 TALLOC_FREE(data.dptr);
121 /***************************************************************************
122 Store the privilege mask (set) for a given SID
123 ****************************************************************************/
125 static bool set_privileges( const struct dom_sid *sid, uint64_t mask )
127 struct db_context *db = get_account_pol_db();
129 struct dom_sid_buf tmp;
133 if ( !lp_enable_privileges() )
139 if ( !sid || (sid->num_auths == 0) ) {
140 DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
144 /* PRIV_<SID> (NULL terminated) as the key */
146 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
148 /* This writes the 64 bit bitmask out in little endian format */
149 SBVAL(privbuf,0,mask);
152 data.dsize = sizeof(privbuf);
154 return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
158 /*********************************************************************
159 get a list of all privileges for all sids in the list
160 *********************************************************************/
162 bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
170 for ( i=0; i<scount; i++ ) {
171 /* don't add unless we actually have a privilege assigned */
173 if ( !get_privileges( &slist[i], &mask ) )
176 DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
177 "set: 0x%llx\n", sid_string_dbg(&slist[i]),
178 (unsigned long long)mask));
187 NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
190 if (!get_privileges(sid, &mask)) {
191 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
194 *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
196 return NT_STATUS_NO_MEMORY;
199 if (!se_priv_to_privilege_set(*privileges, mask)) {
200 return NT_STATUS_NO_MEMORY;
205 /*********************************************************************
206 traversal functions for privilege_enumerate_accounts
207 *********************************************************************/
209 static int priv_traverse_fn(struct db_record *rec, void *state)
211 PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
212 int prefixlen = strlen(PRIVPREFIX);
217 key = dbwrap_record_get_key(rec);
219 /* check we have a PRIV_+SID entry */
221 if (strncmp((char *)key.dptr, PRIVPREFIX, prefixlen) != 0)
224 /* check to see if we are looking for a particular privilege */
226 fstrcpy( sid_string, (char *)&(key.dptr[strlen(PRIVPREFIX)]) );
228 if (priv->privilege != 0) {
232 value = dbwrap_record_get_value(rec);
234 if (value.dsize == 4*4) {
235 mask = map_old_SE_PRIV(value.dptr);
237 if (value.dsize != sizeof( uint64_t ) ) {
238 DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
239 "[%s]\n", sid_string));
242 mask = BVAL(value.dptr, 0);
245 /* if the SID does not have the specified privilege
248 if ((mask & priv->privilege) == 0) {
253 /* this is a last ditch safety check to preventing returning
254 and invalid SID (i've somehow run into this on development branches) */
256 if ( strcmp( "S-0-0", sid_string ) == 0 )
259 if ( !string_to_sid(&sid, sid_string) ) {
260 DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
265 if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
275 /*********************************************************************
276 Retrieve list of privileged SIDs (for _lsa_enumerate_accounts()
277 *********************************************************************/
279 NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
281 struct db_context *db = get_account_pol_db();
286 return NT_STATUS_ACCESS_DENIED;
291 status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
292 if (!NT_STATUS_IS_OK(status)) {
296 /* give the memory away; caller will free */
298 *sids = priv.sids.list;
299 *num_sids = priv.sids.count;
304 /*********************************************************************
305 Retrieve list of SIDs granted a particular privilege
306 *********************************************************************/
308 NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
309 struct dom_sid **sids, int *num_sids)
311 struct db_context *db = get_account_pol_db();
316 return NT_STATUS_ACCESS_DENIED;
321 priv.privilege = sec_privilege_mask(privilege);
322 priv.mem_ctx = mem_ctx;
324 status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
325 if (!NT_STATUS_IS_OK(status)) {
329 /* give the memory away; caller will free */
331 *sids = priv.sids.list;
332 *num_sids = priv.sids.count;
337 /***************************************************************************
339 ****************************************************************************/
341 static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
343 uint64_t old_mask, new_mask;
345 ZERO_STRUCT( old_mask );
346 ZERO_STRUCT( new_mask );
348 if ( get_privileges( sid, &old_mask ) )
353 new_mask |= priv_mask;
355 DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
357 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
359 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)new_mask));
361 return set_privileges( sid, new_mask );
364 /*********************************************************************
365 Add a privilege based on its name
366 *********************************************************************/
368 bool grant_privilege_by_name(const struct dom_sid *sid, const char *name)
372 if (! se_priv_from_name(name, &mask)) {
373 DEBUG(3, ("grant_privilege_by_name: "
374 "No Such Privilege Found (%s)\n", name));
378 return grant_privilege_bitmap( sid, mask );
381 /***************************************************************************
382 Grant a privilege set (list of LUID values) from a sid
383 ****************************************************************************/
385 bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
387 uint64_t privilege_mask;
388 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
391 return grant_privilege_bitmap(sid, privilege_mask);
394 /***************************************************************************
395 Remove privilege from sid
396 ****************************************************************************/
398 static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
402 /* if the user has no privileges, then we can't revoke any */
404 if ( !get_privileges( sid, &mask ) )
407 DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
409 DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
413 DEBUGADD( 10, ("new privilege mask: 0x%llx\n", (unsigned long long)mask));
415 return set_privileges( sid, mask );
418 /***************************************************************************
419 Remove a privilege set (list of LUID values) from a sid
420 ****************************************************************************/
422 bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
424 uint64_t privilege_mask;
425 if (!privilege_set_to_se_priv(&privilege_mask, set)) {
428 return revoke_privilege_bitmap(sid, privilege_mask);
431 /*********************************************************************
432 Revoke all privileges
433 *********************************************************************/
435 bool revoke_all_privileges( const struct dom_sid *sid )
437 return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
440 /*********************************************************************
441 Add a privilege based on its name
442 *********************************************************************/
444 bool revoke_privilege_by_name(const struct dom_sid *sid, const char *name)
448 if (! se_priv_from_name(name, &mask)) {
449 DEBUG(3, ("revoke_privilege_by_name: "
450 "No Such Privilege Found (%s)\n", name));
454 return revoke_privilege_bitmap(sid, mask);
458 /***************************************************************************
459 Retrieve the SIDs assigned to a given privilege
460 ****************************************************************************/
462 NTSTATUS privilege_create_account(const struct dom_sid *sid )
464 return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
467 /***************************************************************************
468 Delete a privileged account
469 ****************************************************************************/
471 NTSTATUS privilege_delete_account(const struct dom_sid *sid)
473 struct db_context *db = get_account_pol_db();
474 struct dom_sid_buf tmp;
477 if (!lp_enable_privileges()) {
482 return NT_STATUS_INVALID_HANDLE;
485 if (!sid || (sid->num_auths == 0)) {
486 return NT_STATUS_INVALID_SID;
489 /* PRIV_<SID> (NULL terminated) as the key */
491 fstr_sprintf(keystr, "%s%s", PRIVPREFIX, dom_sid_str_buf(sid, &tmp));
493 return dbwrap_delete_bystring(db, keystr);
496 /*******************************************************************
497 *******************************************************************/
499 bool is_privileged_sid( const struct dom_sid *sid )
503 return get_privileges( sid, &mask );
506 /*******************************************************************
507 *******************************************************************/
509 bool grant_all_privileges( const struct dom_sid *sid )
513 se_priv_put_all_privileges(&mask);
515 return grant_privilege_bitmap( sid, mask );