plugin_mr.c 7.0 KB

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