statechange.c 13 KB

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