Rename librad_* to fr_*
[freeradius.git] / src / modules / rlm_attr_rewrite / rlm_attr_rewrite.c
1 /*
2  * rlm_attr_rewrite.c
3  *
4  * Version:  $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2002,2006  The FreeRADIUS server project
21  * Copyright 2002  Kostas Kalevras <kkalev@noc.ntua.gr>
22  */
23
24 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29
30 #ifdef HAVE_REGEX_H
31 #       include <regex.h>
32 #endif
33
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
39
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         const char *name;       /* The module name */
54 } rlm_attr_rewrite_t;
55
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 }
66 };
67
68 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
69 {
70         rlm_attr_rewrite_t *data;
71         DICT_ATTR *dattr;
72
73         /*
74          *      Set up a storage area for instance data
75          */
76         data = rad_malloc(sizeof(*data));
77         if (!data) {
78                 return -1;
79         }
80         memset(data, 0, sizeof(*data));
81
82         /*
83          *      If the configuration parameters can't be parsed, then
84          *      fail.
85          */
86         if (cf_section_parse(conf, data, module_config) < 0) {
87                 free(data);
88                 return -1;
89         }
90
91         /*
92          *      Discover the attribute number of the key.
93          */
94         if (data->attribute == NULL) {
95                 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
96                 return -1;
97         }
98         if (data->search == NULL || data->replace == NULL) {
99                 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
100                 return -1;
101         }
102         data->search_len = strlen(data->search);
103         data->replace_len = strlen(data->replace);
104
105         if (data->replace_len == 0 && data->new_attr){
106                 radlog(L_ERR, "rlm_attr_rewrite: replace string must not be zero length in order to create new attribute.");
107                 return -1;
108         }
109
110         if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
111                 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
112                 return -1;
113         }
114         if (data->searchin_str == NULL) {
115                 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
116                 data->searchin = RLM_REGEX_INPACKET;
117         }
118         else{
119                 if (strcmp(data->searchin_str, "packet") == 0)
120                         data->searchin = RLM_REGEX_INPACKET;
121                 else if (strcmp(data->searchin_str, "config") == 0)
122                         data->searchin = RLM_REGEX_INCONFIG;
123                 else if (strcmp(data->searchin_str, "reply") == 0)
124                         data->searchin = RLM_REGEX_INREPLY;
125                 else if (strcmp(data->searchin_str, "proxy") == 0)
126                         data->searchin = RLM_REGEX_INPROXY;
127                 else if (strcmp(data->searchin_str, "proxy_reply") == 0)
128                         data->searchin = RLM_REGEX_INPROXYREPLY;
129                 else {
130                         radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
131                         data->searchin = RLM_REGEX_INPACKET;
132                 }
133                 free((char *)data->searchin_str);
134         }
135         dattr = dict_attrbyname(data->attribute);
136         if (dattr == NULL) {
137                 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
138                                 data->attribute);
139                 return -1;
140         }
141         data->attr_num = dattr->attr;
142         /* Add the module instance name */
143         data->name = cf_section_name2(conf); /* may be NULL */
144
145         *instance = data;
146
147         return 0;
148 }
149
150 static int do_attr_rewrite(void *instance, REQUEST *request)
151 {
152         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
153         int ret = RLM_MODULE_NOOP;
154         VALUE_PAIR *attr_vp = NULL;
155         VALUE_PAIR *tmp = NULL;
156         regex_t preg;
157         regmatch_t pmatch[9];
158         int cflags = 0;
159         int err = 0;
160         char done_xlat = 0;
161         unsigned int len = 0;
162         char err_msg[MAX_STRING_LEN];
163         unsigned int i = 0;
164         unsigned int j = 0;
165         unsigned int counter = 0;
166         char new_str[MAX_STRING_LEN];
167         char *ptr, *ptr2;
168         char search_STR[MAX_STRING_LEN];
169         char replace_STR[MAX_STRING_LEN];
170         int replace_len = 0;
171
172         if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
173                 if (data->name == NULL || strcmp(data->name,attr_vp->vp_strvalue))
174                         return RLM_MODULE_NOOP;
175         }
176
177         if (data->new_attr){
178                 /* new_attribute = yes */
179                 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
180                         DEBUG2("%s: xlat on replace string failed.", data->name);
181                         return ret;
182                 }
183                 replace_len = strlen(replace_STR);
184                 attr_vp = pairmake(data->attribute,replace_STR,0);
185                 if (attr_vp == NULL){
186                         DEBUG2("%s: Could not add new attribute %s with value '%s'", data->name,
187                                 data->attribute,replace_STR);
188                         return ret;
189                 }
190                 switch(data->searchin){
191                         case RLM_REGEX_INPACKET:
192                                 pairadd(&request->packet->vps,attr_vp);
193                                 break;
194                         case RLM_REGEX_INCONFIG:
195                                 pairadd(&request->config_items,attr_vp);
196                                 break;
197                         case RLM_REGEX_INREPLY:
198                                 pairadd(&request->reply->vps,attr_vp);
199                                 break;
200                         case RLM_REGEX_INPROXY:
201                                 if (!request->proxy) {
202                                         pairbasicfree(attr_vp);
203                                         return RLM_MODULE_NOOP;
204                                 }
205                                 pairadd(&request->proxy->vps, attr_vp);
206                                 break;
207                         case RLM_REGEX_INPROXYREPLY:
208                                 if (!request->proxy_reply) {
209                                         pairbasicfree(attr_vp);
210                                         return RLM_MODULE_NOOP;
211                                 }
212                                 pairadd(&request->proxy_reply->vps, attr_vp);
213                                 break;
214                         default:
215                                 radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", data->name);
216                                 data->searchin = RLM_REGEX_INPACKET;
217                                 pairadd(&request->packet->vps,attr_vp);
218                                 break;
219                 }
220                 DEBUG2("%s: Added attribute %s with value '%s'", data->name,data->attribute,replace_STR);
221                 ret = RLM_MODULE_OK;
222         } else {
223                 /* new_attribute = no */
224                 switch (data->searchin) {
225                         case RLM_REGEX_INPACKET:
226                                 if (data->attr_num == PW_USER_NAME)
227                                         attr_vp = request->username;
228                                 else if (data->attr_num == PW_USER_PASSWORD)
229                                         attr_vp = request->password;
230                                 else
231                                         tmp = request->packet->vps;
232                                 break;
233                         case RLM_REGEX_INCONFIG:
234                                 tmp = request->config_items;
235                                 break;
236                         case RLM_REGEX_INREPLY:
237                                 tmp = request->reply->vps;
238                                 break;
239                         case RLM_REGEX_INPROXYREPLY:
240                                 if (!request->proxy_reply)
241                                         return RLM_MODULE_NOOP;
242                                 tmp = request->proxy_reply->vps;
243                                 break;
244                         case RLM_REGEX_INPROXY:
245                                 if (!request->proxy)
246                                         return RLM_MODULE_NOOP;
247                                 tmp = request->proxy->vps;
248                                 break;
249                         default:
250                                 radlog(L_ERR, "%s: Illegal value for searchin. Changing to packet.", data->name);
251                                 data->searchin = RLM_REGEX_INPACKET;
252                                 attr_vp = pairfind(request->packet->vps, data->attr_num);
253                                 break;
254                 }
255 do_again:
256                 if (tmp != NULL)
257                         attr_vp = pairfind(tmp, data->attr_num);
258                 if (attr_vp == NULL) {
259                         DEBUG2("%s: Could not find value pair for attribute %s", data->name,data->attribute);
260                         return ret;
261                 }
262                 if (attr_vp->vp_strvalue == NULL || attr_vp->length == 0){
263                         DEBUG2("%s: Attribute %s string value NULL or of zero length", data->name,data->attribute);
264                         return ret;
265                 }
266                 cflags |= REG_EXTENDED;
267                 if (data->nocase)
268                         cflags |= REG_ICASE;
269
270                 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
271                         DEBUG2("%s: xlat on search string failed.", data->name);
272                         return ret;
273                 }
274
275                 if ((err = regcomp(&preg,search_STR,cflags))) {
276                         regerror(err, &preg, err_msg, MAX_STRING_LEN);
277                         DEBUG2("%s: regcomp() returned error: %s", data->name,err_msg);
278                         return ret;
279                 }
280
281                 if ((attr_vp->type == PW_TYPE_IPADDR) &&
282                     (attr_vp->vp_strvalue[0] == '\0')) {
283                         inet_ntop(AF_INET, &(attr_vp->vp_ipaddr),
284                                   attr_vp->vp_strvalue,
285                                   sizeof(attr_vp->vp_strvalue));
286                 }
287
288                 ptr = new_str;
289                 ptr2 = attr_vp->vp_strvalue;
290                 counter = 0;
291
292                 for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
293                         err = regexec(&preg, ptr2, REQUEST_MAX_REGEX, pmatch, 0);
294                         if (err == REG_NOMATCH) {
295                                 if (i == 0) {
296                                         DEBUG2("%s: Does not match: %s = %s", data->name,
297                                                         data->attribute, attr_vp->vp_strvalue);
298                                         regfree(&preg);
299                                         goto to_do_again;
300                                 } else
301                                         break;
302                         }
303                         if (err != 0) {
304                                 regfree(&preg);
305                                 radlog(L_ERR, "%s: match failure for attribute %s with value '%s'", data->name,
306                                                 data->attribute, attr_vp->vp_strvalue);
307                                 return ret;
308                         }
309                         if (pmatch[0].rm_so == -1)
310                                 break;
311                         len = pmatch[0].rm_so;
312                         if (data->append) {
313                                 len = len + (pmatch[0].rm_eo - pmatch[0].rm_so);
314                         }
315                         counter += len;
316                         if (counter >= MAX_STRING_LEN) {
317                                 regfree(&preg);
318                                 DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
319                                                 data->attribute, attr_vp->vp_strvalue);
320                                 return ret;
321                         }
322
323                         memcpy(ptr, ptr2,len);
324                         ptr += len;
325                         *ptr = '\0';
326                         ptr2 += pmatch[0].rm_eo;
327
328                         if (i == 0){
329                                 /*
330                                  * We only run on the first match, sorry
331                                  */
332                                 for(j = 0; j <= REQUEST_MAX_REGEX; j++){
333                                         char *p;
334                                         char buffer[sizeof(attr_vp->vp_strvalue)];
335
336                                         /*
337                                          * Stolen from src/main/valuepair.c, paircompare()
338                                          */
339
340                                         /*
341                                          * Delete old matches if the corresponding match does not
342                                          * exist in the current regex
343                                          */
344                                         if (pmatch[j].rm_so == -1){
345                                                 p = request_data_get(request,request,REQUEST_DATA_REGEX | j);
346                                                 if (p){
347                                                         free(p);
348                                                         continue;
349                                                 }
350                                                 break;
351                                         }
352                                         memcpy(buffer,
353                                                attr_vp->vp_strvalue + pmatch[j].rm_so,
354                                                pmatch[j].rm_eo - pmatch[j].rm_so);
355                                         buffer[pmatch[j].rm_eo - pmatch[j].rm_so] = '\0';
356                                         p = strdup(buffer);
357                                         request_data_add(request,request,REQUEST_DATA_REGEX | j,p,free);
358                                 }
359                         }
360
361                         if (!done_xlat){
362                                 if (data->replace_len != 0 &&
363                                 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
364                                         DEBUG2("%s: xlat on replace string failed.", data->name);
365                                         return ret;
366                                 }
367                                 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
368                                 done_xlat = 1;
369                         }
370
371                         counter += replace_len;
372                         if (counter >= MAX_STRING_LEN) {
373                                 regfree(&preg);
374                                 DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
375                                                 data->attribute, attr_vp->vp_strvalue);
376                                 return ret;
377                         }
378                         if (replace_len){
379                                 memcpy(ptr, replace_STR, replace_len);
380                                 ptr += replace_len;
381                                 *ptr = '\0';
382                         }
383                 }
384                 regfree(&preg);
385                 len = strlen(ptr2) + 1;         /* We add the ending NULL */
386                 counter += len;
387                 if (counter >= MAX_STRING_LEN){
388                         DEBUG2("%s: Replacement out of limits for attribute %s with value '%s'", data->name,
389                                         data->attribute, attr_vp->vp_strvalue);
390                         return ret;
391                 }
392                 memcpy(ptr, ptr2, len);
393                 ptr[len] = '\0';
394
395                 DEBUG2("%s: Changed value for attribute %s from '%s' to '%s'", data->name,
396                                 data->attribute, attr_vp->vp_strvalue, new_str);
397                 if (pairparsevalue(attr_vp, new_str) == NULL) {
398                         DEBUG2("%s: Could not write value '%s' into attribute %s: %s", data->name, new_str, data->attribute, fr_strerror);
399                         return ret;
400                 }
401
402 to_do_again:
403                 ret = RLM_MODULE_OK;
404
405                 if (tmp != NULL){
406                         tmp = attr_vp->next;
407                         if (tmp != NULL)
408                                 goto do_again;
409                 }
410         }
411
412         return ret;
413 }
414
415 static int attr_rewrite_accounting(void *instance, REQUEST *request)
416 {
417         return do_attr_rewrite(instance, request);
418 }
419
420 static int attr_rewrite_authorize(void *instance, REQUEST *request)
421 {
422         return do_attr_rewrite(instance, request);
423 }
424
425 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
426 {
427         return do_attr_rewrite(instance, request);
428 }
429
430 static int attr_rewrite_preacct(void *instance, REQUEST *request)
431 {
432         return do_attr_rewrite(instance, request);
433 }
434
435 static int attr_rewrite_checksimul(void *instance, REQUEST *request)
436 {
437         return do_attr_rewrite(instance, request);
438 }
439
440 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
441 {
442         return do_attr_rewrite(instance, request);
443 }
444
445 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
446 {
447         return do_attr_rewrite(instance, request);
448 }
449
450 static int attr_rewrite_postauth(void *instance, REQUEST *request)
451 {
452         return do_attr_rewrite(instance, request);
453 }
454
455 static int attr_rewrite_detach(void *instance)
456 {
457         free(instance);
458         return 0;
459 }
460
461 /*
462  *      The module name should be the only globally exported symbol.
463  *      That is, everything else should be 'static'.
464  *
465  *      If the module needs to temporarily modify it's instantiation
466  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
467  *      The server will then take care of ensuring that the module
468  *      is single-threaded.
469  */
470 module_t rlm_attr_rewrite = {
471         RLM_MODULE_INIT,
472         "attr_rewrite",
473         RLM_TYPE_THREAD_UNSAFE,         /* type */
474         attr_rewrite_instantiate,               /* instantiation */
475         attr_rewrite_detach,                    /* detach */
476         {
477                 attr_rewrite_authenticate,      /* authentication */
478                 attr_rewrite_authorize,         /* authorization */
479                 attr_rewrite_preacct,           /* preaccounting */
480                 attr_rewrite_accounting,        /* accounting */
481                 attr_rewrite_checksimul,        /* checksimul */
482                 attr_rewrite_preproxy,          /* pre-proxy */
483                 attr_rewrite_postproxy,         /* post-proxy */
484                 attr_rewrite_postauth           /* post-auth */
485         },
486 };