Remove more redundant lsa parsing functions.
[bbaumbach/samba-autobuild/.git] / source / rpc_parse / parse_sec.c
index 161e42c9ffb679bb7dba80448391fdf51ae318c8..6ea128d3a49cf61c9ecc902ea7d54d889cd4a5fd 100644 (file)
@@ -2,14 +2,14 @@
  *  Unix SMB/Netbios implementation.
  *  Version 1.9.
  *  RPC Pipe client / server routines
- *  Copyright (C) Andrew Tridgell              1992-1999,
- *  Copyright (C) Jeremy R. Allison            1995-1999
- *  Copyright (C) Luke Kenneth Casson Leighton 1996-1999,
- *  Copyright (C) Paul Ashton                  1997-1999.
+ *  Copyright (C) Andrew Tridgell              1992-1998,
+ *  Copyright (C) Jeremy R. Allison            1995-2005.
+ *  Copyright (C) Luke Kenneth Casson Leighton 1996-1998,
+ *  Copyright (C) Paul Ashton                  1997-1998.
  *  
  *  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,
  *  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"
 
-extern int DEBUGLEVEL;
-
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_PARSE
 
 /*******************************************************************
-makes a structure.
+ Reads or writes a SEC_ACCESS structure.
 ********************************************************************/
-BOOL make_sec_access(SEC_ACCESS *t, uint32 mask)
-{
-       t->mask = mask;
-
-       return True;
-}
 
-/*******************************************************************
-reads or writes a 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;
+       if (t == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "sec_io_access");
        depth++;
-
-       prs_align(ps);
        
-       prs_uint32("mask", ps, depth, &(t->mask));
+       if(!prs_uint32("mask", ps, depth, t))
+               return False;
 
        return True;
 }
 
-
 /*******************************************************************
-makes a structure.
+ Reads or writes a SEC_ACE structure.
 ********************************************************************/
-BOOL make_sec_ace(SEC_ACE *t, DOM_SID *sid, uint8 type, SEC_ACCESS mask, uint8 flag)
-{
-       t->type = type;
-       t->flags = flag;
-       t->size = sid_size(sid) + 8;
-       t->info = mask;
-
-       sid_copy(&t->sid, sid);
-
-       return True;
-}
 
-/*******************************************************************
-reads or writes a structure.
-********************************************************************/
-BOOL sec_io_ace(char *desc, SEC_ACE *t, 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;
-       if (t == NULL) return False;
+       uint8 type;
+
+       if (psa == NULL)
+               return False;
 
        prs_debug(ps, depth, desc, "sec_io_ace");
        depth++;
-
-       prs_align(ps);
        
-       old_offset = ps->offset;
+       old_offset = prs_offset(ps);
 
-       prs_uint8     ("type ", ps, depth, &(t->type));
-       prs_uint8     ("flags", ps, depth, &(t->flags));
-       prs_uint16_pre("size ", ps, depth, &(t->size ), &offset_ace_size);
+       if (MARSHALLING(ps)) {
+               type = (uint8)psa->type;
+       }
 
-       sec_io_access   ("info ", &t->info, ps, depth);
-       prs_align(ps);
-       smb_io_dom_sid("sid  ", &t->sid , ps, depth);
+       if(!prs_uint8("type ", ps, depth, &type))
+               return False;
 
-       prs_uint16_post("size ", ps, depth, &t->size, offset_ace_size, old_offset);
+       if (UNMARSHALLING(ps)) {
+               psa->type = (enum security_ace_type)type;
+       }
 
-       return True;
-}
+       if(!prs_uint8("flags", ps, depth, &psa->flags))
+               return False;
 
