"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "filterutil.c" between
leafnode-1.11.12.tar.bz2 and leafnode-1.12.0.tar.xz

About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.

filterutil.c  (leafnode-1.11.12.tar.bz2):filterutil.c  (leafnode-1.12.0.tar.xz)
/* /*
libutil -- read filter file and do filtering of messages filterutil.c -- read filter file and do filtering of messages
Written by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>. Written by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>.
Copyright 1998. Copyright 1998.
Modified and Copyright of modifications 2001-2021 by Matthias Andree
See file COPYING for restrictions on the use of this software. See file COPYING for restrictions on the use of this software.
*/ */
#include "leafnode.h" #include "leafnode.h"
#include <sys/types.h> #include <sys/types.h>
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <pcre.h> #include "ln_log.h"
struct filterlist { struct filterlist {
struct filterlist *next; struct filterlist *next;
char *cleartext; char *cleartext;
pcre *expr; pcre2_code_8 *re;
}; };
static struct filterlist *filter; static struct filterlist *filter;
/* /*
* read filters into memory. Filters are just plain regexps * read filters into memory. Filters are just plain regexps
*/ */
void void
readfilter(char *fifi) readfilter(char *fifi)
{ {
FILE *ff; FILE *ff;
char *l; char *l;
const char *regex_errmsg; int regex_errcode;
int regex_errpos; size_t regex_errpos;
struct filterlist *f, *oldf; struct filterlist *f, *oldf;
if (fifi == NULL || !strlen(fifi)) if (fifi == NULL || !strlen(fifi))
return; return;
filter = NULL; filter = NULL;
ff = fopen(fifi, "r"); ff = fopen(fifi, "r");
if (!ff) { if (!ff) {
int e = errno;
syslog(LOG_ERR, "Unable to open filterfile %s: %m", fifi); syslog(LOG_ERR, "Unable to open filterfile %s: %m", fifi);
printf("Unable to open filterfile %s\n", fifi); printf("Unable to open filterfile %s: %s\n", fifi, strerror(e));
return; return;
} }
oldf = NULL; oldf = NULL;
debug = 0; debug = 0;
while ((l = getaline(ff)) != NULL) { while ((l = getaline(ff)) != NULL) {
if (*l == '#' || *l == '\0') if (*l == '#' || *l == '\0')
continue; continue;
f = (struct filterlist *)critmalloc(sizeof(struct filterlist), f = (struct filterlist *)critmalloc(sizeof(struct filterlist),
"Allocating filterlist space"); "Allocating filterlist space");
if ((f->expr = pcre_compile(l, PCRE_MULTILINE, if (NULL == (f->re = pcre2_compile_8((u_char *)l, PCRE2_ZERO_TERMINATED,
&regex_errmsg, &regex_errpos, PCRE2_MULTILINE,
NULL)) == NULL) { &regex_errcode, &regex_errpos, NULL)))
syslog(LOG_ERR, "Invalid filter pattern %s: %s", l, regex_errmsg); {
printf("Invalid filter pattern %s %s", l, regex_errmsg); unsigned char buf[SIZE_lineout];
int len = pcre2_get_error_message_8(regex_errcode, buf, sizeof(buf));
syslog(LOG_ERR, "Invalid filter pattern %s: %s%s", l, buf,
len == PCRE2_ERROR_NOMEMORY ? "[...]" : "");
printf("Invalid filter pattern %s %s%s", l, buf,
len == PCRE2_ERROR_NOMEMORY ? "[...]" : "");
free(f); free(f);
} else { } else {
f->next = NULL; f->next = NULL;
f->cleartext = critstrdup(l, "readfilter"); f->cleartext = critstrdup(l, "readfilter");
if (!filter) if (!filter)
filter = f; filter = f;
else else
oldf->next = f; oldf->next = f;
oldf = f; oldf = f;
} }
} }
debug = debugmode; debug = debugmode;
fclose(ff); fclose(ff);
} }
/* /*
* read and filter headers. * read and filter headers.
* Return true if headers matched pattern, false if not * Return true if headers matched pattern, false if not
*/ */
int int
dofilter(char *h) dofilter(unsigned char *h)
{ {
struct filterlist *f = filter; struct filterlist *f = filter;
int match; int match;
pcre2_match_data_8 *match_data = pcre2_match_data_create_8(1, NULL);
if (NULL == match_data) {
ln_log(LNLOG_SERR, LNLOG_CTOP, "filterutil.c::dofilter: out of memory
allocating match_data");
return -1;
}
match = -1; match = PCRE2_ERROR_NOMATCH;
while (f && match == -1) { while (f && 0 > match) {
match = pcre_exec(f->expr, NULL, h, strlen(h), 0, 0, NULL, 0); match = pcre2_match_8(f->re, h, PCRE2_ZERO_TERMINATED, /*offset*/ 0, /*op
tions*/ 0, match_data, /*ctx*/ NULL);
if (debugmode > 1) { if (debugmode > 1) {
syslog(LOG_DEBUG, "(\"%s\" =~ /%s/) = %d", h, f->cleartext, match); syslog(LOG_DEBUG, "(\"%s\" =~ /%s/) = %d", h, f->cleartext, match);
} }
if (match >= 0) { if (match >= 0) {
pcre2_match_data_free_8(match_data);
return TRUE; return TRUE;
} }
f = f->next; f = f->next;
} }
pcre2_match_data_free_8(match_data);
return FALSE; /* no match, or internal error */ return FALSE; /* no match, or internal error */
} }
void freefilter(void) { void freefilter(void) {
struct filterlist *f = filter, *g; struct filterlist *f = filter, *g;
while(f) { while(f) {
free(f->cleartext); free(f->cleartext);
pcre_free(f->expr); pcre2_code_free_8(f->re);
g = f->next; g = f->next;
free(f); free(f);
f = g; f = g;
} }
filter = NULL; filter = NULL;
} }
 End of changes. 14 change blocks. 
16 lines changed or deleted 33 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)