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