leafnode  1.12.0
About: Leafnode is a store & forward NNTP proxy for small (dialup) sites.
  Fossies Dox: leafnode-1.12.0.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

filterutil.c
Go to the documentation of this file.
1/*
2filterutil.c -- read filter file and do filtering of messages
3
4Written by Cornelius Krasel <krasel@wpxx02.toxi.uni-wuerzburg.de>.
5Copyright 1998.
6
7Modified and Copyright of modifications 2001-2021 by Matthias Andree
8
9See 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
21struct filterlist {
23 char *cleartext;
24 pcre2_code_8 *re;
25};
26
27static struct filterlist *filter;
28
29/*
30 * read filters into memory. Filters are just plain regexps
31 */
32void
33readfilter(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 }
79 fclose(ff);
80}
81
82/*
83 * read and filter headers.
84 * Return true if headers matched pattern, false if not
85 */
86int
87dofilter(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
113void 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}
int debug
Definition: applyfilter.c:30
int debugmode
Definition: configutil.c:67
char * critstrdup(const char *source, const char *message)
Definition: critmem.c:92
char * critmalloc(size_t size, const char *message)
Definition: critmem.c:61
void readfilter(char *fifi)
Definition: filterutil.c:33
static struct filterlist * filter
Definition: filterutil.c:27
void freefilter(void)
Definition: filterutil.c:113
int dofilter(unsigned char *h)
Definition: filterutil.c:87
char * getaline(FILE *f)
Definition: getaline.c:84
#define TRUE
Definition: leafnode.h:29
#define FALSE
Definition: leafnode.h:32
#define SIZE_lineout
Definition: leafnode.h:279
void ln_log(int sev, int ctx, const char *format,...)
Definition: ln_log.c:103
#define LNLOG_SERR
Definition: ln_log.h:13
#define LNLOG_CTOP
Definition: ln_log.h:22
#define len
Definition: mastring.c:31
pcre2_code_8 * re
Definition: filterutil.c:24
char * cleartext
Definition: filterutil.c:23
struct filterlist * next
Definition: filterutil.c:22