validate_task.c 9.6 KB

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