plugin_mr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. struct mr_indexer_private {
  46. Slapi_Value **sva; /* if using index_sv_fn */
  47. struct berval **bva; /* if using index_fn */
  48. };
  49. static oid_item_t* global_mr_oids = NULL;
  50. static PRLock* global_mr_oids_lock = NULL;
  51. static int default_mr_indexer_create(Slapi_PBlock* pb);
  52. static void
  53. init_global_mr_lock()
  54. {
  55. if(global_mr_oids_lock==NULL)
  56. {
  57. global_mr_oids_lock = PR_NewLock();
  58. }
  59. }
  60. struct slapdplugin *
  61. slapi_get_global_mr_plugins()
  62. {
  63. return get_plugin_list(PLUGIN_LIST_MATCHINGRULE);
  64. }
  65. struct slapdplugin *
  66. plugin_mr_find( const char *nameoroid )
  67. {
  68. struct slapdplugin *pi;
  69. for ( pi = get_plugin_list(PLUGIN_LIST_MATCHINGRULE); pi != NULL; pi = pi->plg_next ) {
  70. if ( charray_inlist( pi->plg_mr_names, (char *)nameoroid ) ) {
  71. break;
  72. }
  73. }
  74. return ( pi );
  75. }
  76. static int
  77. plugin_mr_get_type(struct slapdplugin *pi)
  78. {
  79. int rc = LDAP_FILTER_EQUALITY;
  80. if (pi) {
  81. char **str = pi->plg_mr_names;
  82. for (; str && *str; ++str) {
  83. if (PL_strcasestr(*str, "substr")) {
  84. rc = LDAP_FILTER_SUBSTRINGS;
  85. break;
  86. }
  87. if (PL_strcasestr(*str, "approx")) {
  88. rc = LDAP_FILTER_APPROX;
  89. break;
  90. }
  91. }
  92. }
  93. return rc;
  94. }
  95. static struct slapdplugin*
  96. plugin_mr_find_registered (char* oid)
  97. {
  98. oid_item_t* i;
  99. init_global_mr_lock();
  100. PR_Lock (global_mr_oids_lock);
  101. i = global_mr_oids;
  102. PR_Unlock (global_mr_oids_lock);
  103. for (; i != NULL; i = i->oi_next)
  104. {
  105. if (!strcasecmp (oid, i->oi_oid))
  106. {
  107. LDAPDebug (LDAP_DEBUG_FILTER, "plugin_mr_find_registered(%s) != NULL\n", oid, 0, 0);
  108. return i->oi_plugin;
  109. }
  110. }
  111. LDAPDebug (LDAP_DEBUG_FILTER, "plugin_mr_find_registered(%s) == NULL\n", oid, 0, 0);
  112. return NULL;
  113. }
  114. static void
  115. plugin_mr_bind (char* oid, struct slapdplugin* plugin)
  116. {
  117. oid_item_t* i = (oid_item_t*) slapi_ch_malloc (sizeof (oid_item_t));
  118. LDAPDebug (LDAP_DEBUG_FILTER, "=> plugin_mr_bind(%s)\n", oid, 0, 0);
  119. init_global_mr_lock();
  120. i->oi_oid = slapi_ch_strdup (oid);
  121. i->oi_plugin = plugin;
  122. PR_Lock (global_mr_oids_lock);
  123. i->oi_next = global_mr_oids;
  124. global_mr_oids = i;
  125. PR_Unlock (global_mr_oids_lock);
  126. LDAPDebug (LDAP_DEBUG_FILTER, "<= plugin_mr_bind\n", 0, 0, 0);
  127. }
  128. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  129. slapi_mr_indexer_create (Slapi_PBlock* opb)
  130. {
  131. int rc;
  132. char* oid;
  133. if (!(rc = slapi_pblock_get (opb, SLAPI_PLUGIN_MR_OID, &oid)))
  134. {
  135. IFP createFn = NULL;
  136. struct slapdplugin* mrp = plugin_mr_find_registered (oid);
  137. if (mrp != NULL)
  138. {
  139. if (!(rc = slapi_pblock_set (opb, SLAPI_PLUGIN, mrp)) &&
  140. !(rc = slapi_pblock_get (opb, SLAPI_PLUGIN_MR_INDEXER_CREATE_FN, &createFn)) &&
  141. createFn != NULL)
  142. {
  143. rc = createFn (opb);
  144. }
  145. }
  146. else
  147. {
  148. /* call each plugin, until one is able to handle this request. */
  149. rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  150. for (mrp = get_plugin_list(PLUGIN_LIST_MATCHINGRULE); mrp != NULL; mrp = mrp->plg_next)
  151. {
  152. IFP indexFn = NULL;
  153. Slapi_PBlock pb;
  154. memcpy (&pb, opb, sizeof(Slapi_PBlock));
  155. if (!(rc = slapi_pblock_set (&pb, SLAPI_PLUGIN, mrp)) &&
  156. !(rc = slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_INDEXER_CREATE_FN, &createFn)) &&
  157. createFn != NULL &&
  158. !(rc = createFn (&pb)) &&
  159. ((!(rc = slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_INDEX_FN, &indexFn)) &&
  160. indexFn != NULL) ||
  161. (!(rc = slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_INDEX_SV_FN, &indexFn)) &&
  162. indexFn != NULL)))
  163. {
  164. /* Success: this plugin can handle it. */
  165. memcpy (opb, &pb, sizeof(Slapi_PBlock));
  166. plugin_mr_bind (oid, mrp); /* for future reference */
  167. rc = 0; /* success */
  168. break;
  169. }
  170. }
  171. if (rc != 0) {
  172. /* look for a new syntax-style mr plugin */
  173. struct slapdplugin *pi = plugin_mr_find(oid);
  174. if (pi) {
  175. Slapi_PBlock pb;
  176. memcpy (&pb, opb, sizeof(Slapi_PBlock));
  177. slapi_pblock_set(&pb, SLAPI_PLUGIN, pi);
  178. rc = default_mr_indexer_create(&pb);
  179. if (!rc) {
  180. memcpy (opb, &pb, sizeof(Slapi_PBlock));
  181. plugin_mr_bind (oid, pi); /* for future reference */
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return rc;
  188. }
  189. static int
  190. attempt_mr_filter_create (mr_filter_t* f, struct slapdplugin* mrp, Slapi_PBlock* pb)
  191. {
  192. int rc;
  193. IFP mrf_create = NULL;
  194. f->mrf_match = NULL;
  195. pblock_init (pb);
  196. if (!(rc = slapi_pblock_set (pb, SLAPI_PLUGIN, mrp)) &&
  197. !(rc = slapi_pblock_get (pb, SLAPI_PLUGIN_MR_FILTER_CREATE_FN, &mrf_create)) &&
  198. mrf_create != NULL &&
  199. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_OID, f->mrf_oid)) &&
  200. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_TYPE, f->mrf_type)) &&
  201. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_MR_VALUE, &(f->mrf_value))) &&
  202. !(rc = mrf_create (pb)) &&
  203. !(rc = slapi_pblock_get (pb, SLAPI_PLUGIN_MR_FILTER_MATCH_FN, &(f->mrf_match)))) {
  204. if (f->mrf_match == NULL)
  205. {
  206. rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  207. }
  208. }
  209. return rc;
  210. }
  211. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  212. plugin_mr_filter_create (mr_filter_t* f)
  213. {
  214. int rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  215. struct slapdplugin* mrp = plugin_mr_find_registered (f->mrf_oid);
  216. Slapi_PBlock pb;
  217. if (mrp != NULL)
  218. {
  219. rc = attempt_mr_filter_create (f, mrp, &pb);
  220. }
  221. else
  222. {
  223. /* call each plugin, until one is able to handle this request. */
  224. for (mrp = get_plugin_list(PLUGIN_LIST_MATCHINGRULE); mrp != NULL; mrp = mrp->plg_next)
  225. {
  226. if (!(rc = attempt_mr_filter_create (f, mrp, &pb)))
  227. {
  228. plugin_mr_bind (f->mrf_oid, mrp); /* for future reference */
  229. break;
  230. }
  231. }
  232. }
  233. if (!rc)
  234. {
  235. /* This plugin has created the desired filter. */
  236. f->mrf_plugin = mrp;
  237. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_INDEX_FN, &(f->mrf_index));
  238. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_REUSABLE, &(f->mrf_reusable));
  239. slapi_pblock_get (&pb, SLAPI_PLUGIN_MR_FILTER_RESET_FN, &(f->mrf_reset));
  240. slapi_pblock_get (&pb, SLAPI_PLUGIN_OBJECT, &(f->mrf_object));
  241. slapi_pblock_get (&pb, SLAPI_PLUGIN_DESTROY_FN, &(f->mrf_destroy));
  242. }
  243. return rc;
  244. }
  245. int /* an LDAP error code, hopefully LDAP_SUCCESS */
  246. slapi_mr_filter_index (Slapi_Filter* f, Slapi_PBlock* pb)
  247. {
  248. int rc = LDAP_UNAVAILABLE_CRITICAL_EXTENSION;
  249. if (f->f_choice == LDAP_FILTER_EXTENDED && f->f_mr.mrf_index != NULL &&
  250. !(rc = slapi_pblock_set (pb, SLAPI_PLUGIN_OBJECT, f->f_mr.mrf_object)))
  251. {
  252. rc = f->f_mr.mrf_index (pb);
  253. }
  254. return rc;
  255. }
  256. static struct mr_indexer_private *
  257. mr_indexer_private_new()
  258. {
  259. return (struct mr_indexer_private *)slapi_ch_calloc(1, sizeof(struct mr_indexer_private));
  260. }
  261. static void
  262. mr_indexer_private_done(struct mr_indexer_private *mrip)
  263. {
  264. if (mrip && mrip->sva) {
  265. valuearray_free(&mrip->sva);
  266. } else if (mrip && mrip->bva) {
  267. ber_bvecfree(mrip->bva);
  268. mrip->bva = NULL;
  269. }
  270. }
  271. static void
  272. mr_indexer_private_free(struct mr_indexer_private **mrip)
  273. {
  274. if (mrip) {
  275. mr_indexer_private_done(*mrip);
  276. slapi_ch_free((void **)mrip);
  277. }
  278. }
  279. /* this function takes SLAPI_PLUGIN_MR_VALUES as Slapi_Value ** and
  280. returns SLAPI_PLUGIN_MR_KEYS as Slapi_Value **
  281. */
  282. static int
  283. mr_wrap_mr_index_sv_fn(Slapi_PBlock* pb)
  284. {
  285. int rc = -1;
  286. Slapi_Value **in_vals = NULL;
  287. Slapi_Value **out_vals = NULL;
  288. struct slapdplugin *pi = NULL;
  289. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_KEYS, out_vals); /* make sure output is cleared */
  290. slapi_pblock_get(pb, SLAPI_PLUGIN, &pi);
  291. if (!pi) {
  292. LDAPDebug0Args(LDAP_DEBUG_ANY, "mr_wrap_mr_index_sv_fn: error - no plugin specified\n");
  293. } else if (!pi->plg_mr_values2keys) {
  294. LDAPDebug0Args(LDAP_DEBUG_ANY, "mr_wrap_mr_index_sv_fn: error - plugin has no plg_mr_values2keys function\n");
  295. } else {
  296. struct mr_indexer_private *mrip = NULL;
  297. int ftype = plugin_mr_get_type(pi);
  298. slapi_pblock_get(pb, SLAPI_PLUGIN_MR_VALUES, &in_vals);
  299. (*pi->plg_mr_values2keys)(pb, in_vals, &out_vals, ftype);
  300. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_KEYS, out_vals);
  301. /* we have to save out_vals to free next time or during destroy */
  302. slapi_pblock_get(pb, SLAPI_PLUGIN_OBJECT, &mrip);
  303. mr_indexer_private_done(mrip); /* free old vals, if any */
  304. mrip->sva = out_vals; /* save pointer for later */
  305. rc = 0;
  306. }
  307. return rc;
  308. }
  309. /* this function takes SLAPI_PLUGIN_MR_VALUES as struct berval ** and
  310. returns SLAPI_PLUGIN_MR_KEYS as struct berval **
  311. */
  312. static int
  313. mr_wrap_mr_index_fn(Slapi_PBlock* pb)
  314. {
  315. int rc = -1;
  316. struct berval **in_vals = NULL;
  317. struct berval **out_vals = NULL;
  318. struct mr_indexer_private *mrip = NULL;
  319. Slapi_Value **in_vals_sv = NULL;
  320. Slapi_Value **out_vals_sv = NULL;
  321. slapi_pblock_get(pb, SLAPI_PLUGIN_MR_VALUES, &in_vals); /* get bervals */
  322. /* convert bervals to sv ary */
  323. valuearray_init_bervalarray(in_vals, &in_vals_sv);
  324. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_VALUES, in_vals_sv); /* use sv */
  325. rc = mr_wrap_mr_index_sv_fn(pb);
  326. /* get result sv keys */
  327. slapi_pblock_get(pb, SLAPI_PLUGIN_MR_KEYS, &out_vals_sv);
  328. /* convert to bvec */
  329. valuearray_get_bervalarray(out_vals_sv, &out_vals);
  330. valuearray_free(&out_vals_sv); /* don't need svals */
  331. /* we have to save out_vals to free next time or during destroy */
  332. slapi_pblock_get(pb, SLAPI_PLUGIN_OBJECT, &mrip);
  333. mr_indexer_private_done(mrip); /* free old vals, if any */
  334. mrip->bva = out_vals; /* save pointer for later */
  335. return rc;
  336. }
  337. static int
  338. default_mr_indexer_destroy(Slapi_PBlock* pb)
  339. {
  340. struct mr_indexer_private *mrip = NULL;
  341. slapi_pblock_get(pb, SLAPI_PLUGIN_OBJECT, &mrip);
  342. mr_indexer_private_free(&mrip);
  343. mrip = NULL;
  344. slapi_pblock_set(pb, SLAPI_PLUGIN_OBJECT, mrip);
  345. return 0;
  346. }
  347. /* this is the default mr indexer create func
  348. for new syntax-style mr plugins */
  349. static int
  350. default_mr_indexer_create(Slapi_PBlock* pb)
  351. {
  352. int rc = -1;
  353. struct slapdplugin *pi = NULL;
  354. slapi_pblock_get(pb, SLAPI_PLUGIN, &pi);
  355. if (NULL == pi) {
  356. LDAPDebug0Args(LDAP_DEBUG_ANY, "default_mr_indexer_create: error - no plugin specified\n");
  357. goto done;
  358. }
  359. if (NULL == pi->plg_mr_values2keys) {
  360. LDAPDebug1Arg(LDAP_DEBUG_ANY, "default_mr_indexer_create: error - plugin [%s] has no plg_mr_values2keys function\n",
  361. pi->plg_name);
  362. goto done;
  363. }
  364. slapi_pblock_set(pb, SLAPI_PLUGIN_OBJECT, mr_indexer_private_new());
  365. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_INDEX_FN, mr_wrap_mr_index_fn);
  366. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_INDEX_SV_FN, mr_wrap_mr_index_sv_fn);
  367. slapi_pblock_set(pb, SLAPI_PLUGIN_DESTROY_FN, default_mr_indexer_destroy);
  368. slapi_pblock_set(pb, SLAPI_PLUGIN_MR_INDEXER_CREATE_FN, default_mr_indexer_create);
  369. rc = 0;
  370. done:
  371. return rc;
  372. }