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