s3:registry: move reg_objects.h to registry/ and use it only where needed
[kai/samba.git] / source3 / registry / regfio.c
index 92077aa84788c40519870610f2acc27f6294ae6a..60a05040e264fd13ed05125cf376de9b155428ff 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "includes.h"
 #include "regfio.h"
+#include "reg_objects.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_REGISTRY
@@ -45,7 +46,7 @@ static int write_block( REGF_FILE *file, prs_struct *ps, uint32 offset )
 
        /* check for end of file */
 
-       if ( sys_fstat( file->fd, &sbuf ) ) {
+       if (sys_fstat(file->fd, &sbuf, false)) {
                DEBUG(0,("write_block: stat() failed! (%s)\n", strerror(errno)));
                return -1;
        }
@@ -79,12 +80,12 @@ static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint
 
        /* check for end of file */
 
-       if ( sys_fstat( file->fd, &sbuf ) ) {
+       if (sys_fstat(file->fd, &sbuf, false)) {
                DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));
                return -1;
        }
 
-       if ( (size_t)file_offset >= sbuf.st_size )
+       if ( (size_t)file_offset >= sbuf.st_ex_size )
                return -1;
        
        /* if block_size == 0, we are parsing HBIN records and need 
@@ -123,7 +124,10 @@ static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, uint
                return -1;
        }
        
-       prs_init( ps, block_size, file->mem_ctx, UNMARSHALL );
+       if (!prs_init( ps, block_size, file->mem_ctx, UNMARSHALL )) {
+               DEBUG(0,("read_block: prs_init() failed! (%s)\n", strerror(errno) ));
+               return -1;
+       }
        buffer = prs_data_p( ps );
        bytes_read = returned = 0;
 
@@ -709,8 +713,30 @@ static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_
        if ( !prs_uint32( "size", ps, depth, &sk->size))
                return False;
 
-       if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) 
-               return False;
+       {
+               NTSTATUS status;
+               TALLOC_CTX *mem_ctx = prs_get_mem_context(&hbin->ps);
+               DATA_BLOB blob;
+
+               if (MARSHALLING(&hbin->ps)) {
+                       status = marshall_sec_desc(mem_ctx,
+                                                  sk->sec_desc,
+                                                  &blob.data, &blob.length);
+                       if (!NT_STATUS_IS_OK(status))
+                               return False;
+                       if (!prs_copy_data_in(&hbin->ps, (const char *)blob.data, blob.length))
+                               return False;
+               } else {
+                       blob = data_blob_const(prs_data_p(&hbin->ps),
+                                              prs_data_size(&hbin->ps));
+                       status = unmarshall_sec_desc(mem_ctx,
+                                                    blob.data, blob.length,
+                                                    &sk->sec_desc);
+                       if (!NT_STATUS_IS_OK(status))
+                               return False;
+                       prs_set_offset(&hbin->ps, blob.length);
+               }
+       }
 
        end_off = prs_offset( &hbin->ps );
 
@@ -928,12 +954,12 @@ static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
 /*******************************************************************
 *******************************************************************/
 
-static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
+static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, struct security_descriptor *sd )
 {
        REGF_SK_REC *p;
 
        for ( p=file->sec_desc_list; p; p=p->next ) {
-               if ( sec_desc_equal( p->sec_desc, sd ) )
+               if ( security_descriptor_equal( p->sec_desc, sd ) )
                        return p;
        }
 
@@ -1171,7 +1197,6 @@ out:
        
        if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) {
                regfio_close( rb );
-               SAFE_FREE(rb);
                return NULL;
        }
 
@@ -1182,7 +1207,6 @@ out:
        if ( (rb->fd = open(filename, flags, mode)) == -1 ) {
                DEBUG(0,("regfio_open: failure to open %s (%s)\n", filename, strerror(errno)));
                regfio_close( rb );
-               SAFE_FREE(rb);
                return NULL;
        }
        
@@ -1192,7 +1216,6 @@ out:
                if ( !init_regf_block( rb ) ) {
                        DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
                        regfio_close( rb );
-                       SAFE_FREE(rb);
                        return NULL;
                }
                
@@ -1205,7 +1228,6 @@ out:
        if ( !read_regf_block( rb ) ) {
                DEBUG(0,("regfio_open: Failed to read initial REGF block\n"));
                regfio_close( rb );
-               SAFE_FREE(rb);
                return NULL;
        }
        
@@ -1234,7 +1256,7 @@ static void regfio_mem_free( REGF_FILE *file )
 
        /* cleanup for a file opened for write */
 
