summaryrefslogtreecommitdiffstats
path: root/ldap/servers/slapd/modutil.c
blob: 82147c691d049e3f97d24e336eab5c7ab039342e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/** BEGIN COPYRIGHT BLOCK
 * This Program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; version 2 of the License.
 * 
 * This Program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA.
 * 
 * In addition, as a special exception, Red Hat, Inc. gives You the additional
 * right to link the code of this Program with code not covered under the GNU
 * General Public License ("Non-GPL Code") and to distribute linked combinations
 * including the two, subject to the limitations in this paragraph. Non-GPL Code
 * permitted under this exception must only link to the code of this Program
 * through those well defined interfaces identified in the file named EXCEPTION
 * found in the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline functions from
 * the Approved Interfaces without causing the resulting work to be covered by
 * the GNU General Public License. Only Red Hat, Inc. may make changes or
 * additions to the list of Approved Interfaces. You must obey the GNU General
 * Public License in all respects for all of the Program code and other code used
 * in conjunction with the Program except the Non-GPL Code covered by this
 * exception. If you modify this file, you may extend this exception to your
 * version of the file, but you are not obligated to do so. If you do not wish to
 * provide this exception without modification, you must delete this exception
 * statement from your version and license this file solely under the GPL without
 * exception. 
 * 
 * 
 * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
 * Copyright (C) 2005 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

/* modutil.c - modify utility routine */

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#endif /* _WIN32 */					 
#include "slap.h"

#define SIZE_INIT 4	/* initial element size */
#define SIZE_INC  2	/* size increment */
/*
 * Free an array of LDAPMod structures.  Just like ldap_mods_free,
 * except that it assumes that the mods are expressed as a bervec.
 */
void
freepmods( LDAPMod **pmods )
{
    int	i;

    for ( i = 0; pmods[ i ] != NULL; ++i ) {
	if ( pmods[ i ]->mod_bvalues != NULL ) {
	    ber_bvecfree( pmods[ i ]->mod_bvalues );
	}
	if ( pmods[ i ]->mod_type != NULL ) {
	    slapi_ch_free((void**)&pmods[ i ]->mod_type );
	}
	slapi_ch_free((void**)&pmods[ i ] );
    }
    slapi_ch_free((void**)&pmods );
}

/* ======= Utility functions for manipulating a list of LDAPMods ======= */

/*
 * Slapi_Mods can be used in two ways:
 * 1) To wrap an existing array of LDAPMods, or
 * 2) To create a new array of LDAPMods.
 *
 * Slapi_Mods provides memory management and array manipulation
 * functions to make using (LDAPMod**) easier.
 *
 */

Slapi_Mods* 
slapi_mods_new()
{
	Slapi_Mods *mods;
	mods = (Slapi_Mods*) slapi_ch_calloc (1, sizeof (Slapi_Mods));
	return mods;
}

/*
 * Initialise a Slapi_Mod suggesting how big an LDAPMod array is needed.
 * It will be free'd when the Slapi_Mods is destroyed.
 */
void
slapi_mods_init(Slapi_Mods *smods, int initCount)
{    
	memset (smods, 0, sizeof (*smods));
	smods->free_mods = 1;
	if (initCount > 0)
	{
		smods->num_elements= initCount + 1;	/* one for NULL element */
		smods->mods = (LDAPMod **) slapi_ch_calloc( 1, smods->num_elements * sizeof(LDAPMod *) );	
	}
}

/*
 * Initialise a Slapi_Mod passing in responsibility for the (LDAPMod **).
 * It will be free'd when the Slapi_Mods is destroyed.
 */
void
slapi_mods_init_passin(Slapi_Mods *smods, LDAPMod **mods)
{    
	slapi_mods_init_byref(smods, mods);
	smods->free_mods = 1;
}

/*
 * Initialise a Slapi_Mod passing in a reference to the (LDAPMod **)
 * It will *not* be free'd when the Slapi_Mods is destroyed.
 */
void
slapi_mods_init_byref(Slapi_Mods *smods, LDAPMod **mods)
{    
	memset (smods, 0, sizeof (*smods));
	if(mods!=NULL)
	{
		smods->mods = mods;
		for ( smods->num_mods = 0; mods[smods->num_mods] != NULL; smods->num_mods++ );
		smods->num_elements= smods->num_mods+1; /* We assume there's nothing spare on the end. */
	}
}

