operation.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. /* operation.c - routines to deal with pending ldap operations */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <sys/types.h>
  42. #ifndef _WIN32
  43. #include <sys/socket.h>
  44. #endif
  45. #include "slap.h"
  46. int
  47. slapi_op_abandoned( Slapi_PBlock *pb )
  48. {
  49. int op_status;
  50. op_status = pb->pb_op->o_status;
  51. return( op_status == SLAPI_OP_STATUS_ABANDONED );
  52. }
  53. void
  54. operation_out_of_disk_space()
  55. {
  56. LDAPDebug(LDAP_DEBUG_ANY, "*** DISK FULL ***\n", 0, 0, 0);
  57. LDAPDebug(LDAP_DEBUG_ANY, "Attempting to shut down gracefully.\n", 0, 0, 0);
  58. g_set_shutdown( SLAPI_SHUTDOWN_DISKFULL );
  59. }
  60. /* Setting the flags on the operation allows more control to the plugin
  61. * to disable and enable checks
  62. * Flags that we support for setting in the operation from the plugin are
  63. * SLAPI_OP_FLAG_NO_ACCESS_CHECK - do not check for access control
  64. * This function can be extended to support other flag setting as well
  65. */
  66. void slapi_operation_set_flag(Slapi_Operation *op, unsigned long flag)
  67. {
  68. operation_set_flag(op, flag);
  69. }
  70. void slapi_operation_clear_flag(Slapi_Operation *op, unsigned long flag)
  71. {
  72. operation_clear_flag(op, flag);
  73. }
  74. int slapi_operation_is_flag_set(Slapi_Operation *op, unsigned long flag)
  75. {
  76. return operation_is_flag_set(op, flag);
  77. }
  78. static int operation_type = -1; /* The type number assigned by the Factory for 'Operation' */
  79. int
  80. get_operation_object_type()
  81. {
  82. if(operation_type==-1)
  83. {
  84. /* The factory is given the name of the object type, in
  85. * return for a type handle. Whenever the object is created
  86. * or destroyed the factory is called with the handle so
  87. * that it may call the constructors or destructors registered
  88. * with it.
  89. */
  90. operation_type= factory_register_type(SLAPI_EXT_OPERATION,offsetof(Operation,o_extension));
  91. }
  92. return operation_type;
  93. }
  94. /*
  95. * Allocate a new Slapi_Operation.
  96. * The flag parameter indicates whether the the operation is
  97. * external (from an LDAP Client), or internal (from a plugin).
  98. */
  99. Slapi_Operation *
  100. operation_new(int flags)
  101. {
  102. /* To improve performance, we allocate the Operation, BerElement and
  103. * ber buffer in one block, instead of a separate malloc() for each.
  104. * Subsequently, ber_special_free() frees them all; we're careful
  105. * not to free the Operation separately, and the ber software knows
  106. * not to free the buffer separately.
  107. */
  108. BerElement *ber = NULL;
  109. Slapi_Operation *o;
  110. if(flags & OP_FLAG_INTERNAL)
  111. {
  112. o = (Slapi_Operation *) slapi_ch_malloc(sizeof(Slapi_Operation));
  113. }
  114. else
  115. {
  116. o= (Slapi_Operation *) ber_special_alloc( sizeof(Slapi_Operation), &ber );
  117. }
  118. if (NULL != o)
  119. {
  120. memset(o,0,sizeof(Slapi_Operation));
  121. o->o_ber = ber;
  122. o->o_msgid = -1;
  123. o->o_tag = LBER_DEFAULT;
  124. o->o_status = SLAPI_OP_STATUS_PROCESSING;
  125. slapi_sdn_init(&(o->o_sdn));
  126. o->o_authtype = NULL;
  127. o->o_isroot = 0;
  128. o->o_time = current_time();
  129. o->o_opid = 0;
  130. o->o_connid = 0;
  131. o->o_next = NULL;
  132. o->o_flags= flags;
  133. if ( config_get_accesslog_level() & LDAP_DEBUG_TIMING ) {
  134. o->o_interval = PR_IntervalNow();
  135. } else {
  136. o->o_interval = (PRIntervalTime)0;
  137. }
  138. }
  139. return o;
  140. }
  141. void
  142. operation_free( Slapi_Operation **op, Connection *conn )
  143. {
  144. if(op!=NULL && *op!=NULL)
  145. {
  146. /* Call the plugin extension destructors */
  147. factory_destroy_extension(get_operation_object_type(),*op,conn,&((*op)->o_extension));
  148. slapi_sdn_done(&(*op)->o_sdn);
  149. slapi_sdn_free(&(*op)->o_target_spec);
  150. free( (*op)->o_authtype );
  151. if ( (*op)->o_searchattrs != NULL ) {
  152. cool_charray_free( (*op)->o_searchattrs );
  153. }
  154. if ( NULL != (*op)->o_params.request_controls ) {
  155. ldap_controls_free( (*op)->o_params.request_controls );
  156. }
  157. if ( NULL != (*op)->o_results.result_controls ) {
  158. ldap_controls_free( (*op)->o_results.result_controls );
  159. }
  160. if(operation_is_flag_set(*op, OP_FLAG_INTERNAL))
  161. {
  162. slapi_ch_free((void**)op);
  163. }
  164. else
  165. {
  166. ber_special_free( *op , (*op)->o_ber);
  167. }
  168. /* counters_to_errors_log("after operation"); */
  169. }
  170. }
  171. void
  172. slapi_operation_set_csngen_handler ( Slapi_Operation *op, void *callback )
  173. {
  174. op->o_csngen_handler = (csngen_handler) callback;
  175. }
  176. void
  177. slapi_operation_set_replica_attr_handler ( Slapi_Operation *op, void *callback )
  178. {
  179. op->o_replica_attr_handler = (replica_attr_handler) callback;
  180. }
  181. int
  182. slapi_operation_get_replica_attr ( Slapi_PBlock *pb, Slapi_Operation *op, const char *type, void *value )
  183. {
  184. int rc = -1;
  185. if (op->o_replica_attr_handler)
  186. {
  187. rc = op->o_replica_attr_handler ( pb, type, value );
  188. }
  189. return rc;
  190. }
  191. CSN *
  192. operation_get_csn(Slapi_Operation *op)
  193. {
  194. return op->o_params.csn;
  195. }
  196. void
  197. operation_set_csn(Slapi_Operation *op,CSN *csn)
  198. {
  199. op->o_params.csn= csn;
  200. }
  201. unsigned long
  202. slapi_op_get_type(Slapi_Operation *op)
  203. {
  204. return op->o_params.operation_type;
  205. }
  206. char *
  207. slapi_op_type_to_string(unsigned long type)
  208. {
  209. switch (type)
  210. {
  211. case SLAPI_OPERATION_ADD:
  212. return "add";
  213. case SLAPI_OPERATION_DELETE:
  214. return "delete";
  215. case SLAPI_OPERATION_MODIFY:
  216. return "modify";
  217. case SLAPI_OPERATION_MODRDN:
  218. return "modrdn";
  219. case SLAPI_OPERATION_BIND:
  220. return "bind";
  221. case SLAPI_OPERATION_COMPARE:
  222. return "compare";
  223. case SLAPI_OPERATION_SEARCH:
  224. return "search";
  225. default:
  226. return "unknown operation type";
  227. }
  228. }
  229. /* DEPRECATED : USE FUNCTION ABOVE FOR NEW DVLPT */
  230. unsigned long
  231. operation_get_type(Slapi_Operation *op)
  232. {
  233. return op->o_params.operation_type;
  234. }
  235. void
  236. operation_set_type(Slapi_Operation *op, unsigned long type)
  237. {
  238. op->o_params.operation_type= type;
  239. }
  240. void
  241. operation_set_flag(Slapi_Operation *op, int flag)
  242. {
  243. op->o_flags|= flag;
  244. }
  245. void
  246. operation_clear_flag(Slapi_Operation *op, int flag)
  247. {
  248. op->o_flags &= ~flag;
  249. }
  250. int
  251. operation_is_flag_set(Slapi_Operation *op, int flag)
  252. {
  253. return op->o_flags & flag;
  254. }
  255. Slapi_DN*
  256. operation_get_target_spec (Slapi_Operation *op)
  257. {
  258. return op->o_target_spec;
  259. }
  260. void
  261. operation_set_target_spec (Slapi_Operation *op, const Slapi_DN *target_spec)
  262. {
  263. PR_ASSERT (op);
  264. PR_ASSERT (target_spec);
  265. op->o_target_spec = slapi_sdn_dup(target_spec);
  266. }
  267. void
  268. operation_set_target_spec_str (Slapi_Operation *op, const char *target_spec)
  269. {
  270. PR_ASSERT (op);
  271. op->o_target_spec = slapi_sdn_new_dn_byval (target_spec);
  272. }
  273. unsigned long operation_get_abandoned_op (const Slapi_Operation *op)
  274. {
  275. PR_ASSERT (op);
  276. return op->o_abandoned_op;
  277. }
  278. void operation_set_abandoned_op (Slapi_Operation *op, unsigned long abandoned_op)
  279. {
  280. PR_ASSERT (op);
  281. op->o_abandoned_op = abandoned_op;
  282. }
  283. /* slapi_operation_parameters manipulation functions */
  284. struct slapi_operation_parameters *operation_parameters_new()
  285. {
  286. return (slapi_operation_parameters *)slapi_ch_calloc (1, sizeof (slapi_operation_parameters));
  287. }
  288. static LDAPMod **
  289. copy_mods(LDAPMod **orig_mods)
  290. {
  291. LDAPMod **new_mods = NULL;
  292. LDAPMod *mod;
  293. Slapi_Mods smods_old;
  294. Slapi_Mods smods_new;
  295. slapi_mods_init_byref(&smods_old,orig_mods);
  296. slapi_mods_init_passin(&smods_new,new_mods);
  297. mod= slapi_mods_get_first_mod(&smods_old);
  298. while(mod!=NULL)
  299. {
  300. slapi_mods_add_modbvps(&smods_new,mod->mod_op,mod->mod_type,mod->mod_bvalues);
  301. mod= slapi_mods_get_next_mod(&smods_old);
  302. }
  303. new_mods= slapi_mods_get_ldapmods_passout(&smods_new);
  304. slapi_mods_done(&smods_old);
  305. slapi_mods_done(&smods_new);
  306. return new_mods;
  307. }
  308. struct slapi_operation_parameters *
  309. operation_parameters_dup(struct slapi_operation_parameters *sop)
  310. {
  311. struct slapi_operation_parameters *sop_new= (struct slapi_operation_parameters *)
  312. slapi_ch_malloc(sizeof(struct slapi_operation_parameters));
  313. memcpy(sop_new,sop,sizeof(struct slapi_operation_parameters));
  314. if(sop->target_address.dn!=NULL)
  315. {
  316. sop_new->target_address.dn= slapi_ch_strdup(sop->target_address.dn);
  317. }
  318. if(sop->target_address.uniqueid!=NULL)
  319. {
  320. sop_new->target_address.uniqueid= slapi_ch_strdup(sop->target_address.uniqueid);
  321. }
  322. sop_new->csn= csn_dup(sop->csn);
  323. switch(sop->operation_type)
  324. {
  325. case SLAPI_OPERATION_ADD:
  326. sop_new->p.p_add.target_entry= slapi_entry_dup(sop->p.p_add.target_entry);
  327. sop_new->p.p_add.parentuniqueid = slapi_ch_strdup(sop->p.p_add.parentuniqueid);
  328. break;
  329. case SLAPI_OPERATION_MODIFY:
  330. sop_new->p.p_modify.modify_mods= NULL;
  331. if (sop->p.p_modify.modify_mods!=NULL)
  332. {
  333. sop_new->p.p_modify.modify_mods = copy_mods(sop->p.p_modify.modify_mods);
  334. }
  335. break;
  336. case SLAPI_OPERATION_MODRDN:
  337. if(sop->p.p_modrdn.modrdn_newrdn!=NULL)
  338. {
  339. sop_new->p.p_modrdn.modrdn_newrdn= slapi_ch_strdup(sop->p.p_modrdn.modrdn_newrdn);
  340. }
  341. if(sop->p.p_modrdn.modrdn_newsuperior_address.dn!=NULL)
  342. {
  343. sop_new->p.p_modrdn.modrdn_newsuperior_address.dn =
  344. slapi_ch_strdup(sop->p.p_modrdn.modrdn_newsuperior_address.dn);
  345. }
  346. if(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid!=NULL)
  347. {
  348. sop_new->p.p_modrdn.modrdn_newsuperior_address.uniqueid =
  349. slapi_ch_strdup(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid);
  350. }
  351. sop_new->p.p_modrdn.modrdn_mods= NULL;
  352. if (sop->p.p_modrdn.modrdn_mods!=NULL)
  353. {
  354. sop_new->p.p_modrdn.modrdn_mods = copy_mods(sop->p.p_modrdn.modrdn_mods);
  355. }
  356. break;
  357. case SLAPI_OPERATION_DELETE:
  358. /* Has no extra parameters. */
  359. case SLAPI_OPERATION_BIND:
  360. case SLAPI_OPERATION_COMPARE:
  361. case SLAPI_OPERATION_SEARCH:
  362. default:
  363. /* We are not interested in these. */
  364. break;
  365. }
  366. return sop_new;
  367. }
  368. void
  369. operation_parameters_done (struct slapi_operation_parameters *sop)
  370. {
  371. if(sop!=NULL)
  372. {
  373. slapi_ch_free((void **)&sop->target_address.dn);
  374. slapi_ch_free((void **)&sop->target_address.uniqueid);
  375. csn_free(&sop->csn);
  376. switch(sop->operation_type)
  377. {
  378. case SLAPI_OPERATION_ADD:
  379. slapi_entry_free(sop->p.p_add.target_entry);
  380. sop->p.p_add.target_entry= NULL;
  381. slapi_ch_free((void **)&(sop->p.p_add.parentuniqueid));
  382. break;
  383. case SLAPI_OPERATION_MODIFY:
  384. ldap_mods_free(sop->p.p_modify.modify_mods, 1 /* Free the Array and the Elements */);
  385. sop->p.p_modify.modify_mods= NULL;
  386. break;
  387. case SLAPI_OPERATION_MODRDN:
  388. slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newrdn));
  389. slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newsuperior_address.dn));
  390. slapi_ch_free((void **)&(sop->p.p_modrdn.modrdn_newsuperior_address.uniqueid));
  391. ldap_mods_free(sop->p.p_modrdn.modrdn_mods, 1 /* Free the Array and the Elements */);
  392. sop->p.p_modrdn.modrdn_mods= NULL;
  393. break;
  394. case SLAPI_OPERATION_DELETE:
  395. /* Has no extra parameters. */
  396. case SLAPI_OPERATION_BIND:
  397. case SLAPI_OPERATION_COMPARE:
  398. case SLAPI_OPERATION_SEARCH:
  399. default:
  400. /* We are not interested in these */
  401. break;
  402. }
  403. }
  404. }
  405. void operation_parameters_free(struct slapi_operation_parameters **sop)
  406. {
  407. if (sop)
  408. {
  409. operation_parameters_done (*sop);
  410. slapi_ch_free ((void**)sop);
  411. }
  412. }