1
0

validate_task.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 const char *fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val);
  39. static void syntax_validate_set_plugin_id(void *plugin_id);
  40. static void *syntax_validate_get_plugin_id(void);
  41. /*
  42. * Function Implementations
  43. */
  44. int
  45. syntax_validate_task_init(Slapi_PBlock *pb)
  46. {
  47. int rc = 0;
  48. char *syntax_validate_plugin_identity = NULL;
  49. /* Save plugin ID. */
  50. slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &syntax_validate_plugin_identity);
  51. PR_ASSERT(syntax_validate_plugin_identity);
  52. syntax_validate_set_plugin_id(syntax_validate_plugin_identity);
  53. /* Register task callback. */
  54. rc = slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION,
  55. (void *)SLAPI_PLUGIN_VERSION_03);
  56. rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN,
  57. (void *)syntax_validate_task_start);
  58. return rc;
  59. }
  60. static int
  61. syntax_validate_task_start(Slapi_PBlock *pb __attribute__((unused)))
  62. {
  63. int rc = slapi_task_register_handler("syntax validate", syntax_validate_task_add);
  64. return rc;
  65. }
  66. static int
  67. syntax_validate_task_add(Slapi_PBlock *pb __attribute__((unused)),
  68. Slapi_Entry *e,
  69. Slapi_Entry *eAfter __attribute__((unused)),
  70. int *returncode,
  71. char *returntext __attribute__((unused)),
  72. void *arg __attribute__((unused)))
  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_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  114. "syntax_validate_task_add - 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_err(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_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  162. "syntax_validate_task_thread - 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 %" PRIu64
  174. " invalid entries.\n",
  175. slapi_counter_get_value(td->invalid_entries));
  176. slapi_task_log_status(task, "Syntax validate task complete. Found %" PRIu64
  177. " invalid entries.\n",
  178. slapi_counter_get_value(td->invalid_entries));
  179. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM, "syntax_validate_task_thread - Complete."
  180. " Found %" PRIu64 " invalid entries.\n",
  181. slapi_counter_get_value(td->invalid_entries));
  182. slapi_task_inc_progress(task);
  183. /* this will queue the destruction of the task */
  184. slapi_task_finish(task, rc);
  185. slapi_task_dec_refcount(task);
  186. slapi_log_err(SLAPI_LOG_PLUGIN, SYNTAX_PLUGIN_SUBSYSTEM,
  187. "syntax_validate_task_thread - refcount decremented.\n");
  188. }
  189. static int
  190. syntax_validate_task_callback(Slapi_Entry *e, void *callback_data)
  191. {
  192. int rc = 0;
  193. char *dn = slapi_entry_get_dn(e);
  194. task_data *td = (task_data *)callback_data;
  195. Slapi_PBlock *pb = NULL;
  196. /*
  197. * If the server is ordered to shutdown, stop the fixup and return an error.
  198. */
  199. if (slapi_is_shutting_down()) {
  200. rc = -1;
  201. goto bail;
  202. }
  203. /* Override the syntax checking config to force syntax checking. */
  204. if (slapi_entry_syntax_check(NULL, e, 1) != 0) {
  205. char *error_text = NULL;
  206. /* We need a pblock to get more details on the syntax violation,
  207. * but we don't want to allocate a pblock unless we need it for
  208. * performance reasons. This means that we will actually call
  209. * slapi_entry_syntax_check() twice for entries that have a
  210. * syntax violation. */
  211. pb = slapi_pblock_new();
  212. slapi_entry_syntax_check(pb, e, 1);
  213. slapi_pblock_get(pb, SLAPI_PB_RESULT_TEXT, &error_text);
  214. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  215. "syntax_validate_task_callback - Entry \"%s\" violates syntax.\n%s",
  216. dn, error_text);
  217. slapi_pblock_destroy(pb);
  218. /* Keep a tally of the number of invalid entries found. */
  219. slapi_counter_increment(td->invalid_entries);
  220. }
  221. bail:
  222. return rc;
  223. }
  224. /* extract a single value from the entry (as a string) -- if it's not in the
  225. * entry, the default will be returned (which can be NULL).
  226. * you do not need to free anything returned by this.
  227. */
  228. static const char *
  229. fetch_attr(Slapi_Entry *e, const char *attrname, const char *default_val)
  230. {
  231. Slapi_Attr *attr;
  232. Slapi_Value *val = NULL;
  233. if (slapi_entry_attr_find(e, attrname, &attr) != 0) {
  234. return default_val;
  235. }
  236. slapi_attr_first_value(attr, &val);
  237. return slapi_value_get_string(val);
  238. }
  239. /*
  240. * Plug-in identity management helper functions
  241. */
  242. static void
  243. syntax_validate_set_plugin_id(void *plugin_id)
  244. {
  245. _PluginID = plugin_id;
  246. }
  247. static void *
  248. syntax_validate_get_plugin_id(void)
  249. {
  250. return _PluginID;
  251. }