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