s3-vfstest: Return an error code if cmd failed
[nivanova/samba-autobuild/.git] / source3 / lib / privileges.c
index 1c23d9e40e59f07153453d8bfda448f70d3987bf..19ee688042098785d04ec5c672218039630d61eb 100644 (file)
 /*
    Unix SMB/CIFS implementation.
    Privileges handling functions
-   Copyright (C) Jean François Micouleau       1998-2001
+   Copyright (C) Jean François Micouleau      1998-2001
    Copyright (C) Simo Sorce                    2002-2003
-   
+   Copyright (C) Gerald (Jerry) Carter          2005
+   Copyright (C) Michael Adam                  2007
+
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
-   
+
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
-   
+
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+
 #include "includes.h"
+#include "lib/privileges.h"
+#include "dbwrap/dbwrap.h"
+#include "libcli/security/privileges_private.h"
+#include "../libcli/security/security.h"
+#include "passdb.h"
 
-/* defines */
+#define PRIVPREFIX              "PRIV_"
 
-#define ALLOC_CHECK(ptr, err, label, str) do { if ((ptr) == NULL) { DEBUG(0, ("%s: out of memory!\n", str)); err = NT_STATUS_NO_MEMORY; goto label; } } while(0)
-#define NTSTATUS_CHECK(err, label, str1, str2) do { if (!NT_STATUS_IS_OK(err)) { DEBUG(0, ("%s: %s failed!\n", str1, str2)); } } while(0)
+typedef struct {
+       uint32_t count;
+       struct dom_sid *list;
+} SID_LIST;
+
+typedef struct {
+       TALLOC_CTX *mem_ctx;
+       uint64_t privilege;
+       SID_LIST sids;
+} PRIV_SID_LIST;
+
+/*
+  interpret an old style SE_PRIV structure
+ */
+static uint64_t map_old_SE_PRIV(unsigned char *dptr)
+{
+       uint32_t *old_masks = (uint32_t *)dptr;
+       /*
+        * the old privileges code only ever used up to 0x800, except
+        * for a special case of 'SE_ALL_PRIVS' which was 0xFFFFFFFF
+        */
+       if (old_masks[0] == 0xFFFFFFFF) {
+               /* they set all privileges */
+               return SE_ALL_PRIVS;
+       }
 
-/****************************************************************************
- Check if a user is a mapped group.
+       /* the old code used the machine byte order, but we don't know
+        * the byte order of the machine that wrote it. However we can
+        * tell what byte order it was by taking advantage of the fact
+        * that it only ever use up to 0x800
+        */
+       if (dptr[0] || dptr[1]) {
+               /* it was little endian */
+               return IVAL(dptr, 0);
+       }
 
-   This function will check if the group SID is mapped onto a
-   system managed gid or onto a winbind manged sid.
-   In the first case it will be threated like a mapped group
-   and the backend should take the member list with a getgrgid
-   and ignore any user that have been possibly set into the group
-   object.
+       /* it was either zero or big-endian */
+       return RIVAL(dptr, 0);
+}
 
-   In the second case, the group is a fully SAM managed group
-   served back to the system through winbind. In this case the
-   members of a Local group are "unrolled" to cope with the fact
-   that unix cannot contain groups inside groups.
-   The backend MUST never call any getgr* / getpw* function or
-   loops with winbind may happen. 
- ****************************************************************************/
 
