attr.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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. /* attr.c - routines for dealing with attributes */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <ctype.h>
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #ifndef _WIN32
  45. #include <sys/time.h>
  46. #include <sys/param.h>
  47. #include <fcntl.h>
  48. #include <sys/socket.h>
  49. #endif
  50. #include "slap.h"
  51. #undef DEBUG /* disable counters */
  52. #include <prcountr.h>
  53. static int counters_created= 0;
  54. PR_DEFINE_COUNTER(slapi_attr_counter_created);
  55. PR_DEFINE_COUNTER(slapi_attr_counter_deleted);
  56. PR_DEFINE_COUNTER(slapi_attr_counter_exist);
  57. /*
  58. * structure used within AVL value trees.
  59. */
  60. typedef struct slapi_attr_value_index {
  61. int savi_index; /* index into a_vals[] */
  62. struct berval *savi_normval; /* normalized value */
  63. } SlapiAttrValueIndex;
  64. /*
  65. * Utility function used by slapi_attr_type_cmp to
  66. * find the next component of an attribute type.
  67. */
  68. static const char *
  69. next_comp( const char *s )
  70. {
  71. while ( *s && *s != ';' ) {
  72. s++;
  73. }
  74. if ( *s == '\0' ) {
  75. return( NULL );
  76. } else {
  77. return( s + 1 );
  78. }
  79. }
  80. /*
  81. * Utility function used by slapi_attr_type_cmp to
  82. * compare two components of an attribute type.
  83. */
  84. static int
  85. comp_cmp( const char *s1, const char *s2 )
  86. {
  87. while ( *s1 && *s1 != ';' && tolower( *s1 ) == tolower( *s2 ) ) {
  88. s1++, s2++;
  89. }
  90. if ( *s1 != *s2 ) {
  91. if ( (*s1 == '\0' || *s1 == ';') &&
  92. (*s2 == '\0' || *s2 == ';') ) {
  93. return( 0 );
  94. } else {
  95. return( 1 );
  96. }
  97. } else {
  98. return( 0 );
  99. }
  100. }
  101. int
  102. slapi_attr_type_cmp( const char *a1, const char *a2, int opt )
  103. {
  104. int rc= 0;
  105. switch ( opt ) {
  106. case SLAPI_TYPE_CMP_EXACT: /* compare base name + options as given */
  107. rc = strcmp( a1, a2 );
  108. break;
  109. case SLAPI_TYPE_CMP_BASE: /* ignore options on both names - compare base names only */
  110. rc = comp_cmp( a1, a2 );
  111. break;
  112. case SLAPI_TYPE_CMP_SUBTYPE: /* ignore options on second name that are not in first name */
  113. /*
  114. * first, check that the base types match
  115. */
  116. if ( comp_cmp( a1, a2 ) != 0 ) {
  117. rc = 1;
  118. break;
  119. }
  120. /*
  121. * next, for each component in a1, make sure there is a
  122. * matching component in a2. the order must be the same,
  123. * so we can keep looking where we left off each time in a2
  124. */
  125. rc = 0;
  126. for ( a1 = next_comp( a1 ); a1 != NULL; a1 = next_comp( a1 ) ) {
  127. for ( a2 = next_comp( a2 ); a2 != NULL;
  128. a2 = next_comp( a2 ) ) {
  129. if ( comp_cmp( a1, a2 ) == 0 ) {
  130. break;
  131. }
  132. }
  133. if ( a2 == NULL ) {
  134. rc = 1;
  135. break;
  136. }
  137. }
  138. break;
  139. }
  140. return( rc );
  141. }
  142. /*
  143. * Return 1 if the two types are equivalent -- either the same type name,
  144. * or aliases for one another, including OIDs.
  145. *
  146. * Objective: don't allocate any memory!
  147. */
  148. int
  149. slapi_attr_types_equivalent(const char *t1, const char *t2)
  150. {
  151. int retval = 0;
  152. struct asyntaxinfo *asi1, *asi2;
  153. if (NULL == t1 || NULL == t2) {
  154. return 0;
  155. }
  156. asi1 = attr_syntax_get_by_name(t1);
  157. asi2 = attr_syntax_get_by_name(t2);
  158. if (NULL != asi1) {
  159. if (NULL != asi2) {
  160. /* Both found - compare normalized names */
  161. if (strcasecmp(asi1->asi_name, asi2->asi_name) == 0) {
  162. retval = 1;
  163. } else {
  164. retval = 0;
  165. }
  166. } else {
  167. /* One found, the other wasn't, so not equivalent */
  168. retval = 0;
  169. }
  170. } else if (NULL != asi2) {
  171. /* One found, the other wasn't, so not equivalent */
  172. retval = 0;
  173. } else {
  174. /* Neither found - perform case-insensitive compare */
  175. if (strcasecmp(t1, t2) == 0) {
  176. retval = 1;
  177. } else {
  178. retval = 0;
  179. }
  180. }
  181. attr_syntax_return( asi1 );
  182. attr_syntax_return( asi2 );
  183. return retval;
  184. }
  185. Slapi_Attr *
  186. slapi_attr_new()
  187. {
  188. Slapi_Attr *a= (Slapi_Attr *)slapi_ch_calloc( 1, sizeof(Slapi_Attr));
  189. if(!counters_created)
  190. {
  191. PR_CREATE_COUNTER(slapi_attr_counter_created,"Slapi_Attr","created","");
  192. PR_CREATE_COUNTER(slapi_attr_counter_deleted,"Slapi_Attr","deleted","");
  193. PR_CREATE_COUNTER(slapi_attr_counter_exist,"Slapi_Attr","exist","");
  194. counters_created= 1;
  195. }
  196. PR_INCREMENT_COUNTER(slapi_attr_counter_created);
  197. PR_INCREMENT_COUNTER(slapi_attr_counter_exist);
  198. return a;
  199. }
  200. Slapi_Attr *
  201. slapi_attr_init(Slapi_Attr *a, const char *type)
  202. {
  203. return slapi_attr_init_locking_optional(a, type, PR_TRUE, PR_TRUE);
  204. }
  205. Slapi_Attr *
  206. slapi_attr_init_locking_optional(Slapi_Attr *a, const char *type, PRBool use_lock, PRBool ref_count)
  207. {
  208. PR_ASSERT(a!=NULL);
  209. if(NULL != a)
  210. {
  211. struct asyntaxinfo *asi= NULL;
  212. const char *basetype= NULL;
  213. char *tmp= NULL;
  214. if(type!=NULL)
  215. {
  216. char buf[SLAPD_TYPICAL_ATTRIBUTE_NAME_MAX_LENGTH];
  217. basetype = buf;
  218. tmp = slapi_attr_basetype(type, buf, sizeof(buf));
  219. if(tmp != NULL)
  220. {
  221. basetype = tmp; /* basetype was malloc'd */
  222. }
  223. asi = attr_syntax_get_by_name_locking_optional(basetype, use_lock, ref_count);
  224. }
  225. if(NULL == asi)
  226. {
  227. a->a_type = attr_syntax_normalize_no_lookup( type );
  228. /*
  229. * no syntax for this type... return DirectoryString
  230. * syntax. we accomplish this by looking up a well known
  231. * attribute type that has that syntax.
  232. */
  233. asi = attr_syntax_get_by_name_locking_optional(
  234. ATTR_WITH_DIRSTRING_SYNTAX, use_lock, ref_count);
  235. }
  236. else
  237. {
  238. char *attroptions = NULL;
  239. if ( NULL != type ) {
  240. attroptions = strchr( type, ';' );
  241. }
  242. if ( NULL == attroptions ) {
  243. a->a_type = slapi_ch_strdup(asi->asi_name);
  244. } else {
  245. /*
  246. * If the original type includes any attribute options,
  247. * the a_type field is set to the type contained in the
  248. * attribute syntax followed by a normalized copy of the
  249. * options.
  250. */
  251. char *normalized_options;
  252. normalized_options = attr_syntax_normalize_no_lookup( attroptions );
  253. a->a_type = slapi_ch_smprintf("%s%s", asi->asi_name, normalized_options );
  254. slapi_ch_free_string( &normalized_options );
  255. }
  256. }
  257. if ( asi != NULL )
  258. {
  259. a->a_plugin = asi->asi_plugin;
  260. a->a_flags = asi->asi_flags;
  261. }
  262. else
  263. {
  264. a->a_plugin = NULL; /* XXX - should be rare */
  265. a->a_flags = 0; /* XXX - should be rare */
  266. }
  267. attr_syntax_return_locking_optional( asi, use_lock );
  268. if (NULL != tmp)
  269. {
  270. slapi_ch_free_string(&tmp);
  271. }
  272. slapi_valueset_init(&a->a_present_values);
  273. slapi_valueset_init(&a->a_deleted_values);
  274. a->a_listtofree= NULL;
  275. a->a_deletioncsn= NULL;
  276. a->a_next= NULL;
  277. }
  278. return a;
  279. }
  280. Slapi_Attr *
  281. slapi_attr_dup(const Slapi_Attr *attr)
  282. {
  283. Slapi_Attr *newattr= slapi_attr_new();
  284. Slapi_Value **present_va= valueset_get_valuearray(&attr->a_present_values); /* JCM Mucking about inside the value set */
  285. Slapi_Value **deleted_va= valueset_get_valuearray(&attr->a_deleted_values); /* JCM Mucking about inside the value set */
  286. slapi_attr_init(newattr, attr->a_type);
  287. valueset_add_valuearray( &newattr->a_present_values, present_va );
  288. valueset_add_valuearray( &newattr->a_deleted_values, deleted_va );
  289. newattr->a_deletioncsn= csn_dup(attr->a_deletioncsn);
  290. return newattr;
  291. }
  292. void
  293. slapi_attr_free( Slapi_Attr **ppa )
  294. {
  295. if(ppa!=NULL && *ppa!=NULL)
  296. {
  297. Slapi_Attr *a= *ppa;
  298. attr_done(a);
  299. slapi_ch_free( (void**)&a );
  300. PR_INCREMENT_COUNTER(slapi_attr_counter_deleted);
  301. PR_DECREMENT_COUNTER(slapi_attr_counter_exist);
  302. }
  303. }
  304. void
  305. attr_done(Slapi_Attr *a)
  306. {
  307. if(a!=NULL)
  308. {
  309. slapi_ch_free((void**)&a->a_type);
  310. csn_free(&a->a_deletioncsn);
  311. slapi_valueset_done(&a->a_present_values);
  312. slapi_valueset_done(&a->a_deleted_values);
  313. {
  314. struct bervals2free *freelist;
  315. struct bervals2free *tmp;
  316. freelist = a->a_listtofree;
  317. while(freelist) {
  318. ber_bvecfree(freelist->bvals);
  319. tmp=freelist;
  320. freelist = freelist->next;
  321. slapi_ch_free((void **)&tmp);
  322. }
  323. }
  324. }
  325. }
  326. /*
  327. * slapi_attr_basetype - extracts the attribute base type (without
  328. * any attribute description options) from type. puts the result
  329. * in buf if it can, otherwise, it malloc's and returns the base
  330. * which should be free()'ed later.
  331. */
  332. char *
  333. slapi_attr_basetype( const char *type, char *buf, size_t bufsize )
  334. {
  335. unsigned int i;
  336. i = 0;
  337. while ( *type && *type != ';' && i < bufsize ) {
  338. buf[i++] = *type++;
  339. }
  340. if ( i < bufsize ) {
  341. buf[i] = '\0';
  342. return( NULL );
  343. } else {
  344. int len;
  345. char *tmp;
  346. len = strlen( type );
  347. tmp = slapi_ch_malloc( len + 1 );
  348. slapi_attr_basetype( type, tmp, len + 1 );
  349. return( tmp );
  350. }
  351. }
  352. /*
  353. * returns 0 if "v" is already a value within "a" and non-zero if not.
  354. */
  355. int
  356. slapi_attr_value_find( const Slapi_Attr *a, const struct berval *v )
  357. {
  358. struct ava ava;
  359. if ( NULL == a ) {
  360. return( -1 );
  361. }
  362. ava.ava_type = a->a_type;
  363. ava.ava_value = *v;
  364. return(plugin_call_syntax_filter_ava( a, LDAP_FILTER_EQUALITY, &ava ));
  365. }
  366. int
  367. slapi_attr_get_type( Slapi_Attr *a, char **type )
  368. {
  369. *type = a->a_type;
  370. return( 0 );
  371. }
  372. /*
  373. * Fetch a copy of the values as an array of struct berval *'s.
  374. * Returns 0 upon success and non-zero on failure.
  375. * Free the array of values by calling ber_bvecfree().
  376. */
  377. int
  378. slapi_attr_get_bervals_copy( Slapi_Attr *a, struct berval ***vals )
  379. {
  380. int retVal= 0;
  381. if ( NULL == vals )
  382. {
  383. return -1;
  384. }
  385. if(NULL==a || valueset_isempty(&a->a_present_values))
  386. {
  387. *vals = NULL;
  388. }
  389. else
  390. {
  391. Slapi_Value **va= valueset_get_valuearray(&a->a_present_values);
  392. valuearray_get_bervalarray(va,vals);
  393. }
  394. return retVal;
  395. }
  396. /*
  397. * JCM: BEWARE.. HIGHLY EVIL.. DO NOT USE THIS FUNCTION.
  398. * XXXmcs: Why not? Because it is only provided as a not-quite-backwards
  399. * compatible interface for older (pre-iDS 5.0) plugins. It works by
  400. * making a copy of the attribute values (the pre-iDS 5.0 function with
  401. * the same name returned a pointer into the Slapi_Attr structure -- no
  402. * copying). Since older users of this interface did not have to free
  403. * the values, this function arranges to free them WHEN THE Slapi_Attr
  404. * IS DESTROYED. This is accomplished by adding a pointer to the newly
  405. * allocated copy to a "to be freed" linked list inside the Slapi_Attr.
  406. * But if the Slapi_Attr is not destroyed very often, and this function
  407. * is called repeatedly, memory usage will grow without bound. Not good.
  408. * The value copies are freed inside attr_done() which is called from
  409. * slapi_attr_free() and a few other places.
  410. *
  411. * If you really want a copy of the values as a struct berval ** array,
  412. * call slapi_attr_get_bervals_copy() and free it yourself by calling
  413. * ber_bvecfree().
  414. */
  415. int
  416. slapi_attr_get_values( Slapi_Attr *a, struct berval ***vals )
  417. {
  418. int retVal = slapi_attr_get_bervals_copy( a, vals );
  419. if ( 0 == retVal )
  420. {
  421. struct bervals2free *newfree;
  422. newfree = (struct bervals2free *)slapi_ch_malloc(sizeof(struct bervals2free));
  423. newfree->next = a->a_listtofree;
  424. newfree->bvals = *vals;
  425. a->a_listtofree = newfree;
  426. }
  427. return retVal;
  428. }
  429. /*
  430. * Fetch a copy of the present valueset.
  431. * Caller must free the valueset.
  432. */
  433. int
  434. slapi_attr_get_valueset(const Slapi_Attr *a, Slapi_ValueSet **vs)
  435. {
  436. int retVal= 0;
  437. if(vs!=NULL)
  438. {
  439. *vs= valueset_dup(&a->a_present_values);
  440. }
  441. return retVal;
  442. }
  443. /*
  444. * Careful... this returns a pointer to the contents!
  445. */
  446. Slapi_Value **
  447. attr_get_present_values(const Slapi_Attr *a)
  448. {
  449. return valueset_get_valuearray(&a->a_present_values);
  450. }
  451. int
  452. slapi_attr_get_flags( const Slapi_Attr *a, unsigned long *flags )
  453. {
  454. *flags = a->a_flags;
  455. return( 0 );
  456. }
  457. int
  458. slapi_attr_flag_is_set( const Slapi_Attr *a, unsigned long flag )
  459. {
  460. return( a->a_flags & flag );
  461. }
  462. int
  463. slapi_attr_value_cmp( const Slapi_Attr *a, const struct berval *v1, const struct berval *v2 )
  464. {
  465. int retVal;
  466. if ( a->a_flags & SLAPI_ATTR_FLAG_CMP_BITBYBIT )
  467. {
  468. int cmplen = ( v1->bv_len <= v2->bv_len ? v1->bv_len : v2->bv_len );
  469. retVal = memcmp(v1->bv_val, v2->bv_val, cmplen);
  470. if ( retVal == 0 && v1->bv_len < v2->bv_len )
  471. {
  472. retVal = -1;
  473. }
  474. else if ( retVal == 0 && v1->bv_len > v2->bv_len )
  475. {
  476. retVal = 1;
  477. }
  478. }
  479. else
  480. {
  481. Slapi_Attr a2;
  482. struct ava ava;
  483. Slapi_Value *cvals[2];
  484. Slapi_Value tmpcval;
  485. a2 = *a;
  486. cvals[0] = &tmpcval;
  487. cvals[0]->v_csnset = NULL;
  488. cvals[0]->bv = *v1;
  489. cvals[1] = NULL;
  490. a2.a_present_values.va = cvals; /* JCM - PUKE */
  491. ava.ava_type = a->a_type;
  492. ava.ava_value = *v2;
  493. retVal = plugin_call_syntax_filter_ava(&a2, LDAP_FILTER_EQUALITY, &ava);
  494. }
  495. return retVal;
  496. }
  497. /*
  498. * Set the CSN of all the present values.
  499. */
  500. int
  501. attr_set_csn( Slapi_Attr *a, const CSN *csn)
  502. {
  503. PR_ASSERT(a!=NULL);
  504. valueset_update_csn(&a->a_present_values, CSN_TYPE_VALUE_UPDATED, csn);
  505. return 0;
  506. }
  507. int
  508. attr_set_deletion_csn( Slapi_Attr *a, const CSN *csn)
  509. {
  510. PR_ASSERT(a!=NULL);
  511. if(csn_compare(csn,a->a_deletioncsn)>0)
  512. {
  513. csn_free(&a->a_deletioncsn);
  514. a->a_deletioncsn= csn_dup(csn);
  515. }
  516. return 0;
  517. }
  518. const CSN *
  519. attr_get_deletion_csn(const Slapi_Attr *a)
  520. {
  521. PR_ASSERT(a!=NULL);
  522. return a->a_deletioncsn;
  523. }
  524. int
  525. slapi_attr_first_value( Slapi_Attr *a, Slapi_Value **v )
  526. {
  527. int rc;
  528. if(NULL == a) {
  529. rc = -1;
  530. } else {
  531. rc=slapi_valueset_first_value( &a->a_present_values, v );
  532. }
  533. return rc;
  534. }
  535. int
  536. slapi_attr_next_value( Slapi_Attr *a, int hint, Slapi_Value **v)
  537. {
  538. int rc;
  539. if(NULL == a) {
  540. rc = -1;
  541. } else {
  542. rc=slapi_valueset_next_value( &a->a_present_values, hint, v );
  543. }
  544. return rc;
  545. }
  546. int
  547. slapi_attr_get_numvalues( const Slapi_Attr *a, int *numValues )
  548. {
  549. if(NULL == a) {
  550. *numValues = 0;
  551. } else {
  552. *numValues = slapi_valueset_count(&a->a_present_values);
  553. }
  554. return 0;
  555. }
  556. int
  557. attr_first_deleted_value( Slapi_Attr *a, Slapi_Value **v )
  558. {
  559. return slapi_valueset_first_value( &a->a_deleted_values, v );
  560. }
  561. int
  562. attr_next_deleted_value( Slapi_Attr *a, int hint, Slapi_Value **v)
  563. {
  564. return slapi_valueset_next_value( &a->a_deleted_values, hint, v );
  565. }
  566. /*
  567. * Note: We are passing in the entry so that we may be able to "optimize"
  568. * the csn related information and roll it up higher to the level of entry.
  569. */
  570. void
  571. attr_purge_state_information(Slapi_Entry *entry, Slapi_Attr *attr, const CSN *csnUpTo)
  572. {
  573. if(!valueset_isempty(&attr->a_deleted_values))
  574. {
  575. valueset_purge(&attr->a_deleted_values, csnUpTo);
  576. }
  577. }
  578. /*
  579. * Search an attribute for a value, it could be present, deleted, or not present.
  580. */
  581. int
  582. attr_value_find_wsi(Slapi_Attr *a, const struct berval *bval, Slapi_Value **value)
  583. {
  584. int retVal=0;
  585. struct ava ava;
  586. PR_ASSERT(a!=NULL);
  587. PR_ASSERT(value!=NULL);
  588. /*
  589. * we will first search the present values, and then, if
  590. * necessary, the deleted values.
  591. */
  592. ava.ava_type = a->a_type;
  593. ava.ava_value = *bval;
  594. retVal = plugin_call_syntax_filter_ava_sv(a, LDAP_FILTER_EQUALITY, &ava, value, 0 /* Present */);
  595. if(retVal==0)
  596. {
  597. /* we found the value, so we don't search the deleted list */
  598. retVal= VALUE_PRESENT;
  599. }
  600. else
  601. {
  602. retVal = plugin_call_syntax_filter_ava_sv(a, LDAP_FILTER_EQUALITY, &ava, value, 1 /* Deleted */);
  603. if(retVal==0)
  604. {
  605. /* It was on the deleted value list */
  606. retVal= VALUE_DELETED;
  607. }
  608. else
  609. {
  610. /* Couldn't find it */
  611. retVal= VALUE_NOTFOUND;
  612. }
  613. }
  614. return retVal;
  615. }
  616. int
  617. slapi_attr_add_value(Slapi_Attr *a, const Slapi_Value *v)
  618. {
  619. slapi_valueset_add_value( &a->a_present_values, v);
  620. return 0;
  621. }
  622. /* Make the valuset in SLapi_Attr be *vs--not a copy */
  623. int
  624. slapi_attr_set_valueset(Slapi_Attr *a, const Slapi_ValueSet *vs)
  625. {
  626. slapi_valueset_set_valueset( &a->a_present_values, vs);
  627. return 0;
  628. }
  629. int
  630. attr_add_deleted_value(Slapi_Attr *a, const Slapi_Value *v)
  631. {
  632. slapi_valueset_add_value( &a->a_deleted_values, v);
  633. return 0;
  634. }
  635. /*
  636. * If we are adding or deleting SLAPD_MODUTIL_TREE_THRESHHOLD or more
  637. * entries, we use an AVL tree to speed up searching for duplicates or
  638. * values we are trying to delete. This threshhold is somewhat arbitrary;
  639. * we should really take some measurements to determine an optimal number.
  640. */
  641. #define SLAPD_MODUTIL_TREE_THRESHHOLD 5
  642. /*
  643. * Add a value array to an attribute. If SLAPD_MODUTIL_TREE_THRESHHOLD or
  644. * more values are being added, we build an AVL tree of any existing
  645. * values and then update that in parallel with the existing values. This
  646. * is done so that we do not waste a lot of CPU time searching for duplicate
  647. * values. The AVL tree is created and destroyed all within this function.
  648. *
  649. * Returns
  650. * LDAP_SUCCESS - OK
  651. * LDAP_OPERATIONS_ERROR - Existing duplicates in attribute.
  652. * LDAP_TYPE_OR_VALUE_EXISTS - Duplicate values.
  653. */
  654. int
  655. attr_add_valuearray(Slapi_Attr *a, Slapi_Value **vals, const char *dn)
  656. {
  657. int i = 0;
  658. int duplicate_index = -1;
  659. int was_present_null = 0;
  660. int rc = LDAP_SUCCESS;
  661. if (valuearray_isempty(vals)) {
  662. /*
  663. * No values to add (unexpected but acceptable).
  664. */
  665. return rc;
  666. }
  667. /*
  668. * determine whether we should use an AVL tree of values or not
  669. */
  670. while ( i < SLAPD_MODUTIL_TREE_THRESHHOLD - 1 && vals[i] != NULL ) {
  671. i++;
  672. }
  673. /*
  674. * detect duplicate values
  675. */
  676. if ( i >= SLAPD_MODUTIL_TREE_THRESHHOLD - 1 ) {
  677. /*
  678. * Several values to add: use an AVL tree to detect duplicates.
  679. */
  680. LDAPDebug( LDAP_DEBUG_TRACE,
  681. "slapi_entry_add_values: using an AVL tree to "
  682. "detect duplicate values\n", 0, 0, 0 );
  683. if (valueset_isempty(&a->a_present_values)) {
  684. /* if the attribute contains no values yet, just check the
  685. * input vals array for duplicates
  686. */
  687. Avlnode *vtree = NULL;
  688. rc= valuetree_add_valuearray(a->a_type, a->a_plugin, vals, &vtree, &duplicate_index);
  689. valuetree_free(&vtree);
  690. was_present_null = 1;
  691. } else {
  692. /* the attr and vals both contain values, check intersection */
  693. rc= valueset_intersectswith_valuearray(&a->a_present_values, a, vals, &duplicate_index);
  694. }
  695. } else if ( !valueset_isempty(&a->a_present_values) ) {
  696. /*
  697. * Small number of values to add: don't bother constructing
  698. * an AVL tree, etc. since it probably isn't worth the time.
  699. */
  700. for ( i = 0; vals[i] != NULL; ++i ) {
  701. if ( slapi_attr_value_find( a, slapi_value_get_berval(vals[i]) ) == 0 ) {
  702. duplicate_index = i;
  703. rc = LDAP_TYPE_OR_VALUE_EXISTS;
  704. break;
  705. }
  706. }
  707. }
  708. /*
  709. * add values if no duplicates detected
  710. */
  711. if(rc==LDAP_SUCCESS) {
  712. valueset_add_valuearray( &a->a_present_values, vals );
  713. }
  714. /* In the case of duplicate value, rc == LDAP_TYPE_OR_VALUE_EXISTS or
  715. * LDAP_OPERATIONS_ERROR
  716. */
  717. else if ( duplicate_index >= 0 ) {
  718. char avdbuf[BUFSIZ];
  719. char bvvalcopy[BUFSIZ];
  720. char *duplicate_string = "null or non-ASCII";
  721. i = 0;
  722. while ( (unsigned int)i < vals[duplicate_index]->bv.bv_len &&
  723. i < BUFSIZ - 1 &&
  724. vals[duplicate_index]->bv.bv_val[i] &&
  725. isascii ( vals[duplicate_index]->bv.bv_val[i] )) {
  726. i++;
  727. }
  728. if ( i ) {
  729. if ( vals[duplicate_index]->bv.bv_val[i] == 0 ) {
  730. duplicate_string = vals[duplicate_index]->bv.bv_val;
  731. }
  732. else {
  733. strncpy ( &bvvalcopy[0], vals[duplicate_index]->bv.bv_val, i );
  734. bvvalcopy[i] = '\0';
  735. duplicate_string = bvvalcopy;
  736. }
  737. }
  738. slapi_log_error( SLAPI_LOG_FATAL, NULL, "add value \"%s\" to "
  739. "attribute type \"%s\" in entry \"%s\" failed: %s\n",
  740. duplicate_string,
  741. a->a_type,
  742. dn ? escape_string(dn,avdbuf) : "<null>",
  743. (was_present_null ? "duplicate new value" : "value exists"));
  744. }
  745. return( rc );
  746. }
  747. /* quickly toss an attribute's values and replace them with new ones
  748. * (used by attrlist_replace_fast)
  749. */
  750. void attr_replace(Slapi_Attr *a, Slapi_Value **vals)
  751. {
  752. valueset_replace(&a->a_present_values, vals);
  753. }
  754. int
  755. attr_check_onoff ( const char *attr_name, char *value, long minval, long maxval, char *errorbuf )
  756. {
  757. int retVal = LDAP_SUCCESS;
  758. if ( strcasecmp ( value, "on" ) != 0 &&
  759. strcasecmp ( value, "off") != 0 &&
  760. strcasecmp ( value, "1" ) != 0 &&
  761. strcasecmp ( value, "0" ) != 0 &&
  762. strcasecmp ( value, "true" ) != 0 &&
  763. strcasecmp ( value, "false" ) != 0 ) {
  764. PR_snprintf ( errorbuf, BUFSIZ,
  765. "%s: invalid value \"%s\".", attr_name, value );
  766. retVal = LDAP_CONSTRAINT_VIOLATION;
  767. }
  768. return retVal;
  769. }
  770. int
  771. attr_check_minmax ( const char *attr_name, char *value, long minval, long maxval, char *errorbuf )
  772. {
  773. int retVal = LDAP_SUCCESS;
  774. long val;
  775. val = strtol(value, NULL, 0);
  776. if ( (minval != -1 ? (val < minval ? 1 : 0) : 0) ||
  777. (maxval != -1 ? (val > maxval ? 1 : 0) : 0) ) {
  778. PR_snprintf ( errorbuf, BUFSIZ,
  779. "%s: invalid value \"%s\".",
  780. attr_name, value );
  781. retVal = LDAP_CONSTRAINT_VIOLATION;
  782. }
  783. return retVal;
  784. }