Changeset 588


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

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

Location:
trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r583 r588  
    16791679        [
    16801680        if test "x${enable_logfile_monitor}" = xyes; then
    1681            AC_CHECK_HEADER(pcre.h,
     1681           cflags_store="$CFLAGS"
     1682           CFLAGS="$CFLAGS -DPCRE2_CODE_UNIT_WIDTH=8"
     1683           AC_CHECK_HEADER(pcre2.h,
    16821684                           [
    16831685                           AC_DEFINE([USE_LOGFILE_MONITOR], [1], [Define if you want the logfile monitor module.])
    1684                            LIBS="-lpcre $LIBS"
     1686                           LIBS="-lpcre2-8 $LIBS"
    16851687                           ],
    16861688                           [
    1687                                 AC_CHECK_HEADER(pcre/pcre.h,
     1689                                AC_CHECK_HEADER(pcre2/pcre2.h,
    16881690                                [
    16891691                                AC_DEFINE([USE_LOGFILE_MONITOR], [1], [Define if you want the logfile monitor module.])
    1690                                 AC_DEFINE([HAVE_PCRE_PCRE_H], [1], [Define if you have pcre/pcre.h.])
    1691                                 LIBS="-lpcre $LIBS"
     1692                                AC_DEFINE([HAVE_PCRE2_PCRE2_H], [1], [Define if you have pcre2/pcre2.h.])
     1693                                LIBS="-lpcre2-8 $LIBS"
    16921694                                ],
    16931695                                [AC_MSG_ERROR([The --enable-logfile-monitor option requires libpcre. For compiling the pcre development package is needed.])]
     
    16951697                          ]
    16961698           )
    1697            AC_CHECK_LIB(pcre, pcre_dfa_exec, [
    1698                               AC_DEFINE([HAVE_PCRE_DFA_EXEC], [1], [Define if you have pcre_dfa_exec])
    1699                               ], [
    1700                               AC_MSG_WARN([pcre_dfa_exec not available])
    1701                               ])
     1699          CFLAGS="$cflags_store"
    17021700        fi
    17031701        ]
  • trunk/docs/Changelog

    r584 r588  
    1 4.5.2
     14.5.3:
     2        * fix inadvertent inclusion of scripts/logrotate and init/samhain.startSystemd
     3        (reported by kjd)
     4        * fix annoying message from systemd about read permissions for service file
     5        (reported by kjd)
     6        * fix compiler warnings about unused variables
     7        * move logfile monitor module from PCRE to PCRE2 (PCRE is end of life and no
     8        longer actively maintained)
     9
     104.5.2 (02-01-2025):
    211        * fix segfault with --enable-static on unresolvable host name
    312        * fix autoreconf (problem reported by Pascal de Bruijn)
  • trunk/include/sh_string.h

    r577 r588  
    107107 */ 
    108108sh_string * sh_string_replace(const sh_string * s,
    109                               const int * ovector, int ovecnum,
     109                              const size_t * ovector, int ovecnum,
    110110                              const char * replacement, size_t rlen);
    111111
  • trunk/src/sh_log_check.c

    r581 r588  
    3939#define FIL__  _("sh_log_check.c")
    4040
    41 /* Debian/Ubuntu: libpcre3-dev */
    42 #ifdef HAVE_PCRE_PCRE_H
    43 #include <pcre/pcre.h>
     41/* Debian/Ubuntu: libpcre2-dev */
     42#define PCRE2_CODE_UNIT_WIDTH 8
     43#ifdef HAVE_PCRE2_PCRE2_H
     44#include <pcre2/pcre2.h>
    4445#else
    45 #include <pcre.h>
     46#include <pcre2.h>
    4647#endif
    4748
  • trunk/src/sh_log_correlate.c

    r481 r588  
    99#include <time.h>
    1010
    11 /* Debian/Ubuntu: libpcre3-dev */
    12 #ifdef HAVE_PCRE_PCRE_H
    13 #include <pcre/pcre.h>
     11/* Debian/Ubuntu: libpcre2-dev */
     12#define PCRE2_CODE_UNIT_WIDTH 8
     13#ifdef HAVE_PCRE2_PCRE2_H
     14#include <pcre2/pcre2.h>
    1415#else
    15 #include <pcre.h>
     16#include <pcre2.h>
    1617#endif
    1718
    18 #ifndef PCRE_NO_AUTO_CAPTURE
    19 #define PCRE_NO_AUTO_CAPTURE 0
    20 #endif
    2119
    2220#include "samhain.h"
     
    173171{
    174172  sh_string       * label;           /* label of match rule     */
    175   pcre            * rule;            /* compiled regex for rule */
     173  pcre2_code      * rule;            /* compiled regex for rule */
    176174  time_t            reported;        /* last reported           */
    177175  struct sh_qeval * queue;           /* assigned queue          */
     
    208206    {
    209207      struct sh_mkeep * mkeep = SH_ALLOC(sizeof(struct sh_mkeep));
    210       const char * error;
    211       int          erroffset;
     208      int              error;
     209      size_t            erroffset;
    212210      struct sh_qeval * rqueue = NULL;
    213211
    214       mkeep->rule = pcre_compile(pattern, PCRE_NO_AUTO_CAPTURE,
    215                              &error, &erroffset, NULL);
     212      mkeep->rule = pcre2_compile((PCRE2_SPTR8)pattern, PCRE2_ZERO_TERMINATED, PCRE2_NO_AUTO_CAPTURE,
     213                                  &error, &erroffset, NULL);
    216214      if (!(mkeep->rule))
    217215        {
     
    239237          if (!rqueue)
    240238            {
    241               pcre_free(mkeep->rule);
     239              pcre2_code_free(mkeep->rule);
    242240              SH_FREE(splits);
    243241              SH_FREE(mkeep);
     
    264262      mkeep_list = mkeep->next;
    265263      sh_string_destroy(&(mkeep->label));
    266       pcre_free(mkeep->rule);
     264      pcre2_code_free(mkeep->rule);
    267265      mkeep = mkeep_list;
    268266    }
     
    286284          while (mkeep)
    287285            {
    288               /* Use pcre_dfa_exec() to obtain number of matches. Needs ovector
    289                * array, otherwise number of matches is not returned.
    290                */
    291 #if defined(HAVE_PCRE_DFA_EXEC)
    292               int ovector[SH_MINIBUF];
    293               int wspace[SH_MINIBUF];
    294 #endif
    295 
    296 #if defined(HAVE_PCRE_DFA_EXEC)
    297               int val = pcre_dfa_exec(mkeep->rule, NULL,
    298                                       sh_string_str(res),
    299                                       (int)sh_string_len(res),
    300                                       0, /* start at offset 0 in the subject */
    301                                       0,
    302                                       ovector, SH_MINIBUF,
    303                                       wspace, SH_MINIBUF);
    304 #else
    305               int val = pcre_exec(mkeep->rule, NULL,
    306                                   sh_string_str(res),
    307                                   (int)sh_string_len(res),
    308                                   0, /* start at offset 0 in the subject */
    309                                   0,
    310                                   NULL, 0);
    311               val = (val >= 0) ? 1 : val;                             
    312 #endif
    313 
    314               if (val >= 0)
     286              pcre2_match_data * match_data = pcre2_match_data_create_from_pattern(mkeep->rule, NULL);
     287             
     288              int val = pcre2_match(mkeep->rule,
     289                                    (PCRE2_SPTR8) sh_string_str(res), (int)sh_string_len(res), 0,
     290                                    0, match_data, NULL);
     291             
     292              pcre2_match_data_free(match_data);
     293             
     294              if (val > 0)
    315295                {
    316296                  sh_string * alias;
  • 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;
  • trunk/src/sh_log_parse_apache.c

    r541 r588  
    2828#define FIL__  _("sh_log_parse_apache.c")
    2929
    30 /* Debian/Ubuntu: libpcre3-dev */
    31 #ifdef HAVE_PCRE_PCRE_H
    32 #include <pcre/pcre.h>
     30/* Debian/Ubuntu: libpcre2-dev */
     31#define PCRE2_CODE_UNIT_WIDTH 8
     32#ifdef HAVE_PCRE2_PCRE2_H
     33#include <pcre2/pcre2.h>
    3334#else
    34 #include <pcre.h>
     35#include <pcre2.h>
    3536#endif
    3637
     
    4445
    4546struct sh_fileinfo_apache {
    46   pcre * line_regex;
    47   int * line_ovector;         /* captured substrings     */
    48   int    line_ovecnum;         /* how many captured       */
     47  pcre2_code * line_regex;
     48  PCRE2_SIZE * line_ovector;         /* captured substrings     */
     49  int          line_ovecnum;         /* how many captured       */
    4950 
    5051  int    pos_host;
     
    8283  volatile int          p_time = -1;
    8384  char                * f_time = NULL;
    84   const char * error;
    85   int          erroffset;
     85  int          error;
     86  size_t       erroffset;
    8687 
    8788  /* Take the address to keep gcc from putting them into registers.
     
    261262
    262263  result = SH_ALLOC(sizeof(struct sh_fileinfo_apache));
    263   result->line_regex = pcre_compile(sh_string_str(re_string), 0,
     264  result->line_regex = pcre2_compile((PCRE2_SPTR8)sh_string_str(re_string), sh_string_len(re_string), 0,
    264265                                    &error, &erroffset, NULL);
    265266  if (!(result->line_regex))
     
    285286  sh_string_destroy(&re_string);
    286287
    287   result->line_ovector  = SH_ALLOC(sizeof(int) * (nfields+1) * 3);
     288  result->line_ovector  = NULL;
    288289  result->line_ovecnum  = nfields;
    289290  result->pos_host      = p_host;
     
    304305  char         tstr[128];
    305306  char         sstr[128];
    306   const char * hstr;
     307  char      * hstr;
    307308  int          res;
    308   const char **hstr_addr = (const char **) &hstr;
     309  unsigned char **hstr_addr = (unsigned char **) &hstr;
     310  size_t       hstr_len;
    309311
    310312  struct sh_fileinfo_apache * info = (struct sh_fileinfo_apache *) fileinfo;
    311313
     314  pcre2_match_data * match_data = NULL;
     315 
    312316  if (sh_string_len(logline) > 0 && flag_err_debug == S_TRUE)
    313317    {
     
    324328    }
    325329
    326   res = pcre_exec(info->line_regex, NULL,
    327                   sh_string_str(logline), (int)sh_string_len(logline), 0,
    328                   0, info->line_ovector, (3*(1+info->line_ovecnum)));
    329 
    330   if (res == (1+info->line_ovecnum))
     330  match_data = pcre2_match_data_create_from_pattern(info->line_regex, NULL);
     331 
     332  res = pcre2_match(info->line_regex,
     333                    (PCRE2_SPTR8)sh_string_str(logline), (int)sh_string_len(logline), 0,
     334                    0, match_data, NULL);
     335
     336  if (res == 1+info->line_ovecnum) /* successful match */
    331337    {
    332338      struct sh_logrecord * record;
    333339      time_t timestamp = 0;
    334 
     340      size_t size;
     341
     342      info->line_ovector = pcre2_get_ovector_pointer(match_data);
     343     
    335344      if (info->pos_time > 0)
    336345        {
    337           res = pcre_copy_substring(sh_string_str(logline),
    338                                     info->line_ovector, res,
    339                                     info->pos_time, tstr, sizeof(tstr));
    340           if (res <= 0)
     346          size = sizeof(tstr);
     347          res = pcre2_substring_copy_bynumber(match_data,  info->pos_time,
     348                                              (PCRE2_UCHAR8 *)tstr, &size);
     349          if (res != 0)
    341350            goto corrupt;
    342351        }
    343352      else
    344353        {
    345           res = 0;
     354          res = -1;
    346355          timestamp = 0;
    347356          info->format_time = sh_util_strdup(_("%d/%b/%Y:%T"));
     
    349358        }
    350359
    351       if (res > 0)
     360      if (res == 0)
    352361        {
    353362          struct tm btime;
     
    371380      if (info->pos_status > 0)
    372381        {
    373           res = pcre_copy_substring(sh_string_str(logline),
    374                                     info->line_ovector, res,
    375                                     info->pos_status, sstr, sizeof(sstr));
    376           if (res <= 0)
     382          size = sizeof(sstr);
     383          res = pcre2_substring_copy_bynumber(match_data,  info->pos_status,
     384                                              (PCRE2_UCHAR8 *)sstr, &size);
     385          if (res != 0)
    377386            goto corrupt;
    378387        }
     
    384393      if (info->pos_host > 0)
    385394        {
    386           res = pcre_get_substring(sh_string_str(logline),
    387                                    info->line_ovector, res,
    388                                    info->pos_host, hstr_addr);
    389           if (res <= 0)
     395          res = pcre2_substring_get_bynumber(match_data, info->pos_host,
     396                                             hstr_addr, &hstr_len);
     397          if (res != 0)
    390398            goto corrupt;
    391399        }
     
    401409
    402410      if (hstr)
    403         record->host = sh_string_new_from_lchar(hstr, strlen(hstr));
     411        record->host = sh_string_new_from_lchar(hstr, hstr_len);
    404412      else
    405413        record->host = sh_string_new_from_lchar(sh.host.name, strlen(sh.host.name));
     
    409417      record->pid       = PID_INVALID;
    410418
    411       pcre_free_substring(hstr);
     419      /* does nothing if hstr == NULL */
     420      pcre2_substring_free((PCRE2_UCHAR8 *)hstr);
     421
     422      pcre2_match_data_free(match_data);
    412423      return record;
    413424    }
     
    415426    {
    416427      char msg[128];
    417       sl_snprintf(msg, sizeof(msg), _("Incorrect number of captured subexpressions: %d vs %d"),
    418                   res, info->line_ovecnum);
     428      sl_snprintf(msg, sizeof(msg), _("Matching error: %d"), res);
    419429     
    420430      SH_MUTEX_LOCK(mutex_thread_nolog);
     
    440450    sh_string_destroy(&msg);
    441451  }
     452  pcre2_match_data_free(match_data);
    442453  return NULL;
    443454}
  • trunk/src/sh_log_parse_generic.c

    r481 r588  
    2222#include <time.h>
    2323
    24 /* Debian/Ubuntu: libpcre3-dev */
    25 #ifdef HAVE_PCRE_PCRE_H
    26 #include <pcre/pcre.h>
     24/* Debian/Ubuntu: libpcre2-dev */
     25#define PCRE2_CODE_UNIT_WIDTH 8
     26#ifdef HAVE_PCRE2_PCRE2_H
     27#include <pcre2/pcre2.h>
    2728#else
    28 #include <pcre.h>
     29#include <pcre2.h>
    2930#endif
    3031
     
    3435
    3536struct sh_fileinfo_generic {
    36   pcre * line_regex;
    37   int  * line_ovector;         /* captured substrings     */
    38   int    line_ovecnum;         /* how many captured       */
     37  pcre2_code        * line_regex;
     38  pcre2_match_data  * line_match_data;      /* captured substrings     */
     39  int                 line_ovecnum;         /* how many captured       */
    3940 
    4041  int    pos_host;
  • trunk/src/sh_log_parse_pacct.c

    r481 r588  
    314314    }
    315315
     316  sh_dummy_294_record = NULL;
     317 
    316318  p = strchr(sh_string_str(logline), ':');
    317319
  • trunk/src/sh_string.c

    r587 r588  
    546546 * of field, offset of first char after field (this is how
    547547 * the pcre library does it).
    548  */ 
     548 */
     549#define IS_PCRE2_UNSET           (~(size_t)0)
     550
    549551sh_string * sh_string_replace(const sh_string * s,
    550                               const int * ovector, int ovecnum,
     552                              const size_t * ovector, int ovecnum,
    551553                              const char * replacement, size_t rlen)
    552554{
     
    604606  for (i = 0; i < ovecnum; ++i)
    605607    {
    606       if (ovector[2*i] >= 0)
     608      if (ovector[2*i] != IS_PCRE2_UNSET)
    607609        {
    608610          curr = 2*i;
     
    611613    }
    612614 
    613   if (r && ovecnum > 0 && ovector[curr] >= 0)
     615  if (r && ovecnum > 0 && ovector[curr] != IS_PCRE2_UNSET)
    614616    {
    615617      r->len = 0; r->str[0] = '\0'; p = r->str;
     
    617619      /* First part, until start of first replacement
    618620       */
    619       if (r->siz > (unsigned int)ovector[curr]) {
    620         memcpy(p, s->str, (size_t)ovector[curr]);
     621      if (r->siz > ovector[curr]) {
     622        memcpy(p, s->str, ovector[curr]);
    621623        p += ovector[curr];
    622624        r->len += ovector[curr];
     
    633635      for (i = 1; i < ovecnum; ++i)
    634636        {
    635           if (ovector[2*i] < 0)
     637          if (ovector[2*i] == IS_PCRE2_UNSET)
    636638            continue;
    637639
     
    704706  size_t lengths[16];
    705707  unsigned int iarr;
    706   int ovector[16];
     708  size_t ovector[16];
    707709  int ovecnum;
    708710
Note: See TracChangeset for help on using the changeset viewer.