ld_con.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * $Id$
  3. *
  4. * LDAP Database Driver for SER
  5. *
  6. * Copyright (C) 2008 iptelorg GmbH
  7. *
  8. * This file is part of SER, a free SIP server.
  9. *
  10. * SER is free software; you can redistribute it and/or modify it under the
  11. * terms of the GNU General Public License as published by the Free Software
  12. * Foundation; either version 2 of the License, or (at your option) any later
  13. * version.
  14. *
  15. * SER is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  23. */
  24. /** \addtogroup ldap
  25. * @{
  26. */
  27. /** \file
  28. * Functions related to connections to LDAP servers.
  29. */
  30. #include "ld_con.h"
  31. #include "ld_uri.h"
  32. #include "../../mem/mem.h"
  33. #include "../../dprint.h"
  34. #include "../../ut.h"
  35. #include <ldap.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <sasl/sasl.h>
  39. /** Free all memory allocated for a ld_con structure.
  40. * This function function frees all memory that is in use by
  41. * a ld_con structure.
  42. * @param con A generic db_con connection structure.
  43. * @param payload LDAP specific payload to be freed.
  44. */
  45. static void ld_con_free(db_con_t* con, struct ld_con* payload)
  46. {
  47. struct ld_uri* luri;
  48. int ret;
  49. if (!payload) return;
  50. luri = DB_GET_PAYLOAD(con->uri);
  51. /* Delete the structure only if there are no more references
  52. * to it in the connection pool
  53. */
  54. if (db_pool_remove((db_pool_entry_t*)payload) == 0) return;
  55. db_pool_entry_free(&payload->gen);
  56. if (payload->con) {
  57. ret = ldap_unbind_ext_s(payload->con, NULL, NULL);
  58. if (ret != LDAP_SUCCESS) {
  59. ERR("ldap: Error while unbinding from %s: %s\n",
  60. luri->uri, ldap_err2string(ret));
  61. }
  62. }
  63. pkg_free(payload);
  64. }
  65. int ld_con(db_con_t* con)
  66. {
  67. struct ld_con* lcon;
  68. struct ld_uri* luri;
  69. luri = DB_GET_PAYLOAD(con->uri);
  70. /* First try to lookup the connection in the connection pool and
  71. * re-use it if a match is found
  72. */
  73. lcon = (struct ld_con*)db_pool_get(con->uri);
  74. if (lcon) {
  75. DBG("ldap: Connection to %s found in connection pool\n",
  76. luri->uri);
  77. goto found;
  78. }
  79. lcon = (struct ld_con*)pkg_malloc(sizeof(struct ld_con));
  80. if (!lcon) {
  81. ERR("ldap: No memory left\n");
  82. goto error;
  83. }
  84. memset(lcon, '\0', sizeof(struct ld_con));
  85. if (db_pool_entry_init(&lcon->gen, ld_con_free, con->uri) < 0) goto error;
  86. DBG("ldap: Preparing new connection to %s\n", luri->uri);
  87. /* Put the newly created LDAP connection into the pool */
  88. db_pool_put((struct db_pool_entry*)lcon);
  89. DBG("ldap: Connection stored in connection pool\n");
  90. found:
  91. /* Attach driver payload to the db_con structure and set connect and
  92. * disconnect functions
  93. */
  94. DB_SET_PAYLOAD(con, lcon);
  95. con->connect = ld_con_connect;
  96. con->disconnect = ld_con_disconnect;
  97. return 0;
  98. error:
  99. if (lcon) {
  100. db_pool_entry_free(&lcon->gen);
  101. pkg_free(lcon);
  102. }
  103. return -1;
  104. }
  105. int lutil_sasl_interact(
  106. LDAP *ld,
  107. unsigned flags,
  108. void *defaults,
  109. void *in )
  110. {
  111. sasl_interact_t *interact = in;
  112. const char *dflt = interact->defresult;
  113. if (ld == NULL)
  114. return LDAP_PARAM_ERROR;
  115. while (interact->id != SASL_CB_LIST_END) {
  116. switch( interact->id ) {
  117. // the username to authenticate
  118. case SASL_CB_AUTHNAME:
  119. if (defaults)
  120. dflt = ((struct ld_uri*)defaults)->username;
  121. break;
  122. // the password for the provided username
  123. case SASL_CB_PASS:
  124. if (defaults)
  125. dflt = ((struct ld_uri*)defaults)->password;
  126. break;
  127. // the realm for the authentication attempt
  128. case SASL_CB_GETREALM:
  129. // the username to use for proxy authorization
  130. case SASL_CB_USER:
  131. // generic prompt for input with input echoing disabled
  132. case SASL_CB_NOECHOPROMPT:
  133. // generic prompt for input with input echoing enabled
  134. case SASL_CB_ECHOPROMPT:
  135. break;
  136. }
  137. interact->result = (dflt && *dflt) ? dflt : "";
  138. interact->len = strlen(interact->result);
  139. interact++;
  140. }
  141. return LDAP_SUCCESS;
  142. }
  143. int ld_con_connect(db_con_t* con)
  144. {
  145. struct ld_con* lcon;
  146. struct ld_uri* luri;
  147. int ret, version = 3;
  148. char* err_str = NULL;
  149. lcon = DB_GET_PAYLOAD(con);
  150. luri = DB_GET_PAYLOAD(con->uri);
  151. /* Do not reconnect already connected connections */
  152. if (lcon->flags & LD_CONNECTED) return 0;
  153. DBG("ldap: Connecting to %s\n", luri->uri);
  154. if (lcon->con) {
  155. ret = ldap_unbind_ext_s(lcon->con, NULL, NULL);
  156. if (ret != LDAP_SUCCESS) {
  157. ERR("ldap: Error while unbinding from %s: %s\n",
  158. luri->uri, ldap_err2string(ret));
  159. }
  160. }
  161. /* we pass the TLS_REQCERT and TLS_REQCERT attributes over environment
  162. variables to ldap library */
  163. if (luri->tls) {
  164. if (setenv("LDAPTLS_CACERT", luri->ca_list, 1)) {
  165. ERR("ldap: Can't set environment variable 'LDAPTLS_CACERT'\n");
  166. goto error;
  167. }
  168. if (setenv("LDAPTLS_REQCERT", luri->req_cert, 1)) {
  169. ERR("ldap: Can't set environment variable 'LDAPTLS_REQCERT'\n");
  170. goto error;
  171. }
  172. }
  173. ret = ldap_initialize(&lcon->con, luri->uri);
  174. if (lcon->con == NULL) {
  175. ERR("ldap: Error while initializing new LDAP connection to %s\n",
  176. luri->uri);
  177. goto error;
  178. }
  179. ret = ldap_set_option(lcon->con, LDAP_OPT_PROTOCOL_VERSION, &version);
  180. if (ret != LDAP_OPT_SUCCESS) {
  181. ERR("ldap: Error while setting protocol version 3: %s\n",
  182. ldap_err2string(ret));
  183. goto error;
  184. }
  185. if (luri->tls) {
  186. ret = ldap_start_tls_s(lcon->con, NULL, NULL);
  187. if (ret != LDAP_SUCCESS) {
  188. /* get addition info of this error */
  189. #ifdef OPENLDAP23
  190. ldap_get_option(lcon->con, LDAP_OPT_ERROR_STRING, &err_str);
  191. #elif OPENLDAP24
  192. ldap_get_option(lcon->con, LDAP_OPT_DIAGNOSTIC_MESSAGE, &err_str);
  193. #endif
  194. ERR("ldap: Error while starting TLS: %s\n", ldap_err2string(ret));
  195. if (err_str) {
  196. ERR("ldap: %s\n", err_str);
  197. ldap_memfree(err_str);
  198. }
  199. goto error;
  200. }
  201. }
  202. switch (luri->authmech) {
  203. case LDAP_AUTHMECH_NONE:
  204. ret = ldap_simple_bind_s(lcon->con, NULL, NULL);
  205. break;
  206. case LDAP_AUTHMECH_SIMPLE:
  207. ret = ldap_simple_bind_s(lcon->con, luri->username, luri->password);
  208. break;
  209. case LDAP_AUTHMECH_DIGESTMD5:
  210. ret = ldap_sasl_interactive_bind_s( lcon->con, NULL,
  211. LDAP_MECHANISM_STR_DIGESTMD5, NULL, NULL,
  212. 0, lutil_sasl_interact, luri );
  213. break;
  214. case LDAP_AUTHMECH_EXTERNAL:
  215. default:
  216. ret = !LDAP_SUCCESS;
  217. break;
  218. }
  219. if (ret != LDAP_SUCCESS) {
  220. ERR("ldap: Bind to %s failed: %s\n",
  221. luri->uri, ldap_err2string(ret));
  222. goto error;
  223. }
  224. DBG("ldap: Successfully bound to %s\n", luri->uri);
  225. lcon->flags |= LD_CONNECTED;
  226. return 0;
  227. error:
  228. if (lcon->con) {
  229. ret = ldap_unbind_ext_s(lcon->con, NULL, NULL);
  230. if (ret) {
  231. ERR("ldap: Error while unbinding from %s: %s\n",
  232. luri->uri, ldap_err2string(ret));
  233. }
  234. }
  235. lcon->con = NULL;
  236. return -1;
  237. }
  238. void ld_con_disconnect(db_con_t* con)
  239. {
  240. struct ld_con* lcon;
  241. struct ld_uri* luri;
  242. int ret;
  243. lcon = DB_GET_PAYLOAD(con);
  244. luri = DB_GET_PAYLOAD(con->uri);
  245. if ((lcon->flags & LD_CONNECTED) == 0) return;
  246. DBG("ldap: Unbinding from %s\n", luri->uri);
  247. if (lcon->con) {
  248. ret = ldap_unbind_ext_s(lcon->con, NULL, NULL);
  249. if (ret) {
  250. ERR("ldap: Error while unbinding from %s: %s\n",
  251. luri->uri, ldap_err2string(ret));
  252. }
  253. }
  254. lcon->con = NULL;
  255. lcon->flags &= ~LD_CONNECTED;
  256. }
  257. /** @} */