]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Extend value_match to extract an asserted value from a full value
[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 #if defined( SLAPD_SCHEMA_DN )
205         } else if ( strcasecmp( ndn, SLAPD_SCHEMA_DN ) == 0 ) {
206 #ifdef NEW_LOGGING
207                 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
208                         "do_modify: attempt to modify subschema subentry.\n" ));
209 #else
210                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
211 #endif
212
213                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
214                         NULL, "modification of subschema subentry not supported",
215                         NULL, NULL );
216                 goto cleanup;
217 #endif
218         }
219
220 #ifdef LDAP_DEBUG
221 #ifdef NEW_LOGGING
222         LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
223                    "do_modify: modifications:\n" ));
224 #else
225         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
226 #endif
227
228         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
229 #ifdef NEW_LOGGING
230                 LDAP_LOG(( "operation", LDAP_LEVEL_DETAIL1,
231                            "\t%s:  %s\n", tmp->ml_op == LDAP_MOD_ADD ?
232                            "add" : (tmp->ml_op == LDAP_MOD_DELETE ?
233                                     "delete" : "replace"), tmp->ml_type ));
234 #else
235                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
236                         tmp->ml_op == LDAP_MOD_ADD
237                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
238                                         ? "delete" : "replace"), tmp->ml_type, 0 );
239 #endif
240
241         }
242 #endif
243
244         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
245             op->o_connid, op->o_opid, dn, 0, 0 );
246
247         manageDSAit = get_manageDSAit( op );
248
249         /*
250          * We could be serving multiple database backends.  Select the
251          * appropriate one, or send a referral to our "referral server"
252          * if we don't hold it.
253          */
254         if ( (be = select_backend( ndn, manageDSAit )) == NULL ) {
255                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
256                         NULL, NULL, default_referral, NULL );
257                 goto cleanup;
258         }
259
260         /* check restrictions */
261         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
262         if( rc != LDAP_SUCCESS ) {
263                 send_ldap_result( conn, op, rc,
264                         NULL, text, NULL, NULL );
265                 goto cleanup;
266         }
267
268         /* check for referrals */
269         rc = backend_check_referrals( be, conn, op, dn, ndn );
270         if ( rc != LDAP_SUCCESS ) {
271                 goto cleanup;
272         }
273
274         /* deref suffix alias if appropriate */
275         ndn = suffix_alias( be, ndn );
276
277         /*
278          * do the modify if 1 && (2 || 3)
279          * 1) there is a modify function implemented in this backend;
280          * 2) this backend is master for what it holds;
281          * 3) it's a replica and the dn supplied is the update_ndn.
282          */
283         if ( be->be_modify ) {
284                 /* do the update here */
285                 int repl_user = (be->be_update_ndn != NULL &&
286                         strcmp( be->be_update_ndn, op->o_ndn ) == 0);
287 #ifndef SLAPD_MULTIMASTER
288                 /* Multimaster slapd does not have to check for replicator dn
289                  * because it accepts each modify request
290                  */
291                 if ( be->be_update_ndn == NULL || repl_user )
292 #endif
293                 {
294                         int update = be->be_update_ndn != NULL;
295                         const char *text;
296                         char textbuf[SLAP_TEXT_BUFLEN];
297                         size_t textlen = sizeof textbuf;
298
299                         rc = slap_modlist2mods( modlist, update, &mods, &text,
300                                 textbuf, textlen );
301
302                         if( rc != LDAP_SUCCESS ) {
303                                 send_ldap_result( conn, op, rc,
304                                         NULL, text, NULL, NULL );
305                                 goto cleanup;
306                         }
307
308                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
309                                 global_lastmod == ON)) && !repl_user )
310                         {
311                                 Modifications **modstail;
312                                 for( modstail = &mods;
313                                         *modstail != NULL;
314                                         modstail = &(*modstail)->sml_next )
315                                 {
316                                         /* empty */
317                                 }
318                                 rc = slap_mods_opattrs( op, modstail, &text );
319
320                                 if( rc != LDAP_SUCCESS ) {
321                                         send_ldap_result( conn, op, rc,
322                                                 NULL, text,
323                                                 NULL, NULL );
324                                         goto cleanup;
325                                 }
326                         }
327
328                         if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0 
329 #ifdef SLAPD_MULTIMASTER
330                                 && !repl_user
331 #endif
332                         ) {
333                                 /* but we log only the ones not from a replicator user */
334                                 replog( be, op, dn, mods );
335                         }
336
337 #ifndef SLAPD_MULTIMASTER
338                 /* send a referral */
339                 } else {
340                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
341                                 be->be_update_refs ? be->be_update_refs : default_referral,
342                                 NULL );
343 #endif
344                 }
345         } else {
346                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
347                     NULL, "operation not supported within namingContext", NULL, NULL );
348         }
349
350 cleanup:
351         free( dn );
352         if( ndn != NULL ) free( ndn );
353         if ( modlist != NULL )
354                 slap_modlist_free( modlist );
355         if ( mods != NULL )
356                 slap_mods_free( mods );
357         return rc;
358 }
359
360 /*
361  * convert a raw list of modifications to internal format
362  * Do basic attribute type checking and syntax validation.
363  */
364 int slap_modlist2mods(
365         LDAPModList *ml,
366         int update,
367         Modifications **mods,
368         const char **text,
369         char *textbuf,
370         size_t textlen )
371 {
372         int rc;
373         Modifications **modtail = mods;
374
375         for( ; ml != NULL; ml = ml->ml_next ) {
376                 Modifications *mod;
377                 AttributeDescription *ad = NULL;
378
379                 mod = (Modifications *)
380                         ch_calloc( 1, sizeof(Modifications) );
381
382                 /* copy the op */
383                 mod->sml_op = ml->ml_op;
384
385                 /* convert to attribute description */
386                 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
387
388                 if( rc != LDAP_SUCCESS ) {
389                         slap_mods_free( mod );
390                         snprintf( textbuf, textlen, "%s: %s",
391                                 ml->ml_type, *text );
392                         *text = textbuf;
393                         return rc;
394                 }
395
396                 ad = mod->sml_desc;
397
398                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
399                         && !slap_ad_is_binary( ad ))
400                 {
401                         /* attribute requires binary transfer */
402                         slap_mods_free( mod );
403
404                         snprintf( textbuf, textlen,
405                                 "%s: requires ;binary transfer",
406                                 ml->ml_type );
407                         *text = textbuf;
408                         return LDAP_UNDEFINED_TYPE;
409                 }
410
411                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
412                         && slap_ad_is_binary( ad ))
413                 {
414                         /* attribute requires binary transfer */
415                         slap_mods_free( mod );
416                         snprintf( textbuf, textlen,
417                                 "%s: disallows ;binary transfer",
418                                 ml->ml_type );
419                         *text = textbuf;
420                         return LDAP_UNDEFINED_TYPE;
421                 }
422
423                 if (!update && is_at_no_user_mod( ad->ad_type )) {
424                         /* user modification disallowed */
425                         slap_mods_free( mod );
426                         snprintf( textbuf, textlen,
427                                 "%s: no user modification allowed",
428                                 ml->ml_type );
429                         *text = textbuf;
430                         return LDAP_CONSTRAINT_VIOLATION;
431                 }
432
433                 if ( is_at_obsolete( ad->ad_type ) &&
434                         ( mod->sml_op == LDAP_MOD_ADD || ml->ml_bvalues != NULL ) )
435                 {
436                         /*
437                          * attribute is obsolete,
438                          * only allow replace/delete with no values
439                          */
440                         slap_mods_free( mod );
441                         snprintf( textbuf, textlen,
442                                 "%s: attribute is obsolete",
443                                 ml->ml_type );
444                         *text = textbuf;
445                         return LDAP_CONSTRAINT_VIOLATION;
446                 }
447
448                 /*
449                  * check values
450                  */
451                 if( ml->ml_bvalues != NULL ) {
452                         ber_len_t nvals;
453                         slap_syntax_validate_func *validate =
454                                 ad->ad_type->sat_syntax->ssyn_validate;
455
456                         if( !validate ) {
457                                 slap_mods_free( mod );
458                                 *text = "no validator for syntax";
459                                 snprintf( textbuf, textlen,
460                                         "%s: no validator for syntax %s",
461                                         ml->ml_type,
462                                         ad->ad_type->sat_syntax->ssyn_oid );
463                                 *text = textbuf;
464                                 return LDAP_INVALID_SYNTAX;
465                         }
466
467                         /*
468                          * check that each value is valid per syntax
469                          */
470                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
471                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
472
473                                 if( rc != 0 ) {
474                                         slap_mods_free( mod );
475                                         snprintf( textbuf, textlen,
476                                                 "%s: value #%ld invalid per syntax",
477                                                 ml->ml_type, (long) nvals );
478                                         *text = textbuf;
479                                         return LDAP_INVALID_SYNTAX;
480                                 }
481                         }
482
483                         /*
484                          * a rough single value check... an additional check is needed
485                          * to catch add of single value to existing single valued attribute
486                          */
487                         if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
488                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
489                         {
490                                 slap_mods_free( mod );
491                                 snprintf( textbuf, textlen,
492                                         "%s: multiple value provided",
493                                         ml->ml_type );
494                                 *text = textbuf;
495                                 return LDAP_CONSTRAINT_VIOLATION;
496                         }
497                 }
498
499                 mod->sml_bvalues = ml->ml_bvalues;
500                 ml->ml_values = NULL;
501
502                 *modtail = mod;
503                 modtail = &mod->sml_next;
504         }
505
506         return LDAP_SUCCESS;
507 }
508
509 int slap_mods_opattrs(
510         Operation *op,
511         Modifications **modtail,
512         const char **text )
513 {
514         struct berval name, timestamp;
515         time_t now = slap_get_time();
516         char timebuf[22];
517         struct tm *ltm;
518         Modifications *mod;
519
520         int mop = op->o_tag == LDAP_REQ_ADD
521                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
522
523         assert( modtail != NULL );
524         assert( *modtail == NULL );
525
526         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
527         ltm = gmtime( &now );
528         strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
529         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
530         timestamp.bv_val = timebuf;
531         timestamp.bv_len = strlen(timebuf);
532
533         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
534                 name.bv_val = SLAPD_ANONYMOUS;
535                 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
536         } else {
537                 name.bv_val = op->o_dn;
538                 name.bv_len = strlen( op->o_dn );
539         }
540
541         if( op->o_tag == LDAP_REQ_ADD ) {
542                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
543                 mod->sml_op = mop;
544                 mod->sml_desc = slap_schema.si_ad_creatorsName;
545                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
546                 mod->sml_bvalues[0] = ber_bvdup( &name );
547                 mod->sml_bvalues[1] = NULL;
548
549                 *modtail = mod;
550                 modtail = &mod->sml_next;
551
552                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
553                 mod->sml_op = mop;
554                 mod->sml_desc = slap_schema.si_ad_createTimestamp;
555                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
556                 mod->sml_bvalues[0] = ber_bvdup( &timestamp );
557                 mod->sml_bvalues[1] = NULL;
558                 *modtail = mod;
559                 modtail = &mod->sml_next;
560         }
561
562         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
563         mod->sml_op = mop;
564         mod->sml_desc = slap_schema.si_ad_modifiersName;
565         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
566         mod->sml_bvalues[0] = ber_bvdup( &name );
567         mod->sml_bvalues[1] = NULL;
568         *modtail = mod;
569         modtail = &mod->sml_next;
570
571         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
572         mod->sml_op = mop;
573         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
574         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
575         mod->sml_bvalues[0] = ber_bvdup( &timestamp );
576         mod->sml_bvalues[1] = NULL;
577         *modtail = mod;
578         modtail = &mod->sml_next;
579
580         return LDAP_SUCCESS;
581 }
582