statechange.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. /* plugin which provides a callback mechanism for state changes in the DS */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include "portable.h"
  45. #include "slapi-plugin.h"
  46. #include "statechange.h"
  47. /* get file mode flags for unix */
  48. #ifndef _WIN32
  49. #include <sys/stat.h>
  50. #endif
  51. /* the circular list of systems to notify */
  52. typedef struct _statechange_notify
  53. {
  54. char *caller_id;
  55. char *dn;
  56. char *filter;
  57. Slapi_Filter *realfilter;
  58. notify_callback func;
  59. void *caller_data;
  60. struct _statechange_notify *next;
  61. struct _statechange_notify *prev;
  62. } SCNotify;
  63. static SCNotify *head; /* a place to start in the list */
  64. #define SCN_PLUGIN_SUBSYSTEM "statechange-plugin" /* used for logging */
  65. static void *api[5];
  66. static Slapi_Mutex *buffer_lock = 0;
  67. /* other function prototypes */
  68. int statechange_init( Slapi_PBlock *pb );
  69. static int statechange_start( Slapi_PBlock *pb );
  70. static int statechange_close( Slapi_PBlock *pb );
  71. static int statechange_post_op( Slapi_PBlock *pb, int modtype );
  72. static int statechange_mod_post_op( Slapi_PBlock *pb );
  73. static int statechange_modrdn_post_op( Slapi_PBlock *pb );
  74. static int statechange_add_post_op( Slapi_PBlock *pb );
  75. static int statechange_delete_post_op( Slapi_PBlock *pb );
  76. static int _statechange_register(char *caller_id, char *dn, char *filter, void *caller_data, notify_callback func);
  77. static void *_statechange_unregister(char *dn, char *filter, notify_callback func);
  78. static void _statechange_unregister_all(char *caller_id, caller_data_free_callback);
  79. static void _statechange_vattr_cache_invalidator_callback(Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data);
  80. static SCNotify *statechange_find_notify(char *dn, char *filter, notify_callback func);
  81. static Slapi_PluginDesc pdesc = { "statechange", VENDOR, DS_PACKAGE_VERSION,
  82. "state change notification service plugin" };
  83. #ifdef _WIN32
  84. int *module_ldap_debug = 0;
  85. void plugin_init_debug_level(int *level_ptr)
  86. {
  87. module_ldap_debug = level_ptr;
  88. }
  89. #endif
  90. /*
  91. statechange_init
  92. --------
  93. adds our callbacks to the list
  94. */
  95. int statechange_init( Slapi_PBlock *pb )
  96. {
  97. int ret = 0;
  98. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "--> statechange_init\n");
  99. head = 0;
  100. if ( slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  101. SLAPI_PLUGIN_VERSION_01 ) != 0 ||
  102. slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  103. (void *) statechange_start ) != 0 ||
  104. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODIFY_FN,
  105. (void *) statechange_mod_post_op ) != 0 ||
  106. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_MODRDN_FN,
  107. (void *) statechange_modrdn_post_op ) != 0 ||
  108. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_ADD_FN,
  109. (void *) statechange_add_post_op ) != 0 ||
  110. slapi_pblock_set(pb, SLAPI_PLUGIN_POST_DELETE_FN,
  111. (void *) statechange_delete_post_op ) != 0 ||
  112. slapi_pblock_set(pb, SLAPI_PLUGIN_CLOSE_FN,
  113. (void *) statechange_close ) != 0 ||
  114. slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  115. (void *)&pdesc ) != 0 )
  116. {
  117. slapi_log_error( SLAPI_LOG_FATAL, SCN_PLUGIN_SUBSYSTEM,
  118. "statechange_init: failed to register plugin\n" );
  119. ret = -1;
  120. }
  121. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "<-- statechange_init\n");
  122. return ret;
  123. }
  124. /*
  125. statechange_start
  126. ---------
  127. This function publishes the interface for this plugin
  128. */
  129. static int statechange_start( Slapi_PBlock *pb )
  130. {
  131. int ret = 0;
  132. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "--> statechange_start\n");
  133. api[0] = 0; /* reserved for api broker use, must be zero */
  134. api[1] = (void *)_statechange_register;
  135. api[2] = (void *)_statechange_unregister;
  136. api[3] = (void *)_statechange_unregister_all;
  137. api[4] = (void *)_statechange_vattr_cache_invalidator_callback;
  138. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  139. {
  140. /* badness */
  141. slapi_log_error( SLAPI_LOG_FATAL, SCN_PLUGIN_SUBSYSTEM, "statechange: failed to create lock\n");
  142. ret = -1;
  143. }
  144. else
  145. {
  146. if( slapi_apib_register(StateChange_v1_0_GUID, api) )
  147. {
  148. slapi_log_error( SLAPI_LOG_FATAL, SCN_PLUGIN_SUBSYSTEM, "statechange: failed to publish state change interface\n");
  149. ret = -1;
  150. }
  151. }
  152. head = 0;
  153. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "<-- statechange_start\n");
  154. return ret;
  155. }
  156. /*
  157. statechange_close
  158. ---------
  159. unregisters the interface for this plugin
  160. */
  161. static int statechange_close( Slapi_PBlock *pb )
  162. {
  163. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "--> statechange_close\n");
  164. slapi_apib_unregister(StateChange_v1_0_GUID);
  165. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "<-- statechange_close\n");
  166. return 0;
  167. }
  168. static int statechange_mod_post_op( Slapi_PBlock *pb )
  169. {
  170. return statechange_post_op(pb, LDAP_CHANGETYPE_MODIFY);
  171. }
  172. static int statechange_modrdn_post_op( Slapi_PBlock *pb )
  173. {
  174. return statechange_post_op(pb, LDAP_CHANGETYPE_MODDN);
  175. }
  176. static int statechange_add_post_op( Slapi_PBlock *pb )
  177. {
  178. return statechange_post_op(pb, LDAP_CHANGETYPE_ADD);
  179. }
  180. static int statechange_delete_post_op( Slapi_PBlock *pb )
  181. {
  182. return statechange_post_op(pb, LDAP_CHANGETYPE_DELETE);
  183. }
  184. /*
  185. statechange_post_op
  186. -----------
  187. Catch all for all post operations that change entries
  188. in some way - evaluate the change against the notification
  189. entries and fire off the relevant callbacks - it is called
  190. from the real postop functions which supply it with the
  191. postop type
  192. */
  193. static int statechange_post_op( Slapi_PBlock *pb, int modtype )
  194. {
  195. SCNotify *notify = head;
  196. int execute;
  197. Slapi_DN *sdn = NULL;
  198. char *ndn = NULL;
  199. struct slapi_entry *e_before = NULL;
  200. struct slapi_entry *e_after = NULL;
  201. if(head == 0)
  202. return 0;
  203. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "--> statechange_post_op\n");
  204. /* evaluate this operation against the notification entries */
  205. slapi_lock_mutex(buffer_lock);
  206. if(head)
  207. {
  208. slapi_pblock_get( pb, SLAPI_TARGET_SDN, &sdn );
  209. if (NULL == sdn) {
  210. slapi_log_error( SLAPI_LOG_FATAL, SCN_PLUGIN_SUBSYSTEM,
  211. "statechange_post_op: failed to get dn of changed entry" );
  212. goto bail;
  213. }
  214. ndn = (char *)slapi_sdn_get_ndn(sdn);
  215. slapi_pblock_get( pb, SLAPI_ENTRY_PRE_OP, &e_before );
  216. slapi_pblock_get( pb, SLAPI_ENTRY_POST_OP, &e_after );
  217. do
  218. {
  219. execute = 0;
  220. /* first dn */
  221. if(notify->dn)
  222. {
  223. if(0 != slapi_dn_issuffix(ndn, notify->dn))
  224. execute = 1;
  225. }
  226. else
  227. /* note, if supplied null for everything in the entry *all* ops match */
  228. execute = 1;
  229. if(execute && notify->filter)
  230. {
  231. /* next the filter */
  232. int filter_test = 0;
  233. /* need to test entry both before and after op */
  234. if(e_before && !slapi_filter_test_simple( e_before, notify->realfilter))
  235. filter_test = 1;
  236. if(!filter_test && e_after && !slapi_filter_test_simple( e_after, notify->realfilter))
  237. filter_test = 1;
  238. if(!filter_test)
  239. execute = 0;
  240. }
  241. if(execute)
  242. {
  243. if(e_after)
  244. (notify->func)(e_after, ndn, modtype, pb, notify->caller_data);
  245. else
  246. (notify->func)(e_before, ndn, modtype, pb, notify->caller_data);
  247. }
  248. notify = notify->next;
  249. }
  250. while(notify && notify != head);
  251. }
  252. bail:
  253. slapi_unlock_mutex(buffer_lock);
  254. slapi_log_error( SLAPI_LOG_TRACE, SCN_PLUGIN_SUBSYSTEM, "<-- statechange_post_op\n");
  255. return 0; /* always succeed */
  256. }
  257. static int _statechange_register(char *caller_id, char *dn, char *filter, void *caller_data, notify_callback func)
  258. {
  259. int ret = -1;
  260. SCNotify *item;
  261. /* simple - we don't check for duplicates */
  262. item = (SCNotify*)slapi_ch_malloc(sizeof(SCNotify));
  263. if(item)
  264. {
  265. char *writable_filter = slapi_ch_strdup(filter);
  266. item->caller_id = slapi_ch_strdup(caller_id);
  267. if(dn)
  268. {
  269. item->dn = slapi_ch_strdup(dn);
  270. slapi_dn_normalize( item->dn );
  271. }
  272. else
  273. item->dn = 0;
  274. item->filter = slapi_ch_strdup(filter);
  275. item->caller_data = caller_data;
  276. if (writable_filter &&
  277. (NULL == (item->realfilter = slapi_str2filter(writable_filter)))) {
  278. slapi_log_error(SLAPI_LOG_FATAL, SCN_PLUGIN_SUBSYSTEM,
  279. "Error: invalid filter in statechange entry [%s]: [%s]\n",
  280. dn, filter);
  281. slapi_ch_free_string(&item->caller_id);
  282. slapi_ch_free_string(&item->dn);
  283. slapi_ch_free_string(&item->filter);
  284. slapi_ch_free_string(&writable_filter);
  285. slapi_ch_free((void **)&item);
  286. return -1;
  287. } else if (!writable_filter) {
  288. item->realfilter = NULL;
  289. }
  290. item->func = func;
  291. slapi_lock_mutex(buffer_lock);
  292. if(head == NULL)
  293. {
  294. head = item;
  295. head->next = head;
  296. head->prev = head;
  297. }
  298. else
  299. {
  300. item->next = head;
  301. item->prev = head->prev;
  302. head->prev = item;
  303. item->prev->next = item;
  304. }
  305. slapi_unlock_mutex(buffer_lock);
  306. slapi_ch_free_string(&writable_filter);
  307. ret = 0;
  308. }
  309. return ret;
  310. }
  311. static void *_statechange_unregister(char *dn, char *filter, notify_callback thefunc)
  312. {
  313. void *ret = NULL;
  314. SCNotify *func = NULL;
  315. if(buffer_lock == 0)
  316. return ret;
  317. slapi_lock_mutex(buffer_lock);
  318. if((func = statechange_find_notify(dn, filter, thefunc)))
  319. {
  320. func->prev->next = func->next;
  321. func->next->prev = func->prev;
  322. if(func == head)
  323. {
  324. head = func->next;
  325. }
  326. if(func == head) /* must be the last item, turn off the lights */
  327. head = 0;
  328. slapi_ch_free_string(&func->caller_id);
  329. slapi_ch_free_string(&func->dn);
  330. slapi_ch_free_string(&func->filter);
  331. slapi_filter_free( func->realfilter, 1 );
  332. ret = func->caller_data;
  333. slapi_ch_free((void **)&func);
  334. }
  335. slapi_unlock_mutex(buffer_lock);
  336. return ret;
  337. }
  338. static void _statechange_unregister_all(char *caller_id, caller_data_free_callback callback)
  339. {
  340. SCNotify *notify = head;
  341. SCNotify *start_notify = head;
  342. if(buffer_lock == 0)
  343. return;
  344. slapi_lock_mutex(buffer_lock);
  345. if(notify)
  346. {
  347. do
  348. {
  349. SCNotify *notify_next = notify->next;
  350. if( slapi_utf8casecmp((unsigned char *)caller_id, (unsigned char *)notify->caller_id) )
  351. {
  352. notify->prev->next = notify->next;
  353. notify->next->prev = notify->prev;
  354. if(notify == head)
  355. {
  356. head = notify->next;
  357. start_notify = notify->prev;
  358. }
  359. if(notify == head) /* must be the last item, turn off the lights */
  360. head = 0;
  361. if(callback)
  362. callback(notify->caller_data);
  363. slapi_ch_free_string(&notify->caller_id);
  364. slapi_ch_free_string(&notify->dn);
  365. slapi_ch_free_string(&notify->filter);
  366. slapi_filter_free( notify->realfilter, 1 );
  367. slapi_ch_free((void **)&notify);
  368. }
  369. notify = notify_next;
  370. }
  371. while(notify != start_notify && notify != NULL);
  372. }
  373. slapi_unlock_mutex(buffer_lock);
  374. }
  375. /* this func needs looking at to make work */
  376. static SCNotify *statechange_find_notify(char *dn, char *filter, notify_callback func)
  377. {
  378. SCNotify *notify = head;
  379. SCNotify *start_notify = head;
  380. if(notify)
  381. {
  382. do
  383. {
  384. if( !slapi_utf8casecmp((unsigned char *)dn, (unsigned char *)notify->dn) &&
  385. !slapi_utf8casecmp((unsigned char *)filter, (unsigned char *)notify->filter) && func == notify->func)
  386. {
  387. return notify;
  388. }
  389. notify = notify->next;
  390. }
  391. while(notify != start_notify);
  392. }
  393. return 0;
  394. }
  395. /* intended for use by vattr service providers
  396. * to deal with significant vattr state changes
  397. */
  398. static void _statechange_vattr_cache_invalidator_callback(Slapi_Entry *e, char *dn, int modtype, Slapi_PBlock *pb, void *caller_data)
  399. {
  400. /* simply get the significance data and act */
  401. switch(*(int*)caller_data)
  402. {
  403. case STATECHANGE_VATTR_ENTRY_INVALIDATE:
  404. if(e)
  405. slapi_entry_vattrcache_watermark_invalidate(e);
  406. break;
  407. case STATECHANGE_VATTR_GLOBAL_INVALIDATE:
  408. default:
  409. slapi_entrycache_vattrcache_watermark_invalidate();
  410. break;
  411. }
  412. }