Debian Codegraph @

extensions/app_diameap/plugins/eap_identity/eap_identity.c

raw
1 /*****************************************************************************************************
2 * Software License Agreement (BSD License)
3 * Author : Souheil Ben Ayed <[email protected]>
4 *
5 * Copyright (c) 2009-2010, Souheil Ben Ayed, Teraoka Laboratory of Keio University, and the WIDE Project
6 * All rights reserved.
7 *
8 * Redistribution and use of this software in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Souheil Ben Ayed <[email protected]>.
21 *
22 * 4. Neither the name of Souheil Ben Ayed, Teraoka Laboratory of Keio University or the WIDE Project nor the
23 * names of its contributors may be used to endorse or promote products
24 * derived from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
27 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
30 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *****************************************************************************************************/
37
38 #include "../../plugins.h"
39
40 struct identity_data
41 {
42 enum
43 {
44 IDENTITY_CONTINUE, IDENTITY_SUCCESS, IDENTITY_FAILURE
45 } state;
46 };
47
48 __attribute__((visibility("default")))
49 int identity_init(struct eap_state_machine *smd);
50
51 __attribute__((visibility("default")))
52 int identity_initPickUp(struct eap_state_machine *smd);
53
54 __attribute__((visibility("default")))
55 int identity_buildReq(struct eap_state_machine *smd, u8 identity, struct eap_packet * eapPacket);
56
57 __attribute__((visibility("default")))
58 boolean identity_check(struct eap_state_machine *smd, struct eap_packet *eapRespData);
59
60 __attribute__((visibility("default")))
61 int identity_process(struct eap_state_machine *smd, struct eap_packet *eapRespData);
62
63 __attribute__((visibility("default")))
64 boolean identity_isDone(struct eap_state_machine *smd);
65
66 __attribute__((visibility("default")))
67 void identity_free(void * data);
68
69 REGISTER_METHOD("identity", NULL, "identity_init", "identity_initPickUp", "identity_buildReq", NULL, "identity_check", "identity_process", "identity_isDone", NULL, NULL, "identity_free");
70
71
72 int identity_init(struct eap_state_machine *smd)
73 {
74 struct identity_data *data = NULL;
75 CHECK_MALLOC(data = malloc(sizeof(struct identity_data)));
76 memset(data, 0, sizeof(struct identity_data));
77 data->state = IDENTITY_CONTINUE;
78 smd->methodData = (struct identity_data*) data;
79
80 return 0;
81 }
82
83 int identity_initPickUp(struct eap_state_machine *smd)
84 {
85 struct identity_data *data = NULL;
86 CHECK_MALLOC(data = malloc(sizeof(struct identity_data)));
87 memset(data, 0, sizeof(struct identity_data));
88 data->state = IDENTITY_CONTINUE;
89 smd->methodData = (struct identity_data*) data;
90 return 0;
91 }
92
93 int identity_buildReq(struct eap_state_machine *smd, u8 identity, struct eap_packet * eapPacket)
94 {
95
96 CHECK_FCT(diameap_eap_new(EAP_REQUEST, identity, TYPE_IDENTITY, NULL, 0,eapPacket));
97 return 0;
98
99 }
100
101
102 boolean identity_check(struct eap_state_machine *smd, struct eap_packet *eapPacket)
103 {
104
105 if (eapPacket->data == NULL)
106 {
107 TRACE_DEBUG(INFO,"%s[EAP Identity plugin] Empty EAP packet received.",DIAMEAP_EXTENSION);
108 return FALSE;
109 }
110 eap_type type;
111 if(diameap_eap_get_type(eapPacket,&type)!=0){
112 return FALSE;
113 }
114 if (type == TYPE_IDENTITY)
115 {
116 u16 length;
117 CHECK_FCT(diameap_eap_get_length(eapPacket,&length));
118 if ((int)length < 6)
119 {
120 TRACE_DEBUG(INFO,"%s[EAP Identity plugin] Incorrect EAP packet received (length = %d ).",DIAMEAP_EXTENSION,length);
121 return FALSE;
122 }
123 if ((int)length > 1020)
124 {
125 TRACE_DEBUG(INFO,"%s[EAP Identity plugin] Incorrect EAP packet received (length = %d ).",DIAMEAP_EXTENSION,length);
126 return FALSE;
127 }
128 return TRUE;
129 }
130 return FALSE;
131 }
132
133 int identity_process(struct eap_state_machine *smd, struct eap_packet *eapRespData)
134 {
135 struct identity_data * data;
136 u16 length;
137 char * user;
138 u8 * Respdata;
139 int len,ret;
140
141 CHECK_FCT(diameap_eap_get_length(eapRespData,&length));
142 data = (struct identity_data*) smd->methodData;
143
144 CHECK_MALLOC(user=malloc(sizeof(char)*((int)length-4)));
145
146 diameap_eap_get_data(eapRespData,&Respdata,&len);
147 if(Respdata==NULL){
148 data->state = IDENTITY_FAILURE;
149 goto end;
150 }
151 U8COPY((u8 *)user,0,len,Respdata);
152 user[length-5]='\0';
153 if(check_user_identity == FALSE){
154 ret=diameap_get_eap_user(&(smd->user),"Default User");
155 CHECK_MALLOC_DO(smd->user.userid=realloc(smd->user.userid,strlen(user)+1),{ret = 1; goto next;});
156 memcpy(smd->user.userid,user,strlen(user)+1);
157 smd->user.useridLength = strlen(user);
158 } else {
159 ret=diameap_get_eap_user(&(smd->user),user);
160 }
161 next:
162 if(ret==0)
163 {
164 smd->user.methodId = -1;
165 data->state = IDENTITY_SUCCESS;
166 }else{
167 data->state = IDENTITY_FAILURE;
168 }
169 end:
170 smd->methodData = data;
171 free(user);
172 user=NULL;
173 return 0;
174 }
175
176 boolean identity_isDone(struct eap_state_machine *smd)
177 {
178 struct identity_data *data;
179 data = (struct identity_data*) smd->methodData;
180 if (data->state != IDENTITY_CONTINUE)
181 return TRUE;
182 else
183 return FALSE;
184 }
185
186 void identity_free(void * data)
187 {
188 free(data);
189 data=NULL;
190 }