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