vlv_srch.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. /* vlv_srch.c */
  42. #include "back-ldbm.h"
  43. #include "vlv_srch.h"
  44. /* Attributes for vlvSearch */
  45. char* const type_vlvName = "cn";
  46. char* const type_vlvBase = "vlvBase";
  47. char* const type_vlvScope = "vlvScope";
  48. char* const type_vlvFilter = "vlvFilter";
  49. /* Attributes for vlvIndex */
  50. char* const type_vlvSort = "vlvSort";
  51. char* const type_vlvFilename = "vlvFilename";
  52. char* const type_vlvEnabled = "vlvEnabled";
  53. char* const type_vlvUses = "vlvUses";
  54. static const char *file_prefix= "vlv#"; /* '#' used to avoid collision with real attributes */
  55. static const char *file_suffix= LDBM_FILENAME_SUFFIX;
  56. static int vlvIndex_createfilename(struct vlvIndex* pIndex, char **ppc);
  57. static int vlvIndex_equal(const struct vlvIndex* p1, const sort_spec* sort_control);
  58. static void vlvIndex_checkforindex(struct vlvIndex* p, backend *be);
  59. /*
  60. * Create a new vlvSearch object
  61. */
  62. struct vlvSearch*
  63. vlvSearch_new()
  64. {
  65. struct vlvSearch* p = (struct vlvSearch*)slapi_ch_calloc(1,sizeof(struct vlvSearch));
  66. if(p!=NULL)
  67. {
  68. p->vlv_e= NULL;
  69. p->vlv_dn= NULL;
  70. p->vlv_name= NULL;
  71. p->vlv_base= NULL;
  72. p->vlv_scope= LDAP_SCOPE_BASE;
  73. p->vlv_filter= NULL;
  74. p->vlv_slapifilter= NULL;
  75. p->vlv_index= NULL;
  76. p->vlv_next= NULL;
  77. }
  78. return p;
  79. }
  80. /*
  81. * Trim spaces off the end of the string
  82. */
  83. static void
  84. trimspaces(char *s)
  85. {
  86. if (s) {
  87. PRUint32 i= strlen(s) - 1;
  88. while(i > 0 && isascii(s[i]) && isspace(s[i]))
  89. {
  90. s[i]= '\0';
  91. i--;
  92. }
  93. }
  94. }
  95. /*
  96. * Re-Initialise a vlvSearch object
  97. */
  98. void
  99. vlvSearch_reinit(struct vlvSearch* p, const struct backentry *base)
  100. {
  101. if (p->vlv_initialized) {
  102. return; /* no work to do */
  103. }
  104. if (LDAP_SCOPE_ONELEVEL != p->vlv_scope) {
  105. /* Only kind we re-init is onelevel searches */
  106. return;
  107. }
  108. /* Now down to work */
  109. if (NULL != p->vlv_slapifilter) {
  110. slapi_filter_free(p->vlv_slapifilter,1);
  111. }
  112. p->vlv_slapifilter= slapi_str2filter( p->vlv_filter );
  113. filter_normalize(p->vlv_slapifilter);
  114. /* make (&(parentid=idofbase)(|(originalfilter)(objectclass=referral))) */
  115. {
  116. Slapi_Filter *fid2kids= NULL;
  117. Slapi_Filter *focref= NULL;
  118. Slapi_Filter *fand= NULL;
  119. Slapi_Filter *forr= NULL;
  120. p->vlv_slapifilter= create_onelevel_filter(p->vlv_slapifilter, base, 0 /* managedsait */, &fid2kids, &focref, &fand, &forr);
  121. }
  122. }
  123. /*
  124. * Initialise a vlvSearch object
  125. */
  126. void
  127. vlvSearch_init(struct vlvSearch* p, Slapi_PBlock *pb, const Slapi_Entry *e, ldbm_instance *inst)
  128. {
  129. /* VLV specification */
  130. /* Need to copy the entry here because this one is in the cache,
  131. * not forever ! */
  132. p->vlv_e= slapi_entry_dup( e );
  133. p->vlv_dn= slapi_sdn_dup(slapi_entry_get_sdn_const(e));
  134. p->vlv_name= slapi_entry_attr_get_charptr(e,type_vlvName);
  135. p->vlv_base= slapi_sdn_new_dn_passin(slapi_entry_attr_get_charptr(e,type_vlvBase));
  136. p->vlv_scope= slapi_entry_attr_get_int(e,type_vlvScope);
  137. p->vlv_filter= slapi_entry_attr_get_charptr(e,type_vlvFilter);
  138. p->vlv_initialized = 1;
  139. /* JCM: Should perform some validation and report errors to the error log */
  140. /* JCM: Add brackets around the filter if none are there... */
  141. trimspaces(p->vlv_name);
  142. trimspaces(p->vlv_filter);
  143. if(strlen(p->vlv_filter)>0)
  144. {
  145. /* Convert the textual filter, into a Slapi_Filter structure */
  146. p->vlv_slapifilter= slapi_str2filter( p->vlv_filter );
  147. filter_normalize(p->vlv_slapifilter);
  148. }
  149. /* JCM: Really should convert the slapifilter into a string and use that. */
  150. /* Convert the filter based on the scope of the search */
  151. switch(p->vlv_scope)
  152. {
  153. case LDAP_SCOPE_BASE:
  154. /* Don't need to alter the filter */
  155. break;
  156. case LDAP_SCOPE_ONELEVEL:
  157. {
  158. /*
  159. * Get the base object for the search.
  160. * The entry "" will never be contained in the database,
  161. * so treat it as a special case.
  162. */
  163. struct backentry *e= NULL;
  164. if ( !slapi_sdn_isempty(p->vlv_base)) {
  165. Slapi_Backend *oldbe = NULL;
  166. entry_address addr;
  167. back_txn txn = {NULL};
  168. /* switch context to the target backend */
  169. slapi_pblock_get(pb, SLAPI_BACKEND, &oldbe);
  170. slapi_pblock_set(pb, SLAPI_BACKEND, inst->inst_be);
  171. slapi_pblock_set(pb, SLAPI_PLUGIN, inst->inst_be->be_database);
  172. slapi_pblock_get(pb, SLAPI_TXN, &txn.back_txn_txn);
  173. addr.sdn = p->vlv_base;
  174. addr.uniqueid = NULL;
  175. e = find_entry( pb, inst->inst_be, &addr, &txn );
  176. /* Check to see if the entry is absent. If it is, mark this search
  177. * as not initialized */
  178. if (NULL == e) {
  179. p->vlv_initialized = 0;
  180. /* We crash on anyhow, and rely on the fact that the filter
  181. * we create is bogus to prevent chaos */
  182. }
  183. /* switch context back to the DSE backend */
  184. slapi_pblock_set(pb, SLAPI_BACKEND, oldbe);
  185. slapi_pblock_set(pb, SLAPI_PLUGIN, oldbe->be_database);
  186. }
  187. /* make (&(parentid=idofbase)(|(originalfilter)(objectclass=referral))) */
  188. {
  189. Slapi_Filter *fid2kids= NULL;
  190. Slapi_Filter *focref= NULL;
  191. Slapi_Filter *fand= NULL;
  192. Slapi_Filter *forr= NULL;
  193. p->vlv_slapifilter= create_onelevel_filter(p->vlv_slapifilter, e, 0 /* managedsait */, &fid2kids, &focref, &fand, &forr);
  194. /* jcm: fid2kids, focref, fand, and forr get freed when we free p->vlv_slapifilter */
  195. CACHE_RETURN(&inst->inst_cache,&e);
  196. }
  197. }
  198. break;
  199. case LDAP_SCOPE_SUBTREE:
  200. {
  201. /* make (|(originalfilter)(objectclass=referral))) */
  202. /* No need for scope-filter since we apply a scope test before the filter test */
  203. Slapi_Filter *focref= NULL;
  204. Slapi_Filter *forr= NULL;
  205. p->vlv_slapifilter= create_subtree_filter(p->vlv_slapifilter, 0 /* managedsait */, &focref, &forr);
  206. /* jcm: focref and forr get freed when we free p->vlv_slapifilter */
  207. }
  208. break;
  209. }
  210. }
  211. /*
  212. * Destroy an existing vlvSearch object
  213. */
  214. void
  215. vlvSearch_delete(struct vlvSearch** ppvs)
  216. {
  217. if(ppvs!=NULL && *ppvs!=NULL)
  218. {
  219. struct vlvIndex *pi, *ni;
  220. slapi_sdn_free(&((*ppvs)->vlv_dn));
  221. slapi_ch_free((void**)&((*ppvs)->vlv_name));
  222. slapi_sdn_free(&((*ppvs)->vlv_base));
  223. slapi_ch_free((void**)&((*ppvs)->vlv_filter));
  224. slapi_filter_free((*ppvs)->vlv_slapifilter,1);
  225. for(pi= (*ppvs)->vlv_index;pi!=NULL;)
  226. {
  227. ni= pi->vlv_next;
  228. if(pi->vlv_be != NULL) {
  229. vlvIndex_go_offline(pi,pi->vlv_be);
  230. }
  231. vlvIndex_delete(&pi);
  232. pi= ni;
  233. }
  234. slapi_ch_free((void**)ppvs);
  235. *ppvs= NULL;
  236. }
  237. }
  238. /*
  239. * Add a search to a list.
  240. *
  241. * We add it to the end of the list because there could
  242. * be other threads traversing the list at this time.
  243. */
  244. void
  245. vlvSearch_addtolist(struct vlvSearch* p, struct vlvSearch** pplist)
  246. {
  247. if(pplist!=NULL && p!=NULL)
  248. {
  249. p->vlv_next= NULL;
  250. if(*pplist==NULL)
  251. {
  252. *pplist= p;
  253. }
  254. else
  255. {
  256. struct vlvSearch* last= *pplist;
  257. for(;last->vlv_next!=NULL;last=last->vlv_next);
  258. last->vlv_next= p;
  259. }
  260. }
  261. }
  262. /*
  263. * Compare two VLV Searches to see if they're the same, based on their VLV Search specification.
  264. */
  265. static struct vlvIndex *
  266. vlvSearch_equal(const struct vlvSearch* p1, const Slapi_DN *base, int scope, const char *filter, const sort_spec* sort_control)
  267. {
  268. struct vlvIndex *pi= NULL;
  269. int r= (slapi_sdn_compare(p1->vlv_base,base)==0);
  270. if(r) r= (p1->vlv_scope==scope);
  271. if(r) r= (strcasecmp(p1->vlv_filter,filter)==0);
  272. if(r)
  273. {
  274. pi= p1->vlv_index;
  275. r= 0;
  276. for(;!r && pi!=NULL;)
  277. {
  278. r= vlvIndex_equal(pi, sort_control);
  279. if(!r)
  280. {
  281. pi= pi->vlv_next;
  282. }
  283. }
  284. }
  285. return pi;
  286. }
  287. /*
  288. * Find an enabled VLV Search in a list which matches the
  289. * description provided in "base, scope, filter, sort_control"
  290. */
  291. struct vlvIndex*
  292. vlvSearch_findenabled(backend *be,struct vlvSearch* plist, const Slapi_DN *base, int scope, const char *filter, const sort_spec* sort_control)
  293. {
  294. struct vlvSearch *t= plist;
  295. struct vlvIndex *pi= NULL;
  296. for(; (t!=NULL) && (pi == NULL); t= t->vlv_next)
  297. {
  298. pi= vlvSearch_equal(t,base,scope,filter,sort_control);
  299. if(pi!=NULL)
  300. {
  301. if(!vlvIndex_enabled(pi))
  302. {
  303. /*
  304. * A VLV Spec which matched the search criteria was found.
  305. * But it hasn't been enabled yet. Check to see if the
  306. * index is there. But, only check once every 60 seconds.
  307. */
  308. time_t curtime = current_time();
  309. if(curtime>pi->vlv_lastchecked+60)
  310. {
  311. vlvIndex_checkforindex(pi, be);
  312. pi->vlv_lastchecked= current_time();
  313. }
  314. }
  315. if(!vlvIndex_enabled(pi))
  316. {
  317. pi= NULL;
  318. }
  319. }
  320. }
  321. return pi;
  322. }
  323. /*
  324. * Find a VLV Search in a list which matches the name
  325. */
  326. struct vlvIndex*
  327. vlvSearch_findname(const struct vlvSearch* plist, const char *name)
  328. {
  329. const struct vlvSearch* t= plist;
  330. for(; t!=NULL ; t= t->vlv_next)
  331. {
  332. struct vlvIndex *pi= t->vlv_index;
  333. for(;pi!=NULL;pi= pi->vlv_next)
  334. {
  335. if(strcasecmp(pi->vlv_name,name)==0)
  336. {
  337. return pi;
  338. }
  339. }
  340. }
  341. return NULL;
  342. }
  343. /*
  344. * Find a VLV Search in a list which matches the index name
  345. */
  346. struct vlvIndex*
  347. vlvSearch_findindexname(const struct vlvSearch* plist, const char *name)
  348. {
  349. const struct vlvSearch* t= plist;
  350. if (NULL == name) {
  351. return NULL;
  352. }
  353. for(; t!=NULL ; t= t->vlv_next)
  354. {
  355. struct vlvIndex *pi= t->vlv_index;
  356. for(;pi!=NULL;pi= pi->vlv_next)
  357. {
  358. if(strcasecmp(pi->vlv_attrinfo->ai_type,name)==0)
  359. {
  360. return pi;
  361. }
  362. }
  363. }
  364. return NULL;
  365. }
  366. /*
  367. * Get a list of VLV Index names.
  368. * The returned pointer must be freed with slapi_ch_free
  369. */
  370. char *
  371. vlvSearch_getnames(const struct vlvSearch* plist)
  372. {
  373. /* Work out how long the string will be */
  374. char *text;
  375. int length= 5; /* enough to hold 'none' */
  376. const struct vlvSearch* t= plist;
  377. for(; t!=NULL ; t= t->vlv_next)
  378. {
  379. struct vlvIndex *pi= t->vlv_index;
  380. for(;pi!=NULL;pi= pi->vlv_next)
  381. {
  382. length+= strlen(pi->vlv_name) + 4;
  383. }
  384. }
  385. /* Build a comma delimited list of Index names */
  386. text= slapi_ch_malloc(length);
  387. if(length==5)
  388. {
  389. strcpy(text,"none");
  390. }
  391. else
  392. {
  393. text[0]= '\0';
  394. t= plist;
  395. for(; t!=NULL ; t= t->vlv_next)
  396. {
  397. struct vlvIndex *pi= t->vlv_index;
  398. for(;pi!=NULL;pi= pi->vlv_next)
  399. {
  400. sprintf(text + strlen(text),"'%s', ",pi->vlv_name);
  401. }
  402. }
  403. }
  404. return text;
  405. }
  406. /*
  407. * Find a VLV Search in a list, based on the DN.
  408. */
  409. struct vlvSearch*
  410. vlvSearch_finddn(const struct vlvSearch* plist, const Slapi_DN *dn)
  411. {
  412. const struct vlvSearch* curr= plist;
  413. for(; curr!=NULL && slapi_sdn_compare(curr->vlv_dn,dn)!=0; curr= curr->vlv_next);
  414. return (struct vlvSearch*)curr;
  415. }
  416. /*
  417. * Remove a VLV Search from a list, based on the DN.
  418. */
  419. void
  420. vlvSearch_removefromlist(struct vlvSearch** pplist, const Slapi_DN *dn)
  421. {
  422. int done= 0;
  423. struct vlvSearch* prev= NULL;
  424. struct vlvSearch* curr= *pplist;
  425. while(curr!=NULL && !done)
  426. {
  427. if(slapi_sdn_compare(curr->vlv_dn,dn)==0)
  428. {
  429. if(curr==*pplist)
  430. {
  431. *pplist= curr->vlv_next;
  432. }
  433. else
  434. {
  435. if (prev) prev->vlv_next= curr->vlv_next;
  436. }
  437. done= 1;
  438. }
  439. else
  440. {
  441. prev= curr;
  442. curr= curr->vlv_next;
  443. }
  444. }
  445. }
  446. /*
  447. * Access Control Check to see if the client is allowed to use this VLV Search.
  448. */
  449. int
  450. vlvSearch_accessallowed(struct vlvSearch *p, Slapi_PBlock *pb)
  451. {
  452. char *attrs[2] = { NULL, NULL};
  453. attrs[0] = type_vlvName;
  454. return (plugin_call_acl_plugin ( pb, (Slapi_Entry*)p->vlv_e, attrs, NULL,
  455. SLAPI_ACL_READ, ACLPLUGIN_ACCESS_READ_ON_VLV, NULL ) );
  456. }
  457. const Slapi_DN *vlvSearch_getBase(struct vlvSearch* p)
  458. {
  459. return p->vlv_base;
  460. }
  461. int vlvSearch_getScope(struct vlvSearch* p)
  462. {
  463. return p->vlv_scope;
  464. }
  465. Slapi_Filter *vlvSearch_getFilter(struct vlvSearch* p)
  466. {
  467. return p->vlv_slapifilter;
  468. }
  469. int vlvSearch_isVlvSearchEntry(Slapi_Entry *e)
  470. {
  471. return slapi_entry_attr_hasvalue(e, "objectclass", "vlvsearch");
  472. }
  473. void vlvSearch_addIndex(struct vlvSearch *pSearch, struct vlvIndex *pIndex)
  474. {
  475. pIndex->vlv_next= NULL;
  476. if(pSearch->vlv_index==NULL)
  477. {
  478. pSearch->vlv_index= pIndex;
  479. }
  480. else
  481. {
  482. struct vlvIndex* last= pSearch->vlv_index;
  483. for(;last->vlv_next!=NULL;last=last->vlv_next);
  484. last->vlv_next= pIndex;
  485. }
  486. }
  487. /* ============================================================================================== */
  488. /*
  489. * Create a new vlvIndex object
  490. */
  491. struct vlvIndex*
  492. vlvIndex_new()
  493. {
  494. struct vlvIndex* p = (struct vlvIndex*)slapi_ch_calloc(1,sizeof(struct vlvIndex));
  495. if(p!=NULL)
  496. {
  497. p->vlv_sortspec= NULL;
  498. p->vlv_attrinfo= attrinfo_new();
  499. p->vlv_sortkey= NULL;
  500. p->vlv_filename= NULL;
  501. p->vlv_mrpb= NULL;
  502. p->vlv_indexlength_lock= PR_NewLock();
  503. p->vlv_indexlength_cached= 0;
  504. p->vlv_indexlength= 0;
  505. p->vlv_online = 1;
  506. p->vlv_enabled = 0;
  507. p->vlv_lastchecked= 0;
  508. p->vlv_uses= 0;
  509. p->vlv_search= NULL;
  510. p->vlv_next= NULL;
  511. }
  512. return p;
  513. }
  514. /*
  515. * Destroy an existing vlvIndex object
  516. */
  517. void
  518. vlvIndex_delete(struct vlvIndex** ppvs)
  519. {
  520. if(ppvs!=NULL && *ppvs!=NULL)
  521. {
  522. slapi_ch_free((void**)&((*ppvs)->vlv_sortspec));
  523. {
  524. int n;
  525. for(n=0;(*ppvs)->vlv_sortkey[n]!=NULL;n++)
  526. {
  527. if((*ppvs)->vlv_mrpb[n] != NULL) {
  528. destroy_matchrule_indexer((*ppvs)->vlv_mrpb[n]);
  529. slapi_pblock_destroy((*ppvs)->vlv_mrpb[n]);
  530. }
  531. }
  532. }
  533. internal_ldap_free_sort_keylist((*ppvs)->vlv_sortkey);
  534. attrinfo_delete(&((*ppvs)->vlv_attrinfo));
  535. slapi_ch_free((void**)&((*ppvs)->vlv_name));
  536. slapi_ch_free((void**)&((*ppvs)->vlv_filename));
  537. slapi_ch_free((void**)&((*ppvs)->vlv_mrpb));
  538. PR_DestroyLock((*ppvs)->vlv_indexlength_lock);
  539. slapi_ch_free((void**)ppvs);
  540. *ppvs= NULL;
  541. }
  542. }
  543. /*
  544. * Initialise a vlvSearch object
  545. */
  546. void
  547. vlvIndex_init(struct vlvIndex* p, backend *be, struct vlvSearch* pSearch, const Slapi_Entry *e)
  548. {
  549. struct ldbminfo *li = (struct ldbminfo *) be->be_database->plg_private;
  550. char *filename= NULL;
  551. if (NULL == p)
  552. return;
  553. /* JCM: Should perform some validation and report errors to the error log */
  554. /* JCM: Add brackets around the filter if none are there... */
  555. p->vlv_sortspec= slapi_entry_attr_get_charptr(e,type_vlvSort);
  556. trimspaces(p->vlv_sortspec);
  557. p->vlv_name= slapi_entry_attr_get_charptr(e,type_vlvName);
  558. trimspaces(p->vlv_name);
  559. p->vlv_search= pSearch;
  560. /* Convert the textual sort specification into a keylist structure */
  561. internal_ldap_create_sort_keylist(&(p->vlv_sortkey),p->vlv_sortspec);
  562. {
  563. /*
  564. * For each sort attribute find the appropriate syntax plugin,
  565. * and if it has a matching rule, create a matching rule indexer object.
  566. */
  567. int n;
  568. for(n=0;p->vlv_sortkey[n]!=NULL;n++);
  569. p->vlv_mrpb= (Slapi_PBlock**)slapi_ch_calloc(n+1,sizeof(Slapi_PBlock*));
  570. for(n=0;p->vlv_sortkey[n]!=NULL;n++)
  571. {
  572. if(p->vlv_sortkey[n]->sk_matchruleoid!=NULL)
  573. {
  574. create_matchrule_indexer(&p->vlv_mrpb[n],p->vlv_sortkey[n]->sk_matchruleoid,p->vlv_sortkey[n]->sk_attrtype);
  575. }
  576. }
  577. }
  578. /* Create an index filename for the search */
  579. if(vlvIndex_createfilename(p,&filename))
  580. {
  581. p->vlv_filename= slapi_ch_smprintf("%s%s%s",file_prefix,filename,file_suffix);
  582. /* Create an attrinfo structure */
  583. p->vlv_attrinfo->ai_type= slapi_ch_smprintf("%s%s",file_prefix,filename);
  584. p->vlv_attrinfo->ai_indexmask= INDEX_VLV;
  585. /* Check if the index file actually exists */
  586. if(li!=NULL)
  587. {
  588. vlvIndex_checkforindex(p, be);
  589. }
  590. p->vlv_lastchecked= current_time();
  591. }
  592. slapi_ch_free((void**)&filename);
  593. }
  594. /*
  595. * Determine how many {key,data} pairs there are in the VLV Index.
  596. * We only work out the length of the index once, then we cache
  597. * it and maintain it.
  598. */
  599. PRUint32
  600. vlvIndex_get_indexlength(struct vlvIndex* p, DB *db, back_txn *txn)
  601. {
  602. if (NULL == p)
  603. return 0;
  604. if(!p->vlv_indexlength_cached)
  605. {
  606. DBC *dbc = NULL;
  607. DB_TXN *db_txn = NULL;
  608. int err= 0;
  609. if (NULL != txn)
  610. {
  611. db_txn = txn->back_txn_txn;
  612. }
  613. err = db->cursor(db, db_txn, &dbc, 0);
  614. if(err==0)
  615. {
  616. DBT key= {0};
  617. DBT data= {0};
  618. key.flags= DB_DBT_MALLOC;
  619. data.flags= DB_DBT_MALLOC;
  620. err= dbc->c_get(dbc,&key,&data,DB_LAST);
  621. if(err==0)
  622. {
  623. slapi_ch_free(&(key.data));
  624. slapi_ch_free(&(data.data));
  625. err= dbc->c_get(dbc,&key,&data,DB_GET_RECNO);
  626. if(err==0)
  627. {
  628. PR_Lock(p->vlv_indexlength_lock);
  629. p->vlv_indexlength_cached= 1;
  630. p->vlv_indexlength= *((db_recno_t*)data.data);
  631. PR_Unlock(p->vlv_indexlength_lock);
  632. slapi_ch_free(&(data.data));
  633. }
  634. }
  635. dbc->c_close(dbc);
  636. }
  637. else
  638. {
  639. /* couldn't get cursor??? */
  640. }
  641. }
  642. return p->vlv_indexlength;
  643. }
  644. /*
  645. * Increment the index length count.
  646. * We keep track of the index length for efficiency.
  647. */
  648. void
  649. vlvIndex_increment_indexlength(struct vlvIndex* p, DB *db, back_txn *txn)
  650. {
  651. if (NULL == p)
  652. return;
  653. if(p->vlv_indexlength_cached)
  654. {
  655. PR_Lock(p->vlv_indexlength_lock);
  656. p->vlv_indexlength++;
  657. PR_Unlock(p->vlv_indexlength_lock);
  658. }
  659. else
  660. {
  661. p->vlv_indexlength= vlvIndex_get_indexlength(p, db, txn);
  662. }
  663. }
  664. /*
  665. * Decrement the index length count.
  666. * We keep track of the index length for efficiency.
  667. */
  668. void
  669. vlvIndex_decrement_indexlength(struct vlvIndex* p, DB *db, back_txn *txn)
  670. {
  671. if (NULL == p)
  672. return;
  673. if(p->vlv_indexlength_cached)
  674. {
  675. /* jcm: Check for underflow? */
  676. PR_Lock(p->vlv_indexlength_lock);
  677. p->vlv_indexlength--;
  678. PR_Unlock(p->vlv_indexlength_lock);
  679. }
  680. else
  681. {
  682. p->vlv_indexlength= vlvIndex_get_indexlength(p, db, txn);
  683. }
  684. }
  685. /*
  686. * Increment the usage counter
  687. */
  688. void
  689. vlvIndex_incrementUsage(struct vlvIndex* p)
  690. {
  691. if (NULL == p)
  692. return;
  693. p->vlv_uses++;
  694. }
  695. /*
  696. * Get the filename of the index.
  697. */
  698. const char *
  699. vlvIndex_filename(const struct vlvIndex* p)
  700. {
  701. if (NULL == p)
  702. return NULL;
  703. return p->vlv_filename;
  704. }
  705. /*
  706. * Check if the index is available.
  707. */
  708. int vlvIndex_enabled(const struct vlvIndex* p)
  709. {
  710. if (NULL == p)
  711. return 0;
  712. return p->vlv_enabled;
  713. }
  714. int vlvIndex_online(const struct vlvIndex *p)
  715. {
  716. if (NULL == p)
  717. return 0;
  718. return p->vlv_online;
  719. }
  720. void vlvIndex_go_offline(struct vlvIndex *p, backend *be)
  721. {
  722. if (NULL == p)
  723. return;
  724. p->vlv_online = 0;
  725. p->vlv_enabled = 0;
  726. p->vlv_indexlength = 0;
  727. p->vlv_attrinfo->ai_indexmask |= INDEX_OFFLINE;
  728. dblayer_erase_index_file_nolock(be, p->vlv_attrinfo, 1 /* chkpt if not busy */);
  729. }
  730. void vlvIndex_go_online(struct vlvIndex *p, backend *be)
  731. {
  732. if (NULL == p)
  733. return;
  734. p->vlv_attrinfo->ai_indexmask &= ~INDEX_OFFLINE;
  735. p->vlv_online = 1;
  736. vlvIndex_checkforindex(p, be);
  737. }
  738. /*
  739. * Access Control Check to see if the client is allowed to use this VLV Index.
  740. */
  741. int
  742. vlvIndex_accessallowed(struct vlvIndex *p, Slapi_PBlock *pb)
  743. {
  744. if (NULL == p)
  745. return 0;
  746. return vlvSearch_accessallowed(p->vlv_search, pb);
  747. }
  748. const Slapi_DN *vlvIndex_getBase(struct vlvIndex* p)
  749. {
  750. if (NULL == p)
  751. return NULL;
  752. return vlvSearch_getBase(p->vlv_search);
  753. }
  754. int vlvIndex_getScope(struct vlvIndex* p)
  755. {
  756. if (NULL == p)
  757. return 0;
  758. return vlvSearch_getScope(p->vlv_search);
  759. }
  760. Slapi_Filter *vlvIndex_getFilter(struct vlvIndex* p)
  761. {
  762. if (NULL == p)
  763. return NULL;
  764. return vlvSearch_getFilter(p->vlv_search);
  765. }
  766. const char *vlvIndex_getName(struct vlvIndex* p)
  767. {
  768. if (NULL == p)
  769. return NULL;
  770. return p->vlv_name;
  771. }
  772. /*
  773. * JCM: Could also match reverse sense of index and use in reverse.
  774. */
  775. static int
  776. vlvIndex_equal(const struct vlvIndex* p1, const sort_spec* sort_control)
  777. {
  778. int r= 1;
  779. const sort_spec *t1= sort_control;
  780. LDAPsortkey *t2= p1->vlv_sortkey[0];
  781. int n= 1;
  782. for(;t1!=NULL && t2!=NULL && r;t1= t1->next,t2=p1->vlv_sortkey[n],n++)
  783. {
  784. r= (t1->order && t2->sk_reverseorder) || (!t1->order && !t2->sk_reverseorder);
  785. if(r) r= (strcasecmp(t1->type, t2->sk_attrtype)==0);
  786. if(r)
  787. {
  788. if(t1->matchrule==NULL && t2->sk_matchruleoid==NULL)
  789. {
  790. r= 1;
  791. }
  792. else if(t1->matchrule!=NULL && t2->sk_matchruleoid!=NULL)
  793. {
  794. r= (strcasecmp(t1->matchrule, t2->sk_matchruleoid)==0);
  795. }
  796. else
  797. {
  798. r= 0;
  799. }
  800. }
  801. }
  802. if(r) r= (t1==NULL && t2==NULL);
  803. return r;
  804. }
  805. /*
  806. * Check if the index file actually exists,
  807. * and set vlv_enabled appropriately
  808. */
  809. static void
  810. vlvIndex_checkforindex(struct vlvIndex* p, backend *be)
  811. {
  812. DB *db = NULL;
  813. /* if the vlv index is offline (being generated), don't even look */
  814. if (! p->vlv_online)
  815. return;
  816. if (dblayer_get_index_file(be, p->vlv_attrinfo, &db, 0) == 0) {
  817. p->vlv_enabled = 1;
  818. dblayer_release_index_file( be, p->vlv_attrinfo, db );
  819. } else {
  820. p->vlv_enabled = 0;
  821. }
  822. }
  823. int vlvIndex_isVlvIndexEntry(Slapi_Entry *e)
  824. {
  825. return slapi_entry_attr_hasvalue(e, "objectclass", "vlvindex");
  826. }
  827. /*
  828. * Create the filename for the index.
  829. * Extract all the alphanumeric characters from the descriptive name.
  830. * Convert to all lower case.
  831. */
  832. static int
  833. vlvIndex_createfilename(struct vlvIndex* pIndex, char **ppc)
  834. {
  835. int filenameValid= 1;
  836. unsigned int i;
  837. char *p, *filename;
  838. filename= slapi_ch_malloc(strlen(pIndex->vlv_name) + 1);
  839. p= filename;
  840. for(i=0;i<strlen(pIndex->vlv_name);i++)
  841. {
  842. if(isalnum(pIndex->vlv_name[i]))
  843. {
  844. *p= TOLOWER( pIndex->vlv_name[i] );
  845. p++;
  846. }
  847. }
  848. *p= '\0';
  849. if(strlen(filename)==0)
  850. {
  851. LDAPDebug( LDAP_DEBUG_ANY, "Couldn't generate valid filename from Virtual List View Index Name (%s). Need some alphabetical characters.\n", pIndex->vlv_name, 0, 0);
  852. filenameValid= 0;
  853. }
  854. /* JCM: Check if this file clashes with another VLV Index filename */
  855. *ppc= filename;
  856. return filenameValid;
  857. }
  858. int
  859. vlv_isvlv(char *filename)
  860. {
  861. if (0 == strncmp(filename, file_prefix, 4))
  862. return 1;
  863. return 0;
  864. }
  865. void
  866. internal_ldap_free_sort_keylist(LDAPsortkey **sortKeyList)
  867. {
  868. #if defined(USE_OPENLDAP)
  869. ldap_free_sort_keylist((LDAPSortKey **)sortKeyList);
  870. #else
  871. ldap_free_sort_keylist(sortKeyList);
  872. #endif
  873. }
  874. int
  875. internal_ldap_create_sort_keylist(LDAPsortkey ***sortKeyList, const char *string_rep)
  876. {
  877. #if defined(USE_OPENLDAP)
  878. return ldap_create_sort_keylist((LDAPSortKey ***)sortKeyList, (char *)string_rep);
  879. #else
  880. return ldap_create_sort_keylist(sortKeyList, string_rep);
  881. #endif
  882. }