plugin_syntax.c 28 KB

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