void 
slapi_mods_free(Slapi_Mods **smods)
{
	if(smods!=NULL && *smods!=NULL)
	{
		slapi_mods_done(*smods);
		slapi_ch_free ((void**)smods);
		*smods= NULL;
	}
}

void 
slapi_mods_done(Slapi_Mods *smods)
{
	PR_ASSERT(smods!=NULL);
	if (smods->mods!=NULL)
	{
		if(smods->free_mods)
		{
			ldap_mods_free (smods->mods, 1 /* Free the Array and the Elements */);
		}
	}
	memset(smods, 0, sizeof(*smods));
}

static void
slapi_mods_add_one_element(Slapi_Mods *smods)
{
	int	need = smods->num_mods + 2;
	if ( smods->num_elements == 0 )
	{
		PR_ASSERT(smods->mods==NULL);
		smods->num_elements = SIZE_INIT;
		smods->mods = (LDAPMod **) slapi_ch_malloc( smods->num_elements * sizeof(LDAPMod *) );
		smods->free_mods= 1;
	}
	if ( smods->num_elements < need )
	{
		PR_ASSERT(smods->free_mods);
		smods->num_elements *= SIZE_INC;
		smods->mods = (LDAPMod **) slapi_ch_realloc( (char *) smods->mods, smods->num_elements * sizeof(LDAPMod *) );
	}
}

/*
 * Shift everything down to make room to insert the new mod. 
 */
void
slapi_mods_insert_at(Slapi_Mods *smods, LDAPMod *mod, int pos)
{
	int	i;
	Slapi_Attr a = {0};

	if (NULL == mod) {
		return;
	}
	slapi_mods_add_one_element(smods);
	for( i=smods->num_mods-1; i>=pos; i--)
	{
	    smods->mods[i+1]= smods->mods[i];
	}
	slapi_attr_init(&a, mod->mod_type);
	/* Check if the type of the to-be-added values has DN syntax or not. */
	if (slapi_attr_is_dn_syntax_attr(&a)) {
		int rc = 0;
		struct berval **mbvp = NULL;
		char *normed = NULL;
		size_t len = 0;
		for (mbvp = mod->mod_bvalues; mbvp && *mbvp; mbvp++) {
			rc = slapi_dn_normalize_ext((*mbvp)->bv_val, (*mbvp)->bv_len,
										&normed, &len);
			if (rc > 0) {
				slapi_ch_free((void **)&((*mbvp)->bv_val));
			} else if (rc == 0) { 
				/* original is passed in; not null terminated */
				*(normed + len) = '\0';
			}
			(*mbvp)->bv_val = normed;
			(*mbvp)->bv_len = len;
		}
	}
	attr_done(&a);
	smods->mods[pos]= mod;
	smods->num_mods++;
	smods->mods[smods->num_mods]= NULL;
}

void 
slapi_mods_insert_smod_at(Slapi_Mods *smods, Slapi_Mod *smod, int pos)
{
	slapi_mods_insert_at (smods, smod->mod, pos);	
}

/*
 * Shift everything down to make room to insert the new mod. 
 */
void
slapi_mods_insert_before(Slapi_Mods *smods, LDAPMod *mod)
{
    slapi_mods_insert_at(smods, mod, smods->iterator);
	smods->iterator++;
}

void 
slapi_mods_insert_smod_before(Slapi_Mods *smods, Slapi_Mod *smod)
{
	slapi_mods_insert_before(smods, smod->mod);
}

/*
 * Shift everything down to make room to insert the new mod. 
 */
void
slapi_mods_insert_after(Slapi_Mods *smods, LDAPMod *mod)
{
    slapi_mods_insert_at(smods, mod, smods->iterator+1);
}

void slapi_mods_insert_smod_after(Slapi_Mods *smods, Slapi_Mod *smod)
{
	slapi_mods_insert_after(smods, smod->mod);
}

/*
 * Add the LDAPMod to the end of the array.
 * Does NOT copy the mod.
 */
void
slapi_mods_add_ldapmod(Slapi_Mods *smods, LDAPMod *mod)
{
    slapi_mods_insert_at(smods,mod,smods->num_mods);
}

