Massively cleaned up #include's, so they're in a consistent
[freeradius.git] / src / modules / rlm_attr_filter / rlm_attr_filter.c
1 /*
2  * rlm_attr_filter.c  - Filter A/V Pairs received back from proxy reqs
3  *                      before sending reply to the NAS/Server that sent
4  *                      it to us.
5  *
6  * Version:      $Id$
7  *
8  *   This program is is free software; you can redistribute it and/or modify
9  *   it under the terms of the GNU General Public License, version 2 if the
10  *   License as published by the Free Software Foundation.
11  *
12  *   This program is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with this program; if not, write to the Free Software
19  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * Copyright (C) 2001,2006 The FreeRADIUS server project
22  * Copyright (C) 2001 Chris Parker <cparker@starnetusa.net>
23  */
24
25 #include        <freeradius-devel/ident.h>
26 RCSID("$Id$")
27
28 #include        <freeradius-devel/radiusd.h>
29 #include        <freeradius-devel/modules.h>
30 #include        <freeradius-devel/rad_assert.h>
31
32 #include        <sys/stat.h>
33
34 #include        <ctype.h>
35 #include        <fcntl.h>
36 #include        <limits.h>
37
38
39 /*
40  *      Define a structure with the module configuration, so it can
41  *      be used as the instance handle.
42  */
43 struct attr_filter_instance {
44         char *attrsfile;
45         char *key;
46         PAIR_LIST *attrs;
47 };
48
49 static const CONF_PARSER module_config[] = {
50         { "attrsfile",     PW_TYPE_FILENAME,
51           offsetof(struct attr_filter_instance,attrsfile), NULL, "${raddbdir}/attrs" },
52         { "key",     PW_TYPE_STRING_PTR,
53           offsetof(struct attr_filter_instance,key), NULL, "%{Realm}" },
54         { NULL, -1, 0, NULL, NULL }
55 };
56
57 static void check_pair(VALUE_PAIR *check_item, VALUE_PAIR *reply_item,
58                       int *pass, int *fail)
59 {
60         int compare;
61
62         if (check_item->operator == T_OP_SET) return;
63
64         compare = paircmp(check_item, reply_item);
65         if (compare == 1) {
66                 ++*(pass);
67         } else {
68                 ++*(fail);
69         }
70
71         return;
72 }
73
74 /*
75  *      Copy the specified attribute to the specified list
76  */
77 static int mypairappend(VALUE_PAIR *item, VALUE_PAIR **to)
78 {
79         VALUE_PAIR *tmp;
80         tmp = paircreate(item->attribute, item->type);
81         if (!tmp) {
82                 radlog(L_ERR|L_CONS, "no memory");
83                 return -1;
84         }
85         
86         /*
87          *      Copy EVERYTHING.
88          */
89         memcpy(tmp, item, sizeof(*tmp));
90         tmp->next = NULL;
91         *to = tmp;
92
93         return 0;
94 }
95
96 static int getattrsfile(const char *filename, PAIR_LIST **pair_list)
97 {
98         int rcode;
99         PAIR_LIST *attrs = NULL;
100         PAIR_LIST *entry;
101         VALUE_PAIR *vp;
102
103         rcode = pairlist_read(filename, &attrs, 1);
104         if (rcode < 0) {
105                 return -1;
106         }
107
108         /*
109          * Walk through the 'attrs' file list.
110          */
111
112         entry = attrs;
113         while (entry) {
114
115                 entry->check = entry->reply;
116                 entry->reply = NULL;
117
118                 for (vp = entry->check; vp != NULL; vp = vp->next) {
119
120                     /*
121                      * If it's NOT a vendor attribute,
122                      * and it's NOT a wire protocol
123                      * and we ignore Fall-Through,
124                      * then bitch about it, giving a good warning message.
125                      */
126                     if (!(vp->attribute & ~0xffff) &&
127                          (vp->attribute > 0xff) &&
128                          (vp->attribute > 1000)) {
129                         log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
130                                   "\tfound in filter list for realm \"%s\".\n",
131                                   filename, entry->lineno, vp->name,
132                                   entry->name);
133                     }
134                 }
135
136                 entry = entry->next;
137         }
138
139         *pair_list = attrs;
140         return 0;
141 }
142
143
144 /*
145  *      Clean up.
146  */
147 static int attr_filter_detach(void *instance)
148 {
149         struct attr_filter_instance *inst = instance;
150         pairlist_free(&inst->attrs);
151         free(inst);
152         return 0;
153 }
154
155
156 /*
157  *      (Re-)read the "attrs" file into memory.
158  */
159 static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
160 {
161         struct attr_filter_instance *inst;
162         int rcode;
163
164         inst = rad_malloc(sizeof *inst);
165         if (!inst) {
166                 return -1;
167         }
168         memset(inst, 0, sizeof(*inst));
169
170         if (cf_section_parse(conf, inst, module_config) < 0) {
171                 attr_filter_detach(inst);
172                 return -1;
173         }
174
175         rcode = getattrsfile(inst->attrsfile, &inst->attrs);
176         if (rcode != 0) {
177                 radlog(L_ERR|L_CONS, "Errors reading %s", inst->attrsfile);
178                 attr_filter_detach(inst);
179                 return -1;
180         }
181         *instance = inst;
182         return 0;
183 }
184
185
186 /*
187  *      Common attr_filter checks
188  */
189 static int attr_filter_common(void *instance, REQUEST *request,
190                               VALUE_PAIR **input)
191 {
192         struct attr_filter_instance *inst = instance;
193         VALUE_PAIR      *vp;
194         VALUE_PAIR      *output = NULL;
195         VALUE_PAIR      **output_tail;
196         VALUE_PAIR      *check_item;
197         PAIR_LIST       *pl;
198         int             found = 0;
199         int             pass, fail = 0;
200         char            *keyname = NULL;
201         char            buffer[256];
202
203         if (!inst->key) {
204                 VALUE_PAIR      *namepair;
205
206                 namepair = pairfind(request->packet->vps, PW_REALM);
207                 if (!namepair) {
208                         return (RLM_MODULE_NOOP);
209                 }
210                 keyname = namepair->vp_strvalue;
211         } else {
212                 int len;
213
214                 len = radius_xlat(buffer, sizeof(buffer), inst->key,
215                                   request, NULL);
216                 if (!len) {
217                         return RLM_MODULE_NOOP;
218                 }
219                 keyname = buffer;
220         }
221
222         output_tail = &output;
223
224         /*
225          *      Find the attr_filter profile entry for the entry.
226          */
227         for (pl = inst->attrs; pl; pl = pl->next) {
228                 int fall_through = 0;
229
230                 /*
231                  *  If the current entry is NOT a default,
232                  *  AND the realm does NOT match the current entry,
233                  *  then skip to the next entry.
234                  */
235                 if ((strcmp(pl->name, "DEFAULT") != 0) &&
236                     (strcmp(keyname, pl->name) != 0))  {
237                     continue;
238                 }
239
240                 DEBUG2(" attr_filter: Matched entry %s at line %d", pl->name,
241                        pl->lineno);
242                 found = 1;
243
244                 for (check_item = pl->check;
245                      check_item != NULL;
246                      check_item = check_item->next) {
247                         if (check_item->attribute == PW_FALL_THROUGH) {
248                                 fall_through = 1;
249                                 continue;
250                         }
251
252                         /*
253                          *    If it is a SET operator, add the attribute to
254                          *    the output list without checking it.
255                          */
256                         if (check_item->operator == T_OP_SET ) {
257                                 if (mypairappend(check_item, output_tail) < 0) {
258                                         pairfree(&output);
259                                         return RLM_MODULE_FAIL;
260                                 }
261                                 output_tail = &((*output_tail)->next);
262                         }
263                 }
264
265                 /*
266                  *      Iterate through the input items, comparing
267                  *      each item to every rule, then moving it to the
268                  *      output list only if it matches all rules
269                  *      for that attribute.  IE, Idle-Timeout is moved
270                  *      only if it matches all rules that describe an
271                  *      Idle-Timeout.
272                  */
273                 for (vp = *input; vp != NULL; vp = vp->next ) {
274                         /* reset the pass,fail vars for each reply item */
275                         pass = fail = 0;
276                         
277                         /*
278                          *      reset the check_item pointer to
279                          *      beginning of the list
280                          */
281                         for (check_item = pl->check;
282                              check_item != NULL;
283                              check_item = check_item->next) {
284                                 /*
285                                  *      Vendor-Specific is special, and
286                                  *      matches any VSA if the comparison
287                                  *      is always true.
288                                  */
289                                 if ((check_item->attribute == PW_VENDOR_SPECIFIC) &&
290                                     (VENDOR(vp->attribute) != 0) &&
291                                     (check_item->operator == T_OP_CMP_TRUE)) {
292                                         pass++;
293                                         continue;
294                                 }
295
296                                 if (vp->attribute == check_item->attribute) {
297                                         check_pair(check_item, vp,
298                                                    &pass, &fail);
299                                 }
300                         }
301                         
302                         /* only move attribute if it passed all rules */
303                         if (fail == 0 && pass > 0) {
304                                 if (mypairappend(vp, output_tail) < 0) {
305                                         pairfree(&output);
306                                         return RLM_MODULE_FAIL;
307                                 }
308                                 output_tail = &((*output_tail)->next);
309                         }
310                 }
311                 
312                 /* If we shouldn't fall through, break */
313                 if (!fall_through)
314                         break;
315         }
316
317         /*
318          *      No entry matched.  We didn't do anything.
319          */
320         if (!found) {
321                 rad_assert(output == NULL);
322                 return RLM_MODULE_NOOP;
323         }
324
325         pairfree(input);
326         *input = output;
327
328         return RLM_MODULE_UPDATED;
329 }
330
331 static int attr_filter_accounting(void *instance, REQUEST *request)
332 {
333         return attr_filter_common(instance, request, &request->packet->vps);
334 }
335
336 static int attr_filter_preproxy(void *instance, REQUEST *request)
337 {
338         return attr_filter_common(instance, request, &request->proxy->vps);
339 }
340
341 static int attr_filter_postproxy(void *instance, REQUEST *request)
342 {
343         return attr_filter_common(instance, request, &request->proxy_reply->vps);
344 }
345
346 static int attr_filter_postauth(void *instance, REQUEST *request)
347 {
348         return attr_filter_common(instance, request, &request->reply->vps);
349 }
350
351
352 /* globally exported name */
353 module_t rlm_attr_filter = {
354         RLM_MODULE_INIT,
355         "attr_filter",
356         0,                              /* type: reserved */
357         attr_filter_instantiate,        /* instantiation */
358         attr_filter_detach,             /* detach */
359         {
360                 NULL,                   /* authentication */
361                 NULL,                   /* authorization */
362                 NULL,                   /* preaccounting */
363                 attr_filter_accounting, /* accounting */
364                 NULL,                   /* checksimul */
365                 attr_filter_preproxy,   /* pre-proxy */
366                 attr_filter_postproxy,  /* post-proxy */
367                 attr_filter_postauth    /* post-auth */
368         },
369 };
370