Stackdb
Stackdb is a stackable, multi-target and -level source debugger and memory forensics library.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rfilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 2013 The University of Utah
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef __RFILTER_H__
20 #define __RFILTER_H__
21 
22 #include <stdarg.h>
23 #include <sys/types.h>
24 #include <regex.h>
25 
26 #include "log.h"
27 #include "alist.h"
28 
29 #define RF_ACCEPT 1
30 #define RF_REJECT 0
31 
32 /*
33  * Each rfilter has a default state where nothing matches; if the
34  * default state is 1 and no filter entries match, we accept. If the
35  * default state is 0 and no filter entries match, we reject. If a
36  * filter matches, we do whatever the individual filter says. A NULL
37  * rfilter always rejects.
38  */
39 struct rfilter {
40  int defaccept;
41  struct array_list list;
42 };
43 
44 struct rfilter_entry {
45  regex_t *regex;
46  int accept;
47  void *data;
48 };
49 
50 /*
51  * Returns 1 if there was a match; 0 if not; sets *@accept to whatever
52  * the match (or default filter policy) told us to do.
53  */
54 static inline int rfilter_check(struct rfilter *rf,const char *str,
55  int *accept,struct rfilter_entry **rfmatch) {
56  int i;
57  struct rfilter_entry *rfe;
58 
59  if (!rf) {
60  if (accept)
61  *accept = RF_ACCEPT;
62  return 0;
63  }
64 
65  for (i = 0; i < array_list_len(&rf->list); ++i) {
66  rfe = (struct rfilter_entry *)array_list_item(&rf->list,i);
67  if (regexec(rfe->regex,str,0,NULL,0) == 0) {
68  if (accept)
69  *accept = rfe->accept;
70  if (rfmatch)
71  *rfmatch = rfe;
72  return 1;
73  }
74  }
75 
76  if (accept)
77  *accept = rf->defaccept;
78  return 0;
79 }
80 
81 static inline int rfilter_add(struct rfilter *rf,
82  char *rstr,int accept,void *data) {
83  struct rfilter_entry *rfe;
84  regex_t *regex;
85  int rc;
86  char errbuf[64];
87 
88  regex = (regex_t *)malloc(sizeof(*regex));
89  memset(regex,0,sizeof(*regex));
90  if ((rc = regcomp(regex,rstr,REG_EXTENDED | REG_NOSUB))) {
91  regerror(rc,regex,errbuf,64);
92  verror("bad regex '%s': %s\n",rstr,errbuf);
93  regfree(regex);
94  free(regex);
95  return -1;
96  }
97 
98  rfe = (struct rfilter_entry *)malloc(sizeof(*rfe));
99  memset(rfe,0,sizeof(*rfe));
100  rfe->regex = regex;
101  rfe->accept = accept;
102  rfe->data = data;
103 
104  array_list_add(&rf->list,rfe);
105 
106  return 0;
107 }
108 
109 static inline struct rfilter *rfilter_create(int defaccept) {
110  struct rfilter *rf = (struct rfilter *)malloc(sizeof(*rf));
111  memset(rf,0,sizeof(*rf));
112  rf->defaccept = defaccept;
113  array_list_init(&rf->list,0);
114 
115  return rf;
116 }
117 
118 static inline void rfilter_free(struct rfilter *rf) {
119  int i;
120  struct rfilter_entry *rfe;
121 
122  for (i = 0; i < array_list_len(&rf->list); ++i) {
123  rfe = (struct rfilter_entry *)array_list_item(&rf->list,i);
124  regfree(rfe->regex);
125  if (rfe->data)
126  free(rfe->data);
127  free(rfe);
128  }
129  free(rf->list.list);
130  free(rf);
131 
132  return;
133 }
134 
135 static inline struct rfilter *rfilter_create_parse(char *fstr) {
136  struct rfilter *rf;
137  int pol;
138  char *token = NULL;
139  char *saveptr;
140 
141  while (*fstr == ' ' || *fstr == '\t')
142  ++fstr;
143 
144  if (*fstr == '.' || *fstr == '*' || *fstr == '\0')
145  return NULL;
146 
147  /* Check for default policy. */
148  if ((*fstr == '1' || *fstr == 'A' || *fstr == 'a')
149  && *(fstr+1) == ':' && *(fstr+2) == ':') {
150  pol = 1;
151  fstr += 3;
152  }
153  else if ((*fstr == '1' || *fstr == 'A' || *fstr == 'a')
154  && *(fstr+1) == '\0') {
155  pol = 1;
156  fstr += 1;
157  }
158  else if ((*fstr == '0' || *fstr == 'R' || *fstr == 'r')
159  && *(fstr+1) == ':' && *(fstr+2) == ':') {
160  pol = 0;
161  fstr += 3;
162  }
163  else if ((*fstr == '0' || *fstr == 'R' || *fstr == 'r')
164  && *(fstr+1) == '\0') {
165  pol = 0;
166  fstr += 1;
167  }
168  else {
169  pol = 0;
170  }
171 
172  rf = rfilter_create(pol);
173 
174  while ((token = strtok_r((!token)?fstr:NULL,";",&saveptr))) {
175  vdebug(7,LA_LIB,LF_RFILTER,"token = '%s'\n",token);
176 
177  if ((*token == '1' || *token == 'A' || *token == 'a')
178  && *(token+1) == ':') {
179  pol = 1;
180  token += 2;
181  }
182  else if ((*token == '0' || *token == 'R' || *token == 'r')
183  && *(token+1) == ':') {
184  pol = 0;
185  token += 2;
186  }
187  else
188  pol = 0;
189 
190  if (rfilter_add(rf,token,pol,NULL))
191  goto errout;
192  }
193 
194  return rf;
195 
196  errout:
197  rfilter_free(rf);
198  return NULL;
199 }
200 
201 static inline struct rfilter *rfilter_create_simple(int defaccept,
202  char *rstr,...) {
203  va_list ap;
204  struct rfilter *rf;
205 
206  rf = rfilter_create(defaccept);
207 
208  if (rfilter_add(rf,rstr,0,NULL)) {
209  free(rf->list.list);
210  return NULL;
211  }
212 
213  va_start(ap,rstr);
214  while ((rstr = va_arg(ap,char *))) {
215  if (rfilter_add(rf,va_arg(ap,char *),0,NULL))
216  goto errout;
217  }
218  va_end(ap);
219 
220  return rf;
221 
222  errout:
223  rfilter_free(rf);
224  return NULL;
225 }
226 
227 #endif
#define RF_ACCEPT
Definition: rfilter.h:29
Definition: log.h:99
regex_t * regex
Definition: rfilter.h:45
static uint64_t unsigned int i
#define verror(format,...)
Definition: log.h:30
void ** list
Definition: alist.h:34
void free(void *ptr)
Definition: debugserver.c:207
struct array_list list
Definition: rfilter.h:41
Definition: log.h:68
Definition: rfilter.h:44
#define vdebug(devel, areas, flags, format,...)
Definition: log.h:302
int defaccept
Definition: rfilter.h:40
void * data
Definition: rfilter.h:47
int accept
Definition: rfilter.h:46
void * malloc(size_t size)
Definition: debugserver.c:214