delete.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. /*
  42. * Copyright (c) 1995 Regents of the University of Michigan.
  43. * All rights reserved.
  44. *
  45. * Redistribution and use in source and binary forms are permitted
  46. * provided that this notice is preserved and that due credit is given
  47. * to the University of Michigan at Ann Arbor. The name of the University
  48. * may not be used to endorse or promote products derived from this
  49. * software without specific prior written permission. This software
  50. * is provided ``as is'' without express or implied warranty.
  51. */
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <sys/types.h>
  55. #ifndef _WIN32
  56. #include <sys/socket.h>
  57. #endif
  58. #include "slap.h"
  59. #include "pratom.h"
  60. /* Forward declarations */
  61. static int delete_internal_pb (Slapi_PBlock *pb);
  62. static void op_shared_delete (Slapi_PBlock *pb);
  63. /* This function is called to process operation that come over external connections */
  64. void
  65. do_delete( Slapi_PBlock *pb )
  66. {
  67. Slapi_Operation *operation;
  68. BerElement *ber;
  69. char *dn = NULL;
  70. int err;
  71. LDAPDebug( LDAP_DEBUG_TRACE, "do_delete\n", 0, 0, 0 );
  72. slapi_pblock_get( pb, SLAPI_OPERATION, &operation);
  73. ber = operation->o_ber;
  74. /* count the delete request */
  75. snmp_increment_counter(g_get_global_snmp_vars()->ops_tbl.dsRemoveEntryOps);
  76. /*
  77. * Parse the delete request. It looks like this:
  78. *
  79. * DelRequest := DistinguishedName
  80. */
  81. if ( ber_scanf( pb->pb_op->o_ber, "a", &dn ) == LBER_ERROR ) {
  82. LDAPDebug( LDAP_DEBUG_ANY,
  83. "ber_scanf failed (op=Delete; params=DN)\n", 0, 0, 0 );
  84. op_shared_log_error_access (pb, "DEL", "???", "decoding error");
  85. send_ldap_result( pb, LDAP_PROTOCOL_ERROR, NULL, NULL, 0,
  86. NULL );
  87. goto free_and_return;
  88. }
  89. /*
  90. * in LDAPv3 there can be optional control extensions on
  91. * the end of an LDAPMessage. we need to read them in and
  92. * pass them to the backend.
  93. */
  94. if ( (err = get_ldapmessage_controls( pb, ber, NULL )) != 0 ) {
  95. op_shared_log_error_access (pb, "DEL", dn, "decoding error");
  96. send_ldap_result( pb, err, NULL, NULL, 0, NULL );
  97. goto free_and_return;
  98. }
  99. LDAPDebug( LDAP_DEBUG_ARGS, "do_delete: dn (%s)\n", dn, 0, 0 );
  100. slapi_pblock_set( pb, SLAPI_REQUESTOR_ISROOT, &pb->pb_op->o_isroot );
  101. slapi_pblock_set( pb, SLAPI_ORIGINAL_TARGET, dn);
  102. op_shared_delete (pb);
  103. free_and_return:;
  104. slapi_ch_free ((void**)&dn);
  105. }
  106. /* This function is used to issue internal delete operation
  107. This is an old style API. Its use is discoraged because it is not extendable and
  108. because it does not allow to check whether plugin has right to access part of the
  109. tree it is trying to modify. Use slapi_delete_internal_pb instead */
  110. Slapi_PBlock *
  111. slapi_delete_internal(const char *idn, LDAPControl **controls, int dummy)
  112. {
  113. Slapi_PBlock pb;
  114. Slapi_PBlock *result_pb;
  115. int opresult;
  116. pblock_init (&pb);
  117. slapi_delete_internal_set_pb (&pb, idn, controls, NULL, plugin_get_default_component_id(), 0);
  118. delete_internal_pb (&pb);
  119. result_pb = slapi_pblock_new();
  120. if (result_pb)
  121. {
  122. slapi_pblock_get(&pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  123. slapi_pblock_set(result_pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  124. }
  125. pblock_done(&pb);
  126. return result_pb;
  127. }
  128. /* This is new style API to issue internal delete operation.
  129. pblock should contain the following data (can be set via call to slapi_delete_internal_set_pb):
  130. For uniqueid based operation:
  131. SLAPI_TARGET_DN set to dn that allows to select right backend, can be stale
  132. SLAPI_TARGET_UNIQUEID set to the uniqueid of the entry we are looking for
  133. SLAPI_CONTROLS_ARG set to request controls if present
  134. For dn based search:
  135. SLAPI_TARGET_DN set to the entry dn
  136. SLAPI_CONTROLS_ARG set to request controls if present
  137. */
  138. int slapi_delete_internal_pb (Slapi_PBlock *pb)
  139. {
  140. if (pb == NULL)
  141. return -1;
  142. if (!allow_operation (pb))
  143. {
  144. slapi_send_ldap_result( pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  145. "This plugin is not configured to access operation target data", 0, NULL );
  146. return 0;
  147. }
  148. return delete_internal_pb (pb);
  149. }
  150. /* Initialize a pblock for a call to slapi_delete_internal_pb() */
  151. void slapi_delete_internal_set_pb (Slapi_PBlock *pb, const char *dn, LDAPControl **controls, const char *uniqueid,
  152. Slapi_ComponentId *plugin_identity, int operation_flags)
  153. {
  154. Operation *op;
  155. PR_ASSERT (pb != NULL);
  156. if (pb == NULL || dn == NULL)
  157. {
  158. slapi_log_error(SLAPI_LOG_FATAL, NULL,
  159. "slapi_delete_internal_set_pb: NULL parameter\n");
  160. return;
  161. }
  162. op = internal_operation_new(SLAPI_OPERATION_DELETE,operation_flags);
  163. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  164. slapi_pblock_set(pb, SLAPI_ORIGINAL_TARGET, (void*)dn);
  165. slapi_pblock_set(pb, SLAPI_CONTROLS_ARG, controls);
  166. if (uniqueid)
  167. {
  168. slapi_pblock_set(pb, SLAPI_TARGET_UNIQUEID, (void*)uniqueid);
  169. }
  170. slapi_pblock_set(pb, SLAPI_PLUGIN_IDENTITY, plugin_identity);
  171. }
  172. /* Helper functions */
  173. static int delete_internal_pb (Slapi_PBlock *pb)
  174. {
  175. LDAPControl **controls;
  176. Operation *op;
  177. int opresult = 0;
  178. PR_ASSERT (pb != NULL);
  179. slapi_pblock_get(pb, SLAPI_CONTROLS_ARG, &controls);
  180. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  181. op->o_handler_data = &opresult;
  182. op->o_result_handler = internal_getresult_callback;
  183. slapi_pblock_set(pb, SLAPI_OPERATION, op);
  184. slapi_pblock_set(pb, SLAPI_REQCONTROLS, controls);
  185. /* set parameters common for all internal operations */
  186. set_common_params (pb);
  187. /* set actions taken to process the operation */
  188. set_config_params (pb);
  189. /* perform delete operation */
  190. op_shared_delete (pb);
  191. slapi_pblock_set(pb, SLAPI_PLUGIN_INTOP_RESULT, &opresult);
  192. return 0;
  193. }
  194. static void op_shared_delete (Slapi_PBlock *pb)
  195. {
  196. char *dn;
  197. Slapi_Backend *be = NULL;
  198. char ebuf[ BUFSIZ ];
  199. int internal_op;
  200. Slapi_DN sdn;
  201. Slapi_Operation *operation;
  202. Slapi_Entry *referral;
  203. Slapi_Entry *ecopy = NULL;
  204. char errorbuf[BUFSIZ];
  205. int err;
  206. slapi_pblock_get(pb, SLAPI_ORIGINAL_TARGET, &dn);
  207. slapi_pblock_get(pb, SLAPI_OPERATION, &operation);
  208. internal_op= operation_is_flag_set(operation, OP_FLAG_INTERNAL);
  209. slapi_sdn_init_dn_byref(&sdn,dn);
  210. slapi_pblock_set(pb, SLAPI_DELETE_TARGET, (void*)slapi_sdn_get_ndn (&sdn));
  211. /* target spec is used to decide which plugins are applicable for the operation */
  212. operation_set_target_spec (operation, &sdn);
  213. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_ACCESS))
  214. {
  215. if (!internal_op )
  216. {
  217. slapi_log_access(LDAP_DEBUG_STATS, "conn=%" PRIu64 " op=%d DEL dn=\"%s\"\n",
  218. pb->pb_conn->c_connid,
  219. pb->pb_op->o_opid,
  220. escape_string(dn, ebuf));
  221. }
  222. else
  223. {
  224. slapi_log_access(LDAP_DEBUG_ARGS, "conn=%s op=%d DEL dn=\"%s\"\n",
  225. LOG_INTERNAL_OP_CON_ID,
  226. LOG_INTERNAL_OP_OP_ID,
  227. escape_string(dn, ebuf));
  228. }
  229. }
  230. /*
  231. * We could be serving multiple database backends. Select the
  232. * appropriate one.
  233. */
  234. if ((err = slapi_mapping_tree_select(pb, &be, &referral, errorbuf)) != LDAP_SUCCESS) {
  235. send_ldap_result(pb, err, NULL, errorbuf, 0, NULL);
  236. be = NULL;
  237. goto free_and_return;
  238. }
  239. if (referral)
  240. {
  241. int managedsait;
  242. slapi_pblock_get(pb, SLAPI_MANAGEDSAIT, &managedsait);
  243. if (managedsait)
  244. {
  245. send_ldap_result(pb, LDAP_UNWILLING_TO_PERFORM, NULL,
  246. "cannot delete referral", 0, NULL);
  247. slapi_entry_free(referral);
  248. goto free_and_return;
  249. }
  250. send_referrals_from_entry(pb,referral);
  251. slapi_entry_free(referral);
  252. goto free_and_return;
  253. }
  254. slapi_pblock_set(pb, SLAPI_BACKEND, be);
  255. /*
  256. * call the pre-delete plugins. if they succeed, call
  257. * the backend delete function. then call the
  258. * post-delete plugins.
  259. */
  260. if (plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_PRE_DELETE_FN :
  261. SLAPI_PLUGIN_PRE_DELETE_FN) == 0)
  262. {
  263. int rc;
  264. slapi_pblock_set(pb, SLAPI_PLUGIN, be->be_database);
  265. set_db_default_result_handlers(pb);
  266. if (be->be_delete != NULL)
  267. {
  268. if ((rc = (*be->be_delete)(pb)) == 0)
  269. {
  270. /* we don't perform acl check for internal operations */
  271. /* Dont update aci store for remote acis */
  272. if ((!internal_op) &&
  273. (!slapi_be_is_flag_set(be,SLAPI_BE_FLAG_REMOTE_DATA)))
  274. plugin_call_acl_mods_update (pb, SLAPI_OPERATION_DELETE);
  275. if (operation_is_flag_set(operation,OP_FLAG_ACTION_LOG_AUDIT))
  276. write_audit_log_entry(pb); /* Record the operation in the audit log */
  277. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &ecopy);
  278. do_ps_service(ecopy, NULL, LDAP_CHANGETYPE_DELETE, 0);
  279. }
  280. else
  281. {
  282. if (rc == SLAPI_FAIL_DISKFULL)
  283. {
  284. operation_out_of_disk_space();
  285. goto free_and_return;
  286. }
  287. }
  288. }
  289. slapi_pblock_set(pb, SLAPI_PLUGIN_OPRETURN, &rc);
  290. plugin_call_plugins(pb, internal_op ? SLAPI_PLUGIN_INTERNAL_POST_DELETE_FN :
  291. SLAPI_PLUGIN_POST_DELETE_FN);
  292. }
  293. free_and_return:
  294. if (be)
  295. slapi_be_Unlock(be);
  296. slapi_pblock_get(pb, SLAPI_ENTRY_PRE_OP, &ecopy);
  297. slapi_entry_free(ecopy);
  298. slapi_pblock_get ( pb, SLAPI_DELETE_GLUE_PARENT_ENTRY, &ecopy );
  299. if (ecopy)
  300. {
  301. slapi_entry_free (ecopy);
  302. slapi_pblock_set (pb, SLAPI_DELETE_GLUE_PARENT_ENTRY, NULL);
  303. }
  304. slapi_pblock_get(pb, SLAPI_URP_NAMING_COLLISION_DN, &dn);
  305. slapi_ch_free((void **)&dn);
  306. slapi_sdn_done(&sdn);
  307. }