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
|
/** 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
#include "slap.h"
#define FLAG_RDNS 0
Slapi_RDN *
slapi_rdn_new()
{
Slapi_RDN *rdn= (Slapi_RDN *)slapi_ch_malloc(sizeof(struct slapi_rdn));
slapi_rdn_init(rdn);
return rdn;
}
Slapi_RDN *
slapi_rdn_new_dn(const char *dn)
{
Slapi_RDN *rdn= slapi_rdn_new();
slapi_rdn_init_dn(rdn,dn);
return rdn;
}
Slapi_RDN *
slapi_rdn_new_sdn(const Slapi_DN *sdn)
{
Slapi_RDN *rdn= slapi_rdn_new();
slapi_rdn_init_sdn(rdn,sdn);
return rdn;
}
Slapi_RDN *
slapi_rdn_new_rdn(const Slapi_RDN *fromrdn)
{
Slapi_RDN *rdn= slapi_rdn_new();
slapi_rdn_init_rdn(rdn,fromrdn);
return rdn;
}
void
slapi_rdn_init(Slapi_RDN *rdn)
{
rdn->flag= 0;
rdn->rdn= NULL;
rdn->rdns= NULL;
rdn->butcheredupto= -1; /* Means we haven't started converting '=' to '\0' in rdns */
}
void
slapi_rdn_init_dn(Slapi_RDN *rdn,const char *dn)
{
slapi_rdn_init(rdn);
if(dn!=NULL)
{
char **dns= ldap_explode_dn(dn, 0);
if(dns!=NULL)
{
rdn->rdn= slapi_ch_strdup(dns[0]);
ldap_value_free(dns);
}
}
}
void
slapi_rdn_init_sdn(Slapi_RDN *rdn,const Slapi_DN *sdn)
{
if(sdn!=NULL)
{
slapi_rdn_init_dn(rdn,slapi_sdn_get_dn(sdn));
}
else
{
slapi_rdn_init(rdn);
}
}
void
slapi_rdn_init_rdn(Slapi_RDN *rdn,const Slapi_RDN *fromrdn)
{
slapi_rdn_init(rdn);
rdn->rdn= slapi_ch_strdup(fromrdn->rdn);
}
void
slapi_rdn_set_dn(Slapi_RDN *rdn,const char *dn)
{
slapi_rdn_done(rdn);
slapi_rdn_init_dn(rdn,dn);
}
void
slapi_rdn_set_sdn(Slapi_RDN *rdn,const Slapi_DN *sdn)
{
slapi_rdn_done(rdn);
slapi_rdn_init_sdn(rdn,sdn);
}
void
slapi_rdn_set_rdn(Slapi_RDN *rdn,const Slapi_RDN *fromrdn)
{
slapi_rdn_done(rdn);
slapi_rdn_init_rdn(rdn,fromrdn);
}
static char **
slapi_rdn_get_rdns(Slapi_RDN *rdn)
{
char **rdns= NULL;
/* Check if rdns is upto date */
if(!slapi_isbitset_uchar(rdn->flag,FLAG_RDNS))
{
if(rdn->rdns!=NULL)
{
ldap_value_free(rdn->rdns);
rdn->rdns= NULL;
}
if(rdn->rdn!=NULL)
{
rdn->rdns = ldap_explode_rdn( rdn->rdn, 0 );
rdns= rdn->rdns;
}
slapi_setbit_uchar(rdn->flag,FLAG_RDNS);
rdn->butcheredupto= -1;
}
return rdns;
}
void
slapi_rdn_free(Slapi_RDN **rdn)
{
if(rdn!=NULL)
{
slapi_rdn_done(*rdn);
slapi_ch_free((void**)rdn);
}
}
void
slapi_rdn_done(Slapi_RDN *rdn)
{
if(rdn!=NULL)
{
slapi_ch_free((void**)&(rdn->rdn));
ldap_value_free(rdn->rdns);
slapi_rdn_init(rdn);
}
}
int
slapi_rdn_get_first(Slapi_RDN *rdn, char **type, char **value)
{
return slapi_rdn_get_next(rdn, 0, type, value);
}
int
slapi_rdn_get_next(Slapi_RDN *rdn, int index, char **type, char **value)
{
int returnindex;
PR_ASSERT(index>=0);
if(rdn->rdns==NULL)
{
rdn->rdns= slapi_rdn_get_rdns(rdn);
}
if (rdn->rdns == NULL || rdn->rdns[index]==NULL)
{
*type= NULL;
*value= NULL;
returnindex= -1;
}
else
{
if(rdn->butcheredupto>=index)
{
/* the '=' has already been converted to a '\0' */
*type= rdn->rdns[index];
*value= *type + strlen(*type) + 1;
returnindex= ++index;
}
else
{
*type= strchr(rdn->rdns[index],'=');
if(*type==NULL)
{
/* This just shouldn't happen... */
*type= NULL;
*value= NULL;
returnindex= -1;
}
else
{
**type = '\0';
*value= *type;
(*value)++; /* Skip the '\0' */
*type = rdn->rdns[index];
rdn->butcheredupto= index;
returnindex= ++index;
}
}
}
return returnindex;
}
int
slapi_rdn_get_index(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
{
int result;
char *theValue;
result= slapi_rdn_get_index_attr(rdn, type, &theValue);
if(result!=-1)
{
if(theValue==NULL || (strncasecmp(value,theValue,length)!=0))
{
result= -1;
}
}
return result;
}
int
slapi_rdn_get_index_attr(Slapi_RDN *rdn, const char *type, char **value)
{
int result= -1;
int index;
char *theType;
index= slapi_rdn_get_first(rdn, &theType, value);
while(index!=-1)
{
if(theType!=NULL && value!=NULL &&
(strcasecmp(type,theType)==0))
{
result= index;
index= -1;
}
else
{
index= slapi_rdn_get_next(rdn, index, &theType, value);
}
}
return result;
}
int
slapi_rdn_contains_attr(Slapi_RDN *rdn, const char *type, char **value)
{
return (slapi_rdn_get_index_attr(rdn,type,value)!=-1);
}
int
slapi_rdn_contains(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
{
return (slapi_rdn_get_index(rdn,type,value,length)!=-1);
}
int
slapi_rdn_add(Slapi_RDN *rdn, const char *type, const char *value)
{
PR_ASSERT(NULL != type);
PR_ASSERT(NULL != value);
if(rdn->rdn==NULL)
{
/* type=value '\0' */
rdn->rdn= slapi_ch_malloc(strlen(type)+1+strlen(value)+1);
strcpy( rdn->rdn, type );
strcat( rdn->rdn, "=" );
strcat( rdn->rdn, value );
}
else
{
/* type=value+rdn '\0' */
char *newrdn= slapi_ch_malloc(strlen(type)+1+strlen(value)+1+strlen(rdn->rdn)+1);
strcpy( newrdn, type );
strcat( newrdn, "=" );
strcat( newrdn, value );
strcat( newrdn, "+" );
strcat( newrdn, rdn->rdn );
slapi_ch_free((void**)&rdn->rdn);
rdn->rdn= newrdn;
}
slapi_unsetbit_uchar(rdn->flag,FLAG_RDNS);
return 1;
}
int
slapi_rdn_remove_index(Slapi_RDN *rdn, int atindex)
{
Slapi_RDN newrdn;
int result= 0;
int index;
char *theType;
char *theValue;
slapi_rdn_init(&newrdn);
index= slapi_rdn_get_first(rdn, &theType, &theValue);
while(index!=-1)
{
if(index!=atindex)
{
slapi_rdn_add(&newrdn,theType,theValue);
}
else
{
result= 1;
}
index= slapi_rdn_get_next(rdn, index, &theType, &theValue);
}
if(result)
{
slapi_rdn_set_rdn(rdn,&newrdn);
}
slapi_rdn_done(&newrdn);
return result;
}
int
slapi_rdn_remove(Slapi_RDN *rdn, const char *type, const char *value, size_t length)
{
int result= 0;
if(rdn->rdn!=NULL)
{
int atindex= slapi_rdn_get_index(rdn, type, value, length);
if(atindex!=-1)
{
result= slapi_rdn_remove_index(rdn, atindex);
}
}
return result;
}
int
slapi_rdn_remove_attr(Slapi_RDN *rdn, const char *type)
{
int result= 0;
if(rdn->rdn!=NULL)
{
char *value;
int atindex= slapi_rdn_get_index_attr(rdn, type, &value);
if(atindex!=-1)
{
result= slapi_rdn_remove_index(rdn, atindex);
}
}
return result;
}
int
slapi_rdn_isempty(const Slapi_RDN *rdn)
{
return (rdn->rdn==NULL || rdn->rdn[0]=='\0');
}
int
slapi_rdn_get_num_components(Slapi_RDN *rdn)
{
int i= 0;
char **rdns= slapi_rdn_get_rdns(rdn);
if(rdns!=NULL)
{
for(i=0; rdns[i]!=NULL; i++);
}
return i;
}
int
slapi_rdn_compare(Slapi_RDN *rdn1, Slapi_RDN *rdn2)
{
int r= 1;
int n1= slapi_rdn_get_num_components(rdn1);
int n2= slapi_rdn_get_num_components(rdn2);
if (n1==n2)
{
char *type, *value;
int i= slapi_rdn_get_first(rdn1, &type, &value);
while(r==1 && i!=-1)
{
r= slapi_rdn_contains(rdn2, type, value, strlen(value));
i= slapi_rdn_get_next(rdn1, i, &type, &value);
}
if(r==1) /* All rdn1's rdn components were in rdn2 */
{
r= 0; /* SAME */
}
else
{
r= -1; /* NOT SAME */
}
}
else
{
r= -1; /* NOT SAME */
}
return r;
}
const char *
slapi_rdn_get_rdn(const Slapi_RDN *rdn)
{
return rdn->rdn;
}
const char *
slapi_rdn_get_nrdn(const Slapi_RDN *rdn)
{
/* JCM - Normalised RDN? */
PR_ASSERT(0);
return NULL;
}
|