plugin_mr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /*
  42. * plugin_mr.c - routines for calling matching rule plugins
  43. */
  44. #include "slap.h"
  45. static oid_item_t* global_mr_oids = NULL;
  46. static PRLock* global_mr_oids_lock = NULL;
  47. static void
  48. init_global_mr_lock()
  49. {
  50. if(global_mr_oids_lock==NULL)
  51. {
  52. global_mr_oids_lock = PR_NewLock();
  53. }
  54. }
  55. struct slapdplugin *
  56. slapi_get_global_mr_plugins()
  57. {
  58. return get_plugin_list(PLUGIN_LIST_MATCHINGRULE);
  59. }
  60. static struct slapdplugin*
  61. plugin_mr_find (char* oid)
  62. {
  63. oid_item_t* i;
  64. init_global_mr_lock();
  65. PR_Lock (global_mr_oids_lock);
  66. i = global_mr_oids;
  67. PR_Unlock (global_mr_oids_lock);
  68. for (; i != NULL; i = i->oi_next)
  69. {
  70. if (!strcasecmp (oid, i->oi_oid))
  71. {
  72. LDAPDebug (LDAP_DEBUG_FILTER, "plugin_mr_find(%s) != NULL\n", oid, 0, 0);
  73. return i->oi_plugin;
  74. }
  75. }
  76. LDAPDebug (LDAP_DEBUG_FILTER, "plugin_mr_find(%s) == NULL\n", oid, 0, 0);
  77. return NULL;
  78. }
  79. static void
  80. plugin_mr_bind (char* oid, struct slapdplugin* plugin)
  81. {
  82. oid_item_t* i = (oid_item_t*) slapi_ch_malloc (sizeof (oid_item_t));
  83. LDAPDebug (LDAP_DEBUG_FILTER, "=> plugin_mr_bind(%s)\n", oid, 0, 0);
  84. init_global_mr_lock();
  85. i->oi_oid = slapi_ch_strdup (oid);
  86. i->oi_plugin = plugin;
  87. PR_Lock (global_mr_oids_lock);
  88. i->oi_next = global_mr_oids;
  89. global_mr_oids = i;
  90. PR_Unlock (global_mr_oids_lock);
  91. LDAPDebug (LDAP_DEBUG_FILTER, "<= plugin_mr_bind\n", 0, 0, 0);
  92. }
  93. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  94. slapi_mr_indexer_create (Slapi_PBlock* opb)
  95. {
  96. int rc;
  97. char* oid;
  98. if (!(rc = slapi_pblock_get (opb, SLAPI_PLUGIN_MR_OID, &oid)))
  99. {
  100. IFP createFn = NULL;
  101. struct slapdplugin* mrp = plugin_mr_find (oid);
  102. if (mrp != NULL)
  103. {
  104. if (!(rc = slapi_pblock_set (opb, SLAPI_PLUGIN, mrp)) &&
  105. !(rc = slapi_pblock_get (opb, SLAPI_PLUGIN_MR_INDEXER_CREATE_FN, &createFn)) &&
  106. createFn != NULL)
  107. {
  108. rc = createFn (opb);
  109. }
  110. }
  111. else
  112. {
  113. /* call each plugin, until one is able to handle this request. */
  114. rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  115. for (mrp = get_plugin_list(PLUGIN_LIST_MATCHINGRULE); mrp != NULL; mrp = mrp->plg_next)
  116. {
  117. IFP indexFn = NULL;
  118. Slapi_PBlock pb;
  119. memcpy (&pb, opb, sizeof(Slapi_PBlock));
  120. if (!(rc = slapi_pblock_set (&pb, SLAPI_PLUGIN, mrp)) &&
  121. !(rc = slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_INDEXER_CREATE_FN, &createFn)) &&
  122. createFn != NULL &&
  123. !(rc = createFn (&pb)) &&
  124. !(rc = slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_INDEX_FN, &indexFn)) &&
  125. indexFn != NULL)
  126. {
  127. /* Success: this plugin can handle it. */
  128. memcpy (opb, &pb, sizeof(Slapi_PBlock));
  129. plugin_mr_bind (oid, mrp); /* for future reference */
  130. break;
  131. }
  132. }
  133. }
  134. }
  135. return rc;
  136. }
  137. static int
  138. attempt_mr_filter_create (mr_filter_t* f, struct slapdplugin* mrp, Slapi_PBlock* pb)
  139. {
  140. int rc;
  141. IFP mrf_create = NULL;
  142. f->mrf_match = NULL;
  143. pblock_init (pb);
  144. if (!(rc = slapi_pblock_set (pb, SLAPI_PLUGIN, mrp)) &&
  145. !(rc = slapi_pblock_get (pb, SLAPI_PLUGIN_MR_FILTER_CREATE_FN, &mrf_create)) &&
  146. mrf_create != NULL &&
  147. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_OID, f->mrf_oid)) &&
  148. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_TYPE, f->mrf_type)) &&
  149. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_VALUE, &(f->mrf_value))) &&
  150. !(rc = mrf_create (pb)) &&
  151. !(rc = slapi_pblock_get (pb, SLAPI_PLUGIN_MR_FILTER_MATCH_FN, &(f->mrf_match)))) {
  152. if (f->mrf_match == NULL)
  153. {
  154. rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  155. }
  156. }
  157. return rc;
  158. }
  159. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  160. plugin_mr_filter_create (mr_filter_t* f)
  161. {
  162. int rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  163. struct slapdplugin* mrp = plugin_mr_find (f->mrf_oid);
  164. Slapi_PBlock pb;
  165. if (mrp != NULL)
  166. {
  167. rc = attempt_mr_filter_create (f, mrp, &pb);
  168. }
  169. else
  170. {
  171. /* call each plugin, until one is able to handle this request. */
  172. for (mrp = get_plugin_list(PLUGIN_LIST_MATCHINGRULE); mrp != NULL; mrp = mrp->plg_next)
  173. {
  174. if (!(rc = attempt_mr_filter_create (f, mrp, &pb)))
  175. {
  176. plugin_mr_bind (f->mrf_oid, mrp); /* for future reference */
  177. break;
  178. }
  179. }
  180. }
  181. if (!rc)
  182. {
  183. /* This plugin has created the desired filter. */
  184. f->mrf_plugin = mrp;
  185. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_INDEX_FN, &(f->mrf_index));
  186. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_REUSABLE, &(f->mrf_reusable));
  187. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_RESET_FN, &(f->mrf_reset));
  188. slapi_pblock_get (&pb, SLAPI_PLUGIN_OBJECT, &(f->mrf_object));
  189. slapi_pblock_get (&pb, SLAPI_PLUGIN_DESTROY_FN, &(f->mrf_destroy));
  190. }
  191. return rc;
  192. }
  193. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  194. slapi_mr_filter_index (Slapi_Filter* f, Slapi_PBlock* pb)
  195. {
  196. int rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  197. if (f->f_choice == LDAP_FILTER_EXTENDED && f->f_mr.mrf_index != NULL &&
  198. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_OBJECT, f->f_mr.mrf_object)))
  199. {
  200. rc = f->f_mr.mrf_index (pb);
  201. }
  202. return rc;
  203. }