Also be able to search in the proxy and proxy_reply structures in rlm_attr_rewrite
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2002  The FreeRADIUS server project
21  * Copyright 2002  Kostas Kalevras <kkalev@noc.ntua.gr>
22  */
23
24 #include "config.h"
25 #include "autoconf.h"
26 #include "libradius.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include "config.h"
32 #ifdef HAVE_REGEX_H
33 #       include <regex.h>
34 #endif
35
36 #include "radiusd.h"
37 #include "modules.h"
38 #include "conffile.h"
39
40 #define RLM_REGEX_INPACKET 0
41 #define RLM_REGEX_INCONFIG 1
42 #define RLM_REGEX_INREPLY  2
43 #define RLM_REGEX_INPROXY 3
44 #define RLM_REGEX_INPROXYREPLY 4
45
46 static const char rcsid[] = "$Id$";
47
48 typedef struct rlm_attr_rewrite_t {
49         char *attribute;        /* The attribute to search for */
50         int  attr_num;          /* The attribute number */
51         char *search;           /* The pattern to search for */
52         int search_len;         /* The length of the search pattern */
53         char *searchin_str;     /* The VALUE_PAIR list to search in. Can be either packet,reply,proxy,proxy_reply or config */
54         char searchin;          /* The same as above just coded as a number for speed */
55         char *replace;          /* The replacement */
56         int replace_len;        /* The length of the replacement string */
57         int  append;            /* Switch to control append mode (1,0) */ 
58         int  nocase;            /* Ignore case */
59         int  new_attr;          /* Boolean. Do we create a new attribute or not? */
60         int  num_matches;       /* Maximum number of matches */
61         char *name;             /* The module name */
62 } rlm_attr_rewrite_t;
63
64
65 static CONF_PARSER module_config[] = {
66   { "attribute", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,attribute), NULL, NULL },
67   { "searchfor", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,search), NULL, NULL },
68   { "searchin",  PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,searchin_str), NULL, "packet" },
69   { "replacewith", PW_TYPE_STRING_PTR, offsetof(rlm_attr_rewrite_t,replace), NULL, NULL },
70   { "append", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,append),NULL, "no" },
71   { "ignore_case", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,nocase), NULL, "yes" },
72   { "new_attribute", PW_TYPE_BOOLEAN, offsetof(rlm_attr_rewrite_t,new_attr), NULL, "no" },
73   { "max_matches", PW_TYPE_INTEGER, offsetof(rlm_attr_rewrite_t,num_matches), NULL, "10" },
74   { NULL, -1, 0, NULL, NULL }
75 };
76
77
78 static int attr_rewrite_instantiate(CONF_SECTION *conf, void **instance)
79 {
80         rlm_attr_rewrite_t *data;
81         DICT_ATTR *dattr;
82         char *instance_name = NULL;
83         
84         /*
85          *      Set up a storage area for instance data
86          */
87         data = rad_malloc(sizeof(*data));
88         if (!data) {
89                 return -1;
90         }
91         memset(data, 0, sizeof(*data));
92
93         /*
94          *      If the configuration parameters can't be parsed, then
95          *      fail.
96          */
97         if (cf_section_parse(conf, data, module_config) < 0) {
98                 free(data);
99                 return -1;
100         }
101
102         /*
103          *      Discover the attribute number of the key. 
104          */
105         if (data->attribute == NULL) {
106                 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
107                 return -1;
108         }
109         if (data->search == NULL || data->replace == NULL) {
110                 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
111                 return -1;
112         }
113         data->search_len = strlen(data->search);
114         data->replace_len = strlen(data->replace);
115
116         if (data->replace_len == 0 && data->new_attr){
117                 radlog(L_ERR, "rlm_attr_rewrite: replace string must not be zero length in order to create new attribute.");
118                 return -1;
119         }
120
121         if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
122                 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
123                 return -1;
124         }
125         if (data->searchin_str == NULL) {
126                 radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
127                 data->searchin = RLM_REGEX_INPACKET;
128         }
129         else{
130                 if (strcmp(data->searchin_str, "packet") == 0)
131                         data->searchin = RLM_REGEX_INPACKET;
132                 else if (strcmp(data->searchin_str, "config") == 0)
133                         data->searchin = RLM_REGEX_INCONFIG;
134                 else if (strcmp(data->searchin_str, "reply") == 0)
135                         data->searchin = RLM_REGEX_INREPLY;
136                 else if (strcmp(data->searchin_str, "proxy") == 0)
137                         data->searchin = RLM_REGEX_INPROXY;
138                 else if (strcmp(data->searchin_str, "proxy_reply") == 0)
139                         data->searchin = RLM_REGEX_INPROXYREPLY;
140                 else {
141                         radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
142                         data->searchin = RLM_REGEX_INPACKET;
143                 }
144                 free((char *)data->searchin_str);
145         }
146         dattr = dict_attrbyname(data->attribute);
147         if (dattr == NULL) {
148                 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
149                                 data->attribute);
150                 return -1;
151         }
152         data->attr_num = dattr->attr;
153         /* Add the module instance name */
154         data->name = NULL;
155         instance_name = cf_section_name2(conf);
156         if (instance_name != NULL)
157                 data->name = strdup(instance_name);
158         
159         
160         *instance = data;
161         
162         return 0;
163 }
164
165 static int do_attr_rewrite(void *instance, REQUEST *request)
166 {
167         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
168         int ret = RLM_MODULE_NOOP;
169         VALUE_PAIR *attr_vp = NULL;
170         VALUE_PAIR *tmp = NULL;
171         regex_t preg;
172         regmatch_t pmatch;
173         int cflags = 0;
174         int err = 0;
175         char done_xlat = 0;
176         unsigned int len = 0;
177         char err_msg[MAX_STRING_LEN];
178         unsigned int i = 0;
179         unsigned int counter = 0;
180         char new_str[MAX_STRING_LEN];
181         char *ptr, *ptr2;
182         char search_STR[MAX_STRING_LEN];
183         char replace_STR[MAX_STRING_LEN];
184         int replace_len = 0;
185
186         if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
187                 if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
188                         return RLM_MODULE_NOOP;
189         }
190
191         if (!data->new_attr){
192                 switch (data->searchin) {
193                         case RLM_REGEX_INPACKET:
194                                 if (data->attr_num == PW_USER_NAME)
195                                         attr_vp = request->username;
196                                 else if (data->attr_num == PW_PASSWORD)
197                                         attr_vp = request->password;
198                                 else
199                                         tmp = request->packet->vps;
200                                 break;
201                         case RLM_REGEX_INCONFIG:
202                                 tmp = request->config_items;
203                                 break;
204                         case RLM_REGEX_INREPLY:
205                                 tmp = request->reply->vps;
206                                 break;
207                         case RLM_REGEX_INPROXY:
208                                 tmp = request->proxy_reply->vps;
209                                 break;
210                         case RLM_REGEX_INPROXYREPLY:
211                                 tmp = request->proxy->vps;
212                                 break;
213                         default:
214                                 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
215                                 data->searchin = RLM_REGEX_INPACKET;
216                                 attr_vp = pairfind(request->packet->vps, data->attr_num);
217                                 break;
218                 }
219 do_again:
220                 if (tmp != NULL)
221                         attr_vp = pairfind(tmp, data->attr_num);
222                 if (attr_vp == NULL) {
223                         DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
224                         return ret;
225                 }
226                 if (attr_vp->strvalue == NULL || attr_vp->length == 0){
227                         DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
228                         return ret;
229                 }
230                 cflags |= REG_EXTENDED;
231                 if (data->nocase)
232                         cflags |= REG_ICASE;
233
234                 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
235                         DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
236                         return ret;
237                 }
238         }
239         if (data->new_attr){
240                 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
241                         DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
242                         return ret;
243                 }
244                 replace_len = strlen(replace_STR);
245         }
246
247         if (!data->new_attr){
248                 if ((err = regcomp(&preg,search_STR,cflags))) {
249                         regerror(err, &preg, err_msg, MAX_STRING_LEN);
250                         DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
251                         return ret;
252                 }
253                 ptr = new_str;
254                 ptr2 = attr_vp->strvalue;
255                 counter = 0;
256
257                 for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
258                         err = regexec(&preg, ptr2, 1, &pmatch, 0);
259                         if (err == REG_NOMATCH) {
260                                 if (i == 0) {
261                                         DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
262                                                         data->attribute, attr_vp->strvalue);
263                                         regfree(&preg);
264                                         goto to_do_again;
265                                 } else
266                                         break;
267                         }
268                         if (err != 0) {
269                                 regfree(&preg);
270                                 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
271                                                 data->attribute, attr_vp->strvalue);
272                                 return ret;
273                         }
274                         if (pmatch.rm_so == -1)
275                                 break;
276                         len = pmatch.rm_so;
277                         if (data->append) {
278                                 len = len + (pmatch.rm_eo - pmatch.rm_so);
279                         } 
280                         counter += len;
281                         if (counter >= MAX_STRING_LEN) {
282                                 regfree(&preg);
283                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
284                                                 data->attribute, attr_vp->strvalue);    
285                                 return ret;
286                         }
287
288                         strncpy(ptr, ptr2,len);
289                         ptr += len;
290                         ptr2 += pmatch.rm_eo;
291
292                         if (!done_xlat){
293                                 if (data->replace_len != 0 &&
294                                 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
295                                         DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
296                                         return ret;
297                                 }
298                                 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
299                                 done_xlat = 1;
300                         }
301
302                         counter += replace_len;
303                         if (counter >= MAX_STRING_LEN) {
304                                 regfree(&preg);
305                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
306                                                 data->attribute, attr_vp->strvalue);    
307                                 return ret;
308                         }
309                         if (replace_len){
310                                 strncpy(ptr, replace_STR, replace_len);
311                                 ptr += replace_len;     
312                         }
313                 }
314                 regfree(&preg);
315                 len = strlen(ptr2) + 1;         /* We add the ending NULL */
316                 counter += len;
317                 if (counter >= MAX_STRING_LEN){
318                         DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
319                                         data->attribute, attr_vp->strvalue);    
320                         return ret;
321                 }
322                 strncpy(ptr, ptr2, len);
323
324                 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
325                                 data->attribute, attr_vp->strvalue, new_str);
326                 attr_vp->length = strlen(new_str);
327                 strncpy(attr_vp->strvalue, new_str, (attr_vp->length + 1));
328
329 to_do_again:
330                 ret = RLM_MODULE_OK;
331
332                 if (tmp != NULL){
333                         tmp = attr_vp->next;
334                         if (tmp != NULL)
335                                 goto do_again;
336                 }
337         }
338         else{
339                 attr_vp = pairmake(data->attribute,replace_STR,0);
340                 switch(data->searchin){
341                         case RLM_REGEX_INPACKET:
342                                 pairadd(&request->packet->vps,attr_vp);
343                                 break;
344                         case RLM_REGEX_INCONFIG:
345                                 pairadd(&request->config_items,attr_vp);
346                                 break;
347                         case RLM_REGEX_INREPLY:
348                                 pairadd(&request->reply->vps,attr_vp);
349                                 break;
350                         default:
351                                 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
352                                 data->searchin = RLM_REGEX_INPACKET;
353                                 pairadd(&request->packet->vps,attr_vp);
354                                 break;
355                 }
356                 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,replace_STR);
357                 ret = RLM_MODULE_OK;
358         }
359                                 
360
361         return ret;
362 }
363
364
365 static int attr_rewrite_accounting(void *instance, REQUEST *request)
366 {
367         return do_attr_rewrite(instance, request);
368 }
369
370 static int attr_rewrite_authorize(void *instance, REQUEST *request)
371 {
372         return do_attr_rewrite(instance, request);
373 }
374 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
375 {
376         return do_attr_rewrite(instance, request);
377 }
378 static int attr_rewrite_preacct(void *instance, REQUEST *request)
379 {
380         return do_attr_rewrite(instance, request);
381 }
382 static int attr_rewrite_ismul(void *instance, REQUEST *request)
383 {
384         return do_attr_rewrite(instance, request);
385 }
386
387 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
388 {
389         return do_attr_rewrite(instance, request);
390 }
391
392 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
393 {
394         return do_attr_rewrite(instance, request);
395 }
396
397 static int attr_rewrite_postauth(void *instance, REQUEST *request)
398 {
399         return do_attr_rewrite(instance, request);
400 }
401
402 static int attr_rewrite_detach(void *instance)
403 {
404         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
405
406         if (data->attribute)
407                 free(data->attribute);
408         if (data->search)
409                 free(data->search);
410         if (data->replace)      
411                 free(data->replace);
412         if (data->name)
413                 free(data->name);
414
415         free(instance);
416         return 0;
417 }
418
419 /*
420  *      The module name should be the only globally exported symbol.
421  *      That is, everything else should be 'static'.
422  *
423  *      If the module needs to temporarily modify it's instantiation
424  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
425  *      The server will then take care of ensuring that the module
426  *      is single-threaded.
427  */
428 module_t rlm_attr_rewrite = {
429         "attr_rewrite", 
430         RLM_TYPE_THREAD_UNSAFE,         /* type */
431         NULL,                           /* initialization */
432         attr_rewrite_instantiate,               /* instantiation */
433         {
434                 attr_rewrite_authenticate,      /* authentication */
435                 attr_rewrite_authorize,         /* authorization */
436                 attr_rewrite_preacct,           /* preaccounting */
437                 attr_rewrite_accounting,        /* accounting */
438                 attr_rewrite_ismul,             /* checksimul */
439                 attr_rewrite_preproxy,          /* pre-proxy */
440                 attr_rewrite_postproxy,         /* post-proxy */
441                 attr_rewrite_postauth           /* post-auth */
442         },
443         attr_rewrite_detach,                    /* detach */
444         NULL,                           /* destroy */
445 };