Simple module to fix WiMAX Calling-Station-Id
[freeradius.git] / src / modules / rlm_wimax / rlm_wimax.c
1 /*
2  * rlm_wimax.c
3  *
4  * Version:     $Id$
5  *
6  * Copyright (C) 2008 Alan DeKok <aland@networkradius.com>
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
9  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
10  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
11  * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
12  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
13  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
14  * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
15  * SOFTWARE.
16  */
17
18 #include <freeradius-devel/ident.h>
19 RCSID("$Id$")
20
21 #include <freeradius-devel/radiusd.h>
22 #include <freeradius-devel/modules.h>
23
24
25 /*
26  *      Find the named user in this modules database.  Create the set
27  *      of attribute-value pairs to check and reply with for this user
28  *      from the database. The authentication code only needs to check
29  *      the password, the rest is done here.
30  */
31 static int wimax_authorize(void *instance, REQUEST *request)
32 {
33         VALUE_PAIR *vp;
34
35         /* quiet the compiler */
36         instance = instance;
37         request = request;
38
39         /*
40          *      Fix Calling-Station-Id.  Damn you, WiMAX!
41          */
42         vp =  pairfind(request->packet->vps, PW_CALLING_STATION_ID);
43         if (vp && (vp->length == 6)) {
44                 int i;
45                 uint8_t buffer[6];
46
47                 memcpy(buffer, vp->vp_octets, 6);
48
49                 /*
50                  *      RFC 3580 Section 3.20 says this is the preferred
51                  *      format.  Everyone *SANE* is using this format,
52                  *      so we fix it here.
53                  */
54                 for (i = 0; i < 6; i++) {
55                         fr_bin2hex(&buffer[i], &vp->vp_strvalue[i * 3], 1);
56                         vp->vp_strvalue[(i * 3) + 2] = '-';
57                 }
58
59                 vp->vp_strvalue[(5*3)+2] = '\0';
60                 vp->length = (5*3)+2;
61
62                 DEBUG2("rlm_wimax: Fixing WiMAX binary Calling-Station-Id to %s",
63                        buffer);
64         }
65
66         return RLM_MODULE_OK;
67 }
68
69
70 /*
71  *      Massage the request before recording it or proxying it
72  */
73 static int wimax_preacct(void *instance, REQUEST *request)
74 {
75         return wimax_authorize(instance, request);
76 }
77
78 /*
79  *      Write accounting information to this modules database.
80  */
81 static int wimax_accounting(void *instance, REQUEST *request)
82 {
83         /* quiet the compiler */
84         instance = instance;
85         request = request;
86
87         return RLM_MODULE_OK;
88 }
89
90
91 /*
92  *      The module name should be the only globally exported symbol.
93  *      That is, everything else should be 'static'.
94  *
95  *      If the module needs to temporarily modify it's instantiation
96  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
97  *      The server will then take care of ensuring that the module
98  *      is single-threaded.
99  */
100 module_t rlm_wimax = {
101         RLM_MODULE_INIT,
102         "wimax",
103         RLM_TYPE_THREAD_SAFE,           /* type */
104         NULL,                           /* instantiation */
105         NULL,                           /* detach */
106         {
107                 NULL,                   /* authentication */
108                 wimax_authorize,        /* authorization */
109                 wimax_preacct,          /* preaccounting */
110                 wimax_accounting,       /* accounting */
111                 NULL,                   /* checksimul */
112                 NULL,                   /* pre-proxy */
113                 NULL,                   /* post-proxy */
114                 NULL                    /* post-auth */
115         },
116 };