modutil.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  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. /* modutil.c - modify utility routine */
  42. #include <stdio.h>
  43. #include <string.h>
  44. #include <time.h>
  45. #include <sys/types.h>
  46. #ifndef _WIN32
  47. #include <sys/socket.h>
  48. #endif /* _WIN32 */
  49. #include "slap.h"
  50. #define SIZE_INIT 4 /* initial element size */
  51. #define SIZE_INC 2 /* size increment */
  52. /*
  53. * Free an array of LDAPMod structures. Just like ldap_mods_free,
  54. * except that it assumes that the mods are expressed as a bervec.
  55. */
  56. void
  57. freepmods( LDAPMod **pmods )
  58. {
  59. int i;
  60. for ( i = 0; pmods[ i ] != NULL; ++i ) {
  61. if ( pmods[ i ]->mod_bvalues != NULL ) {
  62. ber_bvecfree( pmods[ i ]->mod_bvalues );
  63. }
  64. if ( pmods[ i ]->mod_type != NULL ) {
  65. slapi_ch_free((void**)&pmods[ i ]->mod_type );
  66. }
  67. slapi_ch_free((void**)&pmods[ i ] );
  68. }
  69. slapi_ch_free((void**)&pmods );
  70. }
  71. /* ======= Utility functions for manipulating a list of LDAPMods ======= */
  72. /*
  73. * Slapi_Mods can be used in two ways:
  74. * 1) To wrap an existing array of LDAPMods, or
  75. * 2) To create a new array of LDAPMods.
  76. *
  77. * Slapi_Mods provides memory management and array manipulation
  78. * functions to make using (LDAPMod**) easier.
  79. *
  80. */
  81. Slapi_Mods*
  82. slapi_mods_new()
  83. {
  84. Slapi_Mods *mods;
  85. mods = (Slapi_Mods*) slapi_ch_calloc (1, sizeof (Slapi_Mods));
  86. return mods;
  87. }
  88. /*
  89. * Initialise a Slapi_Mod suggesting how big an LDAPMod array is needed.
  90. * It will be free'd when the Slapi_Mods is destroyed.
  91. */
  92. void
  93. slapi_mods_init(Slapi_Mods *smods, int initCount)
  94. {
  95. memset (smods, 0, sizeof (*smods));
  96. smods->free_mods = 1;
  97. if (initCount > 0)
  98. {
  99. smods->num_elements= initCount + 1; /* one for NULL element */
  100. smods->mods = (LDAPMod **) slapi_ch_calloc( 1, smods->num_elements * sizeof(LDAPMod *) );
  101. }
  102. }
  103. /*
  104. * Initialise a Slapi_Mod passing in responsibility for the (LDAPMod **).
  105. * It will be free'd when the Slapi_Mods is destroyed.
  106. */
  107. void
  108. slapi_mods_init_passin(Slapi_Mods *smods, LDAPMod **mods)
  109. {
  110. slapi_mods_init_byref(smods, mods);
  111. smods->free_mods = 1;
  112. }
  113. /*
  114. * Initialise a Slapi_Mod passing in a reference to the (LDAPMod **)
  115. * It will *not* be free'd when the Slapi_Mods is destroyed.
  116. */
  117. void
  118. slapi_mods_init_byref(Slapi_Mods *smods, LDAPMod **mods)
  119. {
  120. memset (smods, 0, sizeof (*smods));
  121. if(mods!=NULL)
  122. {
  123. smods->mods = mods;
  124. for ( smods->num_mods = 0; mods[smods->num_mods] != NULL; smods->num_mods++ );
  125. smods->num_elements= smods->num_mods+1; /* We assume there's nothing spare on the end. */
  126. }
  127. }
  128. void
  129. slapi_mods_free(Slapi_Mods **smods)
  130. {
  131. if(smods!=NULL && *smods!=NULL)
  132. {
  133. slapi_mods_done(*smods);
  134. slapi_ch_free ((void**)smods);
  135. *smods= NULL;
  136. }
  137. }
  138. void
  139. slapi_mods_done(Slapi_Mods *smods)
  140. {
  141. PR_ASSERT(smods!=NULL);
  142. if (smods->mods!=NULL)
  143. {
  144. if(smods->free_mods)
  145. {
  146. ldap_mods_free (smods->mods, 1 /* Free the Array and the Elements */);
  147. }
  148. }
  149. memset(smods, 0, sizeof(*smods));
  150. }
  151. static void
  152. slapi_mods_add_one_element(Slapi_Mods *smods)
  153. {
  154. int need = smods->num_mods + 2;
  155. if ( smods->num_elements == 0 )
  156. {
  157. PR_ASSERT(smods->mods==NULL);
  158. smods->num_elements = SIZE_INIT;
  159. smods->mods = (LDAPMod **) slapi_ch_malloc( smods->num_elements * sizeof(LDAPMod *) );
  160. smods->free_mods= 1;
  161. }
  162. if ( smods->num_elements < need )
  163. {
  164. PR_ASSERT(smods->free_mods);
  165. smods->num_elements *= SIZE_INC;
  166. smods->mods = (LDAPMod **) slapi_ch_realloc( (char *) smods->mods, smods->num_elements * sizeof(LDAPMod *) );
  167. }
  168. }
  169. /*
  170. * Shift everything down to make room to insert the new mod.
  171. */
  172. void
  173. slapi_mods_insert_at(Slapi_Mods *smods, LDAPMod *mod, int pos)
  174. {
  175. int i;
  176. if (NULL == mod) {
  177. return;
  178. }
  179. slapi_mods_add_one_element(smods);
  180. for( i=smods->num_mods-1; i>=pos; i--)
  181. {
  182. smods->mods[i+1]= smods->mods[i];
  183. }
  184. smods->mods[pos]= mod;
  185. smods->num_mods++;
  186. smods->mods[smods->num_mods]= NULL;
  187. }
  188. void
  189. slapi_mods_insert_smod_at(Slapi_Mods *smods, Slapi_Mod *smod, int pos)
  190. {
  191. slapi_mods_insert_at (smods, smod->mod, pos);
  192. }
  193. /*
  194. * Shift everything down to make room to insert the new mod.
  195. */
  196. void
  197. slapi_mods_insert_before(Slapi_Mods *smods, LDAPMod *mod)
  198. {
  199. slapi_mods_insert_at(smods, mod, smods->iterator);
  200. smods->iterator++;
  201. }
  202. void
  203. slapi_mods_insert_smod_before(Slapi_Mods *smods, Slapi_Mod *smod)
  204. {
  205. slapi_mods_insert_before(smods, smod->mod);
  206. }
  207. /*
  208. * Shift everything down to make room to insert the new mod.
  209. */
  210. void
  211. slapi_mods_insert_after(Slapi_Mods *smods, LDAPMod *mod)
  212. {
  213. slapi_mods_insert_at(smods, mod, smods->iterator+1);
  214. }
  215. void slapi_mods_insert_smod_after(Slapi_Mods *smods, Slapi_Mod *smod)
  216. {
  217. slapi_mods_insert_after(smods, smod->mod);
  218. }
  219. /*
  220. * Add the LDAPMod to the end of the array.
  221. * Does NOT copy the mod.
  222. */
  223. void
  224. slapi_mods_add_ldapmod(Slapi_Mods *smods, LDAPMod *mod)
  225. {
  226. slapi_mods_insert_at(smods,mod,smods->num_mods);
  227. }
  228. void
  229. slapi_mods_add_smod(Slapi_Mods *smods, Slapi_Mod *smod)
  230. {
  231. slapi_mods_add_ldapmod(smods, smod->mod);
  232. }
  233. /*
  234. * Makes a copy of everything.
  235. */
  236. void
  237. slapi_mods_add_modbvps( Slapi_Mods *smods, int modtype, const char *type, struct berval **bvps )
  238. {
  239. LDAPMod *mod;
  240. mod = (LDAPMod *) slapi_ch_malloc(sizeof(LDAPMod));
  241. mod->mod_type = slapi_ch_strdup( type );
  242. mod->mod_op = modtype | LDAP_MOD_BVALUES;
  243. mod->mod_bvalues = NULL;
  244. if (NULL != bvps)
  245. {
  246. int num_values, i;
  247. num_values = 0;
  248. /* Count mods */
  249. while (NULL != bvps[num_values])
  250. {
  251. num_values++;
  252. }
  253. mod->mod_bvalues = (struct berval **)slapi_ch_malloc((num_values + 1) *
  254. sizeof(struct berval *));
  255. for (i = 0; i < num_values; i++)
  256. {
  257. mod->mod_bvalues[i] = ber_bvdup((struct berval *)bvps[i]); /* jcm had to cast away const */
  258. }
  259. mod->mod_bvalues[num_values] = NULL;
  260. }
  261. slapi_mods_add_ldapmod(smods, mod);
  262. }
  263. /*
  264. * Makes a copy of everything.
  265. */
  266. void
  267. slapi_mods_add_mod_values( Slapi_Mods *smods, int modtype, const char *type, Slapi_Value **va )
  268. {
  269. LDAPMod *mod= (LDAPMod *) slapi_ch_malloc( sizeof(LDAPMod) );
  270. mod->mod_type = slapi_ch_strdup( type );
  271. mod->mod_op = modtype | LDAP_MOD_BVALUES;
  272. mod->mod_bvalues= NULL;
  273. valuearray_get_bervalarray(va,&mod->mod_bvalues);
  274. slapi_mods_add_ldapmod(smods, mod);
  275. }
  276. /*
  277. * Makes a copy of everything.
  278. */
  279. void
  280. slapi_mods_add( Slapi_Mods *smods, int modtype, const char *type, unsigned long len, const char *val)
  281. {
  282. struct berval bv;
  283. struct berval *bvps[2];
  284. if(len>0)
  285. {
  286. bv.bv_len= len;
  287. bv.bv_val= (void*)val; /* We cast away the const, but we're not going to change anything */
  288. bvps[0] = &bv;
  289. bvps[1] = NULL;
  290. }
  291. else
  292. {
  293. bvps[0]= NULL;
  294. }
  295. slapi_mods_add_modbvps( smods, modtype, type, bvps );
  296. }
  297. /*
  298. * Makes a copy of everything.
  299. */
  300. void
  301. slapi_mods_add_string( Slapi_Mods *smods, int modtype, const char *type, const char *val)
  302. {
  303. PR_ASSERT(val);
  304. slapi_mods_add( smods, modtype, type, strlen(val), val);
  305. }
  306. void
  307. slapi_mods_remove(Slapi_Mods *smods)
  308. {
  309. smods->mods[smods->iterator]->mod_op= LDAP_MOD_IGNORE;
  310. }
  311. LDAPMod *
  312. slapi_mods_get_first_mod(Slapi_Mods *smods)
  313. {
  314. /* Reset the iterator in the mod structure */
  315. smods->iterator= -1;
  316. return slapi_mods_get_next_mod(smods);
  317. }
  318. LDAPMod *
  319. slapi_mods_get_next_mod(Slapi_Mods *smods)
  320. {
  321. /* Move the iterator forward */
  322. LDAPMod *r= NULL;
  323. smods->iterator++;
  324. PR_ASSERT (smods->iterator >= 0);
  325. /* skip deleted mods if any */
  326. while (smods->iterator < smods->num_mods && smods->mods[smods->iterator]->mod_op == LDAP_MOD_IGNORE)
  327. smods->iterator ++;
  328. if(smods->iterator<smods->num_mods)
  329. {
  330. r= smods->mods[smods->iterator];
  331. }
  332. return r;
  333. }
  334. static void
  335. mod2smod (LDAPMod *mod, Slapi_Mod *smod)
  336. {
  337. smod->mod = mod;
  338. smod->iterator = 0;
  339. smod->num_values = 0;
  340. if (mod->mod_op & LDAP_MOD_BVALUES)
  341. {
  342. while (mod->mod_bvalues && mod->mod_bvalues[smod->num_values])
  343. {
  344. smod->num_values ++;
  345. }
  346. }
  347. else
  348. {
  349. PR_ASSERT(0); /* ggood shouldn't ever use string values in server */
  350. while (mod->mod_values && mod->mod_values[smod->num_values])
  351. {
  352. smod->num_values ++;
  353. }
  354. }
  355. smod->num_elements = smod->num_values + 1; /* 1- for null char */
  356. }
  357. Slapi_Mod *
  358. slapi_mods_get_first_smod(Slapi_Mods *smods, Slapi_Mod *smod)
  359. {
  360. LDAPMod *mod = slapi_mods_get_first_mod (smods);
  361. if (mod == NULL)
  362. return NULL;
  363. mod2smod (mod, smod);
  364. return smod;
  365. }
  366. Slapi_Mod *
  367. slapi_mods_get_next_smod(Slapi_Mods *smods, Slapi_Mod *smod)
  368. {
  369. LDAPMod *mod = slapi_mods_get_next_mod(smods);
  370. if (mod == NULL)
  371. {
  372. return NULL;
  373. }
  374. else
  375. {
  376. mod2smod(mod, smod);
  377. }
  378. return smod;
  379. }
  380. void
  381. slapi_mods_iterator_backone(Slapi_Mods *smods)
  382. {
  383. smods->iterator--;
  384. }
  385. static void
  386. pack_mods(LDAPMod ***modsp)
  387. {
  388. LDAPMod **mods = NULL;
  389. if (NULL != modsp && NULL != *modsp)
  390. {
  391. int i;
  392. int num_slots;
  393. int src_index, dst_index;
  394. mods = *modsp;
  395. /* Make a pass through the array, freeing any marked LDAP_MODS_IGNORE */
  396. i = 0;
  397. while (NULL != mods[i])
  398. {
  399. if (LDAP_MOD_IGNORE == (mods[i]->mod_op & ~LDAP_MOD_BVALUES))
  400. {
  401. /* Free current slot */
  402. slapi_ch_free((void**)&mods[i]->mod_type);
  403. ber_bvecfree(mods[i]->mod_bvalues);
  404. slapi_ch_free((void **)&mods[i]);
  405. }
  406. i++;
  407. }
  408. num_slots = i + 1; /* Remember total number of slots */
  409. /* Make another pass, packing the array */
  410. dst_index = src_index = 0;
  411. while (src_index < num_slots)
  412. {
  413. if (NULL != mods[src_index])
  414. {
  415. mods[dst_index] = mods[src_index];
  416. dst_index++;
  417. }
  418. src_index++;
  419. }
  420. mods[dst_index] = NULL;
  421. if (NULL == mods[0])
  422. {
  423. /* Packed it down to size zero - deallocate */
  424. slapi_ch_free((void **)modsp);
  425. }
  426. }
  427. }
  428. LDAPMod **
  429. slapi_mods_get_ldapmods_byref(Slapi_Mods *smods)
  430. {
  431. pack_mods(&smods->mods); /* XXXggood const gets in the way of this */
  432. return smods->mods;
  433. }
  434. LDAPMod **
  435. slapi_mods_get_ldapmods_passout(Slapi_Mods *smods)
  436. {
  437. LDAPMod **mods;
  438. pack_mods(&smods->mods);
  439. mods = smods->mods;
  440. smods->free_mods = 0;
  441. slapi_mods_done(smods);
  442. return mods;
  443. }
  444. int
  445. slapi_mods_get_num_mods(const Slapi_Mods *smods)
  446. {
  447. return smods->num_mods;
  448. }
  449. void
  450. slapi_mods_dump(const Slapi_Mods *smods, const char *text)
  451. {
  452. int i;
  453. LDAPDebug( LDAP_DEBUG_ANY, "smod - %s\n", text, 0, 0);
  454. for(i=0;i<smods->num_mods;i++)
  455. {
  456. slapi_mod_dump(smods->mods[i],i);
  457. }
  458. }
  459. /* ======== Utility functions for manipulating an LDAPMod ======= */
  460. /*
  461. * Slapi_Mod can be used in two ways:
  462. * 1) To wrap an existing array of LDAPMods, or
  463. * 2) To create a new array of LDAPMods.
  464. *
  465. * Slapi_Mods provides memory management and array manipulation
  466. * functions to make using (LDAPMod**) easier.
  467. *
  468. */
  469. Slapi_Mod *
  470. slapi_mod_new ()
  471. {
  472. Slapi_Mod *mod = (Slapi_Mod*)slapi_ch_calloc (1, sizeof (Slapi_Mod));
  473. return mod;
  474. }
  475. void
  476. slapi_mod_init(Slapi_Mod *smod, int initCount)
  477. {
  478. PR_ASSERT(smod!=NULL);
  479. memset (smod, 0, sizeof (*smod));
  480. smod->free_mod= 1;
  481. smod->num_elements = initCount + 1;
  482. smod->mod = (LDAPMod *)slapi_ch_calloc (1, sizeof (LDAPMod));
  483. if (smod->num_elements)
  484. smod->mod->mod_bvalues = (struct berval**)slapi_ch_calloc (smod->num_elements, sizeof (struct berval*));
  485. }
  486. void
  487. slapi_mod_init_passin(Slapi_Mod *smod, LDAPMod *mod)
  488. {
  489. PR_ASSERT(smod!=NULL);
  490. memset (smod, 0, sizeof (*smod));
  491. smod->free_mod= 1;
  492. if (mod!=NULL)
  493. {
  494. smod->mod= mod;
  495. if(smod->mod->mod_bvalues!=NULL)
  496. {
  497. while (smod->mod->mod_bvalues[smod->num_values]!=NULL)
  498. smod->num_values++;
  499. smod->num_elements= smod->num_values +1; /* We assume there's nothing spare on the end. */
  500. }
  501. }
  502. }
  503. void
  504. slapi_mod_init_byref(Slapi_Mod *smod, LDAPMod *mod)
  505. {
  506. PR_ASSERT(smod!=NULL);
  507. memset (smod, 0, sizeof (*smod));
  508. if (mod!=NULL)
  509. {
  510. smod->mod= mod;
  511. if(smod->mod->mod_bvalues!=NULL)
  512. {
  513. while (smod->mod->mod_bvalues[smod->num_values]!=NULL)
  514. smod->num_values++;
  515. smod->num_elements= smod->num_values +1; /* We assume there's nothing spare on the end. */
  516. }
  517. }
  518. }
  519. void
  520. slapi_mod_init_byval (Slapi_Mod *smod, const LDAPMod *mod)
  521. {
  522. PR_ASSERT(smod!=NULL);
  523. memset (smod, 0, sizeof (*smod));
  524. if (mod!=NULL)
  525. {
  526. smod->mod = (LDAPMod *)slapi_ch_calloc (1, sizeof (LDAPMod));
  527. smod->free_mod = 1;
  528. slapi_mod_set_operation (smod, mod->mod_op);
  529. slapi_mod_set_type (smod, mod->mod_type);
  530. if(mod->mod_bvalues!=NULL)
  531. {
  532. while (mod->mod_bvalues[smod->num_values]!=NULL)
  533. {
  534. slapi_mod_add_value (smod, mod->mod_bvalues[smod->num_values]);
  535. }
  536. }
  537. }
  538. }
  539. void
  540. slapi_mod_init_valueset_byval(Slapi_Mod *smod, int op, const char *type, const Slapi_ValueSet *svs)
  541. {
  542. PR_ASSERT(smod!=NULL);
  543. slapi_mod_init(smod, 0);
  544. slapi_mod_set_operation (smod, op);
  545. slapi_mod_set_type (smod, type);
  546. if (svs!=NULL) {
  547. Slapi_Value **svary = valueset_get_valuearray(svs);
  548. ber_bvecfree(smod->mod->mod_bvalues);
  549. smod->mod->mod_bvalues = NULL;
  550. valuearray_get_bervalarray(svary, &smod->mod->mod_bvalues);
  551. smod->num_values = slapi_valueset_count(svs);
  552. smod->num_elements = smod->num_values + 1;
  553. }
  554. }
  555. void
  556. slapi_mod_free (Slapi_Mod **smod)
  557. {
  558. slapi_mod_done(*smod);
  559. slapi_ch_free((void**)smod);
  560. *smod= NULL;
  561. }
  562. void
  563. slapi_mod_done(Slapi_Mod *smod)
  564. {
  565. PR_ASSERT(smod!=NULL);
  566. if(smod->free_mod)
  567. {
  568. ber_bvecfree(smod->mod->mod_bvalues);
  569. slapi_ch_free((void**)&(smod->mod->mod_type));
  570. slapi_ch_free((void**)&(smod->mod));
  571. }
  572. memset (smod, 0, sizeof(*smod));
  573. }
  574. /*
  575. * Add a value to the list of values in the modification.
  576. */
  577. void
  578. slapi_mod_add_value(Slapi_Mod *smod, const struct berval *val)
  579. {
  580. PR_ASSERT(smod!=NULL);
  581. PR_ASSERT(val!=NULL);
  582. /* PR_ASSERT(slapi_mod_get_operation(smod) & LDAP_MOD_BVALUES);*/
  583. bervalarray_add_berval_fast(&(smod->mod->mod_bvalues),(struct berval*)val,smod->num_values,&smod->num_elements);
  584. smod->num_values++;
  585. }
  586. /*
  587. * Remove the value at the iterator from the list of values in
  588. * the LDAP modification.
  589. */
  590. void
  591. slapi_mod_remove_value(Slapi_Mod *smod)
  592. {
  593. /* loop over the mod values moving them down to cover up the value to be removed */
  594. struct berval **vals;
  595. int i, k;
  596. PR_ASSERT(smod!=NULL);
  597. PR_ASSERT(smod->mod!=NULL);
  598. vals= smod->mod->mod_bvalues;
  599. i= smod->iterator-1;
  600. ber_bvfree( vals[i] );
  601. for ( k = i + 1; vals[k] != NULL; k++ ) {
  602. vals[k - 1] = vals[k];
  603. }
  604. vals[k - 1] = NULL;
  605. /* Reset the iterator */
  606. smod->num_values--;
  607. smod->iterator--;
  608. }
  609. struct berval *
  610. slapi_mod_get_first_value(Slapi_Mod *smod)
  611. {
  612. PR_ASSERT(smod!=NULL);
  613. /* Reset the iterator in the mod structure */
  614. smod->iterator= 0;
  615. return slapi_mod_get_next_value(smod);
  616. }
  617. struct berval *
  618. slapi_mod_get_next_value(Slapi_Mod *smod)
  619. {
  620. /* Move the iterator forward */
  621. struct berval *r= NULL;
  622. PR_ASSERT(smod!=NULL);
  623. PR_ASSERT(smod->mod!=NULL);
  624. if(smod->iterator<smod->num_values)
  625. {
  626. r= smod->mod->mod_bvalues[smod->iterator];
  627. smod->iterator++;
  628. }
  629. return r;
  630. }
  631. int
  632. slapi_mod_get_num_values(const Slapi_Mod *smod)
  633. {
  634. PR_ASSERT(smod!=NULL);
  635. return smod->num_values;
  636. }
  637. const char *
  638. slapi_mod_get_type (const Slapi_Mod *smod)
  639. {
  640. PR_ASSERT(smod!=NULL);
  641. PR_ASSERT(smod->mod!=NULL);
  642. return smod->mod->mod_type;
  643. }
  644. int
  645. slapi_mod_get_operation (const Slapi_Mod *smod)
  646. {
  647. PR_ASSERT(smod!=NULL);
  648. PR_ASSERT(smod->mod!=NULL);
  649. return smod->mod->mod_op;
  650. }
  651. void
  652. slapi_mod_set_type (Slapi_Mod *smod, const char *type)
  653. {
  654. PR_ASSERT(smod!=NULL);
  655. PR_ASSERT(smod->mod!=NULL);
  656. if(smod->mod->mod_type!=NULL)
  657. {
  658. slapi_ch_free((void**)&smod->mod->mod_type);
  659. }
  660. smod->mod->mod_type = slapi_ch_strdup (type);
  661. }
  662. void
  663. slapi_mod_set_operation (Slapi_Mod *smod, int op)
  664. {
  665. PR_ASSERT(smod!=NULL);
  666. PR_ASSERT(smod->mod!=NULL);
  667. smod->mod->mod_op = op;
  668. }
  669. const LDAPMod *
  670. slapi_mod_get_ldapmod_byref(const Slapi_Mod *smod)
  671. {
  672. PR_ASSERT(smod!=NULL);
  673. return smod->mod;
  674. }
  675. LDAPMod *
  676. slapi_mod_get_ldapmod_passout(Slapi_Mod *smod)
  677. {
  678. LDAPMod *mod;
  679. PR_ASSERT(smod!=NULL);
  680. mod= smod->mod;
  681. smod->free_mod= 0;
  682. slapi_mod_done(smod);
  683. return mod;
  684. }
  685. /* a valid LDAPMod is one with operation of LDAP_MOD_ADD, *_DELETE, *_REPLACE
  686. ored with LDAP_MOD_BVALUES; non-null type and at list one value
  687. for add and replace operations
  688. */
  689. int
  690. slapi_mod_isvalid (const Slapi_Mod *mod)
  691. {
  692. int op;
  693. if (mod == NULL || mod->mod == NULL)
  694. return 0;
  695. op = mod->mod->mod_op;
  696. if (!SLAPI_IS_MOD_ADD(op) && !SLAPI_IS_MOD_DELETE(op) && !SLAPI_IS_MOD_REPLACE(op))
  697. return 0;
  698. if (mod->mod->mod_type == NULL)
  699. return 0;
  700. /* add op must have at least 1 value */
  701. if (SLAPI_IS_MOD_ADD(op) && (mod->num_values == 0))
  702. return 0;
  703. return 1;
  704. }
  705. void
  706. slapi_mod_dump(LDAPMod *mod, int n)
  707. {
  708. if(mod!=NULL)
  709. {
  710. int operationtype= mod->mod_op & ~LDAP_MOD_BVALUES;
  711. switch ( operationtype )
  712. {
  713. case LDAP_MOD_ADD:
  714. LDAPDebug( LDAP_DEBUG_ANY, "smod %d - add: %s\n", n, mod->mod_type, 0);
  715. break;
  716. case LDAP_MOD_DELETE:
  717. LDAPDebug( LDAP_DEBUG_ANY, "smod %d - delete: %s\n", n, mod->mod_type, 0);
  718. break;
  719. case LDAP_MOD_REPLACE:
  720. LDAPDebug( LDAP_DEBUG_ANY, "smod %d - replace: %s\n", n, mod->mod_type, 0);
  721. break;
  722. case LDAP_MOD_IGNORE:
  723. LDAPDebug( LDAP_DEBUG_ANY, "smod %d - ignore: %s\n", n, mod->mod_type, 0);
  724. break;
  725. }
  726. if(operationtype!=LDAP_MOD_IGNORE)
  727. {
  728. int i;
  729. for ( i = 0; mod->mod_bvalues != NULL && mod->mod_bvalues[i] != NULL; i++ )
  730. {
  731. char *buf, *bufp;
  732. int len = strlen( mod->mod_type );
  733. len = LDIF_SIZE_NEEDED( len, mod->mod_bvalues[i]->bv_len ) + 1;
  734. buf = slapi_ch_malloc( len );
  735. bufp = buf;
  736. slapi_ldif_put_type_and_value_with_options( &bufp, mod->mod_type, mod->mod_bvalues[i]->bv_val, mod->mod_bvalues[i]->bv_len, 0 );
  737. *bufp = '\0';
  738. LDAPDebug( LDAP_DEBUG_ANY, "smod %d - value: %s", n, buf, 0);
  739. slapi_ch_free( (void**)&buf );
  740. }
  741. }
  742. }
  743. else
  744. {
  745. LDAPDebug( LDAP_DEBUG_ANY, "smod - null\n", 0, 0, 0);
  746. }
  747. }