]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
- LDAP_SYNCREPL defined in include/ldap.h
[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         if ( modlist == NULL ) {
379                 rs->sr_err = LDAP_SUCCESS;
380                 send_ldap_result( op, rs );
381                 goto cleanup;
382         }
383 #endif /* defined( LDAP_SLAPI ) */
384
385         /*
386          * do the modify if 1 && (2 || 3)
387          * 1) there is a modify function implemented in this backend;
388          * 2) this backend is master for what it holds;
389          * 3) it's a replica and the dn supplied is the update_ndn.
390          */
391         if ( op->o_bd->be_modify ) {
392                 /* do the update here */
393                 int repl_user = be_isupdate( op->o_bd, &op->o_ndn );
394
395                 /* Multimaster slapd does not have to check for replicator dn
396                  * because it accepts each modify request
397                  */
398 #if defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
399                 if ( !op->o_bd->syncinfo &&
400                                 ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
401 #elif defined(LDAP_SYNCREPL) && defined(SLAPD_MULTIMASTER)
402                 if ( !op->o_bd->syncinfo )  /* LDAP_SYNCREPL overrides MM */
403 #elif !defined(LDAP_SYNCREPL) && !defined(SLAPD_MULTIMASTER)
404                 if ( !op->o_bd->be_update_ndn.bv_len || repl_user )
405 #endif
406                 {
407                         int update = op->o_bd->be_update_ndn.bv_len;
408                         char textbuf[SLAP_TEXT_BUFLEN];
409                         size_t textlen = sizeof textbuf;
410
411                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
412                                 textbuf, textlen, NULL );
413
414                         if( rs->sr_err != LDAP_SUCCESS ) {
415                                 send_ldap_result( op, rs );
416                                 goto cleanup;
417                         }
418
419                         if ( !repl_user ) {
420                                 for( modtail = &modlist;
421                                         *modtail != NULL;
422                                         modtail = &(*modtail)->sml_next )
423                                 {
424                                         /* empty */
425                                 }
426
427                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
428                                         &rs->sr_text, textbuf, textlen );
429                                 if( rs->sr_err != LDAP_SUCCESS ) {
430                                         send_ldap_result( op, rs );
431                                         goto cleanup;
432                                 }
433                         }
434
435                         op->orm_modlist = modlist;
436                         if ( (op->o_bd->be_modify)( op, rs ) == 0
437 #ifdef SLAPD_MULTIMASTER
438                                 && !repl_user
439 #endif
440                         ) {
441                                 /* but we log only the ones not from a replicator user */
442                                 replog( op );
443                         }
444
445 #if defined(LDAP_SYNCREPL) || !defined(SLAPD_MULTIMASTER)
446                 /* send a referral */
447                 } else {
448                         BerVarray defref = NULL;
449 #ifdef LDAP_SYNCREPL
450                         if ( op->o_bd->syncinfo ) {
451                                 defref = op->o_bd->syncinfo->master_bv;
452                         } else
453 #endif
454                         {
455                                 defref = op->o_bd->be_update_refs
456                                                 ? op->o_bd->be_update_refs : default_referral;
457                         }
458                         if ( defref != NULL ) {
459                                 rs->sr_ref = referral_rewrite( defref,
460                                         NULL, &op->o_req_dn,
461                                         LDAP_SCOPE_DEFAULT );
462                                 if (!rs->sr_ref) rs->sr_ref = defref;
463                                 rs->sr_err = LDAP_REFERRAL;
464                                 send_ldap_result( op, rs );
465                                 if (rs->sr_ref != defref) {
466                                         ber_bvarray_free( rs->sr_ref );
467                                 }
468                         } else {
469                                 send_ldap_error( op, rs,
470                                                 LDAP_UNWILLING_TO_PERFORM,
471                                                 "referral missing" );
472                         }
473 #endif
474                 }
475         } else {
476                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
477                     "operation not supported within namingContext" );
478         }
479
480 #if defined( LDAP_SLAPI )
481         if ( doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 ) {
482 #ifdef NEW_LOGGING
483                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
484                                 "failed\n", 0, 0, 0 );
485 #else
486                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
487                                 "failed.\n", 0, 0, 0);
488 #endif
489         }
490 #endif /* defined( LDAP_SLAPI ) */
491
492 cleanup:
493         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
494         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
495         if ( modlist != NULL ) slap_mods_free( modlist );
496 #if defined( LDAP_SLAPI )
497         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
498 #endif
499         return rs->sr_err;
500 }
501
502 /*
503  * Do basic attribute type checking and syntax validation.
504  */
505 int slap_mods_check(
506         Modifications *ml,
507         int update,
508         const char **text,
509         char *textbuf,
510         size_t textlen,
511         void *ctx )
512 {
513         int rc;
514
515         for( ; ml != NULL; ml = ml->sml_next ) {
516                 AttributeDescription *ad = NULL;
517
518                 /* convert to attribute description */
519                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
520
521                 if( rc != LDAP_SUCCESS ) {
522                         snprintf( textbuf, textlen, "%s: %s",
523                                 ml->sml_type.bv_val, *text );
524                         *text = textbuf;
525                         return rc;
526                 }
527
528                 ad = ml->sml_desc;
529
530                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
531                         && !slap_ad_is_binary( ad ))
532                 {
533                         /* attribute requires binary transfer */
534                         snprintf( textbuf, textlen,
535                                 "%s: requires ;binary transfer",
536                                 ml->sml_type.bv_val );
537                         *text = textbuf;
538                         return LDAP_UNDEFINED_TYPE;
539                 }
540
541                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
542                         && slap_ad_is_binary( ad ))
543                 {
544                         /* attribute requires binary transfer */
545                         snprintf( textbuf, textlen,
546                                 "%s: disallows ;binary transfer",
547                                 ml->sml_type.bv_val );
548                         *text = textbuf;
549                         return LDAP_UNDEFINED_TYPE;
550                 }
551
552                 if( slap_ad_is_tag_range( ad )) {
553                         /* attribute requires binary transfer */
554                         snprintf( textbuf, textlen,
555                                 "%s: inappropriate use of tag range option",
556                                 ml->sml_type.bv_val );
557                         *text = textbuf;
558                         return LDAP_UNDEFINED_TYPE;
559                 }
560
561                 if (!update && is_at_no_user_mod( ad->ad_type )) {
562                         /* user modification disallowed */
563                         snprintf( textbuf, textlen,
564                                 "%s: no user modification allowed",
565                                 ml->sml_type.bv_val );
566                         *text = textbuf;
567                         return LDAP_CONSTRAINT_VIOLATION;
568                 }
569
570                 if ( is_at_obsolete( ad->ad_type ) &&
571                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_values != NULL ) )
572                 {
573                         /*
574                          * attribute is obsolete,
575                          * only allow replace/delete with no values
576                          */
577                         snprintf( textbuf, textlen,
578                                 "%s: attribute is obsolete",
579                                 ml->sml_type.bv_val );
580                         *text = textbuf;
581                         return LDAP_CONSTRAINT_VIOLATION;
582                 }
583
584                 /*
585                  * check values
586                  */
587                 if( ml->sml_values != NULL ) {
588                         ber_len_t nvals;
589                         slap_syntax_validate_func *validate =
590                                 ad->ad_type->sat_syntax->ssyn_validate;
591                         slap_syntax_transform_func *pretty =
592                                 ad->ad_type->sat_syntax->ssyn_pretty;
593  
594                         if( !pretty && !validate ) {
595                                 *text = "no validator for syntax";
596                                 snprintf( textbuf, textlen,
597                                         "%s: no validator for syntax %s",
598                                         ml->sml_type.bv_val,
599                                         ad->ad_type->sat_syntax->ssyn_oid );
600                                 *text = textbuf;
601                                 return LDAP_INVALID_SYNTAX;
602                         }
603
604                         /*
605                          * check that each value is valid per syntax
606                          *      and pretty if appropriate
607                          */
608                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
609                                 struct berval pval;
610                                 if( pretty ) {
611                                         rc = pretty( ad->ad_type->sat_syntax,
612                                                 &ml->sml_values[nvals], &pval, ctx );
613                                 } else {
614                                         rc = validate( ad->ad_type->sat_syntax,
615                                                 &ml->sml_values[nvals] );
616                                 }
617
618                                 if( rc != 0 ) {
619                                         snprintf( textbuf, textlen,
620                                                 "%s: value #%ld invalid per syntax",
621                                                 ml->sml_type.bv_val, (long) nvals );
622                                         *text = textbuf;
623                                         return LDAP_INVALID_SYNTAX;
624                                 }
625
626                                 if( pretty ) {
627                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
628                                         ml->sml_values[nvals] = pval;
629                                 }
630                         }
631
632                         /*
633                          * a rough single value check... an additional check is needed
634                          * to catch add of single value to existing single valued attribute
635                          */
636                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
637                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
638                         {
639                                 snprintf( textbuf, textlen,
640                                         "%s: multiple values provided",
641                                         ml->sml_type.bv_val );
642                                 *text = textbuf;
643                                 return LDAP_CONSTRAINT_VIOLATION;
644                         }
645
646                         if( nvals && ad->ad_type->sat_equality &&
647                                 ad->ad_type->sat_equality->smr_normalize )
648                         {
649                                 ml->sml_nvalues = ber_memalloc_x(
650                                         (nvals+1)*sizeof(struct berval), ctx );
651
652                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
653                                         rc = ad->ad_type->sat_equality->smr_normalize(
654                                                 0,
655                                                 ad->ad_type->sat_syntax,
656                                                 ad->ad_type->sat_equality,
657                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
658                                         if( rc ) {
659 #ifdef NEW_LOGGING
660                                                 LDAP_LOG( OPERATION, DETAIL1,
661                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
662                                                         rc, 0, 0 );
663 #else
664                                                 Debug( LDAP_DEBUG_ANY,
665                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
666                                                         rc, 0, 0 );
667 #endif
668                                                 snprintf( textbuf, textlen,
669                                                         "%s: value #%ld normalization failed",
670                                                         ml->sml_type.bv_val, (long) nvals );
671                                                 *text = textbuf;
672                                                 return rc;
673                                         }
674                                 }
675
676                                 ml->sml_nvalues[nvals].bv_val = NULL;
677                                 ml->sml_nvalues[nvals].bv_len = 0;
678
679                         } else {
680                         }
681                 }
682         }
683
684         return LDAP_SUCCESS;
685 }
686
687 int slap_mods_opattrs(
688         Operation *op,
689         Modifications *mods,
690         Modifications **modtail,
691         const char **text,
692         char *textbuf, size_t textlen )
693 {
694         struct berval name, timestamp, csn;
695         struct berval nname;
696         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
697         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
698         Modifications *mod;
699
700         int mop = op->o_tag == LDAP_REQ_ADD
701                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
702
703         assert( modtail != NULL );
704         assert( *modtail == NULL );
705
706         if( SLAP_LASTMOD(op->o_bd) ) {
707                 struct tm *ltm;
708                 time_t now = slap_get_time();
709
710                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
711                 ltm = gmtime( &now );
712                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
713
714                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
715                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
716                 csn.bv_val = csnbuf;
717
718                 timestamp.bv_val = timebuf;
719                 timestamp.bv_len = strlen(timebuf);
720
721                 if( op->o_dn.bv_len == 0 ) {
722                         name.bv_val = SLAPD_ANONYMOUS;
723                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
724                         nname = name;
725                 } else {
726                         name = op->o_dn;
727                         nname = op->o_ndn;
728                 }
729         }
730
731         if( op->o_tag == LDAP_REQ_ADD ) {
732                 struct berval tmpval;
733
734                 if( global_schemacheck ) {
735                         int rc = mods_structural_class( mods, &tmpval,
736                                 text, textbuf, textlen );
737                         if( rc != LDAP_SUCCESS ) {
738                                 return rc;
739                         }
740
741                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
742                         mod->sml_op = mop;
743                         mod->sml_type.bv_val = NULL;
744                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
745                         mod->sml_values =
746                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
747                         ber_dupbv( &mod->sml_values[0], &tmpval );
748                         mod->sml_values[1].bv_len = 0;
749                         mod->sml_values[1].bv_val = NULL;
750                         assert( mod->sml_values[0].bv_val );
751                         mod->sml_nvalues =
752                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
753                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
754                         mod->sml_nvalues[1].bv_len = 0;
755                         mod->sml_nvalues[1].bv_val = NULL;
756                         assert( mod->sml_nvalues[0].bv_val );
757                         *modtail = mod;
758                         modtail = &mod->sml_next;
759                 }
760
761                 if( SLAP_LASTMOD(op->o_bd) ) {
762                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
763
764                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
765                         tmpval.bv_val = uuidbuf;
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_entryUUID;
771                         mod->sml_values =
772                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
773                         ber_dupbv( &mod->sml_values[0], &tmpval );
774                         mod->sml_values[1].bv_len = 0;
775                         mod->sml_values[1].bv_val = NULL;
776                         assert( mod->sml_values[0].bv_val );
777                         mod->sml_nvalues = NULL;
778                         *modtail = mod;
779                         modtail = &mod->sml_next;
780
781                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
782                         mod->sml_op = mop;
783                         mod->sml_type.bv_val = NULL;
784                         mod->sml_desc = slap_schema.si_ad_creatorsName;
785                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
786                         ber_dupbv( &mod->sml_values[0], &name );
787                         mod->sml_values[1].bv_len = 0;
788                         mod->sml_values[1].bv_val = NULL;
789                         assert( mod->sml_values[0].bv_val );
790                         mod->sml_nvalues =
791                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
792                         ber_dupbv( &mod->sml_nvalues[0], &nname );
793                         mod->sml_nvalues[1].bv_len = 0;
794                         mod->sml_nvalues[1].bv_val = NULL;
795                         assert( mod->sml_nvalues[0].bv_val );
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_createTimestamp;
803                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
804                         ber_dupbv( &mod->sml_values[0], &timestamp );
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                         mod->sml_nvalues = NULL;
809                         *modtail = mod;
810                         modtail = &mod->sml_next;
811                 }
812         }
813
814         if( SLAP_LASTMOD(op->o_bd) ) {
815                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
816                 mod->sml_op = mop;
817                 mod->sml_type.bv_val = NULL;
818                 mod->sml_desc = slap_schema.si_ad_entryCSN;
819                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
820                 ber_dupbv( &mod->sml_values[0], &csn );
821                 mod->sml_values[1].bv_len = 0;
822                 mod->sml_values[1].bv_val = NULL;
823                 assert( mod->sml_values[0].bv_val );
824                 mod->sml_nvalues = NULL;
825                 *modtail = mod;
826                 modtail = &mod->sml_next;
827
828                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
829                 mod->sml_op = mop;
830                 mod->sml_type.bv_val = NULL;
831                 mod->sml_desc = slap_schema.si_ad_modifiersName;
832                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
833                 ber_dupbv( &mod->sml_values[0], &name );
834                 mod->sml_values[1].bv_len = 0;
835                 mod->sml_values[1].bv_val = NULL;
836                 assert( mod->sml_values[0].bv_val );
837                 mod->sml_nvalues =
838                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
839                 ber_dupbv( &mod->sml_nvalues[0], &nname );
840                 mod->sml_nvalues[1].bv_len = 0;
841                 mod->sml_nvalues[1].bv_val = NULL;
842                 assert( mod->sml_nvalues[0].bv_val );
843                 *modtail = mod;
844                 modtail = &mod->sml_next;
845
846                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
847                 mod->sml_op = mop;
848                 mod->sml_type.bv_val = NULL;
849                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
850                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
851                 ber_dupbv( &mod->sml_values[0], &timestamp );
852                 mod->sml_values[1].bv_len = 0;
853                 mod->sml_values[1].bv_val = NULL;
854                 assert( mod->sml_values[0].bv_val );
855                 mod->sml_nvalues = NULL;
856                 *modtail = mod;
857                 modtail = &mod->sml_next;
858         }
859
860         *modtail = NULL;
861         return LDAP_SUCCESS;
862 }
863