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 2002,2006 The FreeRADIUS server project
21 * Copyright 2002 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 RLM_REGEX_INPACKET 0
35 #define RLM_REGEX_INCONFIG 1
36 #define RLM_REGEX_INREPLY 2
37 #define RLM_REGEX_INPROXY 3
38 #define RLM_REGEX_INPROXYREPLY 4
40 typedef struct rlm_attr_rewrite_t {
41 char *attribute; /* The attribute to search for */
42 int attr_num; /* The attribute number */
43 char *search; /* The pattern to search for */
44 int search_len; /* The length of the search pattern */
45 char *searchin_str; /* The VALUE_PAIR list to search in. Can be either packet,reply,proxy,proxy_reply or config */
46 char searchin; /* The same as above just coded as a number for speed */
47 char *replace; /* The replacement */
48 int replace_len; /* The length of the replacement string */
49 int append; /* Switch to control append mode (1,0) */
50 int nocase; /* Ignore case */
51 int new_attr; /* Boolean. Do we create a new attribute or not? */
52 int num_matches; /* Maximum number of matches */
53 char *name; /* The module name */
56 static const CONF_PARSER module_config[] = {
57 { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
58 { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
59 { "searchin", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
60 { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
61 { "append", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,append),NULL, "no" },
62 { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
63 { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
64 { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
65 { NULL, -1, 0, NULL, NULL }
68 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
70 rlm_attr_rewrite_t *data;
72 const char *instance_name = NULL;
75 * Set up a storage area for instance data
77 data = rad_malloc(sizeof(*data));
81 memset(data, 0, 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.");
103 data->search_len = strlen(data->search);
104 data->replace_len = strlen(data->replace);
106 if (data->replace_len == 0 && data->new_attr){
107 radlog(L_ERR, "rlm_attr_rewrite: replace string must not be zero length in order to create new attribute.");
111 if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
112 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
115 if (data->searchin_str == NULL) {
116 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
117 data->searchin = RLM_REGEX_INPACKET;
120 if (strcmp(data->searchin_str, "packet") == 0)
121 data->searchin = RLM_REGEX_INPACKET;
122 else if (strcmp(data->searchin_str, "config") == 0)
123 data->searchin = RLM_REGEX_INCONFIG;
124 else if (strcmp(data->searchin_str, "reply") == 0)
125 data->searchin = RLM_REGEX_INREPLY;
126 else if (strcmp(data->searchin_str, "proxy") == 0)
127 data->searchin = RLM_REGEX_INPROXY;
128 else if (strcmp(data->searchin_str, "proxy_reply") == 0)
129 data->searchin = RLM_REGEX_INPROXYREPLY;
131 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
132 data->searchin = RLM_REGEX_INPACKET;
134 free((char *)data->searchin_str);
136 dattr = dict_attrbyname(data->attribute);
138 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
142 data->attr_num = dattr->attr;
143 /* Add the module instance name */
145 instance_name = cf_section_name2(conf);
146 if (instance_name != NULL)
147 data->name = strdup(instance_name);
155 static int do_attr_rewrite(void *instance, REQUEST *request)
157 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
158 int ret = RLM_MODULE_NOOP;
159 VALUE_PAIR *attr_vp = NULL;
160 VALUE_PAIR *tmp = NULL;
162 regmatch_t pmatch[9];
166 unsigned int len = 0;
167 char err_msg[MAX_STRING_LEN];
170 unsigned int counter = 0;
171 char new_str[MAX_STRING_LEN];
173 char search_STR[MAX_STRING_LEN];
174 char replace_STR[MAX_STRING_LEN];
177 if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
178 if (data->name == NULL || strcmp(data->name,attr_vp->vp_strvalue))
179 return RLM_MODULE_NOOP;
183 /* new_attribute = yes */
184 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
185 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
188 replace_len = strlen(replace_STR);
189 attr_vp = pairmake(data->attribute,replace_STR,0);
190 if (attr_vp == NULL){
191 DEBUG2("rlm_attr_rewrite: Could not add new attribute %s with value '%s'",
192 data->attribute,replace_STR);
195 switch(data->searchin){
196 case RLM_REGEX_INPACKET:
197 pairadd(&request->packet->vps,attr_vp);
199 case RLM_REGEX_INCONFIG:
200 pairadd(&request->config_items,attr_vp);
202 case RLM_REGEX_INREPLY:
203 pairadd(&request->reply->vps,attr_vp);
205 case RLM_REGEX_INPROXY:
206 if (!request->proxy) {
207 pairbasicfree(attr_vp);
208 return RLM_MODULE_NOOP;
210 pairadd(&request->proxy->vps, attr_vp);
212 case RLM_REGEX_INPROXYREPLY:
213 if (!request->proxy_reply) {
214 pairbasicfree(attr_vp);
215 return RLM_MODULE_NOOP;
217 pairadd(&request->proxy_reply->vps, attr_vp);
220 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
221 data->searchin = RLM_REGEX_INPACKET;
222 pairadd(&request->packet->vps,attr_vp);
225 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,replace_STR);
228 /* new_attribute = no */
229 switch (data->searchin) {
230 case RLM_REGEX_INPACKET:
231 if (data->attr_num == PW_USER_NAME)
232 attr_vp = request->username;
233 else if (data->attr_num == PW_USER_PASSWORD)
234 attr_vp = request->password;
236 tmp = request->packet->vps;
238 case RLM_REGEX_INCONFIG:
239 tmp = request->config_items;
241 case RLM_REGEX_INREPLY:
242 tmp = request->reply->vps;
244 case RLM_REGEX_INPROXYREPLY:
245 if (!request->proxy_reply)
246 return RLM_MODULE_NOOP;
247 tmp = request->proxy_reply->vps;
249 case RLM_REGEX_INPROXY:
251 return RLM_MODULE_NOOP;
252 tmp = request->proxy->vps;
255 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
256 data->searchin = RLM_REGEX_INPACKET;
257 attr_vp = pairfind(request->packet->vps, data->attr_num);
262 attr_vp = pairfind(tmp, data->attr_num);
263 if (attr_vp == NULL) {
264 DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
267 if (attr_vp->vp_strvalue == NULL || attr_vp->length == 0){
268 DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
271 cflags |= REG_EXTENDED;
275 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
276 DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
280 if ((err = regcomp(&preg,search_STR,cflags))) {
281 regerror(err, &preg, err_msg, MAX_STRING_LEN);
282 DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
286 if ((attr_vp->type == PW_TYPE_IPADDR) &&
287 (attr_vp->vp_strvalue[0] == '\0')) {
288 inet_ntop(AF_INET, &(attr_vp->vp_ipaddr),
289 attr_vp->vp_strvalue,
290 sizeof(attr_vp->vp_strvalue));
294 ptr2 = attr_vp->vp_strvalue;
297 for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
298 err = regexec(&preg, ptr2, REQUEST_MAX_REGEX, pmatch, 0);
299 if (err == REG_NOMATCH) {
301 DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
302 data->attribute, attr_vp->vp_strvalue);
310 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
311 data->attribute, attr_vp->vp_strvalue);
314 if (pmatch[0].rm_so == -1)
316 len = pmatch[0].rm_so;
318 len = len + (pmatch[0].rm_eo - pmatch[0].rm_so);
321 if (counter >= MAX_STRING_LEN) {
323 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
324 data->attribute, attr_vp->vp_strvalue);
328 memcpy(ptr, ptr2,len);
331 ptr2 += pmatch[0].rm_eo;
335 * We only run on the first match, sorry
337 for(j = 0; j <= REQUEST_MAX_REGEX; j++){
339 char buffer[sizeof(attr_vp->vp_strvalue)];
342 * Stolen from src/main/valuepair.c, paircompare()
346 * Delete old matches if the corresponding match does not
347 * exist in the current regex
349 if (pmatch[j].rm_so == -1){
350 p = request_data_get(request,request,REQUEST_DATA_REGEX | j);
358 attr_vp->vp_strvalue + pmatch[j].rm_so,
359 pmatch[j].rm_eo - pmatch[j].rm_so);
360 buffer[pmatch[j].rm_eo - pmatch[j].rm_so] = '\0';
362 request_data_add(request,request,REQUEST_DATA_REGEX | j,p,free);
367 if (data->replace_len != 0 &&
368 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
369 DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
372 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
376 counter += replace_len;
377 if (counter >= MAX_STRING_LEN) {
379 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
380 data->attribute, attr_vp->vp_strvalue);
384 memcpy(ptr, replace_STR, replace_len);
390 len = strlen(ptr2) + 1; /* We add the ending NULL */
392 if (counter >= MAX_STRING_LEN){
393 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
394 data->attribute, attr_vp->vp_strvalue);
397 memcpy(ptr, ptr2, len);
400 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
401 data->attribute, attr_vp->vp_strvalue, new_str);
402 if (pairparsevalue(attr_vp, new_str) == NULL) {
403 DEBUG2("rlm_attr_rewrite: Could not write value '%s' into attribute %s: %s", new_str, data->attribute, librad_errstr);
420 static int attr_rewrite_accounting(void *instance, REQUEST *request)
422 return do_attr_rewrite(instance, request);
425 static int attr_rewrite_authorize(void *instance, REQUEST *request)
427 return do_attr_rewrite(instance, request);
430 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
432 return do_attr_rewrite(instance, request);
435 static int attr_rewrite_preacct(void *instance, REQUEST *request)
437 return do_attr_rewrite(instance, request);
440 static int attr_rewrite_checksimul(void *instance, REQUEST *request)
442 return do_attr_rewrite(instance, request);
445 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
447 return do_attr_rewrite(instance, request);
450 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
452 return do_attr_rewrite(instance, request);
455 static int attr_rewrite_postauth(void *instance, REQUEST *request)
457 return do_attr_rewrite(instance, request);
460 static int attr_rewrite_detach(void *instance)
462 rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
472 * The module name should be the only globally exported symbol.
473 * That is, everything else should be 'static'.
475 * If the module needs to temporarily modify it's instantiation
476 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
477 * The server will then take care of ensuring that the module
478 * is single-threaded.
480 module_t rlm_attr_rewrite = {
483 RLM_TYPE_THREAD_UNSAFE, /* type */
484 attr_rewrite_instantiate, /* instantiation */
485 attr_rewrite_detach, /* detach */
487 attr_rewrite_authenticate, /* authentication */
488 attr_rewrite_authorize, /* authorization */
489 attr_rewrite_preacct, /* preaccounting */
490 attr_rewrite_accounting, /* accounting */
491 attr_rewrite_checksimul, /* checksimul */
492 attr_rewrite_preproxy, /* pre-proxy */
493 attr_rewrite_postproxy, /* post-proxy */
494 attr_rewrite_postauth /* post-auth */