forked from linux-audit/audit-userspace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauparse.c
More file actions
2294 lines (2030 loc) · 50.8 KB
/
auparse.c
File metadata and controls
2294 lines (2030 loc) · 50.8 KB
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* auparse.c --
* Copyright 2006-08,2012-23 Red Hat Inc.
* All Rights Reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1335, USA.
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*/
#include "config.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio_ext.h>
#include <limits.h>
#include "internal.h"
#include "expression.h"
#include "auparse.h"
#include "interpret.h"
#include "auparse-idata.h"
#include "libaudit.h"
#include "common.h"
//#define LOL_EVENTS_DEBUG01 1 // add debug for list of list event
// processing
#ifdef LOL_EVENTS_DEBUG01
static int debug = 0;
#endif
static time_t eoe_timeout = EOE_TIMEOUT;
/* like strchr except string is delimited by length, not null byte */
static char *strnchr(const char *s, int c, size_t n)
{
char *p_char;
const char *p_end = s + n;
for (p_char = (char *)s; p_char < p_end && *p_char != c; p_char++);
if (p_char == p_end) return NULL;
return p_char;
}
static int access_ok(const char *filename)
{
int rc = access(filename, R_OK);
if (rc == 0)
return rc;
#ifdef HAVE_FACCESSAT
// If we have faccessat, let's try effective ids.
return faccessat(AT_FDCWD, filename, R_OK, AT_EACCESS);
#else
// If we don't, return what we have from access()
return rc;
#endif
}
static int setup_log_file_array(auparse_state_t *au)
{
struct daemon_conf config;
char *filename, **tmp;
int len, num = 0, i = 0;
/* Load config so we know where logs are */
if (secure_getenv("AUPARSE_DEBUG"))
set_aumessage_mode(au, MSG_STDERR, DBG_NO);
aup_load_config(au, &config, TEST_SEARCH);
/* for each file */
len = strlen(config.log_file) + 16;
filename = malloc(len);
if (!filename) {
fprintf(stderr, "No memory\n");
aup_free_config(&config);
return 1;
}
/* Find oldest log file */
snprintf(filename, len, "%s", config.log_file);
do {
if (access_ok(filename) != 0)
break;
num++;
snprintf(filename, len, "%s.%d", config.log_file, num);
} while (1);
if (num == 0) {
fprintf(stderr, "No log file\n");
aup_free_config(&config);
free(filename);
return 1;
}
num--;
tmp = malloc((num+2)*sizeof(char *));
if (!tmp) {
fprintf(stderr, "Out of memory. Check %s file, %d line", __FILE__, __LINE__);
aup_free_config(&config);
free(filename);
return 1;
}
/* Got it, now process logs from last to first */
if (num > 0)
snprintf(filename, len, "%s.%d", config.log_file, num);
else
snprintf(filename, len, "%s", config.log_file);
do {
tmp[i++] = strdup(filename);
/* Get next log file */
num--;
if (num > 0)
snprintf(filename, len, "%s.%d", config.log_file, num);
else if (num == 0)
snprintf(filename, len, "%s", config.log_file);
else
break;
} while (1);
aup_free_config(&config);
free(filename);
// Terminate the list
tmp[i] = NULL;
au->source_list = tmp;
return 0;
}
/*
* au_lol_create - Create and initialise the base List of List event structure
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* Rtns:
* NULL - no memory
* ptr - pointer to array of event nodes (au_lolnode)
*/
static au_lolnode *au_lol_create(au_lol *lol)
{
int sz = ARRAY_LIMIT * sizeof(au_lolnode);
lol->maxi = -1;
if ((lol->array = (au_lolnode *)malloc(sz)) == NULL)
return NULL;
lol->limit = ARRAY_LIMIT;
memset(lol->array, 0x00, sz);
return lol->array;
}
/*
* au_lol_clear - Free or rest the base List of List event structure
*
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* reset - flag to indicate a reset of the structure, or the complete
* freeing of memory
* Rtns:
* void
*/
static void au_lol_clear(au_lol *lol, int reset)
{
int i;
if (lol->array) {
for (i = 0; i <= lol->maxi; i++) {
if (lol->array[i].l) {
aup_list_clear(lol->array[i].l);
free(lol->array[i].l);
}
}
}
if (reset) {
/* If resetting, we just zero fields */
if (lol->array)
memset(lol->array, 0x00,
lol->limit * sizeof(au_lolnode));
lol->maxi = -1;
} else {
/* If not resetting, we free everything */
if (lol->array) free(lol->array);
lol->array = NULL;
lol->maxi = -1;
}
}
/*
* au_lol_append - Add a new event to our base List of List structure
*
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* l - event list structure (which contains an event's constituent records)
* Rtns:
* ptr - pointer to au_lolnode which holds the event list structure
* NULL - failed to reallocate memory
*/
static au_lolnode *au_lol_append(au_lol *lol, event_list_t *l)
{
int i;
size_t new_size;
au_lolnode *ptr;
for (i = 0; i < lol->limit; i++) {
au_lolnode *cur = &lol->array[i];
if (cur->status == EBS_EMPTY) {
cur->l = l;
cur->status = EBS_BUILDING;
if (i > lol->maxi)
lol->maxi = i;
return cur;
}
}
/* Over ran the array, make it bigger */
new_size = sizeof(au_lolnode) * (lol->limit + ARRAY_LIMIT);
ptr = realloc(lol->array, new_size);
if (ptr) {
lol->array = ptr;
memset(&lol->array[lol->limit], 0x00,
sizeof(au_lolnode) * ARRAY_LIMIT);
lol->array[i].l = l;
lol->array[i].status = EBS_BUILDING;
lol->maxi = i;
lol->limit += ARRAY_LIMIT;
}
return ptr;
}
/*
* au_get_ready_event - Find the next COMPLETE event in our list and mark EMPTY
*
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* is_test - do not mark the node EMPTY
* Rtns:
* ptr - pointer to complete node (possibly just marked empty)
* NULL - no complete nodes exist
*/
static event_list_t *au_get_ready_event(auparse_state_t *au, int is_test)
{
int i;
au_lol *lol = au->au_lo;
au_lolnode *lowest = NULL;
if (au->au_ready == 0) {
//if (debug) printf("No events ready\n");
return NULL;
}
for (i=0; i<=lol->maxi; i++) {
// Look for the event with the lowest timestamp
au_lolnode *cur = &(lol->array[i]);
if (cur->status == EBS_EMPTY)
continue;
// If we are just testing for a complete event, return
if (is_test && cur->status == EBS_COMPLETE)
return cur->l;
if (lowest == NULL)
lowest = cur;
else if (auparse_timestamp_compare(&(lowest->l->e),
&(cur->l->e)) == 1)
lowest = cur;
}
if (lowest && lowest->status == EBS_COMPLETE) {
lowest->status = EBS_EMPTY;
au->au_ready--;
return lowest->l;
}
return NULL;
}
/*
* au_check_events - Run though all events marking those we can mark COMPLETE
*
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* sec - time of current event from stream being processed. We use this to see
* how old the events are we have in our list
* Rtns:
* void
*/
static void au_check_events(auparse_state_t *au, time_t sec)
{
rnode *r;
int i;
au_lol *lol = au->au_lo;
for(i=0; i<=lol->maxi; i++) {
au_lolnode *cur = &lol->array[i];
if (cur->status == EBS_BUILDING) {
if ((r = aup_list_get_cur(cur->l)) == NULL)
continue;
// If eoe_timeout seconds have elapsed, we are done
if (cur->l->e.sec + eoe_timeout <= sec) {
cur->status = EBS_COMPLETE;
au->au_ready++;
} else if (audit_is_last_record(r->type)) {
// If known to be 1 record event, we are done
cur->status = EBS_COMPLETE;
au->au_ready++;
}
}
}
}
/*
* au_terminate_all_events - Mark all events in 'BUILD' state to be COMPLETE
*
* Args:
* lol - pointer to memory holding structure (eg the static au_lo variable)
* Rtns:
* void
*/
static void au_terminate_all_events(auparse_state_t *au)
{
int i;
au_lol *lol = au->au_lo;
for (i=0; i<=lol->maxi; i++) {
au_lolnode *cur = &lol->array[i];
if (cur->status == EBS_BUILDING) {
cur->status = EBS_COMPLETE;
au->au_ready++;
//if (debug) printf("%d events complete\n", au->au_ready);
}
}
}
#ifdef LOL_EVENTS_DEBUG01
/*
* print_list_t - Print summary of event's records
* Args:
* l - event_list to print
* Rtns:
* void
*/
void print_list_t(event_list_t *l)
{
rnode *r;
if (l == NULL) {
printf("\n");
return;
}
printf("0x%p: %lld.%3.3u:%lu %s", l, (long long int)l->e.sec, l->e.milli,
l->e.serial, l->e.host ? l->e.host : "");
printf(" cnt=%u", l->cnt);
for (r = l->head; r != NULL; r = r->next) {
printf(" {%d %d %u}", r->type, r->list_idx, r->line_number);
}
printf("\n");
}
/*
* lol_status - return type of event state as a character
* Args:
* s - event state
* Rtns:
* char - E, B or C for EMPTY, BUILDING or COMPLETE, or '*' for unknown
*/
static char lol_status(au_lol_t s)
{
switch(s) {
case EBS_EMPTY: return 'E'; break;
case EBS_BUILDING: return 'B'; break;
case EBS_COMPLETE: return 'C'; break;
}
return '*';
}
/*
* print_lol - Print a list of list events and their records
* Args:
* label - String to act as label when printing
* lol - pointer to memory holding structure (eg the static au_lo variable)
* Rtns:
* void
*/
void print_lol(char *label, au_lol *lol)
{
int i;
printf("%s 0x%p: a: 0x%p, %d, %d\n", label, lol, lol->array,
lol->maxi, lol->limit);
if (debug > 1) for (i = 0; i <= lol->maxi; i++) {
printf("{%2d 0x%p %c } ", i, (&lol->array[i]),
lol_status(lol->array[i].status));
print_list_t(lol->array[i].l);
}
if (lol->maxi >= 0)
printf("\n");
}
#endif /* LOL_EVENTS_DEBUG01 */
/* General functions that affect operation of the library */
/*
* au_setup_userspace_configitems - load userspace configuration items from auditd.conf
*
* Args:
* au - pointer to auparseing state structure
*
* Rtns:
* void
*/
static void au_setup_userspace_configitems(auparse_state_t *au)
{
struct daemon_conf config;
/* Load config so we know where logs are */
if (secure_getenv("AUPARSE_DEBUG"))
set_aumessage_mode(au, MSG_STDERR, DBG_NO);
aup_load_config(au, &config, TEST_SEARCH);
eoe_timeout = (time_t)config.end_of_event_timeout;
aup_free_config(&config);
}
auparse_state_t *auparse_init(ausource_t source, const void *b)
{
char **tmp, **bb = (char **)b, *buf = (char *)b;
int n, i;
size_t size, len;
auparse_state_t *au = malloc(sizeof(auparse_state_t));
if (au == NULL) {
errno = ENOMEM;
return NULL;
}
au->le = NULL;
/*
* Set up the List of List events base structure
*/
au->au_lo = calloc(sizeof(au_lol), 1);
if (au->au_lo == NULL) {
free(au);
errno = ENOMEM;
return NULL;
}
au_lol_clear(au->au_lo, 0); // python doesn't call auparse_destroy
if (au_lol_create(au->au_lo) == NULL) {
free(au->au_lo);
free(au);
errno = ENOMEM;
return NULL;
}
au->au_ready = 0;
au->escape_mode = AUPARSE_ESC_TTY;
au->message_mode = MSG_QUIET;
au->debug_message = DBG_NO;
au->in = NULL;
au->source_list = NULL;
databuf_init(&au->databuf, 0, 0);
au->callback = NULL;
au->callback_user_data = NULL;
au->callback_user_data_destroy = NULL;
au_setup_userspace_configitems(au);
switch (source)
{
case AUSOURCE_LOGS:
if (setup_log_file_array(au))
goto bad_exit;
break;
case AUSOURCE_FILE:
if (b == NULL)
goto bad_exit;
if (access_ok(b))
goto bad_exit;
tmp = malloc(2*sizeof(char *));
if (tmp == NULL)
goto bad_exit;
tmp[0] = strdup(b);
tmp[1] = NULL;
au->source_list = tmp;
break;
case AUSOURCE_FILE_ARRAY:
if (bb == NULL)
goto bad_exit;
n = 0;
while (bb[n]) {
if (access_ok(bb[n]))
goto bad_exit;
n++;
}
tmp = malloc((n+1)*sizeof(char *));
for (i=0; i<n; i++)
tmp[i] = strdup(bb[i]);
tmp[n] = NULL;
au->source_list = tmp;
break;
case AUSOURCE_BUFFER:
if (buf == NULL)
goto bad_exit;
len = strlen(buf);
if (databuf_init(&au->databuf, len,
DATABUF_FLAG_PRESERVE_HEAD) < 0)
goto bad_exit;
if (databuf_append(&au->databuf, buf, len) < 0)
goto bad_exit;
break;
case AUSOURCE_BUFFER_ARRAY:
if (bb == NULL)
goto bad_exit;
size = 0;
for (n = 0; (buf = bb[n]); n++) {
len = strlen(bb[n]);
if (bb[n][len-1] != '\n') {
size += len + 1;
} else {
size += len;
}
}
if (databuf_init(&au->databuf, size,
DATABUF_FLAG_PRESERVE_HEAD) < 0)
goto bad_exit;
for (n = 0; (buf = bb[n]); n++) {
len = strlen(buf);
if (databuf_append(&au->databuf, buf, len) < 0)
goto bad_exit;
}
break;
case AUSOURCE_DESCRIPTOR:
n = (long)b;
au->in = fdopen(n, "rm");
break;
case AUSOURCE_FILE_POINTER:
au->in = (FILE *)b;
break;
case AUSOURCE_FEED:
if (databuf_init(&au->databuf, 0, 0) < 0) goto bad_exit;
break;
default:
errno = EINVAL;
goto bad_exit;
break;
}
au->source = source;
au->list_idx = 0;
au->line_number = 0;
au->next_buf = NULL;
au->off = 0;
au->cur_buf = NULL;
au->line_pushed = 0;
au->parse_state = EVENT_EMPTY;
au->expr = NULL;
au->find_field = NULL;
au->search_where = AUSEARCH_STOP_EVENT;
au->tmp_translation = NULL;
au->uid_cache = NULL;
au->uid_cache_created = 0;
au->gid_cache = NULL;
au->gid_cache_created = 0;
init_interpretation_list(au);
init_normalizer(&au->norm_data);
return au;
bad_exit:
databuf_free(&au->databuf);
/* Free list of events list (au_lo) structure */
au_lol_clear(au->au_lo, 0);
free(au->au_lo);
free(au);
return NULL;
}
void auparse_add_callback(auparse_state_t *au, auparse_callback_ptr callback,
void *user_data, user_destroy user_destroy_func)
{
if (au == NULL) {
errno = EINVAL;
return;
}
if (au->callback_user_data_destroy) {
(*au->callback_user_data_destroy)(au->callback_user_data);
au->callback_user_data = NULL;
}
au->callback = callback;
au->callback_user_data = user_data;
au->callback_user_data_destroy = user_destroy_func;
}
static void consume_feed(auparse_state_t *au, int flush)
{
//if (debug) printf("consume feed, flush %d\n", flush);
while (auparse_next_event(au) > 0) {
if (au->callback) {
(*au->callback)(au, AUPARSE_CB_EVENT_READY,
au->callback_user_data);
}
}
if (flush) {
// FIXME: might need a call here to force auparse_next_event()
// to consume any partial data not fully consumed.
/* Terminate all outstanding events, as we are at end of input
* (ie mark BUILDING events as COMPLETE events) then if we
* have a callback execute the callback on each event
* FIXME: Should we implement a 'checkpoint' concept as per
* ausearch or accept these 'partial' events?
*/
event_list_t *l;
//if (debug) printf("terminate all events in flush\n");
au_terminate_all_events(au);
while ((l = au_get_ready_event(au, 0)) != NULL) {
rnode *r;
au->le = l; // make this current the event of interest
aup_list_first(l);
r = aup_list_get_cur(l);
free_interpretation_list(au);
load_interpretation_list(au, r->interp);
aup_list_first_field(l);
if (au->callback) {
(*au->callback)(au, AUPARSE_CB_EVENT_READY,
au->callback_user_data);
}
}
}
}
int auparse_new_buffer(auparse_state_t *au, const char *data, size_t data_len)
{
if (au->source != AUSOURCE_BUFFER)
return 1;
auparse_reset(au);
if (databuf_replace(&au->databuf, data, data_len) < 0)
return 1;
return 0;
}
int auparse_feed(auparse_state_t *au, const char *data, size_t data_len)
{
if (databuf_append(&au->databuf, data, data_len) < 0)
return -1;
consume_feed(au, 0);
return 0;
}
int auparse_flush_feed(auparse_state_t *au)
{
consume_feed(au, 1);
return 0;
}
// If there is any data in the state machine, return 1.
// Otherwise return 0 to indicate its empty
int auparse_feed_has_data(const auparse_state_t *au)
{
if (!au)
return 0;
int i;
au_lol *lol = au->au_lo;
// An improvement would be to track how many events we have stored
// to avoid a costly loop
for (i=0; i <= lol->maxi; i++) {
au_lolnode *cur = &(lol->array[i]);
if (cur->status > EBS_EMPTY)
return 1;
}
return 0;
}
// If there is a ready event in the state machine, return 1.
// Otherwise return 0 to indicate its empty
int auparse_feed_has_ready_event(auparse_state_t *au)
{
if (au_get_ready_event(au, 1) != NULL)
return 1;
return 0;
}
void auparse_feed_age_events(auparse_state_t *au)
{
time_t t = time(NULL);
au_check_events(au, t);
consume_feed(au, 0);
}
void auparse_set_escape_mode(auparse_state_t *au, auparse_esc_t mode)
{
if (au == NULL)
return;
au->escape_mode = mode;
}
/*
* Non-public function. Subject to change.
* buf is a string of name value pairs to be used for interpreting.
* Calling this function automatically releases the previous list.
*/
void _auparse_load_interpretations(auparse_state_t *au, const char *buf)
{
free_interpretation_list(au);
if (buf == NULL)
return;
load_interpretation_list(au, buf);
}
/*
* Non-public function. Subject to change.
*/
void _auparse_free_interpretations(auparse_state_t *au)
{
free_interpretation_list(au);
}
int auparse_reset(auparse_state_t *au)
{
if (au == NULL) {
errno = EINVAL;
return -1;
}
/* Create or Free list of events list (au_lo) structure */
if (au->au_lo->array == NULL)
au_lol_create(au->au_lo);
else
au_lol_clear(au->au_lo, 1);
au->parse_state = EVENT_EMPTY;
au->au_ready = 0;
au->le = NULL;
switch (au->source)
{
case AUSOURCE_LOGS:
case AUSOURCE_FILE:
case AUSOURCE_FILE_ARRAY:
if (au->in) {
fclose(au->in);
au->in = NULL;
}
/* Fall through */
case AUSOURCE_DESCRIPTOR:
case AUSOURCE_FILE_POINTER:
if (au->in)
rewind(au->in);
/* Fall through */
case AUSOURCE_BUFFER:
case AUSOURCE_BUFFER_ARRAY:
au->list_idx = 0;
au->line_number = 0;
au->off = 0;
databuf_reset(&au->databuf);
break;
default:
return -1;
}
free_interpretation_list((auparse_state_t *)au);
return 0;
}
char *auparse_metrics(const auparse_state_t *au)
{
char *metrics;
unsigned int uid, gid;
aulookup_metrics(au, &uid, &gid);
if (asprintf(&metrics,
"max lol available: %lu\n"
"max lol used: %d\n"
"pending lol: %d\n"
"uid cache size: %u\n"
"gid cache size: %u",
au->au_lo->limit,
au->au_lo->maxi,
au->au_ready, uid, gid) < 0)
metrics = NULL;
return metrics;
}
/* Add EXPR to AU, using HOW to select the combining operator.
On success, return 0.
On error, free EXPR set errno and return -1.
NOTE: EXPR is freed on error! */
static int add_expr(auparse_state_t *au, struct expr *expr, ausearch_rule_t how)
{
if (au->expr == NULL)
au->expr = expr;
else if (how == AUSEARCH_RULE_CLEAR) {
expr_free(au->expr);
au->expr = expr;
} else {
struct expr *e;
e = expr_create_binary(how == AUSEARCH_RULE_OR ? EO_OR : EO_AND,
au->expr, expr);
if (e == NULL) {
int err;
err = errno;
expr_free(expr);
errno = err;
return -1;
}
au->expr = e;
}
au->expr->started = 0;
return 0;
}
static int ausearch_add_item_internal(auparse_state_t *au, const char *field,
const char *op, const char *value, ausearch_rule_t how, unsigned op_eq,
unsigned op_ne)
{
struct expr *expr;
// Make sure there's a field
if (field == NULL)
goto err_out;
// Make sure how is within range
if (how < AUSEARCH_RULE_CLEAR || how > AUSEARCH_RULE_AND)
goto err_out;
// All pre-checks are done, build a rule
if (strcmp(op, "exists") == 0)
expr = expr_create_field_exists(field);
else {
unsigned t_op;
if (strcmp(op, "=") == 0)
t_op = op_eq;
else if (strcmp(op, "!=") == 0)
t_op = op_ne;
else
goto err_out;
if (value == NULL)
goto err_out;
expr = expr_create_comparison(field, t_op, value);
}
if (expr == NULL)
return -1;
if (add_expr(au, expr, how) != 0)
return -1; /* expr is freed by add_expr() */
return 0;
err_out:
errno = EINVAL;
return -1;
}
int ausearch_add_item(auparse_state_t *au, const char *field, const char *op,
const char *value, ausearch_rule_t how)
{
return ausearch_add_item_internal(au, field, op, value, how, EO_RAW_EQ,
EO_RAW_NE);
}
int ausearch_add_interpreted_item(auparse_state_t *au, const char *field,
const char *op, const char *value, ausearch_rule_t how)
{
return ausearch_add_item_internal(au, field, op, value, how,
EO_INTERPRETED_EQ, EO_INTERPRETED_NE);
}
int ausearch_add_timestamp_item_ex(auparse_state_t *au, const char *op,
time_t sec, unsigned milli, unsigned serial, ausearch_rule_t how)
{
static const struct {
unsigned value;
const char name[3];
} ts_tab[] = {
{EO_VALUE_LT, "<"},
{EO_VALUE_LE, "<="},
{EO_VALUE_GE, ">="},
{EO_VALUE_GT, ">"},
{EO_VALUE_EQ, "="},
};
struct expr *expr;
size_t i;
unsigned t_op;
for (i = 0; i < sizeof(ts_tab) / sizeof(*ts_tab); i++) {
if (strcmp(ts_tab[i].name, op) == 0)
goto found_op;
}
goto err_out;
found_op:
t_op = ts_tab[i].value;
if (milli >= 1000)
goto err_out;
// Make sure how is within range
if (how < AUSEARCH_RULE_CLEAR || how > AUSEARCH_RULE_AND)
goto err_out;
// All pre-checks are done, build a rule
expr = expr_create_timestamp_comparison_ex(t_op, sec, milli, serial);
if (expr == NULL)
return -1;
if (add_expr(au, expr, how) != 0)
return -1; /* expr is freed by add_expr() */
return 0;
err_out:
errno = EINVAL;
return -1;
}
int ausearch_add_timestamp_item(auparse_state_t *au, const char *op, time_t sec,
unsigned milli, ausearch_rule_t how)
{
return ausearch_add_timestamp_item_ex(au, op, sec, milli, 0, how);
}
int ausearch_add_expression(auparse_state_t *au, const char *expression,
char **error, ausearch_rule_t how)
{
struct expr *expr;
if (how < AUSEARCH_RULE_CLEAR || how > AUSEARCH_RULE_AND)
goto err_einval;
expr = expr_parse(expression, error);
if (expr == NULL) {
errno = EINVAL;
return -1;
}
if (add_expr(au, expr, how) != 0)
goto err; /* expr is freed by add_expr() */
return 0;
err_einval:
errno = EINVAL;
err:
*error = NULL;
return -1;
}
int ausearch_add_regex(auparse_state_t *au, const char *regexp)
{
struct expr *expr;
// Make sure there's an expression
if (regexp == NULL)
goto err_out;
expr = expr_create_regexp_expression(regexp);
if (expr == NULL)
return -1;
if (add_expr(au, expr, AUSEARCH_RULE_AND) != 0)
return -1; /* expr is freed by add_expr() */
return 0;
err_out:
errno = EINVAL;
return -1;
}
int ausearch_set_stop(auparse_state_t *au, austop_t where)
{
if (where < AUSEARCH_STOP_EVENT || where > AUSEARCH_STOP_FIELD) {
errno = EINVAL;
return -1;
}
au->search_where = where;
return 0;
}