-#if 0
-NTSTATUS is_mapped_group(BOOL *mapped, const DOM_SID *sid)
+static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
 {
-       NTSTATUS result;
-       gid_t id;
+       struct db_context *db = get_account_pol_db();
+       fstring tmp, keystr;
+       TDB_DATA data;
+       NTSTATUS status;
+
+       /* Fail if the admin has not enable privileges */
+
+       if ( !lp_enable_privileges() ) {
+               return False;
+       }
 
-       /* look if mapping exist, do not make idmap alloc an uid if SID is not found */
-       result = idmap_get_gid_from_sid(&id, sid, False);
-       if (NT_STATUS_IS_OK(result)) {
-               *mapped = gid_is_in_winbind_range(id);
+       if ( db == NULL )
+               return False;
+
+       /* PRIV_<SID> (NULL terminated) as the key */
+
+       fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
+
+       status = dbwrap_fetch_bystring(db, talloc_tos(), keystr, &data);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               DEBUG(4, ("get_privileges: No privileges assigned to SID "
+                         "[%s]\n", sid_string_dbg(sid)));
+               return False;
+       }
+
+       if (data.dsize == 4*4) {
+               /* it's an old style SE_PRIV structure. */
+               *mask = map_old_SE_PRIV(data.dptr);
        } else {
-               *mapped = False;
+               if (data.dsize != sizeof( uint64_t ) ) {
+                       DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
+                                 "[%s]\n", sid_string_dbg(sid)));
+                       return False;
+               }
+
+               *mask = BVAL(data.dptr, 0);
        }
 
-       return result;
+       TALLOC_FREE(data.dptr);
+
+       return True;
 }
-#endif
 
-/****************************************************************************
- duplicate alloc luid_attr
- ****************************************************************************/
-NTSTATUS dupalloc_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la)
+/***************************************************************************
+ Store the privilege mask (set) for a given SID
+****************************************************************************/
+
+static bool set_privileges( const struct dom_sid *sid, uint64_t mask )
 {
-       NTSTATUS ret;
+       struct db_context *db = get_account_pol_db();
+       uint8_t privbuf[8];
+       fstring tmp, keystr;
+       TDB_DATA data;
 
-       *new_la = (LUID_ATTR *)talloc(mem_ctx, sizeof(LUID_ATTR));
-       ALLOC_CHECK(new_la, ret, done, "dupalloc_luid_attr");
+       if ( !lp_enable_privileges() )
+               return False;
 
-       (*new_la)->luid.high = old_la->luid.high;
-       (*new_la)->luid.low = old_la->luid.low;
-       (*new_la)->attr = old_la->attr;
-       
-       ret = NT_STATUS_OK;
+       if ( db == NULL )
+               return False;
 
-done:
-       return ret;
-}
+       if ( !sid || (sid->num_auths == 0) ) {
+               DEBUG(0,("set_privileges: Refusing to store empty SID!\n"));
+               return False;
+       }
 
-/****************************************************************************
- initialise a privilege list
- ****************************************************************************/
-NTSTATUS init_privilege(PRIVILEGE_SET **priv_set)
-{
-       NTSTATUS ret;
-       TALLOC_CTX *mem_ctx = talloc_init("privilege set");
-       ALLOC_CHECK(mem_ctx, ret, done, "init_privilege");
+       /* PRIV_<SID> (NULL terminated) as the key */
 
-       *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
-       ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
+       fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
 
-       (*priv_set)->mem_ctx = mem_ctx;
+       /* This writes the 64 bit bitmask out in little endian format */
+       SBVAL(privbuf,0,mask);
 
-       ret = NT_STATUS_OK;
+       data.dptr  = privbuf;
+       data.dsize = sizeof(privbuf);
 
-done:
-       return ret;
+       return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
+                                                    TDB_REPLACE));
 }
 
-NTSTATUS init_priv_with_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **priv_set)
+/*********************************************************************
+ get a list of all privileges for all sids in the list
+*********************************************************************/
+
+bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
 {
-       NTSTATUS ret;
+       uint64_t mask;
+       int i;
+       bool found = False;
 
-       *priv_set = talloc_zero(mem_ctx, sizeof(PRIVILEGE_SET));
-       ALLOC_CHECK(*priv_set, ret, done, "init_privilege");
+       *privileges = 0;
 
-       (*priv_set)->mem_ctx = mem_ctx;
-       (*priv_set)->ext_ctx = True;
+       for ( i=0; i<scount; i++ ) {
+               /* don't add unless we actually have a privilege assigned */
 
-       ret = NT_STATUS_OK;
+               if ( !get_privileges( &slist[i], &mask ) )
+                       continue;
 
-done:
-       return ret;
-}
+               DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
+                        "set: 0x%llx\n", sid_string_dbg(&slist[i]),
+                        (unsigned long long)mask));
 
