ldbm_attr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. /* attr.c - backend routines for dealing with attributes */
  42. #include "back-ldbm.h"
  43. struct attrinfo *
  44. attrinfo_new()
  45. {
  46. struct attrinfo *p= (struct attrinfo *)slapi_ch_calloc(1, sizeof(struct attrinfo));
  47. return p;
  48. }
  49. void
  50. attrinfo_delete(struct attrinfo **pp)
  51. {
  52. if(pp!=NULL && *pp!=NULL)
  53. {
  54. idl_release_private(*pp);
  55. (*pp)->ai_key_cmp_fn = NULL;
  56. slapi_ch_free((void**)&((*pp)->ai_type));
  57. slapi_ch_free((void**)(*pp)->ai_index_rules);
  58. slapi_ch_free((void**)&((*pp)->ai_attrcrypt));
  59. attr_done(&((*pp)->ai_sattr));
  60. slapi_ch_free((void**)pp);
  61. *pp= NULL;
  62. }
  63. }
  64. static int
  65. attrinfo_internal_delete( caddr_t data, caddr_t arg )
  66. {
  67. struct attrinfo *n = (struct attrinfo *)data;
  68. attrinfo_delete(&n);
  69. return 0;
  70. }
  71. void
  72. attrinfo_deletetree(ldbm_instance *inst)
  73. {
  74. avl_free( inst->inst_attrs, attrinfo_internal_delete );
  75. }
  76. static int
  77. ainfo_type_cmp(
  78. char *type,
  79. struct attrinfo *a
  80. )
  81. {
  82. return( strcasecmp( type, a->ai_type ) );
  83. }
  84. static int
  85. ainfo_cmp(
  86. struct attrinfo *a,
  87. struct attrinfo *b
  88. )
  89. {
  90. return( strcasecmp( a->ai_type, b->ai_type ) );
  91. }
  92. /*
  93. * Called when a duplicate "index" line is encountered.
  94. *
  95. * returns 1 => original from init code, indexmask updated
  96. * 2 => original not from init code, warn the user
  97. *
  98. * Hard coded to return a 1 always...
  99. *
  100. */
  101. static int
  102. ainfo_dup(
  103. struct attrinfo *a,
  104. struct attrinfo *b
  105. )
  106. {
  107. /* merge duplicate indexing information */
  108. if (b->ai_indexmask == 0 || b->ai_indexmask == INDEX_OFFLINE) {
  109. a->ai_indexmask = INDEX_OFFLINE; /* turns off all indexes */
  110. charray_free ( a->ai_index_rules );
  111. a->ai_index_rules = NULL;
  112. }
  113. a->ai_indexmask |= b->ai_indexmask;
  114. if ( b->ai_indexmask & INDEX_RULES ) {
  115. charray_merge( &a->ai_index_rules, b->ai_index_rules, 1 );
  116. }
  117. return( 1 );
  118. }
  119. void
  120. ainfo_get(
  121. backend *be,
  122. char *type,
  123. struct attrinfo **at
  124. )
  125. {
  126. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  127. if ( (*at = (struct attrinfo *) avl_find( inst->inst_attrs, type,
  128. ainfo_type_cmp )) == NULL ) {
  129. if ( (*at = (struct attrinfo *) avl_find( inst->inst_attrs,
  130. LDBM_PSEUDO_ATTR_DEFAULT, ainfo_type_cmp )) == NULL ) {
  131. return;
  132. }
  133. }
  134. }
  135. void
  136. _set_attr_substrlen(int index, char *str, int **substrlens)
  137. {
  138. char *p = NULL;
  139. /* nsSubStrXxx=<VAL> is passed;
  140. set it to the attribute's plugin */
  141. p = strchr(str, '=');
  142. if (NULL != p) {
  143. long sublen = strtol(++p, (char **)NULL, 10);
  144. if (sublen > 0) { /* 0 is not acceptable */
  145. if (NULL == *substrlens) {
  146. *substrlens = (int *)slapi_ch_calloc(1,
  147. sizeof(int) * INDEX_SUBSTRLEN);
  148. }
  149. (*substrlens)[index] = sublen;
  150. }
  151. }
  152. }
  153. void
  154. attr_index_config(
  155. backend *be,
  156. char *fname,
  157. int lineno,
  158. Slapi_Entry *e,
  159. int init,
  160. int indextype_none
  161. )
  162. {
  163. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  164. int j = 0;
  165. struct attrinfo *a;
  166. int return_value = -1;
  167. int *substrlens = NULL;
  168. int need_compare_fn = 0;
  169. int hasIndexType = 0;
  170. const char *attrsyntax_oid = NULL;
  171. const struct berval *attrValue;
  172. Slapi_Value *sval;
  173. Slapi_Attr *attr;
  174. int mr_count = 0;
  175. /* Get the cn */
  176. if (0 == slapi_entry_attr_find(e, "cn", &attr)) {
  177. slapi_attr_first_value(attr, &sval);
  178. attrValue = slapi_value_get_berval(sval);
  179. } else {
  180. LDAPDebug(LDAP_DEBUG_ANY, "attr_index_config: Missing indexing arguments\n", 0, 0, 0);
  181. return;
  182. }
  183. a = attrinfo_new();
  184. slapi_attr_init(&a->ai_sattr, attrValue->bv_val);
  185. /*
  186. * we can't just set a->ai_type to the type from a->ai_sattr
  187. * if the type has attr options or subtypes, ai_sattr.a_type will
  188. * contain them - but for the purposes of indexing, we don't want them
  189. */
  190. a->ai_type = slapi_attr_basetype( attrValue->bv_val, NULL, 0 );
  191. attrsyntax_oid = attr_get_syntax_oid(&a->ai_sattr);
  192. a->ai_indexmask = 0;
  193. if(indextype_none){
  194. /* This is the same has having none for indexType, but applies to the whole entry */
  195. a->ai_indexmask = INDEX_OFFLINE;
  196. } else {
  197. slapi_entry_attr_find(e, "nsIndexType", &attr);
  198. for (j = slapi_attr_first_value(attr, &sval); j != -1;j = slapi_attr_next_value(attr, j, &sval)) {
  199. hasIndexType = 1;
  200. attrValue = slapi_value_get_berval(sval);
  201. if ( strncasecmp( attrValue->bv_val, "pres", 4 ) == 0 ) {
  202. a->ai_indexmask |= INDEX_PRESENCE;
  203. } else if ( strncasecmp( attrValue->bv_val, "eq", 2 ) == 0 ) {
  204. a->ai_indexmask |= INDEX_EQUALITY;
  205. } else if ( strncasecmp( attrValue->bv_val, "approx", 6 ) == 0 ) {
  206. a->ai_indexmask |= INDEX_APPROX;
  207. } else if ( strncasecmp( attrValue->bv_val, "subtree", 7 ) == 0 ) {
  208. /* subtree should be located before "sub" */
  209. a->ai_indexmask |= INDEX_SUBTREE;
  210. a->ai_dup_cmp_fn = entryrdn_compare_dups;
  211. } else if ( strncasecmp( attrValue->bv_val, "sub", 3 ) == 0 ) {
  212. a->ai_indexmask |= INDEX_SUB;
  213. } else if ( strncasecmp( attrValue->bv_val, "none", 4 ) == 0 ) {
  214. if ( a->ai_indexmask != 0 ) {
  215. LDAPDebug(LDAP_DEBUG_ANY,
  216. "%s: line %d: index type \"none\" cannot be combined with other types\n",
  217. fname, lineno, 0);
  218. }
  219. a->ai_indexmask = INDEX_OFFLINE; /* note that the index isn't available */
  220. } else {
  221. LDAPDebug(LDAP_DEBUG_ANY,
  222. "%s: line %d: unknown index type \"%s\" (ignored)\n",
  223. fname, lineno, attrValue->bv_val);
  224. LDAPDebug(LDAP_DEBUG_ANY,
  225. "valid index types are \"pres\", \"eq\", \"approx\", or \"sub\"\n",
  226. 0, 0, 0);
  227. }
  228. }
  229. if(hasIndexType == 0){
  230. /* indexType missing, error out */
  231. LDAPDebug(LDAP_DEBUG_ANY, "attr_index_config: Missing index type\n", 0, 0, 0);
  232. attrinfo_delete(&a);
  233. return;
  234. }
  235. }
  236. /* compute a->ai_index_rules: */
  237. /* for index rules there are two uses:
  238. * 1) a simple way to define an ordered index to support <= and >= searches
  239. * for those attributes which do not have an ORDERING matching rule defined
  240. * for them in their schema definition. The index generated is not a :RULE:
  241. * index, it is a normal = EQUALITY index, with the keys ordered using the
  242. * comparison function provided by the syntax plugin for the attribute. For
  243. * example - the uidNumber attribute has INTEGER syntax, but the standard
  244. * definition of the attribute does not specify an ORDERING matching rule.
  245. * By default, this means that you cannot perform searches like
  246. * (uidNumber>=501) - but many users expect to be able to perform this type of
  247. * search. By specifying that you want an ordered index, using an integer
  248. * matching rule, you can support indexed seaches of this type.
  249. * 2) a RULE index - the index key prefix is :NAMEOROID: - this is used
  250. * to support extensible match searches like (cn:fr-CA.3:=gilles), which would
  251. * find the index key :fr-CA.3:gilles in the cn index.
  252. * We check first to see if this is a simple ordered index - user specified an
  253. * ordering matching rule compatible with the attribute syntax, and there is
  254. * a compare function. If not, we assume it is a RULE index definition.
  255. */
  256. if(0 == slapi_entry_attr_find(e, "nsMatchingRule", &attr)){
  257. char** official_rules;
  258. size_t k = 0;
  259. /* Get a count and allocate an array for the official matching rules */
  260. slapi_attr_get_numvalues(attr, &mr_count);
  261. official_rules = (char**)slapi_ch_malloc ((mr_count + 1) * sizeof (char*));
  262. for (j = slapi_attr_first_value(attr, &sval); j != -1;j = slapi_attr_next_value(attr, j, &sval)) {
  263. /* Check that index_rules[j] is an official OID */
  264. char* officialOID = NULL;
  265. IFP mrINDEX = NULL;
  266. Slapi_PBlock* pb = NULL;
  267. int do_continue = 0; /* can we skip the RULE parsing stuff? */
  268. attrValue = slapi_value_get_berval(sval);
  269. if (strstr(attrValue->bv_val, INDEX_ATTR_SUBSTRBEGIN)) {
  270. _set_attr_substrlen(INDEX_SUBSTRBEGIN, attrValue->bv_val, &substrlens);
  271. do_continue = 1; /* done with j - next j */
  272. } else if (strstr(attrValue->bv_val, INDEX_ATTR_SUBSTRMIDDLE)) {
  273. _set_attr_substrlen(INDEX_SUBSTRMIDDLE, attrValue->bv_val, &substrlens);
  274. do_continue = 1; /* done with j - next j */
  275. } else if (strstr(attrValue->bv_val, INDEX_ATTR_SUBSTREND)) {
  276. _set_attr_substrlen(INDEX_SUBSTREND, attrValue->bv_val, &substrlens);
  277. do_continue = 1; /* done with j - next j */
  278. /* check if this is a simple ordering specification
  279. for an attribute that has no ordering matching rule */
  280. } else if (slapi_matchingrule_is_ordering(attrValue->bv_val, attrsyntax_oid) &&
  281. slapi_matchingrule_can_use_compare_fn(attrValue->bv_val) &&
  282. !a->ai_sattr.a_mr_ord_plugin) { /* no ordering for this attribute */
  283. need_compare_fn = 1; /* get compare func for this attr */
  284. do_continue = 1; /* done with j - next j */
  285. }
  286. if (do_continue) {
  287. continue; /* done with index_rules[j] */
  288. }
  289. /* must be a RULE specification */
  290. pb = slapi_pblock_new();
  291. /*
  292. * next check if this is a RULE type index
  293. * try to actually create an indexer and see if the indexer
  294. * actually has a regular INDEX_FN or an INDEX_SV_FN
  295. */
  296. if (!slapi_pblock_set (pb, SLAPI_PLUGIN_MR_OID, attrValue->bv_val) &&
  297. !slapi_pblock_set (pb, SLAPI_PLUGIN_MR_TYPE, a->ai_type) &&
  298. !slapi_mr_indexer_create (pb) &&
  299. ((!slapi_pblock_get (pb, SLAPI_PLUGIN_MR_INDEX_FN, &mrINDEX) &&
  300. mrINDEX != NULL) ||
  301. (!slapi_pblock_get (pb, SLAPI_PLUGIN_MR_INDEX_SV_FN, &mrINDEX) &&
  302. mrINDEX != NULL)) &&
  303. !slapi_pblock_get (pb, SLAPI_PLUGIN_MR_OID, &officialOID) &&
  304. officialOID != NULL) {
  305. if (!strcasecmp (attrValue->bv_val, officialOID)) {
  306. official_rules[k++] = slapi_ch_strdup (officialOID);
  307. } else {
  308. char* preamble = slapi_ch_smprintf("%s: line %d", fname, lineno);
  309. LDAPDebug (LDAP_DEBUG_ANY, "%s: use \"%s\" instead of \"%s\" (ignored)\n",
  310. preamble, officialOID, attrValue->bv_val);
  311. slapi_ch_free((void**)&preamble);
  312. }
  313. } else { /* we don't know what this is */
  314. LDAPDebug (LDAP_DEBUG_ANY, "%s: line %d: "
  315. "unknown or invalid matching rule \"%s\" in index configuration (ignored)\n",
  316. fname, lineno, attrValue->bv_val);
  317. }
  318. { /*
  319. * It would improve speed to save the indexer, for future use.
  320. * But, for simplicity, we destroy it now:
  321. */
  322. IFP mrDESTROY = NULL;
  323. if (!slapi_pblock_get (pb, SLAPI_PLUGIN_DESTROY_FN, &mrDESTROY) &&
  324. mrDESTROY != NULL) {
  325. mrDESTROY (pb);
  326. }
  327. }
  328. slapi_pblock_destroy (pb);
  329. }
  330. official_rules[k] = NULL;
  331. a->ai_substr_lens = substrlens;
  332. if (k > 0) {
  333. a->ai_index_rules = official_rules;
  334. a->ai_indexmask |= INDEX_RULES;
  335. } else {
  336. slapi_ch_free((void**)&official_rules);
  337. }
  338. }
  339. /* initialize the IDL code's private data */
  340. return_value = idl_init_private(be, a);
  341. if (0 != return_value) {
  342. /* fatal error, exit */
  343. LDAPDebug(LDAP_DEBUG_ANY,"%s: line %d:Fatal Error: Failed to initialize attribute structure\n",
  344. fname, lineno, 0);
  345. exit( 1 );
  346. }
  347. /* if user didn't specify an ordering rule in the index config,
  348. see if the schema def for the attr defines one */
  349. if (!need_compare_fn && a->ai_sattr.a_mr_ord_plugin) {
  350. need_compare_fn = 1;
  351. }
  352. if (need_compare_fn) {
  353. int rc = attr_get_value_cmp_fn( &a->ai_sattr, &a->ai_key_cmp_fn );
  354. if (rc != LDAP_SUCCESS) {
  355. LDAPDebug(LDAP_DEBUG_ANY,
  356. "The attribute [%s] does not have a valid ORDERING matching rule - error %d:s\n",
  357. a->ai_type, rc, ldap_err2string(rc));
  358. a->ai_key_cmp_fn = NULL;
  359. }
  360. }
  361. if ( avl_insert( &inst->inst_attrs, a, ainfo_cmp, ainfo_dup ) != 0 ) {
  362. /* duplicate - existing version updated */
  363. attrinfo_delete(&a);
  364. }
  365. }
  366. /*
  367. * Function that creates a new attrinfo structure and
  368. * inserts it into the avl tree. This is used by code
  369. * that wants to store attribute-level configuration data
  370. * e.g. attribute encryption, but where the attr_info
  371. * structure doesn't exist because the attribute in question
  372. * is not indexed.
  373. */
  374. void
  375. attr_create_empty(backend *be,char *type,struct attrinfo **ai)
  376. {
  377. ldbm_instance *inst = (ldbm_instance *) be->be_instance_info;
  378. struct attrinfo *a = attrinfo_new();
  379. slapi_attr_init(&a->ai_sattr, type);
  380. a->ai_type = slapi_ch_strdup(type);
  381. if ( avl_insert( &inst->inst_attrs, a, ainfo_cmp, ainfo_dup ) != 0 ) {
  382. /* duplicate - existing version updated */
  383. attrinfo_delete(&a);
  384. ainfo_get(be,type,&a);
  385. }
  386. *ai = a;
  387. }
  388. /* Code for computed attributes */
  389. extern char* hassubordinates;
  390. extern char* numsubordinates;
  391. static int
  392. ldbm_compute_evaluator(computed_attr_context *c,char* type,Slapi_Entry *e,slapi_compute_output_t outputfn)
  393. {
  394. int rc = 0;
  395. if ( strcasecmp (type, numsubordinates ) == 0)
  396. {
  397. Slapi_Attr *read_attr = NULL;
  398. /* Check to see whether this attribute is already present in the entry */
  399. if (0 != slapi_entry_attr_find( e, numsubordinates, &read_attr ))
  400. {
  401. /* If not, we return it as zero */
  402. Slapi_Attr our_attr;
  403. slapi_attr_init(&our_attr, numsubordinates);
  404. our_attr.a_flags = SLAPI_ATTR_FLAG_OPATTR;
  405. valueset_add_string(&our_attr, &our_attr.a_present_values,"0",CSN_TYPE_UNKNOWN,NULL);
  406. rc = (*outputfn) (c, &our_attr, e);
  407. attr_done(&our_attr);
  408. return (rc);
  409. }
  410. }
  411. if ( strcasecmp (type, hassubordinates ) == 0)
  412. {
  413. Slapi_Attr *read_attr = NULL;
  414. Slapi_Attr our_attr;
  415. slapi_attr_init(&our_attr, hassubordinates);
  416. our_attr.a_flags = SLAPI_ATTR_FLAG_OPATTR;
  417. /* This attribute is always computed */
  418. /* Check to see whether the subordinate count attribute is already present in the entry */
  419. rc = slapi_entry_attr_find( e, numsubordinates, &read_attr );
  420. if ( (0 != rc) || slapi_entry_attr_hasvalue(e,numsubordinates,"0") ) {
  421. /* If not, or present and zero, we return FALSE, otherwise TRUE */
  422. valueset_add_string(&our_attr, &our_attr.a_present_values,"FALSE",CSN_TYPE_UNKNOWN,NULL);
  423. } else {
  424. valueset_add_string(&our_attr, &our_attr.a_present_values,"TRUE",CSN_TYPE_UNKNOWN,NULL);
  425. }
  426. rc = (*outputfn) (c, &our_attr, e);
  427. attr_done(&our_attr);
  428. return (rc);
  429. }
  430. return -1; /* I see no ships */
  431. }
  432. /* What are we doing ?
  433. The back-end can't search properly for the hasSubordinates and
  434. numSubordinates attributes. The reason being that they're not
  435. always stored on entries, so filter test fails to do the correct thing.
  436. However, it is possible to rewrite a given search to one
  437. which will work, given that numSubordinates is present when non-zero,
  438. and we maintain a presence index for numSubordinates.
  439. */
  440. /* Searches we rewrite here :
  441. substrings of the form
  442. (hassubordinates=TRUE) to (&(numsubordinates=*)(numsubordinates>=1)) [indexed]
  443. (hassubordinates=FALSE) to (&(objectclass=*)(!(numsubordinates=*))) [not indexed]
  444. (hassubordinates=*) to (objectclass=*) [not indexed]
  445. (numsubordinates=*) to (objectclass=*) [not indexed]
  446. (numsubordinates=x) to (&(numsubordinates=*)(numsubordinates=x)) [indexed]
  447. (numsubordinates>=x) to (&(numsubordinates=*)(numsubordinates>=x)) [indexed where X > 0]
  448. (numsubordinates<=x) to (&(numsubordinates=*)(numsubordinates<=x)) [indexed]
  449. anything else involving numsubordinates and hassubordinates we flag as unwilling to perform
  450. */
  451. /* Before calling this function, you must free all the parts
  452. which will be overwritten, this function dosn't know
  453. how to do that */
  454. static int replace_filter(Slapi_Filter *f, char *s)
  455. {
  456. Slapi_Filter *newf = NULL;
  457. Slapi_Filter *temp = NULL;
  458. /* LP: Fix for defect 515161. Crash on AIX
  459. * slapi_str2filter is a nasty function that mangle whatever gets passed in.
  460. * AIX crashes on altering the literal string.
  461. * So we need to allocate the string and then free it.
  462. */
  463. char *buf = slapi_ch_strdup(s);
  464. newf = slapi_str2filter(buf);
  465. slapi_ch_free((void **)&buf);
  466. if (NULL == newf) {
  467. return -1;
  468. }
  469. /* Now take the parts of newf and put them in f */
  470. /* An easy way to do this is to preserve the "next" ptr */
  471. temp = f->f_next;
  472. *f = *newf;
  473. f->f_next = temp;
  474. /* Free the new filter husk */
  475. slapi_ch_free((void**)&newf);
  476. return 0;
  477. }
  478. static void find_our_friends(char *s, int *has, int *num)
  479. {
  480. *has = (0 == strcasecmp(s,"hassubordinates"));
  481. if (!(*has)) {
  482. *num = (0 == strcasecmp(s,LDBM_NUMSUBORDINATES_STR));
  483. }
  484. }
  485. /* Free the parts of a filter we're about to overwrite */
  486. void free_the_filter_bits(Slapi_Filter *f)
  487. {
  488. /* We need to free: */
  489. switch ( f->f_choice ) {
  490. case LDAP_FILTER_EQUALITY:
  491. case LDAP_FILTER_GE:
  492. case LDAP_FILTER_LE:
  493. case LDAP_FILTER_APPROX:
  494. ava_done( &f->f_ava );
  495. break;
  496. case LDAP_FILTER_PRESENT:
  497. if ( f->f_type != NULL ) {
  498. slapi_ch_free( (void**)&(f->f_type) );
  499. }
  500. break;
  501. default:
  502. break;
  503. }
  504. }
  505. static int grok_and_rewrite_filter(Slapi_Filter *f)
  506. {
  507. Slapi_Filter *p = NULL;
  508. int has = 0;
  509. int num = 0;
  510. char *rhs = NULL;
  511. struct berval rhs_berval;
  512. switch ( f->f_choice ) {
  513. case LDAP_FILTER_EQUALITY:
  514. /* Does this involve either of our target attributes ? */
  515. find_our_friends(f->f_ava.ava_type,&has,&num);
  516. if (has || num) {
  517. rhs = f->f_ava.ava_value.bv_val;
  518. if (has) {
  519. if (0 == strcasecmp(rhs,"TRUE")) {
  520. free_the_filter_bits(f);
  521. replace_filter(f,"(&(numsubordinates=*)(numsubordinates>=1))");
  522. } else if (0 == strcasecmp(rhs, "FALSE")) {
  523. free_the_filter_bits(f);
  524. replace_filter(f,"(&(objectclass=*)(!(numsubordinates=*)))");
  525. } else {
  526. return 1; /* Filter we can't rewrite */
  527. }
  528. }
  529. if (num) {
  530. int rhs_number = 0;
  531. rhs_number = atoi(rhs);
  532. if (rhs_number > 0) {
  533. char * theType=f->f_ava.ava_type;
  534. rhs_berval = f->f_ava.ava_value;
  535. replace_filter(f,"(&(numsubordinates=*)(numsubordinates=x))");
  536. /* Now fixup the resulting filter so that x = rhs */
  537. slapi_ch_free((void**)&(f->f_and->f_next->f_ava.ava_value.bv_val));
  538. /*free type also */
  539. slapi_ch_free((void**)&theType);
  540. f->f_and->f_next->f_ava.ava_value = rhs_berval;
  541. } else {
  542. if (rhs_number == 0) {
  543. /* This is the same as hassubordinates=FALSE */
  544. free_the_filter_bits(f);
  545. replace_filter(f,"(&(objectclass=*)(!(numsubordinates=*)))");
  546. } else {
  547. return 1;
  548. }
  549. }
  550. }
  551. return 0;
  552. }
  553. break;
  554. case LDAP_FILTER_GE:
  555. find_our_friends(f->f_ava.ava_type,&has,&num);
  556. if (has) {
  557. return 1; /* Makes little sense for this attribute */
  558. }
  559. if (num) {
  560. int rhs_num = 0;
  561. rhs = f->f_ava.ava_value.bv_val;
  562. /* is the value zero ? */
  563. rhs_num = atoi(rhs);
  564. if (0 == rhs_num) {
  565. /* If so, rewrite to same as numsubordinates=* */
  566. free_the_filter_bits(f);
  567. replace_filter(f,"(objectclass=*)");
  568. } else {
  569. /* Rewrite to present and GE the rhs */
  570. char * theType=f->f_ava.ava_type;
  571. rhs_berval = f->f_ava.ava_value;
  572. replace_filter(f,"(&(numsubordinates=*)(numsubordinates>=x))");
  573. /* Now fixup the resulting filter so that x = rhs */
  574. slapi_ch_free((void**)&(f->f_and->f_next->f_ava.ava_value.bv_val));
  575. /*free type also */
  576. slapi_ch_free((void**)&theType);
  577. f->f_and->f_next->f_ava.ava_value = rhs_berval;
  578. }
  579. return 0;
  580. }
  581. break;
  582. case LDAP_FILTER_LE:
  583. find_our_friends(f->f_ava.ava_type,&has,&num);
  584. if (has) {
  585. return 1; /* Makes little sense for this attribute */
  586. }
  587. if (num) {
  588. /* One could imagine doing this one, but it's quite hard */
  589. return 1;
  590. }
  591. break;
  592. case LDAP_FILTER_APPROX:
  593. find_our_friends(f->f_ava.ava_type,&has,&num);
  594. if (has || num) {
  595. /* Not allowed */
  596. return 1;
  597. }
  598. break;
  599. case LDAP_FILTER_SUBSTRINGS:
  600. find_our_friends(f->f_sub_type,&has,&num);
  601. if (has || num) {
  602. /* Not allowed */
  603. return 1;
  604. }
  605. break;
  606. case LDAP_FILTER_PRESENT:
  607. find_our_friends(f->f_type,&has,&num);
  608. if (has || num) {
  609. /* we rewrite this search to (objectclass=*) */
  610. slapi_ch_free((void**)&(f->f_type));
  611. f->f_type = slapi_ch_strdup("objectclass");
  612. return 0;
  613. } /* We already weeded out the special search we use use in the console */
  614. break;
  615. case LDAP_FILTER_AND:
  616. case LDAP_FILTER_OR:
  617. case LDAP_FILTER_NOT:
  618. for ( p = f->f_list; p != NULL; p = p->f_next ) {
  619. grok_and_rewrite_filter( p );
  620. }
  621. break;
  622. default:
  623. return -1; /* Bad, might be an extended filter or something */
  624. }
  625. return -1;
  626. }
  627. static int
  628. ldbm_compute_rewriter(Slapi_PBlock *pb)
  629. {
  630. int rc = -1;
  631. char *fstr= NULL;
  632. /*
  633. * We need to look at the filter and see whether it might contain
  634. * numSubordinates or hasSubordinates. We want to do a quick check
  635. * before we look thoroughly.
  636. */
  637. slapi_pblock_get( pb, SLAPI_SEARCH_STRFILTER, &fstr );
  638. if ( NULL != fstr ) {
  639. if (PL_strcasestr(fstr, "subordinates")) {
  640. Slapi_Filter *f = NULL;
  641. /* Look for special filters we want to leave alone */
  642. if (0 == strcasecmp(fstr, "(&(numsubordinates=*)(numsubordinates>=1))" )) {
  643. ; /* Do nothing, this one works OK */
  644. } else {
  645. /* So let's grok the filter in detail and try to rewrite it */
  646. slapi_pblock_get( pb, SLAPI_SEARCH_FILTER, &f );
  647. rc = grok_and_rewrite_filter(f);
  648. if (0 == rc) {
  649. /* he rewrote it ! fixup the string version */
  650. /* slapi_pblock_set( pb, SLAPI_SEARCH_STRFILTER, newfstr ); */
  651. }
  652. }
  653. }
  654. }
  655. return rc;
  656. }
  657. int ldbm_compute_init()
  658. {
  659. int ret = 0;
  660. ret = slapi_compute_add_evaluator(ldbm_compute_evaluator);
  661. if (0 == ret) {
  662. ret = slapi_compute_add_search_rewriter(ldbm_compute_rewriter);
  663. }
  664. return ret;
  665. }