s3-privs Remove a pointer from grant_privilege()
[amitay/samba.git] / source3 / lib / privileges.c
index 6da8aaa48de053b44648c2fc52220b955338cad0..349067f047e0bc6fcb2b8ac066d89093852c96d6 100644 (file)
 
 
 #include "includes.h"
+#include "dbwrap.h"
 
 #define PRIVPREFIX              "PRIV_"
 
 typedef struct {
-       size_t count;
+       uint32_t count;
        struct dom_sid *list;
 } SID_LIST;
 
 typedef struct {
        TALLOC_CTX *mem_ctx;
-       SE_PRIV privilege;
+       uint64_t privilege;
        SID_LIST sids;
 } PRIV_SID_LIST;
 
 
-static bool get_privileges( const struct dom_sid *sid, SE_PRIV *mask )
+static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
 {
        struct db_context *db = get_account_pol_db();
        fstring tmp, keystr;
@@ -64,9 +65,20 @@ static bool get_privileges( const struct dom_sid *sid, SE_PRIV *mask )
                return False;
        }
 
-       SMB_ASSERT( data.dsize == sizeof( SE_PRIV ) );
+       if (data.dsize == 4*4) {
+               DEBUG(3, ("get_privileges: Should not have obtained old-style privileges record for SID "
+                         "[%s]\n", sid_string_dbg(sid)));
+               return 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);
 
-       se_priv_copy( mask, (SE_PRIV*)data.dptr );
        TALLOC_FREE(data.dptr);
 
        return True;
@@ -76,9 +88,10 @@ static bool get_privileges( const struct dom_sid *sid, SE_PRIV *mask )
  Store the privilege mask (set) for a given SID
 ****************************************************************************/
 
-static bool set_privileges( const struct dom_sid *sid, SE_PRIV *mask )
+static bool set_privileges( const struct dom_sid *sid, uint64_t *mask )
 {
        struct db_context *db = get_account_pol_db();
+       uint8_t privbuf[8];
        fstring tmp, keystr;
        TDB_DATA data;
 
@@ -97,10 +110,11 @@ static bool set_privileges( const struct dom_sid *sid, SE_PRIV *mask )
 
        fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
 
-       /* no packing.  static size structure, just write it out */
+       /* This writes the 64 bit bitmask out in little endian format */
+       SBVAL(privbuf,0,*mask);
 
        data.dptr  = (uint8 *)mask;
-       data.dsize = sizeof(SE_PRIV);
+       data.dsize = sizeof(uint64_t);
 
        return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
                                                     TDB_REPLACE));
@@ -110,9 +124,9 @@ static bool set_privileges( const struct dom_sid *sid, SE_PRIV *mask )
  get a list of all privileges for all sids in the list
 *********************************************************************/
 
-bool get_privileges_for_sids(SE_PRIV *privileges, struct dom_sid *slist, int scount)
+bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int scount)
 {
-       SE_PRIV mask;
+       uint64_t mask;
        int i;
        bool found = False;
 
@@ -125,8 +139,8 @@ bool get_privileges_for_sids(SE_PRIV *privileges, struct dom_sid *slist, int sco
                        continue;
 
                DEBUG(5,("get_privileges_for_sids: sid = %s\nPrivilege "
-                        "set:\n", sid_string_dbg(&slist[i])));
-               dump_se_priv( DBGC_ALL, 5, &mask );
+                        "set: 0x%llx\n", sid_string_dbg(&slist[i]),
+                        (unsigned long long)mask));
 
                se_priv_add( privileges, &mask );
                found = True;
@@ -149,7 +163,7 @@ static int priv_traverse_fn(struct db_record *rec, void *state)
 
        /* easy check first */
 
-       if (rec->value.dsize != sizeof(SE_PRIV) )
+       if (rec->value.dsize != sizeof(uint64_t) )
                return 0;
 
        /* check we have a PRIV_+SID entry */
@@ -160,9 +174,9 @@ static int priv_traverse_fn(struct db_record *rec, void *state)
        /* check to see if we are looking for a particular privilege */
 
        if ( !se_priv_equal(&priv->privilege, &se_priv_none) ) {
-               SE_PRIV mask;
+               uint64_t mask;
 
-               se_priv_copy( &mask, (SE_PRIV*)rec->value.dptr );
+               se_priv_copy( &mask, (uint64_t*)rec->value.dptr );
 
                /* if the SID does not have the specified privilege
                   then just return */
@@ -226,7 +240,7 @@ NTSTATUS privilege_enumerate_accounts(struct dom_sid **sids, int *num_sids)
  Retrieve list of SIDs granted a particular privilege
 *********************************************************************/
 
-NTSTATUS privilege_enum_sids(const SE_PRIV *mask, TALLOC_CTX *mem_ctx,
+NTSTATUS privilege_enum_sids(const uint64_t *mask, TALLOC_CTX *mem_ctx,
                             struct dom_sid **sids, int *num_sids)
 {
        struct db_context *db = get_account_pol_db();
@@ -255,27 +269,25 @@ NTSTATUS privilege_enum_sids(const SE_PRIV *mask, TALLOC_CTX *mem_ctx,
  Add privilege to sid
 ****************************************************************************/
 
-bool grant_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
+bool grant_privilege(const struct dom_sid *sid, const uint64_t priv_mask)
 {
-       SE_PRIV old_mask, new_mask;
+       uint64_t old_mask, new_mask;
 
        ZERO_STRUCT( old_mask );
        ZERO_STRUCT( new_mask );
 
        if ( get_privileges( sid, &old_mask ) )
-               se_priv_copy( &new_mask, &old_mask );
+               new_mask = old_mask;
        else
-               se_priv_copy( &new_mask, &se_priv_none );
+               new_mask = 0;
 
-       se_priv_add( &new_mask, priv_mask );
+       new_mask |= priv_mask;
 
        DEBUG(10,("grant_privilege: %s\n", sid_string_dbg(sid)));
 
-       DEBUGADD( 10, ("original privilege mask:\n"));
-       dump_se_priv( DBGC_ALL, 10, &old_mask );
+       DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)new_mask));
 
-       DEBUGADD( 10, ("new privilege mask:\n"));
-       dump_se_priv( DBGC_ALL, 10, &new_mask );
+       DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)new_mask));
 
        return set_privileges( sid, &new_mask );
 }
