]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
SLAP_NVALUES: a couple of basic normalizers... but not working yet.
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright 1998-2003 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 "lutil.h"
27
28 #include "ldap_pvt.h"
29 #include "slap.h"
30 #ifdef LDAP_SLAPI
31 #include "slapi.h"
32 #endif
33
34 int
35 do_modify(
36     Connection  *conn,
37     Operation   *op )
38 {
39         struct berval dn = { 0, NULL };
40         struct berval pdn = { 0, NULL };
41         struct berval ndn = { 0, NULL };
42         char            *last;
43         ber_tag_t       tag;
44         ber_len_t       len;
45         Modifications   *modlist = NULL;
46         Modifications   **modtail = &modlist;
47 #ifdef LDAP_DEBUG
48         Modifications *tmp;
49 #endif
50 #ifdef LDAP_SLAPI
51         LDAPMod         **modv = NULL;
52         Slapi_PBlock *pb = op->o_pb;
53 #endif
54         Backend         *be;
55         int rc;
56         const char      *text;
57         int manageDSAit;
58
59 #ifdef NEW_LOGGING
60         LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
61 #else
62         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
63 #endif
64
65         /*
66          * Parse the modify request.  It looks like this:
67          *
68          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
69          *              name    DistinguishedName,
70          *              mods    SEQUENCE OF SEQUENCE {
71          *                      operation       ENUMERATED {
72          *                              add     (0),
73          *                              delete  (1),
74          *                              replace (2)
75          *                      },
76          *                      modification    SEQUENCE {
77          *                              type    AttributeType,
78          *                              values  SET OF AttributeValue
79          *                      }
80          *              }
81          *      }
82          */
83
84         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
87 #else
88                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
89 #endif
90
91                 send_ldap_disconnect( conn, op,
92                         LDAP_PROTOCOL_ERROR, "decoding error" );
93                 return SLAPD_DISCONNECT;
94         }
95
96 #ifdef NEW_LOGGING
97         LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
98 #else
99         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
100 #endif
101
102
103         /* collect modifications & save for later */
104
105         for ( tag = ber_first_element( op->o_ber, &len, &last );
106             tag != LBER_DEFAULT;
107             tag = ber_next_element( op->o_ber, &len, last ) )
108         {
109                 ber_int_t mop;
110                 Modifications tmp, *mod;
111
112 #ifdef SLAP_NVALUES
113                 tmp.sml_nvalues = NULL;
114 #endif
115
116                 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
117                     &tmp.sml_type, &tmp.sml_values )
118                     == LBER_ERROR )
119                 {
120                         send_ldap_disconnect( conn, op,
121                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
122                         rc = SLAPD_DISCONNECT;
123                         goto cleanup;
124                 }
125
126                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
127                 mod->sml_op = mop;
128                 mod->sml_type = tmp.sml_type;
129                 mod->sml_values = tmp.sml_values;
130 #ifdef SLAP_NVALUES
131                 mod->sml_nvalues = NULL;
132 #endif
133                 mod->sml_desc = NULL;
134                 mod->sml_next = NULL;
135                 *modtail = mod;
136
137                 switch( mop ) {
138                 case LDAP_MOD_ADD:
139                         if ( mod->sml_values == NULL ) {
140 #ifdef NEW_LOGGING
141                                 LDAP_LOG( OPERATION, ERR, 
142                                         "do_modify: modify/add operation (%ld) requires values\n",
143                                         (long)mop, 0, 0 );
144 #else
145                                 Debug( LDAP_DEBUG_ANY,
146                                         "do_modify: modify/add operation (%ld) requires values\n",
147                                         (long) mop, 0, 0 );
148 #endif
149
150                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
151                                         NULL, "modify/add operation requires values",
152                                         NULL, NULL );
153                                 rc = LDAP_PROTOCOL_ERROR;
154                                 goto cleanup;
155                         }
156
157                         /* fall through */
158
159                 case LDAP_MOD_DELETE:
160                 case LDAP_MOD_REPLACE:
161                         break;
162
163                 default: {
164 #ifdef NEW_LOGGING
165                                 LDAP_LOG( OPERATION, ERR, 
166                                         "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
167 #else
168                                 Debug( LDAP_DEBUG_ANY,
169                                         "do_modify: invalid modify operation (%ld)\n",
170                                         (long) mop, 0, 0 );
171 #endif
172
173                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
174                                         NULL, "unrecognized modify operation", NULL, NULL );
175                                 rc = LDAP_PROTOCOL_ERROR;
176                                 goto cleanup;
177                         }
178                 }
179
180                 modtail = &mod->sml_next;
181         }
182         *modtail = NULL;
183
184         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
185 #ifdef NEW_LOGGING
186                 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
187 #else
188                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
189 #endif
190
191                 goto cleanup;
192         }
193
194         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
195         if( rc != LDAP_SUCCESS ) {
196 #ifdef NEW_LOGGING
197                 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d  invalid dn (%s)\n",
198                         conn->c_connid, dn.bv_val, 0 );
199 #else
200                 Debug( LDAP_DEBUG_ANY,
201                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
202 #endif
203                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
204                     "invalid DN", NULL, NULL );
205                 goto cleanup;
206         }
207
208         if( ndn.bv_len == 0 ) {
209 #ifdef NEW_LOGGING
210                 LDAP_LOG( OPERATION, ERR, 
211                         "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
212 #else
213                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
214 #endif
215
216                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
217                         NULL, "modify upon the root DSE not supported", NULL, NULL );
218                 goto cleanup;
219
220         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
221 #ifdef NEW_LOGGING
222                 LDAP_LOG( OPERATION, ERR,
223                         "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0  );
224 #else
225                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
226 #endif
227
228                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
229                         NULL, "modification of subschema subentry not supported",
230                         NULL, NULL );
231                 goto cleanup;
232         }
233
234 #ifdef LDAP_DEBUG
235 #ifdef NEW_LOGGING
236         LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0  );
237 #else
238         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
239 #endif
240
241         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
242 #ifdef NEW_LOGGING
243                 LDAP_LOG( OPERATION, DETAIL1, "\t%s:  %s\n", 
244                         tmp->sml_op == LDAP_MOD_ADD ?
245                         "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
246                         "delete" : "replace"), tmp->sml_type.bv_val, 0 );
247
248                 if ( tmp->sml_values == NULL ) {
249                         LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
250                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
251                         LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
252                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
253                         LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
254                 } else {
255                         LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
256                 }
257
258 #else
259                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
260                         tmp->sml_op == LDAP_MOD_ADD
261                                 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
262                                         ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
263
264                 if ( tmp->sml_values == NULL ) {
265                         Debug( LDAP_DEBUG_ARGS, "%s\n",
266                            "\t\tno values", NULL, NULL );
267                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
268                         Debug( LDAP_DEBUG_ARGS, "%s\n",
269                            "\t\tzero values", NULL, NULL );
270                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
271                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
272                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
273                 } else {
274                         Debug( LDAP_DEBUG_ARGS, "%s\n",
275                            "\t\tmultiple values", NULL, NULL );
276                 }
277 #endif
278         }
279 #endif
280
281         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
282                 char abuf[BUFSIZ/2], *ptr = abuf;
283                 int len = 0;
284
285                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
286                         op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
287
288                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
289                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
290                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
291                                     op->o_connid, op->o_opid, abuf, 0, 0 );
292                                 len = 0;
293                                 ptr = abuf;
294                         }
295                         if (len) {
296                                 *ptr++ = ' ';
297                                 len++;
298                         }
299                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
300                         len += tmp->sml_type.bv_len;
301                 }
302                 if (len) {
303                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
304                                 op->o_connid, op->o_opid, abuf, 0, 0 );
305                 }
306         }
307
308         manageDSAit = get_manageDSAit( op );
309
310         /*
311          * We could be serving multiple database backends.  Select the
312          * appropriate one, or send a referral to our "referral server"
313          * if we don't hold it.
314          */
315         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
316                 BerVarray ref = referral_rewrite( default_referral,
317                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
318
319                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
320                         NULL, NULL, ref ? ref : default_referral, NULL );
321
322                 ber_bvarray_free( ref );
323                 goto cleanup;
324         }
325
326         /* check restrictions */
327         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
328         if( rc != LDAP_SUCCESS ) {
329                 send_ldap_result( conn, op, rc,
330                         NULL, text, NULL, NULL );
331                 goto cleanup;
332         }
333
334         /* check for referrals */
335         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
336         if ( rc != LDAP_SUCCESS ) {
337                 goto cleanup;
338         }
339
340 #if defined( LDAP_SLAPI )
341         slapi_x_backend_set_pb( pb, be );
342         slapi_x_connection_set_pb( pb, conn );
343         slapi_x_operation_set_pb( pb, op );
344         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
345         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
346         modv = slapi_x_modifications2ldapmods( &modlist );
347         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
348
349         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
350         if ( rc != 0 ) {
351                 /*
352                  * A preoperation plugin failure will abort the
353                  * entire operation.
354                  */
355 #ifdef NEW_LOGGING
356                 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
357                                 "failed\n", 0, 0, 0 );
358 #else
359                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
360                                 0, 0, 0);
361 #endif
362                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
363                         rc = LDAP_OTHER;
364                 }
365                 ldap_mods_free( modv, 1 );
366                 modv = NULL;
367                 goto cleanup;
368         }
369
370         /*
371          * It's possible that the preoperation plugin changed the
372          * modification array, so we need to convert it back to
373          * a Modification list.
374          *
375          * Calling slapi_x_modifications2ldapmods() destroyed modlist so
376          * we don't need to free it.
377          */
378         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
379         modlist = slapi_x_ldapmods2modifications( modv );
380 #endif /* defined( LDAP_SLAPI ) */
381
382         /*
383          * do the modify if 1 && (2 || 3)
384          * 1) there is a modify function implemented in this backend;
385          * 2) this backend is master for what it holds;
386          * 3) it's a replica and the dn supplied is the update_ndn.
387          */
388         if ( be->be_modify ) {
389                 /* do the update here */
390                 int repl_user = be_isupdate( be, &op->o_ndn );
391 #ifndef SLAPD_MULTIMASTER
392                 /* Multimaster slapd does not have to check for replicator dn
393                  * because it accepts each modify request
394                  */
395                 if ( !be->be_update_ndn.bv_len || repl_user )
396 #endif
397                 {
398                         int update = be->be_update_ndn.bv_len;
399                         const char *text;
400                         char textbuf[SLAP_TEXT_BUFLEN];
401                         size_t textlen = sizeof textbuf;
402
403                         rc = slap_mods_check( modlist, update, &text,
404                                 textbuf, textlen );
405
406                         if( rc != LDAP_SUCCESS ) {
407                                 send_ldap_result( conn, op, rc,
408                                         NULL, text, NULL, NULL );
409                                 goto cleanup;
410                         }
411
412                         if ( !repl_user ) {
413                                 for( modtail = &modlist;
414                                         *modtail != NULL;
415                                         modtail = &(*modtail)->sml_next )
416                                 {
417                                         /* empty */
418                                 }
419
420                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
421                                         textbuf, textlen );
422                                 if( rc != LDAP_SUCCESS ) {
423                                         send_ldap_result( conn, op, rc,
424                                                 NULL, text,
425                                                 NULL, NULL );
426                                         goto cleanup;
427                                 }
428                         }
429
430                         if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
431 #ifdef SLAPD_MULTIMASTER
432                                 && !repl_user
433 #endif
434                         ) {
435                                 /* but we log only the ones not from a replicator user */
436                                 replog( be, op, &pdn, &ndn, modlist );
437                         }
438
439 #ifndef SLAPD_MULTIMASTER
440                 /* send a referral */
441                 } else {
442                         BerVarray defref = be->be_update_refs
443                                 ? be->be_update_refs : default_referral;
444                         BerVarray ref = referral_rewrite( defref,
445                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
446
447                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
448                                 ref ? ref : defref, NULL );
449
450                         ber_bvarray_free( ref );
451 #endif
452                 }
453         } else {
454                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
455                     NULL, "operation not supported within namingContext",
456                         NULL, NULL );
457         }
458
459 #if defined( LDAP_SLAPI )
460         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
461 #ifdef NEW_LOGGING
462                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
463                                 "failed\n", 0, 0, 0 );
464 #else
465                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
466                                 "failed.\n", 0, 0, 0);
467 #endif
468         }
469 #endif /* defined( LDAP_SLAPI ) */
470
471 cleanup:
472         free( pdn.bv_val );
473         free( ndn.bv_val );
474         if ( modlist != NULL ) slap_mods_free( modlist );
475 #if defined( LDAP_SLAPI )
476         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
477 #endif
478         return rc;
479 }
480
481 /*
482  * Do basic attribute type checking and syntax validation.
483  */
484 int slap_mods_check(
485         Modifications *ml,
486         int update,
487         const char **text,
488         char *textbuf,
489         size_t textlen )
490 {
491         int rc;
492
493         for( ; ml != NULL; ml = ml->sml_next ) {
494                 AttributeDescription *ad = NULL;
495
496                 /* convert to attribute description */
497                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
498
499                 if( rc != LDAP_SUCCESS ) {
500                         snprintf( textbuf, textlen, "%s: %s",
501                                 ml->sml_type.bv_val, *text );
502                         *text = textbuf;
503                         return rc;
504                 }
505
506                 ad = ml->sml_desc;
507
508                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
509                         && !slap_ad_is_binary( ad ))
510                 {
511                         /* attribute requires binary transfer */
512                         snprintf( textbuf, textlen,
513                                 "%s: requires ;binary transfer",
514                                 ml->sml_type.bv_val );
515                         *text = textbuf;
516                         return LDAP_UNDEFINED_TYPE;
517                 }
518
519                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
520                         && slap_ad_is_binary( ad ))
521                 {
522                         /* attribute requires binary transfer */
523                         snprintf( textbuf, textlen,
524                                 "%s: disallows ;binary transfer",
525                                 ml->sml_type.bv_val );
526                         *text = textbuf;
527                         return LDAP_UNDEFINED_TYPE;
528                 }
529
530                 if( slap_ad_is_tag_range( ad )) {
531                         /* attribute requires binary transfer */
532                         snprintf( textbuf, textlen,
533                                 "%s: inappropriate use of tag range option",
534                                 ml->sml_type.bv_val );
535                         *text = textbuf;
536                         return LDAP_UNDEFINED_TYPE;
537                 }
538
539                 if (!update && is_at_no_user_mod( ad->ad_type )) {
540                         /* user modification disallowed */
541                         snprintf( textbuf, textlen,
542                                 "%s: no user modification allowed",
543                                 ml->sml_type.bv_val );
544                         *text = textbuf;
545                         return LDAP_CONSTRAINT_VIOLATION;
546                 }
547
548                 if ( is_at_obsolete( ad->ad_type ) &&
549                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
550                 {
551                         /*
552                          * attribute is obsolete,
553                          * only allow replace/delete with no values
554                          */
555                         snprintf( textbuf, textlen,
556                                 "%s: attribute is obsolete",
557                                 ml->sml_type.bv_val );
558                         *text = textbuf;
559                         return LDAP_CONSTRAINT_VIOLATION;
560                 }
561
562                 /*
563                  * check values
564                  */
565                 if( ml->sml_values != NULL ) {
566                         ber_len_t nvals;
567                         slap_syntax_validate_func *validate =
568                                 ad->ad_type->sat_syntax->ssyn_validate;
569                         slap_syntax_transform_func *pretty =
570                                 ad->ad_type->sat_syntax->ssyn_pretty;
571  
572                         if( !pretty && !validate ) {
573                                 *text = "no validator for syntax";
574                                 snprintf( textbuf, textlen,
575                                         "%s: no validator for syntax %s",
576                                         ml->sml_type.bv_val,
577                                         ad->ad_type->sat_syntax->ssyn_oid );
578                                 *text = textbuf;
579                                 return LDAP_INVALID_SYNTAX;
580                         }
581
582                         /*
583                          * check that each value is valid per syntax
584                          *      and pretty if appropriate
585                          */
586                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
587                                 struct berval pval;
588                                 if( pretty ) {
589                                         rc = pretty( ad->ad_type->sat_syntax,
590                                                 &ml->sml_values[nvals], &pval );
591                                 } else {
592                                         rc = validate( ad->ad_type->sat_syntax,
593                                                 &ml->sml_values[nvals] );
594                                 }
595
596                                 if( rc != 0 ) {
597                                         snprintf( textbuf, textlen,
598                                                 "%s: value #%ld invalid per syntax",
599                                                 ml->sml_type.bv_val, (long) nvals );
600                                         *text = textbuf;
601                                         return LDAP_INVALID_SYNTAX;
602                                 }
603
604                                 if( pretty ) {
605                                         ber_memfree( ml->sml_values[nvals].bv_val );
606                                         ml->sml_values[nvals] = pval;
607                                 }
608                         }
609
610                         /*
611                          * a rough single value check... an additional check is needed
612                          * to catch add of single value to existing single valued attribute
613                          */
614                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
615                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
616                         {
617                                 snprintf( textbuf, textlen,
618                                         "%s: multiple values provided",
619                                         ml->sml_type.bv_val );
620                                 *text = textbuf;
621                                 return LDAP_CONSTRAINT_VIOLATION;
622                         }
623
624 #ifdef SLAP_NVALUES
625                         if( nvals && ad->ad_type->sat_equality &&
626                                 ad->ad_type->sat_equality->smr_normalize )
627                         {
628                                 ml->sml_nvalues = ch_malloc( (nvals+1)*sizeof(struct berval) );
629                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
630                                         rc = ad->ad_type->sat_equality->smr_normalize(
631                                                 0,
632                                                 ad->ad_type->sat_syntax,
633                                                 ad->ad_type->sat_equality,
634                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals] );
635                                         if( rc ) {
636 #ifdef NEW_LOGGING
637                                                 LDAP_LOG( OPERATION, DETAIL1,
638                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
639                                                         rc, 0, 0 );
640 #else
641                                                 Debug( LDAP_DEBUG_ANY,
642                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
643                                                         rc, 0, 0 );
644 #endif
645                                                 snprintf( textbuf, textlen,
646                                                         "%s: value #%ld normalization failed",
647                                                         ml->sml_type.bv_val, (long) nvals );
648                                                 *text = textbuf;
649                                                 return rc;
650                                         }
651                                 }
652                                 ml->sml_nvalues[nvals].bv_val = NULL;
653                                 ml->sml_nvalues[nvals].bv_len = 0;
654                         }
655 #endif
656                 }
657         }
658
659         return LDAP_SUCCESS;
660 }
661
662 int slap_mods_opattrs(
663         Backend *be,
664         Operation *op,
665         Modifications *mods,
666         Modifications **modtail,
667         const char **text,
668         char *textbuf, size_t textlen )
669 {
670         struct berval name, timestamp, csn;
671 #ifdef SLAP_NVALUES
672         struct berval nname;
673 #endif
674         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
675         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
676         Modifications *mod;
677
678         int mop = op->o_tag == LDAP_REQ_ADD
679                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
680
681         assert( modtail != NULL );
682         assert( *modtail == NULL );
683
684         if( SLAP_LASTMOD(be) ) {
685                 struct tm *ltm;
686                 time_t now = slap_get_time();
687
688                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
689                 ltm = gmtime( &now );
690                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
691
692                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
693                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
694                 csn.bv_val = csnbuf;
695
696                 timestamp.bv_val = timebuf;
697                 timestamp.bv_len = strlen(timebuf);
698
699                 if( op->o_dn.bv_len == 0 ) {
700                         name.bv_val = SLAPD_ANONYMOUS;
701                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
702 #ifdef SLAP_NVALUES
703                         nname = name;
704 #endif
705                 } else {
706                         name = op->o_dn;
707 #ifdef SLAP_NVALUES
708                         nname = op->o_ndn;
709 #endif
710                 }
711         }
712
713         if( op->o_tag == LDAP_REQ_ADD ) {
714                 struct berval tmpval;
715
716                 if( global_schemacheck ) {
717                         int rc = mods_structural_class( mods, &tmpval,
718                                 text, textbuf, textlen );
719                         if( rc != LDAP_SUCCESS ) {
720                                 return rc;
721                         }
722
723                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
724                         mod->sml_op = mop;
725                         mod->sml_type.bv_val = NULL;
726                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
727                         mod->sml_values =
728                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
729                         ber_dupbv( &mod->sml_values[0], &tmpval );
730                         mod->sml_values[1].bv_len = 0;
731                         mod->sml_values[1].bv_val = NULL;
732                         assert( mod->sml_values[0].bv_val );
733 #ifdef SLAP_NVALUES
734                         mod->sml_nvalues =
735                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
736                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
737                         mod->sml_nvalues[1].bv_len = 0;
738                         mod->sml_nvalues[1].bv_val = NULL;
739                         assert( mod->sml_nvalues[0].bv_val );
740 #endif
741                         *modtail = mod;
742                         modtail = &mod->sml_next;
743                 }
744
745                 if( SLAP_LASTMOD(be) ) {
746                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
747
748                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
749                         tmpval.bv_val = uuidbuf;
750                 
751                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
752                         mod->sml_op = mop;
753                         mod->sml_type.bv_val = NULL;
754                         mod->sml_desc = slap_schema.si_ad_entryUUID;
755                         mod->sml_values =
756                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
757                         ber_dupbv( &mod->sml_values[0], &tmpval );
758                         mod->sml_values[1].bv_len = 0;
759                         mod->sml_values[1].bv_val = NULL;
760                         assert( mod->sml_values[0].bv_val );
761 #ifdef SLAP_NVALUES
762                         mod->sml_nvalues = NULL;
763 #endif
764                         *modtail = mod;
765                         modtail = &mod->sml_next;
766
767                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
768                         mod->sml_op = mop;
769                         mod->sml_type.bv_val = NULL;
770                         mod->sml_desc = slap_schema.si_ad_creatorsName;
771                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
772                         ber_dupbv( &mod->sml_values[0], &name );
773                         mod->sml_values[1].bv_len = 0;
774                         mod->sml_values[1].bv_val = NULL;
775                         assert( mod->sml_values[0].bv_val );
776 #ifdef SLAP_NVALUES
777                         mod->sml_nvalues =
778                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
779                         ber_dupbv( &mod->sml_nvalues[0], &nname );
780                         mod->sml_nvalues[1].bv_len = 0;
781                         mod->sml_nvalues[1].bv_val = NULL;
782                         assert( mod->sml_nvalues[0].bv_val );
783 #endif
784                         *modtail = mod;
785                         modtail = &mod->sml_next;
786
787                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
788                         mod->sml_op = mop;
789                         mod->sml_type.bv_val = NULL;
790                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
791                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
792                         ber_dupbv( &mod->sml_values[0], &timestamp );
793                         mod->sml_values[1].bv_len = 0;
794                         mod->sml_values[1].bv_val = NULL;
795                         assert( mod->sml_values[0].bv_val );
796 #ifdef SLAP_NVALUES
797                         mod->sml_nvalues = NULL;
798 #endif
799                         *modtail = mod;
800                         modtail = &mod->sml_next;
801                 }
802         }
803
804         if( SLAP_LASTMOD(be) ) {
805                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
806                 mod->sml_op = mop;
807                 mod->sml_type.bv_val = NULL;
808                 mod->sml_desc = slap_schema.si_ad_entryCSN;
809                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
810                 ber_dupbv( &mod->sml_values[0], &csn );
811                 mod->sml_values[1].bv_len = 0;
812                 mod->sml_values[1].bv_val = NULL;
813                 assert( mod->sml_values[0].bv_val );
814 #ifdef SLAP_NVALUES
815                 mod->sml_nvalues = NULL;
816 #endif
817                 *modtail = mod;
818                 modtail = &mod->sml_next;
819
820                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
821                 mod->sml_op = mop;
822                 mod->sml_type.bv_val = NULL;
823                 mod->sml_desc = slap_schema.si_ad_modifiersName;
824                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
825                 ber_dupbv( &mod->sml_values[0], &name );
826                 mod->sml_values[1].bv_len = 0;
827                 mod->sml_values[1].bv_val = NULL;
828                 assert( mod->sml_values[0].bv_val );
829 #ifdef SLAP_NVALUES
830                 mod->sml_nvalues =
831                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
832                 ber_dupbv( &mod->sml_nvalues[0], &nname );
833                 mod->sml_nvalues[1].bv_len = 0;
834                 mod->sml_nvalues[1].bv_val = NULL;
835                 assert( mod->sml_nvalues[0].bv_val );
836 #endif
837                 *modtail = mod;
838                 modtail = &mod->sml_next;
839
840                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
841                 mod->sml_op = mop;
842                 mod->sml_type.bv_val = NULL;
843                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
844                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
845                 ber_dupbv( &mod->sml_values[0], &timestamp );
846                 mod->sml_values[1].bv_len = 0;
847                 mod->sml_values[1].bv_val = NULL;
848                 assert( mod->sml_values[0].bv_val );
849 #ifdef SLAP_NVALUES
850                 mod->sml_nvalues = NULL;
851 #endif
852                 *modtail = mod;
853                 modtail = &mod->sml_next;
854         }
855
856         *modtail = NULL;
857         return LDAP_SUCCESS;
858 }
859