void 
slapi_mods_add_smod(Slapi_Mods *smods, Slapi_Mod *smod)
{
	slapi_mods_add_ldapmod(smods, smod->mod);
}

/*
 * Makes a copy of everything.
 */
void
slapi_mods_add_modbvps( Slapi_Mods *smods, int modtype, const char *type, struct berval **bvps )
{
	LDAPMod *mod;

	mod = (LDAPMod *) slapi_ch_malloc(sizeof(LDAPMod));
	mod->mod_type = slapi_ch_strdup( type );
	mod->mod_op = modtype | LDAP_MOD_BVALUES;
	mod->mod_bvalues = NULL;
	
	if (NULL != bvps)
	{
		int num_values, i;
		num_values = 0;
		/* Count mods */
		while (NULL != bvps[num_values])
		{
			num_values++;
		}
		mod->mod_bvalues = (struct berval **)slapi_ch_malloc((num_values + 1) *
			sizeof(struct berval *));
		for (i = 0; i < num_values; i++)
		{
			mod->mod_bvalues[i] = ber_bvdup((struct berval *)bvps[i]); /* jcm had to cast away const */
		}
		mod->mod_bvalues[num_values] = NULL;
	}
	slapi_mods_add_ldapmod(smods, mod);
}

/*
 * Makes a copy of everything.
 */
void
slapi_mods_add_mod_values( Slapi_Mods *smods, int modtype, const char *type, Slapi_Value **va )
{
    LDAPMod *mod= (LDAPMod *) slapi_ch_malloc( sizeof(LDAPMod) );
    mod->mod_type = slapi_ch_strdup( type );
    mod->mod_op = modtype | LDAP_MOD_BVALUES;
	mod->mod_bvalues= NULL;
	valuearray_get_bervalarray(va,&mod->mod_bvalues);
    slapi_mods_add_ldapmod(smods, mod);
}

/*
 * Makes a copy of everything.
 */
void
slapi_mods_add( Slapi_Mods *smods, int modtype, const char *type, unsigned long len, const char *val)
{
	struct berval bv;
	struct berval *bvps[2];
	if(len>0)
	{
		bv.bv_len= len;
		bv.bv_val= (void*)val; /* We cast away the const, but we're not going to change anything */
		bvps[0] = &bv;
		bvps[1] = NULL;
	}
	else
	{
		bvps[0]= NULL;
	}
    slapi_mods_add_modbvps( smods, modtype, type, bvps );
}

/*
 * Makes a copy of everything.
 */
void
slapi_mods_add_string( Slapi_Mods *smods, int modtype, const char *type, const char *val)
{
	PR_ASSERT(val);
	slapi_mods_add( smods, modtype, type, strlen(val), val);
}

void
slapi_mods_remove(Slapi_Mods *smods)
{
	smods->mods[smods->iterator]->mod_op= LDAP_MOD_IGNORE;
}

LDAPMod *
slapi_mods_get_first_mod(Slapi_Mods *smods)
{
    /* Reset the iterator in the mod structure */
    smods->iterator= -1;
	return slapi_mods_get_next_mod(smods);
}

LDAPMod *
slapi_mods_get_next_mod(Slapi_Mods *smods)
{
    /* Move the iterator forward */
    LDAPMod *r= NULL;
	smods->iterator++;

	PR_ASSERT (smods->iterator >= 0);

	/* skip deleted mods if any */
	while (smods->iterator < smods->num_mods && smods->mods[smods->iterator]->mod_op == LDAP_MOD_IGNORE)
		smods->iterator ++;
	
	if(smods->iterator<smods->num_mods)
	{
	    r= smods->mods[smods->iterator];
	}
	return r;
}

static void 
mod2smod (LDAPMod *mod, Slapi_Mod *smod)
{
	smod->mod = mod;
	smod->iterator = 0;
	smod->num_values = 0;

	if (mod->mod_op & LDAP_MOD_BVALUES)
	{
		while (mod->mod_bvalues && mod->mod_bvalues[smod->num_values])
		{
			smod->num_values ++;
		}
	}
	else
	{
		PR_ASSERT(0); /* ggood shouldn't ever use string values in server */
		while (mod->mod_values && mod->mod_values[smod->num_values])
		{
			smod->num_values ++;
		}
	}

	smod->num_elements = smod->num_values + 1; /* 1- for null char */
}

