]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
fix for select_backend suggested G. Gombas (ITS 1090)
[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 #ifdef NEW_LOGGING
370                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
371                                    "modlist2mods: Attribute %s is undefined (%s)\n", ml->ml_type, text ));
372 #endif
373                         slap_mods_free( mod );
374                         return rc;
375                 }
376
377                 ad = mod->sml_desc;
378
379                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
380                         && !slap_ad_is_binary( ad ))
381                 {
382                         /* attribute requires binary transfer */
383                         slap_mods_free( mod );
384                         *text = "attribute requires ;binary transfer";
385 #ifdef NEW_LOGGING
386                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
387                                    "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
388 #endif
389                         return LDAP_UNDEFINED_TYPE;
390                 }
391
392                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
393                         && slap_ad_is_binary( ad ))
394                 {
395                         /* attribute requires binary transfer */
396                         slap_mods_free( mod );
397                         *text = "attribute disallows ;binary transfer";
398 #ifdef NEW_LOGGING
399                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
400                                    "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
401 #endif
402                         return LDAP_UNDEFINED_TYPE;
403                 }
404
405                 if (!update && is_at_no_user_mod( ad->ad_type )) {
406                         /* user modification disallowed */
407                         slap_mods_free( mod );
408                         *text = "no user modification allowed";
409 #ifdef NEW_LOGGING
410                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
411                                    "modlist2mods: Attribute %s %s\n", ml->ml_type, text ));
412 #endif
413                         return LDAP_CONSTRAINT_VIOLATION;
414                 }
415
416                 /*
417                  * check values
418                  */
419                 if( ml->ml_bvalues != NULL ) {
420                         ber_len_t nvals;
421                         slap_syntax_validate_func *validate =
422                                 ad->ad_type->sat_syntax->ssyn_validate;
423
424                         if( !validate ) {
425 #ifdef NEW_LOGGING
426                                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
427                                            "modlist2mods: Attribute %s, no validator for syntax %s\n",
428                                            ml->ml_type, ad->ad_type->sat_syntax->ssyn_oid ));
429 #else
430                                 Debug( LDAP_DEBUG_TRACE,
431                                         "modlist2mods: no validator for syntax %s\n",
432                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
433 #endif
434
435                                 slap_mods_free( mod );
436                                 *text = "no validator for syntax";
437                                 return LDAP_INVALID_SYNTAX;
438                         }
439
440                         /*
441                          * check that each value is valid per syntax
442                          */
443                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
444                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
445
446                                 if( rc != 0 ) {
447                                         slap_mods_free( mod );
448                                         *text = "value contains invalid data";
449 #ifdef NEW_LOGGING
450                                         LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
451                                                    "modlist2mods: value for attribute %s contains invalid data\n",
452                                                    ml->ml_type ));
453 #endif
454                                         return LDAP_INVALID_SYNTAX;
455                                 }
456                         }
457
458                         /*
459                          * a rough single value check... an additional check is needed
460                          * to catch add of single value to existing single valued attribute
461                          */
462                         if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
463                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
464                         {
465                                 slap_mods_free( mod );
466                                 *text = "multiple values provided";
467                                 return LDAP_INVALID_SYNTAX;
468                         }
469                 }
470
471                 mod->sml_bvalues = ml->ml_bvalues;
472                 ml->ml_values = NULL;
473
474                 *modtail = mod;
475                 modtail = &mod->sml_next;
476         }
477
478         return LDAP_SUCCESS;
479 }
480
481 int slap_mods_opattrs(
482         Operation *op,
483         Modifications **modtail,
484         const char **text )
485 {
486         struct berval name, timestamp;
487         time_t now = slap_get_time();
488         char timebuf[22];
489         struct tm *ltm;
490         Modifications *mod;
491
492         int mop = op->o_tag == LDAP_REQ_ADD
493                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
494
495         assert( modtail != NULL );
496         assert( *modtail == NULL );
497
498         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
499         ltm = gmtime( &now );
500         strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
501         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
502         timestamp.bv_val = timebuf;
503         timestamp.bv_len = strlen(timebuf);
504
505         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
506                 name.bv_val = SLAPD_ANONYMOUS;
507                 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
508         } else {
509                 name.bv_val = op->o_dn;
510                 name.bv_len = strlen( op->o_dn );
511         }
512
513         if( op->o_tag == LDAP_REQ_ADD ) {
514                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
515                 mod->sml_op = mop;
516                 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
517                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
518                 mod->sml_bvalues[0] = ber_bvdup( &name );
519                 mod->sml_bvalues[1] = NULL;
520
521                 *modtail = mod;
522                 modtail = &mod->sml_next;
523
524                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
525                 mod->sml_op = mop;
526                 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
527                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
528                 mod->sml_bvalues[0] = ber_bvdup( &timestamp );
529                 mod->sml_bvalues[1] = NULL;
530                 *modtail = mod;
531                 modtail = &mod->sml_next;
532         }
533
534         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
535         mod->sml_op = mop;
536         mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
537         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
538         mod->sml_bvalues[0] = ber_bvdup( &name );
539         mod->sml_bvalues[1] = NULL;
540         *modtail = mod;
541         modtail = &mod->sml_next;
542
543         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
544         mod->sml_op = mop;
545         mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
546         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
547         mod->sml_bvalues[0] = ber_bvdup( &timestamp );
548         mod->sml_bvalues[1] = NULL;
549         *modtail = mod;
550         modtail = &mod->sml_next;
551
552         return LDAP_SUCCESS;
553 }
554
555
556 void
557 slap_mod_free(
558         Modification    *mod,
559         int                             freeit
560 )
561 {
562         ad_free( mod->sm_desc, 1 );
563
564         if ( mod->sm_bvalues != NULL )
565                 ber_bvecfree( mod->sm_bvalues );
566
567         if( freeit )
568                 free( mod );
569 }
570
571 void
572 slap_mods_free(
573     Modifications       *ml
574 )
575 {
576         Modifications *next;
577
578         for ( ; ml != NULL; ml = next ) {
579                 next = ml->sml_next;
580
581                 slap_mod_free( &ml->sml_mod, 0 );
582                 free( ml );
583         }
584 }
585
586 void
587 slap_modlist_free(
588     LDAPModList *ml
589 )
590 {
591         LDAPModList *next;
592
593         for ( ; ml != NULL; ml = next ) {
594                 next = ml->ml_next;
595
596                 if (ml->ml_type)
597                         free( ml->ml_type );
598
599                 if ( ml->ml_bvalues != NULL )
600                         ber_bvecfree( ml->ml_bvalues );
601
602                 free( ml );
603         }
604 }