Ignore:
Timestamp:
Oct 26, 2025, 12:17:47 PM (16 hours ago)
Author:
katerina
Message:

Fix for ticket #476 (move logfile monitoring module from PCRE to PCRE2).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_log_evalrule.c

    r541 r588  
    1515#define FIL__  _("sh_log_evalrule.c")
    1616
    17 /* Debian/Ubuntu: libpcre3-dev */
    18 #ifdef HAVE_PCRE_PCRE_H
    19 #include <pcre/pcre.h>
     17/* Debian/Ubuntu: libpcre2-dev */
     18#define PCRE2_CODE_UNIT_WIDTH 8
     19#ifdef HAVE_PCRE2_PCRE2_H
     20#include <pcre2/pcre2.h>
    2021#else
    21 #include <pcre.h>
     22#include <pcre2.h>
    2223#endif
    2324
    24 #ifndef PCRE_NO_AUTO_CAPTURE
    25 #define PCRE_NO_AUTO_CAPTURE 0
     25#ifndef PCRE2_NO_AUTO_CAPTURE
     26#define PCRE2_NO_AUTO_CAPTURE 0
    2627#endif
    2728
     
    9596struct sh_geval  /* Group of rules (may be a single rule) */
    9697{
    97   sh_string       * label;           /* label for this group    */
    98   pcre            * rule;            /* compiled regex for rule */
    99   pcre_extra      * rule_extra;
    100   int             * ovector;         /* captured substrings     */
    101   int               ovecnum;         /* how many captured       */
    102   int               captures;        /* (captures+1)*3 required */
    103   int               flags;           /* bit flags               */
    104   unsigned long     delay;           /* delay for keep rules    */
    105   zAVLTree        * counterlist;     /* counters if EVAL_SUM    */
    106   struct sh_qeval * queue;           /* queue for this rule     */
    107   struct sh_geval * nextrule;        /* next rule in this group */
    108   struct sh_geval * next;            /* next group of rules     */
    109   struct sh_geval * gnext;           /* grouplist next          */
     98  sh_string        * label;           /* label for this group    */
     99  pcre2_code       * rule;            /* compiled regex for rule */
     100
     101  pcre2_match_data * match_data;      /* captured substrings     */
     102  int                ovecnum;         /* how many captured       */
     103  int                captures;        /* (captures+1)*3 required */
     104  int                flags;           /* bit flags               */
     105  unsigned long      delay;           /* delay for keep rules    */
     106  zAVLTree         * counterlist;     /* counters if EVAL_SUM    */
     107  struct sh_qeval  * queue;           /* queue for this rule     */
     108  struct sh_geval  * nextrule;        /* next rule in this group */
     109  struct sh_geval  * next;            /* next group of rules     */
     110  struct sh_geval  * gnext;           /* grouplist next          */
    110111};
    111112
    112113struct sh_heval  /* host-specific rules */
    113114{
    114   pcre            * hostname;        /* compiled regex for hostname */
    115   pcre_extra      * hostname_extra;
     115  pcre2_code      * hostname;        /* compiled regex for hostname */
    116116  struct sh_geval * rulegroups;      /* list of group of rules      */
    117117  struct sh_heval * next;
     
    142142  struct sh_geval * ng;
    143143  struct sh_geval * tmp;
    144   pcre * group;
    145   pcre_extra * group_extra;
    146   const char * error;
    147   int          erroffset;
     144  pcre2_code * group;
     145
     146  int          error;
     147  PCRE2_SIZE   erroffset;
    148148  unsigned int nfields = 2;
    149149  size_t       lengths[2];
     150 
    150151  char *       new = sh_util_strdup(str);
    151152  char **      splits = split_array(new, &nfields, ':', lengths);
     
    164165    }
    165166
    166   group = pcre_compile(splits[1], PCRE_NO_AUTO_CAPTURE,
     167  group = pcre2_compile((PCRE2_SPTR8)splits[1], lengths[1], PCRE2_NO_AUTO_CAPTURE,
    167168                       &error, &erroffset, NULL);
    168169  if (!group)
     
    183184      return -1;
    184185    }
    185   group_extra = NULL; /* pcre_study(group, 0, &error); */
    186186
    187187  ng = SH_ALLOC(sizeof(struct sh_geval));
     
    192192
    193193  ng->rule        = group;
    194   ng->rule_extra  = group_extra;
    195   ng->ovector     = NULL;
     194
     195  ng->match_data  = pcre2_match_data_create_from_pattern(group, NULL);
    196196  ng->ovecnum     = 0;
    197197  ng->captures    = 0;
     
    206206      if (0 != sh_eval_hadd("^.*"))
    207207        {
    208           pcre_free(group);
     208          pcre2_code_free(group);
     209          pcre2_match_data_free(ng->match_data);
    209210          sh_string_destroy(&(ng->label));
    210211          SH_FREE(splits);
     
    266267  struct sh_heval * nh;
    267268  struct sh_heval * tmp;
    268   pcre *  host;
    269   pcre_extra * host_extra;
    270   const char * error;
    271   int          erroffset;
     269  pcre2_code    *  host;
     270
     271  int              error;
     272  PCRE2_SIZE        erroffset;
    272273
    273274  if (host_open)
    274275    host_open = NULL;
    275276
    276   host = pcre_compile(str, PCRE_NO_AUTO_CAPTURE,
    277                       &error, &erroffset, NULL);
     277  host = pcre2_compile((PCRE2_SPTR8)str, PCRE2_ZERO_TERMINATED, PCRE2_NO_AUTO_CAPTURE,
     278                       &error, &erroffset, NULL);
    278279  if (!host)
    279280    {
     
    291292      return -1;
    292293    }
    293   host_extra = NULL; /* pcre_study(host, 0, &error); */
    294294
    295295  nh = SH_ALLOC(sizeof(struct sh_heval));
     
    297297
    298298  nh->hostname = host;
    299   nh->hostname_extra = host_extra;
    300299  nh->rulegroups = NULL;
    301300
     
    478477  struct sh_geval * tmp;
    479478  struct sh_qeval * queue;
    480   pcre *  rule;
    481   pcre_extra * rule_extra;
    482   const char * error;
    483   int          erroffset;
    484   int          captures = 0;
     479  pcre2_code    *  rule;
     480
     481  int          error;
     482  PCRE2_SIZE   erroffset;
     483  unsigned int captures = 0;
    485484  unsigned int nfields = 2; /* queue:regex */
    486485  size_t       lengths[3];
     
    576575    }
    577576
    578   rule = pcre_compile(splits[rpos], 0,
    579                       &error, &erroffset, NULL);
     577  rule = pcre2_compile((PCRE2_SPTR8)splits[rpos], lengths[rpos], 0,
     578                       &error, &erroffset, NULL);
    580579  if (!rule)
    581580    {
     
    595594      return -1;
    596595    }
    597   rule_extra = NULL; /* pcre_study(rule, 0, &error); */
    598   pcre_fullinfo(rule, rule_extra, PCRE_INFO_CAPTURECOUNT, &captures);
     596
     597  pcre2_pattern_info(rule, PCRE2_INFO_CAPTURECOUNT, &captures);
    599598
    600599  if (flag_err_debug == S_TRUE)
     
    602601      char * emsg = SH_ALLOC(SH_ERRBUF_SIZE);
    603602      if (dstr)
    604         sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %d captures, keep(%lu,%s)"),
     603        sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %u captures, keep(%lu,%s)"),
    605604                    splits[rpos], captures, dsec, dstr);
    606605      else
    607         sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %d captures"),
     606        sl_snprintf(emsg,  SH_ERRBUF_SIZE, _("Adding rule: |%s| with %u captures"),
    608607                    splits[rpos], captures);
    609608      SH_MUTEX_LOCK(mutex_thread_nolog);
     
    614613    }
    615614
    616   DEBUG("adding rule: |%s| with %d captures\n", splits[rpos], captures);
     615  DEBUG("adding rule: |%s| with %u captures\n", splits[rpos], captures);
    617616
    618617  SH_FREE(splits);
     
    627626
    628627  nr->rule        = rule;
    629   nr->rule_extra  = rule_extra;
     628
    630629  nr->captures    = captures;
    631   nr->ovector     = SH_ALLOC(sizeof(int) * (captures+1) * 3);
     630  nr->match_data  = pcre2_match_data_create_from_pattern(rule, NULL);
    632631  nr->ovecnum     = 0;
    633632  nr->counterlist = NULL;
     
    695694              if (nr->label)
    696695                sh_string_destroy(&(nr->label));
    697               SH_FREE(nr->ovector);
     696              if (nr->rule)
     697                pcre2_code_free(nr->rule);
     698              if (nr->match_data)
     699                pcre2_match_data_free(nr->match_data);
    698700              SH_FREE(nr);
    699701              return -1;
     
    743745          if (nr->label)
    744746            sh_string_destroy(&(nr->label));
    745           SH_FREE(nr->ovector);
     747          if (nr->rule)
     748            pcre2_code_free(nr->rule);
     749          if (nr->match_data)
     750            pcre2_match_data_free(nr->match_data);
    746751          SH_FREE(nr);
    747752          return -1;
     
    763768      grouplist = gtmp->gnext;
    764769
    765       if (gtmp->label)      sh_string_destroy(&(gtmp->label));
    766       if (gtmp->rule_extra) (*pcre_free)(gtmp->rule_extra);
    767       if (gtmp->rule)       (*pcre_free)(gtmp->rule);
     770      if (gtmp->label)
     771        sh_string_destroy(&(gtmp->label));
     772      if (gtmp->rule)
     773        pcre2_code_free(gtmp->rule);
     774      if (gtmp->match_data)
     775        pcre2_match_data_free(gtmp->match_data);
    768776      if (gtmp->counterlist)
    769777        zAVLFreeTree(gtmp->counterlist, sh_ceval_free);
    770       if (gtmp->ovector)
    771         SH_FREE(gtmp->ovector);
     778
    772779#if 0
    773780      while (gtmp->nextrule)
     
    776783          gtmp->nextrule = tmp->nextrule;
    777784
    778           if (tmp->rule_extra) (*pcre_free)(tmp->rule_extra);
    779           if (tmp->rule)       (*pcre_free)(tmp->rule);
     785          if (tmp->rule)
     786            pcre2_code_free(tmp->rule);
    780787          if (tmp->counterlist)
    781788            zAVLFreeTree(tmp->counterlist, sh_ceval_free);
     
    800807  while (htmp)
    801808    {
    802       if (htmp->hostname_extra) (*pcre_free)(htmp->hostname_extra);
    803       if (htmp->hostname)       (*pcre_free)(htmp->hostname);
    804       if (htmp->rulegroups)     htmp->rulegroups = NULL;
     809      if (htmp->hostname)
     810        pcre2_code_free(htmp->hostname);
     811      if (htmp->rulegroups)
     812        htmp->rulegroups = NULL;
    805813      hostlist = htmp->next;
    806814      htmp->next = NULL;
     
    862870
    863871        DEBUG("debug: check rule %d for <%s>\n", count, msg->str);
    864         res = pcre_exec(rule->rule, rule->rule_extra,
    865                         sh_string_str(msg), (int)sh_string_len(msg), 0,
    866                         0, rule->ovector, (3*(1+rule->captures)));
    867         if (res >= 0)
     872       
     873        res = pcre2_match(rule->rule,
     874                          (PCRE2_SPTR8)sh_string_str(msg), (int)sh_string_len(msg), 0,
     875                          0,   rule->match_data, NULL);
     876
     877        if (res > 0)
    868878          {
    869             rule->ovecnum = res;
     879            rule->ovecnum = pcre2_get_ovector_count(rule->match_data);
    870880
    871881            if (flag_err_debug == S_TRUE)
     
    965975
    966976            DEBUG("debug: if group->label %s\n", sh_string_str(group->label));
    967             if (pcre_exec(group->rule, group->rule_extra,
    968                           sh_string_str(msg), (int) sh_string_len(msg),
    969                           0, 0, NULL, 0) >= 0)
     977           
     978            if (pcre2_match(group->rule,
     979                            (PCRE2_SPTR8)sh_string_str(msg), (int) sh_string_len(msg), 0,
     980                            0, NULL, NULL) > 0)
    970981              {
    971982                result = test_rule(group->nextrule, msg, timestamp);
     
    10171028    {
    10181029      do {
    1019         if (pcre_exec(hlist->hostname, hlist->hostname_extra,
    1020                       sh_string_str(host), (int) sh_string_len(host),
    1021                       0, 0, NULL, 0) >= 0)
     1030        if (pcre2_match(hlist->hostname,
     1031                        (PCRE2_SPTR8)sh_string_str(host), (int) sh_string_len(host), 0,
     1032                        0, NULL, NULL) > 0)
    10221033          {
    10231034            /* matching host, check rules/groups of rules */
     
    10351046 */
    10361047static sh_string * replace_captures(const sh_string * message,
    1037                                     int * ovector, int ovecnum)
     1048                                    size_t * ovector, int ovecnum)
    10381049{
    10391050  sh_string * retval = sh_string_new_from_lchar(sh_string_str(message),
     
    10581069  SH_MUTEX_LOCK(mutex_thread_nolog);
    10591070  if (rule) {
    1060     mmm = replace_captures(record->message, rule->ovector,
     1071    mmm = replace_captures(record->message, pcre2_get_ovector_pointer(rule->match_data),
    10611072                           rule->ovecnum);
    10621073    rule->ovecnum = 0;
     
    11701181  if (!(counter->counted_str))
    11711182    {
    1172       counter->counted_str = replace_captures(record->message, rule->ovector,
     1183      counter->counted_str = replace_captures(record->message,  pcre2_get_ovector_pointer(rule->match_data),
    11731184                                              rule->ovecnum);
    11741185      rule->ovecnum        = 0;
Note: See TracChangeset for help on using the changeset viewer.