Added more 'const'
[freeradius.git] / src / modules / rlm_preprocess / rlm_preprocess.c
1 /*
2  * rlm_preprocess.c
3  *              Contains the functions for the "huntgroups" and "hints"
4  *              files.
5  *
6  * Version:     $Id$
7  *
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.
12  *
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.
17  *
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
21  *
22  * Copyright 2000,2006  The FreeRADIUS server project
23  * Copyright 2000  Alan DeKok <aland@ox.org>
24  */
25
26 #include        <freeradius-devel/ident.h>
27 RCSID("$Id$")
28
29 #include        <freeradius-devel/radiusd.h>
30 #include        <freeradius-devel/modules.h>
31 #include        <freeradius-devel/rad_assert.h>
32
33 #include        <ctype.h>
34
35 typedef struct rlm_preprocess_t {
36         char            *huntgroup_file;
37         char            *hints_file;
38         PAIR_LIST       *huntgroups;
39         PAIR_LIST       *hints;
40         int             with_ascend_hack;
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;
46 } rlm_preprocess_t;
47
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" },
59
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,
64           "no" },
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" },
69
70         { NULL, -1, 0, NULL, NULL }
71 };
72
73
74 /*
75  *      dgreer --
76  *      This hack changes Ascend's wierd port numberings
77  *      to standard 0-??? port numbers so that the "+" works
78  *      for IP address assignments.
79  */
80 static void ascend_nasport_hack(VALUE_PAIR *nas_port, int channels_per_line)
81 {
82         int service;
83         int line;
84         int channel;
85
86         if (!nas_port) {
87                 return;
88         }
89
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;
96         }
97 }
98
99 /*
100  *      ThomasJ --
101  *      This hack strips out Cisco's VSA duplicities in lines
102  *      (Cisco not implemented VSA's in standard way.
103  *
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.
107  */
108 static void cisco_vsa_hack(VALUE_PAIR *vp)
109 {
110         int             vendorcode;
111         char            *ptr;
112         char            newattr[MAX_STRING_LEN];
113
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 */
117
118                 if (vp->type != PW_TYPE_STRING) continue;
119
120                 /*
121                  *  No weird packing.  Ignore it.
122                  */
123                 ptr = strchr(vp->vp_strvalue, '='); /* find an '=' */
124                 if (!ptr) continue;
125
126                 /*
127                  *      Cisco-AVPair's get packed as:
128                  *
129                  *      Cisco-AVPair = "h323-foo-bar = baz"
130                  *      Cisco-AVPair = "h323-foo-bar=baz"
131                  *
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
135                  *      attribute.
136                  */
137                 if ((vp->attribute & 0xffff) == 1) {
138                         const char *p;
139                         DICT_ATTR       *dattr;
140
141                         p = vp->vp_strvalue;
142                         gettoken(&p, newattr, sizeof(newattr));
143
144                         if (((dattr = dict_attrbyname(newattr)) != NULL) &&
145                             (dattr->type == PW_TYPE_STRING)) {
146                                 VALUE_PAIR *newvp;
147
148                                 /*
149                                  *  Make a new attribute.
150                                  */
151                                 newvp = pairmake(newattr, ptr + 1, T_OP_EQ);
152                                 if (newvp) {
153                                         pairadd(&vp, newvp);
154                                 }
155                         }
156                 } else {        /* h322-foo-bar = "h323-foo-bar = baz" */
157                         /*
158                          *      We strip out the duplicity from the
159                          *      value field, we use only the value on
160                          *      the right side of the '=' character.
161                          */
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);
166                 }
167         }
168 }
169
170
171 /*
172  *      Don't even ask what this is doing...
173  */
174 static void alvarion_vsa_hack(VALUE_PAIR *vp)
175 {
176         int             vendorcode;
177         int             number = 1;
178
179         for ( ; vp != NULL; vp = vp->next) {
180                 vendorcode = VENDOR(vp->attribute);
181                 DICT_ATTR *da;
182                 if (vendorcode != 12394) continue;
183                 if (vp->type != PW_TYPE_STRING) continue;
184
185                 da = dict_attrbyvalue(number | (12394 << 16));
186                 if (!da) continue;
187
188                 vp->attribute = da->attr;
189                 vp->name = da->name;
190
191                 number++;
192         }
193 }
194
195 /*
196  *      Mangle username if needed, IN PLACE.
197  */
198 static void rad_mangle(rlm_preprocess_t *data, REQUEST *request)
199 {
200         VALUE_PAIR      *namepair;
201         VALUE_PAIR      *request_pairs;
202         VALUE_PAIR      *tmp;
203
204         /*
205          *      Get the username from the request
206          *      If it isn't there, then we can't mangle the request.
207          */
208         request_pairs = request->packet->vps;
209         namepair = pairfind(request_pairs, PW_USER_NAME);
210         if ((namepair == NULL) ||
211             (namepair->length <= 0)) {
212           return;
213         }
214
215         if (data->with_ntdomain_hack) {
216                 char            *ptr;
217                 char            newname[MAX_STRING_LEN];
218
219                 /*
220                  *      Windows NT machines often authenticate themselves as
221                  *      NT_DOMAIN\username. Try to be smart about this.
222                  *
223                  *      FIXME: should we handle this as a REALM ?
224                  */
225                 if ((ptr = strchr(namepair->vp_strvalue, '\\')) != NULL) {
226                         strlcpy(newname, ptr + 1, sizeof(newname));
227                         /* Same size */
228                         strcpy(namepair->vp_strvalue, newname);
229                         namepair->length = strlen(newname);
230                 }
231         }
232
233         if (data->with_specialix_jetstream_hack) {
234                 char            *ptr;
235
236                 /*
237                  *      Specialix Jetstream 8500 24 port access server.
238                  *      If the user name is 10 characters or longer, a "/"
239                  *      and the excess characters after the 10th are
240                  *      appended to the user name.
241                  *
242                  *      Reported by Lucas Heise <root@laonet.net>
243                  */
244                 if ((strlen((char *)namepair->vp_strvalue) > 10) &&
245                     (namepair->vp_strvalue[10] == '/')) {
246                         for (ptr = (char *)namepair->vp_strvalue + 11; *ptr; ptr++)
247                                 *(ptr - 1) = *ptr;
248                         *(ptr - 1) = 0;
249                         namepair->length = strlen((char *)namepair->vp_strvalue);
250                 }
251         }
252
253         /*
254          *      Small check: if Framed-Protocol present but Service-Type
255          *      is missing, add Service-Type = Framed-User.
256          */
257         if (pairfind(request_pairs, PW_FRAMED_PROTOCOL) != NULL &&
258             pairfind(request_pairs, PW_SERVICE_TYPE) == NULL) {
259                 tmp = radius_paircreate(request, &request->packet->vps,
260                                         PW_SERVICE_TYPE, PW_TYPE_INTEGER);
261                 tmp->vp_integer = PW_FRAMED_USER;
262         }
263 }
264
265 /*
266  *      Compare the request with the "reply" part in the
267  *      huntgroup, which normally only contains username or group.
268  *      At least one of the "reply" items has to match.
269  */
270 static int hunt_paircmp(REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check)
271 {
272         VALUE_PAIR      *check_item = check;
273         VALUE_PAIR      *tmp;
274         int             result = -1;
275
276         if (check == NULL) return 0;
277
278         while (result != 0 && check_item != NULL) {
279
280                 tmp = check_item->next;
281                 check_item->next = NULL;
282
283                 result = paircompare(req, request, check_item, NULL);
284
285                 check_item->next = tmp;
286                 check_item = check_item->next;
287         }
288
289         return result;
290 }
291
292
293 /*
294  *      Add hints to the info sent by the terminal server
295  *      based on the pattern of the username, and other attributes.
296  */
297 static int hints_setup(PAIR_LIST *hints, REQUEST *request)
298 {
299         char            *name;
300         VALUE_PAIR      *add;
301         VALUE_PAIR      *tmp;
302         PAIR_LIST       *i;
303         VALUE_PAIR *request_pairs;
304
305         request_pairs = request->packet->vps;
306
307         if (hints == NULL || request_pairs == NULL)
308                 return RLM_MODULE_NOOP;
309
310         /*
311          *      Check for valid input, zero length names not permitted
312          */
313         if ((tmp = pairfind(request_pairs, PW_USER_NAME)) == NULL)
314                 name = NULL;
315         else
316                 name = (char *)tmp->vp_strvalue;
317
318         if (name == NULL || name[0] == 0)
319                 /*
320                  *      No name, nothing to do.
321                  */
322                 return RLM_MODULE_NOOP;
323
324         for (i = hints; i; i = i->next) {
325                 /*
326                  *      Use "paircompare", which is a little more general...
327                  */
328                 if (paircompare(request, request_pairs, i->check, NULL) == 0) {
329                         DEBUG2("  hints: Matched %s at %d",
330                                i->name, i->lineno);
331                         break;
332                 }
333         }
334
335         if (i == NULL) return RLM_MODULE_NOOP;
336
337         add = paircopy(i->reply);
338
339         /*
340          *      Now add all attributes to the request list,
341          *      except the PW_STRIP_USER_NAME one, and
342          *      xlat them.
343          */
344         pairdelete(&add, PW_STRIP_USER_NAME);
345         pairxlatmove(request, &request->packet->vps, &add);
346         pairfree(&add);
347
348         return RLM_MODULE_UPDATED;
349 }
350
351 /*
352  *      See if we have access to the huntgroup.
353  */
354 static int huntgroup_access(REQUEST *request, PAIR_LIST *huntgroups)
355 {
356         PAIR_LIST       *i;
357         int             r = RLM_MODULE_OK;
358         VALUE_PAIR      *request_pairs = request->packet->vps;
359
360         /*
361          *      We're not controlling access by huntgroups:
362          *      Allow them in.
363          */
364         if (huntgroups == NULL)
365                 return RLM_MODULE_OK;
366
367         for(i = huntgroups; i; i = i->next) {
368                 /*
369                  *      See if this entry matches.
370                  */
371                 if (paircompare(request, request_pairs, i->check, NULL) != 0)
372                         continue;
373
374                 /*
375                  *      Now check for access.
376                  */
377                 r = RLM_MODULE_REJECT;
378                 if (hunt_paircmp(request, request_pairs, i->reply) == 0) {
379                         VALUE_PAIR *vp;
380
381                         /*
382                          *  We've matched the huntgroup, so add it in
383                          *  to the list of request pairs.
384                          */
385                         vp = pairfind(request_pairs, PW_HUNTGROUP_NAME);
386                         if (!vp) {
387                                 vp = radius_paircreate(request,
388                                                        &request->packet->vps,
389                                                        PW_HUNTGROUP_NAME,
390                                                        PW_TYPE_STRING);
391                                 strlcpy(vp->vp_strvalue, i->name,
392                                         sizeof(vp->vp_strvalue));
393                                 vp->length = strlen(vp->vp_strvalue);
394                         }
395                         r = RLM_MODULE_OK;
396                 }
397                 break;
398         }
399
400         return r;
401 }
402
403 /*
404  *      If the NAS wasn't smart enought to add a NAS-IP-Address
405  *      to the request, then add it ourselves.
406  */
407 static int add_nas_attr(REQUEST *request)
408 {
409         VALUE_PAIR *nas;
410
411         switch (request->packet->src_ipaddr.af) {
412         case AF_INET:
413                 nas = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS);
414                 if (!nas) {
415                         nas = radius_paircreate(request, &request->packet->vps,
416                                                 PW_NAS_IP_ADDRESS,
417                                                 PW_TYPE_IPADDR);
418                         nas->vp_ipaddr = request->packet->src_ipaddr.ipaddr.ip4addr.s_addr;
419                 }
420                 break;
421
422         case AF_INET6:
423                 nas = pairfind(request->packet->vps, PW_NAS_IPV6_ADDRESS);
424                 if (!nas) {
425                         nas = radius_paircreate(request, &request->packet->vps,
426                                                 PW_NAS_IPV6_ADDRESS,
427                                                 PW_TYPE_IPV6ADDR);
428                         memcpy(nas->vp_strvalue,
429                                &request->packet->src_ipaddr.ipaddr,
430                                sizeof(request->packet->src_ipaddr.ipaddr));
431                 }
432                 break;
433
434         default:
435                 radlog(L_ERR, "Unknown address family for packet");
436                 return -1;
437         }
438
439         return 0;
440 }
441
442
443 /*
444  *      Initialize.
445  */
446 static int preprocess_instantiate(CONF_SECTION *conf, void **instance)
447 {
448         int     rcode;
449         rlm_preprocess_t *data;
450
451         /*
452          *      Allocate room to put the module's instantiation data.
453          */
454         data = (rlm_preprocess_t *) rad_malloc(sizeof(*data));
455         memset(data, 0, sizeof(*data));
456
457         /*
458          *      Read this modules configuration data.
459          */
460         if (cf_section_parse(conf, data, module_config) < 0) {
461                 free(data);
462                 return -1;
463         }
464
465         data->huntgroups = NULL;
466         data->hints = NULL;
467
468         /*
469          *      Read the huntgroups file.
470          */
471         rcode = pairlist_read(data->huntgroup_file, &(data->huntgroups), 0);
472         if (rcode < 0) {
473                 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
474                        data->huntgroup_file);
475                 return -1;
476         }
477
478         /*
479          *      Read the hints file.
480          */
481         rcode = pairlist_read(data->hints_file, &(data->hints), 0);
482         if (rcode < 0) {
483                 radlog(L_ERR|L_CONS, "rlm_preprocess: Error reading %s",
484                        data->hints_file);
485                 return -1;
486         }
487
488         /*
489          *      Save the instantiation data for later.
490          */
491         *instance = data;
492
493         return 0;
494 }
495
496 /*
497  *      Preprocess a request.
498  */
499 static int preprocess_authorize(void *instance, REQUEST *request)
500 {
501         int r;
502         rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
503
504         /*
505          *      Mangle the username, to get rid of stupid implementation
506          *      bugs.
507          */
508         rad_mangle(data, request);
509
510         if (data->with_ascend_hack) {
511                 /*
512                  *      If we're using Ascend systems, hack the NAS-Port-Id
513                  *      in place, to go from Ascend's weird values to something
514                  *      approaching rationality.
515                  */
516                 ascend_nasport_hack(pairfind(request->packet->vps,
517                                              PW_NAS_PORT),
518                                     data->ascend_channels_per_line);
519         }
520
521         if (data->with_cisco_vsa_hack) {
522                 /*
523                  *      We need to run this hack because the h323-conf-id
524                  *      attribute should be used.
525                  */
526                 cisco_vsa_hack(request->packet->vps);
527         }
528
529         if (data->with_alvarion_vsa_hack) {
530                 /*
531                  *      We need to run this hack because the Alvarion
532                  *      people are crazy.
533                  */
534                 alvarion_vsa_hack(request->packet->vps);
535         }
536
537         /*
538          *      Note that we add the Request-Src-IP-Address to the request
539          *      structure BEFORE checking huntgroup access.  This allows
540          *      the Request-Src-IP-Address to be used for huntgroup
541          *      comparisons.
542          */
543         if (add_nas_attr(request) < 0) {
544                 return RLM_MODULE_FAIL;
545         }
546
547         hints_setup(data->hints, request);
548
549         /*
550          *      If there is a PW_CHAP_PASSWORD attribute but there
551          *      is PW_CHAP_CHALLENGE we need to add it so that other
552          *      modules can use it as a normal attribute.
553          */
554         if (pairfind(request->packet->vps, PW_CHAP_PASSWORD) &&
555             pairfind(request->packet->vps, PW_CHAP_CHALLENGE) == NULL) {
556                 VALUE_PAIR *vp;
557
558                 vp = radius_paircreate(request, &request->packet->vps,
559                                        PW_CHAP_CHALLENGE, PW_TYPE_OCTETS);
560                 vp->length = AUTH_VECTOR_LEN;
561                 memcpy(vp->vp_strvalue, request->packet->vector, AUTH_VECTOR_LEN);
562         }
563
564         if ((r = huntgroup_access(request,
565                                   data->huntgroups)) != RLM_MODULE_OK) {
566                 char buf[1024];
567                 radlog(L_AUTH, "No huntgroup access: [%s] (%s)",
568                        request->username ? request->username->vp_strvalue : "<NO User-Name>",
569                        auth_name(buf, sizeof(buf), request, 1));
570                 return r;
571         }
572
573         return RLM_MODULE_OK; /* Meaning: try next authorization module */
574 }
575
576 /*
577  *      Preprocess a request before accounting
578  */
579 static int preprocess_preaccounting(void *instance, REQUEST *request)
580 {
581         int r;
582         rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
583
584         /*
585          *  Ensure that we have the SAME user name for both
586          *  authentication && accounting.
587          */
588         rad_mangle(data, request);
589
590         if (data->with_cisco_vsa_hack) {
591                 /*
592                  *      We need to run this hack because the h323-conf-id
593                  *      attribute should be used.
594                  */
595                 cisco_vsa_hack(request->packet->vps);
596         }
597
598         if (data->with_alvarion_vsa_hack) {
599                 /*
600                  *      We need to run this hack because the Alvarion
601                  *      people are crazy.
602                  */
603                 alvarion_vsa_hack(request->packet->vps);
604         }
605
606         /*
607          *  Ensure that we log the NAS IP Address in the packet.
608          */
609         if (add_nas_attr(request) < 0) {
610                 return RLM_MODULE_FAIL;
611         }
612
613         r = hints_setup(data->hints, request);
614
615         if ((r = huntgroup_access(request,
616                                   data->huntgroups)) != RLM_MODULE_OK) {
617                 char buf[1024];
618                 radlog(L_INFO, "No huntgroup access: [%s] (%s)",
619                        request->username ? request->username->vp_strvalue : "<NO User-Name>",
620                        auth_name(buf, sizeof(buf), request, 1));
621                 return r;
622         }
623
624         return r;
625 }
626
627 /*
628  *      Clean up the module's instance.
629  */
630 static int preprocess_detach(void *instance)
631 {
632         rlm_preprocess_t *data = (rlm_preprocess_t *) instance;
633
634         pairlist_free(&(data->huntgroups));
635         pairlist_free(&(data->hints));
636
637         free(data);
638
639         return 0;
640 }
641
642 /* globally exported name */
643 module_t rlm_preprocess = {
644         RLM_MODULE_INIT,
645         "preprocess",
646         RLM_TYPE_CHECK_CONFIG_SAFE,     /* type */
647         preprocess_instantiate, /* instantiation */
648         preprocess_detach,      /* detach */
649         {
650                 NULL,                   /* authentication */
651                 preprocess_authorize,   /* authorization */
652                 preprocess_preaccounting, /* pre-accounting */
653                 NULL,                   /* accounting */
654                 NULL,                   /* checksimul */
655                 NULL,                   /* pre-proxy */
656                 NULL,                   /* post-proxy */
657                 NULL                    /* post-auth */
658         },
659 };
660