statechange.c 13 KB

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