cb_delete.c 6.2 KB

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