validate_task.c 9.8 KB

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