]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
SLAPD_SCHEMA_NOT_COMPAT: Mostly work modify
[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                 /* convert to attribute description */
314                 rc = slap_str2ad( ml->ml_type, &mod->sml_desc, text );
315
316                 if( rc != LDAP_SUCCESS ) {
317                         slap_mods_free( mod );
318                         return rc;
319                 }
320
321                 ad = mod->sml_desc;
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 requires ;binary transfer";
329                         return LDAP_UNDEFINED_TYPE;
330                 }
331
332                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
333                         && slap_ad_is_binary( ad ))
334                 {
335                         /* attribute requires binary transfer */
336                         slap_mods_free( mod );
337                         *text = "attribute disallows ;binary transfer";
338                         return LDAP_UNDEFINED_TYPE;
339                 }
340
341                 if (!update && is_at_no_user_mod( ad->ad_type )) {
342                         /* user modification disallowed */
343                         slap_mods_free( mod );
344                         *text = "no user modification allowed";
345                         return LDAP_CONSTRAINT_VIOLATION;
346                 }
347
348                 /*
349                  * check values
350                  */
351                 if( ml->ml_bvalues != NULL ) {
352                         ber_len_t nvals;
353                         slap_syntax_validate_func *validate =
354                                 ad->ad_type->sat_syntax->ssyn_validate;
355
356                         if( !validate ) {
357                                 Debug( LDAP_DEBUG_TRACE,
358                                         "modlist2mods: no validator for syntax %s\n",
359                                         ad->ad_type->sat_syntax->ssyn_oid, 0, 0 );
360                                 slap_mods_free( mod );
361                                 *text = "no validator for syntax";
362                                 return LDAP_INVALID_SYNTAX;
363                         }
364
365                         /*
366                          * check that each value is valid per syntax
367                          */
368                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
369                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
370
371                                 if( rc != 0 ) {
372                                         slap_mods_free( mod );
373                                         *text = "value contains invalid data";
374                                         return LDAP_INVALID_SYNTAX;
375                                 }
376                         }
377
378                         /*
379                          * a rough single value check... an additional check is needed
380                          * to catch add of single value to existing single valued attribute
381                          */
382                         if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
383                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
384                         {
385                                 slap_mods_free( mod );
386                                 *text = "multiple values provided";
387                                 return LDAP_INVALID_SYNTAX;
388                         }
389                 }
390
391                 mod->sml_bvalues = ml->ml_bvalues;
392                 ml->ml_values = NULL;
393
394                 *modtail = mod;
395                 modtail = &mod->sml_next;
396         }
397
398         return LDAP_SUCCESS;
399 }
400
401 int slap_mods_opattrs(
402         Operation *op,
403         Modifications **modtail,
404         const char **text )
405 {
406         struct berval name, timestamp;
407         time_t now = slap_get_time();
408         char timebuf[22];
409         struct tm *ltm;
410         Modifications *mod;
411
412         int mop = op->o_tag == LDAP_REQ_ADD
413                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
414
415         assert( modtail != NULL );
416         assert( *modtail == NULL );
417
418         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
419         ltm = gmtime( &now );
420         strftime( timebuf, sizeof(timebuf), "%Y%m%d%H%M%SZ", ltm );
421         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
422         timestamp.bv_val = timebuf;
423         timestamp.bv_len = strlen(timebuf);
424
425         if( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
426                 name.bv_val = SLAPD_ANONYMOUS;
427                 name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
428         } else {
429                 name.bv_val = op->o_dn;
430                 name.bv_len = strlen( op->o_dn );
431         }
432
433         if( op->o_tag == LDAP_REQ_ADD ) {
434                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
435                 mod->sml_op = mop;
436                 mod->sml_desc = ad_dup( slap_schema.si_ad_creatorsName );
437                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
438                 mod->sml_bvalues[0] = ber_bvdup( &name );
439                 mod->sml_bvalues[1] = NULL;
440
441                 *modtail = mod;
442                 modtail = &mod->sml_next;
443
444                 mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
445                 mod->sml_op = mop;
446                 mod->sml_desc = ad_dup( slap_schema.si_ad_createTimestamp );
447                 mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
448                 mod->sml_bvalues[0] = ber_bvdup( &timestamp );
449                 mod->sml_bvalues[1] = NULL;
450                 *modtail = mod;
451                 modtail = &mod->sml_next;
452         }
453
454         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
455         mod->sml_op = mop;
456         mod->sml_desc = ad_dup( slap_schema.si_ad_modifiersName );
457         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
458         mod->sml_bvalues[0] = ber_bvdup( &name );
459         mod->sml_bvalues[1] = NULL;
460         *modtail = mod;
461         modtail = &mod->sml_next;
462
463         mod = (Modifications *) ch_calloc( 1, sizeof( Modifications ) );
464         mod->sml_op = mop;
465         mod->sml_desc = ad_dup( slap_schema.si_ad_modifyTimestamp );
466         mod->sml_bvalues = (struct berval **) malloc( 2 * sizeof( struct berval * ) );
467         mod->sml_bvalues[0] = ber_bvdup( &timestamp );
468         mod->sml_bvalues[1] = NULL;
469         *modtail = mod;
470         modtail = &mod->sml_next;
471
472         return LDAP_SUCCESS;
473 }
474
475 #else
476 static int
477 add_modified_attrs( Operation *op, Modifications **modlist )
478 {
479         char            buf[22];
480         struct berval   bv;
481         struct berval   *bvals[2];
482         Modifications           *m;
483         struct tm       *ltm;
484         time_t          currenttime;
485
486         bvals[0] = &bv;
487         bvals[1] = NULL;
488
489         /* remove any attempts by the user to modify these attrs */
490         for ( m = *modlist; m != NULL; m = m->ml_next ) {
491                 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
492                         return LDAP_CONSTRAINT_VIOLATION;
493                 }
494         }
495
496         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
497                 bv.bv_val = SLAPD_ANONYMOUS;
498                 bv.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
499         } else {
500                 bv.bv_val = op->o_dn;
501                 bv.bv_len = strlen( bv.bv_val );
502         }
503         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
504         m->ml_type = ch_strdup( "modifiersname" );
505         m->ml_op = LDAP_MOD_REPLACE;
506         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
507         m->ml_bvalues[0] = ber_bvdup( &bv );
508         m->ml_next = *modlist;
509         *modlist = m;
510
511         currenttime = slap_get_time();
512         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
513         ltm = gmtime( &currenttime );
514         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
515         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
516
517         bv.bv_val = buf;
518         bv.bv_len = strlen( bv.bv_val );
519         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
520         m->ml_type = ch_strdup( "modifytimestamp" );
521         m->ml_op = LDAP_MOD_REPLACE;
522         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
523         m->ml_bvalues[0] = ber_bvdup( &bv );
524         m->ml_next = *modlist;
525         *modlist = m;
526
527         return LDAP_SUCCESS;
528 }
529 #endif
530
531 void
532 slap_mod_free(
533         Modification    *mod,
534         int                             freeit
535 )
536 {
537 #ifdef SLAPD_SCHEMA_NOT_COMPAT
538         ad_free( mod->sm_desc, 1 );
539 #else
540         if (mod->sm_desc) {
541                 free( mod->sm_desc );
542         }
543 #endif
544
545         if ( mod->sm_bvalues != NULL )
546                 ber_bvecfree( mod->sm_bvalues );
547
548         if( freeit )
549                 free( mod );
550 }
551
552 void
553 slap_mods_free(
554     Modifications       *ml
555 )
556 {
557         Modifications *next;
558
559         for ( ; ml != NULL; ml = next ) {
560                 next = ml->sml_next;
561
562                 slap_mod_free( &ml->sml_mod, 0 );
563                 free( ml );
564         }
565 }
566
567 void
568 slap_modlist_free(
569     LDAPModList *ml
570 )
571 {
572         LDAPModList *next;
573
574         for ( ; ml != NULL; ml = next ) {
575                 next = ml->ml_next;
576
577                 if (ml->ml_type)
578                         free( ml->ml_type );
579
580                 if ( ml->ml_bvalues != NULL )
581                         ber_bvecfree( ml->ml_bvalues );
582
583                 free( ml );
584         }
585 }