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