plugin_syntax.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. /*
  13. * plugin_syntax.c - routines for calling syntax plugins
  14. */
  15. #include "slap.h"
  16. struct slapdplugin *
  17. plugin_syntax_find( const char *nameoroid )
  18. {
  19. struct slapdplugin *pi;
  20. /* LDAPDebug( LDAP_DEBUG_FILTER, "=> plugin_syntax_find (%s)\n", nameoroid, 0, 0 ); */
  21. for ( pi = get_plugin_list(PLUGIN_LIST_SYNTAX); pi != NULL; pi = pi->plg_next ) {
  22. if ( charray_inlist( pi->plg_syntax_names, (char *)nameoroid ) ) {
  23. break;
  24. }
  25. }
  26. /* LDAPDebug( LDAP_DEBUG_FILTER, "<= plugin_syntax_find %d\n", pi, 0, 0 ); */
  27. return ( pi );
  28. }
  29. /*
  30. * Enumerate all the syntax plugins, calling (*sef)() for each one.
  31. */
  32. void
  33. plugin_syntax_enumerate( SyntaxEnumFunc sef, void *arg )
  34. {
  35. struct slapdplugin *pi;
  36. for ( pi = get_plugin_list(PLUGIN_LIST_SYNTAX); pi != NULL;
  37. pi = pi->plg_next ) {
  38. (*sef)( pi->plg_syntax_names, &pi->plg_desc, arg );
  39. }
  40. }
  41. struct slapdplugin *
  42. slapi_get_global_syntax_plugins()
  43. {
  44. return get_plugin_list(PLUGIN_LIST_SYNTAX);
  45. }
  46. char *
  47. plugin_syntax2oid( struct slapdplugin *pi )
  48. {
  49. LDAPDebug(LDAP_DEBUG_ANY,
  50. "the function plugin_syntax2oid is deprecated - please use attr_get_syntax_oid instead\n", 0, 0, 0);
  51. PR_ASSERT(0);
  52. return( NULL );
  53. }
  54. int
  55. plugin_call_syntax_get_compare_fn(
  56. void *vpi,
  57. value_compare_fn_type *compare_fn
  58. )
  59. {
  60. LDAPDebug(LDAP_DEBUG_ANY,
  61. "the function plugin_call_syntax_get_compare_fn is deprecated - please use attr_get_value_cmp_fn instead\n", 0, 0, 0);
  62. PR_ASSERT(0);
  63. return( 0 );
  64. }
  65. int
  66. plugin_call_syntax_filter_ava(
  67. const Slapi_Attr *a,
  68. int ftype,
  69. struct ava *ava
  70. )
  71. {
  72. return(plugin_call_syntax_filter_ava_sv(a,ftype,ava,NULL,0 /*Present*/));
  73. }
  74. int
  75. plugin_call_syntax_filter_ava_sv(
  76. const Slapi_Attr *a,
  77. int ftype,
  78. struct ava *ava,
  79. Slapi_Value **retVal,
  80. int useDeletedValues
  81. )
  82. {
  83. int rc;
  84. Slapi_PBlock pipb;
  85. IFP ava_fn = NULL;
  86. LDAPDebug( LDAP_DEBUG_FILTER,
  87. "=> plugin_call_syntax_filter_ava %s=%s\n", ava->ava_type,
  88. ava->ava_value.bv_val, 0 );
  89. if ( ( a->a_mr_eq_plugin == NULL ) && ( a->a_mr_ord_plugin == NULL ) && ( a->a_plugin == NULL ) ) {
  90. /* could be lazy plugin initialization, get it now */
  91. Slapi_Attr *t = (Slapi_Attr *)a;
  92. slapi_attr_init_syntax(t);
  93. }
  94. if ( ( a->a_mr_eq_plugin == NULL ) && ( a->a_mr_ord_plugin == NULL ) && ( a->a_plugin == NULL ) ) {
  95. LDAPDebug( LDAP_DEBUG_FILTER,
  96. "<= plugin_call_syntax_filter_ava no plugin for attr (%s)\n",
  97. a->a_type, 0, 0 );
  98. return( LDAP_PROTOCOL_ERROR ); /* syntax unkonwn */
  99. }
  100. pblock_init( &pipb );
  101. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_plugin );
  102. if (ava->ava_private) {
  103. int filter_normalized = 0;
  104. int f_flags = 0;
  105. f_flags = *(int *)ava->ava_private;
  106. filter_normalized = f_flags | SLAPI_FILTER_NORMALIZED_VALUE;
  107. slapi_pblock_set( &pipb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  108. }
  109. rc = -1; /* does not match by default */
  110. switch ( ftype ) {
  111. case LDAP_FILTER_GE:
  112. case LDAP_FILTER_LE:
  113. if ((a->a_mr_ord_plugin == NULL) &&
  114. ((a->a_plugin->plg_syntax_flags &
  115. SLAPI_PLUGIN_SYNTAX_FLAG_ORDERING) == 0)) {
  116. LDAPDebug( LDAP_DEBUG_FILTER,
  117. "<= plugin_call_syntax_filter_ava: attr (%s) has no ordering matching rule, and syntax does not define a compare function\n",
  118. a->a_type, 0, 0 );
  119. rc = LDAP_PROTOCOL_ERROR;
  120. break;
  121. }
  122. /* if the attribute has an ordering matching rule plugin, use that,
  123. otherwise, just use the syntax plugin */
  124. if (a->a_mr_ord_plugin != NULL) {
  125. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_mr_ord_plugin );
  126. ava_fn = a->a_mr_ord_plugin->plg_mr_filter_ava;
  127. } else {
  128. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_plugin );
  129. ava_fn = a->a_plugin->plg_syntax_filter_ava;
  130. }
  131. /* FALL */
  132. case LDAP_FILTER_EQUALITY:
  133. case LDAP_FILTER_APPROX:
  134. if (NULL == ava_fn) {
  135. /* if we have an equality matching rule plugin, use that,
  136. otherwise, just use the syntax plugin */
  137. if (a->a_mr_eq_plugin) {
  138. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_mr_eq_plugin );
  139. ava_fn = a->a_mr_eq_plugin->plg_mr_filter_ava;
  140. } else {
  141. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_plugin );
  142. ava_fn = a->a_plugin->plg_syntax_filter_ava;
  143. }
  144. }
  145. if ( ava_fn != NULL ) {
  146. /* JCM - Maybe the plugin should use the attr value iterator too... */
  147. Slapi_Value **va;
  148. if(useDeletedValues) {
  149. va= valueset_get_valuearray(&a->a_deleted_values);
  150. } else {
  151. va= valueset_get_valuearray(&a->a_present_values);
  152. }
  153. if(va!=NULL) {
  154. rc = (*ava_fn)( &pipb, &ava->ava_value, va, ftype, retVal );
  155. }
  156. } else {
  157. LDAPDebug( LDAP_DEBUG_FILTER,
  158. "<= plugin_call_syntax_filter_ava: attr (%s) has no ava filter function\n",
  159. a->a_type, 0, 0 );
  160. }
  161. break;
  162. default:
  163. LDAPDebug( LDAP_DEBUG_ANY, "plugin_call_syntax_filter_ava: "
  164. "unknown filter type %d\n", ftype, 0, 0 );
  165. rc = LDAP_PROTOCOL_ERROR;
  166. break;
  167. }
  168. LDAPDebug( LDAP_DEBUG_FILTER,
  169. "<= plugin_call_syntax_filter_ava %d\n", rc, 0, 0 );
  170. return( rc );
  171. }
  172. int
  173. plugin_call_syntax_filter_sub(
  174. Slapi_PBlock *pb,
  175. Slapi_Attr *a,
  176. struct subfilt *fsub
  177. )
  178. {
  179. return(plugin_call_syntax_filter_sub_sv(pb,a,fsub));
  180. }
  181. int
  182. plugin_call_syntax_filter_sub_sv(
  183. Slapi_PBlock *pb,
  184. Slapi_Attr *a,
  185. struct subfilt *fsub
  186. )
  187. {
  188. Slapi_PBlock pipb;
  189. int rc;
  190. IFP sub_fn = NULL;
  191. int filter_normalized = 0;
  192. LDAPDebug( LDAP_DEBUG_FILTER,
  193. "=> plugin_call_syntax_filter_sub_sv\n", 0, 0, 0 );
  194. if ( ( a->a_mr_sub_plugin == NULL ) && ( a->a_plugin == NULL ) ) {
  195. /* could be lazy plugin initialization, get it now */
  196. slapi_attr_init_syntax(a);
  197. }
  198. if ( ( a->a_mr_sub_plugin == NULL ) && ( a->a_plugin == NULL ) ) {
  199. LDAPDebug( LDAP_DEBUG_FILTER,
  200. "<= plugin_call_syntax_filter_sub_sv attribute (%s) has no substring matching rule or syntax plugin\n",
  201. a->a_type, 0, 0 );
  202. return( -1 ); /* syntax unkonwn - does not match */
  203. }
  204. pblock_init( &pipb );
  205. if (pb) {
  206. slapi_pblock_get( pb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  207. slapi_pblock_set( &pipb, SLAPI_PLUGIN_SYNTAX_FILTER_NORMALIZED, &filter_normalized );
  208. }
  209. slapi_pblock_set( &pipb, SLAPI_PLUGIN_SYNTAX_FILTER_DATA, fsub );
  210. /* use the substr matching rule plugin if available, otherwise, use
  211. the syntax plugin */
  212. if (a->a_mr_sub_plugin) {
  213. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_mr_sub_plugin );
  214. sub_fn = a->a_mr_sub_plugin->plg_mr_filter_sub;
  215. } else {
  216. slapi_pblock_set( &pipb, SLAPI_PLUGIN, (void *) a->a_plugin );
  217. sub_fn = a->a_plugin->plg_syntax_filter_sub;
  218. }
  219. if ( sub_fn != NULL )
  220. {
  221. Slapi_Value **va= valueset_get_valuearray(&a->a_present_values);
  222. if (pb)
  223. {
  224. Operation *op = NULL;
  225. /* to pass SLAPI_SEARCH_TIMELIMIT & SLAPI_OPINITATED_TIME */
  226. slapi_pblock_get( pb, SLAPI_OPERATION, &op );
  227. slapi_pblock_set( &pipb, SLAPI_OPERATION, op );
  228. }
  229. rc = (*sub_fn)( &pipb, fsub->sf_initial, fsub->sf_any, fsub->sf_final, va);
  230. } else {
  231. rc = -1;
  232. }
  233. LDAPDebug( LDAP_DEBUG_FILTER, "<= plugin_call_syntax_filter_sub_sv %d\n",
  234. rc, 0, 0 );
  235. return( rc );
  236. }
  237. /* Checks if the DN string is valid according to the Distinguished Name
  238. * syntax. Setting override to 1 will force syntax checking to be performed,
  239. * even if syntax checking is disabled in the config. Setting override to 0
  240. * will obey the config settings.
  241. *
  242. * Returns 1 if there is a syntax violation and sets the error message
  243. * appropriately. Returns 0 if everything checks out fine.
  244. */
  245. int
  246. slapi_dn_syntax_check(
  247. Slapi_PBlock *pb, const char *dn, int override
  248. )
  249. {
  250. int ret = 0;
  251. int is_replicated_operation = 0;
  252. int syntaxcheck = config_get_syntaxcheck();
  253. int syntaxlogging = config_get_syntaxlogging();
  254. char errtext[ BUFSIZ ];
  255. char *errp = &errtext[0];
  256. struct slapdplugin *dn_plugin = NULL;
  257. struct berval dn_bval = {0};
  258. if (pb != NULL) {
  259. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  260. }
  261. /* If syntax checking and logging are off, or if this is a
  262. * replicated operation, just return that the syntax is OK. */
  263. if (((syntaxcheck == 0) && (syntaxlogging == 0) && (override == 0)) ||
  264. is_replicated_operation) {
  265. goto exit;
  266. }
  267. /* Locate the dn syntax plugin. */
  268. slapi_attr_type2plugin("distinguishedName", (void **)&dn_plugin);
  269. /* Assume the value is valid if we don't find a dn validate function */
  270. if (dn_plugin && dn_plugin->plg_syntax_validate != NULL) {
  271. /* Create a berval to pass to the validate function. */
  272. if (dn) {
  273. dn_bval.bv_val = (char *)dn;
  274. dn_bval.bv_len = strlen(dn);
  275. /* Validate the value. */
  276. if (dn_plugin->plg_syntax_validate(&dn_bval) != 0) {
  277. if (syntaxlogging) {
  278. slapi_log_error( SLAPI_LOG_FATAL, "Syntax Check",
  279. "DN value (%s) invalid per syntax\n", dn);
  280. }
  281. if (syntaxcheck || override) {
  282. if (pb) {
  283. errp += PR_snprintf( errp, sizeof(errtext),
  284. "DN value invalid per syntax\n" );
  285. }
  286. ret = 1;
  287. }
  288. }
  289. }
  290. }
  291. /* See if we need to set the error text in the pblock. */
  292. if (pb && errp != &errtext[0]) {
  293. /* SLAPI_PB_RESULT_TEXT duplicates the text in slapi_pblock_set */
  294. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  295. }
  296. exit:
  297. return( ret );
  298. }
  299. /* Checks if the values of all attributes in an entry are valid for the
  300. * syntax specified for the attribute in question. Setting override to
  301. * 1 will force syntax checking to be performed, even if syntax checking
  302. * is disabled in the config. Setting override to 0 will obey the config
  303. * settings.
  304. *
  305. * Returns 1 if there is a syntax violation and sets the error message
  306. * appropriately. Returns 0 if everything checks out fine.
  307. *
  308. * Note: this function allows NULL pb. If NULL, is_replicated_operation
  309. * will not checked and error message will not be generated and returned.
  310. */
  311. int
  312. slapi_entry_syntax_check(
  313. Slapi_PBlock *pb, Slapi_Entry *e, int override
  314. )
  315. {
  316. int ret = 0;
  317. int i = 0;
  318. int is_replicated_operation = 0;
  319. int syntaxcheck = config_get_syntaxcheck();
  320. int syntaxlogging = config_get_syntaxlogging();
  321. Slapi_Attr *prevattr = NULL;
  322. Slapi_Attr *a = NULL;
  323. char errtext[ BUFSIZ ];
  324. char *errp = &errtext[0];
  325. size_t err_remaining = sizeof(errtext);
  326. if (pb != NULL) {
  327. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  328. }
  329. /* If syntax checking and logging are off, or if this is a
  330. * replicated operation, just return that the syntax is OK. */
  331. if (((syntaxcheck == 0) && (syntaxlogging == 0) && (override == 0)) ||
  332. is_replicated_operation) {
  333. goto exit;
  334. }
  335. i = slapi_entry_first_attr(e, &a);
  336. if ( a && ( a->a_plugin == NULL ) ) {
  337. /* could be lazy plugin initialization, get it now */
  338. slapi_attr_init_syntax(a);
  339. }
  340. while ((-1 != i) && a && (a->a_plugin != NULL)) {
  341. /* If no validate function is available for this type, just
  342. * assume that the value is valid. */
  343. if ( a->a_plugin->plg_syntax_validate != NULL ) {
  344. int numvals = 0;
  345. slapi_attr_get_numvalues(a, &numvals);
  346. if ( numvals > 0 ) {
  347. Slapi_Value *val = NULL;
  348. const struct berval *bval = NULL;
  349. int hint = slapi_attr_first_value(a, &val);
  350. /* iterate through each value to check if it's valid */
  351. while (val != NULL) {
  352. bval = slapi_value_get_berval(val);
  353. if ((a->a_plugin->plg_syntax_validate( bval )) != 0) {
  354. if (syntaxlogging) {
  355. slapi_log_error( SLAPI_LOG_FATAL, "Syntax Check",
  356. "\"%s\": (%s) value #%d invalid per syntax\n",
  357. slapi_entry_get_dn(e), a->a_type, hint );
  358. }
  359. if (syntaxcheck || override) {
  360. if (pb) {
  361. /* Append new text to any existing text. */
  362. errp += PR_snprintf( errp, err_remaining,
  363. "%s: value #%d invalid per syntax\n", a->a_type, hint );
  364. err_remaining -= errp - &errtext[0];
  365. }
  366. ret = 1;
  367. }
  368. }
  369. hint = slapi_attr_next_value(a, hint, &val);
  370. }
  371. }
  372. }
  373. prevattr = a;
  374. i = slapi_entry_next_attr(e, prevattr, &a);
  375. if ( a && ( a->a_plugin == NULL ) ) {
  376. /* could be lazy plugin initialization, get it now */
  377. slapi_attr_init_syntax(a);
  378. }
  379. }
  380. /* See if we need to set the error text in the pblock. */
  381. if (pb && (errp != &errtext[0])) { /* Check pb for coverity */
  382. /* SLAPI_PB_RESULT_TEXT duplicates the text in slapi_pblock_set */
  383. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  384. }
  385. exit:
  386. return( ret );
  387. }
  388. /* Checks if the values of all attributes being added in a Slapi_Mods
  389. * are valid for the syntax specified for the attribute in question.
  390. * The new values in an add or replace modify operation and the newrdn
  391. * value for a modrdn operation will be checked.
  392. * Returns 1 if there is a syntax violation and sets the error message
  393. * appropriately. Returns 0 if everything checks out fine.
  394. *
  395. * Note: this function allows NULL pb. If NULL, is_replicated_operation
  396. * will not checked and error message will not be generated and returned.
  397. */
  398. int
  399. slapi_mods_syntax_check(
  400. Slapi_PBlock *pb, LDAPMod **mods, int override
  401. )
  402. {
  403. int ret = 0;
  404. int i, j = 0;
  405. int is_replicated_operation = 0;
  406. int syntaxcheck = config_get_syntaxcheck();
  407. int syntaxlogging = config_get_syntaxlogging();
  408. char errtext[ BUFSIZ ];
  409. char *errp = &errtext[0];
  410. size_t err_remaining = sizeof(errtext);
  411. const char *dn = NULL;
  412. Slapi_DN *sdn = NULL;
  413. LDAPMod *mod = NULL;
  414. if (mods == NULL) {
  415. ret = 1;
  416. goto exit;
  417. }
  418. if (pb != NULL) {
  419. slapi_pblock_get(pb, SLAPI_IS_REPLICATED_OPERATION, &is_replicated_operation);
  420. slapi_pblock_get(pb, SLAPI_TARGET_SDN, &sdn);
  421. dn = slapi_sdn_get_dn(sdn);
  422. }
  423. /* If syntax checking and logging are off, or if this is a
  424. * replicated operation, just return that the syntax is OK. */
  425. if (((syntaxcheck == 0) && (syntaxlogging == 0) && (override == 0)) ||
  426. is_replicated_operation) {
  427. goto exit;
  428. }
  429. /* Loop through mods */
  430. for (i = 0; mods[i] != NULL; i++) {
  431. mod = mods[i];
  432. /* We only care about replace and add modify operations that
  433. * are truly adding new values to the entry. */
  434. if ((SLAPI_IS_MOD_REPLACE(mod->mod_op) || SLAPI_IS_MOD_ADD(mod->mod_op)) &&
  435. (mod->mod_bvalues != NULL)) {
  436. struct slapdplugin *syntax_plugin = NULL;
  437. /* Find the plug-in for this type, then call it's
  438. * validate function.*/
  439. slapi_attr_type2plugin(mod->mod_type, (void **)&syntax_plugin);
  440. if ((syntax_plugin != NULL) && (syntax_plugin->plg_syntax_validate != NULL)) {
  441. /* Loop through the values and validate each one */
  442. for (j = 0; mod->mod_bvalues[j] != NULL; j++) {
  443. if (syntax_plugin->plg_syntax_validate(mod->mod_bvalues[j]) != 0) {
  444. if (syntaxlogging) {
  445. slapi_log_error( SLAPI_LOG_FATAL, "Syntax Check", "\"%s\": (%s) value #%d invalid per syntax\n",
  446. dn ? dn : "NULL", mod->mod_type, j );
  447. }
  448. if (syntaxcheck || override) {
  449. if (pb) {
  450. /* Append new text to any existing text. */
  451. errp += PR_snprintf( errp, err_remaining,
  452. "%s: value #%d invalid per syntax\n", mod->mod_type, j );
  453. err_remaining -= errp - &errtext[0];
  454. }
  455. ret = 1;
  456. }
  457. }
  458. }
  459. }
  460. }
  461. }
  462. /* See if we need to set the error text in the pblock. */
  463. if (pb && (errp != &errtext[0])) { /* Check pb for coverity */
  464. /* SLAPI_PB_RESULT_TEXT duplicates the text in slapi_pblock_set */
  465. slapi_pblock_set( pb, SLAPI_PB_RESULT_TEXT, errtext );
  466. }
  467. exit:
  468. return( ret );
  469. }
  470. SLAPI_DEPRECATED int
  471. slapi_call_syntax_values2keys( /* JCM SLOW FUNCTION */
  472. void *vpi,
  473. struct berval **vals,
  474. struct berval ***ivals,
  475. int ftype
  476. )
  477. {
  478. int rc;
  479. Slapi_Value **svin= NULL;
  480. Slapi_Value **svout= NULL;
  481. valuearray_init_bervalarray(vals,&svin); /* JCM SLOW FUNCTION */
  482. rc= slapi_call_syntax_values2keys_sv(vpi,svin,&svout,ftype);
  483. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  484. valuearray_free(&svout);
  485. valuearray_free(&svin);
  486. return rc;
  487. }
  488. int
  489. slapi_call_syntax_values2keys_sv(
  490. void *vpi,
  491. Slapi_Value **vals,
  492. Slapi_Value ***ivals,
  493. int ftype
  494. )
  495. {
  496. int rc;
  497. Slapi_PBlock pipb;
  498. struct slapdplugin *pi = vpi;
  499. LDAPDebug( LDAP_DEBUG_FILTER, "=> slapi_call_syntax_values2keys\n",
  500. 0, 0, 0 );
  501. pblock_init( &pipb );
  502. slapi_pblock_set( &pipb, SLAPI_PLUGIN, vpi );
  503. *ivals = NULL;
  504. rc = -1; /* means no values2keys function */
  505. if ( pi != NULL && pi->plg_syntax_values2keys != NULL ) {
  506. rc = pi->plg_syntax_values2keys( &pipb, vals, ivals, ftype );
  507. }
  508. LDAPDebug( LDAP_DEBUG_FILTER,
  509. "<= slapi_call_syntax_values2keys %d\n", rc, 0, 0 );
  510. return( rc );
  511. }
  512. int
  513. slapi_attr_values2keys_sv_pb(
  514. const Slapi_Attr *sattr,
  515. Slapi_Value **vals,
  516. Slapi_Value ***ivals,
  517. int ftype,
  518. Slapi_PBlock *pb
  519. )
  520. {
  521. int rc;
  522. struct slapdplugin *pi = NULL;
  523. IFP v2k_fn = NULL;
  524. LDAPDebug( LDAP_DEBUG_FILTER, "=> slapi_attr_values2keys_sv\n",
  525. 0, 0, 0 );
  526. if ( ( sattr->a_plugin == NULL ) ) {
  527. /* could be lazy plugin initialization, get it now */
  528. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  529. }
  530. switch (ftype) {
  531. case LDAP_FILTER_EQUALITY:
  532. case LDAP_FILTER_APPROX:
  533. if (sattr->a_mr_eq_plugin) {
  534. pi = sattr->a_mr_eq_plugin;
  535. v2k_fn = sattr->a_mr_eq_plugin->plg_mr_values2keys;
  536. } else if (sattr->a_plugin) {
  537. pi = sattr->a_plugin;
  538. v2k_fn = sattr->a_plugin->plg_syntax_values2keys;
  539. }
  540. break;
  541. case LDAP_FILTER_SUBSTRINGS:
  542. if (sattr->a_mr_sub_plugin) {
  543. pi = sattr->a_mr_sub_plugin;
  544. v2k_fn = sattr->a_mr_sub_plugin->plg_mr_values2keys;
  545. } else if (sattr->a_plugin) {
  546. pi = sattr->a_plugin;
  547. v2k_fn = sattr->a_plugin->plg_syntax_values2keys;
  548. }
  549. break;
  550. default:
  551. LDAPDebug( LDAP_DEBUG_ANY, "<= slapi_attr_values2keys_sv: ERROR: unsupported filter type %d\n",
  552. ftype, 0, 0 );
  553. rc = LDAP_PROTOCOL_ERROR;
  554. goto done;
  555. }
  556. slapi_pblock_set( pb, SLAPI_PLUGIN, pi );
  557. *ivals = NULL;
  558. rc = -1; /* means no values2keys function */
  559. if ( ( pi != NULL ) && ( v2k_fn != NULL ) ) {
  560. rc = (*v2k_fn)( pb, vals, ivals, ftype );
  561. }
  562. done:
  563. LDAPDebug( LDAP_DEBUG_FILTER,
  564. "<= slapi_call_syntax_values2keys %d\n", rc, 0, 0 );
  565. return( rc );
  566. }
  567. int
  568. slapi_attr_values2keys_sv(
  569. const Slapi_Attr *sattr,
  570. Slapi_Value **vals,
  571. Slapi_Value ***ivals,
  572. int ftype
  573. )
  574. {
  575. Slapi_PBlock pb;
  576. pblock_init(&pb);
  577. return slapi_attr_values2keys_sv_pb(sattr, vals, ivals, ftype, &pb);
  578. }
  579. SLAPI_DEPRECATED int
  580. slapi_attr_values2keys( /* JCM SLOW FUNCTION */
  581. const Slapi_Attr *sattr,
  582. struct berval **vals,
  583. struct berval ***ivals,
  584. int ftype
  585. )
  586. {
  587. int rc;
  588. Slapi_Value **svin= NULL;
  589. Slapi_Value **svout= NULL;
  590. valuearray_init_bervalarray(vals,&svin); /* JCM SLOW FUNCTION */
  591. rc= slapi_attr_values2keys_sv(sattr,svin,&svout,ftype);
  592. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  593. valuearray_free(&svout);
  594. valuearray_free(&svin);
  595. return rc;
  596. }
  597. /*
  598. * almost identical to slapi_call_syntax_values2keys_sv except accepting
  599. * pblock to pass some info such as substrlen.
  600. */
  601. int
  602. slapi_call_syntax_values2keys_sv_pb(
  603. void *vpi,
  604. Slapi_Value **vals,
  605. Slapi_Value ***ivals,
  606. int ftype,
  607. Slapi_PBlock *pb
  608. )
  609. {
  610. int rc;
  611. struct slapdplugin *pi = vpi;
  612. LDAPDebug( LDAP_DEBUG_FILTER, "=> slapi_call_syntax_values2keys\n",
  613. 0, 0, 0 );
  614. slapi_pblock_set( pb, SLAPI_PLUGIN, vpi );
  615. *ivals = NULL;
  616. rc = -1; /* means no values2keys function */
  617. if ( pi != NULL && pi->plg_syntax_values2keys != NULL ) {
  618. rc = pi->plg_syntax_values2keys( pb, vals, ivals, ftype );
  619. }
  620. LDAPDebug( LDAP_DEBUG_FILTER,
  621. "<= slapi_call_syntax_values2keys %d\n", rc, 0, 0 );
  622. return( rc );
  623. }
  624. SLAPI_DEPRECATED int
  625. slapi_call_syntax_assertion2keys_ava( /* JCM SLOW FUNCTION */
  626. void *vpi,
  627. struct berval *val,
  628. struct berval ***ivals,
  629. int ftype
  630. )
  631. {
  632. int rc;
  633. Slapi_Value svin;
  634. Slapi_Value **svout= NULL;
  635. slapi_value_init_berval(&svin, val);
  636. rc= slapi_call_syntax_assertion2keys_ava_sv(vpi,&svin,&svout,ftype);
  637. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  638. valuearray_free(&svout);
  639. value_done(&svin);
  640. return rc;
  641. }
  642. SLAPI_DEPRECATED int
  643. slapi_call_syntax_assertion2keys_ava_sv(
  644. void *vpi,
  645. Slapi_Value *val,
  646. Slapi_Value ***ivals,
  647. int ftype
  648. )
  649. {
  650. int rc;
  651. Slapi_PBlock pipb;
  652. struct slapdplugin *pi = vpi;
  653. LDAPDebug( LDAP_DEBUG_FILTER,
  654. "=> slapi_call_syntax_assertion2keys_ava\n", 0, 0, 0 );
  655. pblock_init( &pipb );
  656. slapi_pblock_set( &pipb, SLAPI_PLUGIN, vpi );
  657. rc = -1; /* means no assertion2keys function */
  658. if ( pi->plg_syntax_assertion2keys_ava != NULL ) {
  659. rc = pi->plg_syntax_assertion2keys_ava( &pipb, val, ivals, ftype );
  660. }
  661. LDAPDebug( LDAP_DEBUG_FILTER,
  662. "<= slapi_call_syntax_assertion2keys_ava %d\n", rc, 0, 0 );
  663. return( rc );
  664. }
  665. int
  666. slapi_attr_assertion2keys_ava_sv(
  667. const Slapi_Attr *sattr,
  668. Slapi_Value *val,
  669. Slapi_Value ***ivals,
  670. int ftype
  671. )
  672. {
  673. int rc;
  674. Slapi_PBlock pipb;
  675. struct slapdplugin *pi = NULL;
  676. IFP a2k_fn = NULL;
  677. LDAPDebug( LDAP_DEBUG_FILTER,
  678. "=> slapi_attr_assertion2keys_ava_sv\n", 0, 0, 0 );
  679. if ( ( sattr->a_plugin == NULL ) ) {
  680. /* could be lazy plugin initialization, get it now */
  681. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  682. }
  683. switch (ftype) {
  684. case LDAP_FILTER_EQUALITY:
  685. case LDAP_FILTER_APPROX:
  686. case LDAP_FILTER_EQUALITY_FAST:
  687. if (sattr->a_mr_eq_plugin) {
  688. pi = sattr->a_mr_eq_plugin;
  689. a2k_fn = sattr->a_mr_eq_plugin->plg_mr_assertion2keys_ava;
  690. } else if (sattr->a_plugin) {
  691. pi = sattr->a_plugin;
  692. a2k_fn = sattr->a_plugin->plg_syntax_assertion2keys_ava;
  693. }
  694. break;
  695. default:
  696. LDAPDebug( LDAP_DEBUG_ANY, "<= slapi_attr_assertion2keys_ava_sv: ERROR: unsupported filter type %d\n",
  697. ftype, 0, 0 );
  698. rc = LDAP_PROTOCOL_ERROR;
  699. goto done;
  700. }
  701. pblock_init( &pipb );
  702. slapi_pblock_set( &pipb, SLAPI_PLUGIN, pi );
  703. rc = -1; /* means no assertion2keys function */
  704. if ( a2k_fn != NULL ) {
  705. rc = (*a2k_fn)( &pipb, val, ivals, ftype );
  706. }
  707. done:
  708. LDAPDebug( LDAP_DEBUG_FILTER,
  709. "<= slapi_attr_assertion2keys_ava_sv %d\n", rc, 0, 0 );
  710. return( rc );
  711. }
  712. SLAPI_DEPRECATED int
  713. slapi_attr_assertion2keys_ava( /* JCM SLOW FUNCTION */
  714. const Slapi_Attr *sattr,
  715. struct berval *val,
  716. struct berval ***ivals,
  717. int ftype
  718. )
  719. {
  720. int rc;
  721. Slapi_Value svin;
  722. Slapi_Value **svout= NULL;
  723. slapi_value_init_berval(&svin, val);
  724. rc= slapi_attr_assertion2keys_ava_sv(sattr,&svin,&svout,ftype);
  725. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  726. valuearray_free(&svout);
  727. value_done(&svin);
  728. return rc;
  729. }
  730. SLAPI_DEPRECATED int
  731. slapi_call_syntax_assertion2keys_sub( /* JCM SLOW FUNCTION */
  732. void *vpi,
  733. char *initial,
  734. char **any,
  735. char *final,
  736. struct berval ***ivals
  737. )
  738. {
  739. int rc;
  740. Slapi_Value **svout= NULL;
  741. rc= slapi_call_syntax_assertion2keys_sub_sv(vpi,initial,any,final,&svout);
  742. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  743. valuearray_free(&svout);
  744. return rc;
  745. }
  746. int
  747. slapi_call_syntax_assertion2keys_sub_sv(
  748. void *vpi,
  749. char *initial,
  750. char **any,
  751. char *final,
  752. Slapi_Value ***ivals
  753. )
  754. {
  755. int rc;
  756. Slapi_PBlock pipb;
  757. struct slapdplugin *pi = vpi;
  758. LDAPDebug( LDAP_DEBUG_FILTER,
  759. "=> slapi_call_syntax_assertion2keys_sub\n", 0, 0, 0 );
  760. pblock_init( &pipb );
  761. slapi_pblock_set( &pipb, SLAPI_PLUGIN, vpi );
  762. rc = -1; /* means no assertion2keys function */
  763. *ivals = NULL;
  764. if ( pi->plg_syntax_assertion2keys_sub != NULL ) {
  765. rc = pi->plg_syntax_assertion2keys_sub( &pipb, initial, any,
  766. final, ivals );
  767. }
  768. LDAPDebug( LDAP_DEBUG_FILTER,
  769. "<= slapi_call_syntax_assertion2keys_sub %d\n", rc, 0, 0 );
  770. return( rc );
  771. }
  772. int
  773. slapi_attr_assertion2keys_sub_sv(
  774. const Slapi_Attr *sattr,
  775. char *initial,
  776. char **any,
  777. char *final,
  778. Slapi_Value ***ivals
  779. )
  780. {
  781. return slapi_attr_assertion2keys_sub_sv_pb(NULL, sattr, initial, any, final, ivals);
  782. }
  783. int
  784. slapi_attr_assertion2keys_sub_sv_pb(
  785. Slapi_PBlock *pb,
  786. const Slapi_Attr *sattr,
  787. char *initial,
  788. char **any,
  789. char *final,
  790. Slapi_Value ***ivals
  791. )
  792. {
  793. int rc;
  794. Slapi_PBlock pipb;
  795. struct slapdplugin *pi = NULL;
  796. struct slapdplugin *origpi = NULL;
  797. IFP a2k_fn = NULL;
  798. LDAPDebug( LDAP_DEBUG_FILTER,
  799. "=> slapi_attr_assertion2keys_sub_sv\n", 0, 0, 0 );
  800. if ( ( sattr->a_plugin == NULL ) ) {
  801. /* could be lazy plugin initialization, get it now */
  802. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  803. }
  804. if (sattr->a_mr_sub_plugin) {
  805. pi = sattr->a_mr_sub_plugin;
  806. a2k_fn = sattr->a_mr_sub_plugin->plg_mr_assertion2keys_sub;
  807. } else if (sattr->a_plugin) {
  808. pi = sattr->a_plugin;
  809. a2k_fn = sattr->a_plugin->plg_syntax_assertion2keys_sub;
  810. }
  811. if (NULL == pb) {
  812. pblock_init( &pipb );
  813. pb = &pipb;
  814. } else {
  815. /* back up the original slapdplugin if any */
  816. slapi_pblock_get(pb, SLAPI_PLUGIN, &origpi);
  817. }
  818. slapi_pblock_set(pb, SLAPI_PLUGIN, pi);
  819. rc = -1; /* means no assertion2keys function */
  820. *ivals = NULL;
  821. if ( a2k_fn != NULL ) {
  822. rc = (*a2k_fn)( pb, initial, any, final, ivals );
  823. }
  824. if (pb != &pipb) {
  825. /* restore the original slapdplugin if pb is not local. */
  826. slapi_pblock_set(pb, SLAPI_PLUGIN, origpi);
  827. }
  828. LDAPDebug( LDAP_DEBUG_FILTER,
  829. "<= slapi_attr_assertion2keys_sub_sv %d\n", rc, 0, 0 );
  830. return( rc );
  831. }
  832. SLAPI_DEPRECATED int
  833. slapi_attr_assertion2keys_sub( /* JCM SLOW FUNCTION */
  834. const Slapi_Attr *sattr,
  835. char *initial,
  836. char **any,
  837. char *final,
  838. struct berval ***ivals
  839. )
  840. {
  841. int rc;
  842. Slapi_Value **svout= NULL;
  843. rc= slapi_attr_assertion2keys_sub_sv(sattr,initial,any,final,&svout);
  844. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  845. valuearray_free(&svout);
  846. return rc;
  847. }
  848. void
  849. slapi_attr_value_normalize_ext(
  850. Slapi_PBlock *pb,
  851. const Slapi_Attr *sattr, /* if sattr is NULL, type must be attr type name */
  852. const char *type,
  853. char *val,
  854. int trim_spaces,
  855. char **retval,
  856. unsigned long filter_type
  857. )
  858. {
  859. Slapi_Attr myattr;
  860. VFPV norm_fn = NULL;
  861. if (!sattr) {
  862. sattr = slapi_attr_init(&myattr, type);
  863. if(!sattr){
  864. attr_done(&myattr);
  865. return;
  866. }
  867. }
  868. if ( ( sattr->a_plugin == NULL ) ) {
  869. /* could be lazy plugin initialization, get it now */
  870. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  871. }
  872. /* use the filter type to determine which matching rule to use */
  873. switch (filter_type) {
  874. case LDAP_FILTER_GE:
  875. case LDAP_FILTER_LE:
  876. if (sattr->a_mr_ord_plugin) {
  877. norm_fn = sattr->a_mr_ord_plugin->plg_mr_normalize;
  878. }
  879. break;
  880. case LDAP_FILTER_EQUALITY:
  881. if (sattr->a_mr_eq_plugin) {
  882. norm_fn = sattr->a_mr_eq_plugin->plg_mr_normalize;
  883. }
  884. break;
  885. case LDAP_FILTER_SUBSTRINGS:
  886. if (sattr->a_mr_sub_plugin) {
  887. norm_fn = sattr->a_mr_sub_plugin->plg_mr_normalize;
  888. }
  889. break;
  890. default:
  891. break;
  892. }
  893. if (!norm_fn && sattr->a_plugin) {
  894. /* no matching rule specific normalizer specified - use syntax default */
  895. norm_fn = sattr->a_plugin->plg_syntax_normalize;
  896. }
  897. if (norm_fn) {
  898. (*norm_fn)(pb, val, trim_spaces, retval);
  899. }
  900. if (sattr == &myattr) {
  901. attr_done(&myattr);
  902. }
  903. return;
  904. }
  905. void
  906. slapi_attr_value_normalize(
  907. Slapi_PBlock *pb,
  908. const Slapi_Attr *sattr, /* if sattr is NULL, type must be attr type name */
  909. const char *type,
  910. char *val,
  911. int trim_spaces,
  912. char **retval
  913. )
  914. {
  915. slapi_attr_value_normalize_ext(pb, sattr, type, val, trim_spaces, retval, 0);
  916. }