ok, working SoH standalone module
[freeradius.git] / src / modules / rlm_soh / rlm_soh.c
1 /*
2  * rlm_soh.c
3  *
4  * Version:     $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2010 Phil Mayers <p.mayers@imperial.ac.uk>
21  */
22
23 #include        <freeradius-devel/ident.h>
24 RCSID("$Id$")
25
26 #include        <freeradius-devel/radiusd.h>
27 #include        <freeradius-devel/modules.h>
28 #include        <freeradius-devel/dhcp.h>
29 #include        <freeradius-devel/soh.h>
30
31
32 typedef struct rlm_soh_t {
33         const char *xlat_name;
34         int dhcp;
35 } rlm_soh_t;
36
37
38 /*
39  * Not sure how to make this useful yet...
40  */
41 static size_t soh_xlat(void *instance, REQUEST *request, char *fmt, char *out, size_t outlen, RADIUS_ESCAPE_STRING func) {
42
43         VALUE_PAIR* vp[6];
44         char buf[256], *osvendor, *osname;
45         rlm_soh_t       *inst = instance;
46
47
48         func = func;            /* -Wunused */
49
50         /* there will be no point unless SoH-Supported = yes
51          *
52          * FIXME: should have a #define for the attribute...
53          * SoH-Supported == 2119 in dictionary.freeradius.internal
54          */
55         vp[0] = pairfind(request->packet->vps, 2119);
56         if (!vp[0])
57                 return 0;
58
59
60         if (strncasecmp(fmt, "OS", 2) == 0) {
61                 /* OS vendor */
62                 vp[0] = pairfind(request->packet->vps, 2100);
63                 vp[1] = pairfind(request->packet->vps, 2101);
64                 vp[2] = pairfind(request->packet->vps, 2102);
65                 vp[3] = pairfind(request->packet->vps, 2103);
66                 vp[4] = pairfind(request->packet->vps, 2104);
67                 vp[5] = pairfind(request->packet->vps, 2105);
68
69                 if (vp[0] && vp[0]->vp_integer == 311) {
70                         if (!vp[1]) {
71                                 snprintf(out, outlen, "Windows unknown");
72                         } else {
73                                 switch (vp[1]->vp_integer) {
74                                         case 7:
75                                                 osname = "7";
76                                                 break;
77                                         case 6:
78                                                 osname = "Vista";
79                                                 break;
80                                         case 5:
81                                                 osname = "XP";
82                                                 break;
83                                         default:
84                                                 osname = "Other";
85                                                 break;
86                                 }
87                                 snprintf(out, outlen, "Windows %s %d.%d.%d sp %d.%d", osname, vp[1]->vp_integer,
88                                                 vp[2] ? vp[2]->vp_integer : 0,
89                                                 vp[3] ? vp[3]->vp_integer : 0,
90                                                 vp[4] ? vp[4]->vp_integer : 0,
91                                                 vp[5] ? vp[5]->vp_integer : 0
92                                         );
93                         }
94                         return strlen(out);
95                 }
96         }
97
98         return 0;
99 }
100
101
102 static const CONF_PARSER module_config[] = {
103         /*
104          * Do SoH over DHCP? 
105          */
106         { "dhcp",    PW_TYPE_BOOLEAN, offsetof(rlm_soh_t,dhcp), NULL, "no" },
107
108         { NULL, -1, 0, NULL, NULL }             /* end the list */
109 };
110
111 static int soh_detach(void *instance) {
112         rlm_soh_t       *inst = instance;
113
114         if (inst->xlat_name) {
115                 xlat_unregister(inst->xlat_name, soh_xlat);
116                 free(inst->xlat_name);
117         }
118         free(instance);
119         return 0;
120 }
121
122 static int soh_instantiate(CONF_SECTION *conf, void **instance) {
123         rlm_soh_t *inst;
124
125         inst = *instance = rad_malloc(sizeof(*inst));
126         if (!inst) {
127                 return -1;
128         }
129         memset(inst, 0, sizeof(*inst));
130
131         if (cf_section_parse(conf, inst, module_config) < 0) {
132                 free(inst);
133                 return -1;
134         }
135
136         inst->xlat_name = cf_section_name2(conf);
137         if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
138         inst->xlat_name = strdup(inst->xlat_name);
139         xlat_register(inst->xlat_name, soh_xlat, inst);
140
141         return 0;
142 }
143
144 static int soh_postauth(void * instance, REQUEST *request) {
145         rlm_soh_t       *inst = instance;
146         VALUE_PAIR *vp, *sohvp;
147         int rv;
148
149 #ifdef WITH_DHCP
150         vp = pairfind(request->packet->vps, DHCP2ATTR(43));
151         if (vp) {
152                 /*
153                  * vendor-specific options contain
154                  *
155                  * vendor opt 220/0xdc - SoH payload, or null byte to probe, or string
156                  * "NAP" to indicate server-side support for SoH in OFFERs
157                  *
158                  * vendor opt 222/0xde - SoH correlation ID as utf-16 string, yuck...
159                  */
160                 uint8_t vopt, vlen, *data;
161
162                 data = vp->vp_octets;
163                 while (data < vp->vp_octets + vp->length) {
164                         vopt = *data++;
165                         vlen = *data++;
166                         switch (vopt) {
167                                 case 220:
168                                         if (vlen <= 1) {
169                                                 RDEBUG("SoH adding NAP marker to DHCP reply");
170                                                 /* client probe; send "NAP" in the reply */
171                                                 vp = paircreate(DHCP2ATTR(43), PW_TYPE_OCTETS);
172                                                 vp->vp_octets[0] = 220;
173                                                 vp->vp_octets[1] = 3;
174                                                 vp->vp_octets[4] = 'N';
175                                                 vp->vp_octets[3] = 'A';
176                                                 vp->vp_octets[2] = 'P';
177                                                 vp->length = 5;
178
179                                                 pairadd(&request->reply->vps, vp);
180
181                                         } else {
182                                                 RDEBUG("SoH decoding NAP from DHCP request");
183                                                 /* SoH payload */
184                                                 soh_verify(request, request->packet->vps, data, vlen);
185                                         }
186                                         break;
187                                 default:
188                                         /* nothing to do */
189                                         break;
190                         }
191                         data += vlen;
192                 }
193                 return RLM_MODULE_OK;
194         }
195 #endif
196         return RLM_MODULE_NOOP;
197 }
198
199 static int soh_authorize(void * instance, REQUEST *request) {
200         rlm_soh_t       *inst = instance;
201         VALUE_PAIR *vp, *sohvp;
202         int rv;
203
204         /* try to find the MS-SoH payload */
205         vp = pairfind(request->packet->vps, (311 | 16) | 55);
206         if (!vp) {
207                 RDEBUG("SoH radius VP not found");
208                 return RLM_MODULE_NOOP;
209         }
210
211         RDEBUG("SoH radius VP found");
212         /* decode it */
213         rv = soh_verify(request, request->packet->vps, vp->vp_strvalue, vp->length);
214
215         return RLM_MODULE_OK;
216 }
217
218 module_t rlm_soh = {
219         RLM_MODULE_INIT,
220         "SoH",
221         RLM_TYPE_THREAD_SAFE,           /* type */
222         soh_instantiate,                /* instantiation */
223         soh_detach,             /* detach */
224         {
225                 NULL,                   /* authenticate */
226                 soh_authorize,          /* authorize */
227                 NULL,                   /* pre-accounting */
228                 NULL,                   /* accounting */
229                 NULL,                   /* checksimul */
230                 NULL,                   /* pre-proxy */
231                 NULL,                   /* post-proxy */
232                 soh_postauth            /* post-auth */
233         },
234 };