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
|
/* $OpenLDAP$ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2024 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in the file LICENSE in the
* top-level directory of the distribution or, alternatively, at
* <http://www.OpenLDAP.org/license.html>.
*/
#include "portable.h"
#include "slap.h"
#include "slap-config.h"
int
bverb_to_mask( struct berval *bword, slap_verbmasks *v ) {
int i;
for ( i = 0; !BER_BVISNULL(&v[i].word); i++ ) {
if ( !ber_bvstrcasecmp( bword, &v[i].word) ) break;
}
return i;
}
int
verb_to_mask( const char *word, slap_verbmasks *v ) {
struct berval bword;
ber_str2bv( word, 0, 0, &bword );
return bverb_to_mask( &bword, v );
}
int
verbs_to_mask( int argc, char *argv[], slap_verbmasks *v, slap_mask_t *m ) {
int i, j;
for (i = 1; i < argc; i++ ) {
j = verb_to_mask( argv[i], v );
if ( BER_BVISNULL(&v[j].word) ) return i;
while ( !v[j].mask ) j--;
*m |= v[j].mask;
}
return 0;
}
/*
* Mask keywords that represent multiple bits should occur before single
* bit keywords in the verbmasks array.
*/
int
mask_to_verbs( slap_verbmasks *v, slap_mask_t m, BerVarray *bva ) {
int i, rc = 1;
if ( m ) {
for ( i=0; !BER_BVISNULL(&v[i].word); i++ ) {
if (!v[i].mask) continue;
if ( ( m & v[i].mask ) == v[i].mask ) {
value_add_one( bva, &v[i].word );
rc = 0;
m ^= v[i].mask;
if ( !m ) break;
}
}
}
return rc;
}
/* Return the verbs as a single string, separated by delim */
int
mask_to_verbstring( slap_verbmasks *v, slap_mask_t m0, char delim, struct berval *bv )
{
int i, rc = 1;
BER_BVZERO( bv );
if ( m0 ) {
slap_mask_t m = m0;
char *ptr;
for ( i=0; !BER_BVISNULL(&v[i].word); i++ ) {
if ( !v[i].mask ) continue;
if ( ( m & v[i].mask ) == v[i].mask ) {
bv->bv_len += v[i].word.bv_len + 1;
rc = 0;
m ^= v[i].mask;
if ( !m ) break;
}
}
bv->bv_val = ch_malloc(bv->bv_len);
bv->bv_len--;
ptr = bv->bv_val;
m = m0;
for ( i=0; !BER_BVISNULL(&v[i].word); i++ ) {
if ( !v[i].mask ) continue;
if ( ( m & v[i].mask ) == v[i].mask ) {
ptr = lutil_strcopy( ptr, v[i].word.bv_val );
*ptr++ = delim;
m ^= v[i].mask;
if ( !m ) break;
}
}
ptr[-1] = '\0';
}
return rc;
}
/* Parse a verbstring */
int
verbstring_to_mask( slap_verbmasks *v, char *str, char delim, slap_mask_t *m ) {
int j;
char *d;
struct berval bv;
do {
bv.bv_val = str;
d = strchr( str, delim );
if ( d )
bv.bv_len = d - str;
else
bv.bv_len = strlen( str );
j = bverb_to_mask( &bv, v );
if ( BER_BVISNULL(&v[j].word) ) return 1;
while ( !v[j].mask ) j--;
*m |= v[j].mask;
str += bv.bv_len + 1;
} while ( d );
return 0;
}
int
slap_verbmasks_init( slap_verbmasks **vp, slap_verbmasks *v )
{
int i;
assert( *vp == NULL );
for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) /* EMPTY */;
*vp = ch_calloc( i + 1, sizeof( slap_verbmasks ) );
for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
ber_dupbv( &(*vp)[ i ].word, &v[ i ].word );
*((slap_mask_t *)&(*vp)[ i ].mask) = v[ i ].mask;
}
BER_BVZERO( &(*vp)[ i ].word );
return 0;
}
int
slap_verbmasks_destroy( slap_verbmasks *v )
{
int i;
assert( v != NULL );
for ( i = 0; !BER_BVISNULL( &v[ i ].word ); i++ ) {
ch_free( v[ i ].word.bv_val );
}
ch_free( v );
return 0;
}
int
slap_verbmasks_append(
slap_verbmasks **vp,
slap_mask_t m,
struct berval *v,
slap_mask_t *ignore )
{
int i;
if ( !m ) {
return LDAP_OPERATIONS_ERROR;
}
for ( i = 0; !BER_BVISNULL( &(*vp)[ i ].word ); i++ ) {
if ( !(*vp)[ i ].mask ) continue;
if ( ignore != NULL ) {
int j;
for ( j = 0; ignore[ j ] != 0; j++ ) {
if ( (*vp)[ i ].mask == ignore[ j ] ) {
goto check_next;
}
}
}
if ( ( m & (*vp)[ i ].mask ) == (*vp)[ i ].mask ) {
if ( ber_bvstrcasecmp( v, &(*vp)[ i ].word ) == 0 ) {
/* already set; ignore */
return LDAP_SUCCESS;
}
/* conflicts */
return LDAP_TYPE_OR_VALUE_EXISTS;
}
if ( m & (*vp)[ i ].mask ) {
/* conflicts */
return LDAP_CONSTRAINT_VIOLATION;
}
check_next:;
}
*vp = ch_realloc( *vp, sizeof( slap_verbmasks ) * ( i + 2 ) );
ber_dupbv( &(*vp)[ i ].word, v );
*((slap_mask_t *)&(*vp)[ i ].mask) = m;
BER_BVZERO( &(*vp)[ i + 1 ].word );
return LDAP_SUCCESS;
}
int
enum_to_verb(slap_verbmasks *v, slap_mask_t m, struct berval *bv) {
int i;
for (i=0; !BER_BVISNULL(&v[i].word); i++) {
if ( m == v[i].mask ) {
if ( bv != NULL ) {
*bv = v[i].word;
}
return i;
}
}
return -1;
}
/* register a new verbmask */
int
slap_verbmask_register(
slap_verbmasks *vm_,
slap_verbmasks **vmp,
struct berval *bv,
int mask )
{
slap_verbmasks *vm = *vmp;
int i;
/* check for duplicate word */
/* NOTE: we accept duplicate codes; the first occurrence will be used
* when mapping from mask to verb */
i = verb_to_mask( bv->bv_val, vm );
if ( !BER_BVISNULL( &vm[ i ].word ) ) {
return -1;
}
for ( i = 0; !BER_BVISNULL( &vm[ i ].word ); i++ )
;
if ( vm == vm_ ) {
/* first time: duplicate array */
vm = ch_calloc( i + 2, sizeof( slap_verbmasks ) );
for ( i = 0; !BER_BVISNULL( &vm_[ i ].word ); i++ )
{
ber_dupbv( &vm[ i ].word, &vm_[ i ].word );
*((slap_mask_t*)&vm[ i ].mask) = vm_[ i ].mask;
}
} else {
vm = ch_realloc( vm, (i + 2) * sizeof( slap_verbmasks ) );
}
ber_dupbv( &vm[ i ].word, bv );
*((slap_mask_t*)&vm[ i ].mask) = mask;
BER_BVZERO( &vm[ i+1 ].word );
*vmp = vm;
return i;
}
|