libcli/security Remove 'always true' return from se_priv_put_all_privileges
[abartlet/samba.git/.git] / source3 / lib / privileges.c
index 181ea5c986e8834dd3472449c6c1c9d0ff147a4d..62ed0a35e0a78fe96beb6112f7fee00ae6f46b5b 100644 (file)
@@ -37,6 +37,35 @@ typedef struct {
        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;
+       }
+
+       /* 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);
+       }
+
+       /* it was either zero or big-endian */
+       return RIVAL(dptr, 0);
+}
+
 
 static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
 {
@@ -66,19 +95,18 @@ static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
        }
 
        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;
-       }
+               /* it's an old style SE_PRIV structure. */
+               *mask = map_old_SE_PRIV(data.dptr);
+       } else {
+               if (data.dsize != sizeof( uint64_t ) ) {
+                       DEBUG(3, ("get_privileges: Invalid privileges record assigned to 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);
        }
 
-       *mask = BVAL(data.dptr, 0);
-
        TALLOC_FREE(data.dptr);
 
        return True;
@@ -88,7 +116,7 @@ static bool get_privileges( const struct dom_sid *sid, uint64_t *mask )
  Store the privilege mask (set) for a given SID
 ****************************************************************************/
 
-static bool set_privileges( const struct dom_sid *sid, uint64_t *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];
@@ -111,10 +139,10 @@ static bool set_privileges( const struct dom_sid *sid, uint64_t *mask )
        fstr_sprintf(keystr, "%s%s", PRIVPREFIX, sid_to_fstring(tmp, sid));
 
        /* This writes the 64 bit bitmask out in little endian format */
-       SBVAL(privbuf,0,*mask);
+       SBVAL(privbuf,0,mask);
 
-       data.dptr  = (uint8 *)mask;
-       data.dsize = sizeof(uint64_t);
+       data.dptr  = privbuf;
+       data.dsize = sizeof(privbuf);
 
        return NT_STATUS_IS_OK(dbwrap_store_bystring(db, keystr, data,
                                                     TDB_REPLACE));
@@ -149,6 +177,23 @@ bool get_privileges_for_sids(uint64_t *privileges, struct dom_sid *slist, int sc
        return found;
 }
 
+NTSTATUS get_privileges_for_sid_as_set(TALLOC_CTX *mem_ctx, PRIVILEGE_SET **privileges, struct dom_sid *sid)
+{
+       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;
+}
 
 /*********************************************************************
  traversal functions for privilege_enumerate_accounts
@@ -161,11 +206,6 @@ static int priv_traverse_fn(struct db_record *rec, void *state)
        struct dom_sid sid;
        fstring sid_string;
 
-       /* easy check first */
-
-       if (rec->value.dsize != sizeof(uint64_t) )
-               return 0;
-
        /* check we have a PRIV_+SID entry */
 
        if ( strncmp((char *)rec->key.dptr, PRIVPREFIX, prefixlen) != 0)
@@ -179,19 +219,16 @@ static int priv_traverse_fn(struct db_record *rec, void *state)
                uint64_t mask;
 
                if (rec->value.dsize == 4*4) {
-                       DEBUG(3, ("get_privileges: Should not have obtained old-style privileges record for SID "
-                                 "[%s]\n", sid_string));
-                       return 0;
-               }
-
-               if (rec->value.dsize != sizeof( uint64_t ) ) {
-                       DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
-                                 "[%s]\n", sid_string));
-                       return 0;
+                       mask = map_old_SE_PRIV(rec->value.dptr);
+               } else {
+                       if (rec->value.dsize != sizeof( uint64_t ) ) {
+                               DEBUG(3, ("get_privileges: Invalid privileges record assigned to SID "
+                                         "[%s]\n", sid_string));
+                               return 0;
+                       }
+                       mask = BVAL(rec->value.dptr, 0);
                }
 
-               mask = BVAL(rec->value.dptr, 0);
-
                /* if the SID does not have the specified privilege
                   then just return */
 
@@ -300,14 +337,14 @@ static bool grant_privilege_bitmap(const struct dom_sid *sid, const uint64_t pri
 
        DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)new_mask));
 
-       return set_privileges( sid, &new_mask );
+       return set_privileges( sid, new_mask );
 }
 
 /*********************************************************************
  Add a privilege based on its name
 *********************************************************************/
 
