ldbm_attr.c 21 KB

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