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