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