]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
99e31fe033bec6a696287ce17a0da2fbde2dbb05
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5  */
6 /*
7  * Copyright (c) 1995 Regents of the University of Michigan.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms are permitted
11  * provided that this notice is preserved and that due credit is given
12  * to the University of Michigan at Ann Arbor. The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission. This software
15  * is provided ``as is'' without express or implied warranty.
16  */
17
18 #include "portable.h"
19
20 #include <stdio.h>
21
22 #include <ac/socket.h>
23 #include <ac/string.h>
24 #include <ac/time.h>
25
26 #include "ldap_pvt.h"
27 #include "slap.h"
28
29
30 int
31 do_modify(
32     Connection  *conn,
33     Operation   *op )
34 {
35         char            *dn, *ndn = NULL;
36         char            *last;
37         ber_tag_t       tag;
38         ber_len_t       len;
39         LDAPModList     *modlist = NULL;
40         LDAPModList     **modtail = &modlist;
41 #ifdef LDAP_DEBUG
42         LDAPModList *tmp;
43 #endif
44         Modifications *mods = NULL;
45         Backend         *be;
46         int rc;
47         const char      *text;
48         int manageDSAit;
49
50         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
51
52         /*
53          * Parse the modify request.  It looks like this:
54          *
55          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
56          *              name    DistinguishedName,
57          *              mods    SEQUENCE OF SEQUENCE {
58          *                      operation       ENUMERATED {
59          *                              add     (0),
60          *                              delete  (1),
61          *                              replace (2)
62          *                      },
63          *                      modification    SEQUENCE {
64          *                              type    AttributeType,
65          *                              values  SET OF AttributeValue
66          *                      }
67          *              }
68          *      }
69          */
70
71         if ( ber_scanf( op->o_ber, "{a" /*}*/, &dn ) == LBER_ERROR ) {
72                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
73                 send_ldap_disconnect( conn, op,
74                         LDAP_PROTOCOL_ERROR, "decoding error" );
75                 return SLAPD_DISCONNECT;
76         }
77
78         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn, 0, 0 );
79
80         /* collect modifications & save for later */
81
82         for ( tag = ber_first_element( op->o_ber, &len, &last );
83             tag != LBER_DEFAULT;
84             tag = ber_next_element( op->o_ber, &len, last ) )
85         {
86                 ber_int_t mop;
87
88                 (*modtail) = (LDAPModList *) ch_calloc( 1, sizeof(LDAPModList) );
89
90                 if ( ber_scanf( op->o_ber, "{i{a[V]}}", &mop,
91                     &(*modtail)->ml_type, &(*modtail)->ml_bvalues )
92                     == LBER_ERROR )
93                 {
94                         send_ldap_disconnect( conn, op,
95                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
96                         rc = SLAPD_DISCONNECT;
97                         goto cleanup;
98                 }
99
100                 switch( mop ) {
101                 case LDAP_MOD_ADD:
102                         if ( (*modtail)->ml_bvalues == NULL ) {
103                                 Debug( LDAP_DEBUG_ANY,
104                                         "do_modify: modify/add operation (%ld) requires values\n",
105                                         (long) mop, 0, 0 );
106                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
107                                         NULL, "modify/add operation requires values",
108                                         NULL, NULL );
109                                 rc = LDAP_PROTOCOL_ERROR;
110                                 goto cleanup;
111                         }
112
113                         /* fall through */
114
115                 case LDAP_MOD_DELETE:
116                 case LDAP_MOD_REPLACE:
117                         break;
118
119                 default: {
120                                 Debug( LDAP_DEBUG_ANY,
121                                         "do_modify: invalid modify operation (%ld)\n",
122                                         (long) mop, 0, 0 );
123                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
124                                         NULL, "unrecognized modify operation", NULL, NULL );
125                                 rc = LDAP_PROTOCOL_ERROR;
126                                 goto cleanup;
127                         }
128                 }
129
130                 (*modtail)->ml_op = mop;
131                 modtail = &(*modtail)->ml_next;
132         }
133         *modtail = NULL;
134
135         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
136                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
137                 goto cleanup;
138         }
139
140         ndn = ch_strdup( dn );
141
142         if(     dn_normalize( ndn ) == NULL ) {
143                 Debug( LDAP_DEBUG_ANY, "do_modify: invalid dn (%s)\n", dn, 0, 0 );
144                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
145                     "invalid DN", NULL, NULL );
146                 goto cleanup;
147         }
148
149         if( ndn == '\0' ) {
150                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
151                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
152                         NULL, "modify upon the root DSE not supported", NULL, NULL );
153                 goto cleanup;
154         }
155
156 #ifdef LDAP_DEBUG
157         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
158         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
159                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
160                         tmp->ml_op == LDAP_MOD_ADD
161                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
162                                         ? "delete" : "replace"), tmp->ml_type, 0 );
163         }
164 #endif
165
166         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
167             op->o_connid, op->o_opid, dn, 0, 0 );
168
169         manageDSAit = get_manageDSAit( op );
170
171         /*
172          * We could be serving multiple database backends.  Select the
173          * appropriate one, or send a referral to our "referral server"
174          * if we don't hold it.
175          */
176         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
177                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
178                         NULL, NULL, default_referral, NULL );
179                 goto cleanup;
180         }
181
182         /* check restrictions */
183         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
184         if( rc != LDAP_SUCCESS ) {
185                 send_ldap_result( conn, op, rc,
186                         NULL, text, NULL, NULL );
187                 goto cleanup;
188         }
189
190         /* check for referrals */
191         rc = backend_check_referrals( be, conn, op, dn, ndn );
192         if ( rc != LDAP_SUCCESS ) {
193                 goto cleanup;
194         }
195
196         /* deref suffix alias if appropriate */
197         ndn = suffix_alias( be, ndn );
198
199         /*
200          * do the modify if 1 && (2 || 3)
201          * 1) there is a modify function implemented in this backend;
202          * 2) this backend is master for what it holds;
203          * 3) it's a replica and the dn supplied is the update_ndn.
204          */
205         if ( be->be_modify ) {
206                 /* do the update here */
207 #ifndef SLAPD_MULTIMASTER
208                 /* we don't have to check for replicator dn
209                  * because we accept each modify request
210                  */
211                 if ( be->be_update_ndn == NULL ||
212                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
213 #endif
214                 {
215                         int update = be->be_update_ndn != NULL;
216                         const char *text;
217                         rc = slap_modlist2mods( modlist, update, &mods, &text );
218
219                         if( rc != LDAP_SUCCESS ) {
220                                 send_ldap_result( conn, op, rc,
221                                         NULL, text, NULL, NULL );
222                                 goto cleanup;
223                         }
224
225                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
226                                 global_lastmod == ON)) && !update )
227                         {
228                                 Modifications **modstail;
229                                 for( modstail = &mods;
230                                         *modstail != NULL;
231                                         modstail = &(*modstail)->sml_next )
232                                 {
233                                         /* empty */
234                                 }
235                                 rc = slap_mods_opattrs( op, modstail, &text );
236
237                                 if( rc != LDAP_SUCCESS ) {
238                                         send_ldap_result( conn, op, rc,
239                                                 NULL, text,
240                                                 NULL, NULL );
241                                         goto cleanup;
242                                 }
243                         }
244
245                         if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0 
246 #ifdef SLAPD_MULTIMASTER
247                                 && ( be->be_update_ndn == NULL ||
248                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
249 #endif
250                         ) {
251                                 /* but we log only the ones not from a replicator user */
252                                 replog( be, op, dn, mods );
253                         }
254
255 #ifndef SLAPD_MULTIMASTER
256                 /* send a referral */
257                 } else {
258                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
259                                 be->be_update_refs ? be->be_update_refs : default_referral,
260                                 NULL );
261 #endif
262                 }
263         } else {
264                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
265                     NULL, "operation not supported within namingContext", NULL, NULL );
266         }
267
268 cleanup:
269         free( dn );
270         if( ndn != NULL ) free( ndn );
271         if ( modlist != NULL )
272                 slap_modlist_free( modlist );
273         if ( mods != NULL )
274                 slap_mods_free( mods );
275         return rc;
276 }
277
278 /*
279  * convert a raw list of modifications to internal format
280  * Do basic attribute type checking and syntax validation.
281  */
282 int slap_modlist2mods(
283         LDAPModList *ml,
284         int update,
285         Modifications **mods,
286         const char **text )
287 {
288         int rc;
289         Modifications **modtail = mods;
290
291         for( ; ml != NULL; ml = ml->ml_next ) {
292                 Modifications *mod;
293                 AttributeDescription *ad = NULL;
294
295                 mod = (Modifications *)
296                         ch_calloc( 1, sizeof(Modifications) );
297
298                 /* copy the op */
299                 mod->sml_op = ml->ml_op;
300
301                 /* convert to attribute description */
302                 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
303
304                 if( rc != LDAP_SUCCESS ) {
305                         slap_mods_free( mod );
306                         return rc;
307                 }
308
309                 ad = mod->sml_desc;
310
311                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
312                         && !slap_ad_is_binary( ad ))
313                 {
314                         /* attribute requires binary transfer */
315                         slap_mods_free( mod );
316                         *text = "attribute requires ;binary transfer";
317                         return LDAP_UNDEFINED_TYPE;
318                 }
319
320                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
321                         && slap_ad_is_binary( ad ))
322                 {
323                         /* attribute requires binary transfer */
324                         slap_mods_free( mod );
325                         *text = "attribute disallows ;binary transfer";
326                         return LDAP_UNDEFINED_TYPE;
327                 }
328
329                 if (!update && is_at_no_user_mod( ad->ad_type )) {
330                         /* user modification disallowed */
331                         slap_mods_free( mod );
332                         *text = "no user modification allowed";
333                         return LDAP_CONSTRAINT_VIOLATION;
334                 }
335
336                 /*
337                  * check values
338                  */
339                 if( ml->ml_bvalues != NULL ) {
340                         ber_len_t nvals;
341                         slap_syntax_validate_func *validate =
342                                 ad->ad_type->sat_syntax->ssyn_validate;
343
344                         if( !validate ) {
345                                 Debug( LDAP_DEBUG_TRACE,
346                                         "modlist2mods: no validator for syntax %s\n",
347                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
348                                 slap_mods_free( mod );
349                                 *text = "no validator for syntax";
350                                 return LDAP_INVALID_SYNTAX;
351                         }
352
353                         /*
354                          * check that each value is valid per syntax
355                          */
356                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
357                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
358
359                                 if( rc != 0 ) {
360                                         slap_mods_free( mod );
361                                         *text = "value contains invalid data";
362                                         return LDAP_INVALID_SYNTAX;
363                                 }
364                         }
365
366                         /*
367                          * a rough single value check... an additional check is needed
368                          * to catch add of single value to existing single valued attribute
369                          */
370                         if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
371                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
372                         {
373                                 slap_mods_free( mod );
374                                 *text = "multiple values provided";
375                                 return LDAP_INVALID_SYNTAX;
376                         }
377                 }
378
379                 mod->sml_bvalues = ml->ml_bvalues;
380                 ml->ml_values = NULL;
381
382                 *modtail = mod;
383                 modtail = &mod->sml_next;
384         }
385
386         return LDAP_SUCCESS;
387 }
388
389 int slap_mods_opattrs(
390         Operation *op,
391         Modifications **modtail,
392         const char **text )
393 {
394         struct berval name, timestamp;
395         time_t now = slap_get_time();
396         char timebuf[22];
397         struct tm *ltm;
398         Modifications *mod;
399
400         int mop = op->o_tag == LDAP_REQ_ADD
401                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
402
403         assert( modtail != NULL );
404         assert( *modtail == NULL );
405
406         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
407         ltm = gmtime( &now );
408         strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
409         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
410         timestamp.bv_val = timebuf;
411         timestamp.bv_len = strlen(timebuf);
412
413         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
414                 name.bv_val = SLAPD_ANONYMOUS;
415                 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
416         } else {
417                 name.bv_val = op->o_dn;
418                 name.bv_len = strlen( op->o_dn );
419         }
420
421         if( op->o_tag == LDAP_REQ_ADD ) {
422                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
423                 mod->sml_op = mop;
424                 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
425                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
426                 mod->sml_bvalues[0] = ber_bvdup( &name );
427                 mod->sml_bvalues[1] = NULL;
428
429                 *modtail = mod;
430                 modtail = &mod->sml_next;
431
432                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
433                 mod->sml_op = mop;
434                 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
435                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
436                 mod->sml_bvalues[0] = ber_bvdup( &timestamp );
437                 mod->sml_bvalues[1] = NULL;
438                 *modtail = mod;
439                 modtail = &mod->sml_next;
440         }
441
442         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
443         mod->sml_op = mop;
444         mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
445         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
446         mod->sml_bvalues[0] = ber_bvdup( &name );
447         mod->sml_bvalues[1] = NULL;
448         *modtail = mod;
449         modtail = &mod->sml_next;
450
451         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
452         mod->sml_op = mop;
453         mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
454         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
455         mod->sml_bvalues[0] = ber_bvdup( &timestamp );
456         mod->sml_bvalues[1] = NULL;
457         *modtail = mod;
458         modtail = &mod->sml_next;
459
460         return LDAP_SUCCESS;
461 }
462
463
464 void
465 slap_mod_free(
466         Modification    *mod,
467         int                             freeit
468 )
469 {
470         ad_free( mod->sm_desc, 1 );
471
472         if ( mod->sm_bvalues != NULL )
473                 ber_bvecfree( mod->sm_bvalues );
474
475         if( freeit )
476                 free( mod );
477 }
478
479 void
480 slap_mods_free(
481     Modifications       *ml
482 )
483 {
484         Modifications *next;
485
486         for ( ; ml != NULL; ml = next ) {
487                 next = ml->sml_next;
488
489                 slap_mod_free( &ml->sml_mod, 0 );
490                 free( ml );
491         }
492 }
493
494 void
495 slap_modlist_free(
496     LDAPModList *ml
497 )
498 {
499         LDAPModList *next;
500
501         for ( ; ml != NULL; ml = next ) {
502                 next = ml->ml_next;
503
504                 if (ml->ml_type)
505                         free( ml->ml_type );
506
507                 if ( ml->ml_bvalues != NULL )
508                         ber_bvecfree( ml->ml_bvalues );
509
510                 free( ml );
511         }
512 }