-bool grant_privilege_by_name(struct dom_sid *sid, const char *name)
+bool grant_privilege_by_name(const struct dom_sid *sid, const char *name)
 {
        uint64_t mask;
 
@@ -354,7 +391,7 @@ static bool revoke_privilege_bitmap(const struct dom_sid *sid, const uint64_t pr
 
        DEBUGADD( 10, ("new privilege mask:      0x%llx\n", (unsigned long long)mask));
 
-       return set_privileges( sid, &mask );
+       return set_privileges( sid, mask );
 }
 
 /***************************************************************************
@@ -374,7 +411,7 @@ bool revoke_privilege_set(const struct dom_sid *sid, struct lsa_PrivilegeSet *se
  Revoke all privileges
 *********************************************************************/
 
-bool revoke_all_privileges( struct dom_sid *sid )
+bool revoke_all_privileges( const struct dom_sid *sid )
 {
        return revoke_privilege_bitmap( sid, SE_ALL_PRIVS);
 }
@@ -383,7 +420,7 @@ bool revoke_all_privileges( struct dom_sid *sid )
  Add a privilege based on its name
 *********************************************************************/
 
-bool revoke_privilege_by_name(struct dom_sid *sid, const char *name)
+bool revoke_privilege_by_name(const struct dom_sid *sid, const char *name)
 {
        uint64_t mask;
 
@@ -434,86 +471,6 @@ NTSTATUS privilege_delete_account(const struct dom_sid *sid)
        return dbwrap_delete_bystring(db, keystr);
 }
 
-/****************************************************************************
- initialise a privilege list and set the talloc context
- ****************************************************************************/
-
-NTSTATUS privilege_set_init(PRIVILEGE_SET *priv_set)
-{
-       TALLOC_CTX *mem_ctx;
-
-       ZERO_STRUCTP( priv_set );
-
-       mem_ctx = talloc_init("privilege set");
-       if ( !mem_ctx ) {
-               DEBUG(0,("privilege_set_init: failed to initialize talloc ctx!\n"));
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       priv_set->mem_ctx = mem_ctx;
-
-       return NT_STATUS_OK;
-}
-
-/****************************************************************************
-  initialise a privilege list and with someone else's talloc context
-****************************************************************************/
-
-NTSTATUS privilege_set_init_by_ctx(TALLOC_CTX *mem_ctx, PRIVILEGE_SET *priv_set)
-{
-       ZERO_STRUCTP( priv_set );
-
-       priv_set->mem_ctx = mem_ctx;
-       priv_set->ext_ctx = True;
-
-       return NT_STATUS_OK;
-}
-
-/****************************************************************************
- Free all memory used by a PRIVILEGE_SET
-****************************************************************************/
-
-void privilege_set_free(PRIVILEGE_SET *priv_set)
-{
-       if ( !priv_set )
-               return;
-
-       if ( !( priv_set->ext_ctx ) )
-               talloc_destroy( priv_set->mem_ctx );
-
-       ZERO_STRUCTP( priv_set );
-}
-
-/****************************************************************************
- duplicate alloc luid_attr
- ****************************************************************************/
-
-NTSTATUS dup_luid_attr(TALLOC_CTX *mem_ctx, struct lsa_LUIDAttribute **new_la, struct lsa_LUIDAttribute *old_la, int count)
-{
-       int i;
-
-       if ( !old_la )
-               return NT_STATUS_OK;
-
-       if (count) {
-               *new_la = TALLOC_ARRAY(mem_ctx, struct lsa_LUIDAttribute, count);
-               if ( !*new_la ) {
-                       DEBUG(0,("dup_luid_attr: failed to alloc new struct lsa_LUIDAttribute array [%d]\n", count));
-                       return NT_STATUS_NO_MEMORY;
-               }
-       } else {
-               *new_la = NULL;
-       }
-
-       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].attribute = old_la[i].attribute;
-       }
-
-       return NT_STATUS_OK;
-}
-
 /*******************************************************************
 *******************************************************************/
 
@@ -531,9 +488,7 @@ bool grant_all_privileges( const struct dom_sid *sid )
 {
        uint64_t mask;
 
-       if (!se_priv_put_all_privileges(&mask)) {
-               return False;
-       }
+       se_priv_put_all_privileges(&mask);
 
        return grant_privilege_bitmap( sid, mask );
 }