027e0f6441508ca8753cd9057881b663c26fe8f0
[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(UNUSED void *instance, REQUEST *request, char *fmt, char *out, size_t outlen, UNUSED RADIUS_ESCAPE_STRING func) {
42
43         VALUE_PAIR* vp[6];
44         const char *osname;
45
46         /* there will be no point unless SoH-Supported = yes
47          *
48          * FIXME: should have a #define for the attribute...
49          * SoH-Supported == 2119 in dictionary.freeradius.internal
50          */
51         vp[0] = pairfind(request->packet->vps, 2119);
52         if (!vp[0])
53                 return 0;
54
55
56         if (strncasecmp(fmt, "OS", 2) == 0) {
57                 /* OS vendor */
58                 vp[0] = pairfind(request->packet->vps, 2100);
59                 vp[1] = pairfind(request->packet->vps, 2101);
60                 vp[2] = pairfind(request->packet->vps, 2102);
61                 vp[3] = pairfind(request->packet->vps, 2103);
62                 vp[4] = pairfind(request->packet->vps, 2104);
63                 vp[5] = pairfind(request->packet->vps, 2105);
64
65                 if (vp[0] && vp[0]->vp_integer == 311) {
66                         if (!vp[1]) {
67                                 snprintf(out, outlen, "Windows unknown");
68                         } else {
69                                 switch (vp[1]->vp_integer) {
70                                         case 7:
71                                                 osname = "7";
72                                                 break;
73                                         case 6:
74                                                 osname = "Vista";
75                                                 break;
76                                         case 5:
77                                                 osname = "XP";
78                                                 break;
79                                         default:
80                                                 osname = "Other";
81                                                 break;
82                                 }
83                                 snprintf(out, outlen, "Windows %s %d.%d.%d sp %d.%d", osname, vp[1]->vp_integer,
84                                                 vp[2] ? vp[2]->vp_integer : 0,
85                                                 vp[3] ? vp[3]->vp_integer : 0,
86                                                 vp[4] ? vp[4]->vp_integer : 0,
87                                                 vp[5] ? vp[5]->vp_integer : 0
88                                         );
89                         }
90                         return strlen(out);
91                 }
92         }
93
94         return 0;
95 }
96
97
98 static const CONF_PARSER module_config[] = {
99         /*
100          * Do SoH over DHCP? 
101          */
102         { "dhcp",    PW_TYPE_BOOLEAN, offsetof(rlm_soh_t,dhcp), NULL, "no" },
103
104         { NULL, -1, 0, NULL, NULL }             /* end the list */
105 };
106
107 static int soh_detach(void *instance) {
108         rlm_soh_t       *inst = instance;
109
110         if (inst->xlat_name) {
111                 xlat_unregister(inst->xlat_name, soh_xlat);
112                 free(inst->xlat_name);
113         }
114         free(instance);
115         return 0;
116 }
117
118 static int soh_instantiate(CONF_SECTION *conf, void **instance) {
119         rlm_soh_t *inst;
120
121         inst = *instance = rad_malloc(sizeof(*inst));
122         if (!inst) {
123                 return -1;
124         }
125         memset(inst, 0, sizeof(*inst));
126
127         if (cf_section_parse(conf, inst, module_config) < 0) {
128                 free(inst);
129                 return -1;
130         }
131
132         inst->xlat_name = cf_section_name2(conf);
133         if (!inst->xlat_name) inst->xlat_name = cf_section_name1(conf);
134         inst->xlat_name = strdup(inst->xlat_name);
135         xlat_register(inst->xlat_name, soh_xlat, inst);
136
137         return 0;
138 }
139
140 static int soh_postauth(UNUSED void * instance, REQUEST *request)
141 {
142 #ifdef WITH_DHCP
143         VALUE_PAIR *vp;
144
145         vp = pairfind(request->packet->vps, DHCP2ATTR(43));
146         if (vp) {
147                 /*
148                  * vendor-specific options contain
149                  *
150                  * vendor opt 220/0xdc - SoH payload, or null byte to probe, or string
151                  * "NAP" to indicate server-side support for SoH in OFFERs
152                  *
153                  * vendor opt 222/0xde - SoH correlation ID as utf-16 string, yuck...
154                  */
155                 uint8_t vopt, vlen, *data;
156
157                 data = vp->vp_octets;
158                 while (data < vp->vp_octets + vp->length) {
159                         vopt = *data++;
160                         vlen = *data++;
161                         switch (vopt) {
162                                 case 220:
163                                         if (vlen <= 1) {
164                                                 RDEBUG("SoH adding NAP marker to DHCP reply");
165                                                 /* client probe; send "NAP" in the reply */
166                                                 vp = paircreate(DHCP2ATTR(43), PW_TYPE_OCTETS);
167                                                 vp->vp_octets[0] = 220;
168                                                 vp->vp_octets[1] = 3;
169                                                 vp->vp_octets[4] = 'N';
170                                                 vp->vp_octets[3] = 'A';
171                                                 vp->vp_octets[2] = 'P';
172                                                 vp->length = 5;
173
174                                                 pairadd(&request->reply->vps, vp);
175
176                                         } else {
177                                                 RDEBUG("SoH decoding NAP from DHCP request");
178                                                 /* SoH payload */
179                                                 soh_verify(request->packet->vps, data, vlen);
180                                         }
181                                         break;
182                                 default:
183                                         /* nothing to do */
184                                         break;
185                         }
186                         data += vlen;
187                 }
188                 return RLM_MODULE_OK;
189         }
190 #endif
191         return RLM_MODULE_NOOP;
192 }
193
194 static int soh_authorize(UNUSED void * instance, REQUEST *request)
195 {
196         VALUE_PAIR *vp;
197         int rv;
198
199         /* try to find the MS-SoH payload */
200         vp = pairfind(request->packet->vps, (311 | 16) | 55);
201         if (!vp) {
202                 RDEBUG("SoH radius VP not found");
203                 return RLM_MODULE_NOOP;
204         }
205
206         RDEBUG("SoH radius VP found");
207         /* decode it */
208         rv = soh_verify(request->packet->vps, vp->vp_octets, vp->length);
209
210         return RLM_MODULE_OK;
211 }
212
213 module_t rlm_soh = {
214         RLM_MODULE_INIT,
215         "SoH",
216         RLM_TYPE_THREAD_SAFE,           /* type */
217         soh_instantiate,                /* instantiation */
218         soh_detach,             /* detach */
219         {
220                 NULL,                   /* authenticate */
221                 soh_authorize,          /* authorize */
222                 NULL,                   /* pre-accounting */
223                 NULL,                   /* accounting */
224                 NULL,                   /* checksimul */
225                 NULL,                   /* pre-proxy */
226                 NULL,                   /* post-proxy */
227                 soh_postauth            /* post-auth */
228         },
229 };