]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
6a091444a583abc2d334d97655e27aa620008128
[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                 (*modtail)->ml_op = mop;
121                 
122                 if ( (*modtail)->ml_op != LDAP_MOD_ADD &&
123                     (*modtail)->ml_op != LDAP_MOD_DELETE &&
124                     (*modtail)->ml_op != LDAP_MOD_REPLACE )
125                 {
126                         Debug( LDAP_DEBUG_ANY,
127                                 "do_modify: invalid modify operation (%ld)\n",
128                                 (long) (*modtail)->ml_op, 0, 0 );
129                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
130                             NULL, "unrecognized modify operation", NULL, NULL );
131                         rc = LDAP_PROTOCOL_ERROR;
132                         goto cleanup;
133                 }
134
135                 if ( (*modtail)->ml_bvalues == NULL && (
136                         (*modtail)->ml_op != LDAP_MOD_REPLACE &&
137                         (*modtail)->ml_op != LDAP_MOD_DELETE ) )
138                 {
139                         Debug( LDAP_DEBUG_ANY,
140                                 "do_modify: invalid modify operation (%ld) without values\n",
141                                 (long) (*modtail)->ml_op, 0, 0 );
142                         send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
143                             NULL, "unrecognized modify operation without values",
144                                 NULL, NULL );
145                         rc = LDAP_PROTOCOL_ERROR;
146                         goto cleanup;
147                 }
148
149 #ifndef SLAPD_SCHEMA_NOT_COMPAT
150                 attr_normalize( (*modtail)->ml_type );
151 #endif
152
153                 modtail = &(*modtail)->ml_next;
154         }
155         *modtail = NULL;
156
157         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
158                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
159                 goto cleanup;
160         }
161
162 #ifdef LDAP_DEBUG
163         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
164         for ( tmp = modlist; tmp != NULL; tmp = tmp->ml_next ) {
165                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
166                         tmp->ml_op == LDAP_MOD_ADD
167                                 ? "add" : (tmp->ml_op == LDAP_MOD_DELETE
168                                         ? "delete" : "replace"), tmp->ml_type, 0 );
169         }
170 #endif
171
172
173         Statslog( LDAP_DEBUG_STATS, "conn=%ld op=%d MOD dn=\"%s\"\n",
174             op->o_connid, op->o_opid, dn, 0, 0 );
175
176         /*
177          * We could be serving multiple database backends.  Select the
178          * appropriate one, or send a referral to our "referral server"
179          * if we don't hold it.
180          */
181         if ( (be = select_backend( ndn )) == NULL ) {
182                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
183                         NULL, NULL, default_referral, NULL );
184                 goto cleanup;
185         }
186
187         /* make sure this backend recongizes critical controls */
188         rc = backend_check_controls( be, conn, op ) ;
189
190         if( rc != LDAP_SUCCESS ) {
191                 send_ldap_result( conn, op, rc,
192                         NULL, NULL, NULL, NULL );
193                 goto cleanup;
194         }
195
196         if ( global_readonly || be->be_readonly ) {
197                 Debug( LDAP_DEBUG_ANY, "do_modify: database is read-only\n",
198                        0, 0, 0 );
199                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
200                                   NULL, "database is read-only", NULL, NULL );
201                 goto cleanup;
202         }
203
204         /* deref suffix alias if appropriate */
205         ndn = suffix_alias( be, ndn );
206
207         /*
208          * do the modify if 1 && (2 || 3)
209          * 1) there is a modify function implemented in this backend;
210          * 2) this backend is master for what it holds;
211          * 3) it's a replica and the dn supplied is the update_ndn.
212          */
213         if ( be->be_modify ) {
214                 /* do the update here */
215 #ifndef SLAPD_MULTIMASTER
216                 /* we don't have to check for replicator dn
217                  * because we accept each modify request
218                  */
219                 if ( be->be_update_ndn == NULL ||
220                         strcmp( be->be_update_ndn, op->o_ndn ) == 0 )
221 #endif
222                 {
223                         int update = be->be_update_ndn != NULL;
224 #ifdef SLAPD_SCHEMA_NOT_COMPAT
225                         char *text;
226                         rc = slap_modlist2mods( modlist, update, &mods, &text );
227
228                         if( rc != LDAP_SUCCESS ) {
229                                 send_ldap_result( conn, op, rc,
230                                         NULL, text, NULL, NULL );
231                                 goto cleanup;
232                         }
233 #else
234                         mods = modlist;
235                         modlist = NULL;
236 #endif
237
238                         if ( (be->be_lastmod == ON || (be->be_lastmod == UNDEFINED &&
239                                 global_lastmod == ON)) && !update )
240                         {
241 #ifdef SLAPD_SCHEMA_NOT_COMPAT
242                                 rc = slap_mods_opattrs( op, &mods, &text );
243 #else
244                                 char *text = "no-user-modification attribute type";
245                                 rc = add_modified_attrs( op, &mods );
246 #endif
247
248                                 if( rc != LDAP_SUCCESS ) {
249                                         send_ldap_result( conn, op, rc,
250                                                 NULL, text,
251                                                 NULL, NULL );
252                                         goto cleanup;
253                                 }
254                         }
255
256                         if ( (*be->be_modify)( be, conn, op, dn, ndn, mods ) == 0 
257 #ifdef SLAPD_MULTIMASTER
258                                 && ( be->be_update_ndn == NULL ||
259                                         strcmp( be->be_update_ndn, op->o_ndn ) != 0 )
260 #endif
261                         ) {
262                                 /* but we log only the ones not from a replicator user */
263                                 replog( be, op, dn, mods );
264                         }
265
266 #ifndef SLAPD_MULTIMASTER
267                 /* send a referral */
268                 } else {
269                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
270                                 be->be_update_refs ? be->be_update_refs : default_referral,
271                                 NULL );
272 #endif
273                 }
274         } else {
275                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
276                     NULL, "Function not implemented", NULL, NULL );
277         }
278
279 cleanup:
280         free( dn );
281         free( ndn );
282         if ( modlist != NULL )
283                 slap_modlist_free( modlist );
284         if ( mods != NULL )
285                 slap_mods_free( mods );
286         return rc;
287 }
288
289 #ifdef SLAPD_SCHEMA_NOT_COMPAT
290 /*
291  * convert a raw list of modifications to internal format
292  * Do basic attribute type checking and syntax validation.
293  */
294 int slap_modlist2mods(
295         LDAPModList *ml,
296         int update,
297         Modifications **mods,
298         char **text )
299 {
300         int rc;
301         Modifications **modtail = mods;
302
303         for( ; ml != NULL; ml = ml->ml_next ) {
304                 Modifications *mod;
305                 AttributeDescription *ad;
306
307                 mod = (Modifications *)
308                         ch_calloc( 1, sizeof(Modifications) );
309
310                 ad = &mod->sml_desc;
311
312                 /* convert to attribute description */
313                 rc = slap_str2ad( ml->ml_type, &ad, text );
314
315                 if( rc != LDAP_SUCCESS ) {
316                         slap_mods_free( mod );
317                         return rc;
318                 }
319
320                 if((ad->ad_type->sat_syntax->ssyn_flags & SLAP_SYNTAX_BINARY)
321                         && !( ad->ad_flags & SLAP_DESC_BINARY ))
322                 {
323                         /* attribute requires binary transfer */
324                         slap_mods_free( mod );
325                         *text = "attribute requires ;binary transfer";
326                         return LDAP_UNDEFINED_TYPE;
327                 }
328
329                 if (!update && is_at_no_user_mod( ad->ad_type )) {
330                         /* user modification disallowed */
331                         slap_mods_free( mod );
332                         *text = "no user modification allowed";
333                         return LDAP_CONSTRAINT_VIOLATION;
334                 }
335
336                 /*
337                  * check values
338                  */
339                 if( ml->ml_bvalues != NULL ) {
340                         ber_len_t nvals;
341                         slap_syntax_validate_func *validate =
342                                 ad->ad_type->sat_syntax->ssyn_validate;
343
344                         /*
345                          * check that each value is valid per syntax
346                          */
347                         for( nvals = 0; ml->ml_bvalues[nvals]; nvals++ ) {
348                                 rc = validate( ad->ad_type->sat_syntax, ml->ml_bvalues[nvals] );
349
350                                 if( rc != 0 ) {
351                                         slap_mods_free( mod );
352                                         *text = "value contains invalid data";
353                                         return LDAP_INVALID_SYNTAX;
354                                 }
355                         }
356
357                         /*
358                          * a rough single value check... an additional check is needed
359                          * to catch add of single value to existing single valued attribute
360                          */
361                         if( ( ml->ml_op == LDAP_MOD_ADD || ml->ml_op == LDAP_MOD_REPLACE )
362                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
363                         {
364                                 slap_mods_free( mod );
365                                 *text = "multiple values provided";
366                                 return LDAP_INVALID_SYNTAX;
367                         }
368                 }
369
370                 mod->sml_bvalues = ml->ml_bvalues;
371                 ml->ml_values = NULL;
372
373                 *modtail = mod;
374                 modtail = &mod->sml_next;
375         }
376
377         return LDAP_SUCCESS;
378 }
379
380 int slap_mods_opattrs(
381         Operation *op,
382         Modifications **modlist,
383         char **text )
384 {
385         /* not yet implemented */
386         return LDAP_SUCCESS;
387 }
388
389 #else
390 static int
391 add_modified_attrs( Operation *op, Modifications **modlist )
392 {
393         char            buf[22];
394         struct berval   bv;
395         struct berval   *bvals[2];
396         Modifications           *m;
397         struct tm       *ltm;
398         time_t          currenttime;
399
400         bvals[0] = &bv;
401         bvals[1] = NULL;
402
403         /* remove any attempts by the user to modify these attrs */
404         for ( m = *modlist; m != NULL; m = m->ml_next ) {
405                 if ( oc_check_op_no_usermod_attr( m->ml_type ) ) {
406                         return LDAP_CONSTRAINT_VIOLATION;
407                 }
408         }
409
410         if ( op->o_dn == NULL || op->o_dn[0] == '\0' ) {
411                 bv.bv_val = "<anonymous>";
412                 bv.bv_len = sizeof("<anonymous>")-1;
413         } else {
414                 bv.bv_val = op->o_dn;
415                 bv.bv_len = strlen( bv.bv_val );
416         }
417         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
418         m->ml_type = ch_strdup( "modifiersname" );
419         m->ml_op = LDAP_MOD_REPLACE;
420         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
421         m->ml_bvalues[0] = ber_bvdup( &bv );
422         m->ml_next = *modlist;
423         *modlist = m;
424
425         currenttime = slap_get_time();
426         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
427         ltm = gmtime( &currenttime );
428         strftime( buf, sizeof(buf), "%Y%m%d%H%M%SZ", ltm );
429         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
430
431         bv.bv_val = buf;
432         bv.bv_len = strlen( bv.bv_val );
433         m = (Modifications *) ch_calloc( 1, sizeof(Modifications) );
434         m->ml_type = ch_strdup( "modifytimestamp" );
435         m->ml_op = LDAP_MOD_REPLACE;
436         m->ml_bvalues = (struct berval **) ch_calloc(2, sizeof(struct berval *));
437         m->ml_bvalues[0] = ber_bvdup( &bv );
438         m->ml_next = *modlist;
439         *modlist = m;
440
441         return LDAP_SUCCESS;
442 }
443 #endif
444
445 void
446 slap_mod_free(
447         Modification    *mod,
448         int                             freeit
449 )
450 {
451 #ifdef SLAPD_SCHEMA_NOT_COMPAT
452         ad_free( &mod->sm_desc, 0 );
453 #else
454         if (mod->sm_desc) {
455                 free( mod->sm_desc );
456         }
457 #endif
458
459         if ( mod->sm_bvalues != NULL )
460                 ber_bvecfree( mod->sm_bvalues );
461
462         if( freeit )
463                 free( mod );
464 }
465
466 void
467 slap_mods_free(
468     Modifications       *ml
469 )
470 {
471         Modifications *next;
472
473         for ( ; ml != NULL; ml = next ) {
474                 next = ml->sml_next;
475
476                 slap_mod_free( &ml->sml_mod, 0 );
477                 free( ml );
478         }
479 }
480
481 void
482 slap_modlist_free(
483     LDAPModList *ml
484 )
485 {
486         LDAPModList *next;
487
488         for ( ; ml != NULL; ml = next ) {
489                 next = ml->ml_next;
490
491                 if (ml->ml_type)
492                         free( ml->ml_type );
493
494                 if ( ml->ml_bvalues != NULL )
495                         ber_bvecfree( ml->ml_bvalues );
496
497                 free( ml );
498         }
499 }