cb_bind.c 8.6 KB

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