1
0

cb_delete.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. #include "cb.h"
  42. /*
  43. * Perform an delete operation
  44. *
  45. * Returns:
  46. * 0 - success
  47. * <0 - fail
  48. *
  49. */
  50. int
  51. chaining_back_delete ( Slapi_PBlock *pb )
  52. {
  53. Slapi_Backend * be;
  54. cb_backend_instance *cb;
  55. LDAPControl **ctrls, **serverctrls;
  56. int rc,parse_rc,msgid,i;
  57. LDAP *ld=NULL;
  58. char **referrals=NULL;
  59. LDAPMessage * res;
  60. const char *dn = NULL;
  61. Slapi_DN *sdn = NULL;
  62. char *matched_msg, *error_msg;
  63. char *cnxerrbuf=NULL;
  64. time_t endtime = 0;
  65. cb_outgoing_conn *cnx;
  66. if ( LDAP_SUCCESS != (rc=cb_forward_operation(pb) )) {
  67. cb_send_ldap_result( pb, rc, NULL, "Chaining forbidden", 0, NULL );
  68. return -1;
  69. }
  70. slapi_pblock_get( pb, SLAPI_BACKEND, &be );
  71. cb = cb_get_instance(be);
  72. cb_update_monitor_info(pb,cb,SLAPI_OPERATION_DELETE);
  73. /* Check wether the chaining BE is available or not */
  74. if ( cb_check_availability( cb, pb ) == FARMSERVER_UNAVAILABLE ){
  75. return -1;
  76. }
  77. slapi_pblock_get( pb, SLAPI_DELETE_TARGET_SDN, &sdn );
  78. dn = slapi_sdn_get_dn(sdn);
  79. /*
  80. * Check local acls
  81. */
  82. if (cb->local_acl && !cb->associated_be_is_disabled) {
  83. char * errbuf=NULL;
  84. Slapi_Entry *te = slapi_entry_alloc();
  85. slapi_entry_set_sdn(te, sdn); /* sdn: copied */
  86. rc = cb_access_allowed (pb, te, NULL, NULL, SLAPI_ACL_DELETE,&errbuf);
  87. slapi_entry_free(te);
  88. if ( rc != LDAP_SUCCESS ) {
  89. cb_send_ldap_result( pb, rc, NULL, errbuf, 0, NULL );
  90. slapi_ch_free((void **)&errbuf);
  91. return -1;
  92. }
  93. }
  94. /*
  95. * Grab a connection handle
  96. */
  97. rc = cb_get_connection(cb->pool, &ld, &cnx, NULL, &cnxerrbuf);
  98. if (LDAP_SUCCESS != rc) {
  99. static int warned_get_conn = 0;
  100. if (!warned_get_conn) {
  101. slapi_log_error(SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  102. "cb_get_connection failed (%d) %s\n",
  103. rc, ldap_err2string(rc));
  104. warned_get_conn = 1;
  105. }
  106. cb_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULL,
  107. cnxerrbuf, 0, NULL);
  108. slapi_ch_free_string(&cnxerrbuf);
  109. /* ping the farm.
  110. * If the farm is unreachable, we increment the counter */
  111. cb_ping_farm(cb, NULL, 0);
  112. return -1;
  113. }
  114. /*
  115. * Control management
  116. */
  117. if ( (rc = cb_update_controls( pb,ld,&ctrls,CB_UPDATE_CONTROLS_ADDAUTH )) != LDAP_SUCCESS ) {
  118. cb_send_ldap_result( pb, rc, NULL,NULL, 0, NULL);
  119. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  120. return -1;
  121. }
  122. if ( slapi_op_abandoned( pb )) {
  123. cb_release_op_connection(cb->pool,ld,0);
  124. if ( NULL != ctrls)
  125. ldap_controls_free(ctrls);
  126. return -1;
  127. }
  128. /* heart-beat management */
  129. if (cb->max_idle_time>0)
  130. endtime=current_time() + cb->max_idle_time;
  131. /*
  132. * Send LDAP operation to the remote host
  133. */
  134. rc = ldap_delete_ext( ld, dn, ctrls, NULL, &msgid );
  135. if ( NULL != ctrls)
  136. ldap_controls_free(ctrls);
  137. if ( rc != LDAP_SUCCESS ) {
  138. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  139. ldap_err2string(rc), 0, NULL);
  140. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  141. return -1;
  142. }
  143. while ( 1 ) {
  144. if (cb_check_forward_abandon(cb,pb,ld,msgid)) {
  145. return -1;
  146. }
  147. rc = ldap_result( ld, msgid, 0, &cb->abandon_timeout, &res );
  148. switch ( rc ) {
  149. case -1:
  150. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  151. ldap_err2string(rc), 0, NULL);
  152. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  153. if (res)
  154. ldap_msgfree(res);
  155. return -1;
  156. case 0:
  157. if ((rc=cb_ping_farm(cb,cnx,endtime)) != LDAP_SUCCESS) {
  158. /* does not respond. give up and return a*/
  159. /* error to the client. */
  160. /*cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL,
  161. ldap_err2string(rc), 0, NULL);*/
  162. cb_send_ldap_result(pb,LDAP_OPERATIONS_ERROR, NULL, "FARM SERVER TEMPORARY UNAVAILABLE", 0, NULL);
  163. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  164. if (res)
  165. ldap_msgfree(res);
  166. return -1;
  167. }
  168. #ifdef CB_YIELD
  169. DS_Sleep(PR_INTERVAL_NO_WAIT);
  170. #endif
  171. break;
  172. default:
  173. matched_msg=error_msg=NULL;
  174. parse_rc = ldap_parse_result( ld, res, &rc, &matched_msg,
  175. &error_msg, &referrals, &serverctrls, 1 );
  176. if ( parse_rc != LDAP_SUCCESS ) {
  177. static int warned_parse_rc = 0;
  178. if (!warned_parse_rc) {
  179. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  180. "%s%s%s\n",
  181. matched_msg?matched_msg:"",
  182. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  183. ldap_err2string(parse_rc) );
  184. warned_parse_rc = 1;
  185. }
  186. cb_send_ldap_result( pb, LDAP_OPERATIONS_ERROR, NULL,
  187. ENDUSERMSG, 0, NULL );
  188. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(parse_rc));
  189. slapi_ch_free((void **)&matched_msg);
  190. slapi_ch_free((void **)&error_msg);
  191. if (serverctrls)
  192. ldap_controls_free(serverctrls);
  193. /* jarnou: free referrals */
  194. if (referrals)
  195. charray_free(referrals);
  196. return -1;
  197. }
  198. if ( rc != LDAP_SUCCESS ) {
  199. struct berval ** refs = referrals2berval(referrals);
  200. static int warned_rc = 0;
  201. if (!warned_rc && error_msg) {
  202. slapi_log_error( SLAPI_LOG_FATAL, CB_PLUGIN_SUBSYSTEM,
  203. "%s%s%s\n",
  204. matched_msg?matched_msg:"",
  205. (matched_msg&&(*matched_msg!='\0'))?": ":"",
  206. error_msg );
  207. warned_rc = 1;
  208. }
  209. cb_send_ldap_result( pb, rc, matched_msg, ENDUSERMSG, 0, refs);
  210. cb_release_op_connection(cb->pool,ld,CB_LDAP_CONN_ERROR(rc));
  211. slapi_ch_free((void **)&matched_msg);
  212. slapi_ch_free((void **)&error_msg);
  213. if (refs)
  214. ber_bvecfree(refs);
  215. if (referrals)
  216. charray_free(referrals);
  217. if (serverctrls)
  218. ldap_controls_free(serverctrls);
  219. return -1;
  220. }
  221. cb_release_op_connection(cb->pool,ld,0);
  222. /* Add control response sent by the farm server */
  223. for (i=0; serverctrls && serverctrls[i];i++)
  224. slapi_pblock_set( pb, SLAPI_ADD_RESCONTROL, serverctrls[i]);
  225. if (serverctrls)
  226. ldap_controls_free(serverctrls);
  227. /* jarnou: free matched_msg, error_msg, and referrals if necessary */
  228. slapi_ch_free((void **)&matched_msg);
  229. slapi_ch_free((void **)&error_msg);
  230. if (referrals)
  231. charray_free(referrals);
  232. cb_send_ldap_result( pb, LDAP_SUCCESS, NULL, NULL, 0, NULL );
  233. return 0;
  234. }
  235. }
  236. /* Never reached */
  237. /* return 0; */
  238. }