eventlog: Fix CID 242105 Unchecked return value
[metze/samba/wip.git] / source3 / lib / eventlog / eventlog.c
index 11cb28a1203c58dd50ad4fec31af456432cc48f3..cc4573c869ceb4edc80ce94ee8ba2fc9e045b622 100644 (file)
  */
 
 #include "includes.h"
+#include "system/filesys.h"
+#include "lib/eventlog/eventlog.h"
+#include "../libcli/security/security.h"
+#include "util_tdb.h"
 
 /* maintain a list of open eventlog tdbs with reference counts */
 
@@ -65,14 +69,29 @@ TDB_CONTEXT *elog_init_tdb( char *tdbfilename )
 
 char *elog_tdbname(TALLOC_CTX *ctx, const char *name )
 {
-       char *path = talloc_asprintf(ctx, "%s/%s.tdb",
-                       state_path("eventlog"),
-                       name);
+       char *path;
+       char *file;
+       char *tdbname;
+
+       path = state_path("eventlog");
        if (!path) {
                return NULL;
        }
-       strlower_m(path);
-       return path;
+
+       file = talloc_asprintf_strlower_m(path, "%s.tdb", name);
+       if (!file) {
+               talloc_free(path);
+               return NULL;
+       }
+
+       tdbname = talloc_asprintf(ctx, "%s/%s", path, file);
+       if (!tdbname) {
+               talloc_free(path);
+               return NULL;
+       }
+
+       talloc_free(path);
+       return tdbname;
 }
 
 
@@ -322,6 +341,7 @@ ELOG_TDB *elog_open_tdb( const char *logname, bool force_clear, bool read_only )
        ELOG_TDB *tdb_node = NULL;
        char *eventlogdir;
        TALLOC_CTX *ctx = talloc_tos();
+       bool ok;
 
        /* check for invalid options */
 
@@ -353,9 +373,15 @@ ELOG_TDB *elog_open_tdb( const char *logname, bool force_clear, bool read_only )
 
        /* make sure that the eventlog dir exists */
 
-       eventlogdir = state_path( "eventlog" );
-       if ( !directory_exist( eventlogdir ) )
-               mkdir( eventlogdir, 0755 );
+       eventlogdir = state_path("eventlog");
+       if (eventlogdir == NULL) {
+               return NULL;
+       }
+       ok = directory_create_or_exist(eventlogdir, 0755);
+       TALLOC_FREE(eventlogdir);
+       if (!ok) {
+               return NULL;
+       }
 
        /* get the path on disk */
 
@@ -398,7 +424,7 @@ ELOG_TDB *elog_open_tdb( const char *logname, bool force_clear, bool read_only )
                        return ptr;
                }
 
-               if ( !(tdb_node = TALLOC_ZERO_P( NULL, ELOG_TDB)) ) {
+               if ( !(tdb_node = talloc_zero( NULL, ELOG_TDB)) ) {
                        DEBUG(0,("elog_open_tdb: talloc() failure!\n"));
                        tdb_close( tdb );
                        return NULL;
@@ -560,7 +586,7 @@ bool parse_logentry( TALLOC_CTX *mem_ctx, char *line, struct eventlog_Record_tdb
                }
        } else if ( 0 == strncmp( start, "STR", stop - start ) ) {
                size_t tmp_len;
-               int num_of_strings;
+               size_t num_of_strings;
                /* skip past initial ":" */
                stop++;
                /* now skip any other leading whitespace */
@@ -676,7 +702,7 @@ struct eventlog_Record_tdb *evlog_pull_record_tdb(TALLOC_CTX *mem_ctx,
 
        blob = data_blob_const(data.dptr, data.dsize);
 
-       ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, NULL, r,
+       ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, r,
                           (ndr_pull_flags_fn_t)ndr_pull_eventlog_Record_tdb);
 
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
@@ -726,7 +752,7 @@ struct EVENTLOGRECORD *evlog_pull_record(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       r->Length = r->Length2 = ndr_size_EVENTLOGRECORD(r, NULL, 0);
+       r->Length = r->Length2 = ndr_size_EVENTLOGRECORD(r, 0);
 
        return r;
 }
@@ -763,14 +789,14 @@ NTSTATUS evlog_push_record_tdb(TALLOC_CTX *mem_ctx,
 
        /* lock */
        ret = tdb_lock_bystring_with_timeout(tdb, EVT_NEXT_RECORD, 1);
-       if (ret == -1) {
+       if (ret != 0) {
                return NT_STATUS_LOCK_NOT_GRANTED;
        }
 
        /* read */
        r->record_number = tdb_fetch_int32(tdb, EVT_NEXT_RECORD);
 
-       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, r,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, r,
                      (ndr_push_flags_fn_t)ndr_push_eventlog_Record_tdb);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
@@ -786,13 +812,13 @@ NTSTATUS evlog_push_record_tdb(TALLOC_CTX *mem_ctx,
        ebuf.dptr  = blob.data;
 
        ret = tdb_store(tdb, kbuf, ebuf, 0);
-       if (ret == -1) {
+       if (ret != 0) {
                tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
                return NT_STATUS_EVENTLOG_FILE_CORRUPT;
        }
 
        ret = tdb_store_int32(tdb, EVT_NEXT_RECORD, r->record_number + 1);
-       if (ret == -1) {
+       if (ret != 0) {
                tdb_unlock_bystring(tdb, EVT_NEXT_RECORD);
                return NT_STATUS_EVENTLOG_FILE_CORRUPT;
        }
@@ -936,11 +962,14 @@ NTSTATUS evlog_tdb_entry_to_evt_entry(TALLOC_CTX *mem_ctx,
                size_t len;
                if (!convert_string_talloc(mem_ctx, CH_UTF16, CH_UNIX,
                                           t->sid.data, t->sid.length,
-                                          (void **)&sid_str, &len, false)) {
+                                          (void *)&sid_str, &len)) {
                        return NT_STATUS_INVALID_SID;
                }
                if (len > 0) {
-                       e->UserSid = *string_sid_talloc(mem_ctx, sid_str);
+                       bool ok = string_to_sid(&e->UserSid, sid_str);
+                       if (!ok) {
+                               return NT_STATUS_INVALID_SID;
+                       }
                }
        }
 
@@ -992,7 +1021,7 @@ NTSTATUS evlog_convert_tdb_to_evt(TALLOC_CTX *mem_ctx,
                        goto done;
                }
 
-               endoffset += ndr_size_EVENTLOGRECORD(&e, NULL, 0);
+               endoffset += ndr_size_EVENTLOGRECORD(&e, 0);
 
                ADD_TO_ARRAY(mem_ctx, struct EVENTLOGRECORD, e, &evt.records, &num_records);
                count++;
@@ -1019,7 +1048,7 @@ NTSTATUS evlog_convert_tdb_to_evt(TALLOC_CTX *mem_ctx,
                NDR_PRINT_DEBUG(EVENTLOGEOF, &evt.eof);
        }
 
-       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, NULL, &evt,
+       ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &evt,
                   (ndr_push_flags_fn_t)ndr_push_EVENTLOG_EVT_FILE);
        if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                status = ndr_map_error2ntstatus(ndr_err);