Slapi_Mod *
slapi_mods_get_first_smod(Slapi_Mods *smods, Slapi_Mod *smod)
{
	LDAPMod *mod = slapi_mods_get_first_mod (smods);

	if (mod == NULL)
		return NULL;

	mod2smod (mod, smod);
	
	return smod;
}

Slapi_Mod *
slapi_mods_get_next_smod(Slapi_Mods *smods, Slapi_Mod *smod)
{
	LDAPMod *mod = slapi_mods_get_next_mod(smods);

	if (mod == NULL)
	{
		return NULL;
	}
	else
	{
		mod2smod(mod, smod);
	}
	return smod;
}

void
slapi_mods_iterator_backone(Slapi_Mods *smods)
{
    smods->iterator--;
}


static void
pack_mods(LDAPMod ***modsp)
{
	LDAPMod **mods = NULL;
	if (NULL != modsp && NULL != *modsp)
	{
		int i;
		int num_slots;
		int src_index, dst_index;
		mods = *modsp;

		/* Make a pass through the array, freeing any marked LDAP_MODS_IGNORE */
		i = 0;
		while (NULL != mods[i])
		{
			if (LDAP_MOD_IGNORE == (mods[i]->mod_op & ~LDAP_MOD_BVALUES))
			{
			  /* Free current slot */
			  slapi_ch_free((void**)&mods[i]->mod_type);
			  ber_bvecfree(mods[i]->mod_bvalues);
			  slapi_ch_free((void **)&mods[i]);
			}
			i++;
		}
		num_slots = i + 1; /* Remember total number of slots */

		/* Make another pass, packing the array */
		dst_index = src_index = 0;
		while (src_index < num_slots)
		{
			if (NULL != mods[src_index])
			{
				mods[dst_index] = mods[src_index];
				dst_index++;
			}
			src_index++;
		}
		mods[dst_index] = NULL;
		if (NULL == mods[0])
		{
			/* Packed it down to size zero - deallocate */
			slapi_ch_free((void **)modsp);
		}
	}
}


LDAPMod **
slapi_mods_get_ldapmods_byref(Slapi_Mods *smods)
{
	pack_mods(&smods->mods); /* XXXggood const gets in the way of this */
    return smods->mods;
}

LDAPMod **
slapi_mods_get_ldapmods_passout(Slapi_Mods *smods)
{
    LDAPMod **mods; 
	pack_mods(&smods->mods);
    mods = smods->mods;
	smods->free_mods = 0;
	slapi_mods_done(smods);
	return mods;
}

int
slapi_mods_get_num_mods(const Slapi_Mods *smods)
{
    return smods->num_mods;
}

void
slapi_mods_dump(const Slapi_Mods *smods, const char *text)
{
	int i;
	LDAPDebug( LDAP_DEBUG_ANY, "smod - %s\n", text, 0, 0);
	for(i=0;i<smods->num_mods;i++)
	{
		slapi_mod_dump(smods->mods[i],i);
	}
}

/* ======== Utility functions for manipulating an LDAPMod ======= */

/*
 * Slapi_Mod can be used in two ways:
 * 1) To wrap an existing array of LDAPMods, or
 * 2) To create a new array of LDAPMods.
 *
 * Slapi_Mods provides memory management and array manipulation
 * functions to make using (LDAPMod**) easier.
 *
 */

Slapi_Mod *
slapi_mod_new ()
{
	Slapi_Mod *mod = (Slapi_Mod*)slapi_ch_calloc (1, sizeof (Slapi_Mod));
	return mod;
}

void
slapi_mod_init(Slapi_Mod *smod, int initCount)
{
	PR_ASSERT(smod!=NULL);
	memset (smod, 0, sizeof (*smod));
	smod->free_mod= 1;
	smod->num_elements = initCount + 1;
	smod->mod = (LDAPMod *)slapi_ch_calloc (1, sizeof (LDAPMod));
	if (smod->num_elements)
		smod->mod->mod_bvalues = (struct berval**)slapi_ch_calloc (smod->num_elements, sizeof (struct berval*));
}

