]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
Ready for release
[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                         }
115
116                         if( permissive && !match ) {
117                                 if( pmod.sm_nvalues ) {
118                                         pmod.sm_nvalues[p] = mod->sm_nvalues[i];
119                                 }
120                                 pmod.sm_values[p++] = mod->sm_values[i];
121                         }
122                 }
123
124                 if( permissive && p == 0 ) {
125                         /* all new values match exist */
126                         ch_free( pmod.sm_values );
127                         if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
128                         return LDAP_SUCCESS;
129                 }
130         }
131
132         /* no - add them */
133         rc = attr_merge( e, mod->sm_desc, pmod.sm_values, pmod.sm_nvalues );
134
135         if( a != NULL && permissive ) {
136                 ch_free( pmod.sm_values );
137                 if( pmod.sm_nvalues ) ch_free( pmod.sm_nvalues );
138         }
139
140         if( rc != 0 ) {
141                 /* this should return result of attr_merge */
142                 *text = textbuf;
143                 snprintf( textbuf, textlen,
144                         "modify/%s: %s: merge error",
145                         op, mod->sm_desc->ad_cname.bv_val );
146                 return LDAP_OTHER;
147         }
148
149         return LDAP_SUCCESS;
150 }
151
152 int
153 modify_delete_values(
154         Entry   *e,
155         Modification    *mod,
156         int     permissive,
157         const char      **text,
158         char *textbuf, size_t textlen )
159 {
160         int             i, j, k, rc = LDAP_SUCCESS;
161         Attribute       *a;
162         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
163         char            dummy = '\0';
164         int                     match = 0;
165
166         /*
167          * If permissive is set, then the non-existence of an 
168          * attribute is not treated as an error.
169          */
170
171         /* delete the entire attribute */
172         if ( mod->sm_values == NULL ) {
173                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
174
175                 if( permissive ) {
176                         rc = LDAP_SUCCESS;
177                 } else if( rc != LDAP_SUCCESS ) {
178                         *text = textbuf;
179                         snprintf( textbuf, textlen,
180                                 "modify/delete: %s: no such attribute",
181                                 mod->sm_desc->ad_cname.bv_val );
182                         rc = LDAP_NO_SUCH_ATTRIBUTE;
183                 }
184                 return rc;
185         }
186
187         if( mr == NULL || !mr->smr_match ) {
188                 /* disallow specific attributes from being deleted if
189                         no equality rule */
190                 *text = textbuf;
191                 snprintf( textbuf, textlen,
192                         "modify/delete: %s: no equality matching rule",
193                         mod->sm_desc->ad_cname.bv_val );
194                 return LDAP_INAPPROPRIATE_MATCHING;
195         }
196
197         /* delete specific values - find the attribute first */
198         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
199                 if( permissive ) {
200                         return LDAP_SUCCESS;
201                 }
202                 *text = textbuf;
203                 snprintf( textbuf, textlen,
204                         "modify/delete: %s: no such attribute",
205                         mod->sm_desc->ad_cname.bv_val );
206                 return LDAP_NO_SUCH_ATTRIBUTE;
207         }
208
209         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
210                 int     found = 0;
211                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
212
213                         if( mod->sm_nvalues ) {
214                                 assert( a->a_nvals );
215                                 rc = (*mr->smr_match)( &match,
216                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
217                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
218                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
219                                         a->a_desc->ad_type->sat_syntax,
220                                         mr, &a->a_nvals[j],
221                                         &mod->sm_nvalues[i] );
222                         } else {
223 #if 0
224                                 assert( a->a_nvals == NULL );
225 #endif
226                                 rc = (*mr->smr_match)( &match,
227                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
228                                         a->a_desc->ad_type->sat_syntax,
229                                         mr, &a->a_vals[j],
230                                         &mod->sm_values[i] );
231                         }
232
233                         if ( rc != LDAP_SUCCESS ) {
234                                 *text = textbuf;
235                                 snprintf( textbuf, textlen,
236                                         "%s: matching rule failed",
237                                         mod->sm_desc->ad_cname.bv_val );
238                                 break;
239                         }
240
241                         if ( match != 0 ) {
242                                 continue;
243                         }
244
245                         found = 1;
246
247                         /* delete value and mark it as dummy */
248                         free( a->a_vals[j].bv_val );
249                         a->a_vals[j].bv_val = &dummy;
250                         if( a->a_nvals != a->a_vals ) {
251                                 free( a->a_nvals[j].bv_val );
252                                 a->a_nvals[j].bv_val = &dummy;
253                         }
254
255                         break;
256                 }
257
258                 if ( found == 0 ) {
259                         *text = textbuf;
260                         snprintf( textbuf, textlen,
261                                 "modify/delete: %s: no such value",
262                                 mod->sm_desc->ad_cname.bv_val );
263                         rc = LDAP_NO_SUCH_ATTRIBUTE;
264                         if ( i > 0 ) {
265                                 break;
266                         } else {
267                                 goto return_results;
268                         }
269                 }
270         }
271
272         /* compact array skipping dummies */
273         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
274                 /* skip dummies */
275                 if( a->a_vals[k].bv_val == &dummy ) {
276                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
277                         continue;
278                 }
279                 if ( j != k ) {
280                         a->a_vals[ j ] = a->a_vals[ k ];
281                         if (a->a_nvals != a->a_vals) {
282                                 a->a_nvals[ j ] = a->a_nvals[ k ];
283                         }
284                 }
285
286                 j++;
287         }
288
289         a->a_vals[j].bv_val = NULL;
290         if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
291
292         /* if no values remain, delete the entire attribute */
293         if ( a->a_vals[0].bv_val == NULL ) {
294                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
295                         *text = textbuf;
296                         snprintf( textbuf, textlen,
297                                 "modify/delete: %s: no such attribute",
298                                 mod->sm_desc->ad_cname.bv_val );
299                         rc = LDAP_NO_SUCH_ATTRIBUTE;
300                 }
301         }
302
303 return_results:;
304
305         return rc;
306 }
307
308 int
309 modify_replace_values(
310         Entry   *e,
311         Modification    *mod,
312         int             permissive,
313         const char      **text,
314         char *textbuf, size_t textlen )
315 {
316         (void) attr_delete( &e->e_attrs, mod->sm_desc );
317
318         if ( mod->sm_values ) {
319                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
320         }
321
322         return LDAP_SUCCESS;
323 }
324
325 int
326 modify_increment_values(
327         Entry   *e,
328         Modification    *mod,
329         int     permissive,
330         const char      **text,
331         char *textbuf, size_t textlen )
332 {
333         Attribute *a;
334
335         a = attr_find( e->e_attrs, mod->sm_desc );
336         if( a == NULL ) {
337                 *text = textbuf;
338                 snprintf( textbuf, textlen,
339                         "modify/increment: %s: no such attribute",
340                         mod->sm_desc->ad_cname.bv_val );
341                 return LDAP_NO_SUCH_ATTRIBUTE;
342         }
343
344         if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
345                 int i;
346                 char str[sizeof(long)*3 + 2]; /* overly long */
347                 long incr = atol( mod->sm_values[0].bv_val );
348
349                 /* treat zero and errors as a no-op */
350                 if( incr == 0 ) {
351                         return LDAP_SUCCESS;
352                 }
353
354                 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
355                         char *tmp;
356                         long value = atol( a->a_nvals[i].bv_val );
357                         size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
358
359                         tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
360                         if( tmp == NULL ) {
361                                 *text = "modify/increment: reallocation error";
362                                 return LDAP_OTHER;;
363                         }
364                         a->a_nvals[i].bv_val = tmp;
365                         a->a_nvals[i].bv_len = strln;
366
367                         AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
368                 }
369
370         } else {
371                 snprintf( textbuf, textlen,
372                         "modify/increment: %s: increment not supported for value syntax %s",
373                         mod->sm_desc->ad_cname.bv_val,
374                         a->a_desc->ad_type->sat_syntax_oid );
375                 return LDAP_CONSTRAINT_VIOLATION;
376         }
377
378         return LDAP_SUCCESS;
379 }
380
381 void
382 slap_mod_free(
383         Modification    *mod,
384         int                             freeit )
385 {
386         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
387         mod->sm_values = NULL;
388
389         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
390         mod->sm_nvalues = NULL;
391
392         if( freeit ) free( mod );
393 }
394
395 void
396 slap_mods_free(
397     Modifications       *ml )
398 {
399         Modifications *next;
400
401         for ( ; ml != NULL; ml = next ) {
402                 next = ml->sml_next;
403
404                 slap_mod_free( &ml->sml_mod, 0 );
405                 free( ml );
406         }
407 }
408