statechange.c 13 KB

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