-       if ( file->open_flags & (O_WRONLY|O_RDWR) ) {
+       if ((file->fd != -1) && (file->open_flags & (O_WRONLY|O_RDWR))) {
                prs_struct ps;
                REGF_SK_REC *sk;
 
@@ -1278,7 +1300,7 @@ static void regfio_mem_free( REGF_FILE *file )
 
        /* nothing tdo do if there is no open file */
 
-       if ( !file || (file->fd == -1) )
+       if (file->fd == -1)
                return 0;
                
        fd = file->fd;
@@ -1408,12 +1430,12 @@ static REGF_HBIN* regf_hbin_allocate( REGF_FILE *file, uint32 block_size )
        memcpy( hbin->header, "hbin", sizeof(HBIN_HDR_SIZE) );
 
 
-       if ( sys_fstat( file->fd, &sbuf ) ) {
+       if (sys_fstat(file->fd, &sbuf, false)) {
                DEBUG(0,("regf_hbin_allocate: stat() failed! (%s)\n", strerror(errno)));
                return NULL;
        }
 
-       hbin->file_off       = sbuf.st_size;
+       hbin->file_off       = sbuf.st_ex_size;
 
        hbin->free_off       = HBIN_HEADER_REC_SIZE;
        hbin->free_size      = block_size - hbin->free_off + sizeof(uint32);;
@@ -1546,7 +1568,7 @@ done:
 /*******************************************************************
 *******************************************************************/
 
-static uint32 sk_record_data_size( SEC_DESC * sd )
+static uint32 sk_record_data_size( struct security_descriptor * sd )
 {
        uint32 size, size_mod8;
 
@@ -1636,7 +1658,8 @@ static uint32 nk_record_data_size( REGF_NK_REC *nk )
 /*******************************************************************
 *******************************************************************/
 
-static bool create_vk_record( REGF_FILE *file, REGF_VK_REC *vk, REGISTRY_VALUE *value )
+static bool create_vk_record(REGF_FILE *file, REGF_VK_REC *vk,
+                            struct regval_blob *value)
 {
        char *name = regval_name(value);
        REGF_HBIN *data_hbin;
@@ -1693,9 +1716,9 @@ static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
 /*******************************************************************
 *******************************************************************/
 
- REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name, 
-                               REGVAL_CTR *values, REGSUBKEY_CTR *subkeys, 
-                               SEC_DESC *sec_desc, REGF_NK_REC *parent )
+ REGF_NK_REC* regfio_write_key( REGF_FILE *file, const char *name,
+                               struct regval_ctr *values, struct regsubkey_ctr *subkeys,
+                               struct security_descriptor *sec_desc, REGF_NK_REC *parent )
 {
        REGF_NK_REC *nk;
        REGF_HBIN *vlist_hbin = NULL;
@@ -1745,8 +1768,7 @@ static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
                parent->subkey_index++;
 
                /* sort the list by keyname */
-
-               qsort( parent->subkeys.hashes, parent->subkey_index, sizeof(REGF_HASH_REC), QSORT_CAST hashrec_cmp );
+               TYPESAFE_QSORT(parent->subkeys.hashes, parent->subkey_index, hashrec_cmp);
 
                if ( !hbin_prs_lf_records( "lf_rec", parent->subkeys.hbin, 0, parent ) )
                        return False;
@@ -1793,8 +1815,8 @@ static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
                           if this is the first record, then just set the next and prev
                           offsets to ourself. */
 
-                       if ( nk->sec_desc->prev ) {
-                               REGF_SK_REC *prev = nk->sec_desc->prev;
+                       if ( DLIST_PREV(nk->sec_desc) ) {
+                               REGF_SK_REC *prev = DLIST_PREV(nk->sec_desc);
 
                                nk->sec_desc->prev_sk_off = prev->hbin_off + prev->hbin->first_hbin_off - HBIN_HDR_SIZE;
                                prev->next_sk_off = nk->sec_desc->sk_off;
@@ -1873,7 +1895,7 @@ static int hashrec_cmp( REGF_HASH_REC *h1, REGF_HASH_REC *h2 )
 
                for ( i=0; i<nk->num_values; i++ ) {
                        uint32 vk_size, namelen, datalen;
-                       REGISTRY_VALUE *r;
+                       struct regval_blob *r;
 
                        r = regval_ctr_specific_value( values, i );
                        create_vk_record( file, &nk->values[i], r );