]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
happy new year
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2007 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( op, 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 #ifdef LDAP_X_TXN
202         if( rs->sr_err == LDAP_X_TXN_SPECIFY_OKAY ) {
203                 /* skip cleanup */
204                 return rs->sr_err;
205         }
206 #endif
207
208 cleanup:
209         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
210         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
211         if ( op->orm_modlist != NULL ) slap_mods_free( op->orm_modlist, 1 );
212
213         return rs->sr_err;
214 }
215
216 int
217 fe_op_modify( Operation *op, SlapReply *rs )
218 {
219 #ifdef LDAP_DEBUG
220         Modifications   *tmp;
221 #endif
222         int             manageDSAit;
223         BackendDB       *op_be, *bd = op->o_bd;
224         char            textbuf[ SLAP_TEXT_BUFLEN ];
225         size_t          textlen = sizeof( textbuf );
226         
227         if ( BER_BVISEMPTY( &op->o_req_ndn ) ) {
228                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
229
230                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
231                         "modify upon the root DSE not supported" );
232                 goto cleanup;
233
234         } else if ( bvmatch( &op->o_req_ndn, &frontendDB->be_schemandn ) ) {
235                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
236
237                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
238                         "modification of subschema subentry not supported" );
239                 goto cleanup;
240         }
241
242 #ifdef LDAP_DEBUG
243         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
244
245         for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
246                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
247                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
248                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
249                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
250                                         "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 ( BER_BVISNULL( &tmp->sml_values[ 0 ] ) ) {
256                         Debug( LDAP_DEBUG_ARGS, "%s\n",
257                            "\t\tzero values", NULL, NULL );
258                 } else if ( BER_BVISNULL( &tmp->sml_values[ 1 ] ) ) {
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         }
266
267         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
268                 char abuf[BUFSIZ/2], *ptr = abuf;
269                 int len = 0;
270
271                 Statslog( LDAP_DEBUG_STATS, "%s MOD dn=\"%s\"\n",
272                         op->o_log_prefix, op->o_req_dn.bv_val, 0, 0, 0 );
273
274                 for ( tmp = op->orm_modlist; tmp != NULL; tmp = tmp->sml_next ) {
275                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
276                                 Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
277                                     op->o_log_prefix, abuf, 0, 0, 0 );
278
279                                 len = 0;
280                                 ptr = abuf;
281
282                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
283                                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
284                                                 op->o_log_prefix, tmp->sml_type.bv_val, 0, 0, 0 );
285                                         continue;
286                                 }
287                         }
288                         if (len) {
289                                 *ptr++ = ' ';
290                                 len++;
291                         }
292                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
293                         len += tmp->sml_type.bv_len;
294                 }
295                 if (len) {
296                         Statslog( LDAP_DEBUG_STATS, "%s MOD attr=%s\n",
297                                 op->o_log_prefix, abuf, 0, 0, 0 );
298                 }
299         }
300 #endif  /* LDAP_DEBUG */
301
302         manageDSAit = get_manageDSAit( op );
303
304         /*
305          * We could be serving multiple database backends.  Select the
306          * appropriate one, or send a referral to our "referral server"
307          * if we don't hold it.
308          */
309         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 1 );
310         if ( op->o_bd == NULL ) {
311                 op->o_bd = bd;
312                 rs->sr_ref = referral_rewrite( default_referral,
313                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
314                 if ( !rs->sr_ref ) {
315                         rs->sr_ref = default_referral;
316                 }
317
318                 if ( rs->sr_ref != NULL ) {
319                         rs->sr_err = LDAP_REFERRAL;
320                         send_ldap_result( op, rs );
321
322                         if ( rs->sr_ref != default_referral ) {
323                                 ber_bvarray_free( rs->sr_ref );
324                         }
325
326                 } else {
327                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
328                                 "no global superior knowledge" );
329                 }
330                 goto cleanup;
331         }
332
333         /* If we've got a glued backend, check the real backend */
334         op_be = op->o_bd;
335         if ( SLAP_GLUE_INSTANCE( op->o_bd )) {
336                 op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
337         }
338
339         /* check restrictions */
340         if ( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
341                 send_ldap_result( op, rs );
342                 goto cleanup;
343         }
344
345         /* check for referrals */
346         if ( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
347                 goto cleanup;
348         }
349
350         rs->sr_err = slap_mods_obsolete_check( op, op->orm_modlist,
351                 &rs->sr_text, textbuf, textlen );
352         if ( rs->sr_err != LDAP_SUCCESS ) {
353                 send_ldap_result( op, rs );
354                 goto cleanup;
355         }
356
357         /* check for modify/increment support */
358         if ( op->orm_increment && !SLAP_INCREMENT( op->o_bd ) ) {
359                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
360                         "modify/increment not supported in context" );
361         }
362
363         /*
364          * do the modify if 1 && (2 || 3)
365          * 1) there is a modify function implemented in this backend;
366          * 2) this backend is master for what it holds;
367          * 3) it's a replica and the dn supplied is the update_ndn.
368          */
369         if ( op->o_bd->be_modify ) {
370                 /* do the update here */
371                 int repl_user = be_isupdate( op );
372
373                 /*
374                  * Multimaster slapd does not have to check for replicator dn
375                  * because it accepts each modify request
376                  */
377                 if ( !SLAP_SINGLE_SHADOW(op->o_bd) || repl_user ) {
378                         int update = !BER_BVISEMPTY( &op->o_bd->be_update_ndn );
379                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
380
381                         op->o_bd = op_be;
382
383                         if ( !update ) {
384                                 rs->sr_err = slap_mods_no_user_mod_check( op, op->orm_modlist,
385                                         &rs->sr_text, textbuf, textlen );
386                                 if ( rs->sr_err != LDAP_SUCCESS ) {
387                                         send_ldap_result( op, rs );
388                                         goto cleanup;
389                                 }
390                         }
391
392                         if ( !repl_user ) {
393                                 /* but multimaster slapd logs only the ones 
394                                  * not from a replicator user */
395                                 cb.sc_next = op->o_callback;
396                                 op->o_callback = &cb;
397                         }
398                         op->o_bd->be_modify( op, rs );
399
400                 } else { /* send a referral */
401                         BerVarray defref = op->o_bd->be_update_refs
402                                 ? op->o_bd->be_update_refs : default_referral;
403                         if ( defref != NULL ) {
404                                 rs->sr_ref = referral_rewrite( defref,
405                                         NULL, &op->o_req_dn,
406                                         LDAP_SCOPE_DEFAULT );
407                                 if ( rs->sr_ref == NULL ) {
408                                         /* FIXME: must duplicate, because
409                                          * overlays may muck with it */
410                                         rs->sr_ref = defref;
411                                 }
412                                 rs->sr_err = LDAP_REFERRAL;
413                                 send_ldap_result( op, rs );
414                                 if ( rs->sr_ref != defref ) {
415                                         ber_bvarray_free( rs->sr_ref );
416                                 }
417
418                         } else {
419                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
420                                         "shadow context; no update referral" );
421                         }
422                 }
423
424         } else {
425                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
426                     "operation not supported within namingContext" );
427         }
428
429 cleanup:;
430         op->o_bd = bd;
431         return rs->sr_err;
432 }
433
434 /*
435  * Obsolete constraint checking.
436  */
437 int
438 slap_mods_obsolete_check(
439         Operation *op,
440         Modifications *ml,
441         const char **text,
442         char *textbuf,
443         size_t textlen )
444 {
445         if( get_relax( op ) ) return LDAP_SUCCESS;
446
447         for ( ; ml != NULL; ml = ml->sml_next ) {
448                 if ( is_at_obsolete( ml->sml_desc->ad_type ) &&
449                         (( ml->sml_op != LDAP_MOD_REPLACE &&
450                                 ml->sml_op != LDAP_MOD_DELETE ) ||
451                                         ml->sml_values != NULL ))
452                 {
453                         /*
454                          * attribute is obsolete,
455                          * only allow replace/delete with no values
456                          */
457                         snprintf( textbuf, textlen,
458                                 "%s: attribute is obsolete",
459                                 ml->sml_type.bv_val );
460                         *text = textbuf;
461                         return LDAP_CONSTRAINT_VIOLATION;
462                 }
463         }
464
465         return LDAP_SUCCESS;
466 }
467
468 /*
469  * No-user-modification constraint checking.
470  */
471 int
472 slap_mods_no_user_mod_check(
473         Operation *op,
474         Modifications *ml,
475         const char **text,
476         char *textbuf,
477         size_t textlen )
478 {
479         for ( ; ml != NULL; ml = ml->sml_next ) {
480                 if ( !is_at_no_user_mod( ml->sml_desc->ad_type ) ) {
481                         continue;
482                 }
483
484                 if ( get_relax( op ) ) {
485                         if ( ml->sml_desc->ad_type->sat_flags & SLAP_AT_MANAGEABLE ) {
486                                 ml->sml_flags |= SLAP_MOD_MANAGING;
487                                 continue;
488                         }
489
490                         /* attribute not manageable */
491                         snprintf( textbuf, textlen,
492                                 "%s: no-user-modification attribute not manageable",
493                                 ml->sml_type.bv_val );
494
495                 } else {
496                         /* user modification disallowed */
497                         snprintf( textbuf, textlen,
498                                 "%s: no user modification allowed",
499                                 ml->sml_type.bv_val );
500                 }
501
502                 *text = textbuf;
503                 return LDAP_CONSTRAINT_VIOLATION;
504         }
505
506         return LDAP_SUCCESS;
507 }
508
509 int
510 slap_mods_no_repl_user_mod_check(
511         Operation *op,
512         Modifications *ml,
513         const char **text,
514         char *textbuf,
515         size_t textlen )
516 {
517         Modifications *mods;
518         Modifications *modp;
519
520         for ( mods = ml; mods != NULL; mods = mods->sml_next ) {
521                 assert( mods->sml_op == LDAP_MOD_ADD );
522
523                 /* check doesn't already appear */
524                 for ( modp = ml; modp != NULL; modp = modp->sml_next ) {
525                         if ( mods->sml_desc == modp->sml_desc && mods != modp ) {
526                                 snprintf( textbuf, textlen,
527                                         "attribute '%s' provided more than once",
528                                         mods->sml_desc->ad_cname.bv_val );
529                                 *text = textbuf;
530                                 return LDAP_TYPE_OR_VALUE_EXISTS;
531                         }
532                 }
533         }
534
535         return LDAP_SUCCESS;
536 }
537
538 /*
539  * Do basic attribute type checking and syntax validation.
540  */
541 int slap_mods_check(
542         Operation *op,
543         Modifications *ml,
544         const char **text,
545         char *textbuf,
546         size_t textlen,
547         void *ctx )
548 {
549         int rc;
550
551         for( ; ml != NULL; ml = ml->sml_next ) {
552                 AttributeDescription *ad = NULL;
553
554                 /* convert to attribute description */
555                 if ( ml->sml_desc == NULL ) {
556                         rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
557                         if( rc != LDAP_SUCCESS ) {
558                                 if ( get_no_schema_check( op )) {
559                                         rc = slap_bv2undef_ad( &ml->sml_type, &ml->sml_desc,
560                                                 text, 0 );
561                                 }
562                         }
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                                         rc = ordered_value_pretty( ad,
667                                                 &ml->sml_values[nvals], &pval, ctx );
668                                 } else {
669                                         rc = ordered_value_validate( ad,
670                                                 &ml->sml_values[nvals], ml->sml_op );
671                                 }
672
673                                 if( rc != 0 ) {
674                                         snprintf( textbuf, textlen,
675                                                 "%s: value #%ld invalid per syntax",
676                                                 ml->sml_type.bv_val, (long) nvals );
677                                         *text = textbuf;
678                                         return LDAP_INVALID_SYNTAX;
679                                 }
680
681                                 if( pretty ) {
682                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
683                                         ml->sml_values[nvals] = pval;
684                                 }
685                         }
686
687                         /*
688                          * a rough single value check... an additional check is needed
689                          * to catch add of single value to existing single valued attribute
690                          */
691                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
692                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
693                         {
694                                 snprintf( textbuf, textlen,
695                                         "%s: multiple values provided",
696                                         ml->sml_type.bv_val );
697                                 *text = textbuf;
698                                 return LDAP_CONSTRAINT_VIOLATION;
699                         }
700
701                         /* if the type has a normalizer, generate the
702                          * normalized values. otherwise leave them NULL.
703                          *
704                          * this is different from the rule for attributes
705                          * in an entry - in an attribute list, the normalized
706                          * value is set equal to the non-normalized value
707                          * when there is no normalizer.
708                          */
709                         if( nvals && ad->ad_type->sat_equality &&
710                                 ad->ad_type->sat_equality->smr_normalize )
711                         {
712                                 ml->sml_nvalues = ber_memalloc_x(
713                                         (nvals+1)*sizeof(struct berval), ctx );
714
715                                 for ( nvals = 0; !BER_BVISNULL( &ml->sml_values[nvals] ); nvals++ ) {
716                                         rc = ordered_value_normalize(
717                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
718                                                 ad,
719                                                 ad->ad_type->sat_equality,
720                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
721                                         if ( rc ) {
722                                                 Debug( LDAP_DEBUG_ANY,
723                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
724                                                         rc, 0, 0 );
725                                                 snprintf( textbuf, textlen,
726                                                         "%s: value #%ld normalization failed",
727                                                         ml->sml_type.bv_val, (long) nvals );
728                                                 *text = textbuf;
729                                                 return rc;
730                                         }
731                                 }
732
733                                 BER_BVZERO( &ml->sml_nvalues[nvals] );
734                         }
735
736                         /* check for duplicates, but ignore Deletes.
737                          */
738                         if( nvals > 1 && ml->sml_op != LDAP_MOD_DELETE ) {
739 #define SLAP_MODS_CHECK_QUICKSORT
740 #ifndef SLAP_MODS_CHECK_QUICKSORT
741                                 int             i, j, rc, match;
742                                 MatchingRule *mr = ad->ad_type->sat_equality;
743
744                                 for ( i = 1; i < nvals ; i++ ) {
745                                         /* test asserted values against themselves */
746                                         for( j = 0; j < i; j++ ) {
747                                                 rc = ordered_value_match( &match, ml->sml_desc, mr,
748                                                         SLAP_MR_EQUALITY
749                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
750                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
751                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
752                                                         ml->sml_nvalues
753                                                                 ? &ml->sml_nvalues[i]
754                                                                 : &ml->sml_values[i],
755                                                         ml->sml_nvalues
756                                                                 ? &ml->sml_nvalues[j]
757                                                                 : &ml->sml_values[j],
758                                                         text );
759                                                 if ( rc == LDAP_SUCCESS && match == 0 ) {
760                                                         /* value exists already */
761                                                         snprintf( textbuf, textlen,
762                                                                 "%s: value #%d provided more than once",
763                                                                 ml->sml_desc->ad_cname.bv_val, j );
764                                                         *text = textbuf;
765                                                         return LDAP_TYPE_OR_VALUE_EXISTS;
766
767                                                 } else if ( rc != LDAP_SUCCESS ) {
768                                                         return rc;
769                                                 }
770                                         }
771                                 }
772 #else   /* SLAP_MODS_CHECK_QUICKSORT */
773
774 /* Quicksort + Insertion sort for small arrays */
775
776 #define SMALL   8
777 #define SWAP(a,b,tmp)   tmp=(a);(a)=(b);(b)=tmp
778 #define COMP(a,b)       match=0; rc = ordered_value_match( &match, \
779                                                 ml->sml_desc, mr, SLAP_MR_EQUALITY \
780                                                                 | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX \
781                                                                 | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH \
782                                                                 | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH, \
783                                                                 &(a), &(b), text );
784
785                                         MatchingRule *mr = ad->ad_type->sat_equality;
786                                         int istack[sizeof(int)*16];
787                                         int i,j,k,l,ir,jstack, rc, match, *ix, itmp;
788                                         struct berval a, *cv;
789
790 /* If PRESERVE_ORDER is defined only the index array is sorted; the
791  * actual values are left in their incoming order. Otherwise, the
792  * only reason to keep the index array is to identify the offending
793  * value when duplicates are found.
794  */
795 #define PRESERVE_ORDER
796 #ifndef PRESERVE_ORDER
797                                         struct berval va, *v, *nv, bvtmp;
798
799 #define IX(x)   x
800 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp); SWAP(cv[x],cv[y],bvtmp); \
801         if (nv) {SWAP(v[x],v[y],bvtmp);}
802 #define SETA(x) itmp = ix[x]; a = cv[x]; if (nv) va=v[x]
803 #define GETA(x) ix[x] = itmp; cv[x] = a; if (nv) v[x]=va
804 #define SET(x,y)        ix[x] = ix[y]; cv[x] = cv[y]; if (nv) v[x]=v[y]
805
806                                         v = ml->sml_values;
807                                         nv = ml->sml_nvalues;
808
809 #else   /* PRESERVE_ORDER */
810
811 #define IX(x)   ix[x]
812 #define EXCH(x,y)       SWAP(ix[x],ix[y],itmp)
813 #define SETA(x) itmp = ix[x]; a = cv[itmp]
814 #define GETA(x) ix[x] = itmp;
815 #define SET(x,y)        ix[x] = ix[y]
816
817 #endif  /* PRESERVE_ORDER */
818
819                                         cv = ml->sml_nvalues ? ml->sml_nvalues : ml->sml_values;
820                                         if ( ad == slap_schema.si_ad_objectClass )
821                                                 mr = NULL;      /* shortcut matching */
822
823                                         /* record indices to preserve input ordering */
824                                         ix = slap_sl_malloc( nvals * sizeof(int), ctx );
825                                         for (i=0; i<nvals; i++) ix[i] = i;
826
827                                         ir = nvals-1;
828                                         l = 0;
829                                         jstack = 0;
830
831                                         for(;;) {
832                                                 if (ir - l < SMALL) {   /* Insertion sort */
833                                                         match=1;
834                                                         for (j=l+1;j<=ir;j++) {
835                                                                 SETA(j);
836                                                                 for (i=j-1;i>=0;i--) {
837                                                                         COMP(cv[IX(i)], a);
838                                                                         if ( match <= 0 )
839                                                                                 break;
840                                                                         SET(i+1,i);
841                                                                 }
842                                                                 GETA(i+1);
843                                                                 if ( match == 0 ) goto done;
844                                                         }
845                                                         if ( jstack == 0 ) break;
846                                                         if ( match == 0 ) break;
847                                                         ir = istack[jstack--];
848                                                         l = istack[jstack--];
849                                                 } else {
850                                                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
851                                                         EXCH(k, l+1);
852                                                         COMP( cv[IX(l)], cv[IX(ir)] );
853                                                         if ( match > 0 ) {
854                                                                 EXCH(l, ir);
855                                                         } else if ( match == 0 ) {
856                                                                 i = ir;
857                                                                 break;
858                                                         }
859                                                         COMP( cv[IX(l+1)], cv[IX(ir)] );
860                                                         if ( match > 0 ) {
861                                                                 EXCH(l+1, ir);
862                                                         } else if ( match == 0 ) {
863                                                                 i = ir;
864                                                                 break;
865                                                         }
866                                                         COMP( cv[IX(l)], cv[IX(l+1)] );
867                                                         if ( match > 0 ) {
868                                                                 EXCH(l, l+1);
869                                                         } else if ( match == 0 ) {
870                                                                 i = l;
871                                                                 break;
872                                                         }
873                                                         i = l+1;
874                                                         j = ir;
875                                                         a = cv[IX(i)];
876                                                         for(;;) {
877                                                                 do {
878                                                                         i++;
879                                                                         COMP( cv[IX(i)], a );
880                                                                 } while( match < 0 );
881                                                                 while( match > 0 ) {
882                                                                         j--;
883                                                                         COMP( cv[IX(j)], a );
884                                                                 }
885                                                                 if (j < i) {
886                                                                         match = 1;
887                                                                         break;
888                                                                 }
889                                                                 if ( match == 0 ) {
890                                                                         i = l+1;
891                                                                         break;
892                                                                 }
893                                                                 EXCH(i,j);
894                                                         }
895                                                         if ( match == 0 )
896                                                                 break;
897                                                         EXCH(l+1,j);
898                                                         jstack += 2;
899                                                         if (ir-i+1 >= j) {
900                                                                 istack[jstack] = ir;
901                                                                 istack[jstack-1] = i;
902                                                                 ir = j;
903                                                         } else {
904                                                                 istack[jstack] = j;
905                                                                 istack[jstack-1] = l;
906                                                                 l = i;
907                                                 }
908                                         }
909                                 }
910 done:
911                                 if ( i >= 0 )
912                                         j = ix[i];
913
914                                 slap_sl_free( ix, ctx );
915
916                                 if ( rc != LDAP_SUCCESS ) {
917                                         return rc;
918                                 } else if ( match == 0 ) {
919                                         /* value exists already */
920                                         assert( i >= 0 );
921                                         assert( i < nvals );
922                                         snprintf( textbuf, textlen,
923                                                 "%s: value #%d provided more than once",
924                                                 ml->sml_desc->ad_cname.bv_val, j );
925                                         *text = textbuf;
926                                         return LDAP_TYPE_OR_VALUE_EXISTS;
927                                 }
928 #endif  /* SLAP_MODS_CHECK_QUICKSORT */
929                         }
930                 }
931         }
932
933         return LDAP_SUCCESS;
934 }
935
936 /* Enter with bv->bv_len = sizeof buffer, returns with
937  * actual length of string
938  */
939 void slap_timestamp( time_t *tm, struct berval *bv )
940 {
941         struct tm *ltm;
942 #ifdef HAVE_GMTIME_R
943         struct tm ltm_buf;
944
945         ltm = gmtime_r( tm, &ltm_buf );
946 #else
947         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
948         ltm = gmtime( tm );
949 #endif
950
951         bv->bv_len = lutil_gentime( bv->bv_val, bv->bv_len, ltm );
952
953 #ifndef HAVE_GMTIME_R
954         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
955 #endif
956 }
957
958 /* Called for all modify and modrdn ops. If the current op was replicated
959  * from elsewhere, all of the attrs should already be present.
960  */
961 void slap_mods_opattrs(
962         Operation *op,
963         Modifications **modsp,
964         int manage_ctxcsn )
965 {
966         struct berval name, timestamp, csn = BER_BVNULL;
967         struct berval nname;
968         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
969         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
970         Modifications *mod, **modtail, *modlast;
971         int gotcsn = 0, gotmname = 0, gotmtime = 0;
972
973         if ( SLAP_LASTMOD( op->o_bd ) ) {
974                 char *ptr;
975                 timestamp.bv_val = timebuf;
976                 for ( modtail = modsp; *modtail; modtail = &(*modtail)->sml_next ) {
977                         if ( (*modtail)->sml_op != LDAP_MOD_ADD &&
978                                 (*modtail)->sml_op != LDAP_MOD_REPLACE ) continue;
979                         if ( (*modtail)->sml_desc == slap_schema.si_ad_entryCSN ) {
980                                 csn = (*modtail)->sml_values[0];
981                                 gotcsn = 1;
982                         } else
983                         if ( (*modtail)->sml_desc == slap_schema.si_ad_modifiersName ) {
984                                 gotmname = 1;
985                         } else
986                         if ( (*modtail)->sml_desc == slap_schema.si_ad_modifyTimestamp ) {
987                                 gotmtime = 1;
988                         }
989                 }
990                 if ( BER_BVISEMPTY( &op->o_csn )) {
991                         if ( !gotcsn ) {
992                                 csn.bv_val = csnbuf;
993                                 csn.bv_len = sizeof( csnbuf );
994                                 slap_get_csn( op, &csn, manage_ctxcsn );
995                         } else {
996                                 if ( manage_ctxcsn )
997                                         slap_queue_csn( op, &csn );
998                         }
999                 } else {
1000                         csn = op->o_csn;
1001                 }
1002                 ptr = ber_bvchr( &csn, '#' );
1003                 if ( ptr ) {
1004                         timestamp.bv_len = ptr - csn.bv_val;
1005                         if ( timestamp.bv_len >= sizeof( timebuf ))     /* ?!? */
1006                                 timestamp.bv_len = sizeof( timebuf ) - 1;
1007                         AC_MEMCPY( timebuf, csn.bv_val, timestamp.bv_len );
1008                         timebuf[timestamp.bv_len] = '\0';
1009                 } else {
1010                         time_t now = slap_get_time();
1011
1012                         timestamp.bv_len = sizeof(timebuf);
1013
1014                         slap_timestamp( &now, &timestamp );
1015                 }
1016
1017                 if ( BER_BVISEMPTY( &op->o_dn ) ) {
1018                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
1019                         nname = name;
1020                 } else {
1021                         name = op->o_dn;
1022                         nname = op->o_ndn;
1023                 }
1024
1025                 if ( !gotcsn ) {
1026                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1027                         mod->sml_op = LDAP_MOD_REPLACE;
1028                         mod->sml_flags = SLAP_MOD_INTERNAL;
1029                         mod->sml_next = NULL;
1030                         BER_BVZERO( &mod->sml_type );
1031                         mod->sml_desc = slap_schema.si_ad_entryCSN;
1032                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1033                         ber_dupbv( &mod->sml_values[0], &csn );
1034                         BER_BVZERO( &mod->sml_values[1] );
1035                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1036                         mod->sml_nvalues = NULL;
1037                         *modtail = mod;
1038                         modlast = mod;
1039                         modtail = &mod->sml_next;
1040                 }
1041
1042                 if ( !gotmname ) {
1043                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1044                         mod->sml_op = LDAP_MOD_REPLACE;
1045                         mod->sml_flags = SLAP_MOD_INTERNAL;
1046                         mod->sml_next = NULL;
1047                         BER_BVZERO( &mod->sml_type );
1048                         mod->sml_desc = slap_schema.si_ad_modifiersName;
1049                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1050                         ber_dupbv( &mod->sml_values[0], &name );
1051                         BER_BVZERO( &mod->sml_values[1] );
1052                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1053                         mod->sml_nvalues =
1054                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1055                         ber_dupbv( &mod->sml_nvalues[0], &nname );
1056                         BER_BVZERO( &mod->sml_nvalues[1] );
1057                         assert( !BER_BVISNULL( &mod->sml_nvalues[0] ) );
1058                         *modtail = mod;
1059                         modtail = &mod->sml_next;
1060                 }
1061
1062                 if ( !gotmtime ) {
1063                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1064                         mod->sml_op = LDAP_MOD_REPLACE;
1065                         mod->sml_flags = SLAP_MOD_INTERNAL;
1066                         mod->sml_next = NULL;
1067                         BER_BVZERO( &mod->sml_type );
1068                         mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1069                         mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1070                         ber_dupbv( &mod->sml_values[0], &timestamp );
1071                         BER_BVZERO( &mod->sml_values[1] );
1072                         assert( !BER_BVISNULL( &mod->sml_values[0] ) );
1073                         mod->sml_nvalues = NULL;
1074                         *modtail = mod;
1075                         modtail = &mod->sml_next;
1076                 }
1077         }
1078 }
1079