Automatic search and replace for pairfind.
[freeradius.git] / src / modules / rlm_realm / rlm_realm.c
1 /*
2  * rlm_realm.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 2000,2006  The FreeRADIUS server project
21  * FIXME add copyrights
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 #define  REALM_FORMAT_PREFIX   0
31 #define  REALM_FORMAT_SUFFIX   1
32
33 typedef struct realm_config_t {
34         int        format;
35         char       *formatstring;
36         char       *delim;
37         int        ignore_default;
38         int        ignore_null;
39 } realm_config_t;
40
41 static CONF_PARSER module_config[] = {
42   { "format", PW_TYPE_STRING_PTR,
43     offsetof(realm_config_t,formatstring), NULL, "suffix" },
44   { "delimiter", PW_TYPE_STRING_PTR,
45     offsetof(realm_config_t,delim), NULL, "@" },
46   { "ignore_default", PW_TYPE_BOOLEAN,
47     offsetof(realm_config_t,ignore_default), NULL, "no" },
48   { "ignore_null", PW_TYPE_BOOLEAN,
49     offsetof(realm_config_t,ignore_null), NULL, "no" },
50   { NULL, -1, 0, NULL, NULL }    /* end the list */
51 };
52
53 /*
54  *      Internal function to cut down on duplicated code.
55  *
56  *      Returns -1 on failure, 0 on no failure.  returnrealm
57  *      is NULL on don't proxy, realm otherwise.
58  */
59 static int check_for_realm(void *instance, REQUEST *request, REALM **returnrealm)
60 {
61         char namebuf[MAX_STRING_LEN];
62         char *username;
63         char *realmname = NULL;
64         char *ptr;
65         VALUE_PAIR *vp;
66         REALM *realm;
67
68         struct realm_config_t *inst = instance;
69
70         /* initiate returnrealm */
71         *returnrealm = NULL;
72
73         /*
74          *      If the request has a proxy entry, then it's a proxy
75          *      reply, and we're walking through the module list again.
76          *
77          *      In that case, don't bother trying to proxy the request
78          *      again.
79          *
80          *      Also, if there's no User-Name attribute, we can't
81          *      proxy it, either.
82          */
83         if ((request->proxy != NULL) ||
84             (request->username == NULL)) {
85                 RDEBUG2("Proxy reply, or no User-Name.  Ignoring.");
86                 return RLM_MODULE_OK;
87         }
88
89         /*
90          *      Check for 'Realm' attribute.  If it exists, then we've proxied
91          *      it already ( via another rlm_realm instance ) and should return.
92          */
93
94         if (pairfind(request->packet->vps, PW_REALM, 0) != NULL ) {
95                 RDEBUG2("Request already proxied.  Ignoring.");
96                 return RLM_MODULE_OK;
97         }
98
99         /*
100          *      We will be modifing this later, so we want our own copy
101          *      of it.
102          */
103         strlcpy(namebuf, (char *)request->username->vp_strvalue, sizeof(namebuf));
104         username = namebuf;
105
106         switch(inst->format)
107         {
108
109         case REALM_FORMAT_SUFFIX:
110
111           /* DEBUG2("  rlm_realm: Checking for suffix after \"%c\"", inst->delim[0]); */
112                 realmname = strrchr(username, inst->delim[0]);
113                 if (realmname) {
114                         *realmname = '\0';
115                         realmname++;
116                 }
117                 break;
118
119         case REALM_FORMAT_PREFIX:
120
121                 /* DEBUG2("  rlm_realm: Checking for prefix before \"%c\"", inst->delim[0]); */
122
123                 ptr = strchr(username, inst->delim[0]);
124                 if (ptr) {
125                         *ptr = '\0';
126                      ptr++;
127                      realmname = username;
128                      username = ptr;
129                 }
130                 break;
131
132         default:
133                 realmname = NULL;
134                 break;
135         }
136
137         /*
138          *      Print out excruciatingly descriptive debugging messages
139          *      for the people who find it too difficult to think about
140          *      what's going on.
141          */
142         if (realmname) {
143                 RDEBUG2("Looking up realm \"%s\" for User-Name = \"%s\"",
144                        realmname, request->username->vp_strvalue);
145         } else {
146                 if( inst->ignore_null ) {
147                         RDEBUG2("No '%c' in User-Name = \"%s\", skipping NULL due to config.",
148                         inst->delim[0], request->username->vp_strvalue);
149                         return RLM_MODULE_NOOP;
150                 }
151                 RDEBUG2("No '%c' in User-Name = \"%s\", looking up realm NULL",
152                        inst->delim[0], request->username->vp_strvalue);
153         }
154
155         /*
156          *      Allow DEFAULT realms unless told not to.
157          */
158         realm = realm_find(realmname);
159         if (!realm) {
160                 RDEBUG2("No such realm \"%s\"",
161                        (realmname == NULL) ? "NULL" : realmname);
162                 return RLM_MODULE_NOOP;
163         }
164         if( inst->ignore_default &&
165             (strcmp(realm->name, "DEFAULT")) == 0) {
166                 RDEBUG2("Found DEFAULT, but skipping due to config.");
167                 return RLM_MODULE_NOOP;
168         }
169
170         RDEBUG2("Found realm \"%s\"", realm->name);
171
172         /*
173          *      If we've been told to strip the realm off, then do so.
174          */
175         if (realm->striprealm) {
176                 /*
177                  *      Create the Stripped-User-Name attribute, if it
178                  *      doesn't exist.
179                  *
180                  */
181                 if (request->username->attribute != PW_STRIPPED_USER_NAME) {
182                         vp = radius_paircreate(request, &request->packet->vps,
183                                                PW_STRIPPED_USER_NAME,
184                                                PW_TYPE_STRING);
185                         RDEBUG2("Adding Stripped-User-Name = \"%s\"", username);
186                 } else {
187                         vp = request->username;
188                         RDEBUG2("Setting Stripped-User-Name = \"%s\"", username);
189                 }
190
191                 strcpy(vp->vp_strvalue, username);
192                 vp->length = strlen((char *)vp->vp_strvalue);
193                 request->username = vp;
194         }
195
196         /*
197          *      Add the realm name to the request.
198          *      If the realm is a regex, the use the realm as entered
199          *      by the user.  Otherwise, use the configured realm name,
200          *      as realm name comparison is case insensitive.  We want
201          *      to use the configured name, rather than what the user
202          *      entered.
203          */
204         if (realm->name[0] != '~') realmname = realm->name;
205         pairadd(&request->packet->vps, pairmake("Realm", realmname,
206                                                 T_OP_EQ));
207         RDEBUG2("Adding Realm = \"%s\"", realmname);
208
209         /*
210          *      Figure out what to do with the request.
211          */
212         switch (request->packet->code) {
213         default:
214                 RDEBUG2("Unknown packet code %d\n",
215                        request->packet->code);
216                 return RLM_MODULE_OK;           /* don't do anything */
217
218                 /*
219                  *      Perhaps accounting proxying was turned off.
220                  */
221         case PW_ACCOUNTING_REQUEST:
222                 if (!realm->acct_pool) {
223                         RDEBUG2("Accounting realm is LOCAL.");
224                         return RLM_MODULE_OK;
225                 }
226                 break;
227
228                 /*
229                  *      Perhaps authentication proxying was turned off.
230                  */
231         case PW_AUTHENTICATION_REQUEST:
232                 if (!realm->auth_pool) {
233                         RDEBUG2("Authentication realm is LOCAL.");
234                         return RLM_MODULE_OK;
235                 }
236                 break;
237         }
238
239         RDEBUG2("Proxying request from user %s to realm %s",
240                username, realm->name);
241
242         /*
243          *      Skip additional checks if it's not an accounting
244          *      request.
245          */
246         if (request->packet->code != PW_ACCOUNTING_REQUEST) {
247                 *returnrealm = realm;
248                 return RLM_MODULE_UPDATED;
249         }
250
251         /*
252          *      FIXME: Each server should have a unique server key,
253          *      and put it in the accounting packet.  Every server
254          *      should know about the keys, and NOT proxy requests to
255          *      a server with key X if the packet already contains key
256          *      X.
257          */
258
259         /*
260          *      If this request has arrived from another freeradius server
261          *      that has already proxied the request, we don't need to do
262          *      it again.
263          */
264         vp = pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO, 0);
265         if (vp && (request->packet->src_ipaddr.af == AF_INET)) {
266                 int i;
267                 fr_ipaddr_t my_ipaddr;
268
269                 my_ipaddr.af = AF_INET;
270                 my_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
271
272                 /*
273                  *      Loop over the home accounting servers for this
274                  *      realm.  If one of them has the same IP as the
275                  *      FreeRADIUS-Proxied-To attribute, then the
276                  *      packet has already been sent there.  Don't
277                  *      send it there again.
278                  */
279                 for (i = 0; i < realm->acct_pool->num_home_servers; i++) {
280                         if (fr_ipaddr_cmp(&realm->acct_pool->servers[i]->ipaddr,
281                                             &my_ipaddr) == 0) {
282                                 RDEBUG2("Suppressing proxy due to FreeRADIUS-Proxied-To");
283                                 return RLM_MODULE_OK;
284                         }
285                 }
286
287                 /*
288                  *      See detail_recv() in src/main/listen.c for the
289                  *      additional checks.
290                  */
291         } else if ((request->listener->type == RAD_LISTEN_DETAIL) &&
292                    ((request->packet->src_ipaddr.af == AF_INET6) ||
293                     (request->packet->src_ipaddr.ipaddr.ip4addr.s_addr != htonl(INADDR_NONE)))) {
294                 int i;
295
296                 /*
297                  *      Loop over the home accounting servers for this
298                  *      realm.  If one of them has the same IP as the
299                  *      FreeRADIUS-Proxied-To attribute, then the
300                  *      packet has already been sent there.  Don't
301                  *      send it there again.
302                  */
303                 for (i = 0; i < realm->acct_pool->num_home_servers; i++) {
304                         if ((fr_ipaddr_cmp(&realm->acct_pool->servers[i]->ipaddr,
305                                              &request->packet->src_ipaddr) == 0) &&
306                             (realm->acct_pool->servers[i]->port == request->packet->src_port)) {
307                                 RDEBUG2("Suppressing proxy because packet was already sent to a server in that realm");
308                                 return RLM_MODULE_OK;
309                         }
310                 }
311
312         }
313
314         /*
315          *      We got this far, which means we have a realm, set returnrealm
316          */
317         *returnrealm = realm;
318         return RLM_MODULE_UPDATED;
319 }
320
321 /*
322  *      Add a "Proxy-To-Realm" attribute to the request.
323  */
324 static void add_proxy_to_realm(VALUE_PAIR **vps, REALM *realm)
325 {
326         VALUE_PAIR *vp;
327
328         /*
329          *      Tell the server to proxy this request to another
330          *      realm.
331          */
332         vp = pairmake("Proxy-To-Realm", realm->name, T_OP_EQ);
333         if (!vp) {
334                 radlog(L_ERR|L_CONS, "no memory");
335                 exit(1);
336         }
337
338         /*
339          *  Add it, even if it's already present.
340          */
341         pairadd(vps, vp);
342 }
343
344 /*
345  *  Perform the realm module instantiation.  Configuration info is
346  *  stored in *instance for later use.
347  */
348
349 static int realm_instantiate(CONF_SECTION *conf, void **instance)
350 {
351         struct realm_config_t *inst;
352
353         /* setup a storage area for instance data */
354         inst = rad_malloc(sizeof(*inst));
355         if (!inst) {
356                 return -1;
357         }
358         memset(inst, 0, sizeof(*inst));
359
360         if(cf_section_parse(conf, inst, module_config) < 0) {
361                free(inst);
362                return -1;
363         }
364
365         if(strcasecmp(inst->formatstring, "suffix") == 0) {
366              inst->format = REALM_FORMAT_SUFFIX;
367         } else if(strcasecmp(inst->formatstring, "prefix") == 0) {
368              inst->format = REALM_FORMAT_PREFIX;
369         } else {
370              radlog(L_ERR, "Bad value \"%s\" for realm format value", inst->formatstring);
371              free(inst);
372              return -1;
373         }
374         if(strlen(inst->delim) != 1) {
375              radlog(L_ERR, "Bad value \"%s\" for realm delimiter value", inst->delim);
376              free(inst);
377              return -1;
378         }
379
380         *instance = inst;
381         return 0;
382
383 }
384
385
386
387
388
389 /*
390  *  Examine a request for a username with an realm, and if it
391  *  corresponds to something in the realms file, set that realm as
392  *  Proxy-To.
393  *
394  *  This should very nearly duplicate the old proxy_send() code
395  */
396 static int realm_authorize(void *instance, REQUEST *request)
397 {
398         int rcode;
399         REALM *realm;
400
401         /*
402          *      Check if we've got to proxy the request.
403          *      If not, return without adding a Proxy-To-Realm
404          *      attribute.
405          */
406         rcode = check_for_realm(instance, request, &realm);
407         if (rcode != RLM_MODULE_UPDATED) return rcode;
408         if (!realm) return RLM_MODULE_NOOP;
409
410         /*
411          *      Maybe add a Proxy-To-Realm attribute to the request.
412          */
413         RDEBUG2("Preparing to proxy authentication request to realm \"%s\"\n",
414                realm->name);
415         add_proxy_to_realm(&request->config_items, realm);
416
417         return RLM_MODULE_UPDATED; /* try the next module */
418 }
419
420 /*
421  * This does the exact same thing as the realm_authorize, it's just called
422  * differently.
423  */
424 static int realm_preacct(void *instance, REQUEST *request)
425 {
426         int rcode;
427         const char *name = (char *)request->username->vp_strvalue;
428         REALM *realm;
429
430         if (!name)
431           return RLM_MODULE_OK;
432
433
434         /*
435          *      Check if we've got to proxy the request.
436          *      If not, return without adding a Proxy-To-Realm
437          *      attribute.
438          */
439         rcode = check_for_realm(instance, request, &realm);
440         if (rcode != RLM_MODULE_UPDATED) return rcode;
441         if (!realm) return RLM_MODULE_NOOP;
442
443         /*
444          *      Maybe add a Proxy-To-Realm attribute to the request.
445          */
446         RDEBUG2("Preparing to proxy accounting request to realm \"%s\"\n",
447                realm->name);
448         add_proxy_to_realm(&request->config_items, realm);
449
450         return RLM_MODULE_UPDATED; /* try the next module */
451 }
452
453 static int realm_detach(void *instance)
454 {
455         free(instance);
456         return 0;
457 }
458
459 /* globally exported name */
460 module_t rlm_realm = {
461         RLM_MODULE_INIT,
462         "realm",
463         RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,         /* type */
464         realm_instantiate,              /* instantiation */
465         realm_detach,                   /* detach */
466         {
467                 NULL,                   /* authentication */
468                 realm_authorize,        /* authorization */
469                 realm_preacct,  /* preaccounting */
470                 NULL,                   /* accounting */
471                 NULL,                   /* checksimul */
472                 NULL,                   /* pre-proxy */
473                 NULL,                   /* post-proxy */
474                 NULL                    /* post-auth */
475         },
476 };