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