~ubuntu-branches/ubuntu/utopic/suricata/utopic

« back to all changes in this revision

Viewing changes to src/util-classification-config.c

  • Committer: Package Import Robot
  • Author(s): Pierre Chifflier
  • Date: 2012-07-22 22:27:36 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120722222736-s2bcw3ruzenagjam
Tags: 1.3-1
* Imported Upstream version 1.3
* Add build-dependency on libnss3-dev and libnspr4-dev
* Bump Standards Version to 3.9.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#define DETECT_CLASSCONFIG_REGEX "^\\s*config\\s*classification\\s*:\\s*([a-zA-Z][a-zA-Z0-9-_]*)\\s*,\\s*(.+)\\s*,\\s*(\\d+)\\s*$"
42
42
 
43
43
/* Default path for the classification.config file */
44
 
#define SC_CLASS_CONF_DEF_CONF_FILEPATH "classification.config"
 
44
#define SC_CLASS_CONF_DEF_CONF_FILEPATH CONFIG_DIR "/classification.config"
45
45
 
46
46
/* Holds a pointer to the default path for the classification.config file */
47
47
static const char *default_file_path = SC_CLASS_CONF_DEF_CONF_FILEPATH;
69
69
 * \retval  0 On success.
70
70
 * \retval -1 On failure.
71
71
 */
72
 
int SCClassConfInitContext(DetectEngineCtx *de_ctx)
 