void
slapi_mod_init_passin(Slapi_Mod *smod, LDAPMod *mod)
{
	PR_ASSERT(smod!=NULL);
	memset (smod, 0, sizeof (*smod));
	smod->free_mod= 1;
	if (mod!=NULL)
	{
		smod->mod= mod;
		if(smod->mod->mod_bvalues!=NULL)
		{
			while (smod->mod->mod_bvalues[smod->num_values]!=NULL)
				smod->num_values++;
			smod->num_elements= smod->num_values +1; /* We assume there's nothing spare on the end. */
		}
	}
}

void
slapi_mod_init_byref(Slapi_Mod *smod, LDAPMod *mod)
{
	PR_ASSERT(smod!=NULL);
	memset (smod, 0, sizeof (*smod));
	if (mod!=NULL)
	{
		smod->mod= mod;
		if(smod->mod->mod_bvalues!=NULL)
		{
			while (smod->mod->mod_bvalues[smod->num_values]!=NULL)
				smod->num_values++;
			smod->num_elements= smod->num_values +1; /* We assume there's nothing spare on the end. */
		}
	}
}

void 
slapi_mod_init_byval (Slapi_Mod *smod, const LDAPMod *mod)
{
	PR_ASSERT(smod!=NULL);
	memset (smod, 0, sizeof (*smod));
	if (mod!=NULL)
	{
		smod->mod = (LDAPMod *)slapi_ch_calloc (1, sizeof (LDAPMod));
		smod->free_mod = 1;
		slapi_mod_set_operation (smod, mod->mod_op);
		slapi_mod_set_type (smod, mod->mod_type);
		if(mod->mod_bvalues!=NULL)
		{
			while (mod->mod_bvalues[smod->num_values]!=NULL)
			{
				slapi_mod_add_value (smod, mod->mod_bvalues[smod->num_values]);
			}
		}
	}
}

void
slapi_mod_init_valueset_byval(Slapi_Mod *smod, int op, const char *type, const Slapi_ValueSet *svs)
{
	PR_ASSERT(smod!=NULL);
	slapi_mod_init(smod, 0);
	slapi_mod_set_operation (smod, op);
	slapi_mod_set_type (smod, type);
	if (svs!=NULL) {
		Slapi_Value **svary = valueset_get_valuearray(svs);
		ber_bvecfree(smod->mod->mod_bvalues);
		smod->mod->mod_bvalues = NULL;
		valuearray_get_bervalarray(svary, &smod->mod->mod_bvalues);
		smod->num_values = slapi_valueset_count(svs);
		smod->num_elements = smod->num_values + 1;
	}
}

void
slapi_mod_free (Slapi_Mod **smod)
{
	slapi_mod_done(*smod);
	slapi_ch_free((void**)smod);
	*smod= NULL;
}

void
slapi_mod_done(Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
	if(smod->free_mod)
	{
		ber_bvecfree(smod->mod->mod_bvalues);
		slapi_ch_free((void**)&(smod->mod->mod_type));
		slapi_ch_free((void**)&(smod->mod));
	}
	memset (smod, 0, sizeof(*smod));
}

/*
 * Add a value to the list of values in the modification.
 */
void
slapi_mod_add_value(Slapi_Mod *smod, const struct berval *val)
{
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(val!=NULL);
/*	PR_ASSERT(slapi_mod_get_operation(smod) & LDAP_MOD_BVALUES);*/
	
    bervalarray_add_berval_fast(&(smod->mod->mod_bvalues),(struct berval*)val,smod->num_values,&smod->num_elements);
	smod->num_values++;
}

/*
 * Remove the value at the iterator from the list of values in
 * the LDAP modification. 
 */
void
slapi_mod_remove_value(Slapi_Mod *smod)
{
    /* loop over the mod values moving them down to cover up the value to be removed */
	struct berval **vals;
	int i, k;
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	vals= smod->mod->mod_bvalues;
	i= smod->iterator-1;
	ber_bvfree( vals[i] );
	for ( k = i + 1; vals[k] != NULL; k++ ) {
		vals[k - 1] = vals[k];
	}
	vals[k - 1] = NULL;
    /* Reset the iterator */
	smod->num_values--;
    smod->iterator--;
}

struct berval *
slapi_mod_get_first_value(Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
    /* Reset the iterator in the mod structure */
    smod->iterator= 0;
	return slapi_mod_get_next_value(smod);
}

