1
0

guide.c 18 KB

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