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