statechange.c 14 KB

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