Fix a return value from sync_eventlog_params() (patch from Volker)
[ira/wip.git] / source3 / rpc_server / srv_eventlog_nt.c
index 85990055dd795dd814335b132d1d33e6ce851ce0..7af8219a3e0cc30b6a8e195802a7155b74a72625 100644 (file)
@@ -7,7 +7,7 @@
  *  
  *  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
  *  
  *  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,
  *  (at your option) any later version.
  *  
  *  This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
  *  GNU General Public License for more details.
  *  
  *  You should have received a copy of the GNU General Public License
  *  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"
  */
 
 #include "includes.h"
@@ -68,11 +67,11 @@ static EVENTLOG_INFO *find_eventlog_info_by_hnd( pipes_struct * p,
 /********************************************************************
 ********************************************************************/
 
 /********************************************************************
 ********************************************************************/
 
-static BOOL elog_check_access( EVENTLOG_INFO *info, NT_USER_TOKEN *token )
+static bool elog_check_access( EVENTLOG_INFO *info, NT_USER_TOKEN *token )
 {
 {
-       char *tdbname = elog_tdbname( info->logname );
+       char *tdbname = elog_tdbname(talloc_tos(), info->logname );
        SEC_DESC *sec_desc;
        SEC_DESC *sec_desc;
-       BOOL ret;
+       bool ret;
        NTSTATUS ntstatus;
        
        if ( !tdbname ) 
        NTSTATUS ntstatus;
        
        if ( !tdbname ) 
@@ -118,11 +117,15 @@ static BOOL elog_check_access( EVENTLOG_INFO *info, NT_USER_TOKEN *token )
 /********************************************************************
  ********************************************************************/
 
 /********************************************************************
  ********************************************************************/
 
-static BOOL elog_validate_logname( const char *name )
+static bool elog_validate_logname( const char *name )
 {
        int i;
        const char **elogs = lp_eventlog_list();
        
 {
        int i;
        const char **elogs = lp_eventlog_list();
        
+       if (!elogs) {
+               return False;
+       }
+
        for ( i=0; elogs[i]; i++ ) {
                if ( strequal( name, elogs[i] ) )
                        return True;
        for ( i=0; elogs[i]; i++ ) {
                if ( strequal( name, elogs[i] ) )
                        return True;
@@ -134,7 +137,7 @@ static BOOL elog_validate_logname( const char *name )
 /********************************************************************
 ********************************************************************/
 
 /********************************************************************
 ********************************************************************/
 
-static BOOL get_num_records_hook( EVENTLOG_INFO * info )
+static bool get_num_records_hook( EVENTLOG_INFO * info )
 {
        int next_record;
        int oldest_record;
 {
        int next_record;
        int oldest_record;
@@ -164,7 +167,7 @@ static BOOL get_num_records_hook( EVENTLOG_INFO * info )
 /********************************************************************
  ********************************************************************/
 
 /********************************************************************
  ********************************************************************/
 
-static BOOL get_oldest_entry_hook( EVENTLOG_INFO * info )
+static bool get_oldest_entry_hook( EVENTLOG_INFO * info )
 {
        /* it's the same thing */
        return get_num_records_hook( info );
 {
        /* it's the same thing */
        return get_num_records_hook( info );
@@ -277,22 +280,28 @@ static int elog_size( EVENTLOG_INFO *info )
 }
 
 /********************************************************************
 }
 
 /********************************************************************
-  For the given tdb, get the next eventlog record into the passed 
+  For the given tdb, get the next eventlog record into the passed
   Eventlog_entry.  returns NULL if it can't get the record for some reason.
  ********************************************************************/
 
   Eventlog_entry.  returns NULL if it can't get the record for some reason.
  ********************************************************************/
 
-static Eventlog_entry *get_eventlog_record( prs_struct * ps, TDB_CONTEXT * tdb,
-                                    int recno, Eventlog_entry * ee )
+static Eventlog_entry *get_eventlog_record(prs_struct *ps,
+                               TDB_CONTEXT *tdb,
+                               int recno)
 {
 {
+       Eventlog_entry *ee = NULL;
        TDB_DATA ret, key;
 
        int srecno;
        int reclen;
        int len;
 
        TDB_DATA ret, key;
 
        int srecno;
        int reclen;
        int len;
 
-       pstring *wpsource, *wpcomputer, *wpsid, *wpstrs, *puserdata;
+       char *wpsource = NULL;
+       char *wpcomputer = NULL;
+       char *wpsid = NULL;
+       char *wpstrs = NULL;
+       char *puserdata = NULL;
 
 
-       key.dsize = sizeof( int32 );
+       key.dsize = sizeof(int32);
 
        srecno = recno;
        key.dptr = ( uint8 * ) &srecno;
 
        srecno = recno;
        key.dptr = ( uint8 * ) &srecno;
@@ -313,10 +322,11 @@ static Eventlog_entry *get_eventlog_record( prs_struct * ps, TDB_CONTEXT * tdb,
        if ( !len )
                return NULL;
 
        if ( !len )
                return NULL;
 
-       /* ee = PRS_ALLOC_MEM(ps, Eventlog_entry, 1); */
-
-       if ( !ee )
+       ee = TALLOC_ARRAY(ps->mem_ctx, Eventlog_entry, 1);
+       if (!ee) {
                return NULL;
                return NULL;
+       }
+       ZERO_STRUCTP(ee);
 
        len = tdb_unpack( ret.dptr, ret.dsize, "ddddddwwwwddddddBBdBBBd",
                          &ee->record.length, &ee->record.reserved1,
 
        len = tdb_unpack( ret.dptr, ret.dsize, "ddddddwwwwddddddBBdBBBd",
                          &ee->record.length, &ee->record.reserved1,
@@ -344,36 +354,62 @@ static Eventlog_entry *get_eventlog_record( prs_struct * ps, TDB_CONTEXT * tdb,
        /* have to do the following because the tdb_unpack allocs a buff, stuffs a pointer to the buff
           into it's 2nd argment for 'B' */
 
        /* have to do the following because the tdb_unpack allocs a buff, stuffs a pointer to the buff
           into it's 2nd argment for 'B' */
 
-       if ( wpcomputer )
-               memcpy( ee->data_record.computer_name, wpcomputer,
-                       ee->data_record.computer_name_len );
-       if ( wpsource )
-               memcpy( ee->data_record.source_name, wpsource,
-                       ee->data_record.source_name_len );
-
-       if ( wpsid )
-               memcpy( ee->data_record.sid, wpsid,
-                       ee->record.user_sid_length );
-       if ( wpstrs )
-               memcpy( ee->data_record.strings, wpstrs,
-                       ee->data_record.strings_len );
-
-       /* note that userdata is a pstring */
-       if ( puserdata )
-               memcpy( ee->data_record.user_data, puserdata,
-                       ee->data_record.user_data_len );
-
-       SAFE_FREE( wpcomputer );
-       SAFE_FREE( wpsource );
-       SAFE_FREE( wpsid );
-       SAFE_FREE( wpstrs );
-       SAFE_FREE( puserdata );
+       if (wpcomputer) {
+               ee->data_record.computer_name = (smb_ucs2_t *)TALLOC_MEMDUP(
+                       ee, wpcomputer, ee->data_record.computer_name_len);
+               if (!ee->data_record.computer_name) {
+                       TALLOC_FREE(ee);
+                       goto out;
+               }
+       }
+       if (wpsource) {
+               ee->data_record.source_name = (smb_ucs2_t *)TALLOC_MEMDUP(
+                       ee, wpsource, ee->data_record.source_name_len);
+               if (!ee->data_record.source_name) {
+                       TALLOC_FREE(ee);
+                       goto out;
+               }
+       }
+
+       if (wpsid) {
+               ee->data_record.sid = (smb_ucs2_t *)TALLOC_MEMDUP(
+                       ee, wpsid, ee->record.user_sid_length);
+               if (!ee->data_record.sid) {
+                       TALLOC_FREE(ee);
+                       goto out;
+               }
+       }
+       if (wpstrs) {
+               ee->data_record.strings = (smb_ucs2_t *)TALLOC_MEMDUP(
+                       ee, wpstrs, ee->data_record.strings_len);
+               if (!ee->data_record.strings) {
+                       TALLOC_FREE(ee);
+                       goto out;
+               }
+       }
+
+       if (puserdata) {
+               ee->data_record.user_data = (char *)TALLOC_MEMDUP(
+                       ee, puserdata, ee->data_record.user_data_len);
+               if (!ee->data_record.user_data) {
+                       TALLOC_FREE(ee);
+                       goto out;
+               }
+       }
+
+  out:
+
+       SAFE_FREE(wpcomputer);
+       SAFE_FREE(wpsource);
+       SAFE_FREE(wpsid);
+       SAFE_FREE(wpstrs);
+       SAFE_FREE(puserdata);
 
        DEBUG( 10, ( "get_eventlog_record: read back %d\n", len ) );
        DEBUG( 10,
               ( "get_eventlog_record: computer_name %d is ",
                 ee->data_record.computer_name_len ) );
 
        DEBUG( 10, ( "get_eventlog_record: read back %d\n", len ) );
        DEBUG( 10,
               ( "get_eventlog_record: computer_name %d is ",
                 ee->data_record.computer_name_len ) );
-       SAFE_FREE( ret.dptr );
+       SAFE_FREE(ret.dptr);
        return ee;
 }
 
        return ee;
 }
 
@@ -382,16 +418,17 @@ static Eventlog_entry *get_eventlog_record( prs_struct * ps, TDB_CONTEXT * tdb,
  since it uses the table to find the tdb handle
  ********************************************************************/
 
  since it uses the table to find the tdb handle
  ********************************************************************/
 
-static BOOL sync_eventlog_params( EVENTLOG_INFO *info )
+static bool sync_eventlog_params( EVENTLOG_INFO *info )
 {
 {
-       pstring path;
+       char *path = NULL;
        uint32 uiMaxSize;
        uint32 uiRetention;
        uint32 uiMaxSize;
        uint32 uiRetention;
-       REGISTRY_KEY *keyinfo;
-       REGISTRY_VALUE *val;
-       REGVAL_CTR *values;
+       struct registry_key *key;
+       struct registry_value *value;
        WERROR wresult;
        char *elogname = info->logname;
        WERROR wresult;
        char *elogname = info->logname;
+       TALLOC_CTX *ctx = talloc_tos();
+       bool ret = false;
 
        DEBUG( 4, ( "sync_eventlog_params with %s\n", elogname ) );
 
 
        DEBUG( 4, ( "sync_eventlog_params with %s\n", elogname ) );
 
@@ -409,38 +446,47 @@ static BOOL sync_eventlog_params( EVENTLOG_INFO *info )
           to use the same fetch/store api that we use in 
           srv_reg_nt.c */
 
           to use the same fetch/store api that we use in 
           srv_reg_nt.c */
 
-       pstr_sprintf( path, "%s/%s", KEY_EVENTLOG, elogname );
+       path = talloc_asprintf(ctx, "%s/%s", KEY_EVENTLOG, elogname );
+       if (!path) {
+               return false;
+       }
 
 
-       wresult = regkey_open_internal( NULL, &keyinfo, path,
-                                       get_root_nt_token(  ), REG_KEY_READ );
+       wresult = reg_open_path(ctx, path, REG_KEY_READ, get_root_nt_token(),
+                               &key);
 
        if ( !W_ERROR_IS_OK( wresult ) ) {
                DEBUG( 4,
                       ( "sync_eventlog_params: Failed to open key [%s] (%s)\n",
                         path, dos_errstr( wresult ) ) );
 
        if ( !W_ERROR_IS_OK( wresult ) ) {
                DEBUG( 4,
                       ( "sync_eventlog_params: Failed to open key [%s] (%s)\n",
                         path, dos_errstr( wresult ) ) );
-               return False;
+               return false;
        }
 
        }
 
-       if ( !( values = TALLOC_ZERO_P( keyinfo, REGVAL_CTR ) ) ) {
-               TALLOC_FREE( keyinfo );
-               DEBUG( 0, ( "control_eventlog_hook: talloc() failed!\n" ) );
-
-               return False;
+       wresult = reg_queryvalue(key, key, "Retention", &value);
+       if (!W_ERROR_IS_OK(wresult)) {
+               DEBUG(4, ("Failed to query value \"Retention\": %s\n",
+                         dos_errstr(wresult)));
+               ret = false;
+               goto done;
        }
        }
-       fetch_reg_values( keyinfo, values );
-
-       if ( ( val = regval_ctr_getvalue( values, "Retention" ) ) != NULL )
-               uiRetention = IVAL( regval_data_p( val ), 0 );
-
-       if ( ( val = regval_ctr_getvalue( values, "MaxSize" ) ) != NULL )
-               uiMaxSize = IVAL( regval_data_p( val ), 0 );
-
-       TALLOC_FREE( keyinfo );
+       uiRetention = value->v.dword;
+
+       wresult = reg_queryvalue(key, key, "MaxSize", &value);
+       if (!W_ERROR_IS_OK(wresult)) {
+               DEBUG(4, ("Failed to query value \"MaxSize\": %s\n",
+                         dos_errstr(wresult)));
+               ret = false;
+               goto done;
+       }
+       uiMaxSize = value->v.dword;
 
        tdb_store_int32( ELOG_TDB_CTX(info->etdb), EVT_MAXSIZE, uiMaxSize );
        tdb_store_int32( ELOG_TDB_CTX(info->etdb), EVT_RETENTION, uiRetention );
 
 
        tdb_store_int32( ELOG_TDB_CTX(info->etdb), EVT_MAXSIZE, uiMaxSize );
        tdb_store_int32( ELOG_TDB_CTX(info->etdb), EVT_RETENTION, uiRetention );
 
-       return True;
+       ret = true;
+
+done:
+       TALLOC_FREE(ctx);
+       return ret;
 }
 
 /********************************************************************
 }
 
 /********************************************************************
@@ -540,7 +586,7 @@ static Eventlog_entry *read_package_entry( prs_struct * ps,
 /********************************************************************
  ********************************************************************/
 
 /********************************************************************
  ********************************************************************/
 
-static BOOL add_record_to_resp( EVENTLOG_R_READ_EVENTLOG * r_u,
+static bool add_record_to_resp( EVENTLOG_R_READ_EVENTLOG * r_u,
                                Eventlog_entry * ee_new )
 {
        Eventlog_entry *insert_point;
                                Eventlog_entry * ee_new )
 {
        Eventlog_entry *insert_point;
@@ -621,16 +667,20 @@ NTSTATUS _eventlog_clear_eventlog( pipes_struct * p,
                                 EVENTLOG_R_CLEAR_EVENTLOG * r_u )
 {
        EVENTLOG_INFO *info = find_eventlog_info_by_hnd( p, &q_u->handle );
                                 EVENTLOG_R_CLEAR_EVENTLOG * r_u )
 {
        EVENTLOG_INFO *info = find_eventlog_info_by_hnd( p, &q_u->handle );
-       pstring backup_file_name;
+       char *backup_file_name = NULL;
 
        if ( !info )
                return NT_STATUS_INVALID_HANDLE;
 
 
        if ( !info )
                return NT_STATUS_INVALID_HANDLE;
 
-       pstrcpy( backup_file_name, "" );
-       if ( q_u->backupfile.string ) {
-               rpcstr_pull( backup_file_name, q_u->backupfile.string->buffer,
-                            sizeof( backup_file_name ),
-                            q_u->backupfile.string->uni_str_len * 2, 0 );
+       if (q_u->backupfile.string) {
+               size_t len = rpcstr_pull_talloc(p->mem_ctx,
+                               &backup_file_name,
+                               q_u->backupfile.string->buffer,
+                               q_u->backupfile.string->uni_str_len * 2,
+                               0 );
+               if (len == (size_t)-1 || !backup_file_name) {
+                       return NT_STATUS_INVALID_PARAMETER;
+               }
 
                DEBUG(8,( "_eventlog_clear_eventlog: Using [%s] as the backup "
                        "file name for log [%s].",
 
                DEBUG(8,( "_eventlog_clear_eventlog: Using [%s] as the backup "
                        "file name for log [%s].",
@@ -644,7 +694,7 @@ NTSTATUS _eventlog_clear_eventlog( pipes_struct * p,
 
        /* Force a close and reopen */
 
 
        /* Force a close and reopen */
 
-       elog_close_tdb( info->etdb, True ); 
+       elog_close_tdb( info->etdb, True );
        become_root();
        info->etdb = elog_open_tdb( info->logname, True );
        unbecome_root();
        become_root();
        info->etdb = elog_open_tdb( info->logname, True );
        unbecome_root();
@@ -671,7 +721,7 @@ NTSTATUS _eventlog_read_eventlog( pipes_struct * p,
                                EVENTLOG_R_READ_EVENTLOG * r_u )
 {
        EVENTLOG_INFO *info = find_eventlog_info_by_hnd( p, &q_u->handle );
                                EVENTLOG_R_READ_EVENTLOG * r_u )
 {
        EVENTLOG_INFO *info = find_eventlog_info_by_hnd( p, &q_u->handle );
-       Eventlog_entry entry, *ee_new;
+       Eventlog_entry *entry = NULL, *ee_new = NULL;
        uint32 num_records_read = 0;
        prs_struct *ps;
        int bytes_left, record_number;
        uint32 num_records_read = 0;
        prs_struct *ps;
        int bytes_left, record_number;
@@ -686,9 +736,9 @@ NTSTATUS _eventlog_read_eventlog( pipes_struct * p,
 
        bytes_left = q_u->max_read_size;
 
 
        bytes_left = q_u->max_read_size;
 
-       if ( !info->etdb ) 
+       if ( !info->etdb )
                return NT_STATUS_ACCESS_DENIED;
                return NT_STATUS_ACCESS_DENIED;
-               
+
        /* check for valid flags.  Can't use the sequential and seek flags together */
 
        elog_read_type = q_u->flags & (EVENTLOG_SEQUENTIAL_READ|EVENTLOG_SEEK_READ);
        /* check for valid flags.  Can't use the sequential and seek flags together */
 
        elog_read_type = q_u->flags & (EVENTLOG_SEQUENTIAL_READ|EVENTLOG_SEEK_READ);
@@ -705,37 +755,39 @@ NTSTATUS _eventlog_read_eventlog( pipes_struct * p,
 
        if ( elog_read_type & EVENTLOG_SEQUENTIAL_READ )
                record_number = info->current_record;
 
        if ( elog_read_type & EVENTLOG_SEQUENTIAL_READ )
                record_number = info->current_record;
-       else 
+       else
                record_number = q_u->offset;
 
        while ( bytes_left > 0 ) {
 
                /* assume that when the record fetch fails, that we are done */
 
                record_number = q_u->offset;
 
        while ( bytes_left > 0 ) {
 
                /* assume that when the record fetch fails, that we are done */
 
-               if ( !get_eventlog_record ( ps, ELOG_TDB_CTX(info->etdb), record_number, &entry ) ) 
+               entry = get_eventlog_record (ps, ELOG_TDB_CTX(info->etdb), record_number);
+               if (!entry) {
                        break;
                        break;
+               }
 
                DEBUG( 8, ( "Retrieved record %d\n", record_number ) );
 
                DEBUG( 8, ( "Retrieved record %d\n", record_number ) );
-                              
+
                /* Now see if there is enough room to add */
 
                /* Now see if there is enough room to add */
 
-               if ( !(ee_new = read_package_entry( ps, q_u, r_u,&entry )) )
+               if ( !(ee_new = read_package_entry( ps, q_u, r_u, entry )) )
                        return NT_STATUS_NO_MEMORY;
 
                if ( r_u->num_bytes_in_resp + ee_new->record.length > q_u->max_read_size ) {
                        r_u->bytes_in_next_record = ee_new->record.length;
 
                        /* response would be too big to fit in client-size buffer */
                        return NT_STATUS_NO_MEMORY;
 
                if ( r_u->num_bytes_in_resp + ee_new->record.length > q_u->max_read_size ) {
                        r_u->bytes_in_next_record = ee_new->record.length;
 
                        /* response would be too big to fit in client-size buffer */
-                               
+
                        bytes_left = 0;
                        break;
                }
                        bytes_left = 0;
                        break;
                }
-                       
+
                add_record_to_resp( r_u, ee_new );
                bytes_left -= ee_new->record.length;
                add_record_to_resp( r_u, ee_new );
                bytes_left -= ee_new->record.length;
-               ZERO_STRUCT( entry );
+               TALLOC_FREE(entry);
                num_records_read = r_u->num_records - num_records_read;
                num_records_read = r_u->num_records - num_records_read;
-                               
+
                DEBUG( 10, ( "_eventlog_read_eventlog: read [%d] records for a total "
                        "of [%d] records using [%d] bytes out of a max of [%d].\n",
                         num_records_read, r_u->num_records,
                DEBUG( 10, ( "_eventlog_read_eventlog: read [%d] records for a total "
                        "of [%d] records using [%d] bytes out of a max of [%d].\n",
                         num_records_read, r_u->num_records,
@@ -746,13 +798,13 @@ NTSTATUS _eventlog_read_eventlog( pipes_struct * p,
                        record_number++;
                else
                        record_number--;
                        record_number++;
                else
                        record_number--;
-               
+
                /* update the eventlog record pointer */
                /* update the eventlog record pointer */
-               
+
                info->current_record = record_number;
        }
 
                info->current_record = record_number;
        }
 
-       /* crazy by WinXP uses NT_STATUS_BUFFER_TOO_SMALL to 
+       /* crazy by WinXP uses NT_STATUS_BUFFER_TOO_SMALL to
           say when there are no more records */
 
        return (num_records_read ? NT_STATUS_OK : NT_STATUS_BUFFER_TOO_SMALL);
           say when there are no more records */
 
        return (num_records_read ? NT_STATUS_OK : NT_STATUS_BUFFER_TOO_SMALL);