forked from linux-audit/audit-userspace
/
ausearch-parse.c
2860 lines (2715 loc) · 55.4 KB
/
ausearch-parse.c
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
/*
* ausearch-parse.c - Extract interesting fields and check for match
* Copyright (c) 2005-08,2011,2013-14,2018-20 Red Hat
* Copyright (c) 2011 IBM Corp.
* All Rights Reserved.
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program 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 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>
* Marcelo Henrique Cerri <mhcerri@br.ibm.com>
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <netdb.h>
#include <limits.h> /* PATH_MAX */
#include <ctype.h>
#include "libaudit.h"
#include "ausearch-options.h"
#include "ausearch-lookup.h"
#include "ausearch-parse.h"
#include "auparse-idata.h"
#include "ausearch-nvpair.h"
#define NAME_OFFSET 28
static const char key_sep[2] = { AUDIT_KEY_SEPARATOR, 0 };
static int parse_task_info(lnode *n, search_items *s);
static int parse_syscall(lnode *n, search_items *s);
static int parse_dir(const lnode *n, search_items *s);
static int common_path_parser(search_items *s, char *path);
static int avc_parse_path(const lnode *n, search_items *s);
static int parse_path(const lnode *n, search_items *s);
static int parse_user(const lnode *n, search_items *s, anode *avc);
static int parse_obj(const lnode *n, search_items *s);
static int parse_login(const lnode *n, search_items *s);
static int parse_container_op(const lnode *n, search_items *s);
static int parse_container_id(const lnode *n, search_items *s);
static int parse_daemon1(const lnode *n, search_items *s);
static int parse_daemon2(const lnode *n, search_items *s);
static int parse_sockaddr(const lnode *n, search_items *s);
static int parse_avc(const lnode *n, search_items *s);
static int parse_integrity(const lnode *n, search_items *s);
static int parse_kernel_anom(const lnode *n, search_items *s);
static int parse_simple_message(const lnode *n, search_items *s);
static int parse_tty(const lnode *n, search_items *s);
static int parse_pkt(const lnode *n, search_items *s);
static int parse_yaasao(lnode *n, search_items *s);
static int audit_avc_init(search_items *s)
{
if (s->avc == NULL) {
//create
s->avc = malloc(sizeof(alist));
if (s->avc == NULL)
return -1;
alist_create(s->avc);
}
return 0;
}
static int audit_contid_init(search_items *s)
{
if (s->contid == NULL) {
//create
s->contid = malloc(sizeof(clist));
if (s->contid == NULL)
return -1;
clist_create(s->contid);
}
return 0;
}
/*
* This function will take the list and extract the searchable fields from it.
* It returns 0 on success and 1 on failure.
*/
int extract_search_items(llist *l)
{
int ret = 0;
lnode *n;
search_items *s = &l->s;
list_first(l);
n = list_get_cur(l);
if (n) {
do {
switch (n->type) {
case AUDIT_SYSCALL:
ret = parse_syscall(n, s);
break;
case AUDIT_CWD:
ret = parse_dir(n, s);
break;
case AUDIT_AVC_PATH:
ret = avc_parse_path(n, s);
break;
case AUDIT_PATH:
ret = parse_path(n, s);
break;
case AUDIT_USER:
case AUDIT_FIRST_USER_MSG...AUDIT_USER_END:
case AUDIT_USER_CHAUTHTOK...AUDIT_LAST_USER_MSG:
case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
ret = parse_user(n, s, NULL);
break;
case AUDIT_SOCKADDR:
ret = parse_sockaddr(n, s);
break;
case AUDIT_LOGIN:
ret = parse_login(n, s);
break;
case AUDIT_CONTAINER_OP:
ret = parse_container_op(n, s);
break;
case AUDIT_IPC:
case AUDIT_OBJ_PID:
ret = parse_obj(n, s);
break;
case AUDIT_DAEMON_START:
case AUDIT_DAEMON_END:
case AUDIT_DAEMON_ABORT:
case AUDIT_DAEMON_CONFIG:
case AUDIT_DAEMON_ROTATE:
case AUDIT_DAEMON_RESUME:
ret = parse_daemon1(n, s);
break;
case AUDIT_DAEMON_ACCEPT:
case AUDIT_DAEMON_CLOSE:
ret = parse_daemon2(n, s);
break;
case AUDIT_CONFIG_CHANGE:
ret = parse_simple_message(n, s);
// We use AVC parser because it just looks for
// the one field. We don't care about return
// code since older events don't have path=
avc_parse_path(n, s);
break;
case AUDIT_AVC:
case AUDIT_USER_AVC:
ret = parse_avc(n, s);
break;
case AUDIT_NETFILTER_PKT:
ret = parse_pkt(n, s);
break;
case AUDIT_FEATURE_CHANGE:
case AUDIT_ANOM_LINK:
ret = parse_task_info(n, s);
break;
case AUDIT_SECCOMP:
case AUDIT_ANOM_PROMISCUOUS:
case AUDIT_ANOM_ABEND:
// AUDIT_FIRST_KERN_ANOM_MSG...AUDIT_LAST_KERN_ANOM_MSG:
ret = parse_kernel_anom(n, s);
break;
case AUDIT_MAC_POLICY_LOAD...AUDIT_MAC_UNLBL_STCDEL:
ret = parse_simple_message(n, s);
break;
case AUDIT_INTEGRITY_DATA...AUDIT_INTEGRITY_RULE:
ret = parse_integrity(n, s);
break;
case AUDIT_KERNEL:
case AUDIT_SELINUX_ERR:
case AUDIT_EXECVE:
case AUDIT_IPC_SET_PERM:
case AUDIT_MQ_OPEN:
case AUDIT_MQ_SENDRECV:
case AUDIT_MQ_NOTIFY:
case AUDIT_MQ_GETSETATTR:
case AUDIT_FD_PAIR:
case AUDIT_BPRM_FCAPS:
case AUDIT_CAPSET:
case AUDIT_MMAP:
case AUDIT_PROCTITLE:
case AUDIT_REPLACE...AUDIT_BPF:
case AUDIT_SET_CAPCONTID:
// Nothing to parse
break;
case AUDIT_NETFILTER_CFG:
case AUDIT_EVENT_LISTENER:
ret = parse_yaasao(n, s);
break;
case AUDIT_TTY:
ret = parse_tty(n, s);
break;
case AUDIT_CONTAINER_ID:
ret = parse_container_id(n, s);
break;
default:
if (event_debug)
fprintf(stderr,
"Unparsed type:%d\n - skipped",
n->type);
break;
}
if (event_debug && ret)
fprintf(stderr,
"Malformed event skipped, rc=%d. %s\n",
ret, n->message);
} while ((n=list_next(l)) && ret == 0);
}
return ret;
}
/*
* returns malloc'ed buffer on success and NULL on failure
*/
static nvlist uid_nvl;
static int uid_list_created=0;
static const char *lookup_uid(const char *field, uid_t uid)
{
const char *value;
value = _auparse_lookup_interpretation(field);
if (value)
return value;
if (uid == 0)
return strdup("root");
else if (uid == -1)
return strdup("unset");
if (uid_list_created == 0) {
nvlist_create(&uid_nvl);
nvlist_clear(&uid_nvl);
uid_list_created = 1;
}
if (nvlist_find_val(&uid_nvl, uid)) {
return strdup(uid_nvl.cur->name);
} else {
struct passwd *pw;
pw = getpwuid(uid);
if (pw) {
nvnode nv;
nv.name = strdup(pw->pw_name);
nv.val = uid;
nvlist_append(&uid_nvl, &nv);
return strdup(pw->pw_name);
}
}
return NULL;
}
void lookup_uid_destroy_list(void)
{
if (uid_list_created == 0)
return;
nvlist_clear(&uid_nvl);
uid_list_created = 0;
}
static int parse_task_info(lnode *n, search_items *s)
{
char *ptr, *str, *term;
term = n->message;
// ppid
if (event_ppid != -1) {
str = strstr(term, "ppid=");
if (str != NULL) { // ppid is an optional field
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 14;
*term = 0;
errno = 0;
s->ppid = strtoul(ptr, NULL, 10);
if (errno)
return 15;
*term = ' ';
}
}
// pid
if (event_pid != -1) {
str = strstr(term, " pid=");
if (str == NULL)
return 16;
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 17;
*term = 0;
errno = 0;
s->pid = strtoul(ptr, NULL, 10);
if (errno)
return 18;
*term = ' ';
}
// optionally get loginuid
if (event_loginuid != -2 || event_tauid) {
str = strstr(term, "auid=");
if (str == NULL) {
str = strstr(term, "loginuid=");
if (str == NULL)
return 19;
ptr = str + 9;
} else
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 20;
*term = 0;
errno = 0;
s->loginuid = strtoul(ptr, NULL, 10);
if (errno)
return 21;
*term = ' ';
if (s->tauid) free((void *)s->tauid);
s->tauid = lookup_uid("auid", s->loginuid);
}
// optionally get uid
if (event_uid != -1 || event_tuid) {
try_again:
str = strstr(term, "uid=");
if (str == NULL)
return 22;
// This sometimes hits auid instead of uid. If so, retry.
if (*(str-1) == 'a') {
term = str +1;
goto try_again;
}
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 23;
*term = 0;
errno = 0;
s->uid = strtoul(ptr, NULL, 10);
if (errno)
return 24;
*term = ' ';
if (s->tuid) free((void *)s->tuid);
s->tuid = lookup_uid("uid", s->uid);
}
// optionally get gid
if (event_gid != -1) {
str = strstr(term, "gid=");
if (str == NULL)
return 25;
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 26;
*term = 0;
errno = 0;
s->gid = strtoul(ptr, NULL, 10);
if (errno)
return 27;
*term = ' ';
}
// euid
if (event_euid != -1 || event_teuid) {
str = strstr(term, "euid=");
if (str == NULL)
return 28;
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 29;
*term = 0;
errno = 0;
s->euid = strtoul(ptr, NULL, 10);
if (errno)
return 30;
*term = ' ';
s->teuid = lookup_uid("euid", s->euid);
}
// egid
if (event_egid != -1) {
str = strstr(term, "egid=");
if (str == NULL)
return 31;
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 32;
*term = 0;
errno = 0;
s->egid = strtoul(ptr, NULL, 10);
if (errno)
return 33;
*term = ' ';
}
if (event_terminal) {
// dont do this search unless needed
str = strstr(term, "tty=");
if (str) {
str += 4;
term = strchr(str, ' ');
if (term == NULL)
return 34;
*term = 0;
if (s->terminal) // ANOM_NETLINK has one
free(s->terminal);
s->terminal = strdup(str);
*term = ' ';
}
}
// ses
if (event_session_id != -2 ) {
str = strstr(term, "ses=");
if (str) {
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 35;
*term = 0;
errno = 0;
s->session_id = strtoul(ptr, NULL, 10);
if (errno)
return 36;
*term = ' ';
}
}
if (event_comm) {
// dont do this search unless needed
str = strstr(term, "comm=");
if (str) {
/* Make the syscall one override */
if (s->comm)
free(s->comm);
str += 5;
if (*str == '"') {
str++;
term = strchr(str, '"');
if (term == NULL)
return 37;
*term = 0;
s->comm = strdup(str);
*term = '"';
} else
s->comm = unescape(str);
} else
return 38;
}
if (event_exe) {
// dont do this search unless needed
str = strstr(n->message, "exe=");
if (str) {
str += 4;
if (*str == '"') {
str++;
term = strchr(str, '"');
if (term == NULL)
return 39;
*term = 0;
if (s->exe) // ANOM_NETLINK has one
free(s->exe);
s->exe = strdup(str);
*term = '"';
} else
s->exe = unescape(str);
} else
return 40;
}
if (event_subject) {
// scontext
str = strstr(term, "subj=");
if (str != NULL) {
str += 5;
term = strchr(str, ' ');
if (term == NULL)
return 41;
*term = 0;
if (audit_avc_init(s) == 0) {
anode an;
anode_init(&an);
an.scontext = strdup(str);
alist_append(s->avc, &an);
*term = ' ';
} else
return 42;
}
}
// success
if (event_success != S_UNSET) {
if (term == NULL)
term = n->message;
str = strstr(term, "res=");
if (str != NULL) {
ptr = str + 4;
term = strchr(ptr, ' ');
if (term)
*term = 0;
errno = 0;
s->success = strtoul(ptr, NULL, 10);
if (errno)
return 43;
if (term)
*term = ' ';
}
}
return 0;
}
static int parse_syscall(lnode *n, search_items *s)
{
char *ptr, *str, *term;
extern int event_machine;
int ret;
term = n->message;
if (report_format > RPT_DEFAULT || event_machine != -1) {
// get arch
str = strstr(term, "arch=");
if (str == NULL)
return 1;
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 2;
*term = 0;
errno = 0;
s->arch = (int)strtoul(ptr, NULL, 16);
if (errno)
return 3;
*term = ' ';
}
// get syscall
str = strstr(term, "syscall=");
if (str == NULL)
return 4;
ptr = str + 8;
term = strchr(ptr, ' ');
if (term == NULL)
return 5;
*term = 0;
errno = 0;
s->syscall = (int)strtoul(ptr, NULL, 10);
if (errno)
return 6;
*term = ' ';
// get success
if (event_success != S_UNSET) {
str = strstr(term, "success=");
if (str) { // exit_group does not set success !?!
ptr = str + 8;
term = strchr(ptr, ' ');
if (term == NULL)
return 7;
*term = 0;
if (strcmp(ptr, "yes") == 0)
s->success = S_SUCCESS;
else
s->success = S_FAILED;
*term = ' ';
}
}
// get exit
if (event_exit_is_set) {
str = strstr(term, "exit=");
if (str == NULL)
return 8;
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 9;
*term = 0;
errno = 0;
s->exit = strtoll(ptr, NULL, 0);
if (errno)
return 10;
s->exit_is_set = 1;
*term = ' ';
}
// get a0
str = strstr(term, "a0=");
if (str == NULL)
return 11;
ptr = str + 3;
term = strchr(ptr, ' ');
if (term == NULL)
return 12;
*term = 0;
errno = 0;
// 64 bit dump on 32 bit machine looks bad here - need long long
n->a0 = strtoull(ptr, NULL, 16); // Hex
if (errno)
return 13;
*term = ' ';
// get a1
str = strstr(term, "a1=");
if (str == NULL)
return 11;
ptr = str + 3;
term = strchr(ptr, ' ');
if (term == NULL)
return 12;
*term = 0;
errno = 0;
// 64 bit dump on 32 bit machine looks bad here - need long long
n->a1 = strtoull(ptr, NULL, 16); // Hex
if (errno)
return 13;
*term = ' ';
ret = parse_task_info(n, s);
if (ret)
return ret;
if (event_key) {
str = strstr(term, "key=");
if (str) {
if (!s->key) {
//create
s->key = malloc(sizeof(slist));
if (s->key == NULL)
return 43;
slist_create(s->key);
}
str += 4;
if (*str == '"') {
str++;
term = strchr(str, '"');
if (term == NULL)
return 44;
*term = 0;
if (s->key) {
// append
snode sn;
sn.str = strdup(str);
sn.key = NULL;
sn.hits = 1;
slist_append(s->key, &sn);
}
*term = '"';
} else {
if (s->key) {
char *saved;
char *keyptr = unescape(str);
if (keyptr == NULL)
return 45;
char *kptr = strtok_r(keyptr,
key_sep, &saved);
while (kptr) {
snode sn;
// append
sn.str = strdup(kptr);
sn.key = NULL;
sn.hits = 1;
slist_append(s->key, &sn);
kptr = strtok_r(NULL,
key_sep, &saved);
}
free(keyptr);
}
}
}
}
return 0;
}
static int parse_dir(const lnode *n, search_items *s)
{
char *str, *term;
if (event_filename) {
// dont do this search unless needed
str = strstr(n->message+NAME_OFFSET, " cwd=");
if (str) {
str += 5;
if (*str == '"') {
/* string is normal */
str++;
term = strchr(str, '"');
if (term == NULL)
return 1;
*term = 0;
if (!s->cwd)
s->cwd = strdup(str);
*term = '"';
} else if (!s->cwd)
s->cwd = unescape(str);
}
}
return 0;
}
static int common_path_parser(search_items *s, char *path)
{
char *term;
if (!s->filename) {
//create
s->filename = malloc(sizeof(slist));
if (s->filename == NULL)
return 1;
slist_create(s->filename);
}
if (*path == '"') {
/* string is normal */
path++;
term = strchr(path, '"');
if (term == NULL)
return 2;
*term = 0;
if (s->filename) {
// append
snode sn;
sn.str = strdup(path);
sn.key = NULL;
sn.hits = 1;
// Attempt to rebuild path if relative
if ((sn.str[0] == '.') && ((sn.str[1] == '.') ||
(sn.str[1] == '/')) && s->cwd) {
char *tmp = malloc(PATH_MAX);
if (tmp == NULL) {
free(sn.str);
return 3;
}
snprintf(tmp, PATH_MAX,
"%s/%s", s->cwd, sn.str);
free(sn.str);
sn.str = tmp;
}
slist_append(s->filename, &sn);
}
*term = '"';
} else {
if (s->filename) {
// append
snode sn;
sn.key = NULL;
sn.hits = 1;
if (strncmp(path, "(null)", 6) == 0) {
sn.str = strdup("(null)");
goto append;
}
if (!isxdigit(path[0]))
return 4;
if (path[0] == '0' && path[1] == '0')
sn.str = unescape(&path[2]); // Abstract name
else {
term = strchr(path, ' ');
if (term == NULL)
return 5;
*term = 0;
sn.str = unescape(path);
*term = ' ';
}
if (sn.str == NULL)
return 7;
// Attempt to rebuild path if relative
if ((sn.str[0] == '.') && ((sn.str[1] == '.') ||
(sn.str[1] == '/')) && s->cwd) {
char *tmp = malloc(PATH_MAX);
if (tmp == NULL)
return 6;
snprintf(tmp, PATH_MAX, "%s/%s",
s->cwd, sn.str);
free(sn.str);
sn.str = tmp;
}
append:
slist_append(s->filename, &sn);
}
}
return 0;
}
/* Older AVCs have path separate from the AVC record */
static int avc_parse_path(const lnode *n, search_items *s)
{
char *str;
if (event_filename) {
// dont do this search unless needed
str = strstr(n->message, " path=");
if (str) {
str += 6;
return common_path_parser(s, str);
}
return 1;
}
return 0;
}
static int parse_path(const lnode *n, search_items *s)
{
// We add 32 to message because we do not need to look at
// anything before that. Its only time and type.
char *str, *term = n->message+NAME_OFFSET;
if (event_filename) {
// dont do this search unless needed
str = strstr(term, " name=");
if (str) {
int rc;
str += 6;
rc = common_path_parser(s, str);
if (rc)
return rc;
term = str;
// Note that at this point we should be past beginning
// and around the path element. The type we search for
// is objtype or nametype. Searching for both will
// slow us down. So, I'm using what is common to both.
str = strstr(term, "type=");
if (str) {
str += 5;
s->filename->cur->key = strdup(str);
}
}
}
if (event_object) {
// tcontext
str = strstr(term, "obj=");
if (str != NULL) {
str += 4;
term = strchr(str, ' ');
if (term)
*term = 0;
if (audit_avc_init(s) == 0) {
anode an;
anode_init(&an);
an.tcontext = strdup(str);
alist_append(s->avc, &an);
if (term)
*term = ' ';
} else
return 7;
}
}
return 0;
}
static int parse_obj(const lnode *n, search_items *s)
{
char *str, *term;
term = n->message;
if (event_object) {
// obj context
str = strstr(term, "obj=");
if (str != NULL) {
str += 4;
term = strchr(str, ' ');
if (term)
*term = 0;
if (audit_avc_init(s) == 0) {
anode an;
anode_init(&an);
an.tcontext = strdup(str);
alist_append(s->avc, &an);
if (term)
*term = ' ';
} else
return 1;
}
}
return 0;
}
static int parse_user(const lnode *n, search_items *s, anode *avc)
{
char *ptr, *str, *term, saved, *mptr;
term = n->message;
// get pid
if (event_pid != -1) {
str = strstr(term, "pid=");
if (str == NULL)
return 1;
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 2;
*term = 0;
errno = 0;
s->pid = strtoul(ptr, NULL, 10);
if (errno)
return 3;
*term = ' ';
}
// optionally get uid
if (event_uid != -1 || event_tuid) {
str = strstr(term, "uid=");
if (str == NULL)
return 4;
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 5;
*term = 0;
errno = 0;
s->uid = strtoul(ptr, NULL, 10);
if (errno)
return 6;
*term = ' ';
s->tuid = lookup_uid("uid", s->uid);
}
// optionally get loginuid
if (event_loginuid != -2 || event_tauid) {
str = strstr(term, "auid=");
if (str == NULL) { // Try the older one
str = strstr(term, "loginuid=");
if (str == NULL)
return 7;
ptr = str + 9;
} else
ptr = str + 5;
term = strchr(ptr, ' ');
if (term == NULL)
return 8;
*term = 0;
errno = 0;
s->loginuid = strtoul(ptr, NULL, 10);
if (errno)
return 9;
*term = ' ';
s->tauid = lookup_uid("auid", s->loginuid);
}
// ses
if (event_session_id != -2 ) {
str = strstr(term, "ses=");
if (str) {
ptr = str + 4;
term = strchr(ptr, ' ');
if (term == NULL)
return 10;
*term = 0;
errno = 0;
s->session_id = strtoul(ptr, NULL, 10);
if (errno)
return 11;
*term = ' ';
}
}
if (event_subject) {
// scontext
str = strstr(term, "subj=");
if (str != NULL) {
str += 5;
term = strchr(str, ' ');
if (term == NULL)
return 12;
*term = 0;
if (avc) {
avc->scontext = strdup(str);
*term = ' ';
} else if (audit_avc_init(s) == 0) {
anode an;
anode_init(&an);
an.scontext = strdup(str);
alist_append(s->avc, &an);
*term = ' ';
} else
return 13;
}
}
// optionally get tcontext
if (avc && event_object) {
// USER_AVC tcontext
str = strstr(term, "tcontext=");
if (str != NULL) {
str += 9;
term = strchr(str, ' ');
if (term) {
*term = 0;
avc->tcontext = strdup(str);
*term = ' ';
} else
term = str;
}
// Grab tclass if it exists
str = strstr(term, "tclass=");
if (str) {
str += 7;