attr.c 22 KB

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