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