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 static int getattrsfile(const char *filename, PAIR_LIST **pair_list)
78 PAIR_LIST *attrs = NULL;
82 rcode = pairlist_read(filename, &attrs, 1);
88 * Walk through the 'attrs' file list.
94 entry->check = entry->reply;
97 for (vp = entry->check; vp != NULL; vp = vp->next) {
100 * If it's NOT a vendor attribute,
101 * and it's NOT a wire protocol
102 * and we ignore Fall-Through,
103 * then bitch about it, giving a good warning message.
105 if ((vp->vendor == 0) &&
106 (vp->attribute > 0xff) &&
107 (vp->attribute > 1000)) {
108 log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
109 "\tfound in filter list for realm \"%s\".\n",
110 filename, entry->lineno, vp->name,
126 static int attr_filter_detach(void *instance)
128 struct attr_filter_instance *inst = instance;
129 pairlist_free(&inst->attrs);
136 * (Re-)read the "attrs" file into memory.
138 static int attr_filter_instantiate(CONF_SECTION *conf, void **instance)
140 struct attr_filter_instance *inst;
143 inst = rad_malloc(sizeof *inst);
147 memset(inst, 0, sizeof(*inst));
149 if (cf_section_parse(conf, inst, module_config) < 0) {
150 attr_filter_detach(inst);
154 rcode = getattrsfile(inst->attrsfile, &inst->attrs);
156 radlog(L_ERR|L_CONS, "Errors reading %s", inst->attrsfile);
157 attr_filter_detach(inst);
166 * Common attr_filter checks
168 static int attr_filter_common(void *instance, REQUEST *request,
169 RADIUS_PACKET *packet)
171 struct attr_filter_instance *inst = instance;
174 VALUE_PAIR **output_tail;
175 VALUE_PAIR *check_item;
179 char *keyname = NULL;
183 if (!packet) return RLM_MODULE_NOOP;
185 input = &(packet->vps);
188 VALUE_PAIR *namepair;
190 namepair = pairfind(request->packet->vps, PW_REALM, 0);
192 return (RLM_MODULE_NOOP);
194 keyname = namepair->vp_strvalue;
198 len = radius_xlat(buffer, sizeof(buffer), inst->key,
201 return RLM_MODULE_NOOP;
207 output_tail = &output;
210 * Find the attr_filter profile entry for the entry.
212 for (pl = inst->attrs; pl; pl = pl->next) {
213 int fall_through = 0;
216 * If the current entry is NOT a default,
217 * AND the realm does NOT match the current entry,
218 * then skip to the next entry.
220 if ((strcmp(pl->name, "DEFAULT") != 0) &&
221 (strcmp(keyname, pl->name) != 0)) {
225 DEBUG2(" attr_filter: Matched entry %s at line %d", pl->name,
229 for (check_item = pl->check;
231 check_item = check_item->next) {
232 if ((check_item->attribute == PW_FALL_THROUGH) &&
233 (check_item->vp_integer == 1)) {
239 * If it is a SET operator, add the attribute to
240 * the output list without checking it.
242 if (check_item->operator == T_OP_SET ) {
243 vp = paircopyvp(check_item);
246 return RLM_MODULE_FAIL;
249 output_tail = &(vp->next);
254 * Iterate through the input items, comparing
255 * each item to every rule, then moving it to the
256 * output list only if it matches all rules
257 * for that attribute. IE, Idle-Timeout is moved
258 * only if it matches all rules that describe an
261 for (vp = *input; vp != NULL; vp = vp->next ) {
262 /* reset the pass,fail vars for each reply item */
266 * reset the check_item pointer to
267 * beginning of the list
269 for (check_item = pl->check;
271 check_item = check_item->next) {
273 * Vendor-Specific is special, and
274 * matches any VSA if the comparison
277 if ((check_item->attribute == PW_VENDOR_SPECIFIC) &&
279 (check_item->operator == T_OP_CMP_TRUE)) {
284 if (vp->attribute == check_item->attribute) {
285 check_pair(check_item, vp,
290 /* only move attribute if it passed all rules */
291 if (fail == 0 && pass > 0) {
292 *output_tail = paircopyvp(vp);
295 return RLM_MODULE_FAIL;
297 output_tail = &((*output_tail)->next);
301 /* If we shouldn't fall through, break */
307 * No entry matched. We didn't do anything.
310 rad_assert(output == NULL);
311 return RLM_MODULE_NOOP;
317 if (request->packet->code == PW_AUTHENTICATION_REQUEST) {
318 request->username = pairfind(request->packet->vps,
319 PW_STRIPPED_USER_NAME, 0);
320 if (!request->username)
321 request->username = pairfind(request->packet->vps,
323 request->password = pairfind(request->packet->vps,
324 PW_USER_PASSWORD, 0);
327 return RLM_MODULE_UPDATED;
330 static int attr_filter_preacct(void *instance, REQUEST *request)
332 return attr_filter_common(instance, request, request->packet);
335 static int attr_filter_accounting(void *instance, REQUEST *request)
337 return attr_filter_common(instance, request, request->reply);
341 static int attr_filter_preproxy(void *instance, REQUEST *request)
343 return attr_filter_common(instance, request, request->proxy);
346 static int attr_filter_postproxy(void *instance, REQUEST *request)
348 return attr_filter_common(instance, request, request->proxy_reply);
352 static int attr_filter_postauth(void *instance, REQUEST *request)
354 return attr_filter_common(instance, request, request->reply);
357 static int attr_filter_authorize(void *instance, REQUEST *request)
359 return attr_filter_common(instance, request, request->packet);
363 /* globally exported name */
364 module_t rlm_attr_filter = {
367 RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE, /* type */
368 attr_filter_instantiate, /* instantiation */
369 attr_filter_detach, /* detach */
371 NULL, /* authentication */
372 attr_filter_authorize, /* authorization */
373 attr_filter_preacct, /* pre-acct */
374 attr_filter_accounting, /* accounting */
375 NULL, /* checksimul */
377 attr_filter_preproxy, /* pre-proxy */
378 attr_filter_postproxy, /* post-proxy */
382 attr_filter_postauth /* post-auth */