Rename vp->lvalue to vp->vp_*, as appropriate.
[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         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         const char *instance_name = NULL;
73
74         /*
75          *      Set up a storage area for instance data
76          */
77         data = rad_malloc(sizeof(*data));
78         if (!data) {
79                 return -1;
80         }
81         memset(data, 0, sizeof(*data));
82
83         /*
84          *      If the configuration parameters can't be parsed, then
85          *      fail.
86          */
87         if (cf_section_parse(conf, data, module_config) < 0) {
88                 free(data);
89                 return -1;
90         }
91
92         /*
93          *      Discover the attribute number of the key.
94          */
95         if (data->attribute == NULL) {
96                 radlog(L_ERR, "rlm_attr_rewrite: 'attribute' must be set.");
97                 return -1;
98         }
99         if (data->search == NULL || data->replace == NULL) {
100                 radlog(L_ERR, "rlm_attr_rewrite: search/replace strings must be set.");
101                 return -1;
102         }
103         data->search_len = strlen(data->search);
104         data->replace_len = strlen(data->replace);
105
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.");
108                 return -1;
109         }
110
111         if (data->num_matches < 1 || data->num_matches > MAX_STRING_LEN) {
112                 radlog(L_ERR, "rlm_attr_rewrite: Illegal range for match number.");
113                 return -1;
114         }
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;
118         }
119         else{
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;
130                 else {
131                         radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
132                         data->searchin = RLM_REGEX_INPACKET;
133                 }
134                 free((char *)data->searchin_str);
135         }
136         dattr = dict_attrbyname(data->attribute);
137         if (dattr == NULL) {
138                 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
139                                 data->attribute);
140                 return -1;
141         }
142         data->attr_num = dattr->attr;
143         /* Add the module instance name */
144         data->name = NULL;
145         instance_name = cf_section_name2(conf);
146         if (instance_name != NULL)
147                 data->name = strdup(instance_name);
148
149
150         *instance = data;
151
152         return 0;
153 }
154
155 static int do_attr_rewrite(void *instance, REQUEST *request)
156 {
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;
161         regex_t preg;
162         regmatch_t pmatch[9];
163         int cflags = 0;
164         int err = 0;
165         char done_xlat = 0;
166         unsigned int len = 0;
167         char err_msg[MAX_STRING_LEN];
168         unsigned int i = 0;
169         unsigned int j = 0;
170         unsigned int counter = 0;
171         char new_str[MAX_STRING_LEN];
172         char *ptr, *ptr2;
173         char search_STR[MAX_STRING_LEN];
174         char replace_STR[MAX_STRING_LEN];
175         int replace_len = 0;
176
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;
180         }
181
182         if (data->new_attr){
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.");
186                         return ret;
187                 }
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);
193                         return ret;
194                 }
195                 switch(data->searchin){
196                         case RLM_REGEX_INPACKET:
197                                 pairadd(&request->packet->vps,attr_vp);
198                                 break;
199                         case RLM_REGEX_INCONFIG:
200                                 pairadd(&request->config_items,attr_vp);
201                                 break;
202                         case RLM_REGEX_INREPLY:
203                                 pairadd(&request->reply->vps,attr_vp);
204                                 break;
205                         case RLM_REGEX_INPROXY:
206                                 if (!request->proxy) {
207                                         pairbasicfree(attr_vp);
208                                         return RLM_MODULE_NOOP;
209                                 }
210                                 pairadd(&request->proxy->vps, attr_vp);
211                                 break;
212                         case RLM_REGEX_INPROXYREPLY:
213                                 if (!request->proxy_reply) {
214                                         pairbasicfree(attr_vp);
215                                         return RLM_MODULE_NOOP;
216                                 }
217                                 pairadd(&request->proxy_reply->vps, attr_vp);
218                                 break;
219                         default:
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);
223                                 break;
224                 }
225                 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,replace_STR);
226                 ret = RLM_MODULE_OK;
227         } else {
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;
235                                 else
236                                         tmp = request->packet->vps;
237                                 break;
238                         case RLM_REGEX_INCONFIG:
239                                 tmp = request->config_items;
240                                 break;
241                         case RLM_REGEX_INREPLY:
242                                 tmp = request->reply->vps;
243                                 break;
244                         case RLM_REGEX_INPROXYREPLY:
245                                 if (!request->proxy_reply)
246                                         return RLM_MODULE_NOOP;
247                                 tmp = request->proxy_reply->vps;
248                                 break;
249                         case RLM_REGEX_INPROXY:
250                                 if (!request->proxy)
251                                         return RLM_MODULE_NOOP;
252                                 tmp = request->proxy->vps;
253                                 break;
254                         default:
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);
258                                 break;
259                 }
260 do_again:
261                 if (tmp != NULL)
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);
265                         return ret;
266                 }
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);
269                         return ret;
270                 }
271                 cflags |= REG_EXTENDED;
272                 if (data->nocase)
273                         cflags |= REG_ICASE;
274
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.");
277                         return ret;
278                 }
279
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);
283                         return ret;
284                 }
285                 
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));
291                 }
292
293                 ptr = new_str;
294                 ptr2 = attr_vp->vp_strvalue;
295                 counter = 0;
296
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) {
300                                 if (i == 0) {
301                                         DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
302                                                         data->attribute, attr_vp->vp_strvalue);
303                                         regfree(&preg);
304                                         goto to_do_again;
305                                 } else
306                                         break;
307                         }
308                         if (err != 0) {
309                                 regfree(&preg);
310                                 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
311                                                 data->attribute, attr_vp->vp_strvalue);
312                                 return ret;
313                         }
314                         if (pmatch[0].rm_so == -1)
315                                 break;
316                         len = pmatch[0].rm_so;
317                         if (data->append) {
318                                 len = len + (pmatch[0].rm_eo - pmatch[0].rm_so);
319                         }
320                         counter += len;
321                         if (counter >= MAX_STRING_LEN) {
322                                 regfree(&preg);
323                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
324                                                 data->attribute, attr_vp->vp_strvalue);
325                                 return ret;
326                         }
327
328                         strlcpy(ptr, ptr2,len);
329                         ptr += len;
330                         ptr2 += pmatch[0].rm_eo;
331
332                         if (i == 0){
333                                 /*
334                                  * We only run on the first match, sorry
335                                  */
336                                 for(j = 0; j <= REQUEST_MAX_REGEX; j++){
337                                         char *p;
338                                         char buffer[sizeof(attr_vp->vp_strvalue)];
339
340                                         /*
341                                          * Stolen from src/main/valuepair.c, paircompare()
342                                          */
343
344                                         /*
345                                          * Delete old matches if the corresponding match does not
346                                          * exist in the current regex
347                                          */
348                                         if (pmatch[j].rm_so == -1){
349                                                 p = request_data_get(request,request,REQUEST_DATA_REGEX | j);
350                                                 if (p){
351                                                         free(p);
352                                                         continue;
353                                                 }
354                                                 break;
355                                         }
356                                         memcpy(buffer,
357                                                attr_vp->vp_strvalue + pmatch[j].rm_so,
358                                                pmatch[j].rm_eo - pmatch[j].rm_so);
359                                         buffer[pmatch[j].rm_eo - pmatch[j].rm_so] = '\0';
360                                         p = strdup(buffer);
361                                         request_data_add(request,request,REQUEST_DATA_REGEX | j,p,free);
362                                 }
363                         }
364
365                         if (!done_xlat){
366                                 if (data->replace_len != 0 &&
367                                 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
368                                         DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
369                                         return ret;
370                                 }
371                                 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
372                                 done_xlat = 1;
373                         }
374
375                         counter += replace_len;
376                         if (counter >= MAX_STRING_LEN) {
377                                 regfree(&preg);
378                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
379                                                 data->attribute, attr_vp->vp_strvalue);
380                                 return ret;
381                         }
382                         if (replace_len){
383                                 strlcpy(ptr, replace_STR, replace_len);
384                                 ptr += replace_len;
385                         }
386                 }
387                 regfree(&preg);
388                 len = strlen(ptr2) + 1;         /* We add the ending NULL */
389                 counter += len;
390                 if (counter >= MAX_STRING_LEN){
391                         DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
392                                         data->attribute, attr_vp->vp_strvalue);
393                         return ret;
394                 }
395                 strlcpy(ptr, ptr2, len);
396
397                 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
398                                 data->attribute, attr_vp->vp_strvalue, new_str);
399                 if (pairparsevalue(attr_vp, new_str) == NULL) {
400                         DEBUG2("rlm_attr_rewrite: Could not write value '%s' into attribute %s: %s", new_str, data->attribute, librad_errstr);
401                         return ret;
402                 }
403
404 to_do_again:
405                 ret = RLM_MODULE_OK;
406
407                 if (tmp != NULL){
408                         tmp = attr_vp->next;
409                         if (tmp != NULL)
410                                 goto do_again;
411                 }
412         }
413
414         return ret;
415 }
416
417 static int attr_rewrite_accounting(void *instance, REQUEST *request)
418 {
419         return do_attr_rewrite(instance, request);
420 }
421
422 static int attr_rewrite_authorize(void *instance, REQUEST *request)
423 {
424         return do_attr_rewrite(instance, request);
425 }
426
427 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
428 {
429         return do_attr_rewrite(instance, request);
430 }
431
432 static int attr_rewrite_preacct(void *instance, REQUEST *request)
433 {
434         return do_attr_rewrite(instance, request);
435 }
436
437 static int attr_rewrite_checksimul(void *instance, REQUEST *request)
438 {
439         return do_attr_rewrite(instance, request);
440 }
441
442 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
443 {
444         return do_attr_rewrite(instance, request);
445 }
446
447 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
448 {
449         return do_attr_rewrite(instance, request);
450 }
451
452 static int attr_rewrite_postauth(void *instance, REQUEST *request)
453 {
454         return do_attr_rewrite(instance, request);
455 }
456
457 static int attr_rewrite_detach(void *instance)
458 {
459         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
460
461         if (data->name)
462                 free(data->name);
463
464         free(instance);
465         return 0;
466 }
467
468 /*
469  *      The module name should be the only globally exported symbol.
470  *      That is, everything else should be 'static'.
471  *
472  *      If the module needs to temporarily modify it's instantiation
473  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
474  *      The server will then take care of ensuring that the module
475  *      is single-threaded.
476  */
477 module_t rlm_attr_rewrite = {
478         RLM_MODULE_INIT,
479         "attr_rewrite",
480         RLM_TYPE_THREAD_UNSAFE,         /* type */
481         attr_rewrite_instantiate,               /* instantiation */
482         attr_rewrite_detach,                    /* detach */
483         {
484                 attr_rewrite_authenticate,      /* authentication */
485                 attr_rewrite_authorize,         /* authorization */
486                 attr_rewrite_preacct,           /* preaccounting */
487                 attr_rewrite_accounting,        /* accounting */
488                 attr_rewrite_checksimul,        /* checksimul */
489                 attr_rewrite_preproxy,          /* pre-proxy */
490                 attr_rewrite_postproxy,         /* post-proxy */
491                 attr_rewrite_postauth           /* post-auth */
492         },
493 };