Changeset 488 for trunk/src/sh_audit.c
- Timestamp:
- Sep 18, 2015, 7:39:03 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_audit.c
r434 r488 28 28 #include <errno.h> 29 29 #include <unistd.h> 30 #include <fcntl.h> 31 #include <sys/stat.h> 32 33 #include "samhain.h" 34 #include "sh_error.h" 30 35 31 36 #if !defined(SH_COMPILE_STATIC) && defined(__linux__) && defined(HAVE_AUPARSE_H) && defined(HAVE_AUPARSE_LIB) 32 37 #include <auparse.h> 33 38 34 #include "samhain.h"35 #include "sh_error.h"36 39 #include "sh_extern.h" 37 40 #include "sh_utils.h" … … 103 106 } 104 107 105 static char * doAuparse (c har * file, time_t time, char * result, size_t rsize)108 static char * doAuparse (const char * file, time_t time, int tol, char * result, size_t rsize, int redo_flag) 106 109 { 107 110 struct recordState state; 108 111 struct recordState stateFetched; 109 112 unsigned int found_flag = 0; 113 110 114 auparse_state_t * au = auparse_init(AUSOURCE_LOGS, NULL); 111 115 … … 131 135 if (time != 0) 132 136 { 133 ausearch_add_timestamp_item(au, ">=", time- 1, 0, AUSEARCH_RULE_AND);134 ausearch_add_timestamp_item(au, "<=", time+ 1, 0, AUSEARCH_RULE_AND);137 ausearch_add_timestamp_item(au, ">=", time-tol, 0, AUSEARCH_RULE_AND); 138 ausearch_add_timestamp_item(au, "<=", time+tol, 0, AUSEARCH_RULE_AND); 135 139 } 136 140 … … 152 156 { 153 157 memcpy(&state, &stateFetched, sizeof(state)); 158 ++found_flag; 154 159 } 155 160 auparse_next_event(au); 156 161 } 157 162 163 if (found_flag == 0 && redo_flag == S_FALSE) 164 { 165 size_t len = strlen(file); 166 char * path = SH_ALLOC(len + 2); 167 char * altres; 168 169 sl_strlcpy(path, file, len+2); 170 path[len] = '/'; path[len+1] = '\0'; 171 auparse_destroy(au); 172 173 altres = doAuparse(path, time, tol, result, rsize, S_TRUE); 174 175 SH_FREE(path); 176 return altres; 177 } 178 158 179 if (0 == strcmp(state.success, "yes")) 159 180 { … … 189 210 * The 'result' array should be sized ~256 char. 190 211 */ 191 char * sh_audit_fetch (char * file, time_t time, char * result, size_t rsize)212 char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, char * result, size_t rsize) 192 213 { 193 214 char * res = NULL; … … 195 216 if (sh_audit_checkdaemon() >= 0) 196 217 { 197 res = doAuparse (file, time, result, rsize); 218 time_t new; 219 char buf[64]; 220 221 if (mtime >= ctime) { new = mtime; } 222 else { new = ctime; } 223 224 res = doAuparse (file, new, 1, result, rsize, S_FALSE); 198 225 199 226 if (!res) 200 227 { 201 res = doAuparse (file, 0, result, rsize);228 res = doAuparse (file, new, 3, result, rsize, S_FALSE); 202 229 } 230 203 231 } 204 232 return res; … … 226 254 } 227 255 256 static int sh_audit_isdir(const char * file) 257 { 258 struct stat buf; 259 260 if ( (0 == lstat (file, &buf)) && S_ISDIR(buf.st_mode)) 261 return S_TRUE; 262 return S_FALSE; 263 } 264 228 265 static void sh_audit_mark_int (const char * file) 229 266 { … … 246 283 char * safe; 247 284 char ctl[64]; 285 char a1[32]; 286 char a2[32]; 287 char a3[32]; 248 288 249 289 sl_snprintf(command, len, _("%s -w %s -p wa -k samhain"), … … 256 296 safe, 257 297 _("sh_audit_mark") ); 298 258 299 SH_FREE(safe); 259 300 … … 261 302 sl_strlcpy(command, file, len); 262 303 263 sh_ext_system(ctl, ctl, "-w", command, "-p", "wa", "-k", _("samhain"), NULL); 264 304 sl_strlcpy(a3, _("samhain"), sizeof(a3)); 305 sh_ext_system(ctl, ctl, "-w", command, "-p", "wa", "-k", a3, NULL); 306 307 /* Placing a watch on a directory will not place a watch on the 308 * directory inode, so we do this explicitely. 309 */ 310 if (S_TRUE == sh_audit_isdir(file)) 311 { 312 safe = sh_util_safe_name(file); 313 sh_error_handle (SH_ERR_ALL, FIL__, __LINE__, 314 0, MSG_E_SUBGPATH, 315 _("Add path watch for directory"), 316 _("sh_audit_mark_int"), safe ); 317 SH_FREE(safe); 318 sl_strlcpy(command, _("path="), len); 319 sl_strlcat(command, file, len); 320 sl_strlcpy(a1, _("always,exit"), sizeof(a1)); 321 sl_strlcpy(a2, _("perm=wa"), sizeof(a2)); 322 sh_ext_system(ctl, ctl, "-a", a1, "-F", command, "-F", a2, "-k", a3, NULL); 323 } 265 324 SH_FREE(command); 266 325 } … … 278 337 { 279 338 struct aud_list * this = SH_ALLOC(sizeof(struct aud_list)); 339 size_t len = strlen(file); 340 280 341 this->file = sh_util_strdup(file); 342 if ((len > 1) && (file[len-1] == '/')) 343 this->file[len-1] = '\0'; 344 281 345 this->next = mark_these; 282 346 mark_these = this; … … 284 348 } 285 349 350 /* Check whether it is already covered by a higher directory 351 */ 286 352 static int test_exchange (struct aud_list * this, char * file) 287 353 { … … 312 378 if (0 == strncmp(s0, s1, len1 + 1)) 313 379 { 380 size_t len = strlen(file); 314 381 SH_FREE(this->file); 315 382 this->file = sh_util_strdup(file); 383 if ((len > 1) && (file[len-1] == '/')) 384 this->file[len-1] = '\0'; 316 385 ret = 0; 317 386 } … … 324 393 } 325 394 395 /* Place a path on the list of of paths to be watched 396 */ 326 397 void sh_audit_mark (char * file) 327 398 { … … 335 406 while (this) 336 407 { 408 /* Check whether it is already covered by a higher 409 * directory 410 */ 337 411 if (0 == test_exchange(this, file)) 338 412 return; … … 464 538 /* HAVE_AUPARSE_H */ 465 539 #else 466 char * sh_audit_fetch (char * file, time_t time, char * result, size_t rsize)540 char * sh_audit_fetch (char * file, time_t mtime, time_t ctime, char * result, size_t rsize) 467 541 { 468 542 (void) file; 469 (void) time; 543 (void) mtime; 544 (void) ctime; 470 545 (void) result; 471 546 (void) rsize; … … 476 551 { 477 552 (void) file; 553 sh_error_handle(SH_ERR_WARN, FIL__, __LINE__, 0, MSG_E_SUBGEN, 554 _("Setting audit watch not supported"), 555 _("sh_audit_mark")); 478 556 return; 479 557 }
Note:
See TracChangeset
for help on using the changeset viewer.