6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2003,2006 The FreeRADIUS server project
21 * Copyright 2003 Kostas Kalevras <kkalev@noc.ntua.gr>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
34 #define REG_EXTENDED (0)
41 * Define a structure for our module configuration.
43 * These variables do not need to be in a structure, but it's
44 * a lot cleaner to do so, and a pointer to the structure can
45 * be used as the instance handle.
47 typedef struct rlm_checkval_t {
48 char *item_name; /* The attribute inside Access-Request ie Calling-Station-Id */
49 char *check_name; /* The attribute to check it with ie Allowed-Calling-Station-Id */
50 char *data_type; /* string,integer,ipaddr,date,abinary,octets */
54 int notfound_reject; /* If we don't find the item_name in the request send back a reject */
58 * A mapping of configuration file names to internal variables.
60 * Note that the string is dynamically allocated, so it MUST
61 * be freed. When the configuration file parse re-reads the string,
62 * it free's the old one, and strdup's the new one, placing the pointer
63 * to the strdup'd string into 'config.string'. This gets around
66 static const CONF_PARSER module_config[] = {
67 { "item-name", PW_TYPE_STRING_PTR, offsetof(rlm_checkval_t,item_name), NULL, NULL},
68 { "check-name", PW_TYPE_STRING_PTR, offsetof(rlm_checkval_t,check_name), NULL, NULL},
69 { "data-type", PW_TYPE_STRING_PTR, offsetof(rlm_checkval_t,data_type),NULL, "integer"},
70 { "notfound-reject", PW_TYPE_BOOLEAN, offsetof(rlm_checkval_t,notfound_reject),NULL, "no"},
71 { NULL, -1, 0, NULL, NULL } /* end the list */
75 static int checkval_detach(void *instance)
82 * Do any per-module initialization that is separate to each
83 * configured instance of the module. e.g. set up connections
84 * to external databases, read configuration files, set up
85 * dictionary entries, etc.
87 * If configuration information is given in the config section
88 * that must be referenced in later calls, store a handle to it
89 * in *instance otherwise put a null pointer there.
91 static int checkval_instantiate(CONF_SECTION *conf, void **instance)
97 static const FR_NAME_NUMBER names[] = {
98 { "string", PW_TYPE_STRING },
99 { "integer", PW_TYPE_INTEGER },
100 { "ipaddr", PW_TYPE_IPADDR },
101 { "date", PW_TYPE_DATE },
102 { "abinary", PW_TYPE_OCTETS },
103 { "octets", PW_TYPE_OCTETS },
104 { "binary", PW_TYPE_OCTETS },
109 * Set up a storage area for instance data
111 data = rad_malloc(sizeof(*data));
115 memset(data, 0, sizeof(*data));
118 * If the configuration parameters can't be parsed, then
121 if (cf_section_parse(conf, data, module_config) < 0) {
122 checkval_detach(data);
127 * Check if data_type exists
129 if (!data->data_type || !strlen(data->data_type)){
130 radlog(L_ERR, "rlm_checkval: Data type not defined");
131 checkval_detach(data);
134 if (!data->item_name || !strlen(data->item_name)){
135 radlog(L_ERR, "rlm_checkval: Item name not defined");
136 checkval_detach(data);
139 if (!data->check_name || !strlen(data->check_name)){
140 radlog(L_ERR, "rlm_checkval: Check item name not defined");
141 checkval_detach(data);
146 * Discover the attribute number of the item name
148 dattr = dict_attrbyname(data->item_name);
150 radlog(L_ERR, "rlm_checkval: No such attribute %s",
152 checkval_detach(data);
155 data->item_attr = dattr->attr;
158 * Add the check attribute name to the dictionary
159 * if it does not already exists. dict_addattr() handles that
162 memset(&flags, 0, sizeof(flags));
163 dict_addattr(data->check_name, -1, 0, PW_TYPE_STRING, flags);
164 dattr = dict_attrbyname(data->check_name);
166 radlog(L_ERR, "rlm_checkval: No such attribute %s",
168 checkval_detach(data);
171 data->chk_attr = dattr->attr;
172 DEBUG2("rlm_checkval: Registered name %s for attribute %d",
173 dattr->name,dattr->attr);
176 * Convert the string type to an integer type,
177 * so we don't have to do string comparisons on each
180 data->dat_type = fr_str2int(names, data->data_type, -1);
181 if (data->dat_type < 0) {
182 radlog(L_ERR, "rlm_checkval: Data type %s in not known",data->data_type);
183 checkval_detach(data);
192 static int do_checkval(void *instance, REQUEST *request)
194 rlm_checkval_t *data = (rlm_checkval_t *) instance;
195 int ret=RLM_MODULE_NOOP;
196 VALUE_PAIR *chk_vp, *item_vp;
200 /* quiet the compiler */
206 * Look for the check item
209 if (!(item_vp = pairfind(request->packet->vps, data->item_attr))){
210 DEBUG2("rlm_checkval: Could not find item named %s in request", data->item_name);
211 if (data->notfound_reject)
212 ret = RLM_MODULE_REJECT;
214 ret = RLM_MODULE_NOTFOUND;
217 DEBUG2("rlm_checkval: Item Name: %s, Value: %s",data->item_name, item_vp->vp_strvalue);
218 tmp = request->config_items;
220 if (!(chk_vp = pairfind(tmp, data->chk_attr))){
222 DEBUG2("rlm_checkval: Could not find attribute named %s in check pairs",data->check_name);
223 ret = RLM_MODULE_NOTFOUND;
229 DEBUG2("rlm_checkval: Value Name: %s, Value: %s",data->check_name, chk_vp->vp_strvalue);
232 * Check if item != check
234 * FIXME: !!! Call normal API functions!
237 if (data->dat_type == PW_TYPE_STRING ||
238 data->dat_type == PW_TYPE_OCTETS) {
239 if (item_vp->length != chk_vp->length)
240 ret = RLM_MODULE_REJECT;
242 if (!memcmp(item_vp->vp_strvalue,
244 (size_t) chk_vp->length))
247 ret = RLM_MODULE_REJECT;
249 } else if (data->dat_type == PW_TYPE_DATE) {
250 if (item_vp->vp_date == chk_vp->vp_date)
253 ret = RLM_MODULE_REJECT;
254 } else if (data->dat_type == PW_TYPE_INTEGER) {
255 if (item_vp->vp_integer == chk_vp->vp_integer)
258 ret = RLM_MODULE_REJECT;
261 if (ret == RLM_MODULE_REJECT &&
262 chk_vp->operator == T_OP_REG_EQ) {
265 char err_msg[MAX_STRING_LEN];
267 DEBUG("rlm_checkval: Doing regex");
268 err = regcomp(®, (char *)chk_vp->vp_strvalue, REG_EXTENDED|REG_NOSUB);
270 regerror(err, ®,err_msg, MAX_STRING_LEN);
271 DEBUG("rlm_checkval: regcomp() returned error: %s", err_msg);
272 return RLM_MODULE_FAIL;
274 if (regexec(®, (char *)item_vp->vp_strvalue,0, NULL, 0) == 0)
277 ret = RLM_MODULE_REJECT;
282 } while (ret == RLM_MODULE_REJECT &&
285 if (ret == RLM_MODULE_REJECT) {
286 if (!item_vp && data->notfound_reject){
287 char module_fmsg[MAX_STRING_LEN];
288 VALUE_PAIR *module_fmsg_vp;
290 snprintf(module_fmsg,sizeof(module_fmsg),
291 "rlm_checkval: Could not find item named %s in request", data->item_name);
292 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
293 pairadd(&request->packet->vps, module_fmsg_vp);
296 char module_fmsg[MAX_STRING_LEN];
297 VALUE_PAIR *module_fmsg_vp;
299 snprintf(module_fmsg,sizeof(module_fmsg),
300 "rlm_checkval: This %s is not allowed for the user", data->item_name);
301 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
302 pairadd(&request->packet->vps, module_fmsg_vp);
312 static int checkval_authorize(void *instance, REQUEST *request)
314 return do_checkval(instance,request);
317 static int checkval_accounting(void *instance, REQUEST *request)
319 return do_checkval(instance,request);
323 * The module name should be the only globally exported symbol.
324 * That is, everything else should be 'static'.
326 * If the module needs to temporarily modify it's instantiation
327 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
328 * The server will then take care of ensuring that the module
329 * is single-threaded.
331 module_t rlm_checkval = {
335 checkval_instantiate, /* instantiation */
336 checkval_detach, /* detach */
338 NULL, /* authentication */
339 checkval_authorize, /* authorization */
340 NULL, /* preaccounting */
341 checkval_accounting, /* accounting */
342 NULL, /* checksimul */
343 NULL, /* pre-proxy */
344 NULL, /* post-proxy */