]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
cleanup
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms are permitted
19  * provided that this notice is preserved and that due credit is given
20  * to the University of Michigan at Ann Arbor. The name of the University
21  * may not be used to endorse or promote products derived from this
22  * software without specific prior written permission. This software
23  * is provided ``as is'' without express or implied warranty.
24  */
25
26 #include "portable.h"
27
28 #include <stdio.h>
29
30 #include <ac/socket.h>
31 #include <ac/string.h>
32 #include <ac/time.h>
33
34 #include "slap.h"
35 #include "lutil.h"
36
37
38 int
39 do_modify(
40     Operation   *op,
41     SlapReply   *rs )
42 {
43         struct berval dn = BER_BVNULL;
44         char            *last;
45         ber_tag_t       tag;
46         ber_len_t       len;
47         Modifications   *modlist = NULL;
48         Modifications   **modtail = &modlist;
49         int             increment = 0;
50         char            textbuf[ SLAP_TEXT_BUFLEN ];
51         size_t          textlen = sizeof( textbuf );
52
53         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
54
55         /*
56          * Parse the modify request.  It looks like this:
57          *
58          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
59          *              name    DistinguishedName,
60          *              mods    SEQUENCE OF SEQUENCE {
61          *                      operation       ENUMERATED {
62          *                              add     (0),
63          *                              delete  (1),
64          *                              replace (2)
65          *                      },
66          *                      modification    SEQUENCE {
67          *                              type    AttributeType,
68          *                              values  SET OF AttributeValue
69          *                      }
70          *              }
71          *      }
72          */
73
74         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
75                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
76
77                 send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR, "decoding error" );
78                 return SLAPD_DISCONNECT;
79         }
80
81         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
82
83         /* collect modifications & save for later */
84         for ( tag = ber_first_element( op->o_ber, &len, &last );
85             tag != LBER_DEFAULT;
86             tag = ber_next_element( op->o_ber, &len, last ) )
87         {
88                 ber_int_t mop;
89                 Modifications tmp, *mod;
90
91                 tmp.sml_nvalues = NULL;
92
93                 if ( ber_scanf( op->o_ber, "{e{m[W]}}", &mop,
94                     &tmp.sml_type, &tmp.sml_values ) == LBER_ERROR )
95                 {
96                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR,
97                                 "decoding modlist error" );
98                         rs->sr_err = SLAPD_DISCONNECT;
99                         goto cleanup;
100                 }
101
102                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
103                 mod->sml_op = mop;
104                 mod->sml_flags = 0;
105                 mod->sml_type = tmp.sml_type;
106                 mod->sml_values = tmp.sml_values;
107                 mod->sml_nvalues = NULL;
108                 mod->sml_desc = NULL;
109                 mod->sml_next = NULL;
110                 *modtail = mod;
111
112                 switch( mop ) {
113                 case LDAP_MOD_ADD:
114                         if ( mod->sml_values == NULL ) {
115                                 Debug( LDAP_DEBUG_ANY,
116                                         "do_modify: modify/add operation (%ld) requires values\n",
117                                         (long) mop, 0, 0 );
118
119                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
120                                         "modify/add operation requires values" );
121                                 goto cleanup;
122                         }
123
124                         /* fall through */
125
126                 case LDAP_MOD_DELETE:
127                 case LDAP_MOD_REPLACE:
128                         break;
129
130                 case LDAP_MOD_INCREMENT:
131                         if( op->o_protocol >= LDAP_VERSION3 ) {
132                                 increment++;
133                                 if ( mod->sml_values == NULL ) {
134                                         Debug( LDAP_DEBUG_ANY, "do_modify: "
135                                                 "modify/increment operation (%ld) requires value\n",
136                                                 (long) mop, 0, 0 );
137
138                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
139                                                 "modify/increment operation requires value" );
140                                         goto cleanup;
141                                 }
142
143                                 if ( !BER_BVISNULL( &mod->sml_values[ 1 ] ) ) {
144                                         Debug( LDAP_DEBUG_ANY, "do_modify: modify/increment "
145                                                 "operation (%ld) requires single value\n",
146                                                 (long) mop, 0, 0 );
147
148                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
149                                                 "modify/increment operation requires single value" );
150                                         goto cleanup;
151                                 }
152
153                                 break;
154                         }
155                         /* fall thru */
156
157                 default: {
158                                 Debug( LDAP_DEBUG_ANY,
159                                         "do_modify: unrecognized modify operation (%ld)\n",
160                                         (long) mop, 0, 0 );
161
162                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
163                                         "unrecognized modify operation" );
164                                 goto cleanup;
165                         }
166                 }
167
168                 modtail = &mod->sml_next;
169         }
170         *modtail = NULL;
171
172         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
173                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
174                 goto cleanup;
175         }
176
177         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
178                 op->o_tmpmemctx );
179         if( rs->sr_err != LDAP_SUCCESS ) {
180                 Debug( LDAP_DEBUG_ANY,
181                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
182                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
183                 goto cleanup;
184         }
185
186         rs->sr_err = slap_mods_check( modlist,
187                 &rs->sr_text, textbuf, textlen, NULL );
188
189         if ( rs->sr_err != LDAP_SUCCESS ) {
190                 send_ldap_result( op, rs );
191                 goto cleanup;
192         }
193
194         /* FIXME: needs review */
195         op->orm_modlist = modlist;
196         op->orm_increment = increment;
197
198         op->o_bd = frontendDB;
199         rs->sr_err = frontendDB->be_modify( op, rs );
200
201 cleanup:
202         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
203         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
204         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
205
206         return rs->sr_err;
207 }
208
209 int
210 fe_op_modify( Operation *op, SlapReply *rs )
211 {
212 #ifdef LDAP_DEBUG
213         Modifications   *tmp;
214 #endif
215         int             manageDSAit;
216         BackendDB       *op_be, *bd = op->o_bd;
217         char            textbuf[ SLAP_TEXT_BUFLEN ];
218         size_t          textlen = sizeof( textbuf );
219         
220         if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
221                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
222
223                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
224                         "modify upon the root DSE not supported" );
225                 goto cleanup;
226
227         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
228                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
229
230                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
231                         "modification of subschema subentry not supported" );
232                 goto cleanup;
233         }
234
235 #ifdef LDAP_DEBUG
236         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
237
238         for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
239                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
240                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
241                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
242                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
243                                         "replace")), tmp->sml_type.bv_val, 0 );
244
245                 if ( tmp->sml_values == NULL ) {
246                         Debug( LDAP_DEBUG_ARGS, "%s\n",
247                            "\t\tno values", NULL, NULL );
248                 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
249                         Debug( LDAP_DEBUG_ARGS, "%s\n",
250                            "\t\tzero values", NULL, NULL );
251                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
252                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
253                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
254                 } else {
255                         Debug( LDAP_DEBUG_ARGS, "%s\n",
256                            "\t\tmultiple values", NULL, NULL );
257                 }
258         }
259
260         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
261                 char abuf[BUFSIZ/2], *ptr = abuf;
262                 int len = 0;
263
264                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
265                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
266
267                 for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
268                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
269                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
270                                     op->o_log_prefix, abuf, 0, 0, 0 );
271
272                                 len = 0;
273                                 ptr = abuf;
274
275                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
276                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
277                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
278                                         continue;
279                                 }
280                         }
281                         if (len) {
282                                 *ptr++ = ' ';
283                                 len++;
284                         }
285                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
286                         len += tmp->sml_type.bv_len;
287                 }
288                 if (len) {
289                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
290                                 op->o_log_prefix, abuf, 0, 0, 0 );
291                 }
292         }
293 #endif  /* LDAP_DEBUG */
294
295         manageDSAit = get_manageDSAit( op );
296
297         /*
298          * We could be serving multiple database backends.  Select the
299          * appropriate one, or send a referral to our "referral server"
300          * if we don't hold it.
301          */
302         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
303         if ( op->o_bd == NULL ) {
304                 op->o_bd = bd;
305                 rs->sr_ref = referral_rewrite( default_referral,
306                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
307                 if ( !rs->sr_ref ) {
308                         rs->sr_ref = default_referral;
309                 }
310
311                 if ( rs->sr_ref != NULL ) {
312                         rs->sr_err = LDAP_REFERRAL;
313                         send_ldap_result( op, rs );
314
315                         if ( rs->sr_ref != default_referral ) {
316                                 ber_bvarray_free( rs->sr_ref );
317                         }
318
319                 } else {
320                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
321                                 "no global superior knowledge" );
322                 }
323                 goto cleanup;
324         }
325
326         /* If we've got a glued backend, check the real backend */
327         op_be = op->o_bd;
328         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
329                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
330         }
331
332         /* check restrictions */
333         if ( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
334                 send_ldap_result( op, rs );
335                 goto cleanup;
336         }
337
338         /* check for referrals */
339         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
340                 goto cleanup;
341         }
342
343         rs->sr_err = slap_mods_obsolete_check( op, op->orm_modlist,
344                 &rs->sr_text, textbuf, textlen );
345         if ( rs->sr_err != LDAP_SUCCESS ) {
346                 send_ldap_result( op, rs );
347                 goto cleanup;
348         }
349
350         /* check for modify/increment support */
351         if ( op->orm_increment && !SLAP_INCREMENT( op->o_bd ) ) {
352                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
353                         "modify/increment not supported in context" );
354         }
355
356         /*
357          * do the modify if 1 && (2 || 3)
358          * 1) there is a modify function implemented in this backend;
359          * 2) this backend is master for what it holds;
360          * 3) it's a replica and the dn supplied is the update_ndn.
361          */
362         if ( op->o_bd->be_modify ) {
363                 /* do the update here */
364                 int repl_user = be_isupdate( op );
365
366                 /*
367                  * Multimaster slapd does not have to check for replicator dn
368                  * because it accepts each modify request
369                  */
370                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
371                         int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
372                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
373
374                         op->o_bd = op_be;
375
376                         if ( !update ) {
377                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->orm_modlist,
378                                         &rs->sr_text, textbuf, textlen );
379                                 if ( rs->sr_err != LDAP_SUCCESS ) {
380                                         send_ldap_result( op, rs );
381                                         goto cleanup;
382                                 }
383                         }
384
385                         if ( !repl_user ) {
386                                 /* but multimaster slapd logs only the ones 
387                                  * not from a replicator user */
388                                 cb.sc_next = op->o_callback;
389                                 op->o_callback = &cb;
390                         }
391                         op->o_bd->be_modify( op, rs );
392
393                 } else { /* send a referral */
394                         BerVarray defref = op->o_bd->be_update_refs
395                                 ? op->o_bd->be_update_refs : default_referral;
396                         if ( defref != NULL ) {
397                                 rs->sr_ref = referral_rewrite( defref,
398                                         NULL, &op->o_req_dn,
399                                         LDAP_SCOPE_DEFAULT );
400                                 if ( rs->sr_ref == NULL ) {
401                                         /* FIXME: must duplicate, because
402                                          * overlays may muck with it */
403                                         rs->sr_ref = defref;
404                                 }
405                                 rs->sr_err = LDAP_REFERRAL;
406                                 send_ldap_result( op, rs );
407                                 if ( rs->sr_ref != defref ) {
408                                         ber_bvarray_free( rs->sr_ref );
409                                 }
410
411                         } else {
412                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
413                                         "shadow context; no update referral" );
414                         }
415                 }
416
417         } else {
418                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
419                     "operation not supported within namingContext" );
420         }
421
422 cleanup:;
423         op->o_bd = bd;
424         return rs->sr_err;
425 }
426
427 /*
428  * Obsolete constraint checking.
429  */
430 int
431 slap_mods_obsolete_check(
432         Operation *op,
433         Modifications *ml,
434         const char **text,
435         char *textbuf,
436         size_t textlen )
437 {
438         if( get_manageDIT( op ) ) return LDAP_SUCCESS;
439
440         for ( ; ml != NULL; ml = ml->sml_next ) {
441                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
442                         (( ml->sml_op != LDAP_MOD_REPLACE &&
443                                 ml->sml_op != LDAP_MOD_DELETE ) ||
444                                         ml->sml_values != NULL ))
445                 {
446                         /*
447                          * attribute is obsolete,
448                          * only allow replace/delete with no values
449                          */
450                         snprintf( textbuf, textlen,
451                                 "%s: attribute is obsolete",
452                                 ml->sml_type.bv_val );
453                         *text = textbuf;
454                         return LDAP_CONSTRAINT_VIOLATION;
455                 }
456         }
457
458         return LDAP_SUCCESS;
459 }
460
461 /*
462  * No-user-modification constraint checking.
463  */
464 int
465 slap_mods_no_user_mod_check(
466         Operation *op,
467         Modifications *ml,
468         const char **text,
469         char *textbuf,
470         size_t textlen )
471 {
472         for ( ; ml != NULL; ml = ml->sml_next ) {
473                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
474                         continue;
475                 }
476
477                 if ( get_manageDIT( op ) ) {
478                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
479                                 ml->sml_flags |= SLAP_MOD_MANAGING;
480                                 continue;
481                         }
482
483                         /* attribute not manageable */
484                         snprintf( textbuf, textlen,
485                                 "%s: no-user-modification attribute not manageable",
486                                 ml->sml_type.bv_val );
487
488                 } else {
489                         /* user modification disallowed */
490                         snprintf( textbuf, textlen,
491                                 "%s: no user modification allowed",
492                                 ml->sml_type.bv_val );
493                 }
494
495                 *text = textbuf;
496                 return LDAP_CONSTRAINT_VIOLATION;
497         }
498
499         return LDAP_SUCCESS;
500 }
501
502 int
503 slap_mods_no_repl_user_mod_check(
504         Operation *op,
505         Modifications *ml,
506         const char **text,
507         char *textbuf,
508         size_t textlen )
509 {
510         Modifications *mods;
511         Modifications *modp;
512
513         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
514                 assert( mods->sml_op == LDAP_MOD_ADD );
515
516                 /* check doesn't already appear */
517                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
518                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
519                                 snprintf( textbuf, textlen,
520                                         "attribute '%s' provided more than once",
521                                         mods->sml_desc->ad_cname.bv_val );
522                                 *text = textbuf;
523                                 return LDAP_TYPE_OR_VALUE_EXISTS;
524                         }
525                 }
526         }
527
528         return LDAP_SUCCESS;
529 }
530
531 /*
532  * Do basic attribute type checking and syntax validation.
533  */
534 int slap_mods_check(
535         Modifications *ml,
536         const char **text,
537         char *textbuf,
538         size_t textlen,
539         void *ctx )
540 {
541         int rc;
542
543         for( ; ml != NULL; ml = ml->sml_next ) {
544                 AttributeDescription *ad = NULL;
545
546                 /* convert to attribute description */
547                 if ( ml->sml_desc == NULL ) {
548                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
549                         if( rc != LDAP_SUCCESS ) {
550                                 snprintf( textbuf, textlen, "%s: %s",
551                                         ml->sml_type.bv_val, *text );
552                                 *text = textbuf;
553                                 return rc;
554                         }
555                 }
556
557                 ad = ml->sml_desc;
558
559                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
560                         && !slap_ad_is_binary( ad ))
561                 {
562                         /* attribute requires binary transfer */
563                         snprintf( textbuf, textlen,
564                                 "%s: requires ;binary transfer",
565                                 ml->sml_type.bv_val );
566                         *text = textbuf;
567                         return LDAP_UNDEFINED_TYPE;
568                 }
569
570                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
571                         && slap_ad_is_binary( ad ))
572                 {
573                         /* attribute does not require binary transfer */
574                         snprintf( textbuf, textlen,
575                                 "%s: disallows ;binary transfer",
576                                 ml->sml_type.bv_val );
577                         *text = textbuf;
578                         return LDAP_UNDEFINED_TYPE;
579                 }
580
581                 if( slap_ad_is_tag_range( ad )) {
582                         /* attribute requires binary transfer */
583                         snprintf( textbuf, textlen,
584                                 "%s: inappropriate use of tag range option",
585                                 ml->sml_type.bv_val );
586                         *text = textbuf;
587                         return LDAP_UNDEFINED_TYPE;
588                 }
589
590 #if 0
591                 if ( is_at_obsolete( ad->ad_type ) &&
592                         (( ml->sml_op != LDAP_MOD_REPLACE &&
593                                 ml->sml_op != LDAP_MOD_DELETE ) ||
594                                         ml->sml_values != NULL ))
595                 {
596                         /*
597                          * attribute is obsolete,
598                          * only allow replace/delete with no values
599                          */
600                         snprintf( textbuf, textlen,
601                                 "%s: attribute is obsolete",
602                                 ml->sml_type.bv_val );
603                         *text = textbuf;
604                         return LDAP_CONSTRAINT_VIOLATION;
605                 }
606 #endif
607
608                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
609 #ifdef SLAPD_REAL_SYNTAX
610                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
611 #endif
612                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
613                 {
614                         /*
615                          * attribute values must be INTEGER or REAL
616                          */
617                         snprintf( textbuf, textlen,
618                                 "%s: attribute syntax inappropriate for increment",
619                                 ml->sml_type.bv_val );
620                         *text = textbuf;
621                         return LDAP_CONSTRAINT_VIOLATION;
622                 }
623
624                 /*
625                  * check values
626                  */
627                 if( ml->sml_values != NULL ) {
628                         ber_len_t nvals;
629                         slap_syntax_validate_func *validate =
630                                 ad->ad_type->sat_syntax->ssyn_validate;
631                         slap_syntax_transform_func *pretty =
632                                 ad->ad_type->sat_syntax->ssyn_pretty;
633  
634                         if( !pretty && !validate ) {
635                                 *text = "no validator for syntax";
636                                 snprintf( textbuf, textlen,
637                                         "%s: no validator for syntax %s",
638                                         ml->sml_type.bv_val,
639                                         ad->ad_type->sat_syntax->ssyn_oid );
640                                 *text = textbuf;
641                                 return LDAP_INVALID_SYNTAX;
642                         }
643
644                         /*
645                          * check that each value is valid per syntax
646                          *      and pretty if appropriate
647                          */
648                         for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
649                                 struct berval pval;
650
651                                 if ( pretty ) {
652 #ifdef SLAP_ORDERED_PRETTYNORM
653                                         rc = ordered_value_pretty( ad,
654                                                 &ml->sml_values[nvals], &pval, ctx );
655 #else /* ! SLAP_ORDERED_PRETTYNORM */
656                                         rc = pretty( ad->ad_type->sat_syntax,
657                                                 &ml->sml_values[nvals], &pval, ctx );
658 #endif /* ! SLAP_ORDERED_PRETTYNORM */
659                                 } else {
660 #ifdef SLAP_ORDERED_PRETTYNORM
661                                         rc = ordered_value_validate( ad,
662                                                 &ml->sml_values[nvals], ml->sml_op );
663 #else /* ! SLAP_ORDERED_PRETTYNORM */
664                                         rc = validate( ad->ad_type->sat_syntax,
665                                                 &ml->sml_values[nvals] );
666 #endif /* ! SLAP_ORDERED_PRETTYNORM */
667                                 }
668
669                                 if( rc != 0 ) {
670                                         snprintf( textbuf, textlen,
671                                                 "%s: value #%ld invalid per syntax",
672                                                 ml->sml_type.bv_val, (long) nvals );
673                                         *text = textbuf;
674                                         return LDAP_INVALID_SYNTAX;
675                                 }
676
677                                 if( pretty ) {
678                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
679                                         ml->sml_values[nvals] = pval;
680                                 }
681                         }
682
683                         /*
684                          * a rough single value check... an additional check is needed
685                          * to catch add of single value to existing single valued attribute
686                          */
687                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
688                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
689                         {
690                                 snprintf( textbuf, textlen,
691                                         "%s: multiple values provided",
692                                         ml->sml_type.bv_val );
693                                 *text = textbuf;
694                                 return LDAP_CONSTRAINT_VIOLATION;
695                         }
696
697                         /* if the type has a normalizer, generate the
698                          * normalized values. otherwise leave them NULL.
699                          *
700                          * this is different from the rule for attributes
701                          * in an entry - in an attribute list, the normalized
702                          * value is set equal to the non-normalized value
703                          * when there is no normalizer.
704                          */
705                         if( nvals && ad->ad_type->sat_equality &&
706                                 ad->ad_type->sat_equality->smr_normalize )
707                         {
708                                 ml->sml_nvalues = ber_memalloc_x(
709                                         (nvals+1)*sizeof(struct berval), ctx );
710
711                                 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
712 #ifdef SLAP_ORDERED_PRETTYNORM
713                                         rc = ordered_value_normalize(
714                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
715                                                 ad,
716                                                 ad->ad_type->sat_equality,
717                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
718 #else /* ! SLAP_ORDERED_PRETTYNORM */
719                                         rc = ad->ad_type->sat_equality->smr_normalize(
720                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
721                                                 ad->ad_type->sat_syntax,
722                                                 ad->ad_type->sat_equality,
723                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
724 #endif /* ! SLAP_ORDERED_PRETTYNORM */
725                                         if ( rc ) {
726                                                 Debug( LDAP_DEBUG_ANY,
727                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
728                                                         rc, 0, 0 );
729                                                 snprintf( textbuf, textlen,
730                                                         "%s: value #%ld normalization failed",
731                                                         ml->sml_type.bv_val, (long) nvals );
732                                                 *text = textbuf;
733                                                 return rc;
734                                         }
735                                 }
736
737                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
738                         }
739
740                         /* check for duplicates, but ignore Deletes.
741                          */
742                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
743                                 int             i, j, rc, match;
744                                 MatchingRule *mr = ad->ad_type->sat_equality;
745
746                                 for ( i = 1; i < nvals ; i++ ) {
747                                         /* test asserted values against themselves */
748                                         for( j = 0; j < i; j++ ) {
749                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
750                                                         SLAP_MR_EQUALITY
751                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
752                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
753                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
754                                                         ml->sml_nvalues
755                                                                 ? &ml->sml_nvalues[i]
756                                                                 : &ml->sml_values[i],
757                                                         ml->sml_nvalues
758                                                                 ? &ml->sml_nvalues[j]
759                                                                 : &ml->sml_values[j],
760                                                         text );
761                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
762                                                         /* value exists already */
763                                                         snprintf( textbuf, textlen,
764                                                                 "%s: value #%d provided more than once",
765                                                                 ml->sml_desc->ad_cname.bv_val, j );
766                                                         *text = textbuf;
767                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
768
769                                                 } else if ( rc != LDAP_SUCCESS ) {
770                                                         return rc;
771                                                 }
772                                         }
773                                 }
774                         }
775
776                 }
777         }
778
779         return LDAP_SUCCESS;
780 }
781
782 /* Enter with bv->bv_len = sizeof buffer, returns with
783  * actual length of string
784  */
785 void slap_timestamp( time_t *tm, struct berval *bv )
786 {
787         struct tm *ltm;
788 #ifdef HAVE_GMTIME_R
789         struct tm ltm_buf;
790
791         ltm = gmtime_r( tm, &ltm_buf );
792 #else
793         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
794         ltm = gmtime( tm );
795 #endif
796
797         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
798
799 #ifndef HAVE_GMTIME_R
800         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
801 #endif
802 }
803
804 /* Called for all modify and modrdn ops. If the current op was replicated
805  * from elsewhere, all of the attrs should already be present.
806  */
807 void slap_mods_opattrs(
808         Operation *op,
809         Modifications **modsp,
810         int manage_ctxcsn )
811 {
812         struct berval name, timestamp, csn = BER_BVNULL;
813         struct berval nname;
814         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
815         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
816         Modifications *mod, **modtail, *modlast;
817         int gotcsn = 0, gotmname = 0, gotmtime = 0;
818
819         if ( SLAP_LASTMOD( op->o_bd ) ) {
820                 char *ptr;
821                 timestamp.bv_val = timebuf;
822                 for ( modtail = modsp; *modtail; modtail = &(*modtail)->sml_next ) {
823                         if ( (*modtail)->sml_op != LDAP_MOD_ADD &&
824                                 (*modtail)->sml_op != LDAP_MOD_REPLACE ) continue;
825                         if ( (*modtail)->sml_desc == slap_schema.si_ad_entryCSN ) {
826                                 csn = (*modtail)->sml_values[0];
827                                 gotcsn = 1;
828                         } else
829                         if ( (*modtail)->sml_desc == slap_schema.si_ad_modifiersName ) {
830                                 gotmname = 1;
831                         } else
832                         if ( (*modtail)->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
833                                 gotmtime = 1;
834                         }
835                 }
836                 if ( BER_BVISEMPTY( &op->o_csn )) {
837                         if ( !gotcsn ) {
838                                 csn.bv_val = csnbuf;
839                                 csn.bv_len = sizeof( csnbuf );
840                                 slap_get_csn( op, &csn, manage_ctxcsn );
841                         } else {
842                                 if ( manage_ctxcsn )
843                                         slap_queue_csn( op, &csn );
844                         }
845                 } else {
846                         csn = op->o_csn;
847                 }
848                 ptr = ber_bvchr( &csn, '#' );
849                 if ( ptr ) {
850                         timestamp.bv_len = ptr - csn.bv_val;
851                         if ( timestamp.bv_len >= sizeof( timebuf ))     /* ?!? */
852                                 timestamp.bv_len = sizeof( timebuf ) - 1;
853                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
854                         timebuf[timestamp.bv_len] = '\0';
855                 } else {
856                         time_t now = slap_get_time();
857
858                         timestamp.bv_len = sizeof(timebuf);
859
860                         slap_timestamp( &now, &timestamp );
861                 }
862
863                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
864                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
865                         nname = name;
866                 } else {
867                         name = op->o_dn;
868                         nname = op->o_ndn;
869                 }
870
871                 if ( !gotcsn ) {
872                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
873                         mod->sml_op = LDAP_MOD_REPLACE;
874                         mod->sml_flags = SLAP_MOD_INTERNAL;
875                         mod->sml_next = NULL;
876                         BER_BVZERO( &mod->sml_type );
877                         mod->sml_desc = slap_schema.si_ad_entryCSN;
878                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
879                         ber_dupbv( &mod->sml_values[0], &csn );
880                         BER_BVZERO( &mod->sml_values[1] );
881                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
882                         mod->sml_nvalues = NULL;
883                         *modtail = mod;
884                         modlast = mod;
885                         modtail = &mod->sml_next;
886                 }
887
888                 if ( !gotmname ) {
889                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
890                         mod->sml_op = LDAP_MOD_REPLACE;
891                         mod->sml_flags = SLAP_MOD_INTERNAL;
892                         mod->sml_next = NULL;
893                         BER_BVZERO( &mod->sml_type );
894                         mod->sml_desc = slap_schema.si_ad_modifiersName;
895                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
896                         ber_dupbv( &mod->sml_values[0], &name );
897                         BER_BVZERO( &mod->sml_values[1] );
898                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
899                         mod->sml_nvalues =
900                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
901                         ber_dupbv( &mod->sml_nvalues[0], &nname );
902                         BER_BVZERO( &mod->sml_nvalues[1] );
903                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
904                         *modtail = mod;
905                         modtail = &mod->sml_next;
906                 }
907
908                 if ( !gotmtime ) {
909                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
910                         mod->sml_op = LDAP_MOD_REPLACE;
911                         mod->sml_flags = SLAP_MOD_INTERNAL;
912                         mod->sml_next = NULL;
913                         BER_BVZERO( &mod->sml_type );
914                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
915                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
916                         ber_dupbv( &mod->sml_values[0], &timestamp );
917                         BER_BVZERO( &mod->sml_values[1] );
918                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
919                         mod->sml_nvalues = NULL;
920                         *modtail = mod;
921                         modtail = &mod->sml_next;
922                 }
923         }
924 }
925