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