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