Browse code

moved ldb_tokenize in readdb to cli_ldbtokenize in str

Kevin Lin authored on 2015/07/22 06:29:23
Showing 3 changed files
... ...
@@ -1580,41 +1580,13 @@ static inline int init_tdb(struct cli_lsig_tdb *tdb, struct cl_engine *engine, c
1580 1580
  * NOTE: Maximum of 64(see MAX_LDB_SUBSIGS) subsignatures (last would be token 66)
1581 1581
  */
1582 1582
 #define LDB_TOKENS 67
1583
-static int ldb_tokenize(char *buffer, const char **tokens)
1584
-{
1585
-    const size_t token_count = LDB_TOKENS + 1;
1586
-    size_t tokens_found, i;
1587
-    int within_pcre = 0;
1588
-
1589
-    for(tokens_found = 0; tokens_found < token_count; ) {
1590
-        tokens[tokens_found++] = buffer;
1591
-
1592
-        while (*buffer != '\0') {
1593
-            if (!within_pcre && (*buffer == ';'))
1594
-                break;
1595
-            else if ((tokens_found > 2) && (*(buffer-1) != '\\') && (*buffer == '/'))
1596
-                within_pcre = !within_pcre;
1597
-            buffer++;
1598
-        }
1599
-
1600
-        if(*buffer != '\0') {
1601
-            *buffer++ = '\0';
1602
-        } else {
1603
-            i = tokens_found;
1604
-            while(i < token_count)
1605
-                tokens[i++] = NULL;
1606
-            return tokens_found;
1607
-        }
1608
-    }
1609
-    return tokens_found;
1610
-}
1611
-
1583
+#define SUB_TOKENS 4
1612 1584
 static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsigned int options, const char *dbname, unsigned int line, unsigned int *sigs, unsigned bc_idx, const char *buffer_cpy, int *skip)