-void reset_privilege(PRIVILEGE_SET *priv_set)
-{
-       priv_set->count = 0;
-       priv_set->control = 0;
-       priv_set->set = NULL;
+               *privileges |= mask;
+               found = True;
+       }
+
+       return found;
 }
 
-void destroy_privilege(PRIVILEGE_SET **priv_set)
+NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
 {
-       reset_privilege(*priv_set);
-       if (!((*priv_set)->ext_ctx))
-               /* mem_ctx is local, destroy it */
-               talloc_destroy((*priv_set)->mem_ctx);
-       *priv_set = NULL;
+       uint64_t mask;
+       if (!get_privileges(sid, &mask)) {
+               return NT_STATUS_OBJECT_NAME_NOT_FOUND;
+       }
+
+       *privileges = talloc_zero(mem_ctx, PRIVILEGE_SET);
+       if (!*privileges) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       if (!se_priv_to_privilege_set(*privileges, mask)) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
 }
 
-/****************************************************************************
- add a privilege to a privilege array
- ****************************************************************************/
-NTSTATUS add_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
+/*********************************************************************
+ traversal functions for privilege_enumerate_accounts
+*********************************************************************/
+
+static int priv_traverse_fn(struct db_record *rec, void *state)
 {
-       NTSTATUS ret;
-       LUID_ATTR *new_set;
+       PRIV_SID_LIST *priv = (PRIV_SID_LIST *)state;
+       int  prefixlen = strlen(PRIVPREFIX);
+       struct dom_sid sid;
+       fstring sid_string;
+       TDB_DATA key;
+
+       key = dbwrap_record_get_key(rec);
+
+       /* check we have a PRIV_+SID entry */
 
-       /* check if the privilege is not already in the list */
-       if (NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
-               return NT_STATUS_UNSUCCESSFUL;
+       if (strncmp((char *)key.dptr, PRIVPREFIX, prefixlen) != 0)
+               return 0;
 
-       /* we can allocate memory to add the new privilege */
+       /* check to see if we are looking for a particular privilege */
 
-       new_set = (LUID_ATTR *)talloc_realloc(priv_set->mem_ctx, priv_set->set, (priv_set->count + 1) * (sizeof(LUID_ATTR)));
-       ALLOC_CHECK(new_set, ret, done, "add_privilege");
+       fstrcpy( sid_string, (char *)&(key.dptr[strlen(PRIVPREFIX)]) );
 
-       new_set[priv_set->count].luid.high = set.luid.high;
-       new_set[priv_set->count].luid.low = set.luid.low;
-       new_set[priv_set->count].attr = set.attr;
+       if (priv->privilege != 0) {
+               uint64_t mask;
+               TDB_DATA value;
 
-       priv_set->count++;
-       priv_set->set = new_set;
+               value = dbwrap_record_get_value(rec);
 
-       ret = NT_STATUS_OK;
+               if (value.dsize == 4*4) {
+                       mask = map_old_SE_PRIV(value.dptr);
+               } else {
+                       if (value.dsize != sizeof( uint64_t ) ) {
+                               DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
+                                         "[%s]\n", sid_string));
+                               return 0;
+                       }
+                       mask = BVAL(value.dptr, 0);
+               }
+
+               /* if the SID does not have the specified privilege
+                  then just return */
 
-done:
-       return ret;
+               if ((mask & priv->privilege) == 0) {
+                       return 0;
+               }
+       }
+
+       /* this is a last ditch safety check to preventing returning
+          and invalid SID (i've somehow run into this on development branches) */
+
+       if ( strcmp( "S-0-0", sid_string ) == 0 )
+               return 0;
+
+       if ( !string_to_sid(&sid, sid_string) ) {
+               DEBUG(0,("travsersal_fn_enum__acct: Could not convert SID [%s]\n",
+                       sid_string));
+               return 0;
+       }
+
+       if (!NT_STATUS_IS_OK(add_sid_to_array(priv->mem_ctx, &sid,
+                                             &priv->sids.list,
+                                             &priv->sids.count)))
+       {
+               return 0;
+       }
+
+       return 0;
 }
 
