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