]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
Fix typo in last commit -- shorten message by removing
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2005 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 ( modlist == NULL ) {
173                 Debug( LDAP_DEBUG_ANY, "do_modify: no modifications\n", 0, 0, 0 );
174                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
175                         "change sequence empty" );
176
177                 goto cleanup;
178         }
179
180         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
181                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
182
183                 goto cleanup;
184         }
185
186         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
187                 op->o_tmpmemctx );
188         if( rs->sr_err != LDAP_SUCCESS ) {
189                 Debug( LDAP_DEBUG_ANY,
190                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
191                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
192                 goto cleanup;
193         }
194
195         rs->sr_err = slap_mods_check( modlist,
196                 &rs->sr_text, textbuf, textlen, NULL );
197
198         if ( rs->sr_err != LDAP_SUCCESS ) {
199                 send_ldap_result( op, rs );
200                 goto cleanup;
201         }
202
203         /* FIXME: needs review */
204         op->orm_modlist = modlist;
205         op->orm_increment = increment;
206
207         op->o_bd = frontendDB;
208         rs->sr_err = frontendDB->be_modify( op, rs );
209
210 cleanup:
211         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
212         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
213         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
214
215         return rs->sr_err;
216 }
217
218 int
219 fe_op_modify( Operation *op, SlapReply *rs )
220 {
221 #ifdef LDAP_DEBUG
222         Modifications   *tmp;
223 #endif
224         int             manageDSAit;
225         Modifications   *modlist = op->orm_modlist;
226         int             increment = op->orm_increment;
227         BackendDB *op_be;
228         char            textbuf[ SLAP_TEXT_BUFLEN ];
229         size_t          textlen = sizeof( textbuf );
230         
231         if( BER_BVISEMPTY( &op->o_req_ndn ) ) {
232                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
233
234                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
235                         "modify upon the root DSE not supported" );
236                 goto cleanup;
237
238         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
239                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
240
241                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
242                         "modification of subschema subentry not supported" );
243                 goto cleanup;
244         }
245
246 #ifdef LDAP_DEBUG
247         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
248
249         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
250                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
251                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
252                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
253                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
254                                         "replace")), tmp->sml_type.bv_val, 0 );
255
256                 if ( tmp->sml_values == NULL ) {
257                         Debug( LDAP_DEBUG_ARGS, "%s\n",
258                            "\t\tno values", NULL, NULL );
259                 } else if ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
260                         Debug( LDAP_DEBUG_ARGS, "%s\n",
261                            "\t\tzero values", NULL, NULL );
262                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
263                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
264                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
265                 } else {
266                         Debug( LDAP_DEBUG_ARGS, "%s\n",
267                            "\t\tmultiple values", NULL, NULL );
268                 }
269         }
270
271         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
272                 char abuf[BUFSIZ/2], *ptr = abuf;
273                 int len = 0;
274
275                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
276                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
277
278                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
279                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
280                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
281                                     op->o_log_prefix, abuf, 0, 0, 0 );
282
283                                 len = 0;
284                                 ptr = abuf;
285
286                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
287                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
288                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
289                                         continue;
290                                 }
291                         }
292                         if (len) {
293                                 *ptr++ = ' ';
294                                 len++;
295                         }
296                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
297                         len += tmp->sml_type.bv_len;
298                 }
299                 if (len) {
300                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
301                                 op->o_log_prefix, abuf, 0, 0, 0 );
302                 }
303         }
304 #endif  /* LDAP_DEBUG */
305
306         manageDSAit = get_manageDSAit( op );
307
308         /*
309          * We could be serving multiple database backends.  Select the
310          * appropriate one, or send a referral to our "referral server"
311          * if we don't hold it.
312          */
313         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
314         if ( op->o_bd == NULL ) {
315                 rs->sr_ref = referral_rewrite( default_referral,
316                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
317                 if (!rs->sr_ref) rs->sr_ref = default_referral;
318
319                 if (rs->sr_ref != NULL ) {
320                         rs->sr_err = LDAP_REFERRAL;
321                         op->o_bd = frontendDB;
322                         send_ldap_result( op, rs );
323                         op->o_bd = NULL;
324
325                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
326                 } else {
327                         op->o_bd = frontendDB;
328                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
329                                 "no global superior knowledge" );
330                         op->o_bd = NULL;
331                 }
332                 goto cleanup;
333         }
334
335         /* If we've got a glued backend, check the real backend */
336         op_be = op->o_bd;
337         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
338                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
339         }
340
341         /* check restrictions */
342         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
343                 send_ldap_result( op, rs );
344                 goto cleanup;
345         }
346
347         /* check for referrals */
348         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
349                 goto cleanup;
350         }
351
352         rs->sr_err = slap_mods_obsolete_check( op, modlist,
353                 &rs->sr_text, textbuf, textlen );
354         if ( rs->sr_err != LDAP_SUCCESS ) {
355                 send_ldap_result( op, rs );
356                 goto cleanup;
357         }
358
359         /* check for modify/increment support */
360         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
361                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
362                         "modify/increment not supported in context" );
363         }
364
365         /*
366          * do the modify if 1 && (2 || 3)
367          * 1) there is a modify function implemented in this backend;
368          * 2) this backend is master for what it holds;
369          * 3) it's a replica and the dn supplied is the update_ndn.
370          */
371         if ( op->o_bd->be_modify ) {
372                 /* do the update here */
373                 int repl_user = be_isupdate( op );
374
375                 /* Multimaster slapd does not have to check for replicator dn
376                  * because it accepts each modify request
377                  */
378 #ifndef SLAPD_MULTIMASTER
379                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
380 #endif
381                 {
382                         int             update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
383                         slap_callback   cb = { NULL, slap_replog_cb, NULL, NULL };
384
385                         op->o_bd = op_be;
386
387                         if ( !update ) {
388                                 rs->sr_err = slap_mods_no_user_mod_check( op, modlist,
389                                         &rs->sr_text, textbuf, textlen );
390                                 if ( rs->sr_err != LDAP_SUCCESS ) {
391                                         send_ldap_result( op, rs );
392                                         goto cleanup;
393                                 }
394                         }
395
396                         op->orm_modlist = modlist;
397 #ifdef SLAPD_MULTIMASTER
398                         if ( !repl_user )
399 #endif
400                         {
401                                 /* but multimaster slapd logs only the ones 
402                                  * not from a replicator user */
403                                 cb.sc_next = op->o_callback;
404                                 op->o_callback = &cb;
405                         }
406                         op->o_bd->be_modify( op, rs );
407
408 #ifndef SLAPD_MULTIMASTER
409                 /* send a referral */
410                 } else {
411                         BerVarray defref = op->o_bd->be_update_refs
412                                 ? op->o_bd->be_update_refs : default_referral;
413                         if ( defref != NULL ) {
414                                 rs->sr_ref = referral_rewrite( defref,
415                                         NULL, &op->o_req_dn,
416                                         LDAP_SCOPE_DEFAULT );
417                                 if ( rs->sr_ref == NULL ) {
418                                         /* FIXME: must duplicate, because
419                                          * overlays may muck with it */
420                                         rs->sr_ref = defref;
421                                 }
422                                 rs->sr_err = LDAP_REFERRAL;
423                                 send_ldap_result( op, rs );
424                                 if ( rs->sr_ref != defref ) {
425                                         ber_bvarray_free( rs->sr_ref );
426                                 }
427
428                         } else {
429                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
430                                         "shadow context; no update referral" );
431                         }
432 #endif
433                 }
434         } else {
435                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
436                     "operation not supported within namingContext" );
437         }
438
439 cleanup:;
440         return rs->sr_err;
441 }
442
443 /*
444  * Obsolete constraint checking.
445  */
446 int
447 slap_mods_obsolete_check(
448         Operation *op,
449         Modifications *ml,
450         const char **text,
451         char *textbuf,
452         size_t textlen )
453 {
454         if( get_manageDIT( op ) ) return LDAP_SUCCESS;
455
456         for ( ; ml != NULL; ml = ml->sml_next ) {
457                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
458                         (( ml->sml_op != LDAP_MOD_REPLACE &&
459                                 ml->sml_op != LDAP_MOD_DELETE ) ||
460                                         ml->sml_values != NULL ))
461                 {
462                         /*
463                          * attribute is obsolete,
464                          * only allow replace/delete with no values
465                          */
466                         snprintf( textbuf, textlen,
467                                 "%s: attribute is obsolete",
468                                 ml->sml_type.bv_val );
469                         *text = textbuf;
470                         return LDAP_CONSTRAINT_VIOLATION;
471                 }
472         }
473
474         return LDAP_SUCCESS;
475 }
476
477 /*
478  * No-user-modification constraint checking.
479  */
480 int
481 slap_mods_no_user_mod_check(
482         Operation *op,
483         Modifications *ml,
484         const char **text,
485         char *textbuf,
486         size_t textlen )
487 {
488         for ( ; ml != NULL; ml = ml->sml_next ) {
489                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) continue;
490
491                 if ( get_manageDIT( op ) ) {
492                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
493                                 ml->sml_flags |= SLAP_MOD_MANAGING;
494                                 continue;
495                         }
496
497                         /* attribute not manageable */
498                         snprintf( textbuf, textlen,
499                                 "%s: no-user-modification attribute not manageable",
500                                 ml->sml_type.bv_val );
501
502                 } else {
503                         /* user modification disallowed */
504                         snprintf( textbuf, textlen,
505                                 "%s: no user modification allowed",
506                                 ml->sml_type.bv_val );
507                 }
508
509                 *text = textbuf;
510                 return LDAP_CONSTRAINT_VIOLATION;
511         }
512
513         return LDAP_SUCCESS;
514 }
515
516 int
517 slap_mods_no_repl_user_mod_check(
518         Operation *op,
519         Modifications *ml,
520         const char **text,
521         char *textbuf,
522         size_t textlen )
523 {
524         Modifications *mods;
525         Modifications *modp;
526
527         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
528                 assert( mods->sml_op == LDAP_MOD_ADD );
529
530                 /* check doesn't already appear */
531                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
532                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
533                                 snprintf( textbuf, textlen,
534                                         "attribute '%s' provided more than once",
535                                         mods->sml_desc->ad_cname.bv_val );
536                                 *text = textbuf;
537                                 return LDAP_TYPE_OR_VALUE_EXISTS;
538                         }
539                 }
540         }
541
542         return LDAP_SUCCESS;
543 }
544
545 /*
546  * Do basic attribute type checking and syntax validation.
547  */
548 int slap_mods_check(
549         Modifications *ml,
550         const char **text,
551         char *textbuf,
552         size_t textlen,
553         void *ctx )
554 {
555         int rc;
556
557         for( ; ml != NULL; ml = ml->sml_next ) {
558                 AttributeDescription *ad = NULL;
559
560                 /* convert to attribute description */
561                 if ( ml->sml_desc == NULL ) {
562                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
563                         if( rc != LDAP_SUCCESS ) {
564                                 snprintf( textbuf, textlen, "%s: %s",
565                                         ml->sml_type.bv_val, *text );
566                                 *text = textbuf;
567                                 return rc;
568                         }
569                 }
570
571                 ad = ml->sml_desc;
572
573                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
574                         && !slap_ad_is_binary( ad ))
575                 {
576                         /* attribute requires binary transfer */
577                         snprintf( textbuf, textlen,
578                                 "%s: requires ;binary transfer",
579                                 ml->sml_type.bv_val );
580                         *text = textbuf;
581                         return LDAP_UNDEFINED_TYPE;
582                 }
583
584                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
585                         && slap_ad_is_binary( ad ))
586                 {
587                         /* attribute does not require binary transfer */
588                         snprintf( textbuf, textlen,
589                                 "%s: disallows ;binary transfer",
590                                 ml->sml_type.bv_val );
591                         *text = textbuf;
592                         return LDAP_UNDEFINED_TYPE;
593                 }
594
595                 if( slap_ad_is_tag_range( ad )) {
596                         /* attribute requires binary transfer */
597                         snprintf( textbuf, textlen,
598                                 "%s: inappropriate use of tag range option",
599                                 ml->sml_type.bv_val );
600                         *text = textbuf;
601                         return LDAP_UNDEFINED_TYPE;
602                 }
603
604 #if 0
605                 if ( is_at_obsolete( ad->ad_type ) &&
606                         (( ml->sml_op != LDAP_MOD_REPLACE &&
607                                 ml->sml_op != LDAP_MOD_DELETE ) ||
608                                         ml->sml_values != NULL ))
609                 {
610                         /*
611                          * attribute is obsolete,
612                          * only allow replace/delete with no values
613                          */
614                         snprintf( textbuf, textlen,
615                                 "%s: attribute is obsolete",
616                                 ml->sml_type.bv_val );
617                         *text = textbuf;
618                         return LDAP_CONSTRAINT_VIOLATION;
619                 }
620 #endif
621
622                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
623 #ifdef SLAPD_REAL_SYNTAX
624                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
625 #endif
626                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
627                 {
628                         /*
629                          * attribute values must be INTEGER or REAL
630                          */
631                         snprintf( textbuf, textlen,
632                                 "%s: attribute syntax inappropriate for increment",
633                                 ml->sml_type.bv_val );
634                         *text = textbuf;
635                         return LDAP_CONSTRAINT_VIOLATION;
636                 }
637
638                 /*
639                  * check values
640                  */
641                 if( ml->sml_values != NULL ) {
642                         ber_len_t nvals;
643                         slap_syntax_validate_func *validate =
644                                 ad->ad_type->sat_syntax->ssyn_validate;
645                         slap_syntax_transform_func *pretty =
646                                 ad->ad_type->sat_syntax->ssyn_pretty;
647  
648                         if( !pretty && !validate ) {
649                                 *text = "no validator for syntax";
650                                 snprintf( textbuf, textlen,
651                                         "%s: no validator for syntax %s",
652                                         ml->sml_type.bv_val,
653                                         ad->ad_type->sat_syntax->ssyn_oid );
654                                 *text = textbuf;
655                                 return LDAP_INVALID_SYNTAX;
656                         }
657
658                         /*
659                          * check that each value is valid per syntax
660                          *      and pretty if appropriate
661                          */
662                         for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
663                                 struct berval pval;
664
665                                 if ( pretty ) {
666 #ifdef SLAP_ORDERED_PRETTYNORM
667                                         rc = ordered_value_pretty( ad,
668                                                 &ml->sml_values[nvals], &pval, ctx );
669 #else /* ! SLAP_ORDERED_PRETTYNORM */
670                                         rc = pretty( ad->ad_type->sat_syntax,
671                                                 &ml->sml_values[nvals], &pval, ctx );
672 #endif /* ! SLAP_ORDERED_PRETTYNORM */
673                                 } else {
674 #ifdef SLAP_ORDERED_PRETTYNORM
675                                         rc = ordered_value_validate( ad,
676                                                 &ml->sml_values[nvals] );
677 #else /* ! SLAP_ORDERED_PRETTYNORM */
678                                         rc = validate( ad->ad_type->sat_syntax,
679                                                 &ml->sml_values[nvals] );
680 #endif /* ! SLAP_ORDERED_PRETTYNORM */
681                                 }
682
683                                 if( rc != 0 ) {
684                                         snprintf( textbuf, textlen,
685                                                 "%s: value #%ld invalid per syntax",
686                                                 ml->sml_type.bv_val, (long) nvals );
687                                         *text = textbuf;
688                                         return LDAP_INVALID_SYNTAX;
689                                 }
690
691                                 if( pretty ) {
692                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
693                                         ml->sml_values[nvals] = pval;
694                                 }
695                         }
696
697                         /*
698                          * a rough single value check... an additional check is needed
699                          * to catch add of single value to existing single valued attribute
700                          */
701                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
702                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
703                         {
704                                 snprintf( textbuf, textlen,
705                                         "%s: multiple values provided",
706                                         ml->sml_type.bv_val );
707                                 *text = textbuf;
708                                 return LDAP_CONSTRAINT_VIOLATION;
709                         }
710
711                         /* if the type has a normalizer, generate the
712                          * normalized values. otherwise leave them NULL.
713                          *
714                          * this is different from the rule for attributes
715                          * in an entry - in an attribute list, the normalized
716                          * value is set equal to the non-normalized value
717                          * when there is no normalizer.
718                          */
719                         if( nvals && ad->ad_type->sat_equality &&
720                                 ad->ad_type->sat_equality->smr_normalize )
721                         {
722                                 ml->sml_nvalues = ber_memalloc_x(
723                                         (nvals+1)*sizeof(struct berval), ctx );
724
725                                 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
726 #ifdef SLAP_ORDERED_PRETTYNORM
727                                         rc = ordered_value_normalize(
728                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
729                                                 ad,
730                                                 ad->ad_type->sat_equality,
731                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
732 #else /* ! SLAP_ORDERED_PRETTYNORM */
733                                         rc = ad->ad_type->sat_equality->smr_normalize(
734                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
735                                                 ad->ad_type->sat_syntax,
736                                                 ad->ad_type->sat_equality,
737                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
738 #endif /* ! SLAP_ORDERED_PRETTYNORM */
739                                         if ( rc ) {
740                                                 Debug( LDAP_DEBUG_ANY,
741                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
742                                                         rc, 0, 0 );
743                                                 snprintf( textbuf, textlen,
744                                                         "%s: value #%ld normalization failed",
745                                                         ml->sml_type.bv_val, (long) nvals );
746                                                 *text = textbuf;
747                                                 return rc;
748                                         }
749                                 }
750
751                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
752                         }
753
754                         /* check for duplicates, but ignore Deletes.
755                          */
756                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
757                                 int             i, j, rc, match;
758                                 MatchingRule *mr = ad->ad_type->sat_equality;
759
760                                 for ( i = 1; i < nvals ; i++ ) {
761                                         /* test asserted values against themselves */
762                                         for( j = 0; j < i; j++ ) {
763                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
764                                                         SLAP_MR_EQUALITY
765                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
766                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
767                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
768                                                         ml->sml_nvalues
769                                                                 ? &ml->sml_nvalues[i]
770                                                                 : &ml->sml_values[i],
771                                                         ml->sml_nvalues
772                                                                 ? &ml->sml_nvalues[j]
773                                                                 : &ml->sml_values[j],
774                                                         text );
775                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
776                                                         /* value exists already */
777                                                         snprintf( textbuf, textlen,
778                                                                 "%s: value #%d provided more than once",
779                                                                 ml->sml_desc->ad_cname.bv_val, j );
780                                                         *text = textbuf;
781                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
782
783                                                 } else if ( rc != LDAP_SUCCESS ) {
784                                                         return rc;
785                                                 }
786                                         }
787                                 }
788                         }
789
790                 }
791         }
792
793         return LDAP_SUCCESS;
794 }
795
796 /* Enter with bv->bv_len = sizeof buffer, returns with
797  * actual length of string
798  */
799 void slap_timestamp( time_t *tm, struct berval *bv )
800 {
801         struct tm *ltm;
802 #ifdef HAVE_GMTIME_R
803         struct tm ltm_buf;
804
805         ltm = gmtime_r( tm, &ltm_buf );
806 #else
807         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
808         ltm = gmtime( tm );
809 #endif
810
811         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
812
813 #ifndef HAVE_GMTIME_R
814         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
815 #endif
816 }
817
818 /* modify only calls this for non-replicas. modrdn always calls.
819  */
820 void slap_mods_opattrs(
821         Operation *op,
822         Modifications *mods,
823         int manage_ctxcsn )
824 {
825         struct berval name, timestamp, csn = BER_BVNULL;
826         struct berval nname;
827         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
828         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
829         Modifications *mod, **modtail, *modlast;
830
831         if ( SLAP_LASTMOD( op->o_bd ) ) {
832                 char *ptr;
833                 timestamp.bv_val = timebuf;
834                 if ( BER_BVISEMPTY( &op->o_csn )) {
835                         csn.bv_val = csnbuf;
836                         csn.bv_len = sizeof( csnbuf );
837                         slap_get_csn( op, &csn, manage_ctxcsn );
838                 } else {
839                         csn = op->o_csn;
840                 }
841                 ptr = ber_bvchr( &csn, '#' );
842                 if ( ptr && ptr < &csn.bv_val[csn.bv_len] ) {
843                         timestamp.bv_len = ptr - csn.bv_val;
844                         if ( timestamp.bv_len >= sizeof( timebuf ))
845                                 timestamp.bv_len = sizeof( timebuf ) - 1;
846                         strncpy( timebuf, csn.bv_val, timestamp.bv_len );
847                         timebuf[timestamp.bv_len] = '\0';
848                 } else {
849                         time_t now = slap_get_time();
850
851                         timestamp.bv_len = sizeof(timebuf);
852
853                         slap_timestamp( &now, &timestamp );
854                 }
855
856                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
857                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
858                         nname = name;
859                 } else {
860                         name = op->o_dn;
861                         nname = op->o_ndn;
862                 }
863
864                 for ( mod = mods; mod->sml_next; mod = mod->sml_next )
865                         ;
866                 modtail = &mod->sml_next;
867
868                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
869                 mod->sml_op = LDAP_MOD_REPLACE;
870                 mod->sml_flags = SLAP_MOD_INTERNAL;
871                 mod->sml_next = NULL;
872                 BER_BVZERO( &mod->sml_type );
873                 mod->sml_desc = slap_schema.si_ad_entryCSN;
874                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
875                 ber_dupbv( &mod->sml_values[0], &csn );
876                 BER_BVZERO( &mod->sml_values[1] );
877                 assert( !BER_BVISNULL( &mod->sml_values[0] ) );
878                 mod->sml_nvalues = NULL;
879                 *modtail = mod;
880                 modlast = mod;
881                 modtail = &mod->sml_next;
882         
883                 if ( get_manageDIT( op ) ) {
884                         for ( mod = mods; mod != modlast; mod = mod->sml_next ) {
885                                 if ( mod->sml_desc == slap_schema.si_ad_modifiersName ) {
886                                         break;
887                                 }
888                         }
889                 }
890
891                 if ( mod->sml_desc != slap_schema.si_ad_modifiersName ) {
892                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
893                         mod->sml_op = LDAP_MOD_REPLACE;
894                         mod->sml_flags = SLAP_MOD_INTERNAL;
895                         mod->sml_next = NULL;
896                         BER_BVZERO( &mod->sml_type );
897                         mod->sml_desc = slap_schema.si_ad_modifiersName;
898                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
899                         ber_dupbv( &mod->sml_values[0], &name );
900                         BER_BVZERO( &mod->sml_values[1] );
901                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
902                         mod->sml_nvalues =
903                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
904                         ber_dupbv( &mod->sml_nvalues[0], &nname );
905                         BER_BVZERO( &mod->sml_nvalues[1] );
906                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
907                         *modtail = mod;
908                         modtail = &mod->sml_next;
909                 }
910
911                 if ( get_manageDIT( op ) ) {
912                         for ( mod = mods; mod != modlast; mod = mod->sml_next ) {
913                                 if ( mod->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
914                                         break;
915                                 }
916                         }
917                 }
918
919                 if ( mod->sml_desc != slap_schema.si_ad_modifyTimestamp ) {
920                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
921                         mod->sml_op = LDAP_MOD_REPLACE;
922                         mod->sml_flags = SLAP_MOD_INTERNAL;
923                         mod->sml_next = NULL;
924                         BER_BVZERO( &mod->sml_type );
925                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
926                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
927                         ber_dupbv( &mod->sml_values[0], &timestamp );
928                         BER_BVZERO( &mod->sml_values[1] );
929                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
930                         mod->sml_nvalues = NULL;
931                         *modtail = mod;
932                         modtail = &mod->sml_next;
933                 }
934         }
935 }
936