"Fossies" - the Fresh Open Source Software Archive

Member "leafnode-1.12.0/filterutil.c" (28 Dec 2021, 3030 Bytes) of package /linux/misc/leafnode-1.12.0.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "filterutil.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.11.12_vs_1.12.0.

    1 /*
    2 filterutil.c -- read filter file and do filtering of messages
    3 
    4 Written by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>.
    5 Copyright 1998.
    6 
    7 Modified and Copyright of modifications 2001-2021 by Matthias Andree
    8 
    9 See file COPYING for restrictions on the use of this software.
   10 */
   11 
   12 #include "leafnode.h"
   13 #include <sys/types.h>
   14 #include <ctype.h>
   15 #include <stdio.h>
   16 #include <stdlib.h>
   17 #include <string.h>
   18 #include <syslog.h>
   19 #include "ln_log.h"
   20 
   21 struct filterlist {
   22     struct filterlist *next;
   23     char *cleartext;
   24     pcre2_code_8 *re;
   25 };
   26 
   27 static struct filterlist *filter;
   28 
   29 /*
   30  * read filters into memory. Filters are just plain regexps
   31  */
   32 void
   33 readfilter(char *fifi)
   34 {
   35     FILE *ff;
   36     char *l;
   37     int regex_errcode;
   38     size_t regex_errpos;
   39     struct filterlist *f, *oldf;
   40 
   41     if (fifi == NULL || !strlen(fifi))
   42     return;
   43     filter = NULL;
   44     ff = fopen(fifi, "r");
   45     if (!ff) {
   46     int e = errno;
   47     syslog(LOG_ERR, "Unable to open filterfile %s: %m", fifi);
   48     printf("Unable to open filterfile %s: %s\n", fifi, strerror(e));
   49     return;
   50     }
   51     oldf = NULL;
   52     debug = 0;
   53     while ((l = getaline(ff)) != NULL) {
   54     if (*l == '#' || *l == '\0')
   55         continue;
   56     f = (struct filterlist *)critmalloc(sizeof(struct filterlist),
   57                         "Allocating filterlist space");
   58     if (NULL == (f->re = pcre2_compile_8((u_char *)l, PCRE2_ZERO_TERMINATED, PCRE2_MULTILINE,
   59                        &regex_errcode, &regex_errpos, NULL)))
   60     {
   61         unsigned char buf[SIZE_lineout];
   62         int len = pcre2_get_error_message_8(regex_errcode, buf, sizeof(buf));
   63         syslog(LOG_ERR, "Invalid filter pattern %s: %s%s", l, buf,
   64            len == PCRE2_ERROR_NOMEMORY ? "[...]" : "");
   65         printf("Invalid filter pattern %s %s%s", l, buf,
   66                len == PCRE2_ERROR_NOMEMORY ? "[...]" : "");
   67         free(f);
   68     } else {
   69         f->next = NULL;
   70         f->cleartext = critstrdup(l, "readfilter");
   71         if (!filter)
   72         filter = f;
   73         else
   74         oldf->next = f;
   75         oldf = f;
   76     }
   77     }
   78     debug = debugmode;
   79     fclose(ff);
   80 }
   81 
   82 /*
   83  * read and filter headers.
   84  * Return true if headers matched pattern, false if not
   85  */
   86 int
   87 dofilter(unsigned char *h)
   88 {
   89     struct filterlist *f = filter;
   90     int match;
   91     pcre2_match_data_8 *match_data = pcre2_match_data_create_8(1, NULL);
   92     if (NULL == match_data) {
   93         ln_log(LNLOG_SERR, LNLOG_CTOP, "filterutil.c::dofilter: out of memory allocating match_data");
   94         return -1;
   95     }
   96 
   97     match = PCRE2_ERROR_NOMATCH;
   98     while (f && 0 > match) {
   99     match = pcre2_match_8(f->re, h, PCRE2_ZERO_TERMINATED, /*offset*/ 0, /*options*/ 0, match_data, /*ctx*/ NULL);
  100     if (debugmode > 1) {
  101         syslog(LOG_DEBUG, "(\"%s\" =~ /%s/) = %d", h, f->cleartext, match);
  102     }
  103     if (match >= 0) {
  104         pcre2_match_data_free_8(match_data);
  105         return TRUE;
  106     }
  107     f = f->next;
  108     }
  109     pcre2_match_data_free_8(match_data);
  110     return FALSE;       /* no match, or internal error */
  111 }
  112 
  113 void freefilter(void) {
  114     struct filterlist *f = filter, *g;
  115 
  116     while(f) {
  117     free(f->cleartext);
  118     pcre2_code_free_8(f->re);
  119     g = f->next;
  120     free(f);
  121     f = g;
  122     }
  123     filter = NULL;
  124 }