blob: a6532b75a0685e8aa08e92729838570d4c0dfe0d [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Alexandre Westfahl <awestfahl@freesipserver.net> *
4* *
5* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. *
6* *
7* All rights reserved. *
8* *
9* Redistribution and use of this software in source and binary forms, with or without modification, are *
10* permitted provided that the following conditions are met: *
11* *
12* * Redistributions of source code must retain the above *
13* copyright notice, this list of conditions and the *
14* following disclaimer. *
15* *
16* * Redistributions in binary form must reproduce the above *
17* copyright notice, this list of conditions and the *
18* following disclaimer in the documentation and/or other *
19* materials provided with the distribution. *
20* *
21* * Neither the name of the Teraoka Laboratory nor the *
22* names of its contributors may be used to endorse or *
23* promote products derived from this software without *
24* specific prior written permission of Teraoka Laboratory *
25* *
26* *
27* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
28* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
29* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
30* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
31* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
32* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
33* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
34* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
35*********************************************************************************************************/
36#include <freeDiameter/extension.h>
37#include <sys/time.h>
38#include <stdint.h>
39#include <stdlib.h>
40#include <stdio.h>
41GCC_DIAG_OFF("-Wdeprecated-declarations")
42#include <gcrypt.h>
43GCC_DIAG_ON("-Wdeprecated-declarations")
44#include <string.h>
45#include <mysql.h>
46#include "md5.h"
47
48
49#define NONCE_SIZE 16
50#define DIGEST_LEN 16
51
52
53/* Mode for the extension */
54#define MODE_DSSERVER 0x1
55#define MODE_SL 0x2
56
57//Redirect_Host_Usage
58#define ALL_USER 6
59
60/* The module configuration */
61struct as_conf {
62 int mode; /* default MODE_DSSERVER | MODE_SL */
63 enum {ASMYSQL} datasource;
64 char * mysql_login;
65 char * mysql_password;
66 char * mysql_database;
67 char * mysql_server;
68 char mysql_prefix[10]; //default: as_
69 uint16_t mysql_port; //if 0, default port will be used
70 uint16_t rtr_port;
71 uint16_t ppr_port;
72};
73extern struct as_conf * as_conf;
74
75/* Parse the configuration file */
76int as_conf_handle(char * conffile);
77
78
79extern MYSQL *conn;
80
81
82
83void calc_md5(char *buffer, char * data);
84void clear_digest(uint8_t * digest, char * readable_digest, int digestlength);
85struct avp_hdr * walk_digest(struct avp *avp, int avp_code);
86
87
88//MySQL part
89int start_mysql_connection();
90void request_mysql(char *query);
91void close_mysql_connection();
92int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen);
93int exist_username(const unsigned char *sip_aor, const size_t sipaorlen);
94int get_sipserver_cap(const unsigned char *sip_aor, const size_t sipaorlen, struct avp **capabilities);
95int get_password(const unsigned char *username, const size_t usernamelen, char *password);
96int check_sipaor(const unsigned char *username, const size_t usernamelen, const char * sip_aor,const size_t sipaorlen);
97int add_user_datatype(const unsigned char *sip_aor, const size_t sipaorlen,struct msg *message);
98int set_pending_flag(const unsigned char *username, const size_t usernamelen);
99int clear_pending_flag(const unsigned char *username, const size_t usernamelen);
100int set_real_sipserver_uri(const unsigned char *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
101int set_sipserver_uri(const unsigned char *username, const size_t usernamelen, const unsigned char *sipserver_uri,const size_t sipserverurilen);
102//int get_sipserver_uri(const unsigned char *sip_aor, const size_t sipaorlen, unsigned char ** sipserver_uri, size_t *sipserverurilen);
103int allow_roaming(const unsigned char *username, const size_t usernamelen, const char * network,const size_t networklen);
104int get_diameter_uri(const unsigned char *sip_aor, const size_t sipaorlen, char ** diameter_uri, size_t *diameterurilen);
105//count functions
106int count_avp(struct msg * message, int code, int vendor);
107
108
109void DigestCalcHA1(char * pszAlg,char * pszUserName,char * pszRealm,char * pszPassword,char * pszNonce,char * pszCNonce,HASHHEX SessionKey);
110void DigestCalcResponse(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
111void DigestCalcResponseAuth(HASHHEX HA1,char * pszNonce,char * pszNonceCount,char * pszCNonce,char * pszQop,char * pszMethod,char * pszDigestUri,HASHHEX HEntity,HASHHEX Response);
112
113int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp );
114
115//thread procedure
116void *rtr_socket(void *);
117void *ppr_socket(void *);
118
119struct rtrsipaor
120{
121 char username[200];
122 char sip_aor1[200];
123 char sip_aor2[200];
124 char sip_aor3[200];
125 char strreason[200];
126 char desthost[200];
127 int reason;
128};
129struct pprsipaor
130{
131 char username[200];
132 char datatype1[255];
133 char datatype2[255];
134 char datatype3[255];
135 int accounting;
136};
137
138int app_sip_RTR_cb(struct rtrsipaor *structure);
139int app_sip_PPR_cb(struct pprsipaor *structure);
140
141
142int ds_entry();
143void fd_ext_fini(void);
144int app_sip_default_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
145int app_sip_MAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
146int app_sip_RTA_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
147int app_sip_PPA_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
148int app_sip_LIR_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
149int app_sip_UAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
150int app_sip_SAR_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act);
151
152//Suscriber Locator
153int app_sip_SL_LIR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, void * opaque, enum disp_action * act);
154//int app_sip_SL_SAR_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, void * opaque, enum disp_action * act);
155
156#define SQL_GETPASSWORD "SELECT `password` FROM ds_users WHERE `username` ='%s'"
157#define SQL_GETPASSWORD_LEN sizeof(SQL_GETPASSWORD)-2
158
159//username by SIP-AOR
160#define SQL_GETUSERNAME "SELECT `username` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
161#define SQL_GETUSERNAME_LEN sizeof(SQL_GETUSERNAME)-2
162
163//sip server uri by username
164#define SQL_GETSIPURI "SELECT `sip_server_uri` FROM ds_users WHERE `username` ='%s'"
165#define SQL_GETSIPURI_LEN sizeof(SQL_GETSIPURI)-2
166
167//sip server uri by SIP-AOR
168#define SQL_GETSIPSERURI "SELECT `ds_sip_aor`.`sip_server_uri` FROM ds_users, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_users`.`id_user`"
169#define SQL_GETSIPSERURI_LEN sizeof(SQL_GETSIPSERURI)-2
170
171//sip capabilities for a SIP-AOR
172#define SQL_GETSIPSERCAP "SELECT `compulsory`,`id_service` FROM ds_user_services, ds_sip_aor WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_user` = `ds_user_services`.`id_user`"
173#define SQL_GETSIPSERCAP_LEN sizeof(SQL_GETSIPSERCAP)-2
174
175//user data for a user data supported
176#define SQL_GETSIPDATA "SELECT `label_type`,`data` FROM ds_sip_aor, ds_user_data, ds_data_types WHERE `sip_aor` ='%s' AND `ds_sip_aor`.`id_sip_aor` = `ds_user_data`.`id_sip_aor` AND `ds_data_types`.`id_data_type`=`ds_user_data`.`id_data_type`"
177#define SQL_GETSIPDATA_LEN sizeof(SQL_GETSIPDATA)-2
178
179#define SQL_GETDIAMURI "SELECT `sipserver_uri` FROM ds_sip_aor_map WHERE `sip_aor` ='%s'"
180#define SQL_GETDIAMURI_LEN sizeof(SQL_GETDIAMURI)-2
181
182//networks for this user
183#define SQL_GETUSERNET "SELECT `label_network` FROM ds_users, ds_user_networks, ds_networks WHERE `ds_users`.`username` ='%s' AND `ds_user_networks`.`id_user` = `ds_users`.`id_user` AND `ds_user_networks`.`id_network` = `ds_networks`.`id_network`"
184#define SQL_GETUSERNET_LEN sizeof(SQL_GETUSERNET)-2
185
186#define SQL_SETSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='%s' WHERE `username` ='%s'"
187#define SQL_SETSIPURI_LEN sizeof(SQL_SETSIPURI)-4
188
189//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
190#define SQL_RMSIPURI "UPDATE ds_users SET `temp_sip_server_uri`='', `sip_server_uri`='' WHERE `id_user` ='%s'"
191#define SQL_RMSIPURI_LEN sizeof(SQL_RMSIPURI)-2
192//TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO TODO
193
194#define SQL_SETREALSIPURI "UPDATE ds_users SET `sip_server_uri`='%s' WHERE `username` ='%s'"
195#define SQL_SETREALSIPURI_LEN sizeof(SQL_SETREALSIPURI)-4
196
197#define SQL_SETFLAG "UPDATE ds_users SET `authentication_pending`=1 WHERE `username` ='%s'"
198#define SQL_SETFLAG_LEN sizeof(SQL_SETFLAG)-2
199
200#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0, `registrated`=1 WHERE `username` ='%s'"
201#define SQL_CLEARFLAG_LEN sizeof(SQL_CLEARFLAG)-2
202
203#define SQL_GETSIPAOR "SELECT `sip_aor` FROM `ds_sip_aor`, `ds_users` WHERE `ds_sip_aor`.`id_user` = `ds_users`.`id_user` AND `ds_users`.`username` = '%s'"
204#define SQL_GETSIPAOR_LEN sizeof(SQL_GETSIPAOR)-2
205
206//#define SQL_CLEARFLAG "UPDATE ds_users SET `authentication_pending`=0 WHERE `username` ='%s'"
207//#define SQL_CLEARFLAG_LEN 67
208
209extern struct session_handler * ds_sess_hdl;
210
211//AVP code
212#define CODE_SIP_USER_DATA_TYPE 388
213#define CODE_SIP_AOR 122
214
215//Storage for some useful AVPs
216struct app_sip_dict{
217 struct dict_object * Auth_Session_State;
218 struct dict_object * Auth_Application_Id;
219 struct dict_object * Destination_Host;
220 struct dict_object * Destination_Realm;
221 struct dict_object * User_Name;
222 struct dict_object * Session_Id;
223 struct dict_object * Redirect_Host;
224 struct dict_object * Redirect_Host_Usage;
225 struct dict_object * SIP_Auth_Data_Item;
226 struct dict_object * SIP_Accounting_Information;
227 struct dict_object * SIP_Accounting_Server_URI;
228 struct dict_object * SIP_Credit_Control_Server_URI;
229 struct dict_object * SIP_Server_Assignment_Type;
230 struct dict_object * SIP_Item_Number;
231 struct dict_object * SIP_User_Authorization_Type;
232 struct dict_object * SIP_Supported_User_Data_Type;
233 struct dict_object * SIP_User_Data;
234 struct dict_object * SIP_User_Data_Type;
235 struct dict_object * SIP_User_Data_Contents;
236 struct dict_object * SIP_User_Data_Already_Available;
237 struct dict_object * SIP_Visited_Network_Id;
238 struct dict_object * SIP_Authorization;
239 struct dict_object * SIP_Authenticate;
240 struct dict_object * SIP_Number_Auth_Items;
241 struct dict_object * SIP_Authentication_Scheme;
242 struct dict_object * SIP_Authentication_Info;
243 struct dict_object * SIP_Server_URI;
244 struct dict_object * SIP_Server_Capabilities;
245 struct dict_object * SIP_Mandatory_Capability;
246 struct dict_object * SIP_Optional_Capability;
247 struct dict_object * SIP_Method;
248 struct dict_object * SIP_AOR;
249 struct dict_object * SIP_Deregistration_Reason;
250 struct dict_object * SIP_Reason_Code;
251 struct dict_object * SIP_Reason_Info;
252 struct dict_object * Digest_URI;
253 struct dict_object * Digest_Nonce;
254 struct dict_object * Digest_Nonce_Count;
255 struct dict_object * Digest_CNonce;
256 struct dict_object * Digest_Realm;
257 struct dict_object * Digest_Response;
258 struct dict_object * Digest_Response_Auth;
259 struct dict_object * Digest_Username;
260 struct dict_object * Digest_Method;
261 struct dict_object * Digest_QOP;
262 struct dict_object * Digest_Algorithm;
263 struct dict_object * Digest_HA1;
264};
265
266extern struct app_sip_dict sip_dict;