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 }
76 * This hack changes Ascend's wierd port numberings
77 * to standard 0-??? port numbers so that the "+" works
78 * for IP address assignments.
80 static void ascend_nasport_hack(VALUE_PAIR *nas_port, int channels_per_line)
90 if (nas_port->vp_integer > 9999) {
91 service = nas_port->vp_integer/10000; /* 1=digital 2=analog */
92 line = (nas_port->vp_integer - (10000 * service)) / 100;
93 channel = nas_port->vp_integer-((10000 * service)+(100 * line));
94 nas_port->vp_integer =
95 (channel - 1) + (line - 1) * channels_per_line;
101 * This hack strips out Cisco's VSA duplicities in lines
102 * (Cisco not implemented VSA's in standard way.
104 * Cisco sends it's VSA attributes with the attribute name *again*
105 * in the string, like: H323-Attribute = "h323-attribute=value".
106 * This sort of behaviour is nonsense.
108 static void cisco_vsa_hack(VALUE_PAIR *vp)
112 char newattr[MAX_STRING_LEN];
114 for ( ; vp != NULL; vp = vp->next) {
115 vendorcode = VENDOR(vp->attribute);
116 if (!((vendorcode == 9) || (vendorcode == 6618))) continue; /* not a Cisco or Quintum VSA, continue */
118 if (vp->type != PW_TYPE_STRING) continue;
121 * No weird packing. Ignore it.
123 ptr = strchr(vp->vp_strvalue, '='); /* find an '=' */
127 * Cisco-AVPair's get packed as:
129 * Cisco-AVPair = "h323-foo-bar = baz"
130 * Cisco-AVPair = "h323-foo-bar=baz"
132 * which makes sense only if you're a lunatic.
133 * This code looks for the attribute named inside
134 * of the string, and if it exists, adds it as a new
137 if ((vp->attribute & 0xffff) == 1) {
142 gettoken(&p, newattr, sizeof(newattr));
144 if (((dattr = dict_attrbyname(newattr)) != NULL) &&
145 (dattr->type == PW_TYPE_STRING)) {
149 * Make a new attribute.
151 newvp = pairmake(newattr, ptr + 1, T_OP_EQ);
156 } else { /* h322-foo-bar = "h323-foo-bar = baz" */
158 * We strip out the duplicity from the
159 * value field, we use only the value on
160 * the right side of the '=' character.
162 strlcpy(newattr, ptr + 1, sizeof(newattr));
163 strlcpy((char *)vp->vp_strvalue, newattr,
164 sizeof(vp->vp_strvalue));
165 vp->length = strlen((char *)vp->vp_strvalue);
172 * Don't even ask what this is doing...
174 static void alvarion_vsa_hack(VALUE_PAIR *vp)
179 for ( ; vp != NULL; vp = vp->next) {
180 vendorcode = VENDOR(vp->attribute);
181 if (vendorcode != 12394) continue;
182 if (vp->type != PW_TYPE_STRING) continue;
184 vp->attribute = number | (12394 << 16);
185 snprintf(vp->name, sizeof(vp->name),
186 "Breezecom-Attr%d", number++);
191 * Mangle username if needed, IN PLACE.
193 static void rad_mangle(rlm_preprocess_t *data, REQUEST *request)
195 VALUE_PAIR *namepair;
196 VALUE_PAIR *request_pairs;
200 * Get the username from the request
201 * If it isn't there, then we can't mangle the request.
203 request_pairs = request->packet->vps;
204 namepair = pairfind(request_pairs, PW_USER_NAME);
205 if ((namepair == NULL) ||
206 (namepair->length <= 0)) {
210 if (data->with_ntdomain_hack) {
212 char newname[MAX_STRING_LEN];
215 * Windows NT machines often authenticate themselves as
216 * NT_DOMAIN\username. Try to be smart about this.
218 * FIXME: should we handle this as a REALM ?
220 if ((ptr = strchr(namepair->vp_strvalue, '\\')) != NULL) {
221 strlcpy(newname, ptr + 1, sizeof(newname));
223 strcpy(namepair->vp_strvalue, newname);
224 namepair->length = strlen(newname);
228 if (data->with_specialix_jetstream_hack) {
232 * Specialix Jetstream 8500 24 port access server.
233 * If the user name is 10 characters or longer, a "/"
234 * and the excess characters after the 10th are
235 * appended to the user name.
237 * Reported by Lucas Heise <root@laonet.net>
239 if ((strlen((char *)namepair->vp_strvalue) > 10) &&
240 (namepair->vp_strvalue[10] == '/')) {
241 for (ptr = (char *)namepair->vp_strvalue + 11; *ptr; ptr++)
244 namepair->length = strlen((char *)namepair->vp_strvalue);
249 * Small check: if Framed-Protocol present but Service-Type
250 * is missing, add Service-Type = Framed-User.
252 if (pairfind(request_pairs, PW_FRAMED_PROTOCOL) != NULL &&
253 pairfind(request_pairs, PW_SERVICE_TYPE) == NULL) {
254 tmp = radius_paircreate(request, &request->packet->vps,
255 PW_SERVICE_TYPE, PW_TYPE_INTEGER);
256 tmp->vp_integer = PW_FRAMED_USER;
261 * Compare the request with the "reply" part in the
262 * huntgroup, which normally only contains username or group.
263 * At least one of the "reply" items has to match.
265 static int hunt_paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check)
267 VALUE_PAIR *check_item = check;
271 if (check == NULL) return 0;
273 while (result != 0 && check_item != NULL) {
275 tmp = check_item->next;
276 check_item->next = NULL;
278 result = paircompare(req, request, check_item, NULL);
280 check_item->next = tmp;
281 check_item = check_item->next;
289 * Add hints to the info sent by the terminal server
290 * based on the pattern of the username, and other attributes.
292 static int hints_setup(PAIR_LIST *hints, REQUEST *request)
298 VALUE_PAIR *request_pairs;
300 request_pairs = request->packet->vps;
302 if (hints == NULL || request_pairs == NULL)
303 return RLM_MODULE_NOOP;
306 * Check for valid input, zero length names not permitted
308 if ((tmp = pairfind(request_pairs, PW_USER_NAME)) == NULL)
311 name = (char *)tmp->vp_strvalue;
313 if (name == NULL || name[0] == 0)
315 * No name, nothing to do.
317 return RLM_MODULE_NOOP;
319 for (i = hints; i; i = i->next) {
321 * Use "paircompare", which is a little more general...
323 if (paircompare(request, request_pairs, i->check, NULL) == 0) {
324 DEBUG2(" hints: Matched %s at %d",
330 if (i == NULL) return RLM_MODULE_NOOP;
332 add = paircopy(i->reply);
335 * Now add all attributes to the request list,
336 * except the PW_STRIP_USER_NAME one, and
339 pairdelete(&add, PW_STRIP_USER_NAME);
340 pairxlatmove(request, &request->packet->vps, &add);
343 return RLM_MODULE_UPDATED;
347 * See if we have access to the huntgroup.
349 static int huntgroup_access(REQUEST *request, PAIR_LIST *huntgroups)
352 int r = RLM_MODULE_OK;
353 VALUE_PAIR *request_pairs = request->packet->vps;
356 * We're not controlling access by huntgroups:
359 if (huntgroups == NULL)
360 return RLM_MODULE_OK;
362 for(i = huntgroups; i; i = i->next) {
364 * See if this entry matches.
366 if (paircompare(request, request_pairs, i->check, NULL) != 0)
370 * Now check for access.
372 r = RLM_MODULE_REJECT;
373 if (hunt_paircmp(request, request_pairs, i->reply) == 0) {
377 * We've matched the huntgroup, so add it in
378 * to the list of request pairs.
380 vp = pairfind(request_pairs, PW_HUNTGROUP_NAME);
382 vp = radius_paircreate(request,
383 &request->packet->vps,
386 strlcpy(vp->vp_strvalue, i->name,
387 sizeof(vp->vp_strvalue));
388 vp->length = strlen(vp->vp_strvalue);
399 * If the NAS wasn't smart enought to add a NAS-IP-Address
400 * to the request, then add it ourselves.
402 static int add_nas_attr(REQUEST *request)
406 switch (request->packet->src_ipaddr.af) {
408 nas = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS);
410 nas = radius_paircreate(request, &request->packet->vps,
413 nas->vp_ipaddr = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
418 nas = pairfind(request->packet->vps, PW_NAS_IPV6_ADDRESS);
420 nas = radius_paircreate(request, &request->packet->vps,
423 memcpy(nas->vp_strvalue,
424 &request->packet->src_ipaddr.ipaddr,
425 sizeof(request->packet->src_ipaddr.ipaddr));
430 radlog(L_ERR, "Unknown address family for packet");
441 static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
444 rlm_preprocess_t *data;
447 * Allocate room to put the module's instantiation data.
449 data = (rlm_preprocess_t *) rad_malloc(sizeof(*data));
450 memset(data, 0, sizeof(*data));
453 * Read this modules configuration data.
455 if (cf_section_parse(conf, data, module_config) < 0) {
460 data->huntgroups = NULL;
464 * Read the huntgroups file.
466 rcode = pairlist_read(data->huntgroup_file, &(data->huntgroups), 0);
468 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
469 data->huntgroup_file);
474 * Read the hints file.
476 rcode = pairlist_read(data->hints_file, &(data->hints), 0);
478 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
484 * Save the instantiation data for later.
492 * Preprocess a request.
494 static int preprocess_authorize(void *instance, REQUEST *request)
497 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
500 * Mangle the username, to get rid of stupid implementation
503 rad_mangle(data, request);
505 if (data->with_ascend_hack) {
507 * If we're using Ascend systems, hack the NAS-Port-Id
508 * in place, to go from Ascend's weird values to something
509 * approaching rationality.
511 ascend_nasport_hack(pairfind(request->packet->vps,
513 data->ascend_channels_per_line);
516 if (data->with_cisco_vsa_hack) {
518 * We need to run this hack because the h323-conf-id
519 * attribute should be used.
521 cisco_vsa_hack(request->packet->vps);
524 if (data->with_alvarion_vsa_hack) {
526 * We need to run this hack because the Alvarion
529 alvarion_vsa_hack(request->packet->vps);
533 * Note that we add the Request-Src-IP-Address to the request
534 * structure BEFORE checking huntgroup access. This allows
535 * the Request-Src-IP-Address to be used for huntgroup
538 if (add_nas_attr(request) < 0) {
539 return RLM_MODULE_FAIL;
542 hints_setup(data->hints, request);
545 * If there is a PW_CHAP_PASSWORD attribute but there
546 * is PW_CHAP_CHALLENGE we need to add it so that other
547 * modules can use it as a normal attribute.
549 if (pairfind(request->packet->vps, PW_CHAP_PASSWORD) &&
550 pairfind(request->packet->vps, PW_CHAP_CHALLENGE) == NULL) {
553 vp = radius_paircreate(request, &request->packet->vps,
554 PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
555 vp->length = AUTH_VECTOR_LEN;
556 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
559 if ((r = huntgroup_access(request,
560 data->huntgroups)) != RLM_MODULE_OK) {
562 radlog(L_AUTH, "No huntgroup access: [%s] (%s)",
563 request->username ? request->username->vp_strvalue : "<NO User-Name>",
564 auth_name(buf, sizeof(buf), request, 1));
568 return RLM_MODULE_OK; /* Meaning: try next authorization module */
572 * Preprocess a request before accounting
574 static int preprocess_preaccounting(void *instance, REQUEST *request)
577 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
580 * Ensure that we have the SAME user name for both
581 * authentication && accounting.
583 rad_mangle(data, request);
585 if (data->with_cisco_vsa_hack) {
587 * We need to run this hack because the h323-conf-id
588 * attribute should be used.
590 cisco_vsa_hack(request->packet->vps);
593 if (data->with_alvarion_vsa_hack) {
595 * We need to run this hack because the Alvarion
598 alvarion_vsa_hack(request->packet->vps);
602 * Ensure that we log the NAS IP Address in the packet.
604 if (add_nas_attr(request) < 0) {
605 return RLM_MODULE_FAIL;
608 r = hints_setup(data->hints, request);
610 if ((r = huntgroup_access(request,
611 data->huntgroups)) != RLM_MODULE_OK) {
613 radlog(L_INFO, "No huntgroup access: [%s] (%s)",
614 request->username ? request->username->vp_strvalue : "<NO User-Name>",
615 auth_name(buf, sizeof(buf), request, 1));
623 * Clean up the module's instance.
625 static int preprocess_detach(void *instance)
627 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
629 pairlist_free(&(data->huntgroups));
630 pairlist_free(&(data->hints));
637 /* globally exported name */
638 module_t rlm_preprocess = {
641 0, /* type: reserved */
642 preprocess_instantiate, /* instantiation */
643 preprocess_detach, /* detach */
645 NULL, /* authentication */
646 preprocess_authorize, /* authorization */
647 preprocess_preaccounting, /* pre-accounting */
648 NULL, /* accounting */
649 NULL, /* checksimul */
650 NULL, /* pre-proxy */
651 NULL, /* post-proxy */