cb_bind.c 9.5 KB

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