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