Fix a segfault in parse_logentry
[metze/samba/wip.git] / source3 / rpc_server / srv_eventlog_lib.c
index 00afe5b05c30c4ad0dc86f40c5a48b169211091e..8cbb319e9b0e53d227d89e6d56da7f115178c654 100644 (file)
@@ -1,20 +1,20 @@
-/* 
+/*
  *  Unix SMB/CIFS implementation.
  *  Eventlog utility  routines
  *  Copyright (C) Marcin Krzysztof Porwit    2005,
  *  Copyright (C) Brian Moran                2005.
  *  Copyright (C) Gerald (Jerry) Carter      2005.
- *  
+ *
  *  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 3 of the License, or
  *  (at your option) any later version.
- *  
+ *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  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, see <http://www.gnu.org/licenses/>.
  */
@@ -26,7 +26,7 @@
 static ELOG_TDB *open_elog_list;
 
 /********************************************************************
- Init an Eventlog TDB, and return it. If null, something bad 
+ Init an Eventlog TDB, and return it. If null, something bad
  happened.
 ********************************************************************/
 
@@ -37,7 +37,7 @@ TDB_CONTEXT *elog_init_tdb( char *tdbfilename )
        DEBUG(10,("elog_init_tdb: Initializing eventlog tdb (%s)\n",
                tdbfilename));
 
