1
0

guide.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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) 2009 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* guide.c - Guide and Enhanced Guide syntax routines */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include "syntax.h"
  46. static int guide_filter_ava( Slapi_PBlock *pb, struct berval *bvfilter,
  47. Slapi_Value **bvals, int ftype, Slapi_Value **retVal );
  48. static int guide_filter_sub( Slapi_PBlock *pb, char *initial, char **any,
  49. char *final, Slapi_Value **bvals );
  50. static int guide_values2keys( Slapi_PBlock *pb, Slapi_Value **val,
  51. Slapi_Value ***ivals, int ftype );
  52. static int guide_assertion2keys_ava( Slapi_PBlock *pb, Slapi_Value *val,
  53. Slapi_Value ***ivals, int ftype );
  54. static int guide_assertion2keys_sub( Slapi_PBlock *pb, char *initial, char **any,
  55. char *final, Slapi_Value ***ivals );
  56. static int guide_compare(struct berval *v1, struct berval *v2);
  57. static int enhancedguide_validate(struct berval *val);
  58. static int guide_validate(struct berval *val);
  59. static int criteria_validate(const char *start, const char *end);
  60. static int andterm_validate(const char *start, const char *end, const char **last);
  61. static int term_validate(const char *start, const char *end, const char **last);
  62. static void guide_normalize(
  63. Slapi_PBlock *pb,
  64. char *s,
  65. int trim_spaces,
  66. char **alt
  67. );
  68. /* the first name is the official one from RFC 4517 */
  69. static char *guide_names[] = { "Guide", "guide", GUIDE_SYNTAX_OID, 0 };
  70. static char *enhancedguide_names[] = { "Enhanced Guide", "enhancedguide",
  71. ENHANCEDGUIDE_SYNTAX_OID, 0 };
  72. static Slapi_PluginDesc guide_pdesc = { "guide-syntax", VENDOR, DS_PACKAGE_VERSION,
  73. "Guide attribute syntax plugin" };
  74. static Slapi_PluginDesc enhancedguide_pdesc = { "enhancedguide-syntax",
  75. VENDOR, DS_PACKAGE_VERSION,
  76. "Enhanced Guide attribute syntax plugin" };
  77. int
  78. guide_init( Slapi_PBlock *pb )
  79. {
  80. int rc, flags;
  81. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> guide_init\n", 0, 0, 0 );
  82. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  83. (void *) SLAPI_PLUGIN_VERSION_01 );
  84. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  85. (void *)&guide_pdesc );
  86. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  87. (void *) guide_filter_ava );
  88. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  89. (void *) guide_filter_sub );
  90. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  91. (void *) guide_values2keys );
  92. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  93. (void *) guide_assertion2keys_ava );
  94. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  95. (void *) guide_assertion2keys_sub );
  96. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  97. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  98. (void *) &flags );
  99. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  100. (void *) guide_names );
  101. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  102. (void *) GUIDE_SYNTAX_OID );
  103. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  104. (void *) guide_compare );
  105. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  106. (void *) guide_validate );
  107. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NORMALIZE,
  108. (void *) guide_normalize );
  109. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= guide_init %d\n", rc, 0, 0 );
  110. return( rc );
  111. }
  112. int
  113. enhancedguide_init( Slapi_PBlock *pb )
  114. {
  115. int rc, flags;
  116. LDAPDebug( LDAP_DEBUG_PLUGIN, "=> guide_init\n", 0, 0, 0 );
  117. rc = slapi_pblock_set( pb, SLAPI_PLUGIN_VERSION,
  118. (void *) SLAPI_PLUGIN_VERSION_01 );
  119. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_DESCRIPTION,
  120. (void *)&enhancedguide_pdesc );
  121. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_AVA,
  122. (void *) guide_filter_ava );
  123. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FILTER_SUB,
  124. (void *) guide_filter_sub );
  125. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALUES2KEYS,
  126. (void *) guide_values2keys );
  127. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_AVA,
  128. (void *) guide_assertion2keys_ava );
  129. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_ASSERTION2KEYS_SUB,
  130. (void *) guide_assertion2keys_sub );
  131. flags = SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING;
  132. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_FLAGS,
  133. (void *) &flags );
  134. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_NAMES,
  135. (void *) enhancedguide_names );
  136. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_OID,
  137. (void *) ENHANCEDGUIDE_SYNTAX_OID );
  138. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_COMPARE,
  139. (void *) guide_compare );
  140. rc |= slapi_pblock_set( pb, SLAPI_PLUGIN_SYNTAX_VALIDATE,
  141. (void *) enhancedguide_validate );
  142. LDAPDebug( LDAP_DEBUG_PLUGIN, "<= guide_init %d\n", rc, 0, 0 );
  143. return( rc );
  144. }
  145. static int
  146. guide_filter_ava(
  147. Slapi_PBlock *pb,
  148. struct berval *bvfilter,
  149. Slapi_Value **bvals,
  150. int ftype,
  151. Slapi_Value **retVal
  152. )
  153. {
  154. int filter_normalized = 0;
  155. int syntax = SYNTAX_CIS;
  156. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  157. if (filter_normalized) {
  158. syntax |= SYNTAX_NORM_FILT;
  159. }
  160. return( string_filter_ava( bvfilter, bvals, syntax,
  161. ftype, retVal ) );
  162. }
  163. static int
  164. guide_filter_sub(
  165. Slapi_PBlock *pb,
  166. char *initial,
  167. char **any,
  168. char *final,
  169. Slapi_Value **bvals
  170. )
  171. {
  172. return( string_filter_sub( pb, initial, any, final, bvals, SYNTAX_CIS ) );
  173. }
  174. static int
  175. guide_values2keys(
  176. Slapi_PBlock *pb,
  177. Slapi_Value **vals,
  178. Slapi_Value ***ivals,
  179. int ftype
  180. )
  181. {
  182. return( string_values2keys( pb, vals, ivals, SYNTAX_CIS,
  183. ftype ) );
  184. }
  185. static int
  186. guide_assertion2keys_ava(
  187. Slapi_PBlock *pb,
  188. Slapi_Value *val,
  189. Slapi_Value ***ivals,
  190. int ftype
  191. )
  192. {
  193. return(string_assertion2keys_ava( pb, val, ivals,
  194. SYNTAX_CIS, ftype ));
  195. }
  196. static int
  197. guide_assertion2keys_sub(
  198. Slapi_PBlock *pb,
  199. char *initial,
  200. char **any,
  201. char *final,
  202. Slapi_Value ***ivals
  203. )
  204. {
  205. return( string_assertion2keys_sub( pb, initial, any, final, ivals,
  206. SYNTAX_CIS ) );
  207. }
  208. static int guide_compare(
  209. struct berval *v1,
  210. struct berval *v2
  211. )
  212. {
  213. return value_cmp(v1, v2, SYNTAX_CIS, 3 /* Normalise both values */);
  214. }
  215. static int
  216. enhancedguide_validate(
  217. struct berval *val
  218. )
  219. {
  220. int rc = 0; /* assume the value is valid */
  221. const char *start = NULL;
  222. const char *end = NULL;
  223. const char *p = NULL;
  224. const char *sharp = NULL;
  225. /* Per RFC4517:
  226. *
  227. * EnhancedGuide = object-class SHARP WSP criteria WSP
  228. * SHARP WSP subset
  229. * subset = "baseobject" / "oneLevel" / "wholeSubtree"
  230. */
  231. /* Don't allow a 0 length string */
  232. if ((val == NULL) || (val->bv_len == 0)) {
  233. rc = 1;
  234. goto exit;
  235. }
  236. start = &(val->bv_val[0]);
  237. end = &(val->bv_val[val->bv_len - 1]);
  238. /* Find the first SHARP. */
  239. for (p = start; p <= end; p++) {
  240. if (IS_SHARP(*p)) {
  241. sharp = p;
  242. break;
  243. }
  244. }
  245. /* Fail if we didn't find a SHARP, or if SHARP
  246. * is at the start or end of the value. */
  247. if ((sharp == NULL) || (sharp == start) || (sharp == end)){
  248. rc = 1;
  249. goto exit;
  250. }
  251. /* Reset p and end to validate the object-class. */
  252. p = start;
  253. end = sharp - 1;
  254. /* Skip any leading spaces. */
  255. while ((p < sharp) && IS_SPACE(*p)) {
  256. p++;
  257. }
  258. /* Skip any trailing spaces. */
  259. while ((end > p) && IS_SPACE(*end)) {
  260. end--;
  261. }
  262. /* See if we only found spaces before the SHARP. */
  263. if (end < p) {
  264. rc = 1;
  265. goto exit;
  266. }
  267. /* Validate p to end as object-class. This is the same
  268. * as an oid, which is either a keystring or a numericoid. */
  269. if (IS_LEADKEYCHAR(*p)) {
  270. rc = keystring_validate(p, end);
  271. /* check if the value matches the numericoid form */
  272. } else if (isdigit(*p)) {
  273. rc = numericoid_validate(p, end);
  274. } else {
  275. rc = 1;
  276. }
  277. /* We're done if the object-class failed to validate. */
  278. if (rc != 0) {
  279. goto exit;
  280. }
  281. /* Reset start and end to validate the criteria. */
  282. start = sharp + 1;
  283. end = &(val->bv_val[val->bv_len - 1]);
  284. /* Find the next SHARP. */
  285. for (p = start; p <= end; p++) {
  286. if (IS_SHARP(*p)) {
  287. sharp = p;
  288. break;
  289. }
  290. }
  291. /* Fail if we didn't find a SHARP, or if SHARP
  292. * is at the start or end of the value. */
  293. if ((sharp == NULL) || (sharp == start) || (sharp == end)){
  294. rc = 1;
  295. goto exit;
  296. }
  297. /* Reset p and end to validate the criteria. */
  298. p = start;
  299. end = sharp - 1;
  300. /* Skip any leading spaces. */
  301. while ((p < sharp) && IS_SPACE(*p)) {
  302. p++;
  303. }
  304. /* Skip any trailing spaces. */
  305. while ((end > p) && IS_SPACE(*end)) {
  306. end--;
  307. }
  308. /* See if we only found spaces before the SHARP. */
  309. if (end < p) {
  310. rc = 1;
  311. goto exit;
  312. }
  313. /* Validate p to end as criteria. */
  314. if ((rc = criteria_validate(p, end)) != 0) {
  315. goto exit;
  316. }
  317. /* Reset start and end to validate the subset. We're
  318. * guaranteed to have a character after sharp. */
  319. p = start = sharp + 1;
  320. end = &(val->bv_val[val->bv_len - 1]);
  321. /* Skip any leading spaces. */
  322. while ((p < end) && IS_SPACE(*p)) {
  323. p ++;
  324. }
  325. /* Validate the subset. */
  326. switch (end - p + 1) {
  327. case 8:
  328. if (strncmp(p, "oneLevel", 8) != 0) {
  329. rc = 1;
  330. }
  331. break;
  332. case 10:
  333. if (strncmp(p, "baseobject", 10) != 0) {
  334. rc = 1;
  335. }
  336. break;
  337. case 12:
  338. if (strncmp(p, "wholeSubtree", 12) != 0) {
  339. rc = 1;
  340. }
  341. break;
  342. default:
  343. rc = 1;
  344. }
  345. exit:
  346. return rc;
  347. }
  348. static int
  349. guide_validate(
  350. struct berval *val
  351. )
  352. {
  353. int rc = 0; /* assume the value is valid */
  354. const char *start = NULL;
  355. const char *end = NULL;
  356. const char *p = NULL;
  357. const char *sharp = NULL;
  358. /* Per RFC4517:
  359. *
  360. * Guide = [ object-class SHARP ] criteria
  361. * object-class = WSP oid WSP
  362. * criteria = and-term *( BAR and-term )
  363. * and-term = term *( AMPERSAND term )
  364. * term = EXCLAIM term /
  365. * attributetype DOLLAR match-type /
  366. * LPAREN criteria RPAREN /
  367. * true /
  368. * false
  369. * match-type = "EQ" / "SUBSTR" / "GE" / "LE" / "APPROX"
  370. * true = "?true"
  371. * false = "?false"
  372. */
  373. /* Don't allow a 0 length string */
  374. if ((val == NULL) || (val->bv_len == 0)) {
  375. rc = 1;
  376. goto exit;
  377. }
  378. start = &(val->bv_val[0]);
  379. end = &(val->bv_val[val->bv_len - 1]);
  380. /* Look for a SHARP. If we have one, the value should
  381. * begin with the optional object-class. */
  382. for (p = start; p <= end; p++) {
  383. if (IS_SHARP(*p)) {
  384. sharp = p;
  385. break;
  386. }
  387. }
  388. if (sharp) {
  389. /* "criteria" must exist, so the SHARP
  390. * can't be at the end of the value. */
  391. if (sharp == end) {
  392. rc = 1;
  393. goto exit;
  394. }
  395. /* An optional object-class should be present. Reset
  396. * p to the beginning of the value and end to just
  397. * before the SHARP to validate the object-class.
  398. * We'll reset end later. */
  399. p = start;
  400. end = sharp - 1;
  401. /* This can happen if the value begins with SHARP. */
  402. if (end < start) {
  403. rc = 1;
  404. goto exit;
  405. }
  406. /* Skip any leading spaces. */
  407. while ((p < sharp) && IS_SPACE(*p)) {
  408. p++;
  409. }
  410. /* Skip any trailing spaces. */
  411. while ((end > p) && IS_SPACE(*end)) {
  412. end--;
  413. }
  414. /* See if we only found spaces before the SHARP. */
  415. if (end < p) {
  416. rc = 1;
  417. goto exit;
  418. }
  419. /* Validate p to end as object-class. This is the same
  420. * as an oid, which is either a keystring or a numericoid. */
  421. if (IS_LEADKEYCHAR(*p)) {
  422. rc = keystring_validate(p, end);
  423. /* check if the value matches the numericoid form */
  424. } else if (isdigit(*p)) {
  425. rc = numericoid_validate(p, end);
  426. } else {
  427. rc = 1;
  428. }
  429. /* If the object-class failed to validate, we're done. */
  430. if (rc != 0) {
  431. goto exit;
  432. }
  433. /* Reset p and end to point to the criteria. */
  434. p = sharp + 1;
  435. end = &(val->bv_val[val->bv_len - 1]);
  436. } else {
  437. /* Reset p. */
  438. p = start;
  439. }
  440. /* Validate the criteria. */
  441. rc = criteria_validate(p, end);
  442. exit:
  443. return rc;
  444. }
  445. /* criteria_validate()
  446. *
  447. * Helper to validate criteria element.
  448. */
  449. static int
  450. criteria_validate(const char *start, const char *end)
  451. {
  452. const char *p = start;
  453. const char *last = NULL;
  454. int rc = 0;
  455. /* Validate the criteria, which is just made up of a number
  456. * of and-term elements. Validate one and-term at a time. */
  457. while (p <= end) {
  458. if ((rc = andterm_validate(p, end, &last)) != 0) {
  459. goto exit;
  460. }
  461. p = last + 1;
  462. /* p should be pointing at a BAR, or one past
  463. * the end of the entire value. If we have
  464. * not reached the end, ensure that the next
  465. * character is a BAR and that there is at
  466. * least another character after the BAR. */
  467. if ((p <= end) && ((p == end) || (*p != '|'))) {
  468. rc = 1;
  469. goto exit;
  470. }
  471. /* Advance the pointer past the BAR so
  472. * it points at the beginning of the
  473. * next and-term (if there is one). */
  474. p++;
  475. }
  476. exit:
  477. return rc;
  478. }
  479. /*
  480. * andterm_validate()
  481. *
  482. * This function will validate a single and-term. If the and-term
  483. * is valid, 0 will be returned, otherwise non-zero will be returned.
  484. * A pointer to the last character of the and-term will be set in the
  485. * "last" parameter in the valid case.
  486. */
  487. static int
  488. andterm_validate(const char *start, const char *end, const char **last)
  489. {
  490. const char *p = start;
  491. int rc = 0;
  492. if ((start == NULL) || (end == NULL)) {
  493. rc = 1;
  494. goto exit;
  495. }
  496. while (p <= end) {
  497. if ((rc = term_validate(p, end, last)) != 0) {
  498. goto exit;
  499. }
  500. p = *last + 1;
  501. /* p should be pointing at an ampersand, a bar, or
  502. * one past the end of the entire value. If we have
  503. * not reached the end, ensure that the next
  504. * character is an ampersand or a bar and that
  505. * there is at least another character afterwards. */
  506. if ((p <= end) && ((p == end) || ((*p != '&') && (*p != '|')))) {
  507. rc = 1;
  508. goto exit;
  509. }
  510. /* If p is a bar, we're done. */
  511. if (*p == '|') {
  512. break;
  513. }
  514. /* Advance the pointer past the ampersand
  515. * or bar so it points at the beginning of
  516. * the next term or and-term (if there is
  517. * one). */
  518. p++;
  519. }
  520. exit:
  521. return rc;
  522. }
  523. static int
  524. term_validate(const char *start, const char *end, const char **last)
  525. {
  526. int rc = 0;
  527. const char *p = start;
  528. /* Per RFC 4517:
  529. *
  530. * term = EXCLAIM term /
  531. * attributetype DOLLAR match-type /
  532. * LPAREN criteria RPAREN /
  533. * true /
  534. * false
  535. * match-type = "EQ" / "SUBSTR" / "GE" / "LE" / "APPROX"
  536. * true = "?true"
  537. * false = "?false"
  538. */
  539. /* See if the term is prefixed by an EXCLAIM. */
  540. if (*p == '!') {
  541. p++;
  542. /* Ensure the value doesn't end with an EXCLAIM. */
  543. if (p > end) {
  544. rc = 1;
  545. goto exit;
  546. }
  547. }
  548. /* Check for valid terms. */
  549. switch (*p) {
  550. case '?':
  551. {
  552. /* true or false */
  553. int length = 0;
  554. p++;
  555. length = end - p + 1;
  556. if ((length >= 5) && (strncmp(p, "false", 5) == 0)) {
  557. /* Found false. We're done. */
  558. *last = p + 4;
  559. goto exit;
  560. }
  561. if ((length >= 4) && (strncmp(p, "true", 4) == 0)) {
  562. /* Found true. We're done. */
  563. *last = p + 3;
  564. goto exit;
  565. }
  566. /* We didn't find true or false. Fail. */
  567. rc = 1;
  568. goto exit;
  569. }
  570. case '(':
  571. {
  572. /* LPAREN criteria RPAREN */
  573. const char *lparen = p;
  574. while ((p <= end) && !IS_RPAREN(*p)) {
  575. p++;
  576. }
  577. if (p > end) {
  578. /* We didn't find a RPAREN. Fail. */
  579. rc = 1;
  580. goto exit;
  581. } else {
  582. /* p is pointing at the RPAREN. Validate
  583. * everything between the parens as criteria. */
  584. rc = criteria_validate(lparen + 1, p - 1);
  585. *last = p;
  586. }
  587. break;
  588. }
  589. default:
  590. {
  591. /* attributetype DOLLAR match-type */
  592. const char *attrtype = p;
  593. while ((p <= end) && !IS_DOLLAR(*p)) {
  594. p++;
  595. }
  596. if (p > end) {
  597. /* We didn't find a DOLLAR. Fail. */
  598. rc = 1;
  599. goto exit;
  600. } else {
  601. /* p is pointing at the DOLLAR. Validate
  602. * the attributetype before the DOLLAR. */
  603. if (IS_LEADKEYCHAR(*attrtype)) {
  604. rc = keystring_validate(attrtype, p - 1);
  605. /* check if the value matches the numericoid form */
  606. } else if (isdigit(*attrtype)) {
  607. rc = numericoid_validate(attrtype, p - 1);
  608. } else {
  609. rc = 1;
  610. }
  611. /* If the attributetype was invalid, we're done. */
  612. if (rc != 0) {
  613. goto exit;
  614. }
  615. /* Validate that a valid match-type
  616. * is after the DOLLAR. */
  617. if (p == end) {
  618. rc = 1;
  619. goto exit;
  620. } else {
  621. int length = 0;
  622. p++;
  623. length = end - p + 1;
  624. if (length >= 6) {
  625. /* APPROX, SUBSTR */
  626. if ((strncmp(p, "APPROX", 6) == 0) ||
  627. (strncmp(p, "SUBSTR", 6) == 0)) {
  628. /* We found a valid match-type.
  629. * We're done. */
  630. *last = p + 5;
  631. goto exit;
  632. }
  633. }
  634. if (length >= 2) {
  635. /* EQ, GE, LE */
  636. if ((strncmp(p, "EQ", 2) == 0) ||
  637. (strncmp(p, "GE", 2) == 0) ||
  638. (strncmp(p, "LE", 2) == 0)) {
  639. /* We found a valid match-type.
  640. * We're done. */
  641. *last = p + 1;
  642. goto exit;
  643. }
  644. }
  645. /* We failed to find a valid match-type. */
  646. rc = 1;
  647. goto exit;
  648. }
  649. }
  650. }
  651. }
  652. exit:
  653. return rc;
  654. }
  655. static void guide_normalize(
  656. Slapi_PBlock *pb,
  657. char *s,
  658. int trim_spaces,
  659. char **alt
  660. )
  661. {
  662. value_normalize_ext(s, SYNTAX_CIS, trim_spaces, alt);
  663. return;
  664. }