modutil.c 19 KB

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