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
00032
00033 #include <string.h>
00034 #include "ld_session.h"
00035 #include "../../mem/mem.h"
00036 #include "../../sr_module.h"
00037
00038
00039 static struct ld_session* ld_sessions = NULL;
00040 static char ini_key_name[512];
00041
00042 int add_ld_session(char* _name, LDAP* _ldh, dictionary* _d)
00043 {
00044 struct ld_session* current = ld_sessions;
00045 struct ld_session* new_lds = NULL;
00046 char *host_name, *bind_dn, *bind_pwd;
00047 int client_search_timeout_ms, client_bind_timeout_ms, network_timeout_ms;
00048
00049 new_lds = (struct ld_session*)pkg_malloc(sizeof(struct ld_session));
00050 if (new_lds == NULL)
00051 {
00052 LM_ERR("no memory\n");
00053 return -1;
00054 }
00055 memset( new_lds, 0, sizeof(struct ld_session));
00056
00057
00058 strncpy(new_lds->name, _name, 255);
00059
00060 new_lds->handle = _ldh;
00061
00062
00063 host_name = iniparser_getstring(
00064 _d,
00065 get_ini_key_name(_name, CFG_N_LDAP_HOST),
00066 CFG_DEF_HOST_NAME);
00067 new_lds->host_name = (char*)pkg_malloc(strlen(host_name)+1);
00068 if (new_lds->host_name == NULL) {
00069 LM_ERR("no memory\n");
00070 return -1;
00071 }
00072 strcpy(new_lds->host_name, host_name);
00073
00074
00075 new_lds->version = iniparser_getint(
00076 _d,
00077 get_ini_key_name(_name, CFG_N_LDAP_VERSION),
00078 CFG_DEF_LDAP_VERSION);
00079
00080
00081 client_search_timeout_ms = iniparser_getint(
00082 _d,
00083 get_ini_key_name(_name, CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT),
00084 CFG_DEF_LDAP_CLIENT_SEARCH_TIMEOUT);
00085 if (client_search_timeout_ms < CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN)
00086 {
00087 LM_INFO("[%s = %d ms] is below allowed min"
00088 " [%d ms] - [%s] set to [%d ms]\n",
00089 CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
00090 client_search_timeout_ms,
00091 CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN,
00092 CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
00093 CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN);
00094 client_search_timeout_ms = CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN;
00095 }
00096 new_lds->client_search_timeout.tv_sec = client_search_timeout_ms / 1000;
00097 new_lds->client_search_timeout.tv_usec =
00098 (client_search_timeout_ms % 1000) * 1000;
00099
00100
00101 client_bind_timeout_ms = iniparser_getint(
00102 _d,
00103 get_ini_key_name(_name, CFG_N_LDAP_CLIENT_BIND_TIMEOUT),
00104 CFG_DEF_LDAP_CLIENT_BIND_TIMEOUT);
00105 new_lds->client_bind_timeout.tv_sec = client_bind_timeout_ms / 1000;
00106 new_lds->client_bind_timeout.tv_usec =
00107 (client_bind_timeout_ms % 1000) * 1000;
00108
00109
00110 network_timeout_ms = iniparser_getint(
00111 _d,
00112 get_ini_key_name(_name, CFG_N_LDAP_NETWORK_TIMEOUT),
00113 LDAP_NO_LIMIT);
00114 new_lds->network_timeout.tv_sec = network_timeout_ms / 1000;
00115 new_lds->network_timeout.tv_usec = (network_timeout_ms % 1000) * 1000;
00116
00117
00118 bind_dn = iniparser_getstring(
00119 _d,
00120 get_ini_key_name(_name, CFG_N_LDAP_BIND_DN),
00121 CFG_DEF_LDAP_BIND_DN);
00122 new_lds->bind_dn = (char*)pkg_malloc(strlen(bind_dn)+1);
00123 if (new_lds->bind_dn == NULL) {
00124 LM_ERR("no memory\n");
00125 return -1;
00126 }
00127 strcpy(new_lds->bind_dn, bind_dn);
00128
00129
00130 bind_pwd = iniparser_getstring(
00131 _d,
00132 get_ini_key_name(_name, CFG_N_LDAP_BIND_PWD),
00133 CFG_DEF_LDAP_BIND_PWD);
00134 new_lds->bind_pwd = (char*)pkg_malloc(strlen(bind_pwd)+1);
00135 if (new_lds->bind_pwd == NULL) {
00136 LM_ERR("no memory\n");
00137 return -1;
00138 }
00139 strcpy(new_lds->bind_pwd, bind_pwd);
00140
00141
00142 new_lds->calculate_ha1 = iniparser_getboolean(
00143 _d,
00144 get_ini_key_name(_name, CFG_N_CALCULATE_HA1),
00145 CFG_DEF_CALCULATE_HA1);
00146
00147
00148 if (current == NULL)
00149 {
00150 ld_sessions = new_lds;
00151 } else
00152 {
00153 while (current->next != NULL) { current = current->next; };
00154 current->next = new_lds;
00155 }
00156
00157 return 0;
00158 }
00159
00160
00161 struct ld_session* get_ld_session(char* _name)
00162 {
00163 struct ld_session* current = ld_sessions;
00164
00165 if (_name == NULL)
00166 {
00167 LM_ERR("lds_name == NULL\n");
00168 return NULL;
00169 }
00170 while (current != NULL)
00171 {
00172 if (strcmp(current->name, _name) == 0)
00173 {
00174 return current;
00175 }
00176 current = current->next;
00177 }
00178
00179 return NULL;
00180 }
00181
00182
00183 int free_ld_sessions(void)
00184 {
00185 struct ld_session* current = ld_sessions;
00186 struct ld_session* tmp;
00187
00188 while (current != NULL)
00189 {
00190 tmp = current->next;
00191
00192 if (current->handle != NULL)
00193 {
00194 ldap_unbind_ext(current->handle, NULL, NULL);
00195 }
00196 if (current->host_name != NULL)
00197 {
00198 pkg_free(current->host_name);
00199 }
00200 if (current->bind_dn != NULL)
00201 {
00202 pkg_free(current->bind_dn);
00203 }
00204 if (current->bind_pwd != NULL)
00205 {
00206 pkg_free(current->bind_pwd);
00207 }
00208
00209 pkg_free(current);
00210 current = tmp;
00211 }
00212
00213 ld_sessions = NULL;
00214
00215 return 0;
00216 }
00217
00218 char* get_ini_key_name(char* _section, char* _key)
00219 {
00220 sprintf(ini_key_name, "%s:%s", _section, _key);
00221 return ini_key_name;
00222 }
00223