-/****************************************************************************
- add all the privileges to a privilege array
- ****************************************************************************/
-NTSTATUS add_all_privilege(PRIVILEGE_SET *priv_set)
+/*********************************************************************
+ Retreive list of privileged SIDs (for _lsa_enumerate_accounts()
+*********************************************************************/
+
+NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
 {
-       NTSTATUS result = NT_STATUS_OK;
-       LUID_ATTR set;
+       struct db_context *db = get_account_pol_db();
+       PRIV_SID_LIST priv;
+       NTSTATUS status;
 
-       set.attr = 0;
-       set.luid.high = 0;
+       if (db == NULL) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       ZERO_STRUCT(priv);
 
-       /* TODO: set a proper list of privileges */
-       set.luid.low = SE_PRIV_ADD_USERS;
-       result = add_privilege(priv_set, set);
-       NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
+       status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
 
-       set.luid.low = SE_PRIV_ADD_MACHINES;
-       result = add_privilege(priv_set, set);
-       NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
+       /* give the memory away; caller will free */
 
-       set.luid.low = SE_PRIV_PRINT_OPERATOR;
-       result = add_privilege(priv_set, set);
-       NTSTATUS_CHECK(result, done, "add_all_privilege", "add_privilege");
+       *sids      = priv.sids.list;
+       *num_sids  = priv.sids.count;
 
-done:
-       return result;
+       return NT_STATUS_OK;
 }
 
-/****************************************************************************
- check if the privilege list is empty
- ****************************************************************************/
-NTSTATUS check_empty_privilege(PRIVILEGE_SET *priv_set)
+/*********************************************************************
+ Retrieve list of SIDs granted a particular privilege
+*********************************************************************/
+
+NTSTATUS privilege_enum_sids(enum sec_privilege privilege, TALLOC_CTX *mem_ctx,
+                            struct dom_sid **sids, int *num_sids)
 {
-       if (!priv_set)
-               return NT_STATUS_INVALID_PARAMETER;
+       struct db_context *db = get_account_pol_db();
+       PRIV_SID_LIST priv;
+       NTSTATUS status;
 
-       if (priv_set->count == 0)
-               return NT_STATUS_OK;
+       if (db == NULL) {
+               return NT_STATUS_ACCESS_DENIED;
+       }
+
+       ZERO_STRUCT(priv);
 
-       return NT_STATUS_UNSUCCESSFUL;
+       priv.privilege = sec_privilege_mask(privilege);
+       priv.mem_ctx = mem_ctx;
+
+       status = dbwrap_traverse_read(db, priv_traverse_fn, &priv, NULL);
+       if (!NT_STATUS_IS_OK(status)) {
+               return status;
+       }
+
+       /* give the memory away; caller will free */
+
+       *sids      = priv.sids.list;
+       *num_sids  = priv.sids.count;
+
+       return NT_STATUS_OK;
 }
 
-/****************************************************************************
- check if the privilege is in the privilege list
- ****************************************************************************/
-NTSTATUS check_priv_in_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
+/***************************************************************************
+ Add privilege to sid
+****************************************************************************/
+
+static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
 {
-       int i;
+       uint64_t old_mask, new_mask;
 
-       if (!priv_set)
-               return NT_STATUS_INVALID_PARAMETER;
+       ZERO_STRUCT( old_mask );
+       ZERO_STRUCT( new_mask );
 
-       /* if the list is empty, obviously we can't have it */
-       if (NT_STATUS_IS_OK(check_empty_privilege(priv_set)))
-               return NT_STATUS_UNSUCCESSFUL;
+       if ( get_privileges( sid, &old_mask ) )
+               new_mask = old_mask;
+       else
+               new_mask = 0;
 
-       for (i = 0; i < priv_set->count; i++) {
-               LUID_ATTR *cur_set;
+       new_mask |= priv_mask;
 
-               cur_set = &priv_set->set[i];
-               /* check only the low and high part. Checking the attr field has no meaning */
-               if (    (cur_set->luid.low == set.luid.low) &&
-                       (cur_set->luid.high == set.luid.high)   ) {
-                       return NT_STATUS_OK;
-               }
-       }
+       DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
+
+       DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
 
-       return NT_STATUS_UNSUCCESSFUL;
+       DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)new_mask));
+
+       return set_privileges( sid, new_mask );
 }
 
