blob: b7a09c5859ea2eb5c96b7470ce64d4c224e7eec6 [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*****************************************************************************************************
2 * Software License Agreement (BSD License)
3 * Author : Souheil Ben Ayed <souheil@tera.ics.keio.ac.jp>
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 <souheil@tera.ics.keio.ac.jp>.
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 "diameap_mysql.h"
39
40static pthread_mutex_t db_cs_mutex =
41PTHREAD_MUTEX_INITIALIZER;
42
43int diameap_get_eap_user(struct eap_user * user, char * username)
44{
45 TRACE_ENTRY("%p %p",user,username);
46 if (db_conn == NULL)
47 {
48 TRACE_DEBUG(INFO, "%sNot connected to the MySQL Database server.",DIAMEAP_EXTENSION);
49
50 return EINVAL;
51
52 }
53 mysql_thread_init();
54
55 MYSQL_RES *res;
56 MYSQL_ROW row;
57 char * query;
58 CHECK_MALLOC(query=(char *)malloc(sizeof(char)*255));
59
60 sprintf(
61 query,
62 "SELECT id,username,password,eapmethod, vendor FROM users WHERE users.username='%s' and users.active='Y' ",
63 username);
64
65 CHECK_POSIX(pthread_mutex_lock( &db_cs_mutex ));
66
67 if (mysql_query(db_conn, query))
68 {
69 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
70 TRACE_DEBUG(INFO, "%sQuery execution fail. %s",DIAMEAP_EXTENSION, mysql_error(db_conn));
71 mysql_thread_end();
72 free(query);
73 query = NULL;
74 return EINVAL;
75 }
76
77 res = mysql_store_result(db_conn);
78
79 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
80
81 if ((row = mysql_fetch_row(res)) != NULL)
82 {
83
84 user->id = atoi(row[0]);
85 CHECK_MALLOC(user->userid=malloc(strlen(row[1])+1));
86 memcpy(user->userid,row[1],strlen(row[1])+1);
87 user->useridLength = strlen(row[1]);
88 CHECK_MALLOC(user->password=malloc(strlen(row[2])+1));
89 memcpy(user->password, row[2],strlen(row[2])+1);
90 user->passwordLength = strlen(row[2]);
91 user->proposed_eap_method = atoi(row[3]);
92 user->proposed_eap_method_vendor = atoi(row[4]);
93
94 mysql_free_result(res);
95 mysql_thread_end();
96 free(query);
97 query = NULL;
98 return 0;
99 }
100
101 TRACE_DEBUG(INFO, "%sUser unavailable.",DIAMEAP_EXTENSION);
102 mysql_free_result(res);
103 mysql_thread_end();
104 free(query);
105 query = NULL;
106 return EINVAL;
107
108}
109
110int diameap_authentication_get_attribs(struct eap_user *user,
111 struct fd_list * attribute_list)
112{
113
114 TRACE_ENTRY("%p %p",user,attribute_list);
115
116 if (db_conn == NULL)
117 {
118 TRACE_DEBUG(INFO, "%sNot connected to the MySQL Database server.",DIAMEAP_EXTENSION);
119
120 return EINVAL;
121
122 }
123
124 mysql_thread_init();
125 MYSQL_RES *res;
126 MYSQL_ROW row;
127 char * query;
128 CHECK_MALLOC(query=malloc(sizeof(char)*255));
129
130 sprintf(
131 query,
132 "SELECT `authe`.`attribute` ,`authe`.`value` FROM `authe` WHERE `authe`.`grp` IN ( SELECT `user_grp`.`grp` FROM `user_grp` WHERE `user_grp`.`user` = %d ) ",
133 user->id);
134
135 CHECK_POSIX(pthread_mutex_lock( &db_cs_mutex ));
136
137 if (mysql_query(db_conn, query))
138 {
139 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
140 TRACE_DEBUG(INFO, "%sQuery execution fail. %s",DIAMEAP_EXTENSION, mysql_error(db_conn));
141 mysql_thread_end();
142 free(query);
143 query = NULL;
144 return EINVAL;
145 }
146
147 res = mysql_store_result(db_conn);
148
149 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
150
151 while ((row = mysql_fetch_row(res)))
152 {
153 struct auth_attribute * attribute;
154 CHECK_MALLOC(attribute = malloc(sizeof(struct auth_attribute)));
155 memset(attribute, 0, sizeof(struct auth_attribute));
156 fd_list_init(&attribute->chain, NULL);
157 attribute->attrib = strdup(row[0]);
158 attribute->op = NULL;
159 attribute->value = strdup(row[1]);
160 fd_list_insert_before(attribute_list, &attribute->chain);
161
162 }
163
164 mysql_free_result(res);
165 mysql_thread_end();
166 free(query);
167 query = NULL;
168 return 0;
169}
170
171int diameap_authorization_get_attribs(struct eap_user *user,
172 struct fd_list * attribute_list)
173{
174 TRACE_ENTRY("%p %p",user,attribute_list);
175
176 if (db_conn == NULL)
177 {
178 TRACE_DEBUG(INFO, "%sNot connected to the MySQL Database server.",DIAMEAP_EXTENSION);
179
180 return EINVAL;
181
182 }
183
184 mysql_thread_init();
185
186 MYSQL_RES *res;
187 MYSQL_ROW row;
188 char * query;
189 CHECK_MALLOC(query=malloc(sizeof(char)*255));
190
191 sprintf(
192 query,
193 "SELECT `authz`.`attribute` , `authz`.`op` , `authz`.`value` FROM `authz` WHERE `authz`.`grp` IN ( SELECT `user_grp`.`grp` FROM `user_grp` WHERE `user_grp`.`user` = %d ) ",
194 user->id);
195
196 CHECK_POSIX(pthread_mutex_lock( &db_cs_mutex ));
197
198 if (mysql_query(db_conn, query))
199 {
200 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
201 TRACE_DEBUG(INFO, "%sQuery execution fail. %s",DIAMEAP_EXTENSION, mysql_error(db_conn));
202 mysql_thread_end();
203 free(query);
204 query = NULL;
205 return EINVAL;
206 }
207
208 res = mysql_store_result(db_conn);
209
210 CHECK_POSIX(pthread_mutex_unlock( &db_cs_mutex ));
211
212 while ((row = mysql_fetch_row(res)))
213 {
214 struct auth_attribute * attribute;
215 CHECK_MALLOC(attribute = malloc(sizeof(struct auth_attribute)));
216 memset(attribute, 0, sizeof(struct auth_attribute));
217 fd_list_init(&attribute->chain, NULL);
218 attribute->attrib = strdup(row[0]);
219 attribute->op = strdup(row[1]);
220 attribute->value = strdup(row[2]);
221 fd_list_insert_before(attribute_list, &attribute->chain);
222 }
223
224 mysql_free_result(res);
225 mysql_thread_end();
226 free(query);
227 query = NULL;
228 return 0;
229}
230
231void diameap_mysql_disconnect()
232{
233 mysql_close(db_conn);
234}