00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <sys/types.h>
00035 #include <sys/socket.h>
00036 #include <netinet/in.h>
00037 #include <netdb.h>
00038
00039 #include "../../sr_module.h"
00040 #include "../../error.h"
00041 #include "../../dprint.h"
00042 #include "../../pvar.h"
00043 #include "../../mem/mem.h"
00044 #include "../sl/sl_api.h"
00045
00046 #include "diameter_msg.h"
00047 #include "auth_diameter.h"
00048 #include "authorize.h"
00049 #include "tcp_comm.h"
00050
00051 MODULE_VERSION
00052
00053
00054
00055 struct sl_binds slb;
00056
00057 static int mod_init(void);
00058 static int mod_child_init(int r);
00059 static int auth_fixup(void** param, int param_no);
00060 static int group_fixup(void** param, int param_no);
00061
00062 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
00063 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2);
00064 int diameter_is_user_in(struct sip_msg* _msg, char* group, char* _s2);
00065
00066
00067
00068
00069 char* diameter_client_host = "localhost";
00070 int diameter_client_port = 3000;
00071 int use_domain = 0;
00072
00073 rd_buf_t *rb;
00074
00075
00076
00077
00078 static cmd_export_t cmds[] = {
00079 {"diameter_www_authorize", (cmd_function)diameter_www_authorize, 1, auth_fixup,
00080 0, REQUEST_ROUTE},
00081 {"diameter_proxy_authorize", (cmd_function)diameter_proxy_authorize, 1, auth_fixup,
00082 0, REQUEST_ROUTE},
00083 {"diameter_is_user_in", (cmd_function)diameter_is_user_in, 2, group_fixup,
00084 0, REQUEST_ROUTE},
00085 {0, 0, 0, 0, 0, 0}
00086 };
00087
00088
00089
00090
00091
00092 static param_export_t params[] = {
00093 {"diameter_client_host", STR_PARAM, &diameter_client_host},
00094 {"diameter_client_port", INT_PARAM, &diameter_client_port},
00095 {"use_domain", INT_PARAM, &use_domain},
00096 {0, 0, 0}
00097 };
00098
00099
00100
00101
00102
00103 struct module_exports exports = {
00104 "auth_diameter",
00105 DEFAULT_DLFLAGS,
00106 cmds,
00107 params,
00108 0,
00109 0,
00110 0,
00111 0,
00112 mod_init,
00113 0,
00114 0,
00115 mod_child_init
00116 };
00117
00118
00119
00120
00121
00122 static int mod_init(void)
00123 {
00124 LM_DBG("auth_diameter - Initializing\n");
00125
00126
00127 if (load_sl_api(&slb)!=0) {
00128 LM_ERR("can't load SL API\n");
00129 return -1;
00130 }
00131
00132 return 0;
00133 }
00134
00135 static int mod_child_init(int r)
00136 {
00137
00138 LM_DBG("initializing TCP connection\n");
00139
00140 sockfd = init_mytcp(diameter_client_host, diameter_client_port);
00141 if(sockfd==-1)
00142 {
00143 LM_DBG("the TCP connection was not established\n");
00144 return -1;
00145 }
00146
00147 LM_DBG("the TCP connection was established on socket=%d\n", sockfd);
00148
00149 rb = (rd_buf_t*)pkg_malloc(sizeof(rd_buf_t));
00150 if(!rb)
00151 {
00152 LM_DBG("no more free pkg memory\n");
00153 return -1;
00154 }
00155 rb->buf = 0;
00156 rb->chall = 0;
00157
00158 return 0;
00159 }
00160
00161 #if 0
00162 static void destroy(void)
00163 {
00164 close_tcp_connection(sockfd);
00165 }
00166 #endif
00167
00168
00169
00170
00171
00172 static int auth_fixup(void** param, int param_no)
00173 {
00174 pv_elem_t *model;
00175 str s;
00176
00177 if (param_no == 1) {
00178 s.s = (char*)*param;
00179 if (s.s==0 || s.s[0]==0) {
00180 model = 0;
00181 } else {
00182 s.len = strlen(s.s);
00183 if (pv_parse_format(&s,&model)<0) {
00184 LM_ERR("pv_parse_format failed\n");
00185 return E_OUT_OF_MEM;
00186 }
00187 }
00188 *param = (void*)model;
00189 }
00190
00191 return 0;
00192 }
00193
00194
00195
00196
00197
00198 int diameter_proxy_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
00199 {
00200
00201 return authorize(_msg, (pv_elem_t*)_realm, HDR_PROXYAUTH_T);
00202 }
00203
00204
00205
00206
00207
00208 int diameter_www_authorize(struct sip_msg* _msg, char* _realm, char* _s2)
00209 {
00210 return authorize(_msg, (pv_elem_t*)_realm, HDR_AUTHORIZATION_T);
00211 }
00212
00213
00214 static int group_fixup(void** param, int param_no)
00215 {
00216 void* ptr;
00217 str* s;
00218
00219 if (param_no == 1)
00220 {
00221 ptr = *param;
00222
00223 if (!strcasecmp((char*)*param, "Request-URI"))
00224 {
00225 *param = (void*)1;
00226 goto end;
00227 }
00228
00229 if(!strcasecmp((char*)*param, "To"))
00230 {
00231 *param = (void*)2;
00232 goto end;
00233 }
00234
00235 if (!strcasecmp((char*)*param, "From"))
00236 {
00237 *param = (void*)3;
00238 goto end;
00239 }
00240
00241 if (!strcasecmp((char*)*param, "Credentials"))
00242 {
00243 *param = (void*)4;
00244 goto end;
00245 }
00246
00247 LM_ERR("unsupported Header Field identifier\n");
00248 return E_UNSPEC;
00249
00250
00251 }
00252
00253 if (param_no == 2)
00254 {
00255 s = (str*)pkg_malloc(sizeof(str));
00256 if (!s)
00257 {
00258 LM_ERR("no pkg memory left\n");
00259 return E_UNSPEC;
00260 }
00261 ptr = *param;
00262 s->s = (char*)*param;
00263 s->len = strlen(s->s);
00264 *param = (void*)s;
00265 }
00266
00267 end:
00268
00269 return 0;
00270 }
00271
00272