cb_bind.c 9.4 KB

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