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/autoconf.h>
39 #include <freeradius-devel/radiusd.h>
40 #include <freeradius-devel/rad_assert.h>
41 #include <freeradius-devel/modules.h>
44 * Define a structure with the module configuration, so it can
45 * be used as the instance handle.
47 struct attr_filter_instance {
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 }
61 static void check_pair(VALUE_PAIR *check_item, VALUE_PAIR *reply_item,
66 if (check_item->operator == T_OP_SET) return;
68 compare = paircmp(check_item, reply_item);
79 * Copy the specified attribute to the specified list
81 static int mypairappend(VALUE_PAIR *item, VALUE_PAIR **to)
84 tmp = paircreate(item->attribute, item->type);
86 radlog(L_ERR|L_CONS, "no memory");
93 memcpy(tmp, item, sizeof(*tmp));
100 static int getattrsfile(const char *filename, PAIR_LIST **pair_list)
103 PAIR_LIST *attrs = NULL;
107 rcode = pairlist_read(filename, &attrs, 1);
113 * Walk through the 'attrs' file list.
119 entry->check = entry->reply;
122 for (vp = entry->check; vp != NULL; vp = vp->next) {
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.
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,
151 static int attr_filter_detach(void *instance)
153 struct attr_filter_instance *inst = instance;
154 pairlist_free(&inst->attrs);
155 free(inst->attrsfile);
163 * (Re-)read the "attrs" file into memory.
165 static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
167 struct attr_filter_instance *inst;
170 inst = rad_malloc(sizeof *inst);
174 memset(inst, 0, sizeof(*inst));
176 if (cf_section_parse(conf, inst, module_config) < 0) {
177 attr_filter_detach(inst);
181 rcode = getattrsfile(inst->attrsfile, &inst->attrs);
183 radlog(L_ERR|L_CONS, "Errors reading %s", inst->attrsfile);
184 attr_filter_detach(inst);
193 * Common attr_filter checks
195 static int attr_filter_common(void *instance, REQUEST *request,
198 struct attr_filter_instance *inst = instance;
200 VALUE_PAIR *output = NULL;
201 VALUE_PAIR **output_tail;
202 VALUE_PAIR *check_item;
206 char *keyname = NULL;
210 VALUE_PAIR *namepair;
212 namepair = pairfind(request->packet->vps, PW_REALM);
214 return (RLM_MODULE_NOOP);
216 keyname = namepair->vp_strvalue;
220 len = radius_xlat(buffer, sizeof(buffer), inst->key,
223 return RLM_MODULE_NOOP;
228 output_tail = &output;
231 * Find the attr_filter profile entry for the entry.
233 for (pl = inst->attrs; pl; pl = pl->next) {
234 int fall_through = 0;
237 * If the current entry is NOT a default,
238 * AND the realm does NOT match the current entry,
239 * then skip to the next entry.
241 if ((strcmp(pl->name, "DEFAULT") != 0) &&
242 (strcmp(keyname, pl->name) != 0)) {
246 DEBUG2(" attr_filter: Matched entry %s at line %d", pl->name,
250 for (check_item = pl->check;
252 check_item = check_item->next) {
253 if (check_item->attribute == PW_FALL_THROUGH) {
259 * If it is a SET operator, add the attribute to
260 * the output list without checking it.
262 if (check_item->operator == T_OP_SET ) {
263 if (mypairappend(check_item, output_tail) < 0) {
265 return RLM_MODULE_FAIL;
267 output_tail = &((*output_tail)->next);
272 * Iterate through the input items, comparing
273 * each item to every rule, then moving it to the
274 * output list only if it matches all rules
275 * for that attribute. IE, Idle-Timeout is moved
276 * only if it matches all rules that describe an
279 for (vp = *input; vp != NULL; vp = vp->next ) {
280 /* reset the pass,fail vars for each reply item */
284 * reset the check_item pointer to
285 * beginning of the list
287 for (check_item = pl->check;
289 check_item = check_item->next) {
291 * Vendor-Specific is special, and
292 * matches any VSA if the comparison
295 if ((check_item->attribute == PW_VENDOR_SPECIFIC) &&
296 (VENDOR(vp->attribute) != 0) &&
297 (check_item->operator == T_OP_CMP_TRUE)) {
302 if (vp->attribute == check_item->attribute) {
303 check_pair(check_item, vp,
308 /* only move attribute if it passed all rules */
309 if (fail == 0 && pass > 0) {
310 if (mypairappend(vp, output_tail) < 0) {
312 return RLM_MODULE_FAIL;
314 output_tail = &((*output_tail)->next);
318 /* If we shouldn't fall through, break */
324 * No entry matched. We didn't do anything.
327 rad_assert(output == NULL);
328 return RLM_MODULE_NOOP;
334 return RLM_MODULE_UPDATED;
337 static int attr_filter_accounting(void *instance, REQUEST *request)
339 return attr_filter_common(instance, request, &request->packet->vps);
342 static int attr_filter_preproxy(void *instance, REQUEST *request)
344 return attr_filter_common(instance, request, &request->proxy->vps);
347 static int attr_filter_postproxy(void *instance, REQUEST *request)
349 return attr_filter_common(instance, request, &request->proxy_reply->vps);
352 static int attr_filter_postauth(void *instance, REQUEST *request)
354 return attr_filter_common(instance, request, &request->reply->vps);
358 /* globally exported name */
359 module_t rlm_attr_filter = {
362 0, /* type: reserved */
363 attr_filter_instantiate, /* instantiation */
364 attr_filter_detach, /* detach */
366 NULL, /* authentication */
367 NULL, /* authorization */
368 NULL, /* preaccounting */
369 attr_filter_accounting, /* accounting */
370 NULL, /* checksimul */
371 attr_filter_preproxy, /* pre-proxy */
372 attr_filter_postproxy, /* post-proxy */
373 attr_filter_postauth /* post-auth */