@@ -286,7 +298,7 @@ bool grant_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
 
 bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
 {
-       SE_PRIV mask;
+       uint64_t mask;
 
        if (! se_priv_from_name(name, &mask)) {
                DEBUG(3, ("grant_privilege_by_name: "
@@ -294,16 +306,16 @@ bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
                return False;
        }
 
-       return grant_privilege( sid, &mask );
+       return grant_privilege( sid, mask );
 }
 
 /***************************************************************************
  Remove privilege from sid
 ****************************************************************************/
 
-bool revoke_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
+bool revoke_privilege(const struct dom_sid *sid, const uint64_t priv_mask)
 {
-       SE_PRIV mask;
+       uint64_t mask;
 
        /* if the user has no privileges, then we can't revoke any */
 
@@ -312,13 +324,11 @@ bool revoke_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
 
        DEBUG(10,("revoke_privilege: %s\n", sid_string_dbg(sid)));
 
-       DEBUGADD( 10, ("original privilege mask:\n"));
-       dump_se_priv( DBGC_ALL, 10, &mask );
+       DEBUGADD( 10, ("original privilege mask: 0x%llx\n", (unsigned long long)mask));
 
-       se_priv_remove( &mask, priv_mask );
+       mask &= ~priv_mask;
 
-       DEBUGADD( 10, ("new privilege mask:\n"));
-       dump_se_priv( DBGC_ALL, 10, &mask );
+       DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)mask));
 
        return set_privileges( sid, &mask );
 }
@@ -329,7 +339,7 @@ bool revoke_privilege(const struct dom_sid *sid, const SE_PRIV *priv_mask)
 
 bool revoke_all_privileges( struct dom_sid *sid )
 {
-       return revoke_privilege( sid, &se_priv_all );
+       return revoke_privilege( sid, SE_ALL_PRIVS);
 }
 
 /*********************************************************************
@@ -338,7 +348,7 @@ bool revoke_all_privileges( struct dom_sid *sid )
 
 bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
 {
-       SE_PRIV mask;
+       uint64_t mask;
 
        if (! se_priv_from_name(name, &mask)) {
                DEBUG(3, ("revoke_privilege_by_name: "
@@ -346,7 +356,7 @@ bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
                return False;
        }
 
-       return revoke_privilege(sid, &mask);
+       return revoke_privilege(sid, mask);
 
 }
 
@@ -356,7 +366,7 @@ bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
 
 NTSTATUS privilege_create_account(const struct dom_sid *sid )
 {
-       return ( grant_privilege(sid, &se_priv_none) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
+       return ( grant_privilege(sid, 0) ? NT_STATUS_OK : NT_STATUS_UNSUCCESSFUL);
 }
 
 /***************************************************************************
@@ -441,7 +451,7 @@ void privilege_set_free(PRIVILEGE_SET *priv_set)
  duplicate alloc luid_attr
  ****************************************************************************/
 
-NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_la, int count)
+NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, struct lsa_LUIDAttribute **new_la, struct lsa_LUIDAttribute *old_la, int count)
 {
        int i;
 
@@ -449,9 +459,9 @@ NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_l
                return NT_STATUS_OK;
 
        if (count) {
-               *new_la = TALLOC_ARRAY(mem_ctx, LUID_ATTR, count);
+               *new_la = TALLOC_ARRAY(mem_ctx, struct lsa_LUIDAttribute, count);
                if ( !*new_la ) {
-                       DEBUG(0,("dup_luid_attr: failed to alloc new LUID_ATTR array [%d]\n", count));
+                       DEBUG(0,("dup_luid_attr: failed to alloc new struct lsa_LUIDAttribute array [%d]\n", count));
                        return NT_STATUS_NO_MEMORY;
                }
        } else {
@@ -461,7 +471,7 @@ NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_l
        for (i=0; i<count; i++) {
                (*new_la)[i].luid.high = old_la[i].luid.high;
                (*new_la)[i].luid.low = old_la[i].luid.low;
-               (*new_la)[i].attr = old_la[i].attr;
+               (*new_la)[i].attribute = old_la[i].attribute;
        }
 
        return NT_STATUS_OK;
@@ -472,7 +482,7 @@ NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, LUID_ATTR **new_la, LUID_ATTR *old_l
 
 bool is_privileged_sid( const struct dom_sid *sid )
 {
-       SE_PRIV mask;
+       uint64_t mask;
 
        return get_privileges( sid, &mask );
 }
@@ -482,11 +492,11 @@ bool is_privileged_sid( const struct dom_sid *sid )
 
 bool grant_all_privileges( const struct dom_sid *sid )
 {
-       SE_PRIV mask;
+       uint64_t mask;
 
        if (!se_priv_put_all_privileges(&mask)) {
                return False;
        }
 
-       return grant_privilege( sid, &mask );
+       return grant_privilege( sid, mask );
 }