2 * rlm_attr_filter.c - Filter A/V Pairs received back from proxy reqs
3 * before sending reply to the NAS/Server that sent
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.
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.
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
21 * Copyright (C) 2001,2006 The FreeRADIUS server project
22 * Copyright (C) 2001 Chris Parker <cparker@starnetusa.net>
25 #include <freeradius-devel/ident.h>
28 #include <freeradius-devel/radiusd.h>
29 #include <freeradius-devel/modules.h>
30 #include <freeradius-devel/rad_assert.h>
40 * Define a structure with the module configuration, so it can
41 * be used as the instance handle.
43 struct attr_filter_instance {
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 }
57 static void check_pair(VALUE_PAIR *check_item, VALUE_PAIR *reply_item,
62 if (check_item->operator == T_OP_SET) return;
64 compare = paircmp(check_item, reply_item);
75 * Copy the specified attribute to the specified list
77 static int mypairappend(REQUEST *request, VALUE_PAIR *item, VALUE_PAIR **to)
80 tmp = radius_paircreate(request, to, item->attribute, item->type);
85 memcpy(tmp, item, sizeof(*tmp));
92 static int getattrsfile(const char *filename, PAIR_LIST **pair_list)
95 PAIR_LIST *attrs = NULL;
99 rcode = pairlist_read(filename, &attrs, 1);
105 * Walk through the 'attrs' file list.
111 entry->check = entry->reply;
114 for (vp = entry->check; vp != NULL; vp = vp->next) {
117 * If it's NOT a vendor attribute,
118 * and it's NOT a wire protocol
119 * and we ignore Fall-Through,
120 * then bitch about it, giving a good warning message.
122 if (!(vp->attribute & ~0xffff) &&
123 (vp->attribute > 0xff) &&
124 (vp->attribute > 1000)) {
125 log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
126 "\tfound in filter list for realm \"%s\".\n",
127 filename, entry->lineno, vp->name,
143 static int attr_filter_detach(void *instance)
145 struct attr_filter_instance *inst = instance;
146 pairlist_free(&inst->attrs);
153 * (Re-)read the "attrs" file into memory.
155 static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
157 struct attr_filter_instance *inst;
160 inst = rad_malloc(sizeof *inst);
164 memset(inst, 0, sizeof(*inst));
166 if (cf_section_parse(conf, inst, module_config) < 0) {
167 attr_filter_detach(inst);
171 rcode = getattrsfile(inst->attrsfile, &inst->attrs);
173 radlog(L_ERR|L_CONS, "Errors reading %s", inst->attrsfile);
174 attr_filter_detach(inst);
183 * Common attr_filter checks
185 static int attr_filter_common(void *instance, REQUEST *request,
188 struct attr_filter_instance *inst = instance;
190 VALUE_PAIR *output = NULL;
191 VALUE_PAIR **output_tail;
192 VALUE_PAIR *check_item;
196 char *keyname = NULL;
200 VALUE_PAIR *namepair;
202 namepair = pairfind(request->packet->vps, PW_REALM);
204 return (RLM_MODULE_NOOP);
206 keyname = namepair->vp_strvalue;
210 len = radius_xlat(buffer, sizeof(buffer), inst->key,
213 return RLM_MODULE_NOOP;
218 output_tail = &output;
221 * Find the attr_filter profile entry for the entry.
223 for (pl = inst->attrs; pl; pl = pl->next) {
224 int fall_through = 0;
227 * If the current entry is NOT a default,
228 * AND the realm does NOT match the current entry,
229 * then skip to the next entry.
231 if ((strcmp(pl->name, "DEFAULT") != 0) &&
232 (strcmp(keyname, pl->name) != 0)) {
236 DEBUG2(" attr_filter: Matched entry %s at line %d", pl->name,
240 for (check_item = pl->check;
242 check_item = check_item->next) {
243 if (check_item->attribute == PW_FALL_THROUGH) {
249 * If it is a SET operator, add the attribute to
250 * the output list without checking it.
252 if (check_item->operator == T_OP_SET ) {
253 if (mypairappend(request, check_item, output_tail) < 0) {
255 return RLM_MODULE_FAIL;
257 output_tail = &((*output_tail)->next);
262 * Iterate through the input items, comparing
263 * each item to every rule, then moving it to the
264 * output list only if it matches all rules
265 * for that attribute. IE, Idle-Timeout is moved
266 * only if it matches all rules that describe an
269 for (vp = *input; vp != NULL; vp = vp->next ) {
270 /* reset the pass,fail vars for each reply item */
274 * reset the check_item pointer to
275 * beginning of the list
277 for (check_item = pl->check;
279 check_item = check_item->next) {
281 * Vendor-Specific is special, and
282 * matches any VSA if the comparison
285 if ((check_item->attribute == PW_VENDOR_SPECIFIC) &&
286 (VENDOR(vp->attribute) != 0) &&
287 (check_item->operator == T_OP_CMP_TRUE)) {
292 if (vp->attribute == check_item->attribute) {
293 check_pair(check_item, vp,
298 /* only move attribute if it passed all rules */
299 if (fail == 0 && pass > 0) {
300 if (mypairappend(request, vp, output_tail) < 0) {
302 return RLM_MODULE_FAIL;
304 output_tail = &((*output_tail)->next);
308 /* If we shouldn't fall through, break */
314 * No entry matched. We didn't do anything.
317 rad_assert(output == NULL);
318 return RLM_MODULE_NOOP;
324 return RLM_MODULE_UPDATED;
327 static int attr_filter_accounting(void *instance, REQUEST *request)
329 return attr_filter_common(instance, request, &request->packet->vps);
332 static int attr_filter_preproxy(void *instance, REQUEST *request)
334 return attr_filter_common(instance, request, &request->proxy->vps);
337 static int attr_filter_postproxy(void *instance, REQUEST *request)
339 return attr_filter_common(instance, request, &request->proxy_reply->vps);
342 static int attr_filter_postauth(void *instance, REQUEST *request)
344 return attr_filter_common(instance, request, &request->reply->vps);
348 /* globally exported name */
349 module_t rlm_attr_filter = {
352 0, /* type: reserved */
353 attr_filter_instantiate, /* instantiation */
354 attr_filter_detach, /* detach */
356 NULL, /* authentication */
357 NULL, /* authorization */
358 NULL, /* preaccounting */
359 attr_filter_accounting, /* accounting */
360 NULL, /* checksimul */
361 attr_filter_preproxy, /* pre-proxy */
362 attr_filter_postproxy, /* post-proxy */
363 attr_filter_postauth /* post-auth */