-/*******************************************************************
-makes a structure.  
-********************************************************************/
-BOOL make_sec_acl(SEC_ACL *t, uint16 revision, int num_aces, SEC_ACE *ace)
-{
-       int i;
-       t->revision = revision;
-       t->num_aces = num_aces;
-       t->size = 4;
-       t->ace = ace;
-
-       for (i = 0; i < num_aces; i++)
-       {
-               t->size += ace[i].size;
+       if(!prs_uint16_pre("size ", ps, depth, &psa->size, &offset_ace_size))
+               return False;
+
+       if(!prs_uint32("access_mask", ps, depth, &psa->access_mask))
+               return False;
+
+       /* check whether object access is present */
+       if (!sec_ace_object(psa->type)) {
+               if (!smb_io_dom_sid("trustee  ", &psa->trustee , ps, depth))
+                       return False;
+       } else {
+               if (!prs_uint32("obj_flags", ps, depth, &psa->object.object.flags))
+                       return False;
+
+               if (psa->object.object.flags & SEC_ACE_OBJECT_PRESENT)
+                       if (!smb_io_uuid("obj_guid", &psa->object.object.type.type, ps,depth))
+                               return False;
+
+               if (psa->object.object.flags & SEC_ACE_OBJECT_INHERITED_PRESENT)
+                       if (!smb_io_uuid("inh_guid", &psa->object.object.inherited_type.inherited_type, ps,depth))
+                               return False;
+
+               if(!smb_io_dom_sid("trustee  ", &psa->trustee , ps, depth))
+                       return False;
        }
 
-       return True;
-}
+       /* Theorectically an ACE can have a size greater than the
+          sum of its components. When marshalling, pad with extra null bytes up to the
+          correct size. */
 
-/*******************************************************************
-frees a structure.  
-********************************************************************/
-void free_sec_acl(SEC_ACL *t)
-{
-       if (t->ace != NULL)
-       {
-               free(t->ace);
+       if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
+               uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
+               uint32 i;
+               uint8 c = 0;
+
+               for (i = 0; i < extra_len; i++) {
+                       if (!prs_uint8("ace extra space", ps, depth, &c))
+                               return False;
+               }
        }
+
+       if(!prs_uint16_post("size ", ps, depth, &psa->size, offset_ace_size, old_offset))
+               return False;
+
+       return True;
 }
 
 /*******************************************************************
-reads or writes a structure.  
+ Reads or writes a SEC_ACL structure.  
 
-first of the xx_io_xx functions that allocates its data structures
+ First of the xx_io_xx functions that allocates its data structures
  for you as it reads them.
 ********************************************************************/
-BOOL sec_io_acl(char *desc, SEC_ACL *t, prs_struct *ps, int depth)
+
+bool sec_io_acl(const char *desc, SEC_ACL **ppsa, prs_struct *ps, int depth)
 {
-       uint32 i;
+       unsigned int i;
        uint32 old_offset;
        uint32 offset_acl_size;
+       SEC_ACL *psa;
+       uint16 revision;
+
+       /*
+        * 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 (RPC_BUFFER).  --jerry
+        */
+
+       if (ppsa == NULL)
+               return False;
 
-       if (t == NULL) return False;
+       psa = *ppsa;
+
+       if(UNMARSHALLING(ps) && psa == NULL) {
+               /*
+                * This is a read and we must allocate the stuct to read into.
+                */
+               if((psa = PRS_ALLOC_MEM(ps, SEC_ACL, 1)) == NULL)
+                       return False;
+               *ppsa = psa;
+       }
 
        prs_debug(ps, depth, desc, "sec_io_acl");
        depth++;
-
-       prs_align(ps);
        
-       old_offset = ps->offset;
+       old_offset = prs_offset(ps);
 
-       prs_uint16("revision", ps, depth, &(t->revision));
-       prs_uint16_pre("size     ", ps, depth, &(t->size     ), &offset_acl_size);
-       prs_uint32("num_aces ", ps, depth, &(t->num_aces ));
+       if (MARSHALLING(ps)) {
+               revision = (uint16)psa->revision;
+       }
 
-       if (ps->io && t->num_aces != 0)
-       {
-               /* reading */
-               t->ace = (SEC_ACE*)malloc(sizeof(t->ace[0]) * t->num_aces);
-               ZERO_STRUCTP(t->ace);
-               }
+       if(!prs_uint16("revision", ps, depth, &revision))
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               psa->revision = (enum security_acl_revision)revision;
+       }
+
+       if(!prs_uint16_pre("size     ", ps, depth, &psa->size, &offset_acl_size))
+               return False;
 
-       if (t->ace == NULL && t->num_aces != 0)
-       {
-               DEBUG(0,("INVALID ACL\n"));
-               ps->offset = 0xfffffffe;
+       if(!prs_uint32("num_aces ", ps, depth, &psa->num_aces))
                return False;
+
+       if (UNMARSHALLING(ps)) {
+               if (psa->num_aces) {
+                       if((psa->aces = PRS_ALLOC_MEM(ps, SEC_ACE, psa->num_aces)) == NULL)
+                               return False;
+               } else {
+                       psa->aces = NULL;
+               }
        }
 
-       for (i = 0; i < MIN(t->num_aces, MAX_SEC_ACES); i++)
-       {
+       for (i = 0; i < psa->num_aces; i++) {
                fstring tmp;
-               slprintf(tmp, sizeof(tmp)-1, "ace[%02d]: ", i);
-               sec_io_ace(tmp, &t->ace[i], ps, depth);
+               slprintf(tmp, sizeof(tmp)-1, "ace_list[%02d]: ", i);
+               if(!sec_io_ace(tmp, &psa->aces[i], ps, depth))
+                       return False;
        }
 
-       prs_align(ps);
+       /* Theorectically an ACL can have a size greater than the
+          sum of its components. When marshalling, pad with extra null bytes up to the
+          correct size. */
 
-       prs_uint16_post("size     ", ps, depth, &t->size    , offset_acl_size, old_offset);
+       if (MARSHALLING(ps) && (psa->size > prs_offset(ps) - old_offset)) {
+               uint32 extra_len = psa->size - (prs_offset(ps) - old_offset);
+               uint8 c = 0;
+
+               for (i = 0; i < extra_len; i++) {
+                       if (!prs_uint8("acl extra space", ps, depth, &c))
+                               return False;
+               }
+       }
+
+       if(!prs_uint16_post("size     ", ps, depth, &psa->size, offset_acl_size, old_offset))
+               return False;
 
        return True;
 }
 
-
 /*******************************************************************
-makes a structure
+ Reads or writes a SEC_DESC structure.
+ If reading and the *ppsd = NULL, allocates the structure.
 ********************************************************************/
-int make_sec_desc(SEC_DESC *t, uint16 revision, uint16 type,
-                       DOM_SID *owner_sid, DOM_SID *grp_sid,
-                               SEC_ACL *sacl, SEC_ACL *dacl)
-{
-       uint32 offset;
 
-       t->revision = revision;
-       t->type     = type;
+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 */
+       uint32 tmp_offset = 0;
+       uint32 off_sacl, off_dacl, off_owner_sid, off_grp_sid;
+       uint16 revision;
 
-       t->off_owner_sid = 0;
-       t->off_grp_sid   = 0;
-       t->off_sacl      = 0;
-       t->off_dacl      = 0;
+       SEC_DESC *psd;
 
-       t->dacl      = dacl;
-       t->sacl      = sacl;
-       t->owner_sid = owner_sid;
-       t->grp_sid   = grp_sid;
+       if (ppsd == NULL)
+               return False;
 
-       offset = 0x0;
+       psd = *ppsd;
 
-       if (dacl != NULL)
-       {
-               if (offset == 0)
-               {
-                       offset = 0x14;
+       if (psd == NULL) {
+               if(UNMARSHALLING(ps)) {
+                       if((psd = PRS_ALLOC_MEM(ps,SEC_DESC,1)) == NULL)
+                               return False;
+                       *ppsd = psd;
+               } else {
+                       /* Marshalling - just ignore. */
+                       return True;
                }
-               t->off_dacl = offset;
-               offset += dacl->size;
        }
 
-       if (sacl != NULL)
-       {
-               if (offset == 0)
-               {
-                       offset = 0x14;
-               }
-               t->off_dacl = offset;
-               offset += dacl->size;
-       }
+       prs_debug(ps, depth, desc, "sec_io_desc");
+       depth++;
 
-       if (owner_sid != NULL)
-       {
-               if (offset == 0)
-               {
-                       offset = 0x14;
-               }
-               t->off_owner_sid = offset;
-               offset += sid_size(owner_sid);
+       /* start of security descriptor stored for back-calc offset purposes */
+       old_offset = prs_offset(ps);
+
+       if (MARSHALLING(ps)) {
+               revision = (uint16)psd->revision;
        }
 
-       if (grp_sid != NULL)
-       {
-               if (offset == 0)
-               {
-                       offset = 0x14;
-               }
-               t->off_grp_sid = offset;
-               offset += sid_size(grp_sid);
+       if(!prs_uint16("revision", ps, depth, &revision))
+               return False;
+
+       if (UNMARSHALLING(ps)) {
+               psd->revision = (enum security_descriptor_revision)revision;
        }
 
-       return (offset == 0) ? 0x14 : offset;
-}
+       if(!prs_uint16("type     ", ps, depth, &psd->type))
+               return False;
 
+       if (MARSHALLING(ps)) {
+               uint32 offset = SEC_DESC_HEADER_SIZE;
 
-/*******************************************************************
-frees a structure
-********************************************************************/
-void free_sec_desc(SEC_DESC *t)
-{
-       if (t->dacl != NULL)
-       {
-               free_sec_acl(t->dacl);
-       }
+               /*
+                * Work out the offsets here, as we write it out.
+                */
 
-       if (t->sacl != NULL)
-       {
-               free_sec_acl(t->dacl);
+               if (psd->sacl != NULL) {
+                       off_sacl = offset;
+                       offset += psd->sacl->size;
+               } else {
+                       off_sacl = 0;
+               }
 
-       }
+               if (psd->dacl != NULL) {
+                       off_dacl = offset;
+                       offset += psd->dacl->size;
+               } else {
+                       off_dacl = 0;
+               }
 
-       if (t->owner_sid != NULL)
-       {
-               free(t->owner_sid);
-       }
+               if (psd->owner_sid != NULL) {
+                       off_owner_sid = offset;
+                       offset += ndr_size_dom_sid(psd->owner_sid, 0);
+               } else {
+                       off_owner_sid = 0;
+               }
 
-       if (t->grp_sid != NULL)
-       {
-               free(t->grp_sid);
+               if (psd->group_sid != NULL) {
+                       off_grp_sid = offset;
+                       offset += ndr_size_dom_sid(psd->group_sid, 0);
+               } else {
+                       off_grp_sid = 0;
+               }
        }
-}
 
+       if(!prs_uint32("off_owner_sid", ps, depth, &off_owner_sid))
+               return False;
 
-/*******************************************************************
-reads or writes a structure.
-********************************************************************/
-static BOOL sec_io_desc(char *desc, SEC_DESC *t, prs_struct *ps, int depth)
-{
-#if 0
-       uint32 off_owner_sid;
-       uint32 off_grp_sid  ;
-       uint32 off_sacl     ;
-       uint32 off_dacl     ;
-#endif
-       uint32 old_offset;
-       uint32 max_offset = 0; /* after we're done, move offset to end */
+       if(!prs_uint32("off_grp_sid  ", ps, depth, &off_grp_sid))
+               return False;
 
-       if (t == NULL) return False;
+       if(!prs_uint32("off_sacl     ", ps, depth, &off_sacl))
+               return False;
 
-       prs_debug(ps, depth, desc, "sec_io_desc");
-       depth++;
+       if(!prs_uint32("off_dacl     ", ps, depth, &off_dacl))
+               return False;
 
-       prs_align(ps);
-       
-       /* start of security descriptor stored for back-calc offset purposes */
-       old_offset = ps->offset;
-
-       prs_uint16("revision ", ps, depth, &(t->revision ));
-       prs_uint16("type     ", ps, depth, &(t->type     ));
-
-       prs_uint32("off_owner_sid", ps, depth, &(t->off_owner_sid));
-       prs_uint32("off_grp_sid  ", ps, depth, &(t->off_grp_sid  ));
-       prs_uint32("off_sacl     ", ps, depth, &(t->off_sacl     ));
-       prs_uint32("off_dacl     ", ps, depth, &(t->off_dacl     ));
-#if 0
-       prs_uint32_pre("off_owner_sid", ps, depth, &(t->off_owner_sid), &off_owner_sid);
-       prs_uint32_pre("off_grp_sid  ", ps, depth, &(t->off_grp_sid  ), &off_grp_sid  );
-       prs_uint32_pre("off_sacl     ", ps, depth, &(t->off_sacl     ), &off_sacl     );
-       prs_uint32_pre("off_dacl     ", ps, depth, &(t->off_dacl     ), &off_dacl     );
-#endif
-       max_offset = MAX(max_offset, ps->offset);
-
-       if (IS_BITS_SET_ALL(t->type, SEC_DESC_DACL_PRESENT))
-       {
-#if 0
-               prs_uint32_post("off_dacl    ", ps, depth, &(t->off_dacl     ), off_dacl     , ps->offset - old_offset);
-#endif
-               ps->offset = old_offset + t->off_dacl;
-               if (ps->io)
-               {
-                       /* reading */
-                       t->dacl = (SEC_ACL*)malloc(sizeof(*t->dacl));
-                       ZERO_STRUCTP(t->dacl);
-               }
+       max_offset = MAX(max_offset, prs_offset(ps));
+
+       if (off_owner_sid != 0) {
 
-               if (t->dacl == NULL)
-               {
-                       DEBUG(0,("INVALID DACL\n"));
-                       ps->offset = 0xfffffffe;
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + off_owner_sid))
                        return False;
-               }
 
-               sec_io_acl     ("dacl"        , t->dacl       , ps, depth);
-               prs_align(ps);
-       }
-#if 0
-       else
-       {
-               prs_uint32_post("off_dacl    ", ps, depth, &(t->off_dacl     ), off_dacl     , 0);
-       }
-#endif
-
-       max_offset = MAX(max_offset, ps->offset);
-
-       if (IS_BITS_SET_ALL(t->type, SEC_DESC_SACL_PRESENT))
-       {
-#if 0
-               prs_uint32_post("off_sacl  ", ps, depth, &(t->off_sacl  ), off_sacl  , ps->offset - old_offset);
-#endif
-               ps->offset = old_offset + t->off_sacl;
-               if (ps->io)
-               {
+               if (UNMARSHALLING(ps)) {
                        /* reading */
-                       t->sacl = (SEC_ACL*)malloc(sizeof(*t->sacl));
-                       ZERO_STRUCTP(t->sacl);
+                       if((psd->owner_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
+                               return False;
                }
 
-               if (t->sacl == NULL)
-               {
-                       DEBUG(0,("INVALID SACL\n"));
-                       ps->offset = 0xfffffffe;
+               if(!smb_io_dom_sid("owner_sid ", psd->owner_sid , ps, depth))
                        return False;
-               }
 
-               sec_io_acl     ("sacl"      , t->sacl       , ps, depth);
-               prs_align(ps);
-       }
-#if 0
-       else
-       {
-               prs_uint32_post("off_sacl  ", ps, depth, &(t->off_sacl  ), off_sacl  , 0);
-       }
-#endif
-
-       max_offset = MAX(max_offset, ps->offset);
-
-#if 0
-       prs_uint32_post("off_owner_sid", ps, depth, &(t->off_owner_sid), off_owner_sid, ps->offset - old_offset);
-#endif
-       if (t->off_owner_sid != 0)
-       {
-               if (ps->io)
-               {
-                       ps->offset = old_offset + t->off_owner_sid;
-                       }
-               if (ps->io)
-               {
-                       /* reading */
-                       t->owner_sid = (DOM_SID*)malloc(sizeof(*t->owner_sid));
-                       ZERO_STRUCTP(t->owner_sid);
-               }
+               max_offset = MAX(max_offset, prs_offset(ps));
 
-               if (t->owner_sid == NULL)
-               {
-                       DEBUG(0,("INVALID OWNER SID\n"));
-                       ps->offset = 0xfffffffe;
+               if (!prs_set_offset(ps,tmp_offset))
                        return False;
-               }
-
-               smb_io_dom_sid("owner_sid ", t->owner_sid , ps, depth);
-               prs_align(ps);
        }
 
-       max_offset = MAX(max_offset, ps->offset);
+       if (psd->group_sid != 0) {
 
-#if 0
-       prs_uint32_post("off_grp_sid  ", ps, depth, &(t->off_grp_sid  ), off_grp_sid  , ps->offset - old_offset);
-#endif
-       if (t->off_grp_sid != 0)
-       {
-               if (ps->io)
-               {
-                       ps->offset = old_offset + t->off_grp_sid;
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + off_grp_sid))
+                       return False;
 
-               }
-               if (ps->io)
-               {
+               if (UNMARSHALLING(ps)) {
                        /* reading */
-                       t->grp_sid = (DOM_SID*)malloc(sizeof(*t->grp_sid));
-                       ZERO_STRUCTP(t->grp_sid);
+                       if((psd->group_sid = PRS_ALLOC_MEM(ps,DOM_SID,1)) == NULL)
+                               return False;
                }
 
-               if (t->grp_sid == NULL)
-               {
-                       DEBUG(0,("INVALID GROUP SID\n"));
-                       ps->offset = 0xfffffffe;
+               if(!smb_io_dom_sid("grp_sid", psd->group_sid, ps, depth))
                        return False;
-               }
+                       
+               max_offset = MAX(max_offset, prs_offset(ps));
 
-               smb_io_dom_sid("grp_sid", t->grp_sid, ps, depth);
-               prs_align(ps);
+               if (!prs_set_offset(ps,tmp_offset))
+                       return False;
        }
 
-       max_offset = MAX(max_offset, ps->offset);
-
-       ps->offset = max_offset;
-
-       return True;
-}
+       if ((psd->type & SEC_DESC_SACL_PRESENT) && off_sacl) {
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + off_sacl))
+                       return False;
+               if(!sec_io_acl("sacl", &psd->sacl, ps, depth))
+                       return False;
+               max_offset = MAX(max_offset, prs_offset(ps));
+               if (!prs_set_offset(ps,tmp_offset))
+                       return False;
+       }
 
-/*******************************************************************
-creates a SEC_DESC_BUF structure.
-********************************************************************/
-BOOL make_sec_desc_buf(SEC_DESC_BUF *buf, int len, SEC_DESC *data)
-{
-       ZERO_STRUCTP(buf);
+       if ((psd->type & SEC_DESC_DACL_PRESENT) && off_dacl != 0) {
+               tmp_offset = prs_offset(ps);
+               if(!prs_set_offset(ps, old_offset + off_dacl))
+                       return False;
+               if(!sec_io_acl("dacl", &psd->dacl, ps, depth))
+                       return False;
+               max_offset = MAX(max_offset, prs_offset(ps));
+               if (!prs_set_offset(ps,tmp_offset))
+                       return False;
+       }
 
-       /* max buffer size (allocated size) */
-       buf->max_len = len;
-       buf->undoc       = 0;
-       buf->len = data != NULL ? len : 0;
-       buf->sec = data;
+       if(!prs_set_offset(ps, max_offset))
+               return False;
 
        return True;
 }
 
 /*******************************************************************
-frees a SEC_DESC_BUF structure.
+ Reads or writes a SEC_DESC_BUF structure.
 ********************************************************************/
-void free_sec_desc_buf(SEC_DESC_BUF *buf)
-{
-       if (buf->sec != NULL)
-       {
-               free_sec_desc(buf->sec);
-               free(buf->sec);
-       }
-}
-
 
-/*******************************************************************
-reads or writes a SEC_DESC_BUF structure.
-********************************************************************/
-BOOL sec_io_desc_buf(char *desc, SEC_DESC_BUF *sec, 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;
        uint32 old_offset;
        uint32 size;
+       uint32 len;
+       SEC_DESC_BUF *psdb;
+       uint32 ptr;
 
-       if (sec == NULL) return False;
+       if (ppsdb == NULL)
+               return False;
+
+       psdb = *ppsdb;
+
+       if (UNMARSHALLING(ps) && psdb == NULL) {
+               if((psdb = PRS_ALLOC_MEM(ps,SEC_DESC_BUF,1)) == NULL)
+                       return False;
+               *ppsdb = psdb;
+       }
 
        prs_debug(ps, depth, desc, "sec_io_desc_buf");
        depth++;
 
-       prs_align(ps);
+       if(!prs_align(ps))
+               return False;
        
-       prs_uint32_pre("max_len", ps, depth, &(sec->max_len), &off_max_len);
-       prs_uint32    ("undoc  ", ps, depth, &(sec->undoc  ));
-       prs_uint32_pre("len    ", ps, depth, &(sec->len    ), &off_len);
-
-       old_offset = ps->offset;
-
-       if (sec->len != 0 && ps->io)
-       {
-               /* reading */
-               sec->sec = (SEC_DESC*)malloc(sizeof(*sec->sec));
-               ZERO_STRUCTP(sec->sec);
-
-               if (sec->sec == NULL)
-               {
-                       DEBUG(0,("INVALID SEC_DESC\n"));
-                       ps->offset = 0xfffffffe;
-                       return False;
-               }
-       }
+       if(!prs_uint32_pre("max_len", ps, depth, &psdb->sd_size, &off_max_len))
+               return False;
+
+       ptr = 1;
+       if(!prs_uint32    ("ptr  ", ps, depth, &ptr))
+               return False;
+
+       len = ndr_size_security_descriptor(psdb->sd, 0);
+       if(!prs_uint32_pre("len    ", ps, depth, &len, &off_len))
+               return False;
+
+       old_offset = prs_offset(ps);
 
        /* reading, length is non-zero; writing, descriptor is non-NULL */
-       if ((sec->len != 0 || (!ps->io)) && sec->sec != NULL)
-       {
-               sec_io_desc("sec   ", sec->sec, ps, depth);
+       if ((UNMARSHALLING(ps) && psdb->sd_size != 0) || (MARSHALLING(ps) && psdb->sd != NULL)) {
+               if(!sec_io_desc("sec   ", &psdb->sd, ps, depth))
+                       return False;
        }
 
-       size = ps->offset - old_offset;
-       prs_uint32_post("max_len", ps, depth, &(sec->max_len), off_max_len, size == 0 ? sec->max_len : size);
-       prs_uint32_post("len    ", ps, depth, &(sec->len    ), off_len    , size);
+       if(!prs_align(ps))
+               return False;
+       
+       size = prs_offset(ps) - old_offset;
+       if(!prs_uint32_post("max_len", ps, depth, &psdb->sd_size, off_max_len, size == 0 ? psdb->sd_size : size))
+               return False;
+
+       if(!prs_uint32_post("len    ", ps, depth, &len, off_len, size))
+               return False;
 
        return True;
 }
-