plugin_syntax.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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 = (Slapi_Attr *)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((Slapi_Attr *)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((Slapi_Attr *)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. return slapi_attr_assertion2keys_sub_sv_pb(NULL, sattr, initial, any, final, ivals);
  811. }
  812. int
  813. slapi_attr_assertion2keys_sub_sv_pb(
  814. Slapi_PBlock *pb,
  815. const Slapi_Attr *sattr,
  816. char *initial,
  817. char **any,
  818. char *final,
  819. Slapi_Value ***ivals
  820. )
  821. {
  822. int rc;
  823. Slapi_PBlock pipb;
  824. struct slapdplugin *pi = NULL;
  825. IFP a2k_fn = NULL;
  826. LDAPDebug( LDAP_DEBUG_FILTER,
  827. "=> slapi_attr_assertion2keys_sub_sv\n", 0, 0, 0 );
  828. if ( ( sattr->a_plugin == NULL ) ) {
  829. /* could be lazy plugin initialization, get it now */
  830. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  831. }
  832. if (sattr->a_mr_sub_plugin) {
  833. pi = sattr->a_mr_sub_plugin;
  834. a2k_fn = sattr->a_mr_sub_plugin->plg_mr_assertion2keys_sub;
  835. } else if (sattr->a_plugin) {
  836. pi = sattr->a_plugin;
  837. a2k_fn = sattr->a_plugin->plg_syntax_assertion2keys_sub;
  838. }
  839. if (NULL == pb) {
  840. pblock_init( &pipb );
  841. pb = &pipb;
  842. }
  843. slapi_pblock_set( pb, SLAPI_PLUGIN, pi );
  844. rc = -1; /* means no assertion2keys function */
  845. *ivals = NULL;
  846. if ( a2k_fn != NULL ) {
  847. rc = (*a2k_fn)( pb, initial, any, final, ivals );
  848. }
  849. LDAPDebug( LDAP_DEBUG_FILTER,
  850. "<= slapi_attr_assertion2keys_sub_sv %d\n", rc, 0, 0 );
  851. return( rc );
  852. }
  853. SLAPI_DEPRECATED int
  854. slapi_attr_assertion2keys_sub( /* JCM SLOW FUNCTION */
  855. const Slapi_Attr *sattr,
  856. char *initial,
  857. char **any,
  858. char *final,
  859. struct berval ***ivals
  860. )
  861. {
  862. int rc;
  863. Slapi_Value **svout= NULL;
  864. rc= slapi_attr_assertion2keys_sub_sv(sattr,initial,any,final,&svout);
  865. valuearray_get_bervalarray(svout,ivals); /* JCM SLOW FUNCTION */
  866. valuearray_free(&svout);
  867. return rc;
  868. }
  869. void
  870. slapi_attr_value_normalize_ext(
  871. Slapi_PBlock *pb,
  872. const Slapi_Attr *sattr, /* if sattr is NULL, type must be attr type name */
  873. const char *type,
  874. char *val,
  875. int trim_spaces,
  876. char **retval,
  877. unsigned long filter_type
  878. )
  879. {
  880. Slapi_Attr myattr;
  881. VFPV norm_fn = NULL;
  882. if (!sattr) {
  883. sattr = slapi_attr_init(&myattr, type);
  884. if(!sattr){
  885. attr_done(&myattr);
  886. return;
  887. }
  888. }
  889. if ( ( sattr->a_plugin == NULL ) ) {
  890. /* could be lazy plugin initialization, get it now */
  891. slapi_attr_init_syntax((Slapi_Attr *)sattr);
  892. }
  893. /* use the filter type to determine which matching rule to use */
  894. switch (filter_type) {
  895. case LDAP_FILTER_GE:
  896. case LDAP_FILTER_LE:
  897. if (sattr->a_mr_ord_plugin) {
  898. norm_fn = sattr->a_mr_ord_plugin->plg_mr_normalize;
  899. }
  900. break;
  901. case LDAP_FILTER_EQUALITY:
  902. if (sattr->a_mr_eq_plugin) {
  903. norm_fn = sattr->a_mr_eq_plugin->plg_mr_normalize;
  904. }
  905. break;
  906. case LDAP_FILTER_SUBSTRINGS:
  907. if (sattr->a_mr_sub_plugin) {
  908. norm_fn = sattr->a_mr_sub_plugin->plg_mr_normalize;
  909. }
  910. break;
  911. default:
  912. break;
  913. }
  914. if (!norm_fn && sattr->a_plugin) {
  915. /* no matching rule specific normalizer specified - use syntax default */
  916. norm_fn = sattr->a_plugin->plg_syntax_normalize;
  917. }
  918. if (norm_fn) {
  919. (*norm_fn)(pb, val, trim_spaces, retval);
  920. }
  921. if (sattr == &myattr) {
  922. attr_done(&myattr);
  923. }
  924. return;
  925. }
  926. void
  927. slapi_attr_value_normalize(
  928. Slapi_PBlock *pb,
  929. const Slapi_Attr *sattr, /* if sattr is NULL, type must be attr type name */
  930. const char *type,
  931. char *val,
  932. int trim_spaces,
  933. char **retval
  934. )
  935. {
  936. slapi_attr_value_normalize_ext(pb, sattr, type, val, trim_spaces, retval, 0);
  937. }