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