1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <locale.h>
#include <errno.h>
#include "libaudit.h"
#include "auparse.h"
// NOTE: First two run together on purpose, #3 is buf[1]
static const char *buf[] = {
"type=LOGIN msg=audit(1143146623.787:142): login pid=2027 uid=0 old auid=4294967295 new auid=848\n"
"type=SYSCALL msg=audit(1143146623.875:143): arch=c000003e syscall=188 success=yes exit=0 a0=7fffffa9a9f0 a1=3958d11333 a2=5131f0 a3=20 items=1 pid=2027 auid=848 uid=0 gid=0 euid=0 suid=0 fsuid=0 egid=0 sgid=0 fsgid=0 tty=tty3 comm=\"login\" exe=\"/bin/login\" subj=system_u:system_r:local_login_t:s0-s0:c0.c255\n",
"type=USER_LOGIN msg=audit(1143146623.879:146): user pid=2027 uid=0 auid=848 msg=\'uid=848: exe=\"/bin/login\" (hostname=?, addr=?, terminal=tty3 res=success)\'\n",
NULL
};
unsigned int walked_fields = 0;
#define FIELDS_EXPECTED 403
static void walk_test(auparse_state_t *au, int interpret)
{
int event_cnt = 1, record_cnt;
do {
if (auparse_first_record(au) <= 0) {
printf("Error getting first record (%s)\n",
strerror(errno));
exit(1);
}
printf("event %d has %u records\n", event_cnt,
auparse_get_num_records(au));
record_cnt = 1;
do {
printf(" record %d of type %d(%s) has %u fields\n",
record_cnt,
auparse_get_type(au),
audit_msg_type_to_name(auparse_get_type(au)),
auparse_get_num_fields(au));
printf(" line=%u file=%s\n",
auparse_get_line_number(au),
auparse_get_filename(au) ?
auparse_get_filename(au) : "None");
const au_event_t *e = auparse_get_timestamp(au);
if (e == NULL) {
printf("Error getting timestamp - aborting\n");
exit(1);
}
printf(" event time: %llu.%u:%lu, host=%s\n",
(unsigned long long)e->sec,
e->milli, e->serial, e->host ? e->host : "?");
auparse_first_field(au);
do {
if (interpret) {
printf(" %s=%s (%s)\n",
auparse_get_field_name(au),
auparse_get_field_str(au),
auparse_interpret_field(au));
} else {
printf(" %s=%s\n",
auparse_get_field_name(au),
auparse_get_field_str(au));
}
walked_fields++;
} while (auparse_next_field(au) > 0);
printf("\n");
record_cnt++;
} while(auparse_next_record(au) > 0);
event_cnt++;
} while (auparse_next_event(au) > 0);
}
void light_test(auparse_state_t *au)
{
int record_cnt;
do {
if (auparse_first_record(au) <= 0) {
puts("Error getting first record");
exit(1);
}
printf("event has %u records\n", auparse_get_num_records(au));
record_cnt = 1;
do {
printf(" record %d of type %d(%s) has %u fields\n",
record_cnt,
auparse_get_type(au),
audit_msg_type_to_name(auparse_get_type(au)),
auparse_get_num_fields(au));
printf(" line=%u file=%s\n",
auparse_get_line_number(au),
auparse_get_filename(au) ?
auparse_get_filename(au) : "None");
const au_event_t *e = auparse_get_timestamp(au);
if (e == NULL) {
printf("Error getting timestamp - aborting\n");
exit(1);
}
printf(" event time: %llu.%u:%lu, host=%s\n",
(unsigned long long)e->sec,
e->milli, e->serial,
e->host ? e->host : "?");
printf("\n");
record_cnt++;
} while(auparse_next_record(au) > 0);
} while (auparse_next_event(au) > 0);
}
void simple_search(ausource_t source, austop_t where)
{
auparse_state_t *au;
const char *val;
if (source == AUSOURCE_FILE) {
au = auparse_init(AUSOURCE_FILE, "./test.log");
val = "4294967295";
} else {
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
val = "848";
}
if (au == NULL) {
printf("auparse_init error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_add_item(au, "auid", "=", val, AUSEARCH_RULE_CLEAR)){
printf("ausearch_add_item error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_set_stop(au, where)){
printf("ausearch_set_stop error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_next_event(au) <= 0)
printf("Error searching for auid - %s\n", strerror(errno));
else
printf("Found %s = %s\n", auparse_get_field_name(au),
auparse_get_field_str(au));
auparse_destroy(au);
}
void compound_search(ausearch_rule_t how)
{
auparse_state_t *au;
au = auparse_init(AUSOURCE_FILE, "./test.log");
if (au == NULL) {
printf("auparse_init error - %s\n", strerror(errno));
exit(1);
}
if (how == AUSEARCH_RULE_AND) {
if (ausearch_add_item(au, "uid", "=", "0",
AUSEARCH_RULE_CLEAR)){
printf("ausearch_add_item 1 error - %s\n",
strerror(errno));
exit(1);
}
if (ausearch_add_item(au, "pid", "=", "13015", how)){
printf("ausearch_add_item 2 error - %s\n",
strerror(errno));
exit(1);
}
if (ausearch_add_item(au, "type", "=", "USER_START", how)){
printf("ausearch_add_item 3 error - %s\n",
strerror(errno));
exit(1);
}
} else {
if (ausearch_add_item(au, "auid", "=", "42",
AUSEARCH_RULE_CLEAR)){
printf("ausearch_add_item 4 error - %s\n",
strerror(errno));
exit(1);
}
// should stop on this one
if (ausearch_add_item(au, "auid", "=", "0", how)){
printf("ausearch_add_item 5 error - %s\n",
strerror(errno));
exit(1);
}
if (ausearch_add_item(au, "auid", "=", "500", how)){
printf("ausearch_add_item 6 error - %s\n",
strerror(errno));
exit(1);
}
}
if (ausearch_set_stop(au, AUSEARCH_STOP_FIELD)){
printf("ausearch_set_stop error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_next_event(au) <= 0)
printf("Error searching for auid - %s\n", strerror(errno));
else
printf("Found %s = %s\n", auparse_get_field_name(au),
auparse_get_field_str(au));
auparse_destroy(au);
}
void regex_search(const char *expr)
{
auparse_state_t *au;
int rc;
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
if (au == NULL) {
printf("auparse_init error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_add_regex(au, expr)){
printf("ausearch_add_regex error - %s\n", strerror(errno));
exit(1);
}
if (ausearch_set_stop(au, AUSEARCH_STOP_RECORD)){
printf("ausearch_set_stop error - %s\n", strerror(errno));
exit(1);
}
rc = ausearch_next_event(au);
if (rc < 0)
printf("Error searching for %s - %s\n", expr, strerror(errno));
else if (rc == 0)
printf("Not found\n");
else
printf("Found %s = %s\n", auparse_get_field_name(au),
auparse_get_field_str(au));
auparse_destroy(au);
}
typedef struct {
int *event_cnt;
int interpret;
} callback_data_t;
static void auparse_callback(auparse_state_t *au, auparse_cb_event_t cb_event_type, void *user_data)
{
callback_data_t *data = (callback_data_t *)user_data;
int record_cnt;
if (cb_event_type == AUPARSE_CB_EVENT_READY) {
if (auparse_first_record(au) <= 0) {
printf("can't get first record\n");
return;
}
printf("event %d has %u records\n", *(data->event_cnt),
auparse_get_num_records(au));
record_cnt = 1;
do {
printf(" record %d of type %d(%s) has %u fields\n",
record_cnt,
auparse_get_type(au),
audit_msg_type_to_name(auparse_get_type(au)),
auparse_get_num_fields(au));
printf(" line=%u file=%s\n",
auparse_get_line_number(au),
auparse_get_filename(au) ?
auparse_get_filename(au) : "None");
const au_event_t *e = auparse_get_timestamp(au);
if (e == NULL) {
return;
}
printf(" event time: %llu.%u:%lu, host=%s\n",
(unsigned long long)e->sec,
e->milli, e->serial,
e->host ? e->host : "?");
auparse_first_field(au);
do {
if (data->interpret) {
printf(" %s=%s (%s)\n",
auparse_get_field_name(au),
auparse_get_field_str(au),
auparse_interpret_field(au));
} else {
printf(" %s=%s\n",
auparse_get_field_name(au),
auparse_get_field_str(au));
}
} while (auparse_next_field(au) > 0);
printf("\n");
record_cnt++;
} while(auparse_next_record(au) > 0);
(*(data->event_cnt))++;
}
}
int main(void)
{
//char *files[4] = { "test.log", "test2.log", "test3.log", NULL };
char *files[3] = { "test2.log", "test.log", NULL };
setlocale (LC_ALL, "");
auparse_state_t *au;
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
printf("Starting Test 1, iterate...\n");
while (auparse_next_event(au) > 0) {
if (auparse_find_field(au, "auid")) {
printf("%s=%s\n", auparse_get_field_name(au),
auparse_get_field_str(au));
printf("interp auid=%s\n", auparse_interpret_field(au));
} else
printf("Error iterating to auid\n");
}
auparse_reset(au);
while (auparse_next_event(au) > 0) {
if (auparse_find_field(au, "auid")) {
do {
printf("%s=%s\n", auparse_get_field_name(au),
auparse_get_field_str(au));
printf("interp auid=%s\n", auparse_interpret_field(au));
} while (auparse_find_field_next(au));
} else
printf("Error iterating to auid\n");
}
printf("Test 1 Done\n\n");
/* Reset, now lets go to beginning and walk the list manually */
printf("Starting Test 2, walk events, records, and fields...\n");
auparse_reset(au);
walk_test(au, 1);
auparse_destroy(au);
printf("Test 2 Done\n\n");
/* Reset, now lets go to beginning and walk the list manually */
printf("Starting Test 3, walk events, records of 1 buffer...\n");
au = auparse_init(AUSOURCE_BUFFER, buf[1]);
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
light_test(au);
auparse_destroy(au);
printf("Test 3 Done\n\n");
printf("Starting Test 4, walk events, records of 1 file...\n");
au = auparse_init(AUSOURCE_FILE, "./test.log");
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
walk_test(au, 0);
auparse_destroy(au);
printf("Test 4 Done\n\n");
printf("Starting Test 5, walk events, records of 2 files...\n");
au = auparse_init(AUSOURCE_FILE_ARRAY, files);
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
walk_test(au, 0);
auparse_destroy(au);
printf("Test 5 Done\n\n");
printf("Starting Test 6, search...\n");
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
if (ausearch_add_item(au, "auid", "=", "500", AUSEARCH_RULE_CLEAR)){
printf("Error - %s", strerror(errno));
return 1;
}
if (ausearch_set_stop(au, AUSEARCH_STOP_EVENT)){
printf("Error - %s", strerror(errno));
exit(1);
}
if (ausearch_next_event(au) != 0) {
printf("Error search found something it shouldn't have\n");
}
puts("auid = 500 not found...which is correct");
ausearch_clear(au);
auparse_destroy(au);
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
if (ausearch_add_item(au,"auid", "exists", NULL, AUSEARCH_RULE_CLEAR)){
printf("Error - %s", strerror(errno));
return 1;
}
if (ausearch_set_stop(au, AUSEARCH_STOP_EVENT)){
printf("Error - %s", strerror(errno));
exit(1);
}
if (ausearch_next_event(au) <= 0) {
printf("Error searching for existence of auid\n");
}
puts("auid exists...which is correct");
puts("Testing BUFFER_ARRAY, stop on field");
simple_search(AUSOURCE_BUFFER_ARRAY, AUSEARCH_STOP_FIELD);
puts("Testing BUFFER_ARRAY, stop on record");
simple_search(AUSOURCE_BUFFER_ARRAY, AUSEARCH_STOP_RECORD);
puts("Testing BUFFER_ARRAY, stop on event");
simple_search(AUSOURCE_BUFFER_ARRAY, AUSEARCH_STOP_EVENT);
puts("Testing test.log, stop on field");
simple_search(AUSOURCE_FILE, AUSEARCH_STOP_FIELD);
puts("Testing test.log, stop on record");
simple_search(AUSOURCE_FILE, AUSEARCH_STOP_RECORD);
puts("Testing test.log, stop on event");
simple_search(AUSOURCE_FILE, AUSEARCH_STOP_EVENT);
auparse_destroy(au);
printf("Test 6 Done\n\n");
printf("Starting Test 7, compound search...\n");
au = auparse_init(AUSOURCE_BUFFER_ARRAY, buf);
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
compound_search(AUSEARCH_RULE_AND);
compound_search(AUSEARCH_RULE_OR);
auparse_destroy(au);
printf("Test 7 Done\n\n");
printf("Starting Test 8, regex search...\n");
puts("Doing regex match...");
regex_search("1143146623");
puts("Doing regex wildcard search...");
regex_search("11431466.*146");
printf("Test 8 Done\n\n");
/* Note: this should match Test 2 exactly */
printf("Starting Test 9, buffer feed...\n");
{
int event_cnt = 1;
callback_data_t cb_data = { &event_cnt, 1 };
size_t len, chunk_len = 3;
const char **cur_buf, *p_beg, *p_end, *p_chunk_beg,
*p_chunk_end;
au = auparse_init(AUSOURCE_FEED, 0);
auparse_add_callback(au, auparse_callback, &cb_data, NULL);
for (cur_buf = buf, p_beg = *cur_buf; *cur_buf;
cur_buf++, p_beg = *cur_buf) {
len = strlen(p_beg);
p_end = p_beg + len;
p_chunk_beg = p_beg;
while (p_chunk_beg < p_end) {
p_chunk_end = p_chunk_beg + chunk_len;
if (p_chunk_end > p_end)
p_chunk_end = p_end;
//fwrite(p_chunk_beg, 1,
// p_chunk_end-p_chunk_beg, stdout);
auparse_feed(au, p_chunk_beg,
p_chunk_end-p_chunk_beg);
p_chunk_beg = p_chunk_end;
}
}
auparse_flush_feed(au);
auparse_destroy(au);
}
printf("Test 9 Done\n\n");
/* Note: this should match Test 4 exactly */
printf("Starting Test 10, file feed...\n");
{
int event_cnt = 1;
callback_data_t cb_data = { &event_cnt, 0 };
size_t len;
char filename[] = "./test.log";
char buf[4];
FILE *fp;
au = auparse_init(AUSOURCE_FEED, 0);
auparse_add_callback(au, auparse_callback, &cb_data, NULL);
if ((fp = fopen(filename, "r")) == NULL) {
fprintf(stderr, "could not open '%s', %s\n",
filename, strerror(errno));
return 1;
}
while ((len = fread(buf, 1, sizeof(buf), fp))) {
auparse_feed(au, buf, len);
}
fclose(fp);
auparse_flush_feed(au);
auparse_destroy(au);
}
printf("Test 10 Done\n\n");
printf("Starting Test 11, walk LONG event records from a file...\n");
au = auparse_init(AUSOURCE_FILE, "test4.log");
if (au == NULL) {
printf("Error - %s\n", strerror(errno));
return 1;
}
walked_fields = 0;
walk_test(au, 0);
auparse_destroy(au);
if (walked_fields != FIELDS_EXPECTED) {
printf("Error: %i fields expected, but %i read!\n",
FIELDS_EXPECTED, walked_fields);
}
printf("Test 11 Done\n\n");
puts("Finished non-admin tests\n");
return 0;
}
|