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