cb_compare.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /*
  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. if (cnxerrbuf) {
  67. PR_smprintf_free(cnxerrbuf);
  68. }
  69. /* ping the farm. If the farm is unreachable, we increment the counter */
  70. cb_ping_farm(cb,NULL,0);
  71. return 1;
  72. }
  73. /*
  74. * Control management
  75. */
  76. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  77. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  78. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  79. return 1;
  80. }
  81. if ( slapi_op_abandoned( pb )) {
  82. cb_release_op_connection(cb->pool,ld,0);
  83. if ( NULL != ctrls)
  84. ldap_controls_free(ctrls);
  85. return -1;
  86. }
  87. /* heart-beat management */
  88. if (cb->max_idle_time>0)
  89. endtime=current_time() + cb->max_idle_time;
  90. /*
  91. * Send LDAP operation to the remote host
  92. */
  93. rc = ldap_compare_ext( ld, dn, type, bval, ctrls, NULL, &msgid );
  94. if ( NULL != ctrls)
  95. ldap_controls_free(ctrls);
  96. if ( rc != LDAP_SUCCESS ) {
  97. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  98. ldap_err2string(rc), 0, NULL);
  99. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  100. return 1;
  101. }
  102. while ( 1 ) {
  103. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  104. return -1;
  105. }
  106. /* No need to lock the config to access cb->abandon_timeout */
  107. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  108. switch ( rc ) {
  109. case -1:
  110. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  111. ldap_err2string(rc), 0, NULL);
  112. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  113. if (res)
  114. ldap_msgfree(res);
  115. return 1;
  116. case 0:
  117. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  118. /* does not respond. give up and return a*/
  119. /* error to the client. */
  120. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  121. ldap_err2string(rc), 0, NULL);*/
  122. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  123. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  124. if (res)
  125. ldap_msgfree(res);
  126. return 1;
  127. }
  128. #ifdef CB_YIELD
  129. DS_Sleep(PR_INTERVAL_NO_WAIT);
  130. #endif
  131. break;
  132. default:
  133. matched_msg=error_msg=NULL;
  134. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  135. &error_msg, &referrals, &serverctrls, 1 );
  136. if ( parse_rc != LDAP_SUCCESS ) {
  137. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  138. ldap_err2string(parse_rc), 0, NULL);
  139. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  140. slapi_ch_free((void **)&matched_msg);
  141. slapi_ch_free((void **)&error_msg);
  142. if (serverctrls)
  143. ldap_controls_free(serverctrls);
  144. /* jarnou: free referrals */
  145. if (referrals)
  146. charray_free(referrals);
  147. return 1;
  148. }
  149. switch ( rc ) {
  150. case LDAP_COMPARE_TRUE:
  151. case LDAP_COMPARE_FALSE:
  152. break;
  153. default: {
  154. struct berval ** refs = referrals2berval(referrals);
  155. cb_send_ldap_result( pb, rc, matched_msg, error_msg, 0, refs);
  156. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  157. slapi_ch_free((void **)&matched_msg);
  158. slapi_ch_free((void **)&error_msg);
  159. if (refs)
  160. ber_bvecfree(refs);
  161. if (referrals)
  162. charray_free(referrals);
  163. if (serverctrls)
  164. ldap_controls_free(serverctrls);
  165. return 1;
  166. }
  167. }
  168. /* Add control response sent by the farm server */
  169. for (i=0; serverctrls && serverctrls[i];i++)
  170. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  171. if (serverctrls)
  172. ldap_controls_free(serverctrls);
  173. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  174. slapi_ch_free((void **)&matched_msg);
  175. slapi_ch_free((void **)&error_msg);
  176. if (referrals)
  177. charray_free(referrals);
  178. cb_send_ldap_result( pb, rc , NULL, NULL, 0, NULL );
  179. cb_release_op_connection(cb->pool,ld,0);
  180. return 0;
  181. }
  182. }
  183. /* Never reached */
  184. /* return 0; */
  185. }