filterentry.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  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. /* filterentry.c - apply a filter to an entry */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #ifndef _WIN32
  46. #include <sys/socket.h>
  47. #endif
  48. #include "slap.h"
  49. static int test_filter_list();
  50. static int test_extensible_filter();
  51. static int vattr_test_filter_list();
  52. static int test_filter_access( Slapi_PBlock *pb, Slapi_Entry*e,
  53. char * attr_type, struct berval *attr_val);
  54. static int slapi_vattr_filter_test_ext_internal( Slapi_PBlock *pb, Slapi_Entry *e,
  55. struct slapi_filter *f, int verify_access, int only_check_access, int *access_check_done);
  56. static char *opt_str = 0;
  57. static int opt = 0;
  58. static int optimise_filter_acl_tests()
  59. {
  60. if(!opt_str)
  61. {
  62. opt_str = getenv( "NS_DS_OPT_FILT_ACL_EVAL" );
  63. if(opt_str)
  64. opt = !strcasecmp(opt_str, "false");
  65. else
  66. opt = 0;
  67. if(!opt_str)
  68. opt_str = "dummy";
  69. }
  70. return opt;
  71. }
  72. /*
  73. * slapi_filter_test - test a filter against a single entry.
  74. * returns 0 filter matched
  75. * -1 filter did not match
  76. * >0 an ldap error code
  77. */
  78. int
  79. slapi_filter_test(
  80. Slapi_PBlock *pb,
  81. Slapi_Entry *e,
  82. struct slapi_filter *f,
  83. int verify_access
  84. )
  85. {
  86. return slapi_filter_test_ext(pb,e,f,verify_access,0);
  87. }
  88. /*
  89. * slapi_filter_test_simple - test without checking access control
  90. *
  91. * returns 0 filter matched
  92. * -1 filter did not match
  93. * >0 an ldap error code
  94. */
  95. int
  96. slapi_filter_test_simple(
  97. Slapi_Entry *e,
  98. struct slapi_filter *f
  99. )
  100. {
  101. return slapi_vattr_filter_test_ext(NULL,e,f,0,0);
  102. }
  103. /*
  104. * slapi_filter_test_ext - full-feature filter test function
  105. *
  106. * returns 0 filter matched
  107. * -1 filter did not match
  108. * >0 an ldap error code
  109. */
  110. int
  111. slapi_filter_test_ext_internal(
  112. Slapi_PBlock *pb,
  113. Slapi_Entry *e,
  114. struct slapi_filter *f,
  115. int verify_access,
  116. int only_check_access,
  117. int *access_check_done
  118. )
  119. {
  120. int rc;
  121. LDAPDebug( LDAP_DEBUG_FILTER, "=> slapi_filter_test_ext\n", 0, 0, 0 );
  122. /*
  123. * RJP: Not sure if this is semantically right, but we have to
  124. * return something if f is NULL. If there is no filter,
  125. * then we say that it did match and return 0.
  126. */
  127. if ( f == NULL) {
  128. return(0);
  129. }
  130. switch ( f->f_choice ) {
  131. case LDAP_FILTER_EQUALITY:
  132. LDAPDebug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
  133. rc = test_ava_filter( pb, e, e->e_attrs, &f->f_ava, LDAP_FILTER_EQUALITY,
  134. verify_access , only_check_access, access_check_done);
  135. break;
  136. case LDAP_FILTER_SUBSTRINGS:
  137. LDAPDebug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
  138. rc = test_substring_filter( pb, e, f, verify_access , only_check_access, access_check_done);
  139. break;
  140. case LDAP_FILTER_GE:
  141. LDAPDebug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
  142. rc = test_ava_filter( pb, e, e->e_attrs, &f->f_ava, LDAP_FILTER_GE,
  143. verify_access , only_check_access, access_check_done);
  144. break;
  145. case LDAP_FILTER_LE:
  146. LDAPDebug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
  147. rc = test_ava_filter( pb, e, e->e_attrs, &f->f_ava, LDAP_FILTER_LE,
  148. verify_access , only_check_access, access_check_done);
  149. break;
  150. case LDAP_FILTER_PRESENT:
  151. LDAPDebug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
  152. rc = test_presence_filter( pb, e, f->f_type, verify_access , only_check_access, access_check_done);
  153. break;
  154. case LDAP_FILTER_APPROX:
  155. LDAPDebug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
  156. rc = test_ava_filter( pb, e, e->e_attrs, &f->f_ava, LDAP_FILTER_APPROX,
  157. verify_access , only_check_access, access_check_done);
  158. break;
  159. case LDAP_FILTER_EXTENDED:
  160. LDAPDebug( LDAP_DEBUG_FILTER, " EXTENDED\n", 0, 0, 0 );
  161. rc = test_extensible_filter( pb, e, &f->f_mr, verify_access , only_check_access, access_check_done);
  162. break;
  163. case LDAP_FILTER_AND:
  164. LDAPDebug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
  165. rc = test_filter_list( pb, e, f->f_and,
  166. LDAP_FILTER_AND , verify_access, only_check_access, access_check_done);
  167. break;
  168. case LDAP_FILTER_OR:
  169. LDAPDebug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
  170. rc = test_filter_list( pb, e, f->f_or,
  171. LDAP_FILTER_OR , verify_access, only_check_access, access_check_done);
  172. break;
  173. case LDAP_FILTER_NOT:
  174. LDAPDebug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
  175. rc = slapi_filter_test_ext_internal( pb, e, f->f_not , verify_access, only_check_access, access_check_done);
  176. if(!(verify_access && only_check_access)) /* dont play with access control return codes */
  177. {
  178. if(verify_access && !rc && !(*access_check_done))
  179. {
  180. /* the filter failed so access control was not checked
  181. * for NOT filters this is significant so we must ensure
  182. * access control is checked
  183. */
  184. /* check access control only */
  185. rc = slapi_filter_test_ext_internal( pb, e, f->f_not , verify_access, -1 /*only_check_access*/, access_check_done);
  186. /* preserve error code if any */
  187. if(!rc)
  188. rc = !rc;
  189. }
  190. else
  191. rc = !rc;
  192. }
  193. break;
  194. default:
  195. LDAPDebug( LDAP_DEBUG_ANY, " unknown filter type 0x%lX\n",
  196. f->f_choice, 0, 0 );
  197. rc = -1;
  198. }
  199. LDAPDebug( LDAP_DEBUG_FILTER, "<= slapi_filter_test %d\n", rc, 0, 0 );
  200. return( rc );
  201. }
  202. int
  203. slapi_filter_test_ext(
  204. Slapi_PBlock *pb,
  205. Slapi_Entry *e,
  206. struct slapi_filter *f,
  207. int verify_access,
  208. int only_check_access
  209. )
  210. {
  211. int rc = 0; /* a no op request succeeds */
  212. int access_check_done = 0;
  213. switch ( f->f_choice ) {
  214. case LDAP_FILTER_AND:
  215. case LDAP_FILTER_OR:
  216. case LDAP_FILTER_NOT:
  217. /*
  218. * optimize acl checking by only doing it once it is
  219. * known that the whole filter passes and so the entry
  220. * is eligible to be returned.
  221. * then we check the filter only for access
  222. *
  223. * complex filters really benefit from
  224. * separate stages, filter eval, followed by acl check...
  225. */
  226. if(!only_check_access)
  227. {
  228. rc = slapi_filter_test_ext_internal(pb,e,f,0,0, &access_check_done);
  229. }
  230. if(rc == 0 && verify_access)
  231. {
  232. rc = slapi_filter_test_ext_internal(pb,e,f,-1,-1, &access_check_done);
  233. }
  234. break;
  235. default:
  236. /*
  237. * ...but simple filters are better off doing eval and
  238. * acl check at once
  239. */
  240. rc = slapi_filter_test_ext_internal(pb,e,f,verify_access,only_check_access, &access_check_done);
  241. break;
  242. }
  243. return rc;
  244. }
  245. int test_ava_filter(
  246. Slapi_PBlock *pb,
  247. Slapi_Entry *e,
  248. Slapi_Attr *a,
  249. struct ava *ava,
  250. int ftype,
  251. int verify_access,
  252. int only_check_access,
  253. int *access_check_done
  254. )
  255. {
  256. int rc;
  257. LDAPDebug( LDAP_DEBUG_FILTER, "=> test_ava_filter\n", 0, 0, 0 );
  258. *access_check_done = 0;
  259. if(optimise_filter_acl_tests())
  260. {
  261. rc = 0;
  262. if(!only_check_access)
  263. {
  264. rc = -1;
  265. for ( ; a != NULL; a = a->a_next ) {
  266. if ( slapi_attr_type_cmp( ava->ava_type, a->a_type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  267. rc = plugin_call_syntax_filter_ava( a, ftype, ava );
  268. if ( rc == 0 ) {
  269. break;
  270. }
  271. }
  272. }
  273. }
  274. if ( rc == 0 && verify_access && pb != NULL ) {
  275. char *attrs[2] = { NULL, NULL };
  276. attrs[0] = ava->ava_type;
  277. rc = plugin_call_acl_plugin( pb, e, attrs, &ava->ava_value,
  278. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  279. *access_check_done = -1;
  280. }
  281. }
  282. else
  283. {
  284. if ( verify_access && pb != NULL ) {
  285. char *attrs[2] = { NULL, NULL };
  286. attrs[0] = ava->ava_type;
  287. rc = plugin_call_acl_plugin( pb, e, attrs, &ava->ava_value,
  288. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  289. *access_check_done = -1;
  290. if ( only_check_access || rc != LDAP_SUCCESS ) {
  291. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_ava_filter %d\n",
  292. rc, 0, 0 );
  293. return( rc );
  294. }
  295. }
  296. rc = -1;
  297. for ( ; a != NULL; a = a->a_next ) {
  298. if ( slapi_attr_type_cmp( ava->ava_type, a->a_type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  299. rc = plugin_call_syntax_filter_ava( a, ftype, ava );
  300. if ( rc == 0 ) {
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_ava_filter %d\n", rc, 0, 0 );
  307. return( rc );
  308. }
  309. int
  310. test_presence_filter(
  311. Slapi_PBlock *pb,
  312. Slapi_Entry *e,
  313. char *type,
  314. int verify_access,
  315. int only_check_access,
  316. int *access_check_done
  317. )
  318. {
  319. int rc;
  320. void *hint = NULL;
  321. *access_check_done = 0;
  322. if(optimise_filter_acl_tests())
  323. {
  324. rc = 0;
  325. /* Use attrlist_find_ex to get subtype matching */
  326. if(!only_check_access)
  327. {
  328. rc = attrlist_find_ex( e->e_attrs, type,
  329. NULL, NULL, &hint ) != NULL ? 0 : -1;
  330. }
  331. if (rc == 0 && verify_access && pb != NULL) {
  332. char *attrs[2] = { NULL, NULL };
  333. attrs[0] = type;
  334. rc = plugin_call_acl_plugin( pb, e, attrs, NULL, SLAPI_ACL_SEARCH,
  335. ACLPLUGIN_ACCESS_DEFAULT, NULL );
  336. *access_check_done = -1;
  337. }
  338. }
  339. else
  340. {
  341. if (verify_access && pb != NULL) {
  342. char *attrs[2] = { NULL, NULL };
  343. attrs[0] = type;
  344. rc = plugin_call_acl_plugin( pb, e, attrs, NULL, SLAPI_ACL_SEARCH,
  345. ACLPLUGIN_ACCESS_DEFAULT, NULL );
  346. *access_check_done = -1;
  347. if ( only_check_access || rc != LDAP_SUCCESS ) {
  348. return( rc );
  349. }
  350. }
  351. /* Use attrlist_find_ex to get subtype matching */
  352. rc = attrlist_find_ex( e->e_attrs, type,
  353. NULL, NULL, &hint ) != NULL ? 0 : -1;
  354. }
  355. return rc;
  356. }
  357. /*
  358. * Convert a DN into a list of attribute values.
  359. * The caller must free the returned attributes.
  360. */
  361. static Slapi_Attr*
  362. dn2attrs(const char *dn)
  363. {
  364. int rc= 0;
  365. Slapi_Attr* dnAttrs = NULL;
  366. char** rdns = ldap_explode_dn (dn, 0);
  367. if (rdns)
  368. {
  369. char** rdn = rdns;
  370. for (; !rc && *rdn; ++rdn)
  371. {
  372. char** avas = ldap_explode_rdn (*rdn, 0);
  373. if (avas)
  374. {
  375. char** ava = avas;
  376. for (; !rc && *ava; ++ava)
  377. {
  378. char* val = strchr (*ava, '=');
  379. if (val)
  380. {
  381. struct berval bv;
  382. struct berval* bvec[] = {NULL, NULL};
  383. size_t type_len = val - *ava;
  384. char* type = slapi_ch_malloc (type_len + 1);
  385. memcpy (type, *ava, type_len);
  386. type[type_len] = '\0';
  387. ++val; /* skip the '=' */
  388. bv.bv_val = val;
  389. bv.bv_len = strlen(val);
  390. bvec[0] = &bv;
  391. attrlist_merge (&dnAttrs, type, bvec);
  392. slapi_ch_free_string( &type );
  393. }
  394. }
  395. slapi_ldap_value_free (avas);
  396. }
  397. }
  398. slapi_ldap_value_free (rdns);
  399. }
  400. return dnAttrs;
  401. }
  402. static int
  403. test_extensible_filter(
  404. Slapi_PBlock *callers_pb,
  405. Slapi_Entry *e,
  406. mr_filter_t *mrf,
  407. int verify_access,
  408. int only_check_access,
  409. int *access_check_done
  410. )
  411. {
  412. /*
  413. * The ABNF for extensible filters is
  414. *
  415. * attr [":dn"] [":" matchingrule] ":=" value
  416. * [":dn"] ":" matchingrule ":=" value
  417. *
  418. * So, sigh, there are six possible combinations:
  419. *
  420. * A) attr ":=" value
  421. * B) attr ":dn" ":=" value
  422. * C) attr ":" matchingrule ":=" value
  423. * D) attr ":dn" ":" matchingrule ":=" value
  424. * E) ":" matchingrule ":=" value
  425. * F) ":dn" ":" matchingrule ":=" value
  426. */
  427. int rc;
  428. LDAPDebug( LDAP_DEBUG_FILTER, "=> test_extensible_filter\n", 0, 0, 0 );
  429. *access_check_done = 0;
  430. if(optimise_filter_acl_tests())
  431. {
  432. rc = LDAP_SUCCESS;
  433. if(!only_check_access)
  434. {
  435. if (mrf->mrf_match==NULL)
  436. {
  437. /*
  438. * Could be A or B
  439. * No matching function. So use a regular equality filter.
  440. * Check the regular attributes for the attribute value.
  441. */
  442. struct ava a;
  443. a.ava_type= mrf->mrf_type;
  444. a.ava_value.bv_len= mrf->mrf_value.bv_len;
  445. a.ava_value.bv_val = mrf->mrf_value.bv_val;
  446. rc= test_ava_filter( callers_pb, e, e->e_attrs, &a, LDAP_FILTER_EQUALITY, 0 /* Don't Verify Access */ , 0 /* don't just verify access */, access_check_done );
  447. if(rc!=LDAP_SUCCESS && mrf->mrf_dnAttrs)
  448. {
  449. /* B) Also check the DN attributes for the attribute value */
  450. Slapi_Attr* dnattrs= dn2attrs(slapi_entry_get_dn_const(e));
  451. rc= test_ava_filter( callers_pb, e, dnattrs, &a, LDAP_FILTER_EQUALITY, 0 /* Don't Verify Access */ , 0 /* don't just verify access */, access_check_done );
  452. attrlist_free( dnattrs );
  453. }
  454. }
  455. else
  456. {
  457. /*
  458. * Could be C, D, E, or F
  459. * We have a matching rule.
  460. */
  461. rc = mrf->mrf_match (mrf->mrf_object, e, e->e_attrs);
  462. if(rc!=LDAP_SUCCESS && mrf->mrf_dnAttrs)
  463. {
  464. /* D & F) Also check the DN attributes for the attribute value */
  465. Slapi_Attr* dnattrs= dn2attrs(slapi_entry_get_dn_const(e));
  466. mrf->mrf_match (mrf->mrf_object, e, dnattrs);
  467. attrlist_free( dnattrs );
  468. }
  469. }
  470. }
  471. if(rc == 0 && mrf->mrf_type!=NULL && verify_access)
  472. {
  473. char *attrs[2] = { NULL, NULL };
  474. /* Could be A, B, C, or D */
  475. /* Check we have access to this attribute on this entry */
  476. attrs[0] = mrf->mrf_type;
  477. rc= plugin_call_acl_plugin (callers_pb, e, attrs, &(mrf->mrf_value),
  478. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  479. *access_check_done = -1;
  480. }
  481. }
  482. else
  483. {
  484. rc = LDAP_SUCCESS;
  485. if(mrf->mrf_type!=NULL && verify_access)
  486. {
  487. char *attrs[2] = { NULL, NULL };
  488. /* Could be A, B, C, or D */
  489. /* Check we have access to this attribute on this entry */
  490. attrs[0] = mrf->mrf_type;
  491. rc= plugin_call_acl_plugin (callers_pb, e, attrs, &(mrf->mrf_value),
  492. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  493. *access_check_done = -1;
  494. if ( only_check_access ) {
  495. return rc;
  496. }
  497. }
  498. if(rc==LDAP_SUCCESS)
  499. {
  500. if (mrf->mrf_match==NULL)
  501. {
  502. /*
  503. * Could be A or B
  504. * No matching function. So use a regular equality filter.
  505. * Check the regular attributes for the attribute value.
  506. */
  507. struct ava a;
  508. a.ava_type= mrf->mrf_type;
  509. a.ava_value.bv_len= mrf->mrf_value.bv_len;
  510. a.ava_value.bv_val = mrf->mrf_value.bv_val;
  511. rc= test_ava_filter( callers_pb, e, e->e_attrs, &a, LDAP_FILTER_EQUALITY, 0 /* Don't Verify Access */ , 0 /* don't just verify access */, access_check_done );
  512. if(rc!=LDAP_SUCCESS && mrf->mrf_dnAttrs)
  513. {
  514. /* B) Also check the DN attributes for the attribute value */
  515. Slapi_Attr* dnattrs= dn2attrs(slapi_entry_get_dn_const(e));
  516. rc= test_ava_filter( callers_pb, e, dnattrs, &a, LDAP_FILTER_EQUALITY, 0 /* Don't Verify Access */ , 0 /* don't just verify access */, access_check_done );
  517. attrlist_free( dnattrs );
  518. }
  519. }
  520. else
  521. {
  522. /*
  523. * Could be C, D, E, or F
  524. * We have a matching rule.
  525. */
  526. rc = mrf->mrf_match (mrf->mrf_object, e, e->e_attrs);
  527. if(rc!=LDAP_SUCCESS && mrf->mrf_dnAttrs)
  528. {
  529. /* D & F) Also check the DN attributes for the attribute value */
  530. Slapi_Attr* dnattrs= dn2attrs(slapi_entry_get_dn_const(e));
  531. mrf->mrf_match (mrf->mrf_object, e, dnattrs);
  532. attrlist_free( dnattrs );
  533. }
  534. }
  535. }
  536. }
  537. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_extensible_filter %d\n", rc, 0, 0 );
  538. return( rc );
  539. }
  540. static int
  541. test_filter_list(
  542. Slapi_PBlock *pb,
  543. Slapi_Entry *e,
  544. struct slapi_filter *flist,
  545. int ftype,
  546. int verify_access,
  547. int only_check_access,
  548. int *access_check_done
  549. )
  550. {
  551. int nomatch;
  552. struct slapi_filter *f;
  553. int access_check_tmp = -1;
  554. LDAPDebug( LDAP_DEBUG_FILTER, "=> test_filter_list\n", 0, 0, 0 );
  555. *access_check_done = -1;
  556. nomatch = 1;
  557. for ( f = flist; f != NULL; f = f->f_next ) {
  558. if ( slapi_filter_test_ext_internal( pb, e, f, verify_access, only_check_access, &access_check_tmp ) != 0 ) {
  559. /* optimize AND evaluation */
  560. if ( ftype == LDAP_FILTER_AND ) {
  561. /* one false is failure */
  562. nomatch = 1;
  563. break;
  564. }
  565. } else {
  566. nomatch = 0;
  567. /* optimize OR evaluation too */
  568. if ( ftype == LDAP_FILTER_OR ) {
  569. /* only one needs to be true */
  570. break;
  571. }
  572. }
  573. if(!access_check_tmp)
  574. *access_check_done = 0;
  575. }
  576. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_filter_list %d\n", nomatch, 0, 0 );
  577. return( nomatch );
  578. }
  579. void
  580. filter_strcpy_special( char *d, char *s )
  581. {
  582. for ( ; *s; s++ ) {
  583. switch ( *s ) {
  584. case '.':
  585. case '\\':
  586. case '[':
  587. case ']':
  588. case '*':
  589. case '+':
  590. case '^':
  591. case '$':
  592. *d++ = '\\';
  593. /* FALL */
  594. default:
  595. *d++ = *s;
  596. }
  597. }
  598. *d = '\0';
  599. }
  600. int test_substring_filter(
  601. Slapi_PBlock *pb,
  602. Slapi_Entry *e,
  603. struct slapi_filter *f,
  604. int verify_access,
  605. int only_check_access,
  606. int *access_check_done
  607. )
  608. {
  609. Slapi_Attr *a;
  610. int rc;
  611. LDAPDebug( LDAP_DEBUG_FILTER, "=> test_substring_filter\n", 0, 0, 0 );
  612. *access_check_done = 0;
  613. if(optimise_filter_acl_tests())
  614. {
  615. rc = 0;
  616. if(!only_check_access)
  617. {
  618. rc = -1;
  619. for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
  620. if ( slapi_attr_type_cmp( f->f_sub_type, a->a_type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  621. rc = plugin_call_syntax_filter_sub( pb, a, &f->f_sub );
  622. if ( rc == 0 ) {
  623. break;
  624. }
  625. }
  626. }
  627. }
  628. if ( rc == 0 && verify_access && pb != NULL) {
  629. char *attrs[2] = { NULL, NULL };
  630. attrs[0] = f->f_sub_type;
  631. rc = plugin_call_acl_plugin( pb, e, attrs, NULL,
  632. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  633. *access_check_done = -1;
  634. }
  635. }
  636. else
  637. {
  638. if ( verify_access && pb != NULL) {
  639. char *attrs[2] = { NULL, NULL };
  640. attrs[0] = f->f_sub_type;
  641. rc = plugin_call_acl_plugin( pb, e, attrs, NULL,
  642. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  643. *access_check_done = -1;
  644. if ( only_check_access || rc != LDAP_SUCCESS ) {
  645. return( rc );
  646. }
  647. }
  648. rc = -1;
  649. for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
  650. if ( slapi_attr_type_cmp( f->f_sub_type, a->a_type, SLAPI_TYPE_CMP_SUBTYPE ) == 0 ) {
  651. rc = plugin_call_syntax_filter_sub( pb, a, &f->f_sub );
  652. if ( rc == 0 || rc == LDAP_TIMELIMIT_EXCEEDED ) {
  653. break;
  654. }
  655. }
  656. }
  657. }
  658. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_substring_filter %d\n",
  659. rc, 0, 0 );
  660. return( rc );
  661. }
  662. /*
  663. * Here's a duplicate vattr filter test code modified to support vattrs.
  664. */
  665. /*
  666. * slapi_vattr_filter_test - test a filter against a single entry.
  667. *
  668. * Supports the case where the filter mentions virtual attributes.
  669. * Performance for a real attr only filter is same as for slapi_filter_test()
  670. * No explicit support for vattrs in extended filters because:
  671. * 1. the matching rules must support virtual attributes themselves.
  672. * 2. if no matching rule is specified it defaults to equality so
  673. * could just use a normal filter with equality.
  674. * 3. virtual naming attributes are probably too complex to support.
  675. *
  676. * returns 0 filter matched
  677. * -1 filter did not match
  678. * >0 an ldap error code
  679. */
  680. int
  681. slapi_vattr_filter_test(
  682. Slapi_PBlock *pb,
  683. Slapi_Entry *e,
  684. struct slapi_filter *f,
  685. int verify_access
  686. )
  687. {
  688. return slapi_vattr_filter_test_ext(pb,e,f,verify_access,0);
  689. }
  690. /*
  691. * vattr_filter_test_ext - full-feature filter test function
  692. *
  693. * returns 0 filter matched
  694. * -1 filter did not match
  695. * >0 an ldap error code
  696. */
  697. int
  698. slapi_vattr_filter_test_ext(
  699. Slapi_PBlock *pb,
  700. Slapi_Entry *e,
  701. struct slapi_filter *f,
  702. int verify_access,
  703. int only_check_access
  704. )
  705. {
  706. int rc = 0; /* a no op request succeeds */
  707. int access_check_done = 0;
  708. switch ( f->f_choice ) {
  709. case LDAP_FILTER_AND:
  710. case LDAP_FILTER_OR:
  711. case LDAP_FILTER_NOT:
  712. /*
  713. * optimize acl checking by only doing it once it is
  714. * known that the whole filter passes and so the entry
  715. * is eligible to be returned.
  716. * then we check the filter only for access
  717. *
  718. * complex filters really benefit from
  719. * separate stages, filter eval, followed by acl check...
  720. */
  721. if(!only_check_access)
  722. {
  723. rc = slapi_vattr_filter_test_ext_internal(pb,e,f,0,0, &access_check_done);
  724. }
  725. if(rc == 0 && verify_access)
  726. {
  727. rc = slapi_vattr_filter_test_ext_internal(pb,e,f,-1,-1, &access_check_done);
  728. }
  729. break;
  730. default:
  731. /*
  732. * ...but simple filters are better off doing eval and
  733. * acl check at once
  734. */
  735. rc = slapi_vattr_filter_test_ext_internal(pb,e,f,verify_access,only_check_access, &access_check_done);
  736. break;
  737. }
  738. return rc;
  739. }
  740. static int
  741. slapi_vattr_filter_test_ext_internal(
  742. Slapi_PBlock *pb,
  743. Slapi_Entry *e,
  744. struct slapi_filter *f,
  745. int verify_access,
  746. int only_check_access,
  747. int *access_check_done
  748. )
  749. {
  750. int rc = LDAP_SUCCESS;
  751. LDAPDebug( LDAP_DEBUG_FILTER, "=> slapi_vattr_filter_test_ext\n", 0, 0, 0 );
  752. /*
  753. * RJP: Not sure if this is semantically right, but we have to
  754. * return something if f is NULL. If there is no filter,
  755. * then we say that it did match and return 0.
  756. */
  757. if ( f == NULL) {
  758. return(0);
  759. }
  760. LDAPDebug( LDAP_DEBUG_FILTER, "=> test_substring_filter\n", 0, 0, 0 );
  761. switch ( f->f_choice ) {
  762. case LDAP_FILTER_EQUALITY:
  763. LDAPDebug( LDAP_DEBUG_FILTER, " EQUALITY\n", 0, 0, 0 );
  764. if ( verify_access ) {
  765. rc = test_filter_access( pb, e, f->f_ava.ava_type,
  766. &f->f_ava.ava_value);
  767. *access_check_done = 1;
  768. }
  769. if ( only_check_access || rc != LDAP_SUCCESS ) {
  770. return( rc );
  771. }
  772. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_AVA, f->f_ava.ava_type );
  773. break;
  774. case LDAP_FILTER_SUBSTRINGS:
  775. LDAPDebug( LDAP_DEBUG_FILTER, " SUBSTRINGS\n", 0, 0, 0 );
  776. if ( verify_access ) {
  777. rc = test_filter_access( pb, e, f->f_sub_type, NULL);
  778. *access_check_done = 1;
  779. }
  780. if ( only_check_access || rc != LDAP_SUCCESS ) {
  781. return( rc );
  782. }
  783. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_SUBSTRING, f->f_sub_type);
  784. break;
  785. case LDAP_FILTER_GE:
  786. LDAPDebug( LDAP_DEBUG_FILTER, " GE\n", 0, 0, 0 );
  787. if ( verify_access ) {
  788. rc = test_filter_access( pb, e, f->f_ava.ava_type,
  789. &f->f_ava.ava_value);
  790. *access_check_done = 1;
  791. }
  792. if ( only_check_access || rc != LDAP_SUCCESS ) {
  793. return( rc );
  794. }
  795. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_AVA, f->f_ava.ava_type);
  796. break;
  797. case LDAP_FILTER_LE:
  798. LDAPDebug( LDAP_DEBUG_FILTER, " LE\n", 0, 0, 0 );
  799. if ( verify_access ) {
  800. rc = test_filter_access( pb, e, f->f_ava.ava_type,
  801. &f->f_ava.ava_value);
  802. *access_check_done = 1;
  803. }
  804. if ( only_check_access || rc != LDAP_SUCCESS ) {
  805. return( rc );
  806. }
  807. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_AVA, f->f_ava.ava_type);
  808. break;
  809. case LDAP_FILTER_PRESENT:
  810. LDAPDebug( LDAP_DEBUG_FILTER, " PRESENT\n", 0, 0, 0 );
  811. if ( verify_access ) {
  812. rc = test_filter_access( pb, e, f->f_type, NULL);
  813. *access_check_done = 1;
  814. }
  815. if ( only_check_access || rc != LDAP_SUCCESS ) {
  816. return( rc );
  817. }
  818. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_PRES, f->f_type);
  819. break;
  820. case LDAP_FILTER_APPROX:
  821. LDAPDebug( LDAP_DEBUG_FILTER, " APPROX\n", 0, 0, 0 );
  822. if ( verify_access ) {
  823. rc = test_filter_access( pb, e, f->f_ava.ava_type,
  824. &f->f_ava.ava_value);
  825. *access_check_done = 1;
  826. }
  827. if ( only_check_access || rc != LDAP_SUCCESS ) {
  828. return( rc );
  829. }
  830. rc = vattr_test_filter( pb, e, f, FILTER_TYPE_AVA, f->f_ava.ava_type);
  831. break;
  832. case LDAP_FILTER_EXTENDED:
  833. LDAPDebug( LDAP_DEBUG_FILTER, " EXTENDED\n", 0, 0, 0 );
  834. rc = test_extensible_filter( pb, e, &f->f_mr, verify_access ,
  835. only_check_access, access_check_done);
  836. break;
  837. case LDAP_FILTER_AND:
  838. LDAPDebug( LDAP_DEBUG_FILTER, " AND\n", 0, 0, 0 );
  839. rc = vattr_test_filter_list( pb, e, f->f_and,
  840. LDAP_FILTER_AND , verify_access, only_check_access, access_check_done);
  841. break;
  842. case LDAP_FILTER_OR:
  843. LDAPDebug( LDAP_DEBUG_FILTER, " OR\n", 0, 0, 0 );
  844. rc = vattr_test_filter_list( pb, e, f->f_or,
  845. LDAP_FILTER_OR , verify_access, only_check_access, access_check_done);
  846. break;
  847. case LDAP_FILTER_NOT:
  848. LDAPDebug( LDAP_DEBUG_FILTER, " NOT\n", 0, 0, 0 );
  849. rc = slapi_vattr_filter_test_ext_internal( pb, e, f->f_not , verify_access, only_check_access, access_check_done);
  850. if(!(verify_access && only_check_access)) /* dont play with access control return codes */
  851. {
  852. if(verify_access && !rc && !(*access_check_done))
  853. {
  854. /* the filter failed so access control was not checked
  855. * for NOT filters this is significant so we must ensure
  856. * access control is checked
  857. */
  858. /* check access control only */
  859. rc = slapi_vattr_filter_test_ext_internal( pb, e, f->f_not , verify_access, -1 /*only_check_access*/, access_check_done);
  860. /* preserve error code if any */
  861. if(!rc)
  862. rc = !rc;
  863. }
  864. else
  865. rc = !rc;
  866. }
  867. break;
  868. default:
  869. LDAPDebug( LDAP_DEBUG_ANY, " unknown filter type 0x%lX\n",
  870. f->f_choice, 0, 0 );
  871. rc = -1;
  872. }
  873. LDAPDebug( LDAP_DEBUG_FILTER, "<= slapi_vattr_filter_test %d\n", rc, 0, 0 );
  874. return( rc );
  875. }
  876. static int test_filter_access( Slapi_PBlock *pb,
  877. Slapi_Entry *e,
  878. char *attr_type,
  879. struct berval *attr_val) {
  880. /*
  881. * attr_type--attr_type to test for.
  882. * attr_val--attr value to test for
  883. */
  884. int rc;
  885. char *attrs[2] = { NULL, NULL };
  886. attrs[0] = attr_type;
  887. rc = plugin_call_acl_plugin( pb, e, attrs, attr_val,
  888. SLAPI_ACL_SEARCH, ACLPLUGIN_ACCESS_DEFAULT, NULL );
  889. return(rc);
  890. }
  891. static int
  892. vattr_test_filter_list(
  893. Slapi_PBlock *pb,
  894. Slapi_Entry *e,
  895. struct slapi_filter *flist,
  896. int ftype,
  897. int verify_access,
  898. int only_check_access,
  899. int *access_check_done
  900. )
  901. {
  902. int nomatch;
  903. struct slapi_filter *f;
  904. LDAPDebug( LDAP_DEBUG_FILTER, "=> vattr_test_filter_list\n", 0, 0, 0 );
  905. nomatch = 1;
  906. for ( f = flist; f != NULL; f = f->f_next ) {
  907. if ( slapi_vattr_filter_test_ext_internal( pb, e, f, verify_access, only_check_access, access_check_done ) != 0 ) {
  908. /* optimize AND evaluation */
  909. if ( ftype == LDAP_FILTER_AND ) {
  910. /* one false is failure */
  911. nomatch = 1;
  912. break;
  913. }
  914. } else {
  915. nomatch = 0;
  916. /* optimize OR evaluation too */
  917. if ( ftype == LDAP_FILTER_OR ) {
  918. /* only one needs to be true */
  919. break;
  920. }
  921. }
  922. }
  923. LDAPDebug( LDAP_DEBUG_FILTER, "<= test_filter_list %d\n", nomatch, 0, 0 );
  924. return( nomatch );
  925. }