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