struct berval *
slapi_mod_get_next_value(Slapi_Mod *smod)
{
    /* Move the iterator forward */
    struct berval *r= NULL;
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	if(smod->iterator<smod->num_values)
	{
	    r= smod->mod->mod_bvalues[smod->iterator];
	    smod->iterator++;
	}
	return r;
}

int
slapi_mod_get_num_values(const Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
    return smod->num_values;
}

const char *
slapi_mod_get_type (const Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	return smod->mod->mod_type;
}

int 
slapi_mod_get_operation (const Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	return smod->mod->mod_op;
}

void 
slapi_mod_set_type (Slapi_Mod *smod, const char *type)
{ 
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	if(smod->mod->mod_type!=NULL)
	{
		slapi_ch_free((void**)&smod->mod->mod_type);
	}
	smod->mod->mod_type = slapi_ch_strdup (type);
}

void 
slapi_mod_set_operation (Slapi_Mod *smod, int op)
{
	PR_ASSERT(smod!=NULL);
	PR_ASSERT(smod->mod!=NULL);
	smod->mod->mod_op = op;
}

const LDAPMod *
slapi_mod_get_ldapmod_byref(const Slapi_Mod *smod)
{
	PR_ASSERT(smod!=NULL);
	return smod->mod;
}

LDAPMod *
slapi_mod_get_ldapmod_passout(Slapi_Mod *smod)
{
	LDAPMod *mod;
	PR_ASSERT(smod!=NULL);
	mod= smod->mod;
	smod->free_mod= 0;
	slapi_mod_done(smod);
	return mod;
}

/* a valid LDAPMod is one with operation of LDAP_MOD_ADD, *_DELETE, *_REPLACE
   ored with LDAP_MOD_BVALUES; non-null type and at list one value
   for add and replace operations
 */
int 
slapi_mod_isvalid (const Slapi_Mod *mod)
{
	int op;

	if (mod == NULL || mod->mod == NULL)
		return 0;

	op = mod->mod->mod_op;

	if (!SLAPI_IS_MOD_ADD(op) && !SLAPI_IS_MOD_DELETE(op) && !SLAPI_IS_MOD_REPLACE(op))
		return 0;

	if (mod->mod->mod_type == NULL)
		return 0;

	/* add op must have at least 1 value */
	if (SLAPI_IS_MOD_ADD(op) && (mod->num_values == 0))
		return 0;

	return 1;
}

void
slapi_mod_dump(LDAPMod *mod, int n)
{
	if(mod!=NULL)
	{
		int operationtype= mod->mod_op & ~LDAP_MOD_BVALUES;
	    switch ( operationtype )
		{
	    case LDAP_MOD_ADD:
			LDAPDebug( LDAP_DEBUG_ANY, "smod %d - add: %s\n", n, mod->mod_type, 0);
			break;

	    case LDAP_MOD_DELETE:
			LDAPDebug( LDAP_DEBUG_ANY, "smod %d - delete: %s\n", n, mod->mod_type, 0);
			break;

	    case LDAP_MOD_REPLACE:
			LDAPDebug( LDAP_DEBUG_ANY, "smod %d - replace: %s\n", n, mod->mod_type, 0);
			break;

	    case LDAP_MOD_IGNORE:
			LDAPDebug( LDAP_DEBUG_ANY, "smod %d - ignore: %s\n", n, mod->mod_type, 0);
			break;
	    }
		if(operationtype!=LDAP_MOD_IGNORE)
		{
			int i;
			for ( i = 0; mod->mod_bvalues != NULL && mod->mod_bvalues[i] != NULL; i++ )
			{
				char *buf, *bufp;
				int len = strlen( mod->mod_type );
				len = LDIF_SIZE_NEEDED( len, mod->mod_bvalues[i]->bv_len ) + 1;
				buf = slapi_ch_malloc( len );
				bufp = buf;
				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 );
				*bufp = '\0';
				LDAPDebug( LDAP_DEBUG_ANY, "smod %d - value: %s", n, buf, 0);
				slapi_ch_free( (void**)&buf );
			}
		}
	}
	else
	{
		LDAPDebug( LDAP_DEBUG_ANY, "smod - null\n", 0, 0, 0);
	}
}