string.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. /* string.c - common string syntax routines */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <sys/types.h>
  16. #include "syntax.h"
  17. #if defined(IRIX)
  18. #include <unistd.h>
  19. #endif
  20. #define MAX_VAL(x, y) ((x) > (y) ? (x) : (y))
  21. static int string_filter_approx(struct berval *bvfilter,
  22. Slapi_Value **bvals,
  23. Slapi_Value **retVal);
  24. static void substring_comp_keys(Slapi_Value ***ivals, int *nsubs, char *str, int lenstr, int prepost, int syntax, char *comp_buf, int *substrlens);
  25. int
  26. string_filter_ava(struct berval *bvfilter, Slapi_Value **bvals, int syntax, int ftype, Slapi_Value **retVal)
  27. {
  28. int i, rc;
  29. struct berval bvfilter_norm = {0, NULL};
  30. struct berval *pbvfilter_norm = &bvfilter_norm;
  31. char *alt = NULL;
  32. if (retVal) {
  33. *retVal = NULL;
  34. }
  35. if (ftype == LDAP_FILTER_APPROX) {
  36. return (string_filter_approx(bvfilter, bvals, retVal));
  37. }
  38. if (syntax & SYNTAX_NORM_FILT) {
  39. pbvfilter_norm = bvfilter; /* already normalized */
  40. } else {
  41. slapi_ber_bvcpy(&bvfilter_norm, bvfilter);
  42. /* 3rd arg: 1 - trim leading blanks */
  43. value_normalize_ext(bvfilter_norm.bv_val, syntax, 1, &alt);
  44. if (alt) {
  45. slapi_ber_bvdone(&bvfilter_norm);
  46. bvfilter_norm.bv_val = alt;
  47. alt = NULL;
  48. }
  49. if (bvfilter_norm.bv_val) {
  50. bvfilter_norm.bv_len = strlen(bvfilter_norm.bv_val);
  51. } else {
  52. bvfilter_norm.bv_len = 0;
  53. }
  54. }
  55. for (i = 0; (bvals != NULL) && (bvals[i] != NULL); i++) {
  56. int norm_val = 1; /* normalize the first value only */
  57. /* if the NORMALIZED flag is set, skip normalizing */
  58. if (slapi_value_get_flags(bvals[i]) & SLAPI_ATTR_FLAG_NORMALIZED) {
  59. norm_val = 0;
  60. }
  61. /* note - do not return the normalized value in retVal - the
  62. caller will usually want the "raw" value, and can normalize it later
  63. */
  64. rc = value_cmp((struct berval *)slapi_value_get_berval(bvals[i]), pbvfilter_norm, syntax, norm_val);
  65. switch (ftype) {
  66. case LDAP_FILTER_GE:
  67. if (rc >= 0) {
  68. if (retVal) {
  69. *retVal = bvals[i];
  70. }
  71. slapi_ch_free_string(&bvfilter_norm.bv_val);
  72. return (0);
  73. }
  74. break;
  75. case LDAP_FILTER_LE:
  76. if (rc <= 0) {
  77. if (retVal) {
  78. *retVal = bvals[i];
  79. }
  80. slapi_ch_free_string(&bvfilter_norm.bv_val);
  81. return (0);
  82. }
  83. break;
  84. case LDAP_FILTER_EQUALITY:
  85. if (rc == 0) {
  86. if (retVal) {
  87. *retVal = bvals[i];
  88. }
  89. slapi_ch_free_string(&bvfilter_norm.bv_val);
  90. return (0);
  91. }
  92. break;
  93. }
  94. }
  95. slapi_ch_free_string(&bvfilter_norm.bv_val);
  96. return (-1);
  97. }
  98. /*
  99. * return value: 0 -- approximately matched
  100. * -1 -- did not match
  101. */
  102. static int
  103. string_filter_approx(struct berval *bvfilter, Slapi_Value **bvals, Slapi_Value **retVal)
  104. {
  105. int i, rc;
  106. int ava_wordcount;
  107. char *w1, *w2, *c1, *c2;
  108. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "=> string_filter_approx\n");
  109. /*
  110. * try to match words in each filter value in order
  111. * in the attribute value.
  112. * XXX should do this once for the filter and save it XXX
  113. */
  114. rc = -1;
  115. if (retVal) {
  116. *retVal = NULL;
  117. }
  118. for (i = 0; (bvals != NULL) && (bvals[i] != NULL); i++) {
  119. w2 = (char *)slapi_value_get_string(bvals[i]); /* JCM cast */
  120. ava_wordcount = 0;
  121. /* for each word in the filter value */
  122. for (w1 = first_word(bvfilter->bv_val); w1 != NULL;
  123. w1 = next_word(w1)) {
  124. ++ava_wordcount;
  125. if ((c1 = phonetic(w1)) == NULL) {
  126. break;
  127. }
  128. /*
  129. * for each word in the attribute value from
  130. * where we left off...
  131. */
  132. for (w2 = first_word(w2); w2 != NULL;
  133. w2 = next_word(w2)) {
  134. c2 = phonetic(w2);
  135. rc = strcmp(c1, c2);
  136. slapi_ch_free((void **)&c2);
  137. if (rc == 0) {
  138. if (retVal) {
  139. *retVal = bvals[i];
  140. }
  141. break;
  142. }
  143. }
  144. slapi_ch_free((void **)&c1);
  145. /*
  146. * if we stopped because we ran out of words
  147. * before making a match, go on to the next
  148. * value. otherwise try to keep matching
  149. * words in this value from where we left off.
  150. */
  151. if (w2 == NULL) {
  152. break;
  153. } else {
  154. w2 = next_word(w2);
  155. }
  156. }
  157. /*
  158. * if we stopped because we ran out of words and
  159. * we found at leasy one word, we have a match.
  160. */
  161. if (w1 == NULL && ava_wordcount > 0) {
  162. rc = 0;
  163. break;
  164. }
  165. }
  166. if (0 != rc) {
  167. rc = -1;
  168. }
  169. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "<= string_filter_approx %d\n", rc);
  170. return (rc);
  171. }
  172. int
  173. string_filter_sub(Slapi_PBlock *pb, char *initial, char **any, char * final, Slapi_Value **bvals, int syntax)
  174. {
  175. int i, j, rc, size = 0;
  176. char *p, *end, *realval, *tmpbuf = NULL, *bigpat = NULL;
  177. size_t tmpbufsize;
  178. char pat[BUFSIZ];
  179. char buf[BUFSIZ];
  180. struct timespec expire_time = {0};
  181. Operation *op = NULL;
  182. Slapi_Regex *re = NULL;
  183. const char *re_result = NULL;
  184. char *alt = NULL;
  185. int filter_normalized = 0;
  186. int free_re = 1;
  187. struct subfilt *sf = NULL;
  188. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "=> string_filter_sub\n");
  189. if (pb) {
  190. slapi_pblock_get(pb, SLAPI_OPERATION, &op);
  191. }
  192. if (NULL != op) {
  193. int32_t timelimit = -1; /* search timelimit */
  194. slapi_pblock_get(pb, SLAPI_SEARCH_TIMELIMIT, &timelimit);
  195. slapi_operation_time_expiry(op, (time_t)timelimit, &expire_time);
  196. }
  197. if (pb) {
  198. slapi_pblock_get(pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized);
  199. slapi_pblock_get(pb, SLAPI_PLUGIN_SYNTAX_FILTER_DATA, &sf);
  200. }
  201. if (sf) {
  202. re = (Slapi_Regex *)sf->sf_private;
  203. if (re) {
  204. free_re = 0;
  205. }
  206. }
  207. if (!re) {
  208. /*
  209. * construct a regular expression corresponding to the
  210. * filter and let regex do the work for each value
  211. * XXX should do this once and save it somewhere XXX
  212. */
  213. pat[0] = '\0';
  214. p = pat;
  215. end = pat + sizeof(pat) - 2; /* leave room for null */
  216. if (initial != NULL) {
  217. size = strlen(initial) + 1; /* add 1 for "^" */
  218. }
  219. if (any != NULL) {
  220. i = 0;
  221. while (any[i]) {
  222. size += strlen(any[i++]) + 2; /* add 2 for ".*" */
  223. }
  224. }
  225. if (final != NULL) {
  226. size += strlen(final) + 3; /* add 3 for ".*" and "$" */
  227. }
  228. size *= 2; /* doubled in case all filter chars need escaping */
  229. size++; /* add 1 for null */
  230. if (p + size > end) {
  231. bigpat = slapi_ch_malloc(size);
  232. p = bigpat;
  233. }
  234. if (initial != NULL) {
  235. /* 3rd arg: 1 - trim leading blanks */
  236. if (!filter_normalized) {
  237. value_normalize_ext(initial, syntax, 1, &alt);
  238. }
  239. *p++ = '^';
  240. if (alt) {
  241. filter_strcpy_special_ext(p, alt, FILTER_STRCPY_ESCAPE_RECHARS);
  242. slapi_ch_free_string(&alt);
  243. } else {
  244. filter_strcpy_special_ext(p, initial, FILTER_STRCPY_ESCAPE_RECHARS);
  245. }
  246. p = strchr(p, '\0');
  247. }
  248. if (any != NULL) {
  249. for (i = 0; any[i] != NULL; i++) {
  250. /* 3rd arg: 0 - DO NOT trim leading blanks */
  251. if (!filter_normalized) {
  252. value_normalize_ext(any[i], syntax, 0, &alt);
  253. }
  254. /* ".*" + value */
  255. *p++ = '.';
  256. *p++ = '*';
  257. if (alt) {
  258. filter_strcpy_special_ext(p, alt, FILTER_STRCPY_ESCAPE_RECHARS);
  259. slapi_ch_free_string(&alt);
  260. } else {
  261. filter_strcpy_special_ext(p, any[i], FILTER_STRCPY_ESCAPE_RECHARS);
  262. }
  263. p = strchr(p, '\0');
  264. }
  265. }
  266. if (final != NULL) {
  267. /* 3rd arg: 0 - DO NOT trim leading blanks */
  268. if (!filter_normalized) {
  269. value_normalize_ext(final, syntax, 0, &alt);
  270. }
  271. /* ".*" + value */
  272. *p++ = '.';
  273. *p++ = '*';
  274. if (alt) {
  275. filter_strcpy_special_ext(p, alt, FILTER_STRCPY_ESCAPE_RECHARS);
  276. slapi_ch_free_string(&alt);
  277. } else {
  278. filter_strcpy_special_ext(p, final, FILTER_STRCPY_ESCAPE_RECHARS);
  279. }
  280. strcat(p, "$");
  281. }
  282. /* compile the regex */
  283. p = (bigpat) ? bigpat : pat;
  284. tmpbuf = NULL;
  285. re = slapi_re_comp(p, &re_result);
  286. if (NULL == re) {
  287. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  288. "string_filter_sub - re_comp (%s) failed (%s): %s\n",
  289. pat, p, re_result ? re_result : "unknown");
  290. rc = LDAP_OPERATIONS_ERROR;
  291. goto bailout;
  292. } else if (slapi_is_loglevel_set(SLAPI_LOG_TRACE)) {
  293. char ebuf[BUFSIZ];
  294. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM,
  295. "string_filter_sub - re_comp (%s)\n", escape_string(p, ebuf));
  296. }
  297. }
  298. if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
  299. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "LDAP_TIMELIMIT_EXCEEDED\n");
  300. rc = LDAP_TIMELIMIT_EXCEEDED;
  301. goto bailout;
  302. }
  303. /*
  304. * test the regex against each value
  305. */
  306. rc = -1;
  307. tmpbuf = NULL;
  308. tmpbufsize = 0;
  309. for (j = 0; (bvals != NULL) && (bvals[j] != NULL); j++) {
  310. int tmprc;
  311. size_t len;
  312. const struct berval *bvp = slapi_value_get_berval(bvals[j]);
  313. len = bvp->bv_len;
  314. if (len < sizeof(buf)) {
  315. realval = buf;
  316. strncpy(realval, bvp->bv_val, sizeof(buf));
  317. } else if (len < tmpbufsize) {
  318. realval = tmpbuf;
  319. strncpy(realval, bvp->bv_val, tmpbufsize);
  320. } else {
  321. tmpbufsize = len + 1;
  322. realval = tmpbuf = (char *)slapi_ch_realloc(tmpbuf, tmpbufsize);
  323. strncpy(realval, bvp->bv_val, tmpbufsize);
  324. }
  325. /* 3rd arg: 1 - trim leading blanks */
  326. if (!(slapi_value_get_flags(bvals[j]) & SLAPI_ATTR_FLAG_NORMALIZED)) {
  327. value_normalize_ext(realval, syntax, 1, &alt);
  328. } else if (syntax & SYNTAX_DN) {
  329. slapi_dn_ignore_case(realval);
  330. }
  331. if (alt) {
  332. if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
  333. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "LDAP_TIMELIMIT_EXCEEDED\n");
  334. rc = LDAP_TIMELIMIT_EXCEEDED;
  335. goto bailout;
  336. }
  337. tmprc = slapi_re_exec_nt(re, alt);
  338. slapi_ch_free_string(&alt);
  339. } else {
  340. if (slapi_timespec_expire_check(&expire_time) == TIMER_EXPIRED) {
  341. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "LDAP_TIMELIMIT_EXCEEDED\n");
  342. rc = LDAP_TIMELIMIT_EXCEEDED;
  343. goto bailout;
  344. }
  345. tmprc = slapi_re_exec_nt(re, realval);
  346. }
  347. if (slapi_is_loglevel_set(SLAPI_LOG_TRACE)) {
  348. char ebuf[BUFSIZ];
  349. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "re_exec (%s) %i\n",
  350. escape_string(realval, ebuf), tmprc);
  351. }
  352. if (tmprc == 1) {
  353. rc = 0;
  354. break;
  355. } else if (tmprc != 0) {
  356. rc = tmprc;
  357. break;
  358. }
  359. }
  360. bailout:
  361. if (free_re) {
  362. slapi_re_free(re);
  363. }
  364. slapi_ch_free((void **)&tmpbuf); /* NULL is fine */
  365. slapi_ch_free((void **)&bigpat); /* NULL is fine */
  366. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "<= string_filter_sub %d\n", rc);
  367. return (rc);
  368. }
  369. int
  370. string_values2keys(Slapi_PBlock *pb, Slapi_Value **bvals, Slapi_Value ***ivals, int syntax, int ftype)
  371. {
  372. int nsubs, numbvals = 0, n;
  373. Slapi_Value **nbvals, **nbvlp;
  374. Slapi_Value **bvlp;
  375. char *w, *c, *p;
  376. char *alt = NULL;
  377. if (NULL == ivals) {
  378. return 1;
  379. }
  380. *ivals = NULL;
  381. if (NULL == bvals) {
  382. return 1;
  383. }
  384. switch (ftype) {
  385. case LDAP_FILTER_EQUALITY:
  386. /* allocate a new array for the normalized values */
  387. for (bvlp = bvals; bvlp && *bvlp; bvlp++) {
  388. numbvals++;
  389. }
  390. nbvals = (Slapi_Value **)slapi_ch_calloc((numbvals + 1), sizeof(Slapi_Value *));
  391. for (bvlp = bvals, nbvlp = nbvals; bvlp && *bvlp; bvlp++, nbvlp++) {
  392. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  393. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  394. /* if the NORMALIZED flag is set, skip normalizing */
  395. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  396. /* 3rd arg: 1 - trim leading blanks */
  397. value_normalize_ext(c, syntax, 1, &alt);
  398. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  399. } else if ((syntax & SYNTAX_DN) &&
  400. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  401. /* This dn value is normalized, but not case-normalized. */
  402. slapi_dn_ignore_case(c);
  403. /* This dn value is case-normalized */
  404. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  405. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  406. }
  407. if (alt) {
  408. slapi_ch_free_string(&c);
  409. *nbvlp = slapi_value_new_string_passin(alt);
  410. alt = NULL;
  411. } else {
  412. *nbvlp = slapi_value_new_string_passin(c);
  413. c = NULL;
  414. }
  415. /* new value is normalized */
  416. slapi_value_set_flags(*nbvlp, value_flags);
  417. }
  418. *ivals = nbvals;
  419. break;
  420. case LDAP_FILTER_APPROX:
  421. /* XXX should not do this twice! XXX */
  422. /* get an upper bound on the number of ivals */
  423. for (bvlp = bvals; bvlp && *bvlp; bvlp++) {
  424. for (w = first_word((char *)slapi_value_get_string(*bvlp));
  425. w != NULL; w = next_word(w)) {
  426. numbvals++;
  427. }
  428. }
  429. nbvals = (Slapi_Value **)slapi_ch_calloc((numbvals + 1), sizeof(Slapi_Value *));
  430. n = 0;
  431. nbvlp = nbvals;
  432. for (bvlp = bvals; bvlp && *bvlp; bvlp++) {
  433. for (w = first_word((char *)slapi_value_get_string(*bvlp));
  434. w != NULL; w = next_word(w)) {
  435. if ((c = phonetic(w)) != NULL) {
  436. *nbvlp = slapi_value_new_string_passin(c);
  437. nbvlp++;
  438. }
  439. }
  440. }
  441. /* even if (n == 0), we should return the array nbvals w/ NULL items */
  442. *ivals = nbvals;
  443. break;
  444. case LDAP_FILTER_SUBSTRINGS: {
  445. /* XXX should remove duplicates! XXX */
  446. Slapi_Value *bvdup;
  447. const struct berval *bvp;
  448. char *buf;
  449. int i;
  450. int *substrlens = NULL;
  451. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND}; /* default values */
  452. int maxsublen;
  453. /*
  454. * Substring key has 3 types:
  455. * begin (e.g., *^a)
  456. * middle (e.g., *abc)
  457. * end (e.g., *xy$)
  458. *
  459. * the each has its own key length, which can be configured as follows:
  460. * Usage: turn an index object to extensibleobject and
  461. * set an integer value for each.
  462. * dn: cn=sn, cn=index, cn=userRoot, cn=ldbm database, cn=plugins,
  463. * cn=config
  464. * objectClass: extensibleObject
  465. * nsSubStrBegin: 2
  466. * nsSubStrMiddle: 3
  467. * nsSubStrEnd: 2
  468. * [...]
  469. *
  470. * By default, begin == 3, middle == 3, end == 3 (defined in syntax.h)
  471. */
  472. /* If nsSubStrLen is specified in each index entry,
  473. respect the length for the substring index key length.
  474. Otherwise, the deafult value SUBLEN is used */
  475. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  476. if (NULL == substrlens) {
  477. substrlens = localsublens;
  478. }
  479. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  480. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  481. }
  482. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  483. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  484. }
  485. if (0 == substrlens[INDEX_SUBSTREND]) {
  486. substrlens[INDEX_SUBSTREND] = SUBEND;
  487. }
  488. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  489. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  490. buf = (char *)slapi_ch_calloc(1, maxsublen + 1);
  491. nsubs = 0;
  492. for (bvlp = bvals; bvlp && *bvlp; bvlp++) {
  493. /*
  494. * Note: this calculation may err on the high side,
  495. * because value_normalize_ext(), which is called below
  496. * before we actually create the substring keys, may
  497. * reduce the length of the value in some cases or
  498. * increase the length in other cases. For example,
  499. * spaces are removed when space insensitive strings
  500. * are normalized. Or if the value includes '\"' (2 bytes),
  501. * it's normalized to '\22' (3 bytes). But it's okay
  502. * for nsubs to be too big. Since the ivals array is
  503. * NULL terminated, the only downside is that we
  504. * allocate more space than we really need.
  505. */
  506. nsubs += slapi_value_get_length(*bvlp) - substrlens[INDEX_SUBSTRMIDDLE] + 3;
  507. }
  508. nsubs += substrlens[INDEX_SUBSTRMIDDLE] * 2 - substrlens[INDEX_SUBSTRBEGIN] - substrlens[INDEX_SUBSTREND];
  509. *ivals = (Slapi_Value **)slapi_ch_calloc((nsubs + 1), sizeof(Slapi_Value *));
  510. n = 0;
  511. bvdup = slapi_value_new();
  512. for (bvlp = bvals; bvlp && *bvlp; bvlp++) {
  513. unsigned long value_flags = slapi_value_get_flags(*bvlp);
  514. /* 3rd arg: 1 - trim leading blanks */
  515. if (!(value_flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  516. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  517. value_normalize_ext(c, syntax, 1, &alt);
  518. if (alt) {
  519. slapi_ch_free_string(&c);
  520. slapi_value_set_string_passin(bvdup, alt);
  521. alt = NULL;
  522. } else {
  523. slapi_value_set_string_passin(bvdup, c);
  524. c = NULL;
  525. }
  526. bvp = slapi_value_get_berval(bvdup);
  527. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  528. } else if ((syntax & SYNTAX_DN) &&
  529. (value_flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  530. /* This dn value is normalized, but not case-normalized. */
  531. c = slapi_ch_strdup(slapi_value_get_string(*bvlp));
  532. slapi_dn_ignore_case(c);
  533. slapi_value_set_string_passin(bvdup, c);
  534. c = NULL;
  535. /* This dn value is case-normalized */
  536. value_flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  537. value_flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  538. bvp = slapi_value_get_berval(bvdup);
  539. } else {
  540. bvp = slapi_value_get_berval(*bvlp);
  541. }
  542. /* leading */
  543. if (bvp->bv_len > substrlens[INDEX_SUBSTRBEGIN] - 2) {
  544. buf[0] = '^';
  545. for (i = 0; i < substrlens[INDEX_SUBSTRBEGIN] - 1; i++) {
  546. buf[i + 1] = bvp->bv_val[i];
  547. }
  548. buf[substrlens[INDEX_SUBSTRBEGIN]] = '\0';
  549. (*ivals)[n] = slapi_value_new_string(buf);
  550. slapi_value_set_flags((*ivals)[n], value_flags);
  551. n++;
  552. }
  553. /* any */
  554. for (p = bvp->bv_val;
  555. p < (bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTRMIDDLE] + 1);
  556. p++) {
  557. for (i = 0; i < substrlens[INDEX_SUBSTRMIDDLE]; i++) {
  558. buf[i] = p[i];
  559. }
  560. buf[substrlens[INDEX_SUBSTRMIDDLE]] = '\0';
  561. (*ivals)[n] = slapi_value_new_string(buf);
  562. slapi_value_set_flags((*ivals)[n], value_flags);
  563. n++;
  564. }
  565. /* trailing */
  566. if (bvp->bv_len > substrlens[INDEX_SUBSTREND] - 2) {
  567. p = bvp->bv_val + bvp->bv_len - substrlens[INDEX_SUBSTREND] + 1;
  568. for (i = 0; i < substrlens[INDEX_SUBSTREND] - 1; i++) {
  569. buf[i] = p[i];
  570. }
  571. buf[substrlens[INDEX_SUBSTREND] - 1] = '$';
  572. buf[substrlens[INDEX_SUBSTREND]] = '\0';
  573. (*ivals)[n] = slapi_value_new_string(buf);
  574. slapi_value_set_flags((*ivals)[n], value_flags);
  575. n++;
  576. }
  577. }
  578. slapi_value_free(&bvdup);
  579. slapi_ch_free_string(&buf);
  580. } break;
  581. }
  582. return (0);
  583. }
  584. /* we've added code to make our equality filter processing faster */
  585. int
  586. string_assertion2keys_ava(
  587. Slapi_PBlock *pb __attribute__((unused)),
  588. Slapi_Value *val,
  589. Slapi_Value ***ivals,
  590. int syntax,
  591. int ftype)
  592. {
  593. int i, numbvals;
  594. size_t len;
  595. char *w, *c;
  596. Slapi_Value *tmpval = NULL;
  597. char *alt = NULL;
  598. unsigned long flags = val ? slapi_value_get_flags(val) : 0;
  599. switch (ftype) {
  600. case LDAP_FILTER_EQUALITY_FAST:
  601. /* this code is trying to avoid multiple malloc/frees */
  602. len = slapi_value_get_length(val);
  603. tmpval = (*ivals)[0];
  604. if (len >= tmpval->bv.bv_len) {
  605. tmpval->bv.bv_val = (char *)slapi_ch_malloc(len + 1);
  606. }
  607. memcpy(tmpval->bv.bv_val, slapi_value_get_string(val), len);
  608. tmpval->bv.bv_val[len] = '\0';
  609. if (!(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  610. /* 3rd arg: 1 - trim leading blanks */
  611. value_normalize_ext(tmpval->bv.bv_val, syntax, 1, &alt);
  612. if (alt) {
  613. if (len >= tmpval->bv.bv_len) {
  614. slapi_ch_free_string(&tmpval->bv.bv_val);
  615. }
  616. tmpval->bv.bv_val = alt;
  617. alt = NULL;
  618. }
  619. tmpval->bv.bv_len = strlen(tmpval->bv.bv_val);
  620. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  621. } else if ((syntax & SYNTAX_DN) &&
  622. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  623. /* This dn value is normalized, but not case-normalized. */
  624. slapi_dn_ignore_case(tmpval->bv.bv_val);
  625. /* This dn value is case-normalized */
  626. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  627. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  628. }
  629. slapi_value_set_flags(tmpval, flags);
  630. break;
  631. case LDAP_FILTER_EQUALITY:
  632. (*ivals) = (Slapi_Value **)slapi_ch_malloc(2 * sizeof(Slapi_Value *));
  633. (*ivals)[0] = val ? slapi_value_dup(val) : NULL;
  634. if (val && !(flags & SLAPI_ATTR_FLAG_NORMALIZED)) {
  635. /* 3rd arg: 1 - trim leading blanks */
  636. value_normalize_ext((*ivals)[0]->bv.bv_val, syntax, 1, &alt);
  637. if (alt) {
  638. slapi_ch_free_string(&(*ivals)[0]->bv.bv_val);
  639. (*ivals)[0]->bv.bv_val = alt;
  640. (*ivals)[0]->bv.bv_len = strlen((*ivals)[0]->bv.bv_val);
  641. alt = NULL;
  642. }
  643. flags |= SLAPI_ATTR_FLAG_NORMALIZED;
  644. } else if ((syntax & SYNTAX_DN) &&
  645. (flags & SLAPI_ATTR_FLAG_NORMALIZED_CES)) {
  646. /* This dn value is normalized, but not case-normalized. */
  647. slapi_dn_ignore_case((*ivals)[0]->bv.bv_val);
  648. /* This dn value is case-normalized */
  649. flags &= ~SLAPI_ATTR_FLAG_NORMALIZED_CES;
  650. flags |= SLAPI_ATTR_FLAG_NORMALIZED_CIS;
  651. }
  652. slapi_value_set_flags((*ivals)[0], flags);
  653. (*ivals)[1] = NULL;
  654. break;
  655. case LDAP_FILTER_APPROX:
  656. /* XXX should not do this twice! XXX */
  657. /* get an upper bound on the number of ivals */
  658. numbvals = 0;
  659. for (w = first_word((char *)slapi_value_get_string(val)); w != NULL;
  660. w = next_word(w)) {
  661. numbvals++;
  662. }
  663. (*ivals) = (Slapi_Value **)slapi_ch_malloc((numbvals + 1) *
  664. sizeof(Slapi_Value *));
  665. i = 0;
  666. for (w = first_word((char *)slapi_value_get_string(val)); w != NULL;
  667. w = next_word(w)) {
  668. if ((c = phonetic(w)) != NULL) {
  669. (*ivals)[i] = slapi_value_new_string_passin(c);
  670. i++;
  671. }
  672. }
  673. (*ivals)[i] = NULL;
  674. if (i == 0) {
  675. slapi_ch_free((void **)ivals);
  676. return (0);
  677. }
  678. break;
  679. default:
  680. slapi_log_err(SLAPI_LOG_ERR, SYNTAX_PLUGIN_SUBSYSTEM,
  681. "string_assertion2keys_ava - Unknown ftype 0x%x\n",
  682. ftype);
  683. break;
  684. }
  685. return (0);
  686. }
  687. int
  688. string_assertion2keys_sub(
  689. Slapi_PBlock *pb,
  690. char *initial,
  691. char **any,
  692. char * final,
  693. Slapi_Value ***ivals,
  694. int syntax)
  695. {
  696. int nsubs, i, len;
  697. int initiallen = 0, finallen = 0;
  698. int *substrlens = NULL;
  699. int localsublens[3] = {SUBBEGIN, SUBMIDDLE, SUBEND}; /* default values */
  700. int maxsublen;
  701. char *comp_buf = NULL;
  702. /* altinit|any|final: store alt string from value_normalize_ext if any,
  703. * otherwise the original string. And use for the real job */
  704. char *altinit = NULL;
  705. char **altany = NULL;
  706. char *altfinal = NULL;
  707. /* oaltinit|any|final: prepared to free altinit|any|final if allocated in
  708. * value_normalize_ext */
  709. char *oaltinit = NULL;
  710. char **oaltany = NULL;
  711. char *oaltfinal = NULL;
  712. int anysize = 0;
  713. if (pb) {
  714. slapi_pblock_get(pb, SLAPI_SYNTAX_SUBSTRLENS, &substrlens);
  715. }
  716. if (NULL == substrlens) {
  717. substrlens = localsublens;
  718. }
  719. if (0 == substrlens[INDEX_SUBSTRBEGIN]) {
  720. substrlens[INDEX_SUBSTRBEGIN] = SUBBEGIN;
  721. }
  722. if (0 == substrlens[INDEX_SUBSTRMIDDLE]) {
  723. substrlens[INDEX_SUBSTRMIDDLE] = SUBMIDDLE;
  724. }
  725. if (0 == substrlens[INDEX_SUBSTREND]) {
  726. substrlens[INDEX_SUBSTREND] = SUBEND;
  727. }
  728. *ivals = NULL;
  729. /*
  730. * First figure out how many keys we will return. The answer is based
  731. * on the length of each assertion value. Since normalization may
  732. * reduce the length (such as when spaces are removed from space
  733. * insensitive strings), we call value_normalize_ext() before checking
  734. * the length.
  735. */
  736. nsubs = 0;
  737. if (initial != NULL) {
  738. /* 3rd arg: 0 - DO NOT trim leading blanks */
  739. value_normalize_ext(initial, syntax, 0, &altinit);
  740. oaltinit = altinit;
  741. if (NULL == altinit) {
  742. altinit = initial;
  743. }
  744. initiallen = strlen(altinit);
  745. if (initiallen > substrlens[INDEX_SUBSTRBEGIN] - 2) {
  746. nsubs += 1; /* for the initial begin string key */
  747. /* the rest of the sub keys are "any" keys for this case */
  748. if (initiallen >= substrlens[INDEX_SUBSTRMIDDLE]) {
  749. nsubs += initiallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  750. }
  751. } else {
  752. altinit = NULL; /* save some work later */
  753. }
  754. }
  755. for (i = 0; any != NULL && any[i] != NULL; i++) {
  756. anysize++;
  757. }
  758. altany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  759. oaltany = (char **)slapi_ch_calloc(anysize + 1, sizeof(char *));
  760. for (i = 0; any != NULL && any[i] != NULL; i++) {
  761. /* 3rd arg: 0 - DO NOT trim leading blanks */
  762. value_normalize_ext(any[i], syntax, 0, &altany[i]);
  763. if (NULL == altany[i]) {
  764. altany[i] = any[i];
  765. } else {
  766. oaltany[i] = altany[i];
  767. }
  768. len = strlen(altany[i]);
  769. if (len >= substrlens[INDEX_SUBSTRMIDDLE]) {
  770. nsubs += len - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  771. }
  772. }
  773. if (final != NULL) {
  774. /* 3rd arg: 0 - DO NOT trim leading blanks */
  775. value_normalize_ext(final, syntax, 0, &altfinal);
  776. oaltfinal = altfinal;
  777. if (NULL == altfinal) {
  778. altfinal = final;
  779. }
  780. finallen = strlen(altfinal);
  781. if (finallen > substrlens[INDEX_SUBSTREND] - 2) {
  782. nsubs += 1; /* for the final end string key */
  783. /* the rest of the sub keys are "any" keys for this case */
  784. if (finallen >= substrlens[INDEX_SUBSTRMIDDLE]) {
  785. nsubs += finallen - substrlens[INDEX_SUBSTRMIDDLE] + 1;
  786. }
  787. } else {
  788. altfinal = NULL; /* save some work later */
  789. }
  790. }
  791. if (nsubs == 0) { /* no keys to return */
  792. goto done;
  793. }
  794. /*
  795. * Next, allocated the ivals array and fill it in with the actual
  796. * keys. *ivals is a NULL terminated array of Slapi_Value pointers.
  797. */
  798. *ivals = (Slapi_Value **)slapi_ch_malloc((nsubs + 1) * sizeof(Slapi_Value *));
  799. maxsublen = MAX_VAL(substrlens[INDEX_SUBSTRBEGIN], substrlens[INDEX_SUBSTRMIDDLE]);
  800. maxsublen = MAX_VAL(maxsublen, substrlens[INDEX_SUBSTREND]);
  801. nsubs = 0;
  802. comp_buf = (char *)slapi_ch_malloc(maxsublen + 1);
  803. if (altinit != NULL) {
  804. substring_comp_keys(ivals, &nsubs, altinit, initiallen, '^', syntax,
  805. comp_buf, substrlens);
  806. }
  807. for (i = 0; altany != NULL && altany[i] != NULL; i++) {
  808. len = strlen(altany[i]);
  809. if (len < substrlens[INDEX_SUBSTRMIDDLE]) {
  810. continue;
  811. }
  812. substring_comp_keys(ivals, &nsubs, altany[i], len, 0, syntax,
  813. comp_buf, substrlens);
  814. }
  815. if (altfinal != NULL) {
  816. substring_comp_keys(ivals, &nsubs, altfinal, finallen, '$', syntax,
  817. comp_buf, substrlens);
  818. }
  819. (*ivals)[nsubs] = NULL;
  820. done:
  821. slapi_ch_free_string(&oaltinit);
  822. for (i = 0; altany != NULL && altany[i] != NULL; i++) {
  823. slapi_ch_free_string(&oaltany[i]);
  824. }
  825. slapi_ch_free((void **)&oaltany);
  826. slapi_ch_free_string(&oaltfinal);
  827. slapi_ch_free((void **)&altany);
  828. slapi_ch_free_string(&comp_buf);
  829. return (0);
  830. }
  831. static void
  832. substring_comp_keys(
  833. Slapi_Value ***ivals,
  834. int *nsubs,
  835. char *str,
  836. int lenstr,
  837. int prepost,
  838. int syntax __attribute__((unused)),
  839. char *comp_buf,
  840. int *substrlens)
  841. {
  842. int i, substrlen;
  843. char *p;
  844. PR_ASSERT(NULL != comp_buf);
  845. PR_ASSERT(NULL != substrlens);
  846. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "=> substring_comp_keys - (%s) %d\n",
  847. str, prepost);
  848. /* prepend ^ for initial substring */
  849. if (prepost == '^') {
  850. substrlen = substrlens[INDEX_SUBSTRBEGIN];
  851. comp_buf[0] = '^';
  852. for (i = 0; i < substrlen - 1; i++) {
  853. comp_buf[i + 1] = str[i];
  854. }
  855. comp_buf[substrlen] = '\0';
  856. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  857. (*nsubs)++;
  858. }
  859. substrlen = substrlens[INDEX_SUBSTRMIDDLE];
  860. for (p = str; p < (str + lenstr - substrlen + 1); p++) {
  861. for (i = 0; i < substrlen; i++) {
  862. comp_buf[i] = p[i];
  863. }
  864. comp_buf[substrlen] = '\0';
  865. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  866. (*nsubs)++;
  867. }
  868. if (prepost == '$') {
  869. substrlen = substrlens[INDEX_SUBSTREND];
  870. p = str + lenstr - substrlen + 1;
  871. for (i = 0; i < substrlen - 1; i++) {
  872. comp_buf[i] = p[i];
  873. }
  874. comp_buf[substrlen - 1] = '$';
  875. comp_buf[substrlen] = '\0';
  876. (*ivals)[*nsubs] = slapi_value_new_string(comp_buf);
  877. (*nsubs)++;
  878. }
  879. slapi_log_err(SLAPI_LOG_TRACE, SYNTAX_PLUGIN_SUBSYSTEM, "<= substring_comp_keys\n");
  880. }