1613 1585
 {
1614
-    const char *sig, *virname, *offset, *logic;
1586
+    const char *sig, *virname, *offset, *logic, *sigopts;
1615 1587
     struct cli_ac_lsig **newtable, *lsig;
1616
-    char *tokens[LDB_TOKENS+1], *pt, *lsl, *rsl;
1617
-    int i, subsigs, tokens_count;
1588
+    char *tokens[LDB_TOKENS+1], *subtokens[SUB_TOKENS+1];
1589
+    int i, j, subsigs, tokens_count, subtokens_count;
1618 1590
     unsigned short target = 0;
1619 1591
     struct cli_matcher *root;
1620 1592
     struct cli_lsig_tdb tdb;
... ...
@@ -1624,8 +1596,9 @@ static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsig
1624 1624
 
1625 1625
     UNUSEDPARAM(dbname);
1626 1626
 
1627
-    tokens_count = ldb_tokenize(buffer, (const char **) tokens);
1627
+    tokens_count = cli_ldbtokenize(buffer, ';', LDB_TOKENS + 1, (const char **) tokens, 2);
1628 1628
     if(tokens_count < 4) {
1629
+        cli_errmsg("Invalid or unsupported ldb signature format\n");
1629 1630
         return CL_EMALFDB;
1630 1631
     }
1631 1632
 
... ...
@@ -1649,9 +1622,9 @@ static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsig
1649 1649
 
1650 1650
     subsigs = cli_ac_chklsig(logic, logic + strlen(logic), NULL, NULL, NULL, 1);
1651 1651
     if(subsigs == -1) {
1652
+        cli_errmsg("Invalid or unsupported ldb logic\n");
1652 1653
         return CL_EMALFDB;
1653 1654
     }
1654
-
1655 1655
     subsigs++;
1656 1656
 
1657 1657
 #if !HAVE_PCRE
... ...
@@ -1682,8 +1655,8 @@ static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsig
1682 1682
 
1683 1683
     /* enforce MAX_LDB_SUBSIGS(currently 64) subsig cap */
1684 1684
     if(subsigs > MAX_LDB_SUBSIGS) {
1685
-	cli_errmsg("cli_loadldb: Broken logical expression or too many subsignatures\n");
1686
-	return CL_EMALFDB;
1685
+        cli_errmsg("cli_loadldb: Broken logical expression or too many subsignatures\n");
1686
+        return CL_EMALFDB;
1687 1687
     }
1688 1688
 
1689 1689
     /* TDB */
... ...
@@ -1733,53 +1706,49 @@ static int load_oneldb(char *buffer, int chkpua, struct cl_engine *engine, unsig
1733 1733
     tdb.subsigs = subsigs;
1734 1734
 
1735 1735
     for(i = 0; i < subsigs; i++) {
1736
-        subsig_opts = 0;
1737
-
1738 1736
         lsigid[1] = i;
1739 1737
         offset = "*";
1740
-        sig = tokens[3 + i];
1741
-
1742
-        /* check for offset and subsig modifiers */
1743
-        pt = tokens[3 + i];
1744
-        lsl = strchr(tokens[3 + i], '/');
1745
-        rsl = strrchr(tokens[3 + i], '/');
1746
-        while((pt = strchr(pt, ':'))) {
1747
-            /* pcre subsig expression */
1748
-            if((lsl && rsl) && (lsl < pt) && (pt <  rsl)) {
1749
-                pt++;
1750
-                continue;
1751
-            }
1752 1738
 
1753
-            *pt++ = 0;
1754
-            if(*pt == ':') { /* signature modifiers */
1755
-                *pt++ = 0;
1756
-                while(*pt != '\0') {
1757
-                    switch(*pt) {
1758
-                    case 'i':
1759
-                        subsig_opts |= ACPATT_OPTION_NOCASE;
1760
-                        break;
1761
-                    case 'f':
1762
-                        subsig_opts |= ACPATT_OPTION_FULLWORD;
1763
-                        break;
1764
-                    case 'w':
1765
-                        subsig_opts |= ACPATT_OPTION_WIDE;
1766
-                        break;
1767
-                    case 'a':
1768
-                        subsig_opts |= ACPATT_OPTION_ASCII;
1769
-                        break;
1770
-                    default:
1771
-                        cli_errmsg("cli_loadldb: Signature for %s uses invalid option: %02x\n", virname, *pt);
1772
-                        return CL_EMALFDB;
1773
-                    }
1739
+        sigopts = NULL;
1740
+        subsig_opts = 0;
1741
+
1742
+        subtokens_count = cli_ldbtokenize(tokens[3 + i], ':', SUB_TOKENS + 1, (const char **) subtokens, 0);
1743
+	    if(!subtokens_count) {
1744
+            cli_errmsg("Invalid or unsupported ldb subsignature format\n");
1745
+            return CL_EMALFDB;
1746
+        }
1747
+
1748
+	    if((subtokens_count % 2) == 0)
1749
+            offset = subtokens[0];
1774 1750
 
1775
-                    pt++;
1751
+	    if(subtokens_count == 3)
1752
+            sigopts = subtokens[2];
1753
+        else if(subtokens_count == 4)
1754
+            sigopts = subtokens[3];
1755
+
1756
+        if(sigopts) { /* signature modifiers */
1757
+            for(j = 0; j < strlen(sigopts); j++)
1758
+                switch(sigopts[j]) {
1759
+                case 'i':
1760
+                    subsig_opts |= ACPATT_OPTION_NOCASE;
1761
+                    break;
1762
+                case 'f':
1763
+                    subsig_opts |= ACPATT_OPTION_FULLWORD;
1764
+                    break;
1765
+                case 'w':
1766
+                    subsig_opts |= ACPATT_OPTION_WIDE;
1767
+                    break;
1768
+                case 'a':
1769
+                    subsig_opts |= ACPATT_OPTION_ASCII;
1770
+                    break;
1771
+                default:
1772
+                    cli_errmsg("cli_loadldb: Signature for %s uses invalid option: %02x\n", virname, sigopts[j]);
1773
+                    return CL_EMALFDB;
1776 1774
                 }
1777
-            } else {
1778
-                sig = pt;
1779
-                offset = tokens[3 + i];
1780
-            }
1781 1775
         }
1782 1776
 
1777
+        sig = (subtokens_count % 2) ? subtokens[0] : subtokens[1];
1778
+
1783 1779
         if(subsig_opts)
1784 1780
             ret = cli_sigopts_handler(root, virname, sig, subsig_opts, 0, 0, offset, target, lsigid, options);
1785 1781
         else
... ...
@@ -480,6 +480,34 @@ size_t cli_strtokenize(char *buffer, const char delim, const size_t token_count,
480 480
     return tokens_found;
481 481
 }
482 482
 
483
+size_t cli_ldbtokenize(char *buffer, const char delim, const size_t token_count, const char **tokens, int token_skip)
484
+{
485
+    size_t tokens_found, i;
486
+    int within_pcre = 0;
487
+
488
+    for(tokens_found = 0; tokens_found < token_count; ) {
489
+        tokens[tokens_found++] = buffer;
490
+
491
+        while (*buffer != '\0') {
492
+            if (!within_pcre && (*buffer == delim))
493
+                break;
494
+            else if ((tokens_found > token_skip) && (*(buffer-1) != '\\') && (*buffer == '/'))
495
+                within_pcre = !within_pcre;
496
+            buffer++;
497
+        }
498
+
499
+        if(*buffer != '\0') {
500
+            *buffer++ = '\0';
501
+        } else {
502
+            i = tokens_found;
503
+            while(i < token_count)
504
+                tokens[i++] = NULL;
505
+            return tokens_found;
506
+        }
507
+    }
508
+    return tokens_found;
509
+}
510
+
483 511
 int cli_isnumber(const char *str)
484 512
 {
485 513
     while(*str)
... ...
@@ -51,6 +51,7 @@ char *cli_strtokbuf(const char *input, int fieldno, const char *delim, char *out
51 51
 const char *cli_memstr(const char *haystack, unsigned int hs, const char *needle, unsigned int ns);
52 52
 char *cli_strrcpy(char *dest, const char *source);
53 53
 size_t cli_strtokenize(char *buffer, const char delim, const size_t token_count, const char **tokens);
54
+size_t cli_ldbtokenize(char *buffer, const char delim, const size_t token_count, const char **tokens, int token_skip);
54 55
 int cli_isnumber(const char *str);
55 56
 char *cli_unescape(const char *str);
56 57
 struct text_buffer;