3 * Contains the functions for the "huntgroups" and "hints"
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * Copyright 2000,2006 The FreeRADIUS server project
23 * Copyright 2000 Alan DeKok <aland@ox.org>
26 #include <freeradius-devel/ident.h>
29 #include <freeradius-devel/radiusd.h>
30 #include <freeradius-devel/modules.h>
31 #include <freeradius-devel/rad_assert.h>
35 typedef struct rlm_preprocess_t {
38 PAIR_LIST *huntgroups;
41 int ascend_channels_per_line;
42 int with_ntdomain_hack;
43 int with_specialix_jetstream_hack;
44 int with_cisco_vsa_hack;
45 int with_alvarion_vsa_hack;
48 static const CONF_PARSER module_config[] = {
49 { "huntgroups", PW_TYPE_FILENAME,
50 offsetof(rlm_preprocess_t,huntgroup_file), NULL,
51 "${raddbdir}/huntgroups" },
52 { "hints", PW_TYPE_FILENAME,
53 offsetof(rlm_preprocess_t,hints_file), NULL,
54 "${raddbdir}/hints" },
55 { "with_ascend_hack", PW_TYPE_BOOLEAN,
56 offsetof(rlm_preprocess_t,with_ascend_hack), NULL, "no" },
57 { "ascend_channels_per_line", PW_TYPE_INTEGER,
58 offsetof(rlm_preprocess_t,ascend_channels_per_line), NULL, "23" },
60 { "with_ntdomain_hack", PW_TYPE_BOOLEAN,
61 offsetof(rlm_preprocess_t,with_ntdomain_hack), NULL, "no" },
62 { "with_specialix_jetstream_hack", PW_TYPE_BOOLEAN,
63 offsetof(rlm_preprocess_t,with_specialix_jetstream_hack), NULL,
65 { "with_cisco_vsa_hack", PW_TYPE_BOOLEAN,
66 offsetof(rlm_preprocess_t,with_cisco_vsa_hack), NULL, "no" },
67 { "with_alvarion_vsa_hack", PW_TYPE_BOOLEAN,
68 offsetof(rlm_preprocess_t,with_alvarion_vsa_hack), NULL, "no" },
70 { NULL, -1, 0, NULL, NULL }
74 * See if a VALUE_PAIR list contains Fall-Through = Yes
76 static int fallthrough(VALUE_PAIR *vp)
79 tmp = pairfind(vp, PW_FALL_THROUGH);
81 return tmp ? tmp->lvalue : 0;
86 * This hack changes Ascend's wierd port numberings
87 * to standard 0-??? port numbers so that the "+" works
88 * for IP address assignments.
90 static void ascend_nasport_hack(VALUE_PAIR *nas_port, int channels_per_line)
100 if (nas_port->vp_integer > 9999) {
101 service = nas_port->vp_integer/10000; /* 1=digital 2=analog */
102 line = (nas_port->vp_integer - (10000 * service)) / 100;
103 channel = nas_port->vp_integer-((10000 * service)+(100 * line));
104 nas_port->vp_integer =
105 (channel - 1) + (line - 1) * channels_per_line;
111 * This hack strips out Cisco's VSA duplicities in lines
112 * (Cisco not implemented VSA's in standard way.
114 * Cisco sends it's VSA attributes with the attribute name *again*
115 * in the string, like: H323-Attribute = "h323-attribute=value".
116 * This sort of behaviour is nonsense.
118 static void cisco_vsa_hack(VALUE_PAIR *vp)
122 char newattr[MAX_STRING_LEN];
124 for ( ; vp != NULL; vp = vp->next) {
125 vendorcode = VENDOR(vp->attribute);
126 if (!((vendorcode == 9) || (vendorcode == 6618))) continue; /* not a Cisco or Quintum VSA, continue */
128 if (vp->type != PW_TYPE_STRING) continue;
131 * No weird packing. Ignore it.
133 ptr = strchr(vp->vp_strvalue, '='); /* find an '=' */
137 * Cisco-AVPair's get packed as:
139 * Cisco-AVPair = "h323-foo-bar = baz"
140 * Cisco-AVPair = "h323-foo-bar=baz"
142 * which makes sense only if you're a lunatic.
143 * This code looks for the attribute named inside
144 * of the string, and if it exists, adds it as a new
147 if ((vp->attribute & 0xffff) == 1) {
152 gettoken(&p, newattr, sizeof(newattr));
154 if (((dattr = dict_attrbyname(newattr)) != NULL) &&
155 (dattr->type == PW_TYPE_STRING)) {
159 * Make a new attribute.
161 newvp = pairmake(newattr, ptr + 1, T_OP_EQ);
166 } else { /* h322-foo-bar = "h323-foo-bar = baz" */
168 * We strip out the duplicity from the
169 * value field, we use only the value on
170 * the right side of the '=' character.
172 strlcpy(newattr, ptr + 1, sizeof(newattr));
173 strlcpy((char *)vp->vp_strvalue, newattr,
174 sizeof(vp->vp_strvalue));
175 vp->length = strlen((char *)vp->vp_strvalue);
182 * Don't even ask what this is doing...
184 static void alvarion_vsa_hack(VALUE_PAIR *vp)
189 for ( ; vp != NULL; vp = vp->next) {
192 vendorcode = VENDOR(vp->attribute);
193 if (vendorcode != 12394) continue;
194 if (vp->type != PW_TYPE_STRING) continue;
196 da = dict_attrbyvalue(number | (12394 << 16));
199 vp->attribute = da->attr;
207 * Mangle username if needed, IN PLACE.
209 static void rad_mangle(rlm_preprocess_t *data, REQUEST *request)
211 VALUE_PAIR *namepair;
212 VALUE_PAIR *request_pairs;
216 * Get the username from the request
217 * If it isn't there, then we can't mangle the request.
219 request_pairs = request->packet->vps;
220 namepair = pairfind(request_pairs, PW_USER_NAME);
221 if ((namepair == NULL) ||
222 (namepair->length <= 0)) {
226 if (data->with_ntdomain_hack) {
228 char newname[MAX_STRING_LEN];
231 * Windows NT machines often authenticate themselves as
232 * NT_DOMAIN\username. Try to be smart about this.
234 * FIXME: should we handle this as a REALM ?
236 if ((ptr = strchr(namepair->vp_strvalue, '\\')) != NULL) {
237 strlcpy(newname, ptr + 1, sizeof(newname));
239 strcpy(namepair->vp_strvalue, newname);
240 namepair->length = strlen(newname);
244 if (data->with_specialix_jetstream_hack) {
248 * Specialix Jetstream 8500 24 port access server.
249 * If the user name is 10 characters or longer, a "/"
250 * and the excess characters after the 10th are
251 * appended to the user name.
253 * Reported by Lucas Heise <root@laonet.net>
255 if ((strlen((char *)namepair->vp_strvalue) > 10) &&
256 (namepair->vp_strvalue[10] == '/')) {
257 for (ptr = (char *)namepair->vp_strvalue + 11; *ptr; ptr++)
260 namepair->length = strlen((char *)namepair->vp_strvalue);
265 * Small check: if Framed-Protocol present but Service-Type
266 * is missing, add Service-Type = Framed-User.
268 if (pairfind(request_pairs, PW_FRAMED_PROTOCOL) != NULL &&
269 pairfind(request_pairs, PW_SERVICE_TYPE) == NULL) {
270 tmp = radius_paircreate(request, &request->packet->vps,
271 PW_SERVICE_TYPE, PW_TYPE_INTEGER);
272 tmp->vp_integer = PW_FRAMED_USER;
277 * Compare the request with the "reply" part in the
278 * huntgroup, which normally only contains username or group.
279 * At least one of the "reply" items has to match.
281 static int hunt_paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check)
283 VALUE_PAIR *check_item = check;
287 if (check == NULL) return 0;
289 while (result != 0 && check_item != NULL) {
291 tmp = check_item->next;
292 check_item->next = NULL;
294 result = paircompare(req, request, check_item, NULL);
296 check_item->next = tmp;
297 check_item = check_item->next;
305 * Add hints to the info sent by the terminal server
306 * based on the pattern of the username, and other attributes.
308 static int hints_setup(PAIR_LIST *hints, REQUEST *request)
314 VALUE_PAIR *request_pairs;
318 request_pairs = request->packet->vps;
320 if (hints == NULL || request_pairs == NULL)
321 return RLM_MODULE_NOOP;
324 * Check for valid input, zero length names not permitted
326 if ((tmp = pairfind(request_pairs, PW_USER_NAME)) == NULL)
329 name = (char *)tmp->vp_strvalue;
331 if (name == NULL || name[0] == 0)
333 * No name, nothing to do.
335 return RLM_MODULE_NOOP;
337 for (i = hints; i; i = i->next) {
339 * Use "paircompare", which is a little more general...
341 if (((strcmp(i->name, "DEFAULT") == 0) ||
342 (strcmp(i->name, name) == 0)) &&
343 (paircompare(request, request_pairs, i->check, NULL) == 0)) {
344 RDEBUG2(" hints: Matched %s at %d",
347 * Now add all attributes to the request list,
348 * except PW_STRIP_USER_NAME and PW_FALL_THROUGH
351 add = paircopy(i->reply);
352 ft = fallthrough(add);
353 pairdelete(&add, PW_STRIP_USER_NAME);
354 pairdelete(&add, PW_FALL_THROUGH);
355 pairxlatmove(request, &request->packet->vps, &add);
362 if (updated == 0) return RLM_MODULE_NOOP;
364 return RLM_MODULE_UPDATED;
368 * See if we have access to the huntgroup.
370 static int huntgroup_access(REQUEST *request, PAIR_LIST *huntgroups)
373 int r = RLM_MODULE_OK;
374 VALUE_PAIR *request_pairs = request->packet->vps;
377 * We're not controlling access by huntgroups:
380 if (huntgroups == NULL)
381 return RLM_MODULE_OK;
383 for(i = huntgroups; i; i = i->next) {
385 * See if this entry matches.
387 if (paircompare(request, request_pairs, i->check, NULL) != 0)
391 * Now check for access.
393 r = RLM_MODULE_REJECT;
394 if (hunt_paircmp(request, request_pairs, i->reply) == 0) {
398 * We've matched the huntgroup, so add it in
399 * to the list of request pairs.
401 vp = pairfind(request_pairs, PW_HUNTGROUP_NAME);
403 vp = radius_paircreate(request,
404 &request->packet->vps,
407 strlcpy(vp->vp_strvalue, i->name,
408 sizeof(vp->vp_strvalue));
409 vp->length = strlen(vp->vp_strvalue);
420 * If the NAS wasn't smart enought to add a NAS-IP-Address
421 * to the request, then add it ourselves.
423 static int add_nas_attr(REQUEST *request)
427 switch (request->packet->src_ipaddr.af) {
429 nas = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS);
431 nas = radius_paircreate(request, &request->packet->vps,
434 nas->vp_ipaddr = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
439 nas = pairfind(request->packet->vps, PW_NAS_IPV6_ADDRESS);
441 nas = radius_paircreate(request, &request->packet->vps,
444 memcpy(nas->vp_strvalue,
445 &request->packet->src_ipaddr.ipaddr,
446 sizeof(request->packet->src_ipaddr.ipaddr));
451 radlog(L_ERR, "Unknown address family for packet");
462 static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
465 rlm_preprocess_t *data;
468 * Allocate room to put the module's instantiation data.
470 data = (rlm_preprocess_t *) rad_malloc(sizeof(*data));
471 memset(data, 0, sizeof(*data));
474 * Read this modules configuration data.
476 if (cf_section_parse(conf, data, module_config) < 0) {
481 data->huntgroups = NULL;
485 * Read the huntgroups file.
487 rcode = pairlist_read(data->huntgroup_file, &(data->huntgroups), 0);
489 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
490 data->huntgroup_file);
495 * Read the hints file.
497 rcode = pairlist_read(data->hints_file, &(data->hints), 0);
499 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
505 * Save the instantiation data for later.
513 * Preprocess a request.
515 static int preprocess_authorize(void *instance, REQUEST *request)
518 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
521 * Mangle the username, to get rid of stupid implementation
524 rad_mangle(data, request);
526 if (data->with_ascend_hack) {
528 * If we're using Ascend systems, hack the NAS-Port-Id
529 * in place, to go from Ascend's weird values to something
530 * approaching rationality.
532 ascend_nasport_hack(pairfind(request->packet->vps,
534 data->ascend_channels_per_line);
537 if (data->with_cisco_vsa_hack) {
539 * We need to run this hack because the h323-conf-id
540 * attribute should be used.
542 cisco_vsa_hack(request->packet->vps);
545 if (data->with_alvarion_vsa_hack) {
547 * We need to run this hack because the Alvarion
550 alvarion_vsa_hack(request->packet->vps);
554 * Note that we add the Request-Src-IP-Address to the request
555 * structure BEFORE checking huntgroup access. This allows
556 * the Request-Src-IP-Address to be used for huntgroup
559 if (add_nas_attr(request) < 0) {
560 return RLM_MODULE_FAIL;
563 hints_setup(data->hints, request);
566 * If there is a PW_CHAP_PASSWORD attribute but there
567 * is PW_CHAP_CHALLENGE we need to add it so that other
568 * modules can use it as a normal attribute.
570 if (pairfind(request->packet->vps, PW_CHAP_PASSWORD) &&
571 pairfind(request->packet->vps, PW_CHAP_CHALLENGE) == NULL) {
574 vp = radius_paircreate(request, &request->packet->vps,
575 PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
576 vp->length = AUTH_VECTOR_LEN;
577 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
580 if ((r = huntgroup_access(request,
581 data->huntgroups)) != RLM_MODULE_OK) {
583 radlog_request(L_AUTH, 0, request, "No huntgroup access: [%s] (%s)",
584 request->username ? request->username->vp_strvalue : "<NO User-Name>",
585 auth_name(buf, sizeof(buf), request, 1));
589 return RLM_MODULE_OK; /* Meaning: try next authorization module */
593 * Preprocess a request before accounting
595 static int preprocess_preaccounting(void *instance, REQUEST *request)
598 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
601 * Ensure that we have the SAME user name for both
602 * authentication && accounting.
604 rad_mangle(data, request);
606 if (data->with_cisco_vsa_hack) {
608 * We need to run this hack because the h323-conf-id
609 * attribute should be used.
611 cisco_vsa_hack(request->packet->vps);
614 if (data->with_alvarion_vsa_hack) {
616 * We need to run this hack because the Alvarion
619 alvarion_vsa_hack(request->packet->vps);
623 * Ensure that we log the NAS IP Address in the packet.
625 if (add_nas_attr(request) < 0) {
626 return RLM_MODULE_FAIL;
629 hints_setup(data->hints, request);
631 if ((r = huntgroup_access(request,
632 data->huntgroups)) != RLM_MODULE_OK) {
634 radlog_request(L_INFO, 0, request, "No huntgroup access: [%s] (%s)",
635 request->username ? request->username->vp_strvalue : "<NO User-Name>",
636 auth_name(buf, sizeof(buf), request, 1));
644 * Clean up the module's instance.
646 static int preprocess_detach(void *instance)
648 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
650 pairlist_free(&(data->huntgroups));
651 pairlist_free(&(data->hints));
658 /* globally exported name */
659 module_t rlm_preprocess = {
662 RLM_TYPE_CHECK_CONFIG_SAFE, /* type */
663 preprocess_instantiate, /* instantiation */
664 preprocess_detach, /* detach */
666 NULL, /* authentication */
667 preprocess_authorize, /* authorization */
668 preprocess_preaccounting, /* pre-accounting */
669 NULL, /* accounting */
670 NULL, /* checksimul */
671 NULL, /* pre-proxy */
672 NULL, /* post-proxy */