operation.c 13 KB

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