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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Copyright 2000 The FreeRADIUS server project
23 * Copyright 2000 Alan DeKok <aland@ox.org>
26 static const char rcsid[] = "$Id$";
28 #include <freeradius-devel/autoconf.h>
37 #include <freeradius-devel/radiusd.h>
38 #include <freeradius-devel/modules.h>
39 #include <freeradius-devel/rad_assert.h>
41 typedef struct rlm_preprocess_t {
44 PAIR_LIST *huntgroups;
47 int ascend_channels_per_line;
48 int with_ntdomain_hack;
49 int with_specialix_jetstream_hack;
50 int with_cisco_vsa_hack;
53 static const CONF_PARSER module_config[] = {
54 { "huntgroups", PW_TYPE_FILENAME,
55 offsetof(rlm_preprocess_t,huntgroup_file), NULL,
56 "${raddbdir}/huntgroups" },
57 { "hints", PW_TYPE_FILENAME,
58 offsetof(rlm_preprocess_t,hints_file), NULL,
59 "${raddbdir}/hints" },
60 { "with_ascend_hack", PW_TYPE_BOOLEAN,
61 offsetof(rlm_preprocess_t,with_ascend_hack), NULL, "no" },
62 { "ascend_channels_per_line", PW_TYPE_INTEGER,
63 offsetof(rlm_preprocess_t,ascend_channels_per_line), NULL, "23" },
65 { "with_ntdomain_hack", PW_TYPE_BOOLEAN,
66 offsetof(rlm_preprocess_t,with_ntdomain_hack), NULL, "no" },
67 { "with_specialix_jetstream_hack", PW_TYPE_BOOLEAN,
68 offsetof(rlm_preprocess_t,with_specialix_jetstream_hack), NULL,
70 { "with_cisco_vsa_hack", PW_TYPE_BOOLEAN,
71 offsetof(rlm_preprocess_t,with_cisco_vsa_hack), NULL, "no" },
73 { NULL, -1, 0, NULL, NULL }
79 * This hack changes Ascend's wierd port numberings
80 * to standard 0-??? port numbers so that the "+" works
81 * for IP address assignments.
83 static void ascend_nasport_hack(VALUE_PAIR *nas_port, int channels_per_line)
93 if (nas_port->lvalue > 9999) {
94 service = nas_port->lvalue/10000; /* 1=digital 2=analog */
95 line = (nas_port->lvalue - (10000 * service)) / 100;
96 channel = nas_port->lvalue-((10000 * service)+(100 * line));
98 (channel - 1) + (line - 1) * channels_per_line;
104 * This hack strips out Cisco's VSA duplicities in lines
105 * (Cisco not implemented VSA's in standard way.
107 * Cisco sends it's VSA attributes with the attribute name *again*
108 * in the string, like: H323-Attribute = "h323-attribute=value".
109 * This sort of behaviour is nonsense.
111 static void cisco_vsa_hack(VALUE_PAIR *vp)
115 char newattr[MAX_STRING_LEN];
117 for ( ; vp != NULL; vp = vp->next) {
118 vendorcode = (vp->attribute >> 16); /* HACK! */
119 if (!((vendorcode == 9) || (vendorcode == 6618))) continue; /* not a Cisco or Quintum VSA, continue */
121 if (vp->type != PW_TYPE_STRING) continue;
124 * No weird packing. Ignore it.
126 ptr = strchr(vp->vp_strvalue, '='); /* find an '=' */
130 * Cisco-AVPair's get packed as:
132 * Cisco-AVPair = "h323-foo-bar = baz"
133 * Cisco-AVPair = "h323-foo-bar=baz"
135 * which makes sense only if you're a lunatic.
136 * This code looks for the attribute named inside
137 * of the string, and if it exists, adds it as a new
140 if ((vp->attribute & 0xffff) == 1) {
145 gettoken(&p, newattr, sizeof(newattr));
147 if (((dattr = dict_attrbyname(newattr)) != NULL) &&
148 (dattr->type == PW_TYPE_STRING)) {
152 * Make a new attribute.
154 newvp = pairmake(newattr, ptr + 1, T_OP_EQ);
159 } else { /* h322-foo-bar = "h323-foo-bar = baz" */
161 * We strip out the duplicity from the
162 * value field, we use only the value on
163 * the right side of the '=' character.
165 strNcpy(newattr, ptr + 1, sizeof(newattr));
166 strNcpy((char *)vp->vp_strvalue, newattr,
167 sizeof(vp->vp_strvalue));
168 vp->length = strlen((char *)vp->vp_strvalue);
174 * Mangle username if needed, IN PLACE.
176 static void rad_mangle(rlm_preprocess_t *data, REQUEST *request)
178 VALUE_PAIR *namepair;
179 VALUE_PAIR *request_pairs;
183 * Get the username from the request
184 * If it isn't there, then we can't mangle the request.
186 request_pairs = request->packet->vps;
187 namepair = pairfind(request_pairs, PW_USER_NAME);
188 if ((namepair == NULL) ||
189 (namepair->length <= 0)) {
193 if (data->with_ntdomain_hack) {
195 char newname[MAX_STRING_LEN];
198 * Windows NT machines often authenticate themselves as
199 * NT_DOMAIN\username. Try to be smart about this.
201 * FIXME: should we handle this as a REALM ?
203 if ((ptr = strchr(namepair->vp_strvalue, '\\')) != NULL) {
204 strNcpy(newname, ptr + 1, sizeof(newname));
206 strcpy(namepair->vp_strvalue, newname);
207 namepair->length = strlen(newname);
211 if (data->with_specialix_jetstream_hack) {
215 * Specialix Jetstream 8500 24 port access server.
216 * If the user name is 10 characters or longer, a "/"
217 * and the excess characters after the 10th are
218 * appended to the user name.
220 * Reported by Lucas Heise <root@laonet.net>
222 if ((strlen((char *)namepair->vp_strvalue) > 10) &&
223 (namepair->vp_strvalue[10] == '/')) {
224 for (ptr = (char *)namepair->vp_strvalue + 11; *ptr; ptr++)
227 namepair->length = strlen((char *)namepair->vp_strvalue);
232 * Small check: if Framed-Protocol present but Service-Type
233 * is missing, add Service-Type = Framed-User.
235 if (pairfind(request_pairs, PW_FRAMED_PROTOCOL) != NULL &&
236 pairfind(request_pairs, PW_SERVICE_TYPE) == NULL) {
237 tmp = paircreate(PW_SERVICE_TYPE, PW_TYPE_INTEGER);
239 tmp->lvalue = PW_FRAMED_USER;
240 pairmove(&request_pairs, &tmp);
246 * Compare the request with the "reply" part in the
247 * huntgroup, which normally only contains username or group.
248 * At least one of the "reply" items has to match.
250 static int hunt_paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check)
252 VALUE_PAIR *check_item = check;
256 if (check == NULL) return 0;
258 while (result != 0 && check_item != NULL) {
260 tmp = check_item->next;
261 check_item->next = NULL;
263 result = paircompare(req, request, check_item, NULL);
265 check_item->next = tmp;
266 check_item = check_item->next;
274 * Add hints to the info sent by the terminal server
275 * based on the pattern of the username, and other attributes.
277 static int hints_setup(PAIR_LIST *hints, REQUEST *request)
283 VALUE_PAIR *request_pairs;
285 request_pairs = request->packet->vps;
287 if (hints == NULL || request_pairs == NULL)
288 return RLM_MODULE_NOOP;
291 * Check for valid input, zero length names not permitted
293 if ((tmp = pairfind(request_pairs, PW_USER_NAME)) == NULL)
296 name = (char *)tmp->vp_strvalue;
298 if (name == NULL || name[0] == 0)
300 * No name, nothing to do.
302 return RLM_MODULE_NOOP;
304 for (i = hints; i; i = i->next) {
306 * Use "paircompare", which is a little more general...
308 if (paircompare(request, request_pairs, i->check, NULL) == 0) {
309 DEBUG2(" hints: Matched %s at %d",
315 if (i == NULL) return RLM_MODULE_NOOP;
317 add = paircopy(i->reply);
320 * Now add all attributes to the request list,
321 * except the PW_STRIP_USER_NAME one, and
324 pairdelete(&add, PW_STRIP_USER_NAME);
325 pairxlatmove(request, &request->packet->vps, &add);
328 return RLM_MODULE_UPDATED;
332 * See if we have access to the huntgroup.
334 static int huntgroup_access(REQUEST *request,
335 PAIR_LIST *huntgroups, VALUE_PAIR *request_pairs)
338 int r = RLM_MODULE_OK;
341 * We're not controlling access by huntgroups:
344 if (huntgroups == NULL)
345 return RLM_MODULE_OK;
347 for(i = huntgroups; i; i = i->next) {
349 * See if this entry matches.
351 if (paircompare(request, request_pairs, i->check, NULL) != 0)
355 * Now check for access.
357 r = RLM_MODULE_REJECT;
358 if (hunt_paircmp(request, request_pairs, i->reply) == 0) {
362 * We've matched the huntgroup, so add it in
363 * to the list of request pairs.
365 vp = pairfind(request_pairs, PW_HUNTGROUP_NAME);
367 vp = paircreate(PW_HUNTGROUP_NAME,
370 radlog(L_ERR, "No memory");
374 strNcpy(vp->vp_strvalue, i->name,
375 sizeof(vp->vp_strvalue));
376 vp->length = strlen(vp->vp_strvalue);
378 pairadd(&request_pairs, vp);
389 * If the NAS wasn't smart enought to add a NAS-IP-Address
390 * to the request, then add it ourselves.
392 static int add_nas_attr(REQUEST *request)
396 switch (request->packet->src_ipaddr.af) {
398 nas = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS);
400 nas = paircreate(PW_NAS_IP_ADDRESS, PW_TYPE_IPADDR);
402 radlog(L_ERR, "No memory");
405 nas->lvalue = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
406 pairadd(&request->packet->vps, nas);
411 nas = pairfind(request->packet->vps, PW_NAS_IPV6_ADDRESS);
413 nas = paircreate(PW_NAS_IPV6_ADDRESS, PW_TYPE_IPV6ADDR);
415 radlog(L_ERR, "No memory");
419 memcpy(nas->vp_strvalue,
420 &request->packet->src_ipaddr.ipaddr,
421 sizeof(request->packet->src_ipaddr.ipaddr));
422 pairadd(&request->packet->vps, nas);
427 radlog(L_ERR, "Unknown address family for packet");
438 static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
441 rlm_preprocess_t *data;
444 * Allocate room to put the module's instantiation data.
446 data = (rlm_preprocess_t *) rad_malloc(sizeof(*data));
447 memset(data, 0, sizeof(*data));
450 * Read this modules configuration data.
452 if (cf_section_parse(conf, data, module_config) < 0) {
457 data->huntgroups = NULL;
461 * Read the huntgroups file.
463 rcode = pairlist_read(data->huntgroup_file, &(data->huntgroups), 0);
465 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
466 data->huntgroup_file);
471 * Read the hints file.
473 rcode = pairlist_read(data->hints_file, &(data->hints), 0);
475 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
481 * Save the instantiation data for later.
489 * Preprocess a request.
491 static int preprocess_authorize(void *instance, REQUEST *request)
494 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
497 * Mangle the username, to get rid of stupid implementation
500 rad_mangle(data, request);
502 if (data->with_ascend_hack) {
504 * If we're using Ascend systems, hack the NAS-Port-Id
505 * in place, to go from Ascend's weird values to something
506 * approaching rationality.
508 ascend_nasport_hack(pairfind(request->packet->vps,
510 data->ascend_channels_per_line);
513 if (data->with_cisco_vsa_hack) {
515 * We need to run this hack because the h323-conf-id
516 * attribute should be used.
518 cisco_vsa_hack(request->packet->vps);
522 * Note that we add the Request-Src-IP-Address to the request
523 * structure BEFORE checking huntgroup access. This allows
524 * the Request-Src-IP-Address to be used for huntgroup
527 if (add_nas_attr(request) < 0) {
528 return RLM_MODULE_FAIL;
531 hints_setup(data->hints, request);
534 * If there is a PW_CHAP_PASSWORD attribute but there
535 * is PW_CHAP_CHALLENGE we need to add it so that other
536 * modules can use it as a normal attribute.
538 if (pairfind(request->packet->vps, PW_CHAP_PASSWORD) &&
539 pairfind(request->packet->vps, PW_CHAP_CHALLENGE) == NULL) {
541 vp = paircreate(PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
543 radlog(L_ERR|L_CONS, "no memory");
544 return RLM_MODULE_FAIL;
546 vp->length = AUTH_VECTOR_LEN;
547 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
548 pairadd(&request->packet->vps, vp);
551 if ((r = huntgroup_access(request, data->huntgroups,
552 request->packet->vps)) != RLM_MODULE_OK) {
554 radlog(L_AUTH, "No huntgroup access: [%s] (%s)",
555 request->username->vp_strvalue,
556 auth_name(buf, sizeof(buf), request, 1));
560 return RLM_MODULE_OK; /* Meaning: try next authorization module */
564 * Preprocess a request before accounting
566 static int preprocess_preaccounting(void *instance, REQUEST *request)
569 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
572 * Ensure that we have the SAME user name for both
573 * authentication && accounting.
575 rad_mangle(data, request);
577 if (data->with_cisco_vsa_hack) {
579 * We need to run this hack because the h323-conf-id
580 * attribute should be used.
582 cisco_vsa_hack(request->packet->vps);
586 * Ensure that we log the NAS IP Address in the packet.
588 if (add_nas_attr(request) < 0) {
589 return RLM_MODULE_FAIL;
592 r = hints_setup(data->hints, request);
594 if ((r = huntgroup_access(request, data->huntgroups,
595 request->packet->vps)) != RLM_MODULE_OK) {
597 radlog(L_INFO, "No huntgroup access: [%s] (%s)",
598 request->username->vp_strvalue,
599 auth_name(buf, sizeof(buf), request, 1));
607 * Clean up the module's instance.
609 static int preprocess_detach(void *instance)
611 rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
613 pairlist_free(&(data->huntgroups));
614 pairlist_free(&(data->hints));
616 free(data->huntgroup_file);
617 free(data->hints_file);
623 /* globally exported name */
624 module_t rlm_preprocess = {
627 0, /* type: reserved */
628 preprocess_instantiate, /* instantiation */
629 preprocess_detach, /* detach */
631 NULL, /* authentication */
632 preprocess_authorize, /* authorization */
633 preprocess_preaccounting, /* pre-accounting */
634 NULL, /* accounting */
635 NULL, /* checksimul */
636 NULL, /* pre-proxy */
637 NULL, /* post-proxy */