cb_compare.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. #include "cb.h"
  7. /*
  8. * Perform a compare operation
  9. *
  10. * Returns:
  11. * 0 - success
  12. * <0 - fail
  13. *
  14. */
  15. int
  16. chaining_back_compare ( Slapi_PBlock *pb )
  17. {
  18. Slapi_Backend * be;
  19. cb_backend_instance *cb=NULL;
  20. struct berval *bval=NULL;
  21. LDAPControl **ctrls, **serverctrls;
  22. int rc,parse_rc,msgid,i,checkacl;
  23. LDAP *ld=NULL;
  24. char **referrals=NULL;
  25. LDAPMessage * res;
  26. char *type,*dn,* matched_msg, *error_msg;
  27. char *cnxerrbuf=NULL;
  28. time_t endtime;
  29. cb_outgoing_conn *cnx;
  30. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  31. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  32. return -1;
  33. }
  34. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  35. cb = cb_get_instance(be);
  36. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_COMPARE);
  37. /* Check wether the chaining BE is available or not */
  38. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  39. return -1;
  40. }
  41. slapi_pblock_get( pb, SLAPI_COMPARE_TARGET, &dn );
  42. slapi_pblock_get( pb, SLAPI_COMPARE_TYPE, &type );
  43. slapi_pblock_get( pb, SLAPI_COMPARE_VALUE, &bval );
  44. /*
  45. * Check local acls
  46. * No need to lock the config to access cb->local_acl
  47. */
  48. checkacl=cb->local_acl && !cb->associated_be_is_disabled;
  49. if (checkacl) {
  50. char * errbuf=NULL;
  51. Slapi_Entry *te = slapi_entry_alloc();
  52. slapi_entry_set_dn(te,slapi_ch_strdup(dn));
  53. rc = cb_access_allowed (pb, te, type, bval, SLAPI_ACL_COMPARE,&errbuf);
  54. slapi_entry_free(te);
  55. if ( rc != LDAP_SUCCESS ) {
  56. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  57. slapi_ch_free((void **) &errbuf);
  58. return 1;
  59. }
  60. }
  61. /*
  62. * Grab a connection handle
  63. */
  64. if ((rc = cb_get_connection(cb->pool,&ld,&cnx,NULL,&cnxerrbuf)) != LDAP_SUCCESS) {
  65. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL, cnxerrbuf, 0, NULL);
  66. slapi_ch_free((void **)&cnxerrbuf);
  67. /* ping the farm. If the farm is unreachable, we increment the counter */
  68. cb_ping_farm(cb,NULL,0);
  69. return 1;
  70. }
  71. /*
  72. * Control management
  73. */
  74. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  75. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  76. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  77. return 1;
  78. }
  79. if ( slapi_op_abandoned( pb )) {
  80. cb_release_op_connection(cb->pool,ld,0);
  81. if ( NULL != ctrls)
  82. ldap_controls_free(ctrls);
  83. return -1;
  84. }
  85. /* heart-beat management */
  86. if (cb->max_idle_time>0)
  87. endtime=current_time() + cb->max_idle_time;
  88. /*
  89. * Send LDAP operation to the remote host
  90. */
  91. rc = ldap_compare_ext( ld, dn, type, bval, ctrls, NULL, &msgid );
  92. if ( NULL != ctrls)
  93. ldap_controls_free(ctrls);
  94. if ( rc != LDAP_SUCCESS ) {
  95. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  96. ldap_err2string(rc), 0, NULL);
  97. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  98. return 1;
  99. }
  100. while ( 1 ) {
  101. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  102. return -1;
  103. }
  104. /* No need to lock the config to access cb->abandon_timeout */
  105. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  106. switch ( rc ) {
  107. case -1:
  108. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  109. ldap_err2string(rc), 0, NULL);
  110. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  111. if (res)
  112. ldap_msgfree(res);
  113. return 1;
  114. case 0:
  115. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  116. /* does not respond. give up and return a*/
  117. /* error to the client. */
  118. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  119. ldap_err2string(rc), 0, NULL);*/
  120. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  121. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  122. if (res)
  123. ldap_msgfree(res);
  124. return 1;
  125. }
  126. #ifdef CB_YIELD
  127. DS_Sleep(PR_INTERVAL_NO_WAIT);
  128. #endif
  129. break;
  130. default:
  131. matched_msg=error_msg=NULL;
  132. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  133. &error_msg, &referrals, &serverctrls, 1 );
  134. if ( parse_rc != LDAP_SUCCESS ) {
  135. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  136. ldap_err2string(parse_rc), 0, NULL);
  137. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  138. slapi_ch_free((void **)&matched_msg);
  139. slapi_ch_free((void **)&error_msg);
  140. if (serverctrls)
  141. ldap_controls_free(serverctrls);
  142. /* jarnou: free referrals */
  143. if (referrals)
  144. charray_free(referrals);
  145. return 1;
  146. }
  147. switch ( rc ) {
  148. case LDAP_COMPARE_TRUE:
  149. case LDAP_COMPARE_FALSE:
  150. break;
  151. default: {
  152. struct berval ** refs = referrals2berval(referrals);
  153. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  154. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  155. slapi_ch_free((void **)&matched_msg);
  156. slapi_ch_free((void **)&error_msg);
  157. if (refs)
  158. ber_bvecfree(refs);
  159. if (referrals)
  160. charray_free(referrals);
  161. if (serverctrls)
  162. ldap_controls_free(serverctrls);
  163. return 1;
  164. }
  165. }
  166. /* Add control response sent by the farm server */
  167. for (i=0; serverctrls && serverctrls[i];i++)
  168. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  169. if (serverctrls)
  170. ldap_controls_free(serverctrls);
  171. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  172. slapi_ch_free((void **)&matched_msg);
  173. slapi_ch_free((void **)&error_msg);
  174. if (referrals)
  175. charray_free(referrals);
  176. cb_send_ldap_result( pb, rc , NULL, NULL, 0, NULL );
  177. cb_release_op_connection(cb->pool,ld,0);
  178. return 0;
  179. }
  180. }
  181. /* Never reached */
  182. /* return 0; */
  183. }