]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
minor cleanup
[openldap] / servers / slapd / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1998-2004 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                 if ( rs->sr_err < 0 ) {
402                         /*
403                          * A preoperation plugin failure will abort the
404                          * entire operation.
405                          */
406 #ifdef NEW_LOGGING
407                         LDAP_LOG( OPERATION, INFO,
408                                 "do_modify: modify preoperation plugin failed\n",
409                                 0, 0, 0 );
410 #else
411                         Debug(LDAP_DEBUG_TRACE,
412                                 "do_modify: modify preoperation plugin failed.\n",
413                                 0, 0, 0);
414 #endif
415                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
416                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
417                         {
418                                 rs->sr_err = LDAP_OTHER;
419                         }
420                         slapi_int_free_ldapmods( modv );
421                         modv = NULL;
422                         goto cleanup;
423                 }
424
425                 /*
426                  * It's possible that the preoperation plugin changed the
427                  * modification array, so we need to convert it back to
428                  * a Modification list.
429                  *
430                  * Calling slapi_int_modifications2ldapmods() destroyed modlist so
431                  * we don't need to free it.
432                  */
433                 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
434                 modlist = slapi_int_ldapmods2modifications( modv );
435         }
436
437         /*
438          * NB: it is valid for the plugin to return no modifications
439          * (for example, a plugin might store some attributes elsewhere
440          * and remove them from the modification list; if only those
441          * attribute types were included in the modification request,
442          * then slapi_int_ldapmods2modifications() above will return
443          * NULL).
444          *
445          * However, the post-operation plugin should still be 
446          * called.
447          */
448         if ( modlist == NULL ) {
449                 rs->sr_err = LDAP_SUCCESS;
450                 send_ldap_result( op, rs );
451         } else {
452 #endif /* defined( LDAP_SLAPI ) */
453
454         /*
455          * do the modify if 1 && (2 || 3)
456          * 1) there is a modify function implemented in this backend;
457          * 2) this backend is master for what it holds;
458          * 3) it's a replica and the dn supplied is the update_ndn.
459          */
460         if ( op->o_bd->be_modify ) {
461                 /* do the update here */
462                 int repl_user = be_isupdate( op );
463
464                 /* Multimaster slapd does not have to check for replicator dn
465                  * because it accepts each modify request
466                  */
467 #ifndef SLAPD_MULTIMASTER
468                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
469 #endif
470                 {
471                         int update = op->o_bd->be_update_ndn.bv_len;
472                         char textbuf[SLAP_TEXT_BUFLEN];
473                         size_t textlen = sizeof textbuf;
474                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
475
476                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
477                                 textbuf, textlen, NULL );
478
479                         if( rs->sr_err != LDAP_SUCCESS ) {
480                                 send_ldap_result( op, rs );
481                                 goto cleanup;
482                         }
483
484                         if ( !repl_user ) {
485                                 for( modtail = &modlist;
486                                         *modtail != NULL;
487                                         modtail = &(*modtail)->sml_next )
488                                 {
489                                         /* empty */
490                                 }
491
492                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
493                                         &rs->sr_text, textbuf, textlen );
494                                 if( rs->sr_err != LDAP_SUCCESS ) {
495                                         send_ldap_result( op, rs );
496                                         goto cleanup;
497                                 }
498                         }
499
500                         op->orm_modlist = modlist;
501 #ifdef SLAPD_MULTIMASTER
502                         if ( !repl_user )
503 #endif
504                         {
505                                 /* but we log only the ones not from a replicator user */
506                                 cb.sc_next = op->o_callback;
507                                 op->o_callback = &cb;
508                         }
509                         op->o_bd->be_modify( op, rs );
510
511 #ifndef SLAPD_MULTIMASTER
512                 /* send a referral */
513                 } else {
514                         BerVarray defref = op->o_bd->be_update_refs
515                                 ? op->o_bd->be_update_refs : default_referral;
516                         if ( defref != NULL ) {
517                                 rs->sr_ref = referral_rewrite( defref,
518                                         NULL, &op->o_req_dn,
519                                         LDAP_SCOPE_DEFAULT );
520                                 if (!rs->sr_ref) rs->sr_ref = defref;
521                                 rs->sr_err = LDAP_REFERRAL;
522                                 send_ldap_result( op, rs );
523                                 if (rs->sr_ref != defref) {
524                                         ber_bvarray_free( rs->sr_ref );
525                                 }
526                         } else {
527                                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
528                                         "shadow context; no update referral" );
529                         }
530 #endif
531                 }
532         } else {
533                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
534                     "operation not supported within namingContext" );
535         }
536
537 #if defined( LDAP_SLAPI )
538         } /* modlist != NULL */
539
540         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
541                 SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 )
542         {
543 #ifdef NEW_LOGGING
544                 LDAP_LOG( OPERATION, INFO,
545                         "do_modify: modify postoperation plugins failed\n", 0, 0, 0 );
546 #else
547                 Debug(LDAP_DEBUG_TRACE,
548                         "do_modify: modify postoperation plugins failed.\n", 0, 0, 0);
549 #endif
550         }
551 #endif /* defined( LDAP_SLAPI ) */
552
553 cleanup:
554         slap_graduate_commit_csn( op );
555
556         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
557         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
558         if ( modlist != NULL ) slap_mods_free( modlist );
559 #if defined( LDAP_SLAPI )
560         if ( modv != NULL ) slapi_int_free_ldapmods( modv );
561 #endif
562         return rs->sr_err;
563 }
564
565 /*
566  * Do basic attribute type checking and syntax validation.
567  */
568 int slap_mods_check(
569         Modifications *ml,
570         int update,
571         const char **text,
572         char *textbuf,
573         size_t textlen,
574         void *ctx )
575 {
576         int rc;
577
578         for( ; ml != NULL; ml = ml->sml_next ) {
579                 AttributeDescription *ad = NULL;
580
581                 /* convert to attribute description */
582                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
583
584                 if( rc != LDAP_SUCCESS ) {
585                         snprintf( textbuf, textlen, "%s: %s",
586                                 ml->sml_type.bv_val, *text );
587                         *text = textbuf;
588                         return rc;
589                 }
590
591                 ad = ml->sml_desc;
592
593                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
594                         && !slap_ad_is_binary( ad ))
595                 {
596                         /* attribute requires binary transfer */
597                         snprintf( textbuf, textlen,
598                                 "%s: requires ;binary transfer",
599                                 ml->sml_type.bv_val );
600                         *text = textbuf;
601                         return LDAP_UNDEFINED_TYPE;
602                 }
603
604                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
605                         && slap_ad_is_binary( ad ))
606                 {
607                         /* attribute does not require binary transfer */
608                         snprintf( textbuf, textlen,
609                                 "%s: disallows ;binary transfer",
610                                 ml->sml_type.bv_val );
611                         *text = textbuf;
612                         return LDAP_UNDEFINED_TYPE;
613                 }
614
615                 if( slap_ad_is_tag_range( ad )) {
616                         /* attribute requires binary transfer */
617                         snprintf( textbuf, textlen,
618                                 "%s: inappropriate use of tag range option",
619                                 ml->sml_type.bv_val );
620                         *text = textbuf;
621                         return LDAP_UNDEFINED_TYPE;
622                 }
623
624                 if (!update && is_at_no_user_mod( ad->ad_type )) {
625                         /* user modification disallowed */
626                         snprintf( textbuf, textlen,
627                                 "%s: no user modification allowed",
628                                 ml->sml_type.bv_val );
629                         *text = textbuf;
630                         return LDAP_CONSTRAINT_VIOLATION;
631                 }
632
633                 if ( is_at_obsolete( ad->ad_type ) &&
634                         (( ml->sml_op != LDAP_MOD_REPLACE &&
635                                 ml->sml_op != LDAP_MOD_DELETE ) ||
636                                         ml->sml_values != NULL ))
637                 {
638                         /*
639                          * attribute is obsolete,
640                          * only allow replace/delete with no values
641                          */
642                         snprintf( textbuf, textlen,
643                                 "%s: attribute is obsolete",
644                                 ml->sml_type.bv_val );
645                         *text = textbuf;
646                         return LDAP_CONSTRAINT_VIOLATION;
647                 }
648
649                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
650 #ifdef SLAPD_REAL_SYNTAX
651                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
652 #endif
653                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
654                 {
655                         /*
656                          * attribute values must be INTEGER or REAL
657                          */
658                         snprintf( textbuf, textlen,
659                                 "%s: attribute syntax inappropriate for increment",
660                                 ml->sml_type.bv_val );
661                         *text = textbuf;
662                         return LDAP_CONSTRAINT_VIOLATION;
663                 }
664
665                 /*
666                  * check values
667                  */
668                 if( ml->sml_values != NULL ) {
669                         ber_len_t nvals;
670                         slap_syntax_validate_func *validate =
671                                 ad->ad_type->sat_syntax->ssyn_validate;
672                         slap_syntax_transform_func *pretty =
673                                 ad->ad_type->sat_syntax->ssyn_pretty;
674  
675                         if( !pretty && !validate ) {
676                                 *text = "no validator for syntax";
677                                 snprintf( textbuf, textlen,
678                                         "%s: no validator for syntax %s",
679                                         ml->sml_type.bv_val,
680                                         ad->ad_type->sat_syntax->ssyn_oid );
681                                 *text = textbuf;
682                                 return LDAP_INVALID_SYNTAX;
683                         }
684
685                         /*
686                          * check that each value is valid per syntax
687                          *      and pretty if appropriate
688                          */
689                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
690                                 struct berval pval;
691                                 if( pretty ) {
692                                         rc = pretty( ad->ad_type->sat_syntax,
693                                                 &ml->sml_values[nvals], &pval, ctx );
694                                 } else {
695                                         rc = validate( ad->ad_type->sat_syntax,
696                                                 &ml->sml_values[nvals] );
697                                 }
698
699                                 if( rc != 0 ) {
700                                         snprintf( textbuf, textlen,
701                                                 "%s: value #%ld invalid per syntax",
702                                                 ml->sml_type.bv_val, (long) nvals );
703                                         *text = textbuf;
704                                         return LDAP_INVALID_SYNTAX;
705                                 }
706
707                                 if( pretty ) {
708                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
709                                         ml->sml_values[nvals] = pval;
710                                 }
711                         }
712
713                         /*
714                          * a rough single value check... an additional check is needed
715                          * to catch add of single value to existing single valued attribute
716                          */
717                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
718                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
719                         {
720                                 snprintf( textbuf, textlen,
721                                         "%s: multiple values provided",
722                                         ml->sml_type.bv_val );
723                                 *text = textbuf;
724                                 return LDAP_CONSTRAINT_VIOLATION;
725                         }
726
727                         /* if the type has a normalizer, generate the
728                          * normalized values. otherwise leave them NULL.
729                          *
730                          * this is different from the rule for attributes
731                          * in an entry - in an attribute list, the normalized
732                          * value is set equal to the non-normalized value
733                          * when there is no normalizer.
734                          */
735                         if( nvals && ad->ad_type->sat_equality &&
736                                 ad->ad_type->sat_equality->smr_normalize )
737                         {
738                                 ml->sml_nvalues = ber_memalloc_x(
739                                         (nvals+1)*sizeof(struct berval), ctx );
740
741                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
742                                         rc = ad->ad_type->sat_equality->smr_normalize(
743                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
744                                                 ad->ad_type->sat_syntax,
745                                                 ad->ad_type->sat_equality,
746                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
747                                         if( rc ) {
748 #ifdef NEW_LOGGING
749                                                 LDAP_LOG( OPERATION, DETAIL1,
750                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
751                                                         rc, 0, 0 );
752 #else
753                                                 Debug( LDAP_DEBUG_ANY,
754                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
755                                                         rc, 0, 0 );
756 #endif
757                                                 snprintf( textbuf, textlen,
758                                                         "%s: value #%ld normalization failed",
759                                                         ml->sml_type.bv_val, (long) nvals );
760                                                 *text = textbuf;
761                                                 return rc;
762                                         }
763                                 }
764
765                                 ml->sml_nvalues[nvals].bv_val = NULL;
766                                 ml->sml_nvalues[nvals].bv_len = 0;
767                         }
768
769                         if( nvals ) {
770                                 /* check for duplicates */
771                                 int             i, j;
772                                 MatchingRule *mr = ad->ad_type->sat_equality;
773
774                                 /* check if the values we're adding already exist */
775                                 if( mr == NULL || !mr->smr_match ) {
776                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
777                                                 /* test asserted values against themselves */
778                                                 for( j = 0; j < i; j++ ) {
779                                                         if ( bvmatch( &ml->sml_values[i],
780                                                                 &ml->sml_values[j] ) )
781                                                         {
782                                                                 /* value exists already */
783                                                                 snprintf( textbuf, textlen,
784                                                                         "%s: value #%d provided more than once",
785                                                                         ml->sml_desc->ad_cname.bv_val, j );
786                                                                 *text = textbuf;
787                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
788                                                         }
789                                                 }
790                                         }
791
792                                 } else {
793                                         int rc;
794                                         int match;
795
796                                         for ( i = 1; ml->sml_values[i].bv_val != NULL; i++ ) {
797                                                 /* test asserted values against themselves */
798                                                 for( j = 0; j < i; j++ ) {
799                                                         rc = value_match( &match, ml->sml_desc, mr,
800                                                                 SLAP_MR_EQUALITY
801                                                                         | SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX
802                                                                         | SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH
803                                                                         | SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH,
804                                                                 ml->sml_nvalues
805                                                                         ? &ml->sml_nvalues[i]
806                                                                         : &ml->sml_values[i],
807                                                                 ml->sml_nvalues
808                                                                         ? &ml->sml_nvalues[j]
809                                                                         : &ml->sml_values[j],
810                                                                 text );
811                                                         if ( rc == LDAP_SUCCESS && match == 0 ) {
812                                                                 /* value exists already */
813                                                                 snprintf( textbuf, textlen,
814                                                                         "%s: value #%d provided more than once",
815                                                                         ml->sml_desc->ad_cname.bv_val, j );
816                                                                 *text = textbuf;
817                                                                 return LDAP_TYPE_OR_VALUE_EXISTS;
818
819                                                         } else if ( rc != LDAP_SUCCESS ) {
820                                                                 return rc;
821                                                         }
822                                                 }
823                                         }
824                                 }
825                         }
826
827                 }
828         }
829
830         return LDAP_SUCCESS;
831 }
832
833 int slap_mods_opattrs(
834         Operation *op,
835         Modifications *mods,
836         Modifications **modtail,
837         const char **text,
838         char *textbuf, size_t textlen )
839 {
840         struct berval name, timestamp, csn;
841         struct berval nname;
842         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
843         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
844         Modifications *mod;
845
846         int mop = op->o_tag == LDAP_REQ_ADD
847                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
848
849         assert( modtail != NULL );
850         assert( *modtail == NULL );
851
852         if ( SLAP_LASTMOD( op->o_bd )) {
853                 struct tm *ltm;
854 #ifdef HAVE_GMTIME_R
855                 struct tm ltm_buf;
856 #endif
857                 time_t now = slap_get_time();
858
859 #ifdef HAVE_GMTIME_R
860                 ltm = gmtime_r( &now, &ltm_buf );
861 #else
862                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
863                 ltm = gmtime( &now );
864 #endif /* HAVE_GMTIME_R */
865                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
866
867                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
868
869 #ifndef HAVE_GMTIME_R
870                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
871 #endif
872
873                 timestamp.bv_val = timebuf;
874                 timestamp.bv_len = strlen(timebuf);
875
876                 if( op->o_dn.bv_len == 0 ) {
877                         BER_BVSTR( &name, SLAPD_ANONYMOUS );
878                         nname = name;
879                 } else {
880                         name = op->o_dn;
881                         nname = op->o_ndn;
882                 }
883         }
884
885         if( op->o_tag == LDAP_REQ_ADD ) {
886                 struct berval tmpval;
887
888                 if( global_schemacheck ) {
889                         int rc = mods_structural_class( mods, &tmpval,
890                                 text, textbuf, textlen );
891                         if( rc != LDAP_SUCCESS ) return rc;
892
893                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
894                         mod->sml_op = mop;
895                         mod->sml_type.bv_val = NULL;
896                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
897                         mod->sml_values =
898                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
899                         ber_dupbv( &mod->sml_values[0], &tmpval );
900                         mod->sml_values[1].bv_len = 0;
901                         mod->sml_values[1].bv_val = NULL;
902                         assert( mod->sml_values[0].bv_val );
903                         mod->sml_nvalues =
904                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
905                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
906                         mod->sml_nvalues[1].bv_len = 0;
907                         mod->sml_nvalues[1].bv_val = NULL;
908                         assert( mod->sml_nvalues[0].bv_val );
909                         *modtail = mod;
910                         modtail = &mod->sml_next;
911                 }
912
913                 if ( SLAP_LASTMOD( op->o_bd )) {
914                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
915
916                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
917                         tmpval.bv_val = uuidbuf;
918                 
919                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
920                         mod->sml_op = mop;
921                         mod->sml_type.bv_val = NULL;
922                         mod->sml_desc = slap_schema.si_ad_entryUUID;
923                         mod->sml_values =
924                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
925                         ber_dupbv( &mod->sml_values[0], &tmpval );
926                         mod->sml_values[1].bv_len = 0;
927                         mod->sml_values[1].bv_val = NULL;
928                         assert( mod->sml_values[0].bv_val );
929                         mod->sml_nvalues =
930                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
931                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
932                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
933                                         mod->sml_desc->ad_type->sat_syntax,
934                                         mod->sml_desc->ad_type->sat_equality,
935                                         mod->sml_values, mod->sml_nvalues, NULL );
936                         mod->sml_nvalues[1].bv_len = 0;
937                         mod->sml_nvalues[1].bv_val = NULL;
938                         *modtail = mod;
939                         modtail = &mod->sml_next;
940
941                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
942                         mod->sml_op = mop;
943                         mod->sml_type.bv_val = NULL;
944                         mod->sml_desc = slap_schema.si_ad_creatorsName;
945                         mod->sml_values =
946                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
947                         ber_dupbv( &mod->sml_values[0], &name );
948                         mod->sml_values[1].bv_len = 0;
949                         mod->sml_values[1].bv_val = NULL;
950                         assert( mod->sml_values[0].bv_val );
951                         mod->sml_nvalues =
952                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
953                         ber_dupbv( &mod->sml_nvalues[0], &nname );
954                         mod->sml_nvalues[1].bv_len = 0;
955                         mod->sml_nvalues[1].bv_val = NULL;
956                         assert( mod->sml_nvalues[0].bv_val );
957                         *modtail = mod;
958                         modtail = &mod->sml_next;
959
960                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
961                         mod->sml_op = mop;
962                         mod->sml_type.bv_val = NULL;
963                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
964                         mod->sml_values =
965                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
966                         ber_dupbv( &mod->sml_values[0], &timestamp );
967                         mod->sml_values[1].bv_len = 0;
968                         mod->sml_values[1].bv_val = NULL;
969                         assert( mod->sml_values[0].bv_val );
970                         mod->sml_nvalues = NULL;
971                         *modtail = mod;
972                         modtail = &mod->sml_next;
973                 }
974         }
975
976         if ( SLAP_LASTMOD( op->o_bd )) {
977                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
978                 mod->sml_op = mop;
979                 mod->sml_type.bv_val = NULL;
980                 mod->sml_desc = slap_schema.si_ad_entryCSN;
981                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
982                 ber_dupbv( &mod->sml_values[0], &csn );
983                 mod->sml_values[1].bv_len = 0;
984                 mod->sml_values[1].bv_val = NULL;
985                 assert( mod->sml_values[0].bv_val );
986                 mod->sml_nvalues = NULL;
987                 *modtail = mod;
988                 modtail = &mod->sml_next;
989
990                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
991                 mod->sml_op = mop;
992                 mod->sml_type.bv_val = NULL;
993                 mod->sml_desc = slap_schema.si_ad_modifiersName;
994                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
995                 ber_dupbv( &mod->sml_values[0], &name );
996                 mod->sml_values[1].bv_len = 0;
997                 mod->sml_values[1].bv_val = NULL;
998                 assert( mod->sml_values[0].bv_val );
999                 mod->sml_nvalues =
1000                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1001                 ber_dupbv( &mod->sml_nvalues[0], &nname );
1002                 mod->sml_nvalues[1].bv_len = 0;
1003                 mod->sml_nvalues[1].bv_val = NULL;
1004                 assert( mod->sml_nvalues[0].bv_val );
1005                 *modtail = mod;
1006                 modtail = &mod->sml_next;
1007
1008                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
1009                 mod->sml_op = mop;
1010                 mod->sml_type.bv_val = NULL;
1011                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
1012                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
1013                 ber_dupbv( &mod->sml_values[0], &timestamp );
1014                 mod->sml_values[1].bv_len = 0;
1015                 mod->sml_values[1].bv_val = NULL;
1016                 assert( mod->sml_values[0].bv_val );
1017                 mod->sml_nvalues = NULL;
1018                 *modtail = mod;
1019                 modtail = &mod->sml_next;
1020         }
1021
1022         *modtail = NULL;
1023         return LDAP_SUCCESS;
1024 }
1025