chbind: logic error oops
[freeradius.git] / src / modules / rlm_eap / libeap / eap_chbind.c
1 /*
2  * eap_chbind.c
3  *
4  * Version:     $Id$
5  *
6  * Copyright (c) 2012, JANET(UK)
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * 3. Neither the name of JANET(UK) nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
26  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
27  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36
37 #include <freeradius-devel/ident.h>
38 RCSID("$Id$")
39
40 #include "eap_chbind.h"
41
42 #define MAX_PACKET_LEN          4096
43
44 /*
45  * Process any channel bindings included in the request.
46  */
47 CHBIND_REQ *chbind_allocate(void)
48 {
49   CHBIND_REQ *ret;
50   ret = malloc(sizeof *ret);
51   if (0 != ret)
52     memset(ret, 0, sizeof *ret);
53   return ret;
54 }
55
56 void chbind_free(CHBIND_REQ *chbind)
57 {
58   /* free the chbind response, if allocated by chbind_process */
59   if (chbind->chbind_resp)
60     free(chbind->chbind_resp);
61
62   free(chbind);
63 }
64
65 int chbind_process(REQUEST *req, CHBIND_REQ *chbind_req)
66 {
67   int rcode = PW_AUTHENTICATION_REJECT;
68   REQUEST *fake = NULL;
69   VALUE_PAIR *vp = NULL;
70   uint8_t *attr_data;
71   size_t datalen = 0;
72
73   /* check input parameters */
74   rad_assert((req != NULL) && 
75              (chbind_req != NULL) &&
76              (chbind_req->chbind_req_pkt != NULL));
77   if (chbind_req->chbind_req_len < 4)
78     return PW_AUTHENTICATION_REJECT;  /* Is this the right response? */
79
80   /* Set-up NULL response for cases where channel bindings can't be processed */
81   chbind_req->chbind_resp = NULL;
82   chbind_req->chbind_resp_len = 0;
83
84   /* Set-up the fake request */
85   fake = request_alloc_fake(req);
86   rad_assert(fake->packet->vps == NULL);
87   vp = pairmake("Freeradius-Proxied-To", "127.0.0.1", T_OP_EQ);
88   if (vp) {
89     pairadd(&fake->packet->vps, vp);
90   }
91   
92   /* Add the username to the fake request */
93   if (chbind_req->username) {
94     vp = paircreate(PW_USER_NAME, 0);
95     rad_assert(vp);
96     memcpy(vp->vp_octets, chbind_req->username, chbind_req->username_len);
97     vp->length = chbind_req->username_len;
98
99     pairadd(&fake->packet->vps, vp);
100     fake->username = pairfind(fake->packet->vps, PW_USER_NAME, 0, TAG_ANY);
101   }
102
103   /* Copy the request state into the fake request */
104   /*xxx vp = paircopy(req->state);
105   if (vp)
106   pairadd(&fake->packet->vps, vp);*/
107
108   /* Add the channel binding attributes to the fake packet */
109   if (0 != (datalen = chbind_get_data((CHBIND_PACKET_T *)chbind_req->chbind_req_pkt, 
110                                       chbind_req->chbind_req_len, 
111                                       CHBIND_NSID_RADIUS, &attr_data))) {
112           while(datalen > 0) {
113                   int mylen = rad_attr2vp(NULL, NULL, NULL, attr_data, datalen, &vp);
114                   if (mylen <= 0) {
115                           /* If radaddr2vp fails, return NULL string for 
116                              channel binding response */
117                           request_free(&fake);
118                           return PW_AUTHENTICATION_ACK;
119                   }
120                   /* TODO: need to account for the possibility of rad_attr2vp generating 
121                      multiple vps */
122                   if (vp)
123                           pairadd(&fake->packet->vps, vp);
124                   attr_data += mylen;
125                   datalen -= mylen;
126           }
127   }
128
129   /* Set virtual server based on configuration for channel bindings,
130      this is hard-coded to "chbind" for now */
131   fake->server = "chbind";
132
133   /* Call rad_authenticate */
134   if ((debug_flag > 0) && fr_log_fp) {
135           DEBUG("prcoessing chbind request");
136
137           debug_pair_list(fake->packet->vps);
138
139           fprintf(fr_log_fp, "server %s {\n",
140             (fake->server == NULL) ? "" : fake->server);
141   }
142   rcode = rad_authenticate(fake);
143
144   switch(rcode) {
145     /* If rad_authenticate succeeded, build a reply */
146   case RLM_MODULE_OK:
147   case RLM_MODULE_HANDLED:
148     if ((chbind_req->chbind_resp = chbind_build_response(fake, &chbind_req->chbind_resp_len)) != NULL)
149       rcode = PW_AUTHENTICATION_ACK;
150     else
151       rcode = PW_AUTHENTICATION_REJECT;
152     break;
153   
154   /* If we got any other response from rad_authenticate, it maps to a reject */
155   default:
156     rcode = PW_AUTHENTICATION_REJECT;
157     break;
158   }
159
160   request_free(&fake);
161
162   return rcode;
163 }
164
165 /*
166  * Parse channel binding packet to obtain data for a specific NSID.
167  * See http://tools.ietf.org/html/draft-ietf-emu-chbind-13#section-5.3.2:
168  */ 
169
170 size_t chbind_get_data(CHBIND_PACKET_T *chbind_packet,
171                            size_t chbind_packet_len,
172                            int desired_nsid,
173                            uint8_t **radbuf_data)
174 {
175   size_t chbind_data_len = chbind_packet_len-1;
176   size_t pos=0;
177   if (chbind_packet->code != CHBIND_CODE_REQUEST)
178     return 0;
179   while (pos + 3 < chbind_data_len) {
180     size_t len = (chbind_packet->data[pos] << 8) + 
181       chbind_packet->data[pos + 1];
182     uint8_t nsid = chbind_packet->data[pos + 2];
183     if (pos + 3 > chbind_data_len + len) {
184       /* malformed packet; warn here */
185       return 0;
186     }
187     if (nsid == desired_nsid) {
188       *radbuf_data = &chbind_packet->data[pos+3];
189       return len;
190     }
191     pos += 3 + len;
192   }
193   /* didn't find any data matching nsid */
194   if (pos != chbind_data_len) {
195     /* warn about malformed packet */
196   }
197
198   return 0;
199 }
200
201 uint8_t *chbind_build_response(REQUEST *req, size_t *resp_len)
202 {
203   uint8_t *resp;
204   uint16_t rlen, len = 0;
205   VALUE_PAIR *vp = NULL;
206
207   *resp_len = 0;
208   resp = malloc(MAX_PACKET_LEN + 4);
209   rad_assert(resp);
210
211   /* Set-up the chbind header fields (except length, computed later) */
212   vp = pairfind(req->config_items, PW_CHBIND_RESPONSE_CODE, 0, TAG_ANY);
213   if (vp)
214     resp[0] = vp->vp_integer;
215   else resp[0] = 3; /*failure*/
216   
217
218   resp[3] = CHBIND_NSID_RADIUS;
219
220   if ((debug_flag > 0) && fr_log_fp) {
221           DEBUG("Sending chbind response: code %i\n", (int )(resp[0]));
222           debug_pair_list(req->reply->vps);
223           DEBUG("end chbind response\n");
224   }
225   /* Encode the chbind attributes into the response */
226   for (vp = req->reply->vps, rlen = 4; 
227        (vp != NULL) && (rlen < MAX_PACKET_LEN + 4); 
228        rlen += len) {
229     len = rad_vp2attr(NULL, NULL, NULL, (const VALUE_PAIR **) &vp, &resp[rlen], (MAX_PACKET_LEN + 4) - rlen);
230   }
231
232   /* Write the length field into the header */
233   resp[1] = (uint8_t)(rlen >> 8);
234   resp[2] = (uint8_t)(rlen & 0x00FF);
235   
236   /* Output the length of the entire response (attrs + header) */
237   *resp_len = rlen + 4;
238
239   return resp;
240 }