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