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 LRAD_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, 0, PW_TYPE_STRING, -1,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 = lrad_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
235 if (data->dat_type == PW_TYPE_STRING ||
236 data->dat_type == PW_TYPE_OCTETS) {
237 if (item_vp->length != chk_vp->length)
238 ret = RLM_MODULE_REJECT;
240 if (!memcmp(item_vp->vp_strvalue,
242 (size_t) chk_vp->length))
245 ret = RLM_MODULE_REJECT;
248 else{ /* Integer or Date */
250 if (item_vp->lvalue == chk_vp->lvalue)
253 ret = RLM_MODULE_REJECT;
256 if (ret == RLM_MODULE_REJECT &&
257 chk_vp->operator == T_OP_REG_EQ) {
260 char err_msg[MAX_STRING_LEN];
262 DEBUG("rlm_checkval: Doing regex");
263 err = regcomp(®, (char *)chk_vp->vp_strvalue, REG_EXTENDED|REG_NOSUB);
265 regerror(err, ®,err_msg, MAX_STRING_LEN);
266 DEBUG("rlm_checkval: regcomp() returned error: %s", err_msg);
267 return RLM_MODULE_FAIL;
269 if (regexec(®, (char *)item_vp->vp_strvalue,0, NULL, 0) == 0)
272 ret = RLM_MODULE_REJECT;
277 } while (ret == RLM_MODULE_REJECT &&
280 if (ret == RLM_MODULE_REJECT) {
281 if (!item_vp && data->notfound_reject){
282 char module_fmsg[MAX_STRING_LEN];
283 VALUE_PAIR *module_fmsg_vp;
285 snprintf(module_fmsg,sizeof(module_fmsg),
286 "rlm_checkval: Could not find item named %s in request", data->item_name);
287 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
288 pairadd(&request->packet->vps, module_fmsg_vp);
291 char module_fmsg[MAX_STRING_LEN];
292 VALUE_PAIR *module_fmsg_vp;
294 snprintf(module_fmsg,sizeof(module_fmsg),
295 "rlm_checkval: This %s is not allowed for the user", data->item_name);
296 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
297 pairadd(&request->packet->vps, module_fmsg_vp);
307 static int checkval_authorize(void *instance, REQUEST *request)
309 return do_checkval(instance,request);
312 static int checkval_accounting(void *instance, REQUEST *request)
314 return do_checkval(instance,request);
318 * The module name should be the only globally exported symbol.
319 * That is, everything else should be 'static'.
321 * If the module needs to temporarily modify it's instantiation
322 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
323 * The server will then take care of ensuring that the module
324 * is single-threaded.
326 module_t rlm_checkval = {
330 checkval_instantiate, /* instantiation */
331 checkval_detach, /* detach */
333 NULL, /* authentication */
334 checkval_authorize, /* authorization */
335 NULL, /* preaccounting */
336 checkval_accounting, /* accounting */
337 NULL, /* checksimul */
338 NULL, /* pre-proxy */
339 NULL, /* post-proxy */