cb_bind.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #include "cb.h"
  13. static void
  14. cb_free_bervals(struct berval **bvs);
  15. static int
  16. cb_sasl_bind_once_s(cb_conn_pool *pool, const char *dn, ber_tag_t method, char *mechanism, struct berval *creds, LDAPControl **reqctrls, char **matcheddnp, char **errmsgp, struct berval ***refurlsp, LDAPControl ***resctrlsp, int *status);
  17. /*
  18. * Attempt to chain a bind request off to "srvr." We return an LDAP error
  19. * code that indicates whether we successfully got a response from the
  20. * other server or not. If we succeed, we return LDAP_SUCCESS and *lderrnop
  21. * is set to the result code from the remote server.
  22. *
  23. * Note that in the face of "ldap server down" or "ldap connect failed" errors
  24. * we make up to "tries" attempts to bind to the remote server. Since we
  25. * are only interested in recovering silently when the remote server is up
  26. * but decided to close our connection, we retry without pausing between
  27. * attempts.
  28. */
  29. static int
  30. cb_sasl_bind_s(Slapi_PBlock *pb, cb_conn_pool *pool, int tries, const char *dn, ber_tag_t method, char *mechanism, struct berval *creds, LDAPControl **reqctrls, char **matcheddnp, char **errmsgp, struct berval ***refurlsp, LDAPControl ***resctrlsp, int *status)
  31. {
  32. int rc;
  33. do {
  34. /* check to see if operation has been abandoned...*/
  35. if (LDAP_AUTH_SIMPLE != method) {
  36. return LDAP_AUTH_METHOD_NOT_SUPPORTED;
  37. }
  38. if (slapi_op_abandoned(pb)) {
  39. rc = LDAP_USER_CANCELLED;
  40. } else {
  41. rc = cb_sasl_bind_once_s(pool, dn, method, mechanism, creds, reqctrls,
  42. matcheddnp, errmsgp, refurlsp, resctrlsp, status);
  43. }
  44. } while (CB_LDAP_CONN_ERROR(rc) && --tries > 0);
  45. return (rc);
  46. }
  47. static int
  48. cb_sasl_bind_once_s(cb_conn_pool *pool, const char *dn, ber_tag_t method __attribute__((unused)), char *mechanism __attribute__((unused)), struct berval *creds, LDAPControl **reqctrls, char **matcheddnp, char **errmsgp, struct berval ***refurlsp, LDAPControl ***resctrlsp, int *status)
  49. {
  50. int rc, msgid;
  51. char **referrals;
  52. struct timeval timeout_copy, *timeout;
  53. LDAPMessage *result = NULL;
  54. LDAP *ld = NULL;
  55. char *cnxerrbuf = NULL;
  56. cb_outgoing_conn *cnx;
  57. int version = LDAP_VERSION3;
  58. /* Grab an LDAP connection to use for this bind. */
  59. slapi_rwlock_rdlock(pool->rwl_config_lock);
  60. timeout_copy.tv_sec = pool->conn.bind_timeout.tv_sec;
  61. timeout_copy.tv_usec = pool->conn.bind_timeout.tv_usec;
  62. slapi_rwlock_unlock(pool->rwl_config_lock);
  63. rc = cb_get_connection(pool, &ld, &cnx, NULL, &cnxerrbuf);
  64. if (LDAP_SUCCESS != rc) {
  65. static int warned_get_conn = 0;
  66. if (!warned_get_conn) {
  67. slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
  68. "cb_sasl_bind_once_s - cb_get_connection failed (%d) %s\n",
  69. rc, ldap_err2string(rc));
  70. warned_get_conn = 1;
  71. }
  72. *errmsgp = cnxerrbuf;
  73. goto release_and_return;
  74. }
  75. /* Send the bind operation (need to retry on LDAP_SERVER_DOWN) */
  76. ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &version);
  77. if ((rc = ldap_sasl_bind(ld, dn, LDAP_SASL_SIMPLE, creds, reqctrls,
  78. NULL, &msgid)) != LDAP_SUCCESS) {
  79. goto release_and_return;
  80. }
  81. /* XXXSD what is the exact semantics of bind_to ? it is used to get a
  82. connection handle and later to bind ==> bind op may last 2*bind_to
  83. from the user point of view
  84. confusion comes from teh fact that bind to is used 2for 3 differnt thinks,
  85. */
  86. /*
  87. * determine timeout value (how long we will wait for a response)
  88. * if timeout is zero'd, we wait indefinitely.
  89. */
  90. if (timeout_copy.tv_sec == 0 && timeout_copy.tv_usec == 0) {
  91. timeout = NULL;
  92. } else {
  93. timeout = &timeout_copy;
  94. }
  95. /*
  96. * Wait for a result.
  97. */
  98. rc = ldap_result(ld, msgid, 1, timeout, &result);
  99. /*
  100. * Interpret the result.
  101. */
  102. if (rc == 0) { /* timeout */
  103. /*
  104. * Timed out waiting for a reply from the server.
  105. */
  106. rc = LDAP_TIMEOUT;
  107. } else if (rc < 0) {
  108. /* Some other error occurred (no result received). */
  109. char *matcheddnp2, *errmsgp2;
  110. matcheddnp2 = errmsgp2 = NULL;
  111. rc = slapi_ldap_get_lderrno(ld, &matcheddnp2, &errmsgp2);
  112. /* Need to allocate errmsgs */
  113. if (matcheddnp2)
  114. *matcheddnp = slapi_ch_strdup(matcheddnp2);
  115. if (errmsgp2)
  116. *errmsgp = slapi_ch_strdup(errmsgp2);
  117. if (LDAP_SUCCESS != rc) {
  118. static int warned_bind_once = 0;
  119. if (!warned_bind_once) {
  120. int hasmatched = (matcheddnp && *matcheddnp && (**matcheddnp != '\0'));
  121. slapi_log_err(SLAPI_LOG_ERR, CB_PLUGIN_SUBSYSTEM,
  122. "cb_sasl_bind_once_s - Failed (%s%s%s)\n",
  123. hasmatched ? *matcheddnp : "",
  124. hasmatched ? ": " : "",
  125. ldap_err2string(rc));
  126. warned_bind_once = 1;
  127. }
  128. }
  129. } else {
  130. /* Got a result from remote server -- parse it.*/
  131. char *matcheddnp2, *errmsgp2;
  132. matcheddnp2 = errmsgp2 = NULL;
  133. *resctrlsp = NULL;
  134. rc = ldap_parse_result(ld, result, status, &matcheddnp2, &errmsgp2,
  135. &referrals, resctrlsp, 1);
  136. if (referrals != NULL) {
  137. *refurlsp = referrals2berval(referrals);
  138. slapi_ldap_value_free(referrals);
  139. }
  140. /* realloc matcheddn & errmsg because the mem alloc model */
  141. /* may differ from malloc */
  142. if (matcheddnp2) {
  143. *matcheddnp = slapi_ch_strdup(matcheddnp2);
  144. ldap_memfree(matcheddnp2);
  145. }
  146. if (errmsgp2) {
  147. *errmsgp = slapi_ch_strdup(errmsgp2);
  148. ldap_memfree(errmsgp2);
  149. }
  150. }
  151. release_and_return:
  152. if (ld != NULL) {
  153. cb_release_op_connection(pool, ld, CB_LDAP_CONN_ERROR(rc));
  154. }
  155. return (rc);
  156. }
  157. int
  158. chainingdb_bind(Slapi_PBlock *pb)
  159. {
  160. cb_backend_instance *cb;
  161. Slapi_Backend *be;
  162. struct berval *creds = NULL, **urls = NULL;
  163. const char *dn = NULL;
  164. Slapi_DN *sdn = NULL;
  165. Slapi_DN *mysdn = NULL;
  166. char *matcheddn = NULL, *errmsg = NULL;
  167. LDAPControl **reqctrls = NULL, **resctrls = NULL, **ctrls = NULL;
  168. char *mechanism = NULL;
  169. int status = LDAP_SUCCESS;
  170. int allocated_errmsg = 0;
  171. int rc = LDAP_SUCCESS;
  172. int freectrls = 1;
  173. int bind_retry;
  174. ber_tag_t method;
  175. if (LDAP_SUCCESS != (rc = cb_forward_operation(pb))) {
  176. cb_send_ldap_result(pb, rc, NULL, "Chaining forbidden", 0, NULL);
  177. return SLAPI_BIND_FAIL;
  178. }
  179. /* don't add proxy auth control. use this call to check for supported */
  180. /* controls only. */
  181. if (LDAP_SUCCESS != (rc = cb_update_controls(pb, NULL, &ctrls, 0))) {
  182. cb_send_ldap_result(pb, rc, NULL, NULL, 0, NULL);
  183. if (ctrls)
  184. ldap_controls_free(ctrls);
  185. return SLAPI_BIND_FAIL;
  186. }
  187. if (ctrls)
  188. ldap_controls_free(ctrls);
  189. slapi_pblock_get(pb, SLAPI_BACKEND, &be);
  190. slapi_pblock_get(pb, SLAPI_BIND_TARGET_SDN, &sdn);
  191. slapi_pblock_get(pb, SLAPI_BIND_METHOD, &method);
  192. slapi_pblock_get(pb, SLAPI_BIND_SASLMECHANISM, &mechanism);
  193. slapi_pblock_get(pb, SLAPI_BIND_CREDENTIALS, &creds);
  194. if (NULL == creds) {
  195. cb_send_ldap_result(pb, rc, NULL, "No credentials", 0, NULL);
  196. return SLAPI_BIND_FAIL;
  197. }
  198. slapi_pblock_get(pb, SLAPI_REQCONTROLS, &reqctrls);
  199. cb = cb_get_instance(be);
  200. if (NULL == sdn) {
  201. sdn = mysdn = slapi_sdn_new_ndn_byval("");
  202. }
  203. dn = slapi_sdn_get_ndn(sdn);
  204. /* always allow noauth simple binds */
  205. if ((method == LDAP_AUTH_SIMPLE) && (creds->bv_len == 0)) {
  206. slapi_sdn_free(&mysdn);
  207. return (SLAPI_BIND_ANONYMOUS);
  208. }
  209. cb_update_monitor_info(pb, cb, SLAPI_OPERATION_BIND);
  210. /* Check wether the chaining BE is available or not */
  211. if (cb_check_availability(cb, pb) == FARMSERVER_UNAVAILABLE) {
  212. slapi_sdn_free(&mysdn);
  213. return -1;
  214. }
  215. slapi_rwlock_rdlock(cb->rwl_config_lock);
  216. bind_retry = cb->bind_retry;
  217. slapi_rwlock_unlock(cb->rwl_config_lock);
  218. rc = cb_sasl_bind_s(pb, cb->bind_pool, bind_retry, dn, method,
  219. mechanism, creds, reqctrls, &matcheddn, &errmsg,
  220. &urls, &resctrls, &status);
  221. if (LDAP_SUCCESS == rc) {
  222. rc = status;
  223. allocated_errmsg = 1;
  224. } else if (LDAP_USER_CANCELLED != rc) {
  225. slapi_ch_free_string(&errmsg);
  226. errmsg = ldap_err2string(rc);
  227. if (rc == LDAP_TIMEOUT) {
  228. cb_ping_farm(cb, NULL, 0);
  229. }
  230. rc = LDAP_OPERATIONS_ERROR;
  231. } else {
  232. allocated_errmsg = 1;
  233. }
  234. if (rc != LDAP_USER_CANCELLED) { /* not abandoned */
  235. if (resctrls != NULL) {
  236. slapi_pblock_set(pb, SLAPI_RESCONTROLS, resctrls);
  237. freectrls = 0;
  238. }
  239. if (rc != LDAP_SUCCESS) {
  240. cb_send_ldap_result(pb, rc, matcheddn, errmsg, 0, urls);
  241. }
  242. }
  243. if (urls != NULL) {
  244. cb_free_bervals(urls);
  245. }
  246. if (freectrls && (resctrls != NULL)) {
  247. ldap_controls_free(resctrls);
  248. }
  249. slapi_ch_free_string(&matcheddn);
  250. if (allocated_errmsg) {
  251. slapi_ch_free_string(&errmsg);
  252. }
  253. slapi_sdn_free(&mysdn);
  254. return ((rc == LDAP_SUCCESS) ? SLAPI_BIND_SUCCESS : SLAPI_BIND_FAIL);
  255. }
  256. static void
  257. cb_free_bervals(struct berval **bvs)
  258. {
  259. int i;
  260. if (bvs != NULL) {
  261. for (i = 0; bvs[i] != NULL; ++i) {
  262. slapi_ch_free((void **)&bvs[i]);
  263. }
  264. }
  265. slapi_ch_free((void **)&bvs);
  266. }