validate_task.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2009 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /* validate_task.c - syntax validation task */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sys/types.h>
  15. #include "syntax.h"
  16. /*
  17. * Globals
  18. */
  19. static void* _PluginID = NULL;
  20. /*
  21. * Data Structures
  22. */
  23. typedef struct _task_data
  24. {
  25. char *dn;
  26. char *filter_str;
  27. Slapi_Counter *invalid_entries;
  28. } task_data;
  29. /*
  30. * Function Prototypes
  31. */
  32. int syntax_validate_task_init(Slapi_PBlock *pb);
  33. static int syntax_validate_task_start(Slapi_PBlock *pb);
  34. static int syntax_validate_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
  35. Slapi_Entry *eAfter, int *returncode,
  36. char *returntext, void *arg);
  37. static void syntax_validate_task_destructor(Slapi_Task *task);
  38. static void syntax_validate_task_thread(void *arg);
  39. static int syntax_validate_task_callback(Slapi_Entry *e, void *callback_data);
  40. static const char *fetch_attr(Slapi_Entry *e, const char *attrname,
  41. const char *default_val);
  42. static void syntax_validate_set_plugin_id(void * plugin_id);
  43. static void *syntax_validate_get_plugin_id();
  44. /*
  45. * Function Implementations
  46. */
  47. int
  48. syntax_validate_task_init(Slapi_PBlock *pb)
  49. {
  50. int rc = 0;
  51. char *syntax_validate_plugin_identity = NULL;
  52. /* Save plugin ID. */
  53. slapi_pblock_get (pb, SLAPI_PLUGIN_IDENTITY, &syntax_validate_plugin_identity);
  54. PR_ASSERT (syntax_validate_plugin_identity);
  55. syntax_validate_set_plugin_id(syntax_validate_plugin_identity);
  56. /* Register task callback. */
  57. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  58. (void *) SLAPI_PLUGIN_VERSION_03 );
  59. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  60. (void *) syntax_validate_task_start );
  61. return rc;
  62. }
  63. static int
  64. syntax_validate_task_start(Slapi_PBlock *pb)
  65. {
  66. int rc = slapi_task_register_handler("syntax validate", syntax_validate_task_add);
  67. return rc;
  68. }
  69. static int
  70. syntax_validate_task_add(Slapi_PBlock *pb, Slapi_Entry *e,
  71. Slapi_Entry *eAfter, int *returncode,
  72. char *returntext, void *arg)
  73. {
  74. PRThread *thread = NULL;
  75. int rv = SLAPI_DSE_CALLBACK_OK;
  76. task_data *mytaskdata = NULL;
  77. Slapi_Task *task = NULL;
  78. const char *filter;
  79. const char *dn = 0;
  80. *returncode = LDAP_SUCCESS;
  81. /* get arg(s) */
  82. if ((dn = fetch_attr(e, "basedn", 0)) == NULL) {
  83. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  84. rv = SLAPI_DSE_CALLBACK_ERROR;
  85. goto out;
  86. }
  87. if ((filter = fetch_attr(e, "filter", "(objectclass=*)")) == NULL) {
  88. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  89. rv = SLAPI_DSE_CALLBACK_ERROR;
  90. goto out;
  91. }
  92. /* setup our task data */
  93. mytaskdata = (task_data*)slapi_ch_malloc(sizeof(task_data));
  94. if (mytaskdata == NULL) {
  95. *returncode = LDAP_OPERATIONS_ERROR;
  96. rv = SLAPI_DSE_CALLBACK_ERROR;
  97. goto out;
  98. }
  99. mytaskdata->dn = slapi_ch_strdup(dn);
  100. mytaskdata->filter_str = slapi_ch_strdup(filter);
  101. mytaskdata->invalid_entries = slapi_counter_new();
  102. /* allocate new task now */
  103. task = slapi_new_task(slapi_entry_get_ndn(e));
  104. /* register our destructor for cleaning up our private data */
  105. slapi_task_set_destructor_fn(task, syntax_validate_task_destructor);
  106. /* Stash a pointer to our data in the task */
  107. slapi_task_set_data(task, mytaskdata);
  108. /* start the sample task as a separate thread */
  109. thread = PR_CreateThread(PR_USER_THREAD, syntax_validate_task_thread,
  110. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  111. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  112. if (thread == NULL) {
  113. slapi_log_error( SLAPI_LOG_FATAL, SYNTAX_PLUGIN_SUBSYSTEM,
  114. "unable to create task thread!\n");
  115. *returncode = LDAP_OPERATIONS_ERROR;
  116. rv = SLAPI_DSE_CALLBACK_ERROR;
  117. slapi_task_finish(task, *returncode);
  118. } else {
  119. rv = SLAPI_DSE_CALLBACK_OK;
  120. }
  121. out:
  122. return rv;
  123. }
  124. static void
  125. syntax_validate_task_destructor(Slapi_Task *task)
  126. {
  127. if (task) {
  128. task_data *mydata = (task_data *)slapi_task_get_data(task);
  129. while (slapi_task_get_refcount(task) > 0) {
  130. /* Yield to wait for the fixup task finishes. */
  131. DS_Sleep (PR_MillisecondsToInterval(100));
  132. }
  133. if (mydata) {
  134. slapi_ch_free_string(&mydata->dn);
  135. slapi_ch_free_string(&mydata->filter_str);
  136. slapi_counter_destroy(&mydata->invalid_entries);
  137. /* Need to cast to avoid a compiler warning */
  138. slapi_ch_free((void **)&mydata);
  139. }
  140. }
  141. }
  142. static void
  143. syntax_validate_task_thread(void *arg)
  144. {
  145. int rc = 0;
  146. Slapi_Task *task = (Slapi_Task *)arg;
  147. task_data *td = NULL;
  148. Slapi_PBlock *search_pb = NULL;
  149. if (!task) {
  150. return; /* no task */
  151. }
  152. slapi_task_inc_refcount(task);
  153. slapi_log_error(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  154. "syntax_validate_task_thread --> refcount incremented.\n" );
  155. /* Fetch our task data from the task */
  156. td = (task_data *)slapi_task_get_data(task);
  157. /* Log started message. */
  158. slapi_task_begin(task, 1);
  159. slapi_task_log_notice(task, "Syntax validation task starting (arg: %s) ...\n",
  160. td->filter_str);
  161. slapi_log_error(SLAPI_LOG_FATAL, SYNTAX_PLUGIN_SUBSYSTEM,
  162. "Syntax validate task starting (base: \"%s\", filter: \"%s\") ...\n",
  163. td->dn, td->filter_str);
  164. /* Perform the search and use a callback
  165. * to validate each matching entry. */
  166. search_pb = slapi_pblock_new();
  167. slapi_search_internal_set_pb(search_pb, td->dn,
  168. LDAP_SCOPE_SUBTREE, td->filter_str, 0, 0,
  169. 0, 0, syntax_validate_get_plugin_id(), 0);
  170. rc = slapi_search_internal_callback_pb(search_pb, td, 0, syntax_validate_task_callback, 0);
  171. slapi_pblock_destroy(search_pb);
  172. /* Log finished message. */
  173. slapi_task_log_notice(task, "Syntax validate task complete. Found %" NSPRIu64
  174. " invalid entries.\n", slapi_counter_get_value(td->invalid_entries));
  175. slapi_task_log_status(task, "Syntax validate task complete. Found %" NSPRIu64
  176. " invalid entries.\n", slapi_counter_get_value(td->invalid_entries));
  177. slapi_log_error(SLAPI_LOG_FATAL, SYNTAX_PLUGIN_SUBSYSTEM, "Syntax validate task complete."
  178. " Found %" NSPRIu64 " invalid entries.\n",
  179. slapi_counter_get_value(td->invalid_entries));
  180. slapi_task_inc_progress(task);
  181. /* this will queue the destruction of the task */
  182. slapi_task_finish(task, rc);
  183. slapi_task_dec_refcount(task);
  184. slapi_log_error(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  185. "syntax_validate_task_thread <-- refcount decremented.\n");
  186. }
  187. static int
  188. syntax_validate_task_callback(Slapi_Entry *e, void *callback_data)
  189. {
  190. int rc = 0;
  191. char *dn = slapi_entry_get_dn(e);
  192. task_data *td = (task_data *)callback_data;
  193. Slapi_PBlock *pb = NULL;
  194. /*
  195. * If the server is ordered to shutdown, stop the fixup and return an error.
  196. */
  197. if (slapi_is_shutting_down()) {
  198. rc = -1;
  199. goto bail;
  200. }
  201. /* Override the syntax checking config to force syntax checking. */
  202. if (slapi_entry_syntax_check(NULL, e, 1) != 0) {
  203. char *error_text = NULL;
  204. /* We need a pblock to get more details on the syntax violation,
  205. * but we don't want to allocate a pblock unless we need it for
  206. * performance reasons. This means that we will actually call
  207. * slapi_entry_syntax_check() twice for entries that have a
  208. * syntax violation. */
  209. pb = slapi_pblock_new();
  210. slapi_entry_syntax_check(pb, e, 1);
  211. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &error_text);
  212. slapi_log_error(SLAPI_LOG_FATAL, SYNTAX_PLUGIN_SUBSYSTEM,
  213. "Entry \"%s\" violates syntax.\n%s",
  214. dn, error_text);
  215. slapi_pblock_destroy(pb);
  216. /* Keep a tally of the number of invalid entries found. */
  217. slapi_counter_increment(td->invalid_entries);
  218. }
  219. bail:
  220. return rc;
  221. }
  222. /* extract a single value from the entry (as a string) -- if it's not in the
  223. * entry, the default will be returned (which can be NULL).
  224. * you do not need to free anything returned by this.
  225. */
  226. static const char *
  227. fetch_attr(Slapi_Entry *e, const char *attrname,
  228. const char *default_val)
  229. {
  230. Slapi_Attr *attr;
  231. Slapi_Value *val = NULL;
  232. if (slapi_entry_attr_find(e, attrname, &attr) != 0) {
  233. return default_val;
  234. }
  235. slapi_attr_first_value(attr, &val);
  236. return slapi_value_get_string(val);
  237. }
  238. /*
  239. * Plug-in identity management helper functions
  240. */
  241. static void
  242. syntax_validate_set_plugin_id(void * plugin_id)
  243. {
  244. _PluginID=plugin_id;
  245. }
  246. static void *
  247. syntax_validate_get_plugin_id()
  248. {
  249. return _PluginID;
  250. }