This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 190
/
spo_database_cache.c
5835 lines (4786 loc) · 143 KB
/
spo_database_cache.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
/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License Version 2 as
** published by the Free Software Foundation. You may not use, modify or
** distribute this program under any other version of the GNU General
** Public License.
**
** 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; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
* Theses caches are built by combining existing caches from the snort map files and config files,
* The goal is to reduce the number of database interaction to a minimum so the output plugins
* is more performant especially under heavy load of events.
*
*
* Note that the default schema compatibility is kept intact
* Maintainers : The Barnyard2 Team <firnsy@gmail.com> <beenph@gmail.com> - 2011-2012
*
* Special thanks to: Rusell Fuleton <russell.fulton@gmail.com> for helping us stress test
* this in production for us.
*
*/
#include "output-plugins/spo_database.h"
#include "output-plugins/spo_database_cache.h"
/* LOOKUP FUNCTIONS */
cacheSignatureObj *cacheGetSignatureNodeUsingDBid(cacheSignatureObj *iHead,u_int32_t lookupId);
cacheReferenceObj *cacheGetReferenceNodeUsingDBid(cacheSystemObj *iHead,u_int32_t lookupId);
u_int32_t cacheSignatureLookup(dbSignatureObj *iLookup,cacheSignatureObj *iHead);
u_int32_t cacheClassificationLookup(dbClassificationObj *iLookup,cacheClassificationObj *iHead);
u_int32_t cacheSystemLookup(dbSystemObj *iLookup,cacheSystemObj *iHead,cacheSystemObj **rcacheSystemObj);
u_int32_t cacheReferenceLookup(dbReferenceObj *iLookup,cacheReferenceObj *iHead,cacheReferenceObj **retRefLookupNode);
u_int32_t dbSignatureReferenceLookup(dbSignatureReferenceObj *iLookup,cacheSignatureReferenceObj *iHead,cacheSignatureReferenceObj **retSigRef,u_int32_t refCondCheck);
u_int32_t dbReferenceLookup(dbReferenceObj *iLookup,cacheReferenceObj *iHead);
u_int32_t dbSystemLookup(dbSystemObj *iLookup,cacheSystemObj *iHead);
u_int32_t dbSignatureLookup(dbSignatureObj *iLookup,cacheSignatureObj *iHead);
u_int32_t dbClassificationLookup(dbClassificationObj *iLookup,cacheClassificationObj *iHead);
/* LOOKUP FUNCTIONS */
/* CLASSIFICATION FUNCTIONS */
u_int32_t ClassificationPullDataStore(DatabaseData *data, dbClassificationObj **iArrayPtr,u_int32_t *array_length);
u_int32_t ClassificationCacheUpdateDBid(dbClassificationObj *iDBList,u_int32_t array_length,cacheClassificationObj **cacheHead);
u_int32_t ClassificationPopulateDatabase(DatabaseData *data,cacheClassificationObj *cacheHead);
u_int32_t ClassificationCacheSynchronize(DatabaseData *data,cacheClassificationObj **cacheHead);
/* CLASSIFICATION FUNCTIONS */
/* SIGNATURE FUNCTIONS */
u_int32_t SignaturePopulateDatabase(DatabaseData *data,cacheSignatureObj *cacheHead,int inTransac);
u_int32_t SignatureCacheUpdateDBid(dbSignatureObj *iDBList,u_int32_t array_length,cacheSignatureObj **cacheHead);
u_int32_t SignaturePullDataStore(DatabaseData *data, dbSignatureObj **iArrayPtr,u_int32_t *array_length);
u_int32_t SignatureCacheSynchronize(DatabaseData *data,cacheSignatureObj **cacheHead);
/* SIGNATURE FUNCTIONS */
/* REFERENCE FUNCTIONS */
u_int32_t ReferencePullDataStore(DatabaseData *data, dbReferenceObj **iArrayPtr,u_int32_t *array_length);
u_int32_t ReferenceCacheUpdateDBid(dbReferenceObj *iDBList,u_int32_t array_length,cacheSystemObj **cacheHead);
u_int32_t ReferencePopulateDatabase(DatabaseData *data,cacheReferenceObj *cacheHead);
/* REFERENCE FUNCTIONS */
/* SYSTEM FUNCTIONS */
u_int32_t SystemPopulateDatabase(DatabaseData *data,cacheSystemObj *cacheHead);
u_int32_t SystemPullDataStore(DatabaseData *data, dbSystemObj **iArrayPtr,u_int32_t *array_length);
u_int32_t SystemCacheUpdateDBid(dbSystemObj *iDBList,u_int32_t array_length,cacheSystemObj **cacheHead);
u_int32_t SystemCacheSynchronize(DatabaseData *data,cacheSystemObj **cacheHead);
/* SYSTEM FUNCTIONS */
/* SIGNATURE REFERENCE FUNCTIONS */
u_int32_t SignatureReferencePullDataStore(DatabaseData *data, dbSignatureReferenceObj **iArrayPtr,u_int32_t *array_length);
u_int32_t SignatureReferenceCacheUpdateDBid(dbSignatureReferenceObj *iDBList,
u_int32_t array_length,
cacheSignatureReferenceObj **cacheHead,
cacheSignatureObj *sigCacheHead,
cacheSystemObj *systemCacheHead);
u_int32_t SignatureReferencePopulateDatabase(DatabaseData *data,cacheSignatureReferenceObj *cacheHead);
u_int32_t SigRefSynchronize(DatabaseData *data,cacheSignatureReferenceObj **cacheHead,cacheSignatureObj *cacheSigHead);
u_int32_t SignatureReferencePreGenerate(cacheSignatureObj *iHead);
/* SIGNATURE REFERENCE FUNCTIONS */
/* Init FUNCTIONS */
u_int32_t ConvertDefaultCache(Barnyard2Config *bc,DatabaseData *data);
u_int32_t GenerateSigRef(cacheSignatureReferenceObj **iHead,cacheSignatureObj *sigHead);
u_int32_t ConvertReferenceCache(ReferenceNode *iHead,MasterCache *iMasterCache,cacheSignatureObj *cSobj,DatabaseData *data);
u_int32_t ConvertClassificationCache(ClassType **iHead, MasterCache *iMasterCache,DatabaseData *data);
u_int32_t ConvertSignatureCache(SigNode **iHead,MasterCache *iMasterCache,DatabaseData *data);
u_int32_t CacheSynchronize(DatabaseData *data);
/* Init FUNCTIONS */
/* Destructor */
void MasterCacheFlush(DatabaseData *data,u_int32_t flushFlag);
/* Destructor */
extern SigNode *sigTypes;
#if DEBUG
u_int32_t file_reference_object_count = 0;
u_int32_t file_system_object_count = 0;
u_int32_t file_signature_object_count = 0;
u_int32_t file_classification_object_count = 0;
u_int32_t file_sigref_object_count = 0;
u_int32_t db_reference_object_count = 0;
u_int32_t db_system_object_count = 0;
u_int32_t db_signature_object_count = 0;
u_int32_t db_classification_object_count = 0;
u_int32_t db_sigref_object_count = 0;
u_int32_t inserted_reference_object_count = 0;
u_int32_t inserted_system_object_count = 0;
u_int32_t inserted_signature_object_count = 0;
u_int32_t inserted_classification_object_count = 0;
u_int32_t inserted_sigref_object_count = 0;
#endif
/**
* Lookup for dbSignatureReferenceObj in cacheSignatureReferenceObj
*
* @note ref_seq is not compared because it could have changed and it is
* handled elsewhere.
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t cacheSignatureReferenceLookup(dbSignatureReferenceObj *iLookup,cacheSignatureReferenceObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceSignatureObj[0x%x] cacheSignatureReferenceObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
while(iHead != NULL)
{
if( ((iLookup->db_ref_id == iHead->obj.db_ref_id) &&
(iLookup->db_sig_id == iHead->obj.db_sig_id)))
{
/* Found */
return 1;
}
iHead = iHead->next;
}
return 0;
}
u_int32_t cacheEventSignatureLookup(cacheSignatureObj *iHead,
plgSignatureObj *sigContainer,
u_int32_t gid,
u_int32_t sid)
{
u_int32_t matchCount = 0;
if( (iHead == NULL) ||
(sigContainer == NULL))
{
return 0;
}
/* Clean up */
memset(sigContainer,'\0',(sizeof(plgSignatureObj) * MAX_SIGLOOKUP));
while(iHead != NULL)
{
if( (iHead->obj.sid == sid) &&
(iHead->obj.gid == gid))
{
if(matchCount < MAX_SIGLOOKUP)
{
sigContainer[matchCount].cacheSigObj = iHead;
matchCount++;
}
else
{
/* We reached maximum count for possible reference matching objects... */
return matchCount;
}
}
iHead = iHead->next;
}
return matchCount;
}
/**
* Lookup for dbSignatureObj in cacheReferenceObj
* @note compare message,sid,gid and revision.
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t cacheSignatureLookup(dbSignatureObj *iLookup,cacheSignatureObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbSignatureObj[0x%x] cacheSignatureObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->message,iHead->obj.message,strlen(iHead->obj.message)) == 0) &&
(iLookup->sid == iHead->obj.sid) &&
(iLookup->gid == iHead->obj.gid) &&
(iLookup->rev == iHead->obj.rev))
{
/* Found */
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbSignatureObj in cacheReferenceObj and if a match is found it will return the object for further comparaisons.
* @note compare message,sid,gid and revision.
*
* @param iLookup
* @param iHead
*
* @return
* NULL NOT FOUND
* Valid POINTER FOUND
*/
cacheSignatureObj * cacheSignatureGetObject(dbSignatureObj *iLookup,cacheSignatureObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbSignatureObj[0x%x] cacheSignatureObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->message,iHead->obj.message,strlen(iHead->obj.message)) == 0) &&
(iLookup->sid == iHead->obj.sid) &&
(iLookup->gid == iHead->obj.gid) &&
(iLookup->rev == iHead->obj.rev))
{
/* Found */
return iHead;
}
iHead = iHead->next;
}
return NULL;
}
u_int32_t cacheEventClassificationLookup(cacheClassificationObj *iHead,u_int32_t iClass_id)
{
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if(iHead->obj.sig_class_id == iClass_id)
{
return iHead->obj.db_sig_class_id;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbClassificationObj in cacheClassificationObj
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t cacheClassificationLookup(dbClassificationObj *iLookup,cacheClassificationObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbClassiciationObj[0x%x] cacheClassificationObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if( (memcmp(iLookup,&iHead->obj,sizeof(dbClassificationObj)) == 0))
{
/* Found */
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbSystemObj in cacheSystemObj and also set rcacheSystemObj to found object.
*
* @param iLookup
* @param iHead
* @param rcacheSystemObj
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t cacheSystemLookup(dbSystemObj *iLookup,cacheSystemObj *iHead,cacheSystemObj **rcacheSystemObj)
{
if( (iLookup == NULL) ||
(rcacheSystemObj == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceObj[0x%x] cacheReferenceObj[0x%x] **rcacheSystemObj[0x%x]\n",
__FUNCTION__,
iLookup,
iHead,
rcacheSystemObj);
}
while(iHead != NULL)
{
if( (memcmp(iLookup->ref_system_name,iHead->obj.ref_system_name,SYSTEM_NAME_LEN) == 0) &&
(memcmp(iLookup->ref_system_url,iHead->obj.ref_system_url,SYSTEM_URL_LEN) == 0))
{
/* Match */
*rcacheSystemObj = iHead;
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbReferenceObj in cacheReferenceObj and also set retRefLookupNode found object.
*
* @param iLookup
* @param iHead
* @param retRefLookupNode
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t cacheReferenceLookup(dbReferenceObj *iLookup,cacheReferenceObj *iHead,cacheReferenceObj **retRefLookupNode)
{
if( (iLookup == NULL) ||
(retRefLookupNode == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceObj[0x%x] cacheReferenceObj[0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->ref_tag,iHead->obj.ref_tag,strlen(iLookup->ref_tag)) == 0))
{
/* Match */
*retRefLookupNode = iHead;
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbSignatureReferenceObj in cacheSignatureReferenceObj
* and return the cacheSignatureReferenceObj found (if any)
* @note ref_seq is not compared because it could have changed and it is
* handled elsewhere.
*
* @param iLookup
* @param iHead
* @param retSigRef
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t dbSignatureReferenceLookup(dbSignatureReferenceObj *iLookup,cacheSignatureReferenceObj *iHead,cacheSignatureReferenceObj **retSigRef,u_int32_t refCondCheck)
{
if( (iLookup == NULL) ||
(retSigRef == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceSignatureObj[0x%x] cacheSignatureReferenceObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
while(iHead != NULL)
{
if(refCondCheck == 0)
{
if( (iLookup->ref_seq == iHead->obj.ref_seq) &&
(iLookup->db_sig_id == iHead->obj.db_sig_id))
{
/* Found */
*retSigRef = iHead;
return 1;
}
}
else if(refCondCheck == 1)
{
if( (iLookup->db_ref_id == iHead->obj.db_ref_id) &&
(iLookup->db_sig_id == iHead->obj.db_sig_id))
{
/* Found */
*retSigRef = iHead;
return 1;
}
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbReferenceObj in cacheReferenceObj
* @note Only compare tag, from there assign system_id from parent node
* and reference id from lookup id.
* @note Used in context db->internaCache lookup (if found remove CACHE_INTERNAL_ONLY and set CACHE_BOTH flag)
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t dbReferenceLookup(dbReferenceObj *iLookup,cacheReferenceObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceObj[0x%x] cacheReferenceObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->ref_tag,iHead->obj.ref_tag,strlen(iHead->obj.ref_tag)) == 0))
{
/* Found */
if( iHead->flag & CACHE_INTERNAL_ONLY)
{
iHead->flag ^= (CACHE_INTERNAL_ONLY | CACHE_BOTH);
}
else
{
iHead->flag ^= CACHE_BOTH;
}
iHead->obj.ref_id = iLookup->ref_id;
iHead->obj.system_id = iHead->obj.parent->obj.db_ref_system_id;
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbSystemObj in cacheSystemeObj
* @note compare only reference name, assign system id from parent node if a match is found.
* @note Used in context db->internaCache lookup (if found remove CACHE_INTERNAL_ONLY and set CACHE_BOTH flag)
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t dbSystemLookup(dbSystemObj *iLookup,cacheSystemObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbSystemObj[0x%x] cacheSystemObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if((strncasecmp(iLookup->ref_system_name,iHead->obj.ref_system_name,strlen(iHead->obj.ref_system_name)) == 0))
{
/* Found */
if( iHead->flag & CACHE_INTERNAL_ONLY)
{
iHead->flag ^= (CACHE_INTERNAL_ONLY | CACHE_BOTH);
}
else
{
iHead->flag ^= CACHE_BOTH;
}
iHead->obj.db_ref_system_id = iLookup->db_ref_system_id;
return 1;
}
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbSignatureObj in cacheSignatureObj
* @note compare message,sid,gid and revision.
* @note Used in context db->internaCache lookup (if found remove CACHE_INTERNAL_ONLY and set CACHE_BOTH flag)
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t dbSignatureLookup(dbSignatureObj *iLookup,cacheSignatureObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbSignatureObj[0x%x] cacheSignatureObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->message,iHead->obj.message,strlen(iHead->obj.message)) == 0) &&
(iLookup->sid == iHead->obj.sid) &&
(iLookup->gid == iHead->obj.gid))
{
/* Found */
/*
If the object in current list has a revision of 0,
and that a match is found for gid/sid (we are probably being called from the initialization
(should) and the current node is
initialized with information from the files only, and probably the database has cleaner information)
set the revision to the revision of the lookup node,
but if the revision is not set to 0 and does
not match, continue searching.
*/
if(iHead->obj.rev == 0)
{
iHead->obj.rev = iLookup->rev;
}
else
{
if( iHead->obj.rev != iLookup->rev)
{
/* It is not the signature object that we are looking for */
goto next_obj;
}
}
if( iHead->flag & CACHE_INTERNAL_ONLY)
{
iHead->flag ^= (CACHE_INTERNAL_ONLY | CACHE_BOTH);
}
else
{
iHead->flag ^= CACHE_BOTH;
}
/* NOTE: -elz For cleanness this should be removed from here, moved to the caller and add a
param for the found node.. but since its only called from one place, this is currently
how its done...lazyness first.
*/
/* Set values from the database */
iHead->obj.db_id = iLookup->db_id;
iHead->obj.class_id = iLookup->class_id;
iHead->obj.priority_id = iLookup->priority_id;
return 1;
}
next_obj:
iHead = iHead->next;
}
return 0;
}
/**
* Lookup for dbClassificationObj in cacheClassificationObj
* @note Used in context db->internaCache lookup (if found remove CACHE_INTERNAL_ONLY and set CACHE_BOTH flag)
*
* @param iLookup
* @param iHead
*
* @return
* 0 NOT FOUND
* 1 FOUND
*/
u_int32_t dbClassificationLookup(dbClassificationObj *iLookup,cacheClassificationObj *iHead)
{
if( (iLookup == NULL))
{
/* XXX */
FatalError("database [%s()], Called with dbReferenceObj[0x%x] cacheClassificationObj [0x%x] \n",
__FUNCTION__,
iLookup,
iHead);
}
if(iHead == NULL)
{
return 0;
}
while(iHead != NULL)
{
if( (strncasecmp(iLookup->sig_class_name,iHead->obj.sig_class_name,strlen(iHead->obj.sig_class_name)) == 0))
{
/* Found */
if( iHead->flag & CACHE_INTERNAL_ONLY)
{
iHead->flag ^= (CACHE_INTERNAL_ONLY | CACHE_BOTH);
}
else
{
iHead->flag ^= CACHE_BOTH;
}
iHead->obj.db_sig_class_id = iLookup->db_sig_class_id;
return 1;
}
iHead = iHead->next;
}
return 0;
}
/*
iHead->system == lookup for system obj
iHead = reference lookup for system.
*/
/**
* This function will convert the system cache and the reference cache.
* This as a twist since there is an issue in the map parsing code
* We get reference before system, so we have to lookup for system first
* which is ref->system, then we lookup for reference node in the system node.
* if not present we create the system node then use that system node to insert the reference which is the
* parent, ignore next for both cases since we do not traverse list because of the way its built.
*
* @param iHead
* @param iMasterCache
* @param cSobj
*
* @return
* 0 OK
* 1 ERROR
*/
u_int32_t ConvertReferenceCache(ReferenceNode *iHead,MasterCache *iMasterCache,cacheSignatureObj *cSobj,DatabaseData *data)
{
ReferenceNode *cNode = NULL;
cacheReferenceObj *ref_TobjNode = NULL;
cacheReferenceObj *retRefLookupNode = NULL;
dbReferenceObj ref_LobjNode = {0};
cacheSystemObj *sys_TobjNode = NULL;
cacheSystemObj *sysRetCacheNode = NULL;
dbSystemObj sys_LobjNode = {0};
u_int32_t tItr = 0;
u_int32_t refFound = 0;
if( (iMasterCache == NULL) ||
(cSobj == NULL))
{
/* XXX */
return 1;
}
if(iHead == NULL)
{
/* Nothing to do */
return 0;
}
cNode = iHead;
while(cNode != NULL)
{
memset(&sys_LobjNode,'\0',sizeof(dbSystemObj));
if(cNode->system != NULL)
{
strncpy(sys_LobjNode.ref_system_name,cNode->system->name,SYSTEM_NAME_LEN);
sys_LobjNode.ref_system_name[SYSTEM_NAME_LEN-1] = '\0'; //safety
if( (snort_escape_string_STATIC(sys_LobjNode.ref_system_name,SYSTEM_NAME_LEN,data)))
{
FatalError("database [%s()], Failed a call to snort_escape_string_STATIC() for string : \n"
"[%s], Exiting. \n",
__FUNCTION__,
sys_LobjNode.ref_system_name);
}
}
if(cNode->system->url != NULL)
{
strncpy(sys_LobjNode.ref_system_url,cNode->system->url,SYSTEM_URL_LEN);
sys_LobjNode.ref_system_url[SYSTEM_URL_LEN-1] = '\0'; //safety
if( (snort_escape_string_STATIC(sys_LobjNode.ref_system_url,SYSTEM_URL_LEN,data)))
{
FatalError("database [%s()], Failed a call to snort_escape_string_STATIC() for string : \n"
"[%s], Exiting. \n",
__FUNCTION__,
sys_LobjNode.ref_system_url);
}
}
sysRetCacheNode = NULL;
if(cacheSystemLookup(&sys_LobjNode,iMasterCache->cacheSystemHead,&sysRetCacheNode) == 0)
{
if( (sys_TobjNode = SnortAlloc(sizeof(cacheSystemObj))) == NULL)
{
/* XXX */
return 1;
}
#if DEBUG
file_system_object_count++;
#endif
memcpy(&sys_TobjNode->obj,&sys_LobjNode,sizeof(dbSystemObj));
sys_TobjNode->flag = CACHE_INTERNAL_ONLY;
sys_TobjNode->next = iMasterCache->cacheSystemHead;
iMasterCache->cacheSystemHead = sys_TobjNode;
sysRetCacheNode = sys_TobjNode;
}
if(sysRetCacheNode != NULL)
{
//Populate the lookup reference object
memset(&ref_LobjNode,'\0',sizeof(dbReferenceObj));
strncpy(ref_LobjNode.ref_tag,cNode->id,REF_TAG_LEN);
ref_LobjNode.ref_tag[REF_TAG_LEN-1] = '\0'; //safety
if( (snort_escape_string_STATIC(ref_LobjNode.ref_tag,REF_TAG_LEN,data)))
{
FatalError("database [%s()], Failed a call to snort_escape_string_STATIC() for string : \n"
"[%s], Exiting. \n",
__FUNCTION__,
ref_LobjNode.ref_tag);
}
/* Lookup Reference node */
if((cacheReferenceLookup(&ref_LobjNode,sysRetCacheNode->obj.refList,&retRefLookupNode) == 0))
{
if( (ref_TobjNode = SnortAlloc(sizeof(cacheReferenceObj))) == NULL)
{
/* XXX */
return 1;
}
#if DEBUG
file_reference_object_count++;
#endif
memcpy(&ref_TobjNode->obj,&ref_LobjNode,sizeof(dbReferenceObj));
ref_TobjNode->flag ^= CACHE_INTERNAL_ONLY;
ref_TobjNode->next = sysRetCacheNode->obj.refList;
sysRetCacheNode->obj.refList = ref_TobjNode;
ref_TobjNode->obj.parent = sysRetCacheNode;
if( cSobj->obj.ref_count < MAX_REF_OBJ)
{
cSobj->obj.ref[cSobj->obj.ref_count] = ref_TobjNode;
cSobj->obj.ref_count++;
}
}
else
{
/* Found in reference node, is it already defined in the signature object? */
refFound = 0;
for(tItr=0; tItr < cSobj->obj.ref_count; tItr++)
{
if( (memcmp(&cSobj->obj.ref[tItr]->obj,&retRefLookupNode->obj,sizeof(dbReferenceObj))) == 0)
{
refFound = 1;
break;
}
}
if(refFound == 0)
{
if( cSobj->obj.ref_count < MAX_REF_OBJ)
{
cSobj->obj.ref[cSobj->obj.ref_count] = retRefLookupNode;
cSobj->obj.ref_count++;
}
}
}
}
else
{
/* XXX */
return 1;
}
cNode = cNode->next;
}
return 0;
}
/*
^
|
|
These function are slightly different since they are recurssively called by each signatures
call to ConvertSignatureCache()
*/
u_int32_t SignatureCacheInsertObj(dbSignatureObj *iSigObj,MasterCache *iMasterCache,u_int32_t from)
{
cacheSignatureObj *TobjNode = NULL;
if( (iMasterCache == NULL) ||
(iSigObj == NULL))
{
/* XXX */
return 1;
}
if( (TobjNode = SnortAlloc(sizeof(cacheSignatureObj))) == NULL)
{
/* XXX */
return 1;
}
memcpy(&TobjNode->obj,iSigObj,sizeof(dbSignatureObj));
if( from == 0)
{
TobjNode->flag ^= CACHE_INTERNAL_ONLY;
}
else
{
TobjNode->flag ^= CACHE_BOTH;
}
TobjNode->next = iMasterCache->cacheSignatureHead;
iMasterCache->cacheSignatureHead = TobjNode;
return 0;
}
/**
* This function will convert the signature cache.
*
* @param iHead
* @param iMasterCache
*
* @return
* 0 OK
* 1 ERROR
*/
u_int32_t ConvertSignatureCache(SigNode **iHead,MasterCache *iMasterCache,DatabaseData *data)
{
SigNode *cNode = NULL;
cacheSignatureObj *TobjNode = NULL;
dbSignatureObj lookupNode = {0};
if( (iHead == NULL) ||
(iMasterCache == NULL) ||
(data == NULL))
{
/* XXX */
return 1;
}
if( (cNode = *iHead) == NULL)
{
/* Nothing to do */