ld_session.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * $Id$
  3. *
  4. * Kamailio LDAP Module
  5. *
  6. * Copyright (C) 2007 University of North Carolina
  7. *
  8. * Original author: Christian Schlatter, [email protected]
  9. *
  10. *
  11. * This file is part of Kamailio, a free SIP server.
  12. *
  13. * Kamailio is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version
  17. *
  18. * Kamailio is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  26. *
  27. * History:
  28. * --------
  29. * 2007-02-18: Initial version
  30. */
  31. #include <string.h>
  32. #include "ld_session.h"
  33. #include "../../mem/mem.h"
  34. #include "../../sr_module.h"
  35. static struct ld_session* ld_sessions = NULL;
  36. static char ini_key_name[512];
  37. int add_ld_session(char* _name, LDAP* _ldh, dictionary* _d)
  38. {
  39. struct ld_session* current = ld_sessions;
  40. struct ld_session* new_lds = NULL;
  41. char *host_name, *bind_dn, *bind_pwd;
  42. int client_search_timeout_ms, client_bind_timeout_ms, network_timeout_ms;
  43. new_lds = (struct ld_session*)pkg_malloc(sizeof(struct ld_session));
  44. if (new_lds == NULL)
  45. {
  46. LM_ERR("no memory\n");
  47. return -1;
  48. }
  49. memset( new_lds, 0, sizeof(struct ld_session));
  50. /* name */
  51. strncpy(new_lds->name, _name, 255);
  52. /* handle */
  53. new_lds->handle = _ldh;
  54. /* host_name */
  55. host_name = iniparser_getstring(
  56. _d,
  57. get_ini_key_name(_name, CFG_N_LDAP_HOST),
  58. CFG_DEF_HOST_NAME);
  59. new_lds->host_name = (char*)pkg_malloc(strlen(host_name)+1);
  60. if (new_lds->host_name == NULL) {
  61. LM_ERR("no memory\n");
  62. return -1;
  63. }
  64. strcpy(new_lds->host_name, host_name);
  65. /* version */
  66. new_lds->version = iniparser_getint(
  67. _d,
  68. get_ini_key_name(_name, CFG_N_LDAP_VERSION),
  69. CFG_DEF_LDAP_VERSION);
  70. /* client_search_timeout */
  71. client_search_timeout_ms = iniparser_getint(
  72. _d,
  73. get_ini_key_name(_name, CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT),
  74. CFG_DEF_LDAP_CLIENT_SEARCH_TIMEOUT);
  75. if (client_search_timeout_ms < CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN)
  76. {
  77. LM_INFO("[%s = %d ms] is below allowed min"
  78. " [%d ms] - [%s] set to [%d ms]\n",
  79. CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
  80. client_search_timeout_ms,
  81. CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN,
  82. CFG_N_LDAP_CLIENT_SEARCH_TIMEOUT,
  83. CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN);
  84. client_search_timeout_ms = CFG_LDAP_CLIENT_SEARCH_TIMEOUT_MIN;
  85. }
  86. new_lds->client_search_timeout.tv_sec = client_search_timeout_ms / 1000;
  87. new_lds->client_search_timeout.tv_usec =
  88. (client_search_timeout_ms % 1000) * 1000;
  89. /* client_bind_timeout */
  90. client_bind_timeout_ms = iniparser_getint(
  91. _d,
  92. get_ini_key_name(_name, CFG_N_LDAP_CLIENT_BIND_TIMEOUT),
  93. CFG_DEF_LDAP_CLIENT_BIND_TIMEOUT);
  94. new_lds->client_bind_timeout.tv_sec = client_bind_timeout_ms / 1000;
  95. new_lds->client_bind_timeout.tv_usec =
  96. (client_bind_timeout_ms % 1000) * 1000;
  97. /* network_timeout */
  98. network_timeout_ms = iniparser_getint(
  99. _d,
  100. get_ini_key_name(_name, CFG_N_LDAP_NETWORK_TIMEOUT),
  101. LDAP_NO_LIMIT);
  102. new_lds->network_timeout.tv_sec = network_timeout_ms / 1000;
  103. new_lds->network_timeout.tv_usec = (network_timeout_ms % 1000) * 1000;
  104. /* bind_dn */
  105. bind_dn = iniparser_getstring(
  106. _d,
  107. get_ini_key_name(_name, CFG_N_LDAP_BIND_DN),
  108. CFG_DEF_LDAP_BIND_DN);
  109. new_lds->bind_dn = (char*)pkg_malloc(strlen(bind_dn)+1);
  110. if (new_lds->bind_dn == NULL) {
  111. LM_ERR("no memory\n");
  112. return -1;
  113. }
  114. strcpy(new_lds->bind_dn, bind_dn);
  115. /* bind_pwd */
  116. bind_pwd = iniparser_getstring(
  117. _d,
  118. get_ini_key_name(_name, CFG_N_LDAP_BIND_PWD),
  119. CFG_DEF_LDAP_BIND_PWD);
  120. new_lds->bind_pwd = (char*)pkg_malloc(strlen(bind_pwd)+1);
  121. if (new_lds->bind_pwd == NULL) {
  122. LM_ERR("no memory\n");
  123. return -1;
  124. }
  125. strcpy(new_lds->bind_pwd, bind_pwd);
  126. /* calculate_ha1 */
  127. new_lds->calculate_ha1 = iniparser_getboolean(
  128. _d,
  129. get_ini_key_name(_name, CFG_N_CALCULATE_HA1),
  130. CFG_DEF_CALCULATE_HA1);
  131. if (current == NULL)
  132. {
  133. ld_sessions = new_lds;
  134. } else
  135. {
  136. while (current->next != NULL) { current = current->next; };
  137. current->next = new_lds;
  138. }
  139. return 0;
  140. }
  141. struct ld_session* get_ld_session(char* _name)
  142. {
  143. struct ld_session* current = ld_sessions;
  144. if (_name == NULL)
  145. {
  146. LM_ERR("lds_name == NULL\n");
  147. return NULL;
  148. }
  149. while (current != NULL)
  150. {
  151. if (strcmp(current->name, _name) == 0)
  152. {
  153. return current;
  154. }
  155. current = current->next;
  156. }
  157. return NULL;
  158. }
  159. int free_ld_sessions(void)
  160. {
  161. struct ld_session* current = ld_sessions;
  162. struct ld_session* tmp;
  163. while (current != NULL)
  164. {
  165. tmp = current->next;
  166. if (current->handle != NULL)
  167. {
  168. ldap_unbind_ext(current->handle, NULL, NULL);
  169. }
  170. if (current->host_name != NULL)
  171. {
  172. pkg_free(current->host_name);
  173. }
  174. if (current->bind_dn != NULL)
  175. {
  176. pkg_free(current->bind_dn);
  177. }
  178. if (current->bind_pwd != NULL)
  179. {
  180. pkg_free(current->bind_pwd);
  181. }
  182. pkg_free(current);
  183. current = tmp;
  184. }
  185. ld_sessions = NULL;
  186. return 0;
  187. }
  188. char* get_ini_key_name(char* _section, char* _key)
  189. {
  190. sprintf(ini_key_name, "%s:%s", _section, _key);
  191. return ini_key_name;
  192. }