The "proxy" and "proxy_reply" cases were missing when new_attribute
[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 "autoconf.h"
25 #include "libradius.h"
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #ifdef HAVE_REGEX_H
31 #       include <regex.h>
32 #endif
33
34 #include "radiusd.h"
35 #include "modules.h"
36 #include "conffile.h"
37
38 #define RLM_REGEX_INPACKET 0
39 #define RLM_REGEX_INCONFIG 1
40 #define RLM_REGEX_INREPLY  2
41 #define RLM_REGEX_INPROXY 3
42 #define RLM_REGEX_INPROXYREPLY 4
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,proxy,proxy_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 if (strcmp(data->searchin_str, "proxy") == 0)
135                         data->searchin = RLM_REGEX_INPROXY;
136                 else if (strcmp(data->searchin_str, "proxy_reply") == 0)
137                         data->searchin = RLM_REGEX_INPROXYREPLY;
138                 else {
139                         radlog(L_ERR, "rlm_attr_rewrite: Illegal searchin directive given. Assuming packet.");
140                         data->searchin = RLM_REGEX_INPACKET;
141                 }
142                 free((char *)data->searchin_str);
143         }
144         dattr = dict_attrbyname(data->attribute);
145         if (dattr == NULL) {
146                 radlog(L_ERR, "rlm_attr_rewrite: No such attribute %s",
147                                 data->attribute);
148                 return -1;
149         }
150         data->attr_num = dattr->attr;
151         /* Add the module instance name */
152         data->name = NULL;
153         instance_name = cf_section_name2(conf);
154         if (instance_name != NULL)
155                 data->name = strdup(instance_name);
156
157
158         *instance = data;
159
160         return 0;
161 }
162
163 static int do_attr_rewrite(void *instance, REQUEST *request)
164 {
165         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
166         int ret = RLM_MODULE_NOOP;
167         VALUE_PAIR *attr_vp = NULL;
168         VALUE_PAIR *tmp = NULL;
169         regex_t preg;
170         regmatch_t pmatch[9];
171         int cflags = 0;
172         int err = 0;
173         char done_xlat = 0;
174         unsigned int len = 0;
175         char err_msg[MAX_STRING_LEN];
176         unsigned int i = 0;
177         unsigned int j = 0;
178         unsigned int counter = 0;
179         char new_str[MAX_STRING_LEN];
180         char *ptr, *ptr2;
181         char search_STR[MAX_STRING_LEN];
182         char replace_STR[MAX_STRING_LEN];
183         int replace_len = 0;
184
185         if ((attr_vp = pairfind(request->config_items, PW_REWRITE_RULE)) != NULL){
186                 if (data->name == NULL || strcmp(data->name,attr_vp->strvalue))
187                         return RLM_MODULE_NOOP;
188         }
189
190         if (data->new_attr){
191                 /* new_attribute = yes */
192                 if (!radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL)) {
193                         DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
194                         return ret;
195                 }
196                 replace_len = strlen(replace_STR);
197                 attr_vp = pairmake(data->attribute,replace_STR,0);
198                 if (attr_vp == NULL){
199                         DEBUG2("rlm_attr_rewrite: Could not add new attribute %s with value '%s'",
200                                 data->attribute,replace_STR);
201                         return ret;
202                 }
203                 switch(data->searchin){
204                         case RLM_REGEX_INPACKET:
205                                 pairadd(&request->packet->vps,attr_vp);
206                                 break;
207                         case RLM_REGEX_INCONFIG:
208                                 pairadd(&request->config_items,attr_vp);
209                                 break;
210                         case RLM_REGEX_INREPLY:
211                                 pairadd(&request->reply->vps,attr_vp);
212                                 break;
213                         case RLM_REGEX_INPROXY:
214                                 if (!request->proxy)
215                                         return RLM_MODULE_NOOP;
216                                 pairadd(&request->proxy->vps, attr_vp);
217                                 break;
218                         case RLM_REGEX_INPROXYREPLY:
219                                 if (!request->proxy_reply)
220                                         return RLM_MODULE_NOOP;
221                                 pairadd(&request->proxy_reply->vps, attr_vp);
222                                 break;
223                         default:
224                                 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
225                                 data->searchin = RLM_REGEX_INPACKET;
226                                 pairadd(&request->packet->vps,attr_vp);
227                                 break;
228                 }
229                 DEBUG2("rlm_attr_rewrite: Added attribute %s with value '%s'",data->attribute,replace_STR);
230                 ret = RLM_MODULE_OK;
231         } else {
232                 /* new_attribute = no */
233                 switch (data->searchin) {
234                         case RLM_REGEX_INPACKET:
235                                 if (data->attr_num == PW_USER_NAME)
236                                         attr_vp = request->username;
237                                 else if (data->attr_num == PW_PASSWORD)
238                                         attr_vp = request->password;
239                                 else
240                                         tmp = request->packet->vps;
241                                 break;
242                         case RLM_REGEX_INCONFIG:
243                                 tmp = request->config_items;
244                                 break;
245                         case RLM_REGEX_INREPLY:
246                                 tmp = request->reply->vps;
247                                 break;
248                         case RLM_REGEX_INPROXYREPLY:
249                                 if (!request->proxy_reply)
250                                         return RLM_MODULE_NOOP;
251                                 tmp = request->proxy_reply->vps;
252                                 break;
253                         case RLM_REGEX_INPROXY:
254                                 if (!request->proxy)
255                                         return RLM_MODULE_NOOP;
256                                 tmp = request->proxy->vps;
257                                 break;
258                         default:
259                                 radlog(L_ERR, "rlm_attr_rewrite: Illegal value for searchin. Changing to packet.");
260                                 data->searchin = RLM_REGEX_INPACKET;
261                                 attr_vp = pairfind(request->packet->vps, data->attr_num);
262                                 break;
263                 }
264 do_again:
265                 if (tmp != NULL)
266                         attr_vp = pairfind(tmp, data->attr_num);
267                 if (attr_vp == NULL) {
268                         DEBUG2("rlm_attr_rewrite: Could not find value pair for attribute %s",data->attribute);
269                         return ret;
270                 }
271                 if (attr_vp->strvalue == NULL || attr_vp->length == 0){
272                         DEBUG2("rlm_attr_rewrite: Attribute %s string value NULL or of zero length",data->attribute);
273                         return ret;
274                 }
275                 cflags |= REG_EXTENDED;
276                 if (data->nocase)
277                         cflags |= REG_ICASE;
278
279                 if (!radius_xlat(search_STR, sizeof(search_STR), data->search, request, NULL) && data->search_len != 0) {
280                         DEBUG2("rlm_attr_rewrite: xlat on search string failed.");
281                         return ret;
282                 }
283
284                 if ((err = regcomp(&preg,search_STR,cflags))) {
285                         regerror(err, &preg, err_msg, MAX_STRING_LEN);
286                         DEBUG2("rlm_attr_rewrite: regcomp() returned error: %s",err_msg);
287                         return ret;
288                 }
289                 ptr = new_str;
290                 ptr2 = attr_vp->strvalue;
291                 counter = 0;
292
293                 for ( i = 0 ;i < (unsigned)data->num_matches; i++) {
294                         err = regexec(&preg, ptr2, 9, pmatch, 0);
295                         if (err == REG_NOMATCH) {
296                                 if (i == 0) {
297                                         DEBUG2("rlm_attr_rewrite: No match found for attribute %s with value '%s'",
298                                                         data->attribute, attr_vp->strvalue);
299                                         regfree(&preg);
300                                         goto to_do_again;
301                                 } else
302                                         break;
303                         }
304                         if (err != 0) {
305                                 regfree(&preg);
306                                 radlog(L_ERR, "rlm_attr_rewrite: match failure for attribute %s with value '%s'",
307                                                 data->attribute, attr_vp->strvalue);
308                                 return ret;
309                         }
310                         if (pmatch[0].rm_so == -1)
311                                 break;
312                         len = pmatch[0].rm_so;
313                         if (data->append) {
314                                 len = len + (pmatch[0].rm_eo - pmatch[0].rm_so);
315                         }
316                         counter += len;
317                         if (counter >= MAX_STRING_LEN) {
318                                 regfree(&preg);
319                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
320                                                 data->attribute, attr_vp->strvalue);
321                                 return ret;
322                         }
323
324                         strncpy(ptr, ptr2,len);
325                         ptr += len;
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 <= 8; j++){
333                                         char *p;
334                                         char buffer[sizeof(attr_vp->strvalue)];
335
336                                         /*
337                                          * Stolen from src/main/valuepair.c, paircmp()
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->strvalue + pmatch[j].rm_so,pmatch[j].rm_eo);
354                                         buffer[pmatch[j].rm_eo] = '\0';
355                                         p = strdup(buffer);
356                                         request_data_add(request,request,REQUEST_DATA_REGEX | j,p,free);
357                                 }
358                         }
359
360                         if (!done_xlat){
361                                 if (data->replace_len != 0 &&
362                                 radius_xlat(replace_STR, sizeof(replace_STR), data->replace, request, NULL) == 0) {
363                                         DEBUG2("rlm_attr_rewrite: xlat on replace string failed.");
364                                         return ret;
365                                 }
366                                 replace_len = (data->replace_len != 0) ? strlen(replace_STR) : 0;
367                                 done_xlat = 1;
368                         }
369
370                         counter += replace_len;
371                         if (counter >= MAX_STRING_LEN) {
372                                 regfree(&preg);
373                                 DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
374                                                 data->attribute, attr_vp->strvalue);
375                                 return ret;
376                         }
377                         if (replace_len){
378                                 strncpy(ptr, replace_STR, replace_len);
379                                 ptr += replace_len;
380                         }
381                 }
382                 regfree(&preg);
383                 len = strlen(ptr2) + 1;         /* We add the ending NULL */
384                 counter += len;
385                 if (counter >= MAX_STRING_LEN){
386                         DEBUG2("rlm_attr_rewrite: Replacement out of limits for attribute %s with value '%s'",
387                                         data->attribute, attr_vp->strvalue);
388                         return ret;
389                 }
390                 strncpy(ptr, ptr2, len);
391
392                 DEBUG2("rlm_attr_rewrite: Changed value for attribute %s from '%s' to '%s'",
393                                 data->attribute, attr_vp->strvalue, new_str);
394                 if (pairparsevalue(attr_vp, new_str) == NULL) {
395                         DEBUG2("rlm_attr_rewrite: Could not write value '%s' into attribute %s: %s", new_str, data->attribute, librad_errstr);
396                         return ret;
397                 }
398
399 to_do_again:
400                 ret = RLM_MODULE_OK;
401
402                 if (tmp != NULL){
403                         tmp = attr_vp->next;
404                         if (tmp != NULL)
405                                 goto do_again;
406                 }
407         }
408
409         return ret;
410 }
411
412
413 static int attr_rewrite_accounting(void *instance, REQUEST *request)
414 {
415         return do_attr_rewrite(instance, request);
416 }
417
418 static int attr_rewrite_authorize(void *instance, REQUEST *request)
419 {
420         return do_attr_rewrite(instance, request);
421 }
422 static int attr_rewrite_authenticate(void *instance, REQUEST *request)
423 {
424         return do_attr_rewrite(instance, request);
425 }
426 static int attr_rewrite_preacct(void *instance, REQUEST *request)
427 {
428         return do_attr_rewrite(instance, request);
429 }
430 static int attr_rewrite_ismul(void *instance, REQUEST *request)
431 {
432         return do_attr_rewrite(instance, request);
433 }
434
435 static int attr_rewrite_preproxy(void *instance, REQUEST *request)
436 {
437         return do_attr_rewrite(instance, request);
438 }
439
440 static int attr_rewrite_postproxy(void *instance, REQUEST *request)
441 {
442         return do_attr_rewrite(instance, request);
443 }
444
445 static int attr_rewrite_postauth(void *instance, REQUEST *request)
446 {
447         return do_attr_rewrite(instance, request);
448 }
449
450 static int attr_rewrite_detach(void *instance)
451 {
452         rlm_attr_rewrite_t *data = (rlm_attr_rewrite_t *) instance;
453
454         if (data->attribute)
455                 free(data->attribute);
456         if (data->search)
457                 free(data->search);
458         if (data->replace)
459                 free(data->replace);
460         if (data->name)
461                 free(data->name);
462
463         free(instance);
464         return 0;
465 }
466
467 /*
468  *      The module name should be the only globally exported symbol.
469  *      That is, everything else should be 'static'.
470  *
471  *      If the module needs to temporarily modify it's instantiation
472  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
473  *      The server will then take care of ensuring that the module
474  *      is single-threaded.
475  */
476 module_t rlm_attr_rewrite = {
477         "attr_rewrite",
478         RLM_TYPE_THREAD_UNSAFE,         /* type */
479         NULL,                           /* initialization */
480         attr_rewrite_instantiate,               /* instantiation */
481         {
482                 attr_rewrite_authenticate,      /* authentication */
483                 attr_rewrite_authorize,         /* authorization */
484                 attr_rewrite_preacct,           /* preaccounting */
485                 attr_rewrite_accounting,        /* accounting */
486                 attr_rewrite_ismul,             /* checksimul */
487                 attr_rewrite_preproxy,          /* pre-proxy */
488                 attr_rewrite_postproxy,         /* post-proxy */
489                 attr_rewrite_postauth           /* post-auth */
490         },
491         attr_rewrite_detach,                    /* detach */
492         NULL,                           /* destroy */
493 };