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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2002 The FreeRADIUS server project
21 * Copyright 2002 Kostas Kalevras <kkalev@noc.ntua.gr>
26 #include "libradius.h"
40 #define RLM_REGEX_INPACKET 0
41 #define RLM_REGEX_INCONFIG 1
42 #define RLM_REGEX_INREPLY 2
44 static const char rcsid[] = "$Id$";
46 typedef struct rlm_attr_rewrite_t {
47 char *attribute; /* The attribute to search for */
48 int attr_num; /* The attribute number */
49 char *search; /* The pattern to search for */
50 int search_len; /* The length of the search pattern */
51 char *searchin_str; /* The VALUE_PAIR list to search in. Can be either packet,reply or config */
52 char searchin; /* The same as above just coded as a number for speed */
53 char *replace; /* The replacement */
54 int replace_len; /* The length of the replacement string */
55 int append; /* Switch to control append mode (1,0) */
56 int nocase; /* Ignore case */
57 int new_attr; /* Boolean. Do we create a new attribute or not? */
58 int num_matches; /* Maximum number of matches */
59 char *name; /* The module name */
63 static CONF_PARSER module_config[] = {
64 { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
65 { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
66 { "searchin", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
67 { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
68 { "append", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,append),NULL, "no" },
69 { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
70 { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
71 { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
72 { NULL, -1, 0, NULL, NULL }
76 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
78 rlm_attr_rewrite_t *data;
80 char *instance_name = NULL;
83 * Set up a storage area for instance data
85 data = rad_malloc(sizeof(*data));
89 memset(data, 0, sizeof(*data));
92 * If the configuration parameters can't be parsed, then
95 if (cf_section_parse(conf, data, module_config) < 0) {
101 * Discover the attribute number of the key.
103 if (data->attribute == NULL) {
104 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
107 if (data->search == NULL || data->replace == NULL) {
108 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
111 data->search_len = strlen(data->search);
112 data->replace_len = strlen(data->replace);
114 if (data->replace_len == 0 && data->new_attr){
115 radlog(L_ERR, "rlm_attr_rewrite: replace string must not be zero length in order to create new attribute.");
119 if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
120 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
123 if (data->searchin_str == NULL) {
124 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
125 data->searchin = RLM_REGEX_INPACKET;
128 if (strcmp(data->searchin_str, "packet") == 0)
129 data->searchin = RLM_REGEX_INPACKET;
130 else if (strcmp(data->searchin_str, "config") == 0)
131 data->searchin = RLM_REGEX_INCONFIG;
132 else if (strcmp(data->searchin_str, "reply") == 0)
133 data->searchin = RLM_REGEX_INREPLY;
135 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
136 data->searchin = RLM_REGEX_INPACKET;
138 free((char *)data->searchin_str);
140 dattr = dict_attrbyname(data->attribute);
142 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
146 data->attr_num = dattr->attr;
147 /* Add the module instance name */
149 instance_name = cf_section_name2(conf);
150 if (instance_name != NULL)
151 data->name = strdup(instance_name);
159 static int do_attr_rewrite(void *instance, REQUEST *request)
161 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
162 int ret = RLM_MODULE_NOOP;
163 VALUE_PAIR *attr_vp = NULL;
164 VALUE_PAIR *tmp = NULL;
170 unsigned int len = 0;
171 char err_msg[MAX_STRING_LEN];
173 unsigned int counter = 0;
174 char new_str[MAX_STRING_LEN];
176 char search_STR[MAX_STRING_LEN];
177 char replace_STR[MAX_STRING_LEN];
180 if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
181 if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
182 return RLM_MODULE_NOOP;
185 if (!data->new_attr){
186 switch (data->searchin) {
187 case RLM_REGEX_INPACKET:
188 if (data->attr_num == PW_USER_NAME)
189 attr_vp = request->username;
190 else if (data->attr_num == PW_PASSWORD)
191 attr_vp = request->password;
193 tmp = request->packet->vps;
195 case RLM_REGEX_INCONFIG:
196 tmp = request->config_items;
198 case RLM_REGEX_INREPLY:
199 tmp = request->reply->vps;
202 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
203 data->searchin = RLM_REGEX_INPACKET;
204 attr_vp = pairfind(request->packet->vps, data->attr_num);
209 attr_vp = pairfind(tmp, data->attr_num);
210 if (attr_vp == NULL) {
211 DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
214 if (attr_vp->strvalue == NULL || attr_vp->length == 0){
215 DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
218 cflags |= REG_EXTENDED;
222 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
223 DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
228 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
229 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
232 replace_len = strlen(replace_STR);
235 if (!data->new_attr){
236 if ((err = regcomp(&preg,search_STR,cflags))) {
237 regerror(err, &preg, err_msg, MAX_STRING_LEN);
238 DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
242 ptr2 = attr_vp->strvalue;
245 for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
246 err = regexec(&preg, ptr2, 1, &pmatch, 0);
247 if (err == REG_NOMATCH) {
249 DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
250 data->attribute, attr_vp->strvalue);
258 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
259 data->attribute, attr_vp->strvalue);
262 if (pmatch.rm_so == -1)
266 len = len + (pmatch.rm_eo - pmatch.rm_so);
269 if (counter >= MAX_STRING_LEN) {
271 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
272 data->attribute, attr_vp->strvalue);
276 strncpy(ptr, ptr2,len);
278 ptr2 += pmatch.rm_eo;
281 if (data->replace_len != 0 &&
282 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
283 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
286 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
290 counter += replace_len;
291 if (counter >= MAX_STRING_LEN) {
293 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
294 data->attribute, attr_vp->strvalue);
298 strncpy(ptr, replace_STR, replace_len);
303 len = strlen(ptr2) + 1; /* We add the ending NULL */
305 if (counter >= MAX_STRING_LEN){
306 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
307 data->attribute, attr_vp->strvalue);
310 strncpy(ptr, ptr2, len);
312 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
313 data->attribute, attr_vp->strvalue, new_str);
314 attr_vp->length = strlen(new_str);
315 strncpy(attr_vp->strvalue, new_str, (attr_vp->length + 1));
327 attr_vp = pairmake(data->attribute,replace_STR,0);
328 switch(data->searchin){
329 case RLM_REGEX_INPACKET:
330 pairadd(&request->packet->vps,attr_vp);
332 case RLM_REGEX_INCONFIG:
333 pairadd(&request->config_items,attr_vp);
335 case RLM_REGEX_INREPLY:
336 pairadd(&request->reply->vps,attr_vp);
339 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
340 data->searchin = RLM_REGEX_INPACKET;
341 pairadd(&request->packet->vps,attr_vp);
344 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,attr_vp->strvalue);
353 static int attr_rewrite_accounting(void *instance, REQUEST *request)
355 return do_attr_rewrite(instance, request);
358 static int attr_rewrite_authorize(void *instance, REQUEST *request)
360 return do_attr_rewrite(instance, request);
362 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
364 return do_attr_rewrite(instance, request);
366 static int attr_rewrite_preacct(void *instance, REQUEST *request)
368 return do_attr_rewrite(instance, request);
370 static int attr_rewrite_ismul(void *instance, REQUEST *request)
372 return do_attr_rewrite(instance, request);
375 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
377 return do_attr_rewrite(instance, request);
380 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
382 return do_attr_rewrite(instance, request);
385 static int attr_rewrite_postauth(void *instance, REQUEST *request)
387 return do_attr_rewrite(instance, request);
390 static int attr_rewrite_detach(void *instance)
392 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
395 free(data->attribute);
408 * The module name should be the only globally exported symbol.
409 * That is, everything else should be 'static'.
411 * If the module needs to temporarily modify it's instantiation
412 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
413 * The server will then take care of ensuring that the module
414 * is single-threaded.
416 module_t rlm_attr_rewrite = {
418 RLM_TYPE_THREAD_UNSAFE, /* type */
419 NULL, /* initialization */
420 attr_rewrite_instantiate, /* instantiation */
422 attr_rewrite_authenticate, /* authentication */
423 attr_rewrite_authorize, /* authorization */
424 attr_rewrite_preacct, /* preaccounting */
425 attr_rewrite_accounting, /* accounting */
426 attr_rewrite_ismul, /* checksimul */
427 attr_rewrite_preproxy, /* pre-proxy */
428 attr_rewrite_postproxy, /* post-proxy */
429 attr_rewrite_postauth /* post-auth */
431 attr_rewrite_detach, /* detach */