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