]> git.sur5r.net Git - openldap/blob - servers/slapd/mods.c
1fa5804553840b47fee0b79b3b91ea446cd0b1cb
[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         const char *op;
41         Attribute *a;
42
43         switch( mod->sm_op ) {
44         case LDAP_MOD_ADD:
45                 op = "add";
46                 break;
47         case LDAP_MOD_REPLACE:
48                 op = "replace";
49                 break;
50         default:
51                 op = "?";
52                 assert( 0 );
53         }
54
55         a = attr_find( e->e_attrs, mod->sm_desc );
56         if( a != NULL ) { /* check if values to add exist in attribute */
57                 int     rc, i, j;
58                 MatchingRule *mr;
59
60                 mr = mod->sm_desc->ad_type->sat_equality;
61                 if( mr == NULL || !mr->smr_match ) {
62                         /* do not allow add of additional attribute
63                                 if no equality rule exists */
64                         *text = textbuf;
65                         snprintf( textbuf, textlen,
66                                 "modify/%s: %s: no equality matching rule",
67                                 op, mod->sm_desc->ad_cname.bv_val );
68                         return LDAP_INAPPROPRIATE_MATCHING;
69                 }
70
71                 /* no normalization is done in this routine nor
72                  * in the matching routines called by this routine. 
73                  * values are now normalized once on input to the
74                  * server (whether from LDAP or from the underlying
75                  * database).
76                  */
77                 for ( i=0; mod->sm_values[i].bv_val != NULL; i++ ) {
78                         for ( j=0; a->a_vals[j].bv_val; j++ ) {
79                                 int match;
80                                 if( mod->sm_nvalues ) {
81                                         rc = value_match( &match, mod->sm_desc, mr,
82                                                 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
83                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
84                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
85                                                 &a->a_nvals[j], &mod->sm_nvalues[i], text );
86                                 } else {
87                                         rc = value_match( &match, mod->sm_desc, mr,
88                                                 SLAP_MR_EQUALITY | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
89                                                 &a->a_vals[i], &mod->sm_values[i], text );
90                                 }
91
92                                 if( rc != LDAP_SUCCESS ) {
93                                         *text = textbuf;
94                                         snprintf( textbuf, textlen,
95                                                 "modify/%s: %s: value #%d already exists",
96                                                 op, mod->sm_desc->ad_cname.bv_val, i );
97                                         return LDAP_TYPE_OR_VALUE_EXISTS;
98                                 }
99                         }
100                 }
101         }
102
103         /* no - add them */
104         if( attr_merge( e, mod->sm_desc, mod->sm_values, mod->sm_nvalues ) != 0 ) {
105                 /* this should return result of attr_merge */
106                 *text = textbuf;
107                 snprintf( textbuf, textlen,
108                         "modify/%s: %s: merge error",
109                         op, mod->sm_desc->ad_cname.bv_val );
110                 return LDAP_OTHER;
111         }
112
113         return LDAP_SUCCESS;
114 }
115
116 int
117 modify_delete_values(
118         Entry   *e,
119         Modification    *mod,
120         int     permissive,
121         const char      **text,
122         char *textbuf, size_t textlen )
123 {
124         int             i, j, k, rc = LDAP_SUCCESS;
125         Attribute       *a;
126         MatchingRule    *mr = mod->sm_desc->ad_type->sat_equality;
127         char            dummy = '\0';
128         int                     match = 0;
129
130         /*
131          * If permissive is set, then the non-existence of an 
132          * attribute is not treated as an error.
133          */
134
135         /* delete the entire attribute */
136         if ( mod->sm_values == NULL ) {
137                 rc = attr_delete( &e->e_attrs, mod->sm_desc );
138
139                 if( permissive ) {
140                         rc = LDAP_SUCCESS;
141                 } else if( rc != LDAP_SUCCESS ) {
142                         *text = textbuf;
143                         snprintf( textbuf, textlen,
144                                 "modify/delete: %s: no such attribute",
145                                 mod->sm_desc->ad_cname.bv_val );
146                         rc = LDAP_NO_SUCH_ATTRIBUTE;
147                 }
148                 return rc;
149         }
150
151         if( mr == NULL || !mr->smr_match ) {
152                 /* disallow specific attributes from being deleted if
153                         no equality rule */
154                 *text = textbuf;
155                 snprintf( textbuf, textlen,
156                         "modify/delete: %s: no equality matching rule",
157                         mod->sm_desc->ad_cname.bv_val );
158                 return LDAP_INAPPROPRIATE_MATCHING;
159         }
160
161         /* delete specific values - find the attribute first */
162         if ( (a = attr_find( e->e_attrs, mod->sm_desc )) == NULL ) {
163                 if( permissive ) {
164                         return LDAP_SUCCESS;
165                 }
166                 *text = textbuf;
167                 snprintf( textbuf, textlen,
168                         "modify/delete: %s: no such attribute",
169                         mod->sm_desc->ad_cname.bv_val );
170                 return LDAP_NO_SUCH_ATTRIBUTE;
171         }
172
173         for ( i = 0; mod->sm_values[i].bv_val != NULL; i++ ) {
174                 int     found = 0;
175                 for ( j = 0; a->a_vals[j].bv_val != NULL; j++ ) {
176
177                         if( mod->sm_nvalues ) {
178                                 assert( a->a_nvals );
179                                 rc = (*mr->smr_match)( &match,
180                                         SLAP_MR_VALUE_OF_ASSERTION_SYNTAX
181                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
182                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
183                                         a->a_desc->ad_type->sat_syntax,
184                                         mr, &a->a_nvals[j],
185                                         &mod->sm_nvalues[i] );
186                         } else {
187 #if 0
188                                 assert( a->a_nvals == NULL );
189 #endif
190                                 rc = (*mr->smr_match)( &match,
191                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
192                                         a->a_desc->ad_type->sat_syntax,
193                                         mr, &a->a_vals[j],
194                                         &mod->sm_values[i] );
195                         }
196
197                         if ( rc != LDAP_SUCCESS ) {
198                                 *text = textbuf;
199                                 snprintf( textbuf, textlen,
200                                         "%s: matching rule failed",
201                                         mod->sm_desc->ad_cname.bv_val );
202                                 break;
203                         }
204
205                         if ( match != 0 ) {
206                                 continue;
207                         }
208
209                         found = 1;
210
211                         /* delete value and mark it as dummy */
212                         free( a->a_vals[j].bv_val );
213                         a->a_vals[j].bv_val = &dummy;
214                         if( a->a_nvals != a->a_vals ) {
215                                 free( a->a_nvals[j].bv_val );
216                                 a->a_nvals[j].bv_val = &dummy;
217                         }
218
219                         break;
220                 }
221
222                 if ( found == 0 ) {
223                         *text = textbuf;
224                         snprintf( textbuf, textlen,
225                                 "modify/delete: %s: no such value",
226                                 mod->sm_desc->ad_cname.bv_val );
227                         rc = LDAP_NO_SUCH_ATTRIBUTE;
228                         if ( i > 0 ) {
229                                 break;
230                         } else {
231                                 goto return_results;
232                         }
233                 }
234         }
235
236         /* compact array skipping dummies */
237         for ( k = 0, j = 0; a->a_vals[k].bv_val != NULL; k++ ) {
238                 /* skip dummies */
239                 if( a->a_vals[k].bv_val == &dummy ) {
240                         assert( a->a_nvals == NULL || a->a_nvals[k].bv_val == &dummy );
241                         continue;
242                 }
243                 if ( j != k ) {
244                         a->a_vals[ j ] = a->a_vals[ k ];
245                         if (a->a_nvals != a->a_vals) {
246                                 a->a_nvals[ j ] = a->a_nvals[ k ];
247                         }
248                 }
249
250                 j++;
251         }
252
253         a->a_vals[j].bv_val = NULL;
254         if (a->a_nvals != a->a_vals) a->a_nvals[j].bv_val = NULL;
255
256         /* if no values remain, delete the entire attribute */
257         if ( a->a_vals[0].bv_val == NULL ) {
258                 if ( attr_delete( &e->e_attrs, mod->sm_desc ) ) {
259                         *text = textbuf;
260                         snprintf( textbuf, textlen,
261                                 "modify/delete: %s: no such attribute",
262                                 mod->sm_desc->ad_cname.bv_val );
263                         rc = LDAP_NO_SUCH_ATTRIBUTE;
264                 }
265         }
266
267 return_results:;
268
269         return rc;
270 }
271
272 int
273 modify_replace_values(
274         Entry   *e,
275         Modification    *mod,
276         int             permissive,
277         const char      **text,
278         char *textbuf, size_t textlen )
279 {
280         (void) attr_delete( &e->e_attrs, mod->sm_desc );
281
282         if ( mod->sm_values ) {
283                 return modify_add_values( e, mod, permissive, text, textbuf, textlen );
284         }
285
286         return LDAP_SUCCESS;
287 }
288
289 int
290 modify_increment_values(
291         Entry   *e,
292         Modification    *mod,
293         int     permissive,
294         const char      **text,
295         char *textbuf, size_t textlen )
296 {
297         Attribute *a;
298
299         a = attr_find( e->e_attrs, mod->sm_desc );
300         if( a == NULL ) {
301                 *text = textbuf;
302                 snprintf( textbuf, textlen,
303                         "modify/increment: %s: no such attribute",
304                         mod->sm_desc->ad_cname.bv_val );
305                 return LDAP_NO_SUCH_ATTRIBUTE;
306         }
307
308         if ( !strcmp( a->a_desc->ad_type->sat_syntax_oid, SLAPD_INTEGER_SYNTAX )) {
309                 int i;
310                 char str[sizeof(long)*3 + 2]; /* overly long */
311                 long incr = atol( mod->sm_values[0].bv_val );
312
313                 /* treat zero and errors as a no-op */
314                 if( incr == 0 ) {
315                         return LDAP_SUCCESS;
316                 }
317
318                 for( i=0; a->a_nvals[i].bv_val != NULL; i++ ) {
319                         char *tmp;
320                         long value = atol( a->a_nvals[i].bv_val );
321                         size_t strln = snprintf( str, sizeof(str), "%ld", value+incr );
322
323                         tmp = SLAP_REALLOC( a->a_nvals[i].bv_val, strln+1 );
324                         if( tmp == NULL ) {
325                                 *text = "modify/increment: reallocation error";
326                                 return LDAP_OTHER;;
327                         }
328                         a->a_nvals[i].bv_val = tmp;
329                         a->a_nvals[i].bv_len = strln;
330
331                         AC_MEMCPY( a->a_nvals[i].bv_val, str, strln+1 );
332                 }
333
334         } else {
335                 snprintf( textbuf, textlen,
336                         "modify/increment: %s: increment not supported for value syntax %s",
337                         mod->sm_desc->ad_cname.bv_val,
338                         a->a_desc->ad_type->sat_syntax_oid );
339                 return LDAP_CONSTRAINT_VIOLATION;
340         }
341
342         return LDAP_SUCCESS;
343 }
344
345 void
346 slap_mod_free(
347         Modification    *mod,
348         int                             freeit )
349 {
350         if ( mod->sm_values != NULL ) ber_bvarray_free( mod->sm_values );
351         mod->sm_values = NULL;
352
353         if ( mod->sm_nvalues != NULL ) ber_bvarray_free( mod->sm_nvalues );
354         mod->sm_nvalues = NULL;
355
356         if( freeit ) free( mod );
357 }
358
359 void
360 slap_mods_free(
361     Modifications       *ml )
362 {
363         Modifications *next;
364
365         for ( ; ml != NULL; ml = next ) {
366                 next = ml->sml_next;
367
368                 slap_mod_free( &ml->sml_mod, 0 );
369                 free( ml );
370         }
371 }
372