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