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