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