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.
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.
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
20 * Copyright 2000,2006 The FreeRADIUS server project
21 * FIXME add copyrights
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
30 #define REALM_FORMAT_PREFIX 0
31 #define REALM_FORMAT_SUFFIX 1
33 typedef struct realm_config_t {
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 */
54 * Internal function to cut down on duplicated code.
56 * Returns -1 on failure, 0 on no failure. returnrealm
57 * is NULL on don't proxy, realm otherwise.
59 static int check_for_realm(void *instance, REQUEST *request, REALM **returnrealm)
61 char namebuf[MAX_STRING_LEN];
63 char *realmname = NULL;
68 struct realm_config_t *inst = instance;
70 /* initiate returnrealm */
73 if (request->packet->src_ipaddr.af != AF_INET) {
74 DEBUG2("rlm_realm: IPv6 is not supported!");
79 * If the request has a proxy entry, then it's a proxy
80 * reply, and we're walking through the module list again.
82 * In that case, don't bother trying to proxy the request
85 * Also, if there's no User-Name attribute, we can't
88 if ((request->proxy != NULL) ||
89 (request->username == NULL)) {
90 DEBUG2(" rlm_realm: Proxy reply, or no User-Name. Ignoring.");
95 * Check for 'Realm' attribute. If it exists, then we've proxied
96 * it already ( via another rlm_realm instance ) and should return.
99 if ( (vp = pairfind(request->packet->vps, PW_REALM)) != NULL ) {
100 DEBUG2(" rlm_realm: Request already proxied. Ignoring.");
105 * We will be modifing this later, so we want our own copy
108 strlcpy(namebuf, (char *)request->username->vp_strvalue, sizeof(namebuf));
114 case REALM_FORMAT_SUFFIX:
116 /* DEBUG2(" rlm_realm: Checking for suffix after \"%c\"", inst->delim[0]); */
117 realmname = strrchr(username, inst->delim[0]);
124 case REALM_FORMAT_PREFIX:
126 /* DEBUG2(" rlm_realm: Checking for prefix before \"%c\"", inst->delim[0]); */
128 ptr = strchr(username, inst->delim[0]);
132 realmname = username;
143 * Print out excruciatingly descriptive debugging messages
144 * for the people who find it too difficult to think about
148 DEBUG2(" rlm_realm: Looking up realm \"%s\" for User-Name = \"%s\"",
149 realmname, request->username->vp_strvalue);
151 if( inst->ignore_null ) {
152 DEBUG2(" rlm_realm: No '%c' in User-Name = \"%s\", skipping NULL due to config.",
153 inst->delim[0], request->username->vp_strvalue);
156 DEBUG2(" rlm_realm: No '%c' in User-Name = \"%s\", looking up realm NULL",
157 inst->delim[0], request->username->vp_strvalue);
161 * Allow DEFAULT realms unless told not to.
163 realm = realm_find(realmname);
165 DEBUG2(" rlm_realm: No such realm \"%s\"",
166 (realmname == NULL) ? "NULL" : realmname);
169 if( inst->ignore_default &&
170 (strcmp(realm->name, "DEFAULT")) == 0) {
171 DEBUG2(" rlm_realm: Found DEFAULT, but skipping due to config.");
176 DEBUG2(" rlm_realm: Found realm \"%s\"", realm->name);
179 * If we've been told to strip the realm off, then do so.
181 if (realm->striprealm) {
183 * Create the Stripped-User-Name attribute, if it
187 if (request->username->attribute != PW_STRIPPED_USER_NAME) {
188 vp = paircreate(PW_STRIPPED_USER_NAME, PW_TYPE_STRING);
190 radlog(L_ERR|L_CONS, "no memory");
193 pairadd(&request->packet->vps, vp);
194 DEBUG2(" rlm_realm: Adding Stripped-User-Name = \"%s\"", username);
196 vp = request->username;
197 DEBUG2(" rlm_realm: Setting Stripped-User-Name = \"%s\"", username);
200 strcpy(vp->vp_strvalue, username);
201 vp->length = strlen((char *)vp->vp_strvalue);
202 request->username = vp;
205 DEBUG2(" rlm_realm: Proxying request from user %s to realm %s",
206 username, realm->name);
209 * Add the realm name to the request.
211 pairadd(&request->packet->vps, pairmake("Realm", realm->name,
213 DEBUG2(" rlm_realm: Adding Realm = \"%s\"", realm->name);
216 * Figure out what to do with the request.
218 switch (request->packet->code) {
220 DEBUG2(" rlm_realm: Unknown packet code %d\n",
221 request->packet->code);
222 return 0; /* don't do anything */
225 * Perhaps accounting proxying was turned off.
227 case PW_ACCOUNTING_REQUEST:
228 if (!realm->acct_pool) {
229 DEBUG2(" rlm_realm: Accounting realm is LOCAL.");
235 * Perhaps authentication proxying was turned off.
237 case PW_AUTHENTICATION_REQUEST:
238 if (!realm->auth_pool) {
239 DEBUG2(" rlm_realm: Authentication realm is LOCAL.");
246 * If this request has arrived from another freeradius server
247 * that has already proxied the request, we don't need to do
250 vp = pairfind(request->packet->vps, PW_FREERADIUS_PROXIED_TO);
256 * What the heck is this code doing, and why?
259 if (request->packet->code == PW_AUTHENTICATION_REQUEST &&
260 vp->lvalue == realm->home_auth->ipaddr.ipaddr.ip4addr.s_addr) {
261 DEBUG2(" rlm_realm: Request not proxied due to Freeradius-Proxied-To");
264 if (request->packet->code == PW_ACCOUNTING_REQUEST &&
265 vp->lvalue == realm->home_acct->ipaddr.ipaddr.ip4addr.s_addr) {
266 DEBUG2(" rlm_realm: Request not proxied due to Freeradius-Proxied-To");
273 * We got this far, which means we have a realm, set returnrealm
275 *returnrealm = realm;
280 * Add a "Proxy-To-Realm" attribute to the request.
282 static void add_proxy_to_realm(VALUE_PAIR **vps, REALM *realm)
287 * Tell the server to proxy this request to another
290 vp = pairmake("Proxy-To-Realm", realm->name, T_OP_EQ);
292 radlog(L_ERR|L_CONS, "no memory");
297 * Add it, even if it's already present.
303 * Perform the realm module instantiation. Configuration info is
304 * stored in *instance for later use.
307 static int realm_instantiate(CONF_SECTION *conf, void **instance)
309 struct realm_config_t *inst;
311 /* setup a storage area for instance data */
312 inst = rad_malloc(sizeof(*inst));
316 memset(inst, 0, sizeof(*inst));
318 if(cf_section_parse(conf, inst, module_config) < 0) {
323 if(strcasecmp(inst->formatstring, "suffix") == 0) {
324 inst->format = REALM_FORMAT_SUFFIX;
325 } else if(strcasecmp(inst->formatstring, "prefix") == 0) {
326 inst->format = REALM_FORMAT_PREFIX;
328 radlog(L_ERR, "Bad value \"%s\" for realm format value", inst->formatstring);
332 if(strlen(inst->delim) != 1) {
333 radlog(L_ERR, "Bad value \"%s\" for realm delimiter value", inst->delim);
348 * Examine a request for a username with an realm, and if it
349 * corresponds to something in the realms file, set that realm as
352 * This should very nearly duplicate the old proxy_send() code
354 static int realm_authorize(void *instance, REQUEST *request)
359 * Check if we've got to proxy the request.
360 * If not, return without adding a Proxy-To-Realm
363 if (check_for_realm(instance, request, &realm) < 0) {
364 return RLM_MODULE_FAIL;
367 return RLM_MODULE_NOOP;
371 * Maybe add a Proxy-To-Realm attribute to the request.
373 DEBUG2(" rlm_realm: Preparing to proxy authentication request to realm \"%s\"\n",
375 add_proxy_to_realm(&request->config_items, realm);
377 return RLM_MODULE_UPDATED; /* try the next module */
381 * This does the exact same thing as the realm_authorize, it's just called
384 static int realm_preacct(void *instance, REQUEST *request)
386 const char *name = (char *)request->username->vp_strvalue;
390 return RLM_MODULE_OK;
394 * Check if we've got to proxy the request.
395 * If not, return without adding a Proxy-To-Realm
398 if (check_for_realm(instance, request, &realm) < 0) {
399 return RLM_MODULE_FAIL;
402 return RLM_MODULE_NOOP;
407 * Maybe add a Proxy-To-Realm attribute to the request.
409 DEBUG2(" rlm_realm: Preparing to proxy accounting request to realm \"%s\"\n",
411 add_proxy_to_realm(&request->config_items, realm);
413 return RLM_MODULE_UPDATED; /* try the next module */
416 static int realm_detach(void *instance)
418 struct realm_config_t *inst = instance;
423 /* globally exported name */
424 module_t rlm_realm = {
427 0, /* type: reserved */
428 realm_instantiate, /* instantiation */
429 realm_detach, /* detach */
431 NULL, /* authentication */
432 realm_authorize, /* authorization */
433 realm_preacct, /* preaccounting */
434 NULL, /* accounting */
435 NULL, /* checksimul */
436 NULL, /* pre-proxy */
437 NULL, /* post-proxy */