modutil.c 18 KB

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