-/****************************************************************************
- remove a privilege from a privilege array
- ****************************************************************************/
-NTSTATUS remove_privilege(PRIVILEGE_SET *priv_set, LUID_ATTR set)
+/*********************************************************************
+ Add a privilege based on its name
+*********************************************************************/
+
+bool grant_privilege_by_name(const struct dom_sid *sid, const char *name)
 {
-       NTSTATUS ret;
-       LUID_ATTR *new_set;
-       LUID_ATTR *old_set;
-       int i,j;
+       uint64_t mask;
 
-       if (!priv_set)
-               return NT_STATUS_INVALID_PARAMETER;
+       if (! se_priv_from_name(name, &mask)) {
+               DEBUG(3, ("grant_privilege_by_name: "
+                         "No Such Privilege Found (%s)\n", name));
+               return False;
+       }
+
+       return grant_privilege_bitmap( sid, mask );
+}
 
-       /* check if the privilege is in the list */
-       if (!NT_STATUS_IS_OK(check_priv_in_privilege(priv_set, set)))
-               return NT_STATUS_UNSUCCESSFUL;
+/***************************************************************************
+ Grant a privilege set (list of LUID values) from a sid
+****************************************************************************/
 
-       /* special case if it's the only privilege in the list */
-       if (priv_set->count == 1) {
-               reset_privilege(priv_set);      
-               return NT_STATUS_OK;
+bool grant_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
+{
+       uint64_t privilege_mask;
+       if (!privilege_set_to_se_priv(&privilege_mask, set)) {
+               return false;
        }
+       return grant_privilege_bitmap(sid, privilege_mask);
+}
 
-       /* 
-        * the privilege is there, create a new list,
-        * and copy the other privileges
-        */
+/***************************************************************************
+ Remove privilege from sid
+****************************************************************************/
+
+static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t priv_mask)
+{
+       uint64_t mask;
 
-       old_set = priv_set->set;
+       /* if the user has no privileges, then we can't revoke any */
 
-       new_set = (LUID_ATTR *)talloc(priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
-       ALLOC_CHECK(new_set, ret, done, "remove_privilege");
+       if ( !get_privileges( sid, &mask ) )
+               return True;
 
-       for (i=0, j=0; i < priv_set->count; i++) {
-               if (    (old_set[i].luid.low == set.luid.low) && 
-                       (old_set[i].luid.high == set.luid.high) ) {
-                       continue;
-               }
-               
-               new_set[j].luid.low = old_set[i].luid.low;
-               new_set[j].luid.high = old_set[i].luid.high;
-               new_set[j].attr = old_set[i].attr;
+       DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
+
+       DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
+
+       mask &= ~priv_mask;
 
-               j++;
+       DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)mask));
+
+       return set_privileges( sid, mask );
+}
+
+/***************************************************************************
+ Remove a privilege set (list of LUID values) from a sid
+****************************************************************************/
+
+bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *set)
+{
+       uint64_t privilege_mask;
+       if (!privilege_set_to_se_priv(&privilege_mask, set)) {
+               return false;
        }
-       
-       if (j != priv_set->count - 1) {
-               DEBUG(0,("remove_privilege: mismatch ! difference is not -1\n"));
-               DEBUGADD(0,("old count:%d, new count:%d\n", priv_set->count, j));
-               return NT_STATUS_INTERNAL_ERROR;
+       return revoke_privilege_bitmap(sid, privilege_mask);
+}
+
+/*********************************************************************
+ Revoke all privileges
+*********************************************************************/
+
+bool revoke_all_privileges( const struct dom_sid *sid )
+{
+       return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
+}
+
+/*********************************************************************
+ Add a privilege based on its name
+*********************************************************************/
+
+bool revoke_privilege_by_name(const struct dom_sid *sid, const char *name)
+{
+       uint64_t mask;
+
+       if (! se_priv_from_name(name, &mask)) {
+               DEBUG(3, ("revoke_privilege_by_name: "
+                         "No Such Privilege Found (%s)\n", name));
+               return False;
        }
-               
-       /* ok everything is fine */
-       
-       priv_set->count--;
-       priv_set->set = new_set;
-       
-       ret = NT_STATUS_OK;
-
-done:
-       return ret;
+
+       return revoke_privilege_bitmap(sid, mask);
+
 }
 
-/****************************************************************************
- duplicates a privilege array
- the new privilege set must be passed inited
- (use init_privilege or init_priv_with_ctx)
- ****************************************************************************/
-NTSTATUS dup_priv_set(PRIVILEGE_SET *new_priv_set, PRIVILEGE_SET *priv_set)
+/***************************************************************************
+ Retrieve the SIDs assigned to a given privilege
+****************************************************************************/
+
+NTSTATUS privilege_create_account(const struct dom_sid *sid )
 {
-       NTSTATUS ret;
-       LUID_ATTR *new_set;
-       LUID_ATTR *old_set;
-       int i;
+       return ( grant_privilege_bitmap(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
+}
 
-       if (!new_priv_set || !priv_set)
-               return NT_STATUS_INVALID_PARAMETER;
+/***************************************************************************
+ Delete a privileged account
+****************************************************************************/
 
-       /* special case if there are no privileges in the list */
-       if (priv_set->count == 0) {
+NTSTATUS privilege_delete_account(const struct dom_sid *sid)
+{
+       struct db_context *db = get_account_pol_db();
+       fstring tmp, keystr;
+
+       if (!lp_enable_privileges()) {
                return NT_STATUS_OK;
        }
 
-       /* 
-        * create a new list,
-        * and copy the other privileges
-        */
+       if (!db) {
+               return NT_STATUS_INVALID_HANDLE;
+       }
+
+       if (!sid || (sid->num_auths == 0)) {
+               return NT_STATUS_INVALID_SID;
+       }
 
-       old_set = priv_set->set;
+       /* PRIV_<SID> (NULL terminated) as the key */
 
-       new_set = (LUID_ATTR *)talloc(new_priv_set->mem_ctx, (priv_set->count - 1) * (sizeof(LUID_ATTR)));
-       ALLOC_CHECK(new_set, ret, done, "dup_priv_set");
+       fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
 
-       for (i=0; i < priv_set->count; i++) {
-               
-               new_set[i].luid.low = old_set[i].luid.low;
-               new_set[i].luid.high = old_set[i].luid.high;
-               new_set[i].attr = old_set[i].attr;
-       }
-                       
-       new_priv_set->count = priv_set->count;
-       new_priv_set->control = priv_set->control;
-       new_priv_set->set = new_set;
-       
-       ret = NT_STATUS_OK;
-
-done:
-       return ret;
+       return dbwrap_delete_bystring(db, keystr);
+}
+
+/*******************************************************************
+*******************************************************************/
+
+bool is_privileged_sid( const struct dom_sid *sid )
+{
+       uint64_t mask;
+
+       return get_privileges( sid, &mask );
+}
+
+/*******************************************************************
+*******************************************************************/
+
+bool grant_all_privileges( const struct dom_sid *sid )
+{
+       uint64_t mask;
+
+       se_priv_put_all_privileges(&mask);
+
+       return grant_privilege_bitmap( sid, mask );
 }