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