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 char *searchin_str; /* The VALUE_PAIR list to search in. Can be either packet,reply or config */
51 char searchin; /* The same as above just coded as a number for speed */
52 char *replace; /* The replacement */
53 int nocase; /* Ignore case */
54 int new_attr; /* Boolean. Do we create a new attribute or not? */
55 int num_matches; /* Maximum number of matches */
56 char *name; /* The module name */
60 static CONF_PARSER module_config[] = {
61 { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
62 { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
63 { "searchin", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
64 { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
65 { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
66 { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
67 { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
68 { NULL, -1, 0, NULL, NULL }
72 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
74 rlm_attr_rewrite_t *data;
76 char *instance_name = NULL;
79 * Set up a storage area for instance data
81 data = rad_malloc(sizeof(*data));
84 * If the configuration parameters can't be parsed, then
87 if (cf_section_parse(conf, data, module_config) < 0) {
93 * Discover the attribute number of the key.
95 if (data->attribute == NULL) {
96 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
99 if (data->search == NULL || data->replace == NULL) {
100 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
104 if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
105 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
108 if (data->searchin_str == NULL) {
109 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
110 data->searchin = RLM_REGEX_INPACKET;
113 if (strcmp(data->searchin_str, "packet") == 0)
114 data->searchin = RLM_REGEX_INPACKET;
115 else if (strcmp(data->searchin_str, "config") == 0)
116 data->searchin = RLM_REGEX_INCONFIG;
117 else if (strcmp(data->searchin_str, "reply") == 0)
118 data->searchin = RLM_REGEX_INREPLY;
120 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
121 data->searchin = RLM_REGEX_INPACKET;
123 free((char *)data->searchin_str);
125 dattr = dict_attrbyname(data->attribute);
127 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
131 data->attr_num = dattr->attr;
132 /* Add the module instance name */
134 instance_name = cf_section_name2(conf);
135 if (instance_name != NULL)
136 data->name = strdup(instance_name);
144 static int do_attr_rewrite(void *instance, REQUEST *request)
146 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
147 int ret = RLM_MODULE_NOOP;
148 VALUE_PAIR *attr_vp = NULL;
153 unsigned int len = 0;
154 char err_msg[MAX_STRING_LEN];
156 unsigned int counter = 0;
157 char new_str[MAX_STRING_LEN];
159 char search_STR[MAX_STRING_LEN];
160 char replace_STR[MAX_STRING_LEN];
163 if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
164 if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
165 return RLM_MODULE_NOOP;
168 if (!data->new_attr){
169 switch (data->searchin) {
170 case RLM_REGEX_INPACKET:
171 if (data->attr_num == PW_USER_NAME)
172 attr_vp = request->username;
173 else if (data->attr_num == PW_PASSWORD)
174 attr_vp = request->password;
176 attr_vp = pairfind(request->packet->vps, data->attr_num);
178 case RLM_REGEX_INCONFIG:
179 attr_vp = pairfind(request->config_items, data->attr_num);
181 case RLM_REGEX_INREPLY:
182 attr_vp = pairfind(request->reply->vps, data->attr_num);
185 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
186 data->searchin = RLM_REGEX_INPACKET;
187 attr_vp = pairfind(request->packet->vps, data->attr_num);
190 if (attr_vp == NULL) {
191 DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
194 if (attr_vp->strvalue == NULL || attr_vp->length == 0){
195 DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
198 cflags |= REG_EXTENDED;
202 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL)) {
203 DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
207 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
208 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
211 replace_len = strlen(replace_STR);
213 if (!data->new_attr){
214 if ((err = regcomp(&preg,search_STR,cflags))) {
215 regerror(err, &preg, err_msg, MAX_STRING_LEN);
216 DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
220 ptr2 = attr_vp->strvalue;
223 for ( /**/ ;i < data->num_matches; i++) {
224 err = regexec(&preg, ptr2, 1, &pmatch, 0);
225 if (err == REG_NOMATCH) {
227 DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
228 data->attribute, attr_vp->strvalue);
230 return RLM_MODULE_OK;
236 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
237 data->attribute, attr_vp->strvalue);
240 if (pmatch.rm_so == -1)
244 if (counter >= MAX_STRING_LEN) {
246 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
247 data->attribute, attr_vp->strvalue);
251 strncpy(ptr, ptr2,len);
253 ptr2 += pmatch.rm_eo;
255 counter += replace_len;
256 if (counter >= MAX_STRING_LEN) {
258 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
259 data->attribute, attr_vp->strvalue);
262 strncpy(ptr, replace_STR, replace_len);
266 len = strlen(ptr2) + 1; /* We add the ending NULL */
268 if (counter >= MAX_STRING_LEN){
269 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
270 data->attribute, attr_vp->strvalue);
273 strncpy(ptr, ptr2, len);
275 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
276 data->attribute, attr_vp->strvalue, new_str);
277 attr_vp->length = strlen(new_str);
278 strncpy(attr_vp->strvalue, new_str, (attr_vp->length + 1));
283 attr_vp = pairmake(data->attribute,replace_STR,0);
284 switch(data->searchin){
285 case RLM_REGEX_INPACKET:
286 pairadd(&request->packet->vps,attr_vp);
288 case RLM_REGEX_INCONFIG:
289 pairadd(&request->config_items,attr_vp);
291 case RLM_REGEX_INREPLY:
292 pairadd(&request->reply->vps,attr_vp);
295 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
296 data->searchin = RLM_REGEX_INPACKET;
297 pairadd(&request->packet->vps,attr_vp);
300 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,attr_vp->strvalue);
309 static int attr_rewrite_accounting(void *instance, REQUEST *request)
311 return do_attr_rewrite(instance, request);
314 static int attr_rewrite_authorize(void *instance, REQUEST *request)
316 return do_attr_rewrite(instance, request);
318 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
320 return do_attr_rewrite(instance, request);
322 static int attr_rewrite_preacct(void *instance, REQUEST *request)
324 return do_attr_rewrite(instance, request);
326 static int attr_rewrite_ismul(void *instance, REQUEST *request)
328 return do_attr_rewrite(instance, request);
331 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
333 return do_attr_rewrite(instance, request);
336 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
338 return do_attr_rewrite(instance, request);
341 static int attr_rewrite_postauth(void *instance, REQUEST *request)
343 return do_attr_rewrite(instance, request);
346 static int attr_rewrite_detach(void *instance)
348 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
351 free(data->attribute);
364 * The module name should be the only globally exported symbol.
365 * That is, everything else should be 'static'.
367 * If the module needs to temporarily modify it's instantiation
368 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
369 * The server will then take care of ensuring that the module
370 * is single-threaded.
372 module_t rlm_attr_rewrite = {
374 RLM_TYPE_THREAD_UNSAFE, /* type */
375 NULL, /* initialization */
376 attr_rewrite_instantiate, /* instantiation */
378 attr_rewrite_authenticate, /* authentication */
379 attr_rewrite_authorize, /* authorization */
380 attr_rewrite_preacct, /* preaccounting */
381 attr_rewrite_accounting, /* accounting */
382 attr_rewrite_ismul, /* checksimul */
383 attr_rewrite_preproxy, /* pre-proxy */
384 attr_rewrite_postproxy, /* post-proxy */
385 attr_rewrite_postauth /* post-auth */
387 attr_rewrite_detach, /* detach */