]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
f2c7319db52b5d480987628b3d8902d3031dd717
[openldap] / servers / slapd / mods.c
1 /*
2  * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  */
5 /*
6  * Copyright (c) 1995 Regents of the University of Michigan.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms are permitted
10  * provided that this notice is preserved and that due credit is given
11  * to the University of Michigan at Ann Arbor. The name of the University
12  * may not be used to endorse or promote products derived from this
13  * software without specific prior written permission. This software
14  * is provided ``as is'' without express or implied warranty.
15  */
16
17 #include "portable.h"
18
19 #include <ac/string.h>
20
21 #include "slap.h"
22
23 int
24 modify_check_duplicates(
25         AttributeDescription    *ad,
26         MatchingRule            *mr,
27         BerVarray               vals,
28         BerVarray               mods,
29         int                     permissive,
30         const char      **text,
31         char *textbuf, size_t textlen )
32 {
33         int             i, j, numvals = 0, nummods,
34                         rc = LDAP_SUCCESS, matched;
35         /* this function is no longer used */
36         return rc;
37 }
38
39 int
40 modify_add_values(
41         Entry   *e,
42         Modification    *mod,
43         int     permissive,
44         const char      **text,
45         char *textbuf, size_t textlen )
46 {
47         int             i, j;
48         int             matched;
49         Attribute       *a;
50         MatchingRule *mr = mod->sm_desc->ad_type->sat_equality;
51         const char *op;
52
53         switch( mod->sm_op ) {
54         case LDAP_MOD_ADD:
55                 op = "add";
56                 break;
57         case LDAP_MOD_REPLACE:
58                 op = "replace";
59                 break;
60         default:
61                 op = "?";
62                 assert( 0 );
63         }
64
65         a = attr_find( e->e_attrs, mod->sm_desc );
66
67         /*
68          * With permissive set, as long as the attribute being added
69          * has the same value(s?) as the existing attribute, then the
70          * modify will succeed.
71          */
72
73         /* check if the values we're adding already exist */
74         if( mr == NULL || !mr->smr_match ) {
75                 if ( a != NULL ) {
76                         /* do not allow add of additional attribute
77                                 if no equality rule exists */
78                         *text = textbuf;
79                         snprintf( textbuf, textlen,
80                                 "modify/%s: %s: no equality matching rule",
81                                 op, mod->sm_desc->ad_cname.bv_val );
82                         return LDAP_INAPPROPRIATE_MATCHING;
83                 }
84
85                 for ( i = 0; mod->sm_bvalues[i].bv_val != NULL; i++ ) {
86                         /* test asserted values against existing values */
87                         if( a ) {
88                                 for( matched = 0, j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
89                                         if ( bvmatch( &mod->sm_bvalues[i], &a->a_vals[j] ) ) {
90                                                 if ( permissive ) {
91                                                         matched++;
92                                                         continue;
93                                                 }
94                                                 /* value exists already */
95                                                 *text = textbuf;
96                                                 snprintf( textbuf, textlen,
97                                                         "modify/%s: %s: value #%i already exists",
98                                                         op, mod->sm_desc->ad_cname.bv_val, j );
99                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
100                                         }
101                                 }
102                                 if ( permissive && matched == j ) {
103                                         /* values already exist; do nothing */
104                                         return LDAP_SUCCESS;
105                                 }
106                         }
107
108                         /* test asserted values against themselves */
109                         for( j = 0; j < i; j++ ) {
110                                 if ( bvmatch( &mod->sm_bvalues[i], &mod->sm_bvalues[j] ) ) {
111                                         /* value exists already */
112                                         *text = textbuf;
113                                         snprintf( textbuf, textlen,
114                                                 "modify/%s: %s: value #%i already exists",
115                                                 op, mod->sm_desc->ad_cname.bv_val, j );
116                                         return LDAP_TYPE_OR_VALUE_EXISTS;
117                                 }
118                         }
119                 }
120
121         } else {
122                 /* no normalization is done in this routine nor
123                  * in the matching routines called by this routine. 
124                  * values are now normalized once on input to the
125                  * server (whether from LDAP or from the underlying
126                  * database).
127                  * This should outperform the old code.  No numbers
128                  * are available yet.
129                  */
130
131                 int             rc;
132
133                 if ( mod->sm_bvalues[1].bv_val == 0 ) {
134                         if ( a != NULL ) {
135                                 int             i;
136
137                                 for ( matched = 0, i = 0; a->a_vals[ i ].bv_val; i++ ) {
138                                         int     match;
139
140                                         if( mod->sm_nvalues ) {
141                                                 rc = value_match( &match, mod->sm_desc, mr,
142                                                         SLAP_MR_EQUALITY
143                                                                 | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
144                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
145                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
146                                                         &a->a_nvals[i],
147                                                         &mod->sm_nvalues[0],
148                                                         text );
149
150                                         } else {
151                                                 rc = value_match( &match, mod->sm_desc, mr,
152                                                         SLAP_MR_EQUALITY
153                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
154                                                         &a->a_vals[i],
155                                                         &mod->sm_values[0],
156                                                         text );
157                                         }
158
159
160                                         if( rc == LDAP_SUCCESS && match == 0 ) {
161                                                 if ( permissive ) {
162                                                         matched++;
163                                                         continue;
164                                                 }
165                                                 *text = textbuf;
166                                                 snprintf( textbuf, textlen,
167                                                         "modify/%s: %s: value #0 already exists",
168                                                         op, mod->sm_desc->ad_cname.bv_val );
169                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
170                                         }
171                                 }
172                                 if ( permissive && matched == i ) {
173                                         /* values already exist; do nothing */
174                                         return LDAP_SUCCESS;
175                                 }
176                         }
177
178                 } else {
179                         rc = modify_check_duplicates( mod->sm_desc, mr,
180                                 a ? a->a_vals : NULL, mod->sm_bvalues,
181                                 permissive, text, textbuf, textlen );
182
183                         if ( permissive && rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
184                                 return LDAP_SUCCESS;
185                         }
186
187                         if ( rc != LDAP_SUCCESS ) {
188                                 return rc;
189                         }
190                 }
191         }
192
193         /* no - add them */
194         if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
195                 /* this should return result of attr_merge */
196                 *text = textbuf;
197                 snprintf( textbuf, textlen,
198                         "modify/%s: %s: merge error",
199                         op, mod->sm_desc->ad_cname.bv_val );
200                 return LDAP_OTHER;
201         }
202
203         return LDAP_SUCCESS;
204 }
205
206 int
207 modify_delete_values(
208         Entry   *e,
209         Modification    *mod,
210         int     permissive,
211         const char      **text,
212         char *textbuf, size_t textlen
213 )
214 {
215         int             i, j, k, rc = LDAP_SUCCESS;
216         Attribute       *a;
217         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
218         char            dummy = '\0';
219         int                     match = 0;
220
221         /*
222          * If permissive is set, then the non-existence of an 
223          * attribute is not treated as an error.
224          */
225
226         /* delete the entire attribute */
227         if ( mod->sm_bvalues == NULL ) {
228                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
229
230                 if( permissive ) {
231                         rc = LDAP_SUCCESS;
232                 } else if( rc != LDAP_SUCCESS ) {
233                         *text = textbuf;
234                         snprintf( textbuf, textlen,
235                                 "modify/delete: %s: no such attribute",
236                                 mod->sm_desc->ad_cname.bv_val );
237                         rc = LDAP_NO_SUCH_ATTRIBUTE;
238                 }
239                 return rc;
240         }
241
242         if( mr == NULL || !mr->smr_match ) {
243                 /* disallow specific attributes from being deleted if
244                         no equality rule */
245                 *text = textbuf;
246                 snprintf( textbuf, textlen,
247                         "modify/delete: %s: no equality matching rule",
248                         mod->sm_desc->ad_cname.bv_val );
249                 return LDAP_INAPPROPRIATE_MATCHING;
250         }
251
252         /* delete specific values - find the attribute first */
253         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
254                 if( permissive ) {
255                         return LDAP_SUCCESS;
256                 }
257                 *text = textbuf;
258                 snprintf( textbuf, textlen,
259                         "modify/delete: %s: no such attribute",
260                         mod->sm_desc->ad_cname.bv_val );
261                 return LDAP_NO_SUCH_ATTRIBUTE;
262         }
263
264
265         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
266                 int     found = 0;
267                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
268
269                         if( mod->sm_nvalues ) {
270                                 assert( a->a_nvals );
271                                 rc = (*mr->smr_match)( &match,
272                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
273                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
274                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
275                                         a->a_desc->ad_type->sat_syntax,
276                                         mr, &a->a_nvals[j],
277                                         &mod->sm_nvalues[i] );
278                         } else {
279 #if 0
280                                 assert( a->a_nvals == NULL );
281 #endif
282                                 rc = (*mr->smr_match)( &match,
283                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
284                                         a->a_desc->ad_type->sat_syntax,
285                                         mr, &a->a_vals[j],
286                                         &mod->sm_values[i] );
287                         }
288
289                         if ( rc != LDAP_SUCCESS ) {
290                                 *text = textbuf;
291                                 snprintf( textbuf, textlen,
292                                         "%s: matching rule failed",
293                                         mod->sm_desc->ad_cname.bv_val );
294                                 break;
295                         }
296
297                         if ( match != 0 ) {
298                                 continue;
299                         }
300
301                         found = 1;
302
303                         /* delete value and mark it as dummy */
304                         free( a->a_vals[j].bv_val );
305                         a->a_vals[j].bv_val = &dummy;
306                         if( a->a_nvals != a->a_vals ) {
307                                 free( a->a_nvals[j].bv_val );
308                                 a->a_nvals[j].bv_val = &dummy;
309                         }
310
311                         break;
312                 }
313
314                 if ( found == 0 ) {
315                         *text = textbuf;
316                         snprintf( textbuf, textlen,
317                                 "modify/delete: %s: no such value",
318                                 mod->sm_desc->ad_cname.bv_val );
319                         rc = LDAP_NO_SUCH_ATTRIBUTE;
320                         if ( i > 0 ) {
321                                 break;
322                         } else {
323                                 goto return_results;
324                         }
325                 }
326         }
327
328         /* compact array skipping dummies */
329         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
330                 /* skip dummies */
331                 if( a->a_vals[k].bv_val == &dummy ) {
332                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
333                         continue;
334                 }
335                 if ( j != k ) {
336                         a->a_vals[ j ] = a->a_vals[ k ];
337                         if (a->a_nvals != a->a_vals) {
338                                 a->a_nvals[ j ] = a->a_nvals[ k ];
339                         }
340                 }
341
342                 j++;
343         }
344
345         a->a_vals[j].bv_val = NULL;
346         if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
347
348         /* if no values remain, delete the entire attribute */
349         if ( a->a_vals[0].bv_val == NULL ) {
350                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
351                         *text = textbuf;
352                         snprintf( textbuf, textlen,
353                                 "modify/delete: %s: no such attribute",
354                                 mod->sm_desc->ad_cname.bv_val );
355                         rc = LDAP_NO_SUCH_ATTRIBUTE;
356                 }
357         }
358
359 return_results:;
360
361         return rc;
362 }
363
364 int
365 modify_replace_values(
366         Entry   *e,
367         Modification    *mod,
368         int             permissive,
369         const char      **text,
370         char *textbuf, size_t textlen )
371 {
372         (void) attr_delete( &e->e_attrs, mod->sm_desc );
373
374         if ( mod->sm_bvalues ) {
375                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
376         }
377
378         return LDAP_SUCCESS;
379 }
380
381 int
382 modify_increment_values(
383         Entry   *e,
384         Modification    *mod,
385         int     permissive,
386         const char      **text,
387         char *textbuf, size_t textlen )
388 {
389         Attribute *a;
390
391         a = attr_find( e->e_attrs, mod->sm_desc );
392         if( a == NULL ) {
393                 *text = textbuf;
394                 snprintf( textbuf, textlen,
395                         "modify/increment: %s: no such attribute",
396                         mod->sm_desc->ad_cname.bv_val );
397                 return LDAP_NO_SUCH_ATTRIBUTE;
398         }
399
400
401         if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
402                 int i;
403                 char str[sizeof(long)*3 + 2]; /* overly long */
404                 long incr = atol( mod->sm_bvalues[0].bv_val );
405
406                 /* treat zero and errors as a no-op */
407                 if( incr == 0 ) {
408                         return LDAP_SUCCESS;
409                 }
410
411                 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
412                         char *tmp;
413                         long value = atol( a->a_nvals[i].bv_val );
414                         size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
415
416                         tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
417                         if( tmp == NULL ) {
418                                 *text = "modify/increment: reallocation error";
419                                 return LDAP_OTHER;;
420                         }
421                         a->a_nvals[i].bv_val = tmp;
422                         a->a_nvals[i].bv_len = strln;
423
424                         AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
425                 }
426
427         } else {
428                 snprintf( textbuf, textlen,
429                         "modify/increment: %s: increment not supported for value syntax %s",
430                         mod->sm_desc->ad_cname.bv_val,
431                         a->a_desc->ad_type->sat_syntax_oid );
432                 return LDAP_CONSTRAINT_VIOLATION;
433         }
434
435         return LDAP_SUCCESS;
436 }
437
438 void
439 slap_mod_free(
440         Modification    *mod,
441         int                             freeit )
442 {
443         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
444         mod->sm_values = NULL;
445
446         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
447         mod->sm_nvalues = NULL;
448
449         if( freeit ) free( mod );
450 }
451
452 void
453 slap_mods_free(
454     Modifications       *ml )
455 {
456         Modifications *next;
457
458         for ( ; ml != NULL; ml = next ) {
459                 next = ml->sml_next;
460
461                 slap_mod_free( &ml->sml_mod, 0 );
462                 free( ml );
463         }
464 }
465