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