]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Prepare for unifdef -DSLAPD_SCHEMA_NOT_COMPAT
[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                                 Modifications **modstail;
239                                 for( modstail = &mods;
240                                         *modstail != NULL;
241                                         modstail = &(*modstail)->sml_next )
242                                 {
243                                         /* empty */
244                                 }
245                                 rc = slap_mods_opattrs( op, modstail, &text );
246 #else
247                                 char *text = "no-user-modification attribute type";
248                                 rc = add_modified_attrs( op, &mods );
249 #endif
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                                 && ( be->be_update_ndn == NULL ||
262                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
263 #endif
264                         ) {
265                                 /* but we log only the ones not from a replicator user */
266                                 replog( be, op, dn, mods );
267                         }
268
269 #ifndef SLAPD_MULTIMASTER
270                 /* send a referral */
271                 } else {
272                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
273                                 be->be_update_refs ? be->be_update_refs : default_referral,
274                                 NULL );
275 #endif
276                 }
277         } else {
278                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
279                     NULL, "operation not supported within namingContext", NULL, NULL );
280         }
281
282 cleanup:
283         free( dn );
284         if( ndn != NULL ) free( ndn );
285         if ( modlist != NULL )
286                 slap_modlist_free( modlist );
287         if ( mods != NULL )
288                 slap_mods_free( mods );
289         return rc;
290 }
291
292 #ifdef SLAPD_SCHEMA_NOT_COMPAT
293 /*
294  * convert a raw list of modifications to internal format
295  * Do basic attribute type checking and syntax validation.
296  */
297 int slap_modlist2mods(
298         LDAPModList *ml,
299         int update,
300         Modifications **mods,
301         const char **text )
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                         return rc;
322                 }
323
324                 ad = mod->sml_desc;
325
326                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
327                         && !slap_ad_is_binary( ad ))
328                 {
329                         /* attribute requires binary transfer */
330                         slap_mods_free( mod );
331                         *text = "attribute requires ;binary transfer";
332                         return LDAP_UNDEFINED_TYPE;
333                 }
334
335                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
336                         && slap_ad_is_binary( ad ))
337                 {
338                         /* attribute requires binary transfer */
339                         slap_mods_free( mod );
340                         *text = "attribute disallows ;binary transfer";
341                         return LDAP_UNDEFINED_TYPE;
342                 }
343
344                 if (!update && is_at_no_user_mod( ad->ad_type )) {
345                         /* user modification disallowed */
346                         slap_mods_free( mod );
347                         *text = "no user modification allowed";
348                         return LDAP_CONSTRAINT_VIOLATION;
349                 }
350
351                 /*
352                  * check values
353                  */
354                 if( ml->ml_bvalues != NULL ) {
355                         ber_len_t nvals;
356                         slap_syntax_validate_func *validate =
357                                 ad->ad_type->sat_syntax->ssyn_validate;
358
359                         if( !validate ) {
360                                 Debug( LDAP_DEBUG_TRACE,
361                                         "modlist2mods: no validator for syntax %s\n",
362                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
363                                 slap_mods_free( mod );
364                                 *text = "no validator for syntax";
365                                 return LDAP_INVALID_SYNTAX;
366                         }
367
368                         /*
369                          * check that each value is valid per syntax
370                          */
371                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
372                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
373
374                                 if( rc != 0 ) {
375                                         slap_mods_free( mod );
376                                         *text = "value contains invalid data";
377                                         return LDAP_INVALID_SYNTAX;
378                                 }
379                         }
380
381                         /*
382                          * a rough single value check... an additional check is needed
383                          * to catch add of single value to existing single valued attribute
384                          */
385                         if( ( mod->sml_op == LDAP_MOD_ADD || mod->sml_op == LDAP_MOD_REPLACE )
386                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
387                         {
388                                 slap_mods_free( mod );
389                                 *text = "multiple values provided";
390                                 return LDAP_INVALID_SYNTAX;
391                         }
392                 }
393
394                 mod->sml_bvalues = ml->ml_bvalues;
395                 ml->ml_values = NULL;
396
397                 *modtail = mod;
398                 modtail = &mod->sml_next;
399         }
400
401         return LDAP_SUCCESS;
402 }
403
404 int slap_mods_opattrs(
405         Operation *op,
406         Modifications **modtail,
407         const char **text )
408 {
409         struct berval name, timestamp;
410         time_t now = slap_get_time();
411         char timebuf[22];
412         struct tm *ltm;
413         Modifications *mod;
414
415         int mop = op->o_tag == LDAP_REQ_ADD
416                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
417
418         assert( modtail != NULL );
419         assert( *modtail == NULL );
420
421         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
422         ltm = gmtime( &now );
423         strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
424         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
425         timestamp.bv_val = timebuf;
426         timestamp.bv_len = strlen(timebuf);
427
428         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
429                 name.bv_val = SLAPD_ANONYMOUS;
430                 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
431         } else {
432                 name.bv_val = op->o_dn;
433                 name.bv_len = strlen( op->o_dn );
434         }
435
436         if( op->o_tag == LDAP_REQ_ADD ) {
437                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
438                 mod->sml_op = mop;
439                 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
440                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
441                 mod->sml_bvalues[0] = ber_bvdup( &name );
442                 mod->sml_bvalues[1] = NULL;
443
444                 *modtail = mod;
445                 modtail = &mod->sml_next;
446
447                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
448                 mod->sml_op = mop;
449                 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
450                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
451                 mod->sml_bvalues[0] = ber_bvdup( &timestamp );
452                 mod->sml_bvalues[1] = NULL;
453                 *modtail = mod;
454                 modtail = &mod->sml_next;
455         }
456
457         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
458         mod->sml_op = mop;
459         mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
460         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
461         mod->sml_bvalues[0] = ber_bvdup( &name );
462         mod->sml_bvalues[1] = NULL;
463         *modtail = mod;
464         modtail = &mod->sml_next;
465
466         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
467         mod->sml_op = mop;
468         mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
469         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
470         mod->sml_bvalues[0] = ber_bvdup( &timestamp );
471         mod->sml_bvalues[1] = NULL;
472         *modtail = mod;
473         modtail = &mod->sml_next;
474
475         return LDAP_SUCCESS;
476 }
477
478 #else
479 static int
480 add_modified_attrs( Operation *op, Modifications **modlist )
481 {
482         char            buf[22];
483         struct berval   bv;
484         struct berval   *bvals[2];
485         Modifications           *m;
486         struct tm       *ltm;
487         time_t          currenttime;
488
489         bvals[0] = &bv;
490         bvals[1] = NULL;
491
492         /* remove any attempts by the user to modify these attrs */
493         for ( m = *modlist; m != NULL; m = m->ml_next ) {
494                 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
495                         return LDAP_CONSTRAINT_VIOLATION;
496                 }
497         }
498
499         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
500                 bv.bv_val = SLAPD_ANONYMOUS;
501                 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
502         } else {
503                 bv.bv_val = op->o_dn;
504                 bv.bv_len = strlen( bv.bv_val );
505         }
506         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
507         m->ml_type = ch_strdup( "modifiersname" );
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         currenttime = slap_get_time();
515         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
516         ltm = gmtime( &currenttime );
517         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
518         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
519
520         bv.bv_val = buf;
521         bv.bv_len = strlen( bv.bv_val );
522         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
523         m->ml_type = ch_strdup( "modifytimestamp" );
524         m->ml_op = LDAP_MOD_REPLACE;
525         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
526         m->ml_bvalues[0] = ber_bvdup( &bv );
527         m->ml_next = *modlist;
528         *modlist = m;
529
530         return LDAP_SUCCESS;
531 }
532 #endif
533
534 void
535 slap_mod_free(
536         Modification    *mod,
537         int                             freeit
538 )
539 {
540 #ifdef SLAPD_SCHEMA_NOT_COMPAT
541         ad_free( mod->sm_desc, 1 );
542 #else
543         if (mod->sm_desc) {
544                 free( mod->sm_desc );
545         }
546 #endif
547
548         if ( mod->sm_bvalues != NULL )
549                 ber_bvecfree( mod->sm_bvalues );
550
551         if( freeit )
552                 free( mod );
553 }
554
555 void
556 slap_mods_free(
557     Modifications       *ml
558 )
559 {
560         Modifications *next;
561
562         for ( ; ml != NULL; ml = next ) {
563                 next = ml->sml_next;
564
565                 slap_mod_free( &ml->sml_mod, 0 );
566                 free( ml );
567         }
568 }
569
570 void
571 slap_modlist_free(
572     LDAPModList *ml
573 )
574 {
575         LDAPModList *next;
576
577         for ( ; ml != NULL; ml = next ) {
578                 next = ml->ml_next;
579
580                 if (ml->ml_type)
581                         free( ml->ml_type );
582
583                 if ( ml->ml_bvalues != NULL )
584                         ber_bvecfree( ml->ml_bvalues );
585
586                 free( ml );
587         }
588 }