72
int SCClassConfInitContextAndLocalResources(DetectEngineCtx *de_ctx)
73
73
{
74
74
    char *filename = NULL;
75
75
    const char *eb = NULL;
83
83
    if (de_ctx->class_conf_ht == NULL) {
84
84
        SCLogError(SC_ERR_HASH_TABLE_INIT, "Error initializing the hash "
85
85
                   "table");
86
 
        return -1;
 
86
        goto error;
87
87
    }
88
88
 
89
89
    /* if it is not NULL, use the file descriptor.  The hack so that we can
123
123
        fd = NULL;
124
124
    }
125
125
 
126
 
    printf("\nPlease check the \"classification-file\" option in your suricata.yaml file.\n");
127
 
    exit(EXIT_FAILURE);
128
 
//    return -1;
 
126
    if (regex != NULL) {
 
127
        pcre_free(regex);
 
128
        regex = NULL;
 
129
    }
 
130
    if (regex_study != NULL) {
 
131
        pcre_free(regex_study);
 
132
        regex_study = NULL;
 
133
    }
 
134
 
 
135
    return -1;
129
136
}
130
137
 
131
138
 
152
159
/**
153
160
 * \brief Releases resources used by the Classification Config API.
154
161
 */
155
 
static void SCClassConfDeInitContext(DetectEngineCtx *de_ctx)
 
162
static void SCClassConfDeInitLocalResources(DetectEngineCtx *de_ctx)
156
163
{
157
164
 
158
165
    fclose(fd);
159
166
    default_file_path = SC_CLASS_CONF_DEF_CONF_FILEPATH;
160
167
    fd = NULL;
 
168
    if (regex != NULL) {
 
169
        pcre_free(regex);
 
170
        regex = NULL;
 
171
    }
 
172
    if (regex_study != NULL) {
 
173
        pcre_free(regex_study);
 
174
        regex_study = NULL;
 
175
    }
 
176
 
 
177
    return;
 
178
}
 
179
 
 
180
/**
 
181
 * \brief Releases resources used by the Classification Config API.
 
182
 */
 
183
void SCClassConfDeInitContext(DetectEngineCtx *de_ctx)
 
184
{
 
185
    if (de_ctx->class_conf_ht != NULL)
 
186
        HashTableFree(de_ctx->class_conf_ht);
 
187
 
 
188
    de_ctx->class_conf_ht = NULL;
 
189
 
161
190
    return;
162
191
}
163
192
 
485
514
 */
486
515
void SCClassConfLoadClassficationConfigFile(DetectEngineCtx *de_ctx)
487
516
{
488
 
    if (SCClassConfInitContext(de_ctx) == -1) {
489
 
        SCLogDebug("Error initializing classification config API");
490
 
        return;
 
517
    if (SCClassConfInitContextAndLocalResources(de_ctx) == -1) {
 
518
        SCLogInfo("Please check the \"classification-file\" option in your suricata.yaml file");
 
519
        exit(EXIT_FAILURE);
491
520
    }
492
521
 
493
522
    SCClassConfParseFile(de_ctx);
494
 
    SCClassConfDeInitContext(de_ctx);
 
523
    SCClassConfDeInitLocalResources(de_ctx);
495
524
 
496
525
    return;
497
526
}
498
527
 
 
528
/**
 
529
 * \brief Gets the classtype from the corresponding hash table stored
 
530
 *        in the Detection Engine Context's class conf ht, given the
 
531
 *        classtype name.
 
532
 *
 
533
 * \param ct_name Pointer to the classtype name that has to be looked up.
 
534
 * \param de_ctx  Pointer to the Detection Engine Context.
 
535
 *
 
536
 * \retval lookup_ct_info Pointer to the SCClassConfClasstype instance from
 
537
 *                        the hash table on success; NULL on failure.
 
538
 */
 
539
SCClassConfClasstype *SCClassConfGetClasstype(const char *ct_name,
 
540
                                              DetectEngineCtx *de_ctx)
 
541
{
 
542
    SCClassConfClasstype *ct_info = SCClassConfAllocClasstype(0, ct_name, NULL,
 
543
                                                              0);
 
544
    if (ct_info == NULL)
 
545
        return NULL;
 
546
    SCClassConfClasstype *lookup_ct_info = HashTableLookup(de_ctx->class_conf_ht,
 
547
                                                           ct_info, 0);
 
548
 
 
549
    SCClassConfDeAllocClasstype(ct_info);
 
550
    return lookup_ct_info;
 
551
}
499
552
 
500
553
/*----------------------------------Unittests---------------------------------*/
501
554
 
668
721
int SCClassConfTest04(void)
669
722
{
670
723
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
671
 
    SCClassConfClasstype *ct = NULL;
672
724
    int result = 1;
673
725
 
674
726
    if (de_ctx == NULL)
683
735
 
684
736
    result = (de_ctx->class_conf_ht->count == 3);
685
737
 
686
 
    ct = SCClassConfAllocClasstype(0, "unknown", NULL, 0);
687
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
688
 
    SCClassConfDeAllocClasstype(ct);
689
 
 
690
 
    ct = SCClassConfAllocClasstype(0, "unKnoWn", NULL, 0);
691
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
692
 
    SCClassConfDeAllocClasstype(ct);
693
 
 
694
 
    ct = SCClassConfAllocClasstype(0, "bamboo", NULL, 0);
695
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
696
 
    SCClassConfDeAllocClasstype(ct);
697
 
 
698
 
    ct = SCClassConfAllocClasstype(0, "bad-unknown", NULL, 0);
699
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
700
 
    SCClassConfDeAllocClasstype(ct);
701
 
 
702
 
    ct = SCClassConfAllocClasstype(0, "BAD-UNKnOWN", NULL, 0);
703
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
704
 
    SCClassConfDeAllocClasstype(ct);
705
 
 
706
 
    ct = SCClassConfAllocClasstype(0, "bed-unknown", NULL, 0);
707
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
708
 
    SCClassConfDeAllocClasstype(ct);
 
738
    result &= (SCClassConfGetClasstype("unknown", de_ctx) != NULL);
 
739
    result &= (SCClassConfGetClasstype("unKnoWn", de_ctx) != NULL);
 
740
    result &= (SCClassConfGetClasstype("bamboo", de_ctx) == NULL);
 
741
    result &= (SCClassConfGetClasstype("bad-unknown", de_ctx) != NULL);
 
742
    result &= (SCClassConfGetClasstype("BAD-UNKnOWN", de_ctx) != NULL);
 
743
    result &= (SCClassConfGetClasstype("bed-unknown", de_ctx) == NULL);
709
744
 
710
745
    DetectEngineCtxFree(de_ctx);
711
746
 
720
755
int SCClassConfTest05(void)
721
756
{
722
757
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
723
 
    SCClassConfClasstype *ct = NULL;
724
758
    int result = 1;
725
759
 
726
760
    if (de_ctx == NULL)
735
769
 
736
770
    result = (de_ctx->class_conf_ht->count == 0);
737
771
 
738
 
    ct = SCClassConfAllocClasstype(0, "unknown", NULL, 0);
739
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
740
 
    SCClassConfDeAllocClasstype(ct);
741
 
 
742
 
    ct = SCClassConfAllocClasstype(0, "unKnoWn", NULL, 0);
743
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
744
 
    SCClassConfDeAllocClasstype(ct);
745
 
 
746
 
    ct = SCClassConfAllocClasstype(0, "bamboo", NULL, 0);
747
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
748
 
    SCClassConfDeAllocClasstype(ct);
749
 
 
750
 
    ct = SCClassConfAllocClasstype(0, "bad-unknown", NULL, 0);
751
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
752
 
    SCClassConfDeAllocClasstype(ct);
753
 
 
754
 
    ct = SCClassConfAllocClasstype(0, "BAD-UNKnOWN", NULL, 0);
755
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
756
 
    SCClassConfDeAllocClasstype(ct);
757
 
 
758
 
    ct = SCClassConfAllocClasstype(0, "bed-unknown", NULL, 0);
759
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
760
 
    SCClassConfDeAllocClasstype(ct);
 
772
    result &= (SCClassConfGetClasstype("unknown", de_ctx) == NULL);
 
773
    result &= (SCClassConfGetClasstype("unKnoWn", de_ctx) == NULL);
 
774
    result &= (SCClassConfGetClasstype("bamboo", de_ctx) == NULL);
 
775
    result &= (SCClassConfGetClasstype("bad-unknown", de_ctx) == NULL);
 
776
    result &= (SCClassConfGetClasstype("BAD-UNKnOWN", de_ctx) == NULL);
 
777
    result &= (SCClassConfGetClasstype("bed-unknown", de_ctx) == NULL);
761
778
 
762
779
    DetectEngineCtxFree(de_ctx);
763
780
 
771
788
int SCClassConfTest06(void)
772
789
{
773
790
    DetectEngineCtx *de_ctx = DetectEngineCtxInit();
774
 
    SCClassConfClasstype *ct = NULL;
775
791
    int result = 1;
776
792
 
777
793
    if (de_ctx == NULL)
786
802
 
787
803
    result = (de_ctx->class_conf_ht->count == 3);
788
804
 
789
 
    ct = SCClassConfAllocClasstype(0, "unknown", NULL, 0);
790
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
791
 
    SCClassConfDeAllocClasstype(ct);
792
 
 
793
 
    ct = SCClassConfAllocClasstype(0, "not-suspicious", NULL, 0);
794
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
795
 
    SCClassConfDeAllocClasstype(ct);
796
 
 
797
 
    ct = SCClassConfAllocClasstype(0, "bamboola1", NULL, 0);
798
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
799
 
    SCClassConfDeAllocClasstype(ct);
800
 
 
801
 
    ct = SCClassConfAllocClasstype(0, "bamboola1", NULL, 0);
802
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
803
 
    SCClassConfDeAllocClasstype(ct);
804
 
 
805
 
    ct = SCClassConfAllocClasstype(0, "BAMBOolA1", NULL, 0);
806
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) != NULL);
807
 
    SCClassConfDeAllocClasstype(ct);
808
 
 
809
 
    ct = SCClassConfAllocClasstype(0, "unkNOwn", NULL, 0);
810
 
    result &= (HashTableLookup(de_ctx->class_conf_ht, ct, 0) == NULL);
811
 
    SCClassConfDeAllocClasstype(ct);
 
805
    result &= (SCClassConfGetClasstype("unknown", de_ctx) == NULL);
 
806
    result &= (SCClassConfGetClasstype("not-suspicious", de_ctx) != NULL);
 
807
    result &= (SCClassConfGetClasstype("bamboola1", de_ctx) != NULL);
 
808
    result &= (SCClassConfGetClasstype("bamboola1", de_ctx) != NULL);
 
809
    result &= (SCClassConfGetClasstype("BAMBOolA1", de_ctx) != NULL);
 
810
    result &= (SCClassConfGetClasstype("unkNOwn", de_ctx) == NULL);
812
811
 
813
812
    DetectEngineCtxFree(de_ctx);
814
813