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