-       tdb = tdb_open_log( tdbfilename, 0, TDB_DEFAULT, 
+       tdb = tdb_open_log( tdbfilename, 0, TDB_DEFAULT,
                O_RDWR|O_CREAT|O_TRUNC, 0660 );
 
        if ( !tdb ) {
@@ -58,26 +58,25 @@ TDB_CONTEXT *elog_init_tdb( char *tdbfilename )
 }
 
 /********************************************************************
- make the tdb file name for an event log, given destination buffer 
+ make the tdb file name for an event log, given destination buffer
  and size. Caller must free memory.
 ********************************************************************/
 
-char *elog_tdbname( const char *name )
+char *elog_tdbname(TALLOC_CTX *ctx, const char *name )
 {
-       fstring path;
-       char *tdb_fullpath;
-       char *eventlogdir = state_path( "eventlog" );
-       
-       pstr_sprintf( path, "%s/%s.tdb", eventlogdir, name );
-       strlower_m( path );
-       tdb_fullpath = SMB_STRDUP( path );
-       
-       return tdb_fullpath;
+       char *path = talloc_asprintf(ctx, "%s/%s.tdb",
+                       state_path("eventlog"),
+                       name);
+       if (!path) {
+               return NULL;
+       }
+       strlower_m(path);
+       return path;
 }
 
 
 /********************************************************************
- this function is used to count up the number of bytes in a 
+ this function is used to count up the number of bytes in a
  particular TDB
 ********************************************************************/
 
@@ -90,27 +89,27 @@ static int eventlog_tdb_size_fn( TDB_CONTEXT * tdb, TDB_DATA key, TDB_DATA data,
                          void *state )
 {
        struct trav_size_struct  *tsize = (struct trav_size_struct *)state;
-       
+
        tsize->size += data.dsize;
        tsize->rec_count++;
-       
+
        return 0;
 }
 
 /********************************************************************
- returns the size of the eventlog, and if MaxSize is a non-null 
- ptr, puts the MaxSize there. This is purely a way not to have yet 
- another function that solely reads the maxsize of the eventlog. 
+ returns the size of the eventlog, and if MaxSize is a non-null
+ ptr, puts the MaxSize there. This is purely a way not to have yet
+ another function that solely reads the maxsize of the eventlog.
  Yeah, that's it.
 ********************************************************************/
 
 int elog_tdb_size( TDB_CONTEXT * tdb, int *MaxSize, int *Retention )
 {
        struct trav_size_struct tsize;
-       
+
        if ( !tdb )
                return 0;
-               
+
        ZERO_STRUCT( tsize );
 
        tdb_traverse( tdb, eventlog_tdb_size_fn, &tsize );
@@ -131,25 +130,24 @@ int elog_tdb_size( TDB_CONTEXT * tdb, int *MaxSize, int *Retention )
 
 /********************************************************************
  Discard early event logs until we have enough for 'needed' bytes...
- NO checking done beforehand to see that we actually need to do 
- this, and it's going to pluck records one-by-one. So, it's best 
- to determine that this needs to be done before doing it.  
+ NO checking done beforehand to see that we actually need to do
+ this, and it's going to pluck records one-by-one. So, it's best
+ to determine that this needs to be done before doing it.
 
- Setting whack_by_date to True indicates that eventlogs falling 
+ Setting whack_by_date to True indicates that eventlogs falling
  outside of the retention range need to go...
+
  return True if we made enough room to accommodate needed bytes
 ********************************************************************/
 
-bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
-                            bool whack_by_date )
+static bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32_t needed,
+                                   bool whack_by_date )
 {
-       int start_record, i, new_start;
-       int end_record;
-       int nbytes, reclen, len, Retention, MaxSize;
-       int tresv1, trecnum, timegen, timewr;
+       int32_t start_record, i, new_start;
+       int32_t end_record;
+       int32_t reclen, tresv1, trecnum, timegen, timewr;
+       int nbytes, len, Retention, MaxSize;
        TDB_DATA key, ret;
-       TALLOC_CTX *mem_ctx = NULL;
        time_t current_time, exp_time;
 
        /* discard some eventlogs */
@@ -157,10 +155,7 @@ bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
        /* read eventlogs from oldest_entry -- there can't be any discontinuity in recnos,
           although records not necessarily guaranteed to have successive times */
        /* */
-       mem_ctx = talloc_init( "make_way_for_eventlogs" );      /* Homage to BPG */
 
-       if ( mem_ctx == NULL )
-               return False;   /* can't allocate memory indicates bigger problems */
        /* lock */
        tdb_lock_bystring_with_timeout( the_tdb, EVT_NEXT_RECORD, 1 );
        /* read */
@@ -178,16 +173,17 @@ bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
        nbytes = 0;
 
        DEBUG( 3,
-              ( "MaxSize [%d] Retention [%d] Current Time [%d]  exp_time [%d]\n",
-                MaxSize, Retention, (uint32)current_time, (uint32)exp_time ) );
+              ( "MaxSize [%d] Retention [%d] Current Time [%u]  exp_time [%u]\n",
+                MaxSize, Retention, (unsigned int)current_time, (unsigned int)exp_time ) );
        DEBUG( 3,
-              ( "Start Record [%d] End Record [%d]\n", start_record,
-                end_record ) );
+              ( "Start Record [%u] End Record [%u]\n",
+               (unsigned int)start_record,
+               (unsigned int)end_record ));
 
        for ( i = start_record; i < end_record; i++ ) {
                /* read a record, add the amt to nbytes */
-               key.dsize = sizeof( int32 );
-               key.dptr = ( uint8 * ) ( int32 * ) & i;
+               key.dsize = sizeof(int32_t);
+               key.dptr = (unsigned char *)&i;
                ret = tdb_fetch( the_tdb, key );
                if ( ret.dsize == 0 ) {
                        DEBUG( 8,
@@ -203,18 +199,19 @@ bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
                if (len == -1) {
                        DEBUG( 10,("make_way_for_eventlogs: tdb_unpack failed.\n"));
                        tdb_unlock_bystring( the_tdb, EVT_NEXT_RECORD );
+                       SAFE_FREE( ret.dptr );
                        return False;
                }
 
                DEBUG( 8,
-                      ( "read record %d, record size is [%d], total so far [%d]\n",
-                        i, reclen, nbytes ) );
+                      ( "read record %u, record size is [%d], total so far [%d]\n",
+                        (unsigned int)i, reclen, nbytes ) );
 
                SAFE_FREE( ret.dptr );
 
                /* note that other servers may just stop writing records when the size limit
-                  is reached, and there are no records older than 'retention'. This doesn't 
-                  like a very useful thing to do, so instead we whack (as in sleeps with the 
+                  is reached, and there are no records older than 'retention'. This doesn't
+                  like a very useful thing to do, so instead we whack (as in sleeps with the
                   fishes) just enough records to fit the what we need.  This behavior could
                   be changed to 'match', if the need arises. */
 
@@ -225,14 +222,14 @@ bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
        }
 
        DEBUG( 3,
-              ( "nbytes [%d] needed [%d] start_record is [%d], should be set to [%d]\n",
-                nbytes, needed, start_record, i ) );
+              ( "nbytes [%d] needed [%d] start_record is [%u], should be set to [%u]\n",
+                nbytes, needed, (unsigned int)start_record, (unsigned int)i ) );
        /* todo - remove eventlog entries here and set starting record to start_record... */
        new_start = i;
        if ( start_record != new_start ) {
                for ( i = start_record; i < new_start; i++ ) {
-                       key.dsize = sizeof( int32 );
-                       key.dptr = ( uint8 * ) ( int32 * ) & i;
+                       key.dsize = sizeof(int32_t);
+                       key.dptr = (unsigned char *)&i;
                        tdb_delete( the_tdb, key );
                }
 
@@ -243,8 +240,8 @@ bool make_way_for_eventlogs( TDB_CONTEXT * the_tdb, int32 needed,
 }
 
 /********************************************************************
-  some hygiene for an eventlog - see how big it is, and then 
-  calculate how many bytes we need to remove                   
+  some hygiene for an eventlog - see how big it is, and then
+  calculate how many bytes we need to remove
 ********************************************************************/
 
 bool prune_eventlog( TDB_CONTEXT * tdb )
@@ -272,7 +269,7 @@ bool prune_eventlog( TDB_CONTEXT * tdb )
 /********************************************************************
 ********************************************************************/
 
-bool can_write_to_eventlog( TDB_CONTEXT * tdb, int32 needed )
+bool can_write_to_eventlog( TDB_CONTEXT * tdb, int32_t needed )
 {
        int calcd_size;
        int MaxSize, Retention;
@@ -318,23 +315,23 @@ bool can_write_to_eventlog( TDB_CONTEXT * tdb, int32 needed )
 ELOG_TDB *elog_open_tdb( char *logname, bool force_clear )
 {
        TDB_CONTEXT *tdb = NULL;
-       uint32 vers_id;
+       uint32_t vers_id;
        ELOG_TDB *ptr;
-       char *tdbfilename;
-       pstring tdbpath;
+       char *tdbpath = NULL;
        ELOG_TDB *tdb_node = NULL;
        char *eventlogdir;
+       TALLOC_CTX *ctx = talloc_tos();
 
        /* first see if we have an open context */
-       
+
        for ( ptr=open_elog_list; ptr; ptr=ptr->next ) {
                if ( strequal( ptr->name, logname ) ) {
                        ptr->ref_count++;
 
                        /* trick to alow clearing of the eventlog tdb.
                           The force_clear flag should imply that someone
-                          has done a force close.  So make sure the tdb 
-                          is NULL.  If this is a normal open, then just 
+                          has done a force close.  So make sure the tdb
+                          is NULL.  If this is a normal open, then just
                           return the existing reference */
 
                        if ( force_clear ) {
@@ -345,27 +342,28 @@ ELOG_TDB *elog_open_tdb( char *logname, bool force_clear )
                                return ptr;
                }
        }
-       
+
        /* make sure that the eventlog dir exists */
-       
+
        eventlogdir = state_path( "eventlog" );
-       if ( !directory_exist( eventlogdir, NULL ) )
-               mkdir( eventlogdir, 0755 );     
-       
+       if ( !directory_exist( eventlogdir ) )
+               mkdir( eventlogdir, 0755 );
+
        /* get the path on disk */
-       
-       tdbfilename = elog_tdbname( logname );
-       pstrcpy( tdbpath, tdbfilename );
-       SAFE_FREE( tdbfilename );
 
-       DEBUG(7,("elog_open_tdb: Opening %s...(force_clear == %s)\n", 
+       tdbpath = elog_tdbname(ctx, logname);
+       if (!tdbpath) {
+               return NULL;
+       }
+
+       DEBUG(7,("elog_open_tdb: Opening %s...(force_clear == %s)\n",
                tdbpath, force_clear?"True":"False" ));
-               
+
        /* the tdb wasn't already open or this is a forced clear open */
 
        if ( !force_clear ) {
 
-               tdb = tdb_open_log( tdbpath, 0, TDB_DEFAULT, O_RDWR , 0 );      
+               tdb = tdb_open_log( tdbpath, 0, TDB_DEFAULT, O_RDWR , 0 );
                if ( tdb ) {
                        vers_id = tdb_fetch_int32( tdb, EVT_VERSION );
 
@@ -377,12 +375,12 @@ ELOG_TDB *elog_open_tdb( char *logname, bool force_clear )
                        }
                }
        }
-       
+
        if ( !tdb )
                tdb = elog_init_tdb( tdbpath );
-       
+
        /* if we got a valid context, then add it to the list */
-       
+
        if ( tdb ) {
                /* on a forced clear, just reset the tdb context if we already
                   have an open entry in the list */
@@ -397,11 +395,11 @@ ELOG_TDB *elog_open_tdb( char *logname, bool force_clear )
                        tdb_close( tdb );
                        return NULL;
                }
-               
+
                tdb_node->name = talloc_strdup( tdb_node, logname );
                tdb_node->tdb = tdb;
                tdb_node->ref_count = 1;
-               
+
                DLIST_ADD( open_elog_list, tdb_node );
        }
 
@@ -418,9 +416,9 @@ int elog_close_tdb( ELOG_TDB *etdb, bool force_close )
 
        if ( !etdb )
                return 0;
-               
+
        etdb->ref_count--;
-       
+
        SMB_ASSERT( etdb->ref_count >= 0 );
 
        if ( etdb->ref_count == 0 ) {
@@ -429,7 +427,7 @@ int elog_close_tdb( ELOG_TDB *etdb, bool force_close )
                TALLOC_FREE( etdb );
                return tdb_close( tdb );
        }
-       
+
        if ( force_close ) {
                tdb = etdb->tdb;
                etdb->tdb = NULL;
@@ -441,15 +439,15 @@ int elog_close_tdb( ELOG_TDB *etdb, bool force_close )
 
 
 /*******************************************************************
- write an eventlog entry. Note that we have to lock, read next 
- eventlog, increment, write, write the record, unlock 
- coming into this, ee has the eventlog record, and the auxilliary date 
- (computer name, etc.) filled into the other structure. Before packing 
- into a record, this routine will calc the appropriate padding, etc., 
+ write an eventlog entry. Note that we have to lock, read next
+ eventlog, increment, write, write the record, unlock
+
+ coming into this, ee has the eventlog record, and the auxilliary date
+ (computer name, etc.) filled into the other structure. Before packing
+ into a record, this routine will calc the appropriate padding, etc.,
  and then blast out the record in a form that can be read back in
 *******************************************************************/
+
 #define MARGIN 512
 
 int write_eventlog_tdb( TDB_CONTEXT * the_tdb, Eventlog_entry * ee )
@@ -458,7 +456,7 @@ int write_eventlog_tdb( TDB_CONTEXT * the_tdb, Eventlog_entry * ee )
        uint8 *packed_ee;
        TALLOC_CTX *mem_ctx = NULL;
        TDB_DATA kbuf, ebuf;
-       uint32 n_packed;
+       uint32_t n_packed;
 
        if ( !ee )
                return 0;
@@ -468,8 +466,6 @@ int write_eventlog_tdb( TDB_CONTEXT * the_tdb, Eventlog_entry * ee )
        if ( mem_ctx == NULL )
                return 0;
 
-       if ( !ee )
-               return 0;
        /* discard any entries that have bogus time, which usually indicates a bogus entry as well. */
        if ( ee->record.time_generated == 0 )
                return 0;
@@ -584,18 +580,17 @@ void fixup_eventlog_entry( Eventlog_entry * ee )
 }
 
 /********************************************************************
- Note that it's a pretty good idea to initialize the Eventlog_entry 
- structure to zero's before calling parse_logentry on an batch of 
- lines that may resolve to a record.  ALSO, it's a good idea to 
- remove any linefeeds (that's EOL to you and me) on the lines 
+ Note that it's a pretty good idea to initialize the Eventlog_entry
+ structure to zero's before calling parse_logentry on an batch of
+ lines that may resolve to a record.  ALSO, it's a good idea to
+ remove any linefeeds (that's EOL to you and me) on the lines
  going in.
 ********************************************************************/
 
 bool parse_logentry( char *line, Eventlog_entry * entry, bool * eor )
 {
+       TALLOC_CTX *ctx = talloc_tos();
        char *start = NULL, *stop = NULL;
-       pstring temp;
-       int temp_len = 0;
 
        start = line;
 
@@ -661,62 +656,69 @@ bool parse_logentry( char *line, Eventlog_entry * entry, bool * eor )
        } else if ( 0 == strncmp( start, "USL", stop - start ) ) {
                entry->record.user_sid_length = atoi( stop + 1 );
        } else if ( 0 == strncmp( start, "SRC", stop - start ) ) {
-               memset( temp, 0, sizeof( temp ) );
                stop++;
                while ( isspace( stop[0] ) ) {
                        stop++;
                }
-               temp_len = strlen( stop );
-               strncpy( temp, stop, temp_len );
-               rpcstr_push( ( void * ) ( entry->data_record.source_name ),
-                            temp, sizeof( entry->data_record.source_name ),
-                            STR_TERMINATE );
-               entry->data_record.source_name_len =
-                       ( strlen_w( entry->data_record.source_name ) * 2 ) +
-                       2;
+               entry->data_record.source_name_len = rpcstr_push_talloc(ctx,
+                               &entry->data_record.source_name,
+                               stop);
+               if (entry->data_record.source_name_len == (uint32_t)-1 ||
+                               entry->data_record.source_name == NULL) {
+                       return false;
+               }
        } else if ( 0 == strncmp( start, "SRN", stop - start ) ) {
-               memset( temp, 0, sizeof( temp ) );
                stop++;
                while ( isspace( stop[0] ) ) {
                        stop++;
                }
-               temp_len = strlen( stop );
-               strncpy( temp, stop, temp_len );
-               rpcstr_push( ( void * ) ( entry->data_record.computer_name ),
-                            temp, sizeof( entry->data_record.computer_name ),
-                            STR_TERMINATE );
-               entry->data_record.computer_name_len =
-                       ( strlen_w( entry->data_record.computer_name ) * 2 ) +
-                       2;
+               entry->data_record.computer_name_len = rpcstr_push_talloc(ctx,
+                               &entry->data_record.computer_name,
+                               stop);
+               if (entry->data_record.computer_name_len == (uint32_t)-1 ||
+                               entry->data_record.computer_name == NULL) {
+                       return false;
+               }
        } else if ( 0 == strncmp( start, "SID", stop - start ) ) {
-               memset( temp, 0, sizeof( temp ) );
                stop++;
                while ( isspace( stop[0] ) ) {
                        stop++;
                }
-               temp_len = strlen( stop );
-               strncpy( temp, stop, temp_len );
-               rpcstr_push( ( void * ) ( entry->data_record.sid ), temp,
-                            sizeof( entry->data_record.sid ),
-                            STR_TERMINATE );
-               entry->record.user_sid_length =
-                       ( strlen_w( entry->data_record.sid ) * 2 ) + 2;
+               entry->record.user_sid_length = rpcstr_push_talloc(ctx,
+                               &entry->data_record.sid,
+                               stop);
+               if (entry->record.user_sid_length == (uint32_t)-1 ||
+                               entry->data_record.sid == NULL) {
+                       return false;
+               }
        } else if ( 0 == strncmp( start, "STR", stop - start ) ) {
+               smb_ucs2_t *temp = NULL;
+               size_t tmp_len;
+               uint32_t old_len;
                /* skip past initial ":" */
                stop++;
                /* now skip any other leading whitespace */
-               while ( isspace( stop[0] ) ) {
+               while ( isspace(stop[0])) {
                        stop++;
                }
-               temp_len = strlen( stop );
-               memset( temp, 0, sizeof( temp ) );
-               strncpy( temp, stop, temp_len );
-               rpcstr_push( ( void * ) ( entry->data_record.strings +
-                                         ( entry->data_record.strings_len / 2 ) ),
-                            temp,
-                            sizeof( entry->data_record.strings ) -
-                            ( entry->data_record.strings_len / 2 ), STR_TERMINATE );
-               entry->data_record.strings_len += ( temp_len * 2 ) + 2;
+               tmp_len = rpcstr_push_talloc(ctx,
+                                               &temp,
+                                               stop);
+               if (tmp_len == (size_t)-1 || !temp) {
+                       return false;
+               }
+               old_len = entry->data_record.strings_len;
+               entry->data_record.strings = (smb_ucs2_t *)TALLOC_REALLOC_ARRAY(ctx,
+                                               entry->data_record.strings,
+                                               char,
+                                               old_len + tmp_len);
+               if (!entry->data_record.strings) {
+                       return false;
+               }
+               memcpy(((char *)entry->data_record.strings) + old_len,
+                               temp,
+                               tmp_len);
+               entry->data_record.strings_len += tmp_len;
                entry->record.num_strings++;
        } else if ( 0 == strncmp( start, "DAT", stop - start ) ) {
                /* skip past initial ":" */
@@ -725,25 +727,18 @@ bool parse_logentry( char *line, Eventlog_entry * entry, bool * eor )
                while ( isspace( stop[0] ) ) {
                        stop++;
                }
-               entry->data_record.user_data_len = strlen( stop );
-               memset( entry->data_record.user_data, 0,
-                       sizeof( entry->data_record.user_data ) );
-               if ( entry->data_record.user_data_len > 0 ) {
-                       /* copy no more than the first 1024 bytes */
-                       if ( entry->data_record.user_data_len >
-                            sizeof( entry->data_record.user_data ) )
-                               entry->data_record.user_data_len =
-                                       sizeof( entry->data_record.
-                                               user_data );
-                       memcpy( entry->data_record.user_data, stop,
-                               entry->data_record.user_data_len );
+               entry->data_record.user_data_len = strlen(stop);
+               entry->data_record.user_data = talloc_strdup(ctx,
+                                               stop);
+               if (!entry->data_record.user_data) {
+                       return false;
                }
        } else {
                /* some other eventlog entry -- not implemented, so dropping on the floor */
                DEBUG( 10, ( "Unknown entry [%s]. Ignoring.\n", line ) );
                /* For now return true so that we can keep on parsing this mess. Eventually
                   we will return False here. */
-               return True;
+               return true;
        }
-       return True;
+       return true;
 }