|
| 1 | + |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdio.h> |
| 4 | +#include <dirent.h> |
| 5 | +#include <string.h> |
| 6 | +#include <sys/stat.h> |
| 7 | + |
| 8 | +#include "mlogc-ng.h" |
| 9 | +#include "input_batch.h" |
| 10 | +#include "read_from_filesystem.h" |
| 11 | + |
| 12 | +int inspect_file(unsigned char *full_path, void *audit_log_entry_cb, |
| 13 | + struct read_from_filesystem_config_t *conf) |
| 14 | +{ |
| 15 | + int res = 0; |
| 16 | + FILE *fp; |
| 17 | + struct stat s; |
| 18 | + char *file_content_buf = NULL; |
| 19 | + struct audit_log_entry_t audit_log; |
| 20 | + void (*ptrEntry)(struct audit_log_entry_t *, |
| 21 | + struct read_from_filesystem_config_t *); |
| 22 | + ptrEntry = audit_log_entry_cb; |
| 23 | + |
| 24 | + memset(&audit_log, '\0', sizeof(audit_log)); |
| 25 | + |
| 26 | + d("inspecting: %s\n", full_path); |
| 27 | + |
| 28 | + res = stat(full_path, &s); |
| 29 | + if (res < 0) |
| 30 | + { |
| 31 | + e("Not able to get file size: %s\n", full_path); |
| 32 | + goto failed; |
| 33 | + } |
| 34 | + |
| 35 | + if (s.st_size > MAX_FILE_SIZE) |
| 36 | + { |
| 37 | + e("This file is too big for us. Limit is: %d bytes\n", MAX_FILE_SIZE); |
| 38 | + res = -1; |
| 39 | + goto failed; |
| 40 | + } |
| 41 | + |
| 42 | + file_content_buf = malloc(s.st_size + 1); |
| 43 | + if (file_content_buf == NULL) |
| 44 | + { |
| 45 | + e("Failed to allocate memory to read: %s\n", full_path); |
| 46 | + res = -1; |
| 47 | + goto failed; |
| 48 | + } |
| 49 | + |
| 50 | + /* Everything is ok, lets have the data inside a buffer. */ |
| 51 | + fp = fopen(full_path, "r"); |
| 52 | + if (fp == NULL) |
| 53 | + { |
| 54 | + e("Failed to open the file: %s\n", full_path); |
| 55 | + res = -1; |
| 56 | + goto failed_fp; |
| 57 | + } |
| 58 | + |
| 59 | + fread(file_content_buf, sizeof(char), s.st_size, fp); |
| 60 | + fclose(fp); |
| 61 | + |
| 62 | + audit_log.hostname = "-"; |
| 63 | + audit_log.remote_addr = "-"; |
| 64 | + audit_log.remote_user = "-"; |
| 65 | + audit_log.local_user = "-"; |
| 66 | + audit_log.logtime = "-"; |
| 67 | + audit_log.request = "-"; |
| 68 | + audit_log.response_status = "-"; |
| 69 | + audit_log.bytes_sent = "-"; |
| 70 | + audit_log.refer = "-"; |
| 71 | + audit_log.user_agent = "-"; |
| 72 | + audit_log.uniqueid = "-"; |
| 73 | + audit_log.sessionid = "-"; |
| 74 | + audit_log.audit_file = full_path; |
| 75 | + audit_log.extra = "0"; |
| 76 | + audit_log.md5 = "-"; |
| 77 | + audit_log.audit_size = "0"; |
| 78 | + |
| 79 | + if (load_buffer(file_content_buf, &audit_log) == 0) |
| 80 | + { |
| 81 | + (*ptrEntry)(&audit_log, conf); |
| 82 | + } |
| 83 | + |
| 84 | +failed_fp: |
| 85 | + free(file_content_buf); |
| 86 | +failed: |
| 87 | + return res; |
| 88 | +} |
| 89 | + |
| 90 | +int open_directory_recursive(unsigned char *path, void *audit_log_entry_cb, |
| 91 | + struct read_from_filesystem_config_t *conf) |
| 92 | +{ |
| 93 | + DIR *d; |
| 94 | + struct dirent *entry; |
| 95 | + int res = 0; |
| 96 | + |
| 97 | + d = opendir(path); |
| 98 | + if (d == NULL) |
| 99 | + { |
| 100 | + e("Failed to open directory: %s\n", path); |
| 101 | + res = -1; |
| 102 | + goto failed; |
| 103 | + } |
| 104 | + |
| 105 | + entry = readdir(d); |
| 106 | + while (entry != NULL) |
| 107 | + { |
| 108 | + const char *d_name = NULL; |
| 109 | + int full_path_len = 0; |
| 110 | + char *full_path = NULL; |
| 111 | + |
| 112 | + d_name = entry->d_name; |
| 113 | + |
| 114 | + full_path_len = snprintf(NULL, 0, "%s/%s", path, d_name); |
| 115 | + full_path = malloc(sizeof(char) * (full_path_len + 1)); |
| 116 | + if (full_path == NULL) |
| 117 | + { |
| 118 | + e("Failed: missing memory to continue.\n"); |
| 119 | + return -1; |
| 120 | + } |
| 121 | + |
| 122 | + full_path_len = snprintf(full_path, full_path_len + 1, "%s/%s", path, |
| 123 | + d_name); |
| 124 | + |
| 125 | + /* if it is a dir (different from "." or ".." we want to jump in. */ |
| 126 | + if (entry->d_type & DT_DIR) |
| 127 | + { |
| 128 | + if (strcmp (d_name, "..") != 0 && strcmp (d_name, ".") != 0) |
| 129 | + { |
| 130 | + /* FIXME: we should not ignore this return value. */ |
| 131 | + open_directory_recursive(full_path, audit_log_entry_cb, conf); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /* if it is a file, let see inside. */ |
| 136 | + if (!(entry->d_type & DT_DIR)) |
| 137 | + { |
| 138 | + inspect_file(full_path, audit_log_entry_cb, conf); |
| 139 | + } |
| 140 | + |
| 141 | + free(full_path); |
| 142 | + |
| 143 | + entry = readdir(d); |
| 144 | + } |
| 145 | + |
| 146 | +failed_read_dir: |
| 147 | + closedir(d); |
| 148 | +failed: |
| 149 | + return res; |
| 150 | +} |
| 151 | + |
0 commit comments