statechange.c 15 KB

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