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 2001 The FreeRADIUS server project
21 * Copyright 2001 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 nocase; /* Ignore case */
56 int new_attr; /* Boolean. Do we create a new attribute or not? */
57 int num_matches; /* Maximum number of matches */
58 char *name; /* The module name */
62 static CONF_PARSER module_config[] = {
63 { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
64 { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
65 { "searchin", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
66 { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
67 { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
68 { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
69 { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
70 { NULL, -1, 0, NULL, NULL }
74 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
76 rlm_attr_rewrite_t *data;
78 char *instance_name = NULL;
81 * Set up a storage area for instance data
83 data = rad_malloc(sizeof(*data));
86 * If the configuration parameters can't be parsed, then
89 if (cf_section_parse(conf, data, module_config) < 0) {
95 * Discover the attribute number of the key.
97 if (data->attribute == NULL) {
98 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
101 if (data->search == NULL || data->replace == NULL) {
102 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
105 data->search_len = strlen(data->search);
106 data->replace_len = strlen(data->replace);
108 if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
109 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
112 if (data->searchin_str == NULL) {
113 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
114 data->searchin = RLM_REGEX_INPACKET;
117 if (strcmp(data->searchin_str, "packet") == 0)
118 data->searchin = RLM_REGEX_INPACKET;
119 else if (strcmp(data->searchin_str, "config") == 0)
120 data->searchin = RLM_REGEX_INCONFIG;
121 else if (strcmp(data->searchin_str, "reply") == 0)
122 data->searchin = RLM_REGEX_INREPLY;
124 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
125 data->searchin = RLM_REGEX_INPACKET;
127 free((char *)data->searchin_str);
129 dattr = dict_attrbyname(data->attribute);
131 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
135 data->attr_num = dattr->attr;
136 /* Add the module instance name */
138 instance_name = cf_section_name2(conf);
139 if (instance_name != NULL)
140 data->name = strdup(instance_name);
148 static int do_attr_rewrite(void *instance, REQUEST *request)
150 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
151 int ret = RLM_MODULE_NOOP;
152 VALUE_PAIR *attr_vp = NULL;
153 VALUE_PAIR *tmp = NULL;
158 unsigned int len = 0;
159 char err_msg[MAX_STRING_LEN];
161 unsigned int counter = 0;
162 char new_str[MAX_STRING_LEN];
164 char search_STR[MAX_STRING_LEN];
165 char replace_STR[MAX_STRING_LEN];
168 if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
169 if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
170 return RLM_MODULE_NOOP;
173 if (!data->new_attr){
174 switch (data->searchin) {
175 case RLM_REGEX_INPACKET:
176 if (data->attr_num == PW_USER_NAME)
177 attr_vp = request->username;
178 else if (data->attr_num == PW_PASSWORD)
179 attr_vp = request->password;
181 tmp = request->packet->vps;
183 case RLM_REGEX_INCONFIG:
184 tmp = request->config_items;
186 case RLM_REGEX_INREPLY:
187 tmp = request->reply->vps;
190 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
191 data->searchin = RLM_REGEX_INPACKET;
192 attr_vp = pairfind(request->packet->vps, data->attr_num);
197 attr_vp = pairfind(tmp, data->attr_num);
198 if (attr_vp == NULL) {
199 DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
202 if (attr_vp->strvalue == NULL || attr_vp->length == 0){
203 DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
206 cflags |= REG_EXTENDED;
210 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
211 DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
215 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) && data->replace_len != 0) {
216 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
219 replace_len = strlen(replace_STR);
221 if (!data->new_attr){
222 if ((err = regcomp(&preg,search_STR,cflags))) {
223 regerror(err, &preg, err_msg, MAX_STRING_LEN);
224 DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
228 ptr2 = attr_vp->strvalue;
231 for ( i = 0 ;i < data->num_matches; i++) {
232 err = regexec(&preg, ptr2, 1, &pmatch, 0);
233 if (err == REG_NOMATCH) {
235 DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
236 data->attribute, attr_vp->strvalue);
238 return RLM_MODULE_OK;
244 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
245 data->attribute, attr_vp->strvalue);
248 if (pmatch.rm_so == -1)
252 if (counter >= MAX_STRING_LEN) {
254 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
255 data->attribute, attr_vp->strvalue);
259 strncpy(ptr, ptr2,len);
261 ptr2 += pmatch.rm_eo;
263 counter += replace_len;
264 if (counter >= MAX_STRING_LEN) {
266 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
267 data->attribute, attr_vp->strvalue);
270 strncpy(ptr, replace_STR, replace_len);
274 len = strlen(ptr2) + 1; /* We add the ending NULL */
276 if (counter >= MAX_STRING_LEN){
277 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
278 data->attribute, attr_vp->strvalue);
281 strncpy(ptr, ptr2, len);
283 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
284 data->attribute, attr_vp->strvalue, new_str);
285 attr_vp->length = strlen(new_str);
286 strncpy(attr_vp->strvalue, new_str, (attr_vp->length + 1));
297 attr_vp = pairmake(data->attribute,replace_STR,0);
298 switch(data->searchin){
299 case RLM_REGEX_INPACKET:
300 pairadd(&request->packet->vps,attr_vp);
302 case RLM_REGEX_INCONFIG:
303 pairadd(&request->config_items,attr_vp);
305 case RLM_REGEX_INREPLY:
306 pairadd(&request->reply->vps,attr_vp);
309 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
310 data->searchin = RLM_REGEX_INPACKET;
311 pairadd(&request->packet->vps,attr_vp);
314 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,attr_vp->strvalue);
323 static int attr_rewrite_accounting(void *instance, REQUEST *request)
325 return do_attr_rewrite(instance, request);
328 static int attr_rewrite_authorize(void *instance, REQUEST *request)
330 return do_attr_rewrite(instance, request);
332 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
334 return do_attr_rewrite(instance, request);
336 static int attr_rewrite_preacct(void *instance, REQUEST *request)
338 return do_attr_rewrite(instance, request);
340 static int attr_rewrite_ismul(void *instance, REQUEST *request)
342 return do_attr_rewrite(instance, request);
345 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
347 return do_attr_rewrite(instance, request);
350 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
352 return do_attr_rewrite(instance, request);
355 static int attr_rewrite_postauth(void *instance, REQUEST *request)
357 return do_attr_rewrite(instance, request);
360 static int attr_rewrite_detach(void *instance)
362 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
365 free(data->attribute);
378 * The module name should be the only globally exported symbol.
379 * That is, everything else should be 'static'.
381 * If the module needs to temporarily modify it's instantiation
382 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
383 * The server will then take care of ensuring that the module
384 * is single-threaded.
386 module_t rlm_attr_rewrite = {
388 RLM_TYPE_THREAD_UNSAFE, /* type */
389 NULL, /* initialization */
390 attr_rewrite_instantiate, /* instantiation */
392 attr_rewrite_authenticate, /* authentication */
393 attr_rewrite_authorize, /* authorization */
394 attr_rewrite_preacct, /* preaccounting */
395 attr_rewrite_accounting, /* accounting */
396 attr_rewrite_ismul, /* checksimul */
397 attr_rewrite_preproxy, /* pre-proxy */
398 attr_rewrite_postproxy, /* post-proxy */
399 attr_rewrite_postauth /* post-auth */
401 attr_rewrite_detach, /* detach */