validate_task.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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, Slapi_Entry *eAfter, int *returncode, char *returntext, void *arg);
  35. static void syntax_validate_task_destructor(Slapi_Task *task);
  36. static void syntax_validate_task_thread(void *arg);
  37. static int syntax_validate_task_callback(Slapi_Entry *e, void *callback_data);
  38. static void syntax_validate_set_plugin_id(void *plugin_id);
  39. static void *syntax_validate_get_plugin_id(void);
  40. /*
  41. * Function Implementations
  42. */
  43. int
  44. syntax_validate_task_init(Slapi_PBlock *pb)
  45. {
  46. int rc = 0;
  47. char *syntax_validate_plugin_identity = NULL;
  48. /* Save plugin ID. */
  49. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &syntax_validate_plugin_identity);
  50. PR_ASSERT(syntax_validate_plugin_identity);
  51. syntax_validate_set_plugin_id(syntax_validate_plugin_identity);
  52. /* Register task callback. */
  53. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  54. (void *)SLAPI_PLUGIN_VERSION_03);
  55. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  56. (void *)syntax_validate_task_start);
  57. return rc;
  58. }
  59. static int
  60. syntax_validate_task_start(Slapi_PBlock *pb __attribute__((unused)))
  61. {
  62. int rc = slapi_task_register_handler("syntax validate", syntax_validate_task_add);
  63. return rc;
  64. }
  65. static int
  66. syntax_validate_task_add(Slapi_PBlock *pb __attribute__((unused)),
  67. Slapi_Entry *e,
  68. Slapi_Entry *eAfter __attribute__((unused)),
  69. int *returncode,
  70. char *returntext __attribute__((unused)),
  71. void *arg __attribute__((unused)))
  72. {
  73. PRThread *thread = NULL;
  74. int rv = SLAPI_DSE_CALLBACK_OK;
  75. task_data *mytaskdata = NULL;
  76. Slapi_Task *task = NULL;
  77. const char *filter;
  78. const char *dn = 0;
  79. *returncode = LDAP_SUCCESS;
  80. /* get arg(s) */
  81. if ((dn = fetch_attr(e, "basedn", 0)) == NULL) {
  82. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  83. rv = SLAPI_DSE_CALLBACK_ERROR;
  84. goto out;
  85. }
  86. if ((filter = fetch_attr(e, "filter", "(objectclass=*)")) == NULL) {
  87. *returncode = LDAP_OBJECT_CLASS_VIOLATION;
  88. rv = SLAPI_DSE_CALLBACK_ERROR;
  89. goto out;
  90. }
  91. /* setup our task data */
  92. mytaskdata = (task_data *)slapi_ch_malloc(sizeof(task_data));
  93. if (mytaskdata == NULL) {
  94. *returncode = LDAP_OPERATIONS_ERROR;
  95. rv = SLAPI_DSE_CALLBACK_ERROR;
  96. goto out;
  97. }
  98. mytaskdata->dn = slapi_ch_strdup(dn);
  99. mytaskdata->filter_str = slapi_ch_strdup(filter);
  100. mytaskdata->invalid_entries = slapi_counter_new();
  101. /* allocate new task now */
  102. task = slapi_new_task(slapi_entry_get_ndn(e));
  103. /* register our destructor for cleaning up our private data */
  104. slapi_task_set_destructor_fn(task, syntax_validate_task_destructor);
  105. /* Stash a pointer to our data in the task */
  106. slapi_task_set_data(task, mytaskdata);
  107. /* start the sample task as a separate thread */
  108. thread = PR_CreateThread(PR_USER_THREAD, syntax_validate_task_thread,
  109. (void *)task, PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
  110. PR_UNJOINABLE_THREAD, SLAPD_DEFAULT_THREAD_STACKSIZE);
  111. if (thread == NULL) {
  112. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  113. "syntax_validate_task_add - Unable to create task thread!\n");
  114. *returncode = LDAP_OPERATIONS_ERROR;
  115. rv = SLAPI_DSE_CALLBACK_ERROR;
  116. slapi_task_finish(task, *returncode);
  117. } else {
  118. rv = SLAPI_DSE_CALLBACK_OK;
  119. }
  120. out:
  121. return rv;
  122. }
  123. static void
  124. syntax_validate_task_destructor(Slapi_Task *task)
  125. {
  126. if (task) {
  127. task_data *mydata = (task_data *)slapi_task_get_data(task);
  128. while (slapi_task_get_refcount(task) > 0) {
  129. /* Yield to wait for the fixup task finishes. */
  130. DS_Sleep(PR_MillisecondsToInterval(100));
  131. }
  132. if (mydata) {
  133. slapi_ch_free_string(&mydata->dn);
  134. slapi_ch_free_string(&mydata->filter_str);
  135. slapi_counter_destroy(&mydata->invalid_entries);
  136. /* Need to cast to avoid a compiler warning */
  137. slapi_ch_free((void **)&mydata);
  138. }
  139. }
  140. }
  141. static void
  142. syntax_validate_task_thread(void *arg)
  143. {
  144. int rc = 0;
  145. Slapi_Task *task = (Slapi_Task *)arg;
  146. task_data *td = NULL;
  147. Slapi_PBlock *search_pb = NULL;
  148. if (!task) {
  149. return; /* no task */
  150. }
  151. slapi_task_inc_refcount(task);
  152. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  153. "syntax_validate_task_thread - refcount incremented.\n");
  154. /* Fetch our task data from the task */
  155. td = (task_data *)slapi_task_get_data(task);
  156. /* Log started message. */
  157. slapi_task_begin(task, 1);
  158. slapi_task_log_notice(task, "Syntax validation task starting (arg: %s) ...\n",
  159. td->filter_str);
  160. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  161. "syntax_validate_task_thread - Starting (base: \"%s\", filter: \"%s\") ...\n",
  162. td->dn, td->filter_str);
  163. /* Perform the search and use a callback
  164. * to validate each matching entry. */
  165. search_pb = slapi_pblock_new();
  166. slapi_search_internal_set_pb(search_pb, td->dn,
  167. LDAP_SCOPE_SUBTREE, td->filter_str, 0, 0,
  168. 0, 0, syntax_validate_get_plugin_id(), 0);
  169. rc = slapi_search_internal_callback_pb(search_pb, td, 0, syntax_validate_task_callback, 0);
  170. slapi_pblock_destroy(search_pb);
  171. /* Log finished message. */
  172. slapi_task_log_notice(task, "Syntax validate task complete. Found %" PRIu64
  173. " invalid entries.\n",
  174. slapi_counter_get_value(td->invalid_entries));
  175. slapi_task_log_status(task, "Syntax validate task complete. Found %" PRIu64
  176. " invalid entries.\n",
  177. slapi_counter_get_value(td->invalid_entries));
  178. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM, "syntax_validate_task_thread - Complete."
  179. " Found %" PRIu64 " invalid entries.\n",
  180. slapi_counter_get_value(td->invalid_entries));
  181. slapi_task_inc_progress(task);
  182. /* this will queue the destruction of the task */
  183. slapi_task_finish(task, rc);
  184. slapi_task_dec_refcount(task);
  185. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  186. "syntax_validate_task_thread - refcount decremented.\n");
  187. }
  188. static int
  189. syntax_validate_task_callback(Slapi_Entry *e, void *callback_data)
  190. {
  191. int rc = 0;
  192. char *dn = slapi_entry_get_dn(e);
  193. task_data *td = (task_data *)callback_data;
  194. Slapi_PBlock *pb = NULL;
  195. /*
  196. * If the server is ordered to shutdown, stop the fixup and return an error.
  197. */
  198. if (slapi_is_shutting_down()) {
  199. rc = -1;
  200. goto bail;
  201. }
  202. /* Override the syntax checking config to force syntax checking. */
  203. if (slapi_entry_syntax_check(NULL, e, 1) != 0) {
  204. char *error_text = NULL;
  205. /* We need a pblock to get more details on the syntax violation,
  206. * but we don't want to allocate a pblock unless we need it for
  207. * performance reasons. This means that we will actually call
  208. * slapi_entry_syntax_check() twice for entries that have a
  209. * syntax violation. */
  210. pb = slapi_pblock_new();
  211. slapi_entry_syntax_check(pb, e, 1);
  212. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &error_text);
  213. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  214. "syntax_validate_task_callback - Entry \"%s\" violates syntax.\n%s",
  215. dn, error_text);
  216. slapi_pblock_destroy(pb);
  217. /* Keep a tally of the number of invalid entries found. */
  218. slapi_counter_increment(td->invalid_entries);
  219. }
  220. bail:
  221. return rc;
  222. }
  223. /*
  224. * Plug-in identity management helper functions
  225. */
  226. static void
  227. syntax_validate_set_plugin_id(void *plugin_id)
  228. {
  229. _PluginID = plugin_id;
  230. }
  231. static void *
  232. syntax_validate_get_plugin_id(void)
  233. {
  234. return _PluginID;
  235. }