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