This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.git] / source / rpc_parse / parse_sec.c
index 55e05531cbc965220954aaaf096e321bb853bc03..47300e083ab148cc902cdfeed24646890bdabe85 100644 (file)
@@ -1,5 +1,6 @@
 /* 
- *  Unix SMB/CIFS implementation.
+ *  Unix SMB/Netbios implementation.
+ *  Version 1.9.
  *  RPC Pipe client / server routines
  *  Copyright (C) Andrew Tridgell              1992-1998,
  *  Copyright (C) Jeremy R. Allison            1995-1998
@@ -39,16 +40,13 @@ void init_sec_access(SEC_ACCESS *t, uint32 mask)
  Reads or writes a SEC_ACCESS structure.
 ********************************************************************/
 
-BOOL sec_io_access(char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
+BOOL sec_io_access(const char *desc, SEC_ACCESS *t, prs_struct *ps, int depth)
 {
        if (t == NULL)
                return False;
 
        prs_debug(ps, depth, desc, "sec_io_access");
        depth++;
-
-       if(!prs_align(ps))
-               return False;
        
        if(!prs_uint32("mask", ps, depth, &(t->mask)))
                return False;
@@ -105,7 +103,7 @@ void init_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 f
  Reads or writes a SEC_ACE structure.
 ********************************************************************/
 
-BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
+BOOL sec_io_ace(const char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
 {
        uint32 old_offset;
        uint32 offset_ace_size;
@@ -115,9 +113,6 @@ BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
 
        prs_debug(ps, depth, desc, "sec_io_ace");
        depth++;
-
-       if(!prs_align(ps))
-               return False;
        
        old_offset = prs_offset(ps);
 
@@ -133,9 +128,6 @@ BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
        if(!sec_io_access("info ", &psa->info, ps, depth))
                return False;
 
-       if(!prs_align(ps))
-               return False;
-
        /* check whether object access is present */
        if (!sec_ace_object(psa->type)) {
                if (!smb_io_dom_sid("trustee  ", &psa->trustee , ps, depth))
@@ -165,15 +157,15 @@ BOOL sec_io_ace(char *desc, SEC_ACE *psa, prs_struct *ps, int depth)
  adds new SID with its permissions to ACE list
 ********************************************************************/
 
-NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, SEC_ACE **new, SEC_ACE *old, size_t *num, DOM_SID *sid, uint32 mask)
+NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, SEC_ACE **new, SEC_ACE *old, unsigned *num, DOM_SID *sid, uint32 mask)
 {
-       int i = 0;
+       unsigned int i = 0;
        
        if (!ctx || !new || !old || !sid || !num)  return NT_STATUS_INVALID_PARAMETER;
 
        *num += 1;
        
-       if((new[0] = (SEC_ACE *) talloc_zero(ctx, *num * sizeof(SEC_ACE))) == 0)
+       if((new[0] = (SEC_ACE *) talloc_zero(ctx, (*num) * sizeof(SEC_ACE))) == 0)
                return NT_STATUS_NO_MEMORY;
 
        for (i = 0; i < *num - 1; i ++)
@@ -193,7 +185,7 @@ NTSTATUS sec_ace_add_sid(TALLOC_CTX *ctx, SEC_ACE **new, SEC_ACE *old, size_t *n
 
 NTSTATUS sec_ace_mod_sid(SEC_ACE *ace, size_t num, DOM_SID *sid, uint32 mask)
 {
-       int i = 0;
+       unsigned int i = 0;
 
        if (!ace || !sid)  return NT_STATUS_INVALID_PARAMETER;
 
@@ -210,14 +202,14 @@ NTSTATUS sec_ace_mod_sid(SEC_ACE *ace, size_t num, DOM_SID *sid, uint32 mask)
  delete SID from ACL
 ********************************************************************/
 
-NTSTATUS sec_ace_del_sid(TALLOC_CTX *ctx, SEC_ACE **new, SEC_ACE *old, size_t *num, DOM_SID *sid)
+static NTSTATUS sec_ace_del_sid(TALLOC_CTX *ctx, SEC_ACE **new, SEC_ACE *old, uint32 *num, DOM_SID *sid)
 {
-       int i     = 0;
-       int n_del = 0;
+       unsigned int i     = 0;
+       unsigned int n_del = 0;
 
        if (!ctx || !new || !old || !sid || !num)  return NT_STATUS_INVALID_PARAMETER;
 
-       if((new[0] = (SEC_ACE *) talloc_zero(ctx, *num * sizeof(SEC_ACE))) == 0)
+       if((new[0] = (SEC_ACE *) talloc_zero(ctx, (*num) * sizeof(SEC_ACE))) == 0)
                return NT_STATUS_NO_MEMORY;
 
        for (i = 0; i < *num; i ++) {
@@ -289,13 +281,20 @@ SEC_ACL *dup_sec_acl(TALLOC_CTX *ctx, SEC_ACL *src)
  for you as it reads them.
 ********************************************************************/
 
-BOOL sec_io_acl(char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
+BOOL sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
 {
-       int i;
+       unsigned int i;
        uint32 old_offset;
        uint32 offset_acl_size;
        SEC_ACL *psa;
 
+       /*
+        * Note that the size is always a multiple of 4 bytes due to the
+        * nature of the data structure.  Therefore the prs_align() calls
+        * have been removed as they through us off when doing two-layer
+        * marshalling such as in the printing code (NEW_BUFFER).  --jerry
+        */
+
        if (ppsa == NULL)
                return False;
 
@@ -312,9 +311,6 @@ BOOL sec_io_acl(char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
 
        prs_debug(ps, depth, desc, "sec_io_acl");
        depth++;
-
-       if(!prs_align(ps))
-               return False;
        
        old_offset = prs_offset(ps);
 
@@ -344,9 +340,6 @@ BOOL sec_io_acl(char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
                        return False;
        }
 
-       if(!prs_align(ps))
-               return False;
-
        if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
                return False;
 
@@ -365,17 +358,19 @@ size_t sec_desc_size(SEC_DESC *psd)
 
        offset = SEC_DESC_HEADER_SIZE;
 
+       /* don't align */
+
        if (psd->owner_sid != NULL)
-               offset += ((sid_size(psd->owner_sid) + 3) & ~3);
+               offset += sid_size(psd->owner_sid);
 
        if (psd->grp_sid != NULL)
-               offset += ((sid_size(psd->grp_sid) + 3) & ~3);
+               offset += sid_size(psd->grp_sid);
 
        if (psd->sacl != NULL)
-               offset += ((psd->sacl->size + 3) & ~3);
+               offset += psd->sacl->size;
 
        if (psd->dacl != NULL)
-               offset += ((psd->dacl->size + 3) & ~3);
+               offset += psd->dacl->size;
 
        return offset;
 }
@@ -412,7 +407,7 @@ BOOL sec_ace_equal(SEC_ACE *s1, SEC_ACE *s2)
 
 BOOL sec_acl_equal(SEC_ACL *s1, SEC_ACL *s2)
 {
-       int i, j;
+       unsigned int i, j;
 
        /* Trivial cases */
 
@@ -643,7 +638,7 @@ SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision,
                if (offset == 0)
                        offset = SEC_DESC_HEADER_SIZE;
 
-               offset += ((sid_size(dst->owner_sid) + 3) & ~3);
+               offset += sid_size(dst->owner_sid);
        }
 
        if (dst->grp_sid != NULL) {
@@ -651,7 +646,7 @@ SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision,
                if (offset == 0)
                        offset = SEC_DESC_HEADER_SIZE;
 
-               offset += ((sid_size(dst->grp_sid) + 3) & ~3);
+               offset += sid_size(dst->grp_sid);
        }
 
        if (dst->sacl != NULL) {
@@ -659,7 +654,7 @@ SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision,
                offset_acl = SEC_DESC_HEADER_SIZE;
 
                dst->off_sacl  = offset_acl;
-               offset_acl    += ((dst->sacl->size + 3) & ~3);
+               offset_acl    += dst->sacl->size;
                offset        += dst->sacl->size;
                offset_sid    += dst->sacl->size;
        }
@@ -670,20 +665,20 @@ SEC_DESC *make_sec_desc(TALLOC_CTX *ctx, uint16 revision,
                        offset_acl = SEC_DESC_HEADER_SIZE;
 
                dst->off_dacl  = offset_acl;
-               offset_acl    += ((dst->dacl->size + 3) & ~3);
+               offset_acl    += dst->dacl->size;
                offset        += dst->dacl->size;
                offset_sid    += dst->dacl->size;
        }
 
        *sd_size = (size_t)((offset == 0) ? SEC_DESC_HEADER_SIZE : offset);
 
-       if (dst->owner_sid != NULL) {
+       if (dst->owner_sid != NULL)
                dst->off_owner_sid = offset_sid;
+               
+       /* sid_size() returns 0 if the sid is NULL so this is ok */
+               
+       if (dst->grp_sid != NULL)
                dst->off_grp_sid = offset_sid + sid_size(dst->owner_sid);
-       }
-       else
-               if (dst->grp_sid != NULL)
-                       dst->off_grp_sid = offset_sid;
 
        return dst;
 
@@ -725,7 +720,7 @@ SEC_DESC *make_standard_sec_desc(TALLOC_CTX *ctx, DOM_SID *owner_sid, DOM_SID *g
  If reading and the *ppsd = NULL, allocates the structure.
 ********************************************************************/
 
-BOOL sec_io_desc(char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
+BOOL sec_io_desc(const char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
 {
        uint32 old_offset;
        uint32 max_offset = 0; /* after we're done, move offset to end */
@@ -752,7 +747,7 @@ BOOL sec_io_desc(char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
        prs_debug(ps, depth, desc, "sec_io_desc");
        depth++;
 
-#if 0  /* JERRY */
+#if 0  
        /*
         * if alignment is needed, should be done by the the 
         * caller.  Not here.  This caused me problems when marshalling
@@ -787,72 +782,69 @@ BOOL sec_io_desc(char *desc, SEC_DESC **ppsd, prs_struct *ps, int depth)
 
        if (psd->off_owner_sid != 0) {
 
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + psd->off_owner_sid))
+                       return False;
+
                if (UNMARSHALLING(ps)) {
-                       if(!prs_set_offset(ps, old_offset + psd->off_owner_sid))
-                               return False;
                        /* reading */
                        if((psd->owner_sid = (DOM_SID *)prs_alloc_mem(ps,sizeof(*psd->owner_sid))) == NULL)
                                return False;
                }
 
-               tmp_offset = ps->data_offset;
-               ps->data_offset = psd->off_owner_sid;
-
                if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
                        return False;
-               if(!prs_align(ps))
-                       return False;
 
-               ps->data_offset = tmp_offset;
-       }
+               max_offset = MAX(max_offset, prs_offset(ps));
 
-       max_offset = MAX(max_offset, prs_offset(ps));
+               if (!prs_set_offset(ps,tmp_offset))
+                       return False;
+       }
 
        if (psd->off_grp_sid != 0) {
 
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + psd->off_grp_sid))
+                       return False;
+
                if (UNMARSHALLING(ps)) {
                        /* reading */
-                       if(!prs_set_offset(ps, old_offset + psd->off_grp_sid))
-                               return False;
                        if((psd->grp_sid = (DOM_SID *)prs_alloc_mem(ps,sizeof(*psd->grp_sid))) == NULL)
                                return False;
                }
 
-               tmp_offset = ps->data_offset;
-               ps->data_offset = psd->off_grp_sid;
-
                if(!smb_io_dom_sid("grp_sid", psd->grp_sid, ps, depth))
                        return False;
-               if(!prs_align(ps))
-                       return False;
+                       
+               max_offset = MAX(max_offset, prs_offset(ps));
 
-               ps->data_offset = tmp_offset;
+               if (!prs_set_offset(ps,tmp_offset))
+                       return False;
        }
 
-       max_offset = MAX(max_offset, prs_offset(ps));
-
        if ((psd->type & SEC_DESC_SACL_PRESENT) && psd->off_sacl) {
+               tmp_offset = prs_offset(ps);
                if(!prs_set_offset(ps, old_offset + psd->off_sacl))
                        return False;
                if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
                        return False;
-               if(!prs_align(ps))
+               max_offset = MAX(max_offset, prs_offset(ps));
+               if (!prs_set_offset(ps,tmp_offset))
                        return False;
        }
 
-       max_offset = MAX(max_offset, prs_offset(ps));
 
        if ((psd->type & SEC_DESC_DACL_PRESENT) && psd->off_dacl != 0) {
+               tmp_offset = prs_offset(ps);
                if(!prs_set_offset(ps, old_offset + psd->off_dacl))
                        return False;
                if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
                        return False;
-               if(!prs_align(ps))
+               max_offset = MAX(max_offset, prs_offset(ps));
+               if (!prs_set_offset(ps,tmp_offset))
                        return False;
        }
 
-       max_offset = MAX(max_offset, prs_offset(ps));
-
        if(!prs_set_offset(ps, max_offset))
                return False;
        return True;
@@ -898,7 +890,7 @@ SEC_DESC_BUF *dup_sec_desc_buf(TALLOC_CTX *ctx, SEC_DESC_BUF *src)
  Reads or writes a SEC_DESC_BUF structure.
 ********************************************************************/
 
-BOOL sec_io_desc_buf(char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
+BOOL sec_io_desc_buf(const char *desc, SEC_DESC_BUF **ppsdb, prs_struct *ps, int depth)
 {
        uint32 off_len;
        uint32 off_max_len;