]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
4c28b687b2fb53b3b7e318d339d8138e4999ab18
[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 ( LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo ) &&
459                         ( !op->o_bd->be_update_ndn.bv_len || repl_user ))
460 #else
461                 if ( LDAP_STAILQ_EMPTY( &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 ( !LDAP_STAILQ_EMPTY( &op->o_bd->be_syncinfo )) {
507                                 syncinfo_t *si;
508                                 LDAP_STAILQ_FOREACH( si, &op->o_bd->be_syncinfo, si_next ) {
509                                         struct berval tmpbv;
510                                         ber_dupbv( &tmpbv, &si->si_provideruri_bv[0] );
511                                         ber_bvarray_add( &defref, &tmpbv );
512                                 }
513                         } else {
514                                 defref = op->o_bd->be_update_refs
515                                                 ? op->o_bd->be_update_refs : default_referral;
516                         }
517                         if ( defref != NULL ) {
518                                 rs->sr_ref = referral_rewrite( defref,
519                                         NULL, &op->o_req_dn,
520                                         LDAP_SCOPE_DEFAULT );
521                                 if (!rs->sr_ref) rs->sr_ref = defref;
522                                 rs->sr_err = LDAP_REFERRAL;
523                                 send_ldap_result( op, rs );
524                                 if (rs->sr_ref != defref) {
525                                         ber_bvarray_free( rs->sr_ref );
526                                 }
527                         } else {
528                                 send_ldap_error( op, rs,
529                                                 LDAP_UNWILLING_TO_PERFORM,
530                                                 "referral missing" );
531                         }
532 #endif
533                 }
534         } else {
535                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
536                     "operation not supported within namingContext" );
537         }
538
539 #if defined( LDAP_SLAPI )
540         } /* modlist != NULL */
541
542         if ( pb && doPluginFNs( op->o_bd, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 ) {
543 #ifdef NEW_LOGGING
544                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
545                                 "failed\n", 0, 0, 0 );
546 #else
547                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
548                                 "failed.\n", 0, 0, 0);
549 #endif
550         }
551 #endif /* defined( LDAP_SLAPI ) */
552
553 cleanup:
554
555         slap_graduate_commit_csn( op );
556
557         op->o_tmpfree( op->o_req_dn.bv_val, op->o_tmpmemctx );
558         op->o_tmpfree( op->o_req_ndn.bv_val, op->o_tmpmemctx );
559         if ( modlist != NULL ) slap_mods_free( modlist );
560 #if defined( LDAP_SLAPI )
561         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
562 #endif
563         return rs->sr_err;
564 }
565
566 /*
567  * Do basic attribute type checking and syntax validation.
568  */
569 int slap_mods_check(
570         Modifications *ml,
571         int update,
572         const char **text,
573         char *textbuf,
574         size_t textlen,
575         void *ctx )
576 {
577         int rc;
578
579         for( ; ml != NULL; ml = ml->sml_next ) {
580                 AttributeDescription *ad = NULL;
581
582                 /* convert to attribute description */
583                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
584
585                 if( rc != LDAP_SUCCESS ) {
586                         snprintf( textbuf, textlen, "%s: %s",
587                                 ml->sml_type.bv_val, *text );
588                         *text = textbuf;
589                         return rc;
590                 }
591
592                 ad = ml->sml_desc;
593
594                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
595                         && !slap_ad_is_binary( ad ))
596                 {
597                         /* attribute requires binary transfer */
598                         snprintf( textbuf, textlen,
599                                 "%s: requires ;binary transfer",
600                                 ml->sml_type.bv_val );
601                         *text = textbuf;
602                         return LDAP_UNDEFINED_TYPE;
603                 }
604
605                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
606                         && slap_ad_is_binary( ad ))
607                 {
608                         /* attribute requires binary transfer */
609                         snprintf( textbuf, textlen,
610                                 "%s: disallows ;binary transfer",
611                                 ml->sml_type.bv_val );
612                         *text = textbuf;
613                         return LDAP_UNDEFINED_TYPE;
614                 }
615
616                 if( slap_ad_is_tag_range( ad )) {
617                         /* attribute requires binary transfer */
618                         snprintf( textbuf, textlen,
619                                 "%s: inappropriate use of tag range option",
620                                 ml->sml_type.bv_val );
621                         *text = textbuf;
622                         return LDAP_UNDEFINED_TYPE;
623                 }
624
625                 if (!update && is_at_no_user_mod( ad->ad_type )) {
626                         /* user modification disallowed */
627                         snprintf( textbuf, textlen,
628                                 "%s: no user modification allowed",
629                                 ml->sml_type.bv_val );
630                         *text = textbuf;
631                         return LDAP_CONSTRAINT_VIOLATION;
632                 }
633
634                 if ( is_at_obsolete( ad->ad_type ) &&
635                         (( ml->sml_op != LDAP_MOD_REPLACE &&
636                                 ml->sml_op != LDAP_MOD_DELETE ) ||
637                                         ml->sml_values != NULL ))
638                 {
639                         /*
640                          * attribute is obsolete,
641                          * only allow replace/delete with no values
642                          */
643                         snprintf( textbuf, textlen,
644                                 "%s: attribute is obsolete",
645                                 ml->sml_type.bv_val );
646                         *text = textbuf;
647                         return LDAP_CONSTRAINT_VIOLATION;
648                 }
649
650                 if ( ml->sml_op == LDAP_MOD_INCREMENT &&
651 #ifdef SLAPD_REAL_SYNTAX
652                         !is_at_syntax( ad->ad_type, SLAPD_REAL_SYNTAX ) &&
653 #endif
654                         !is_at_syntax( ad->ad_type, SLAPD_INTEGER_SYNTAX ) )
655                 {
656                         /*
657                          * attribute values must be INTEGER or REAL
658                          */
659                         snprintf( textbuf, textlen,
660                                 "%s: attribute syntax inappropriate for increment",
661                                 ml->sml_type.bv_val );
662                         *text = textbuf;
663                         return LDAP_CONSTRAINT_VIOLATION;
664                 }
665
666                 /*
667                  * check values
668                  */
669                 if( ml->sml_values != NULL ) {
670                         ber_len_t nvals;
671                         slap_syntax_validate_func *validate =
672                                 ad->ad_type->sat_syntax->ssyn_validate;
673                         slap_syntax_transform_func *pretty =
674                                 ad->ad_type->sat_syntax->ssyn_pretty;
675  
676                         if( !pretty && !validate ) {
677                                 *text = "no validator for syntax";
678                                 snprintf( textbuf, textlen,
679                                         "%s: no validator for syntax %s",
680                                         ml->sml_type.bv_val,
681                                         ad->ad_type->sat_syntax->ssyn_oid );
682                                 *text = textbuf;
683                                 return LDAP_INVALID_SYNTAX;
684                         }
685
686                         /*
687                          * check that each value is valid per syntax
688                          *      and pretty if appropriate
689                          */
690                         for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
691                                 struct berval pval;
692                                 if( pretty ) {
693                                         rc = pretty( ad->ad_type->sat_syntax,
694                                                 &ml->sml_values[nvals], &pval, ctx );
695                                 } else {
696                                         rc = validate( ad->ad_type->sat_syntax,
697                                                 &ml->sml_values[nvals] );
698                                 }
699
700                                 if( rc != 0 ) {
701                                         snprintf( textbuf, textlen,
702                                                 "%s: value #%ld invalid per syntax",
703                                                 ml->sml_type.bv_val, (long) nvals );
704                                         *text = textbuf;
705                                         return LDAP_INVALID_SYNTAX;
706                                 }
707
708                                 if( pretty ) {
709                                         ber_memfree_x( ml->sml_values[nvals].bv_val, ctx );
710                                         ml->sml_values[nvals] = pval;
711                                 }
712                         }
713
714                         /*
715                          * a rough single value check... an additional check is needed
716                          * to catch add of single value to existing single valued attribute
717                          */
718                         if ((ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE)
719                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
720                         {
721                                 snprintf( textbuf, textlen,
722                                         "%s: multiple values provided",
723                                         ml->sml_type.bv_val );
724                                 *text = textbuf;
725                                 return LDAP_CONSTRAINT_VIOLATION;
726                         }
727
728                         /* if the type has a normalizer, generate the
729                          * normalized values. otherwise leave them NULL.
730                          *
731                          * this is different from the rule for attributes
732                          * in an entry - in an attribute list, the normalized
733                          * value is set equal to the non-normalized value
734                          * when there is no normalizer.
735                          */
736                         if( nvals && ad->ad_type->sat_equality &&
737                                 ad->ad_type->sat_equality->smr_normalize )
738                         {
739                                 ml->sml_nvalues = ber_memalloc_x(
740                                         (nvals+1)*sizeof(struct berval), ctx );
741
742                                 for( nvals = 0; ml->sml_values[nvals].bv_val; nvals++ ) {
743                                         rc = ad->ad_type->sat_equality->smr_normalize(
744                                                 SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
745                                                 ad->ad_type->sat_syntax,
746                                                 ad->ad_type->sat_equality,
747                                                 &ml->sml_values[nvals], &ml->sml_nvalues[nvals], ctx );
748                                         if( rc ) {
749 #ifdef NEW_LOGGING
750                                                 LDAP_LOG( OPERATION, DETAIL1,
751                                                         "str2entry:  NULL (ssyn_normalize %d)\n",
752                                                         rc, 0, 0 );
753 #else
754                                                 Debug( LDAP_DEBUG_ANY,
755                                                         "<= str2entry NULL (ssyn_normalize %d)\n",
756                                                         rc, 0, 0 );
757 #endif
758                                                 snprintf( textbuf, textlen,
759                                                         "%s: value #%ld normalization failed",
760                                                         ml->sml_type.bv_val, (long) nvals );
761                                                 *text = textbuf;
762                                                 return rc;
763                                         }
764                                 }
765
766                                 ml->sml_nvalues[nvals].bv_val = NULL;
767                                 ml->sml_nvalues[nvals].bv_len = 0;
768                         }
769                 }
770         }
771
772         return LDAP_SUCCESS;
773 }
774
775 int slap_mods_opattrs(
776         Operation *op,
777         Modifications *mods,
778         Modifications **modtail,
779         const char **text,
780         char *textbuf, size_t textlen )
781 {
782         struct berval name, timestamp, csn;
783         struct berval nname;
784         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
785         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
786         Modifications *mod;
787
788         int mop = op->o_tag == LDAP_REQ_ADD
789                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
790
791         assert( modtail != NULL );
792         assert( *modtail == NULL );
793
794         if ( SLAP_LASTMOD( op->o_bd )) {
795                 struct tm *ltm;
796                 time_t now = slap_get_time();
797
798                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
799                 ltm = gmtime( &now );
800                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
801
802                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
803
804                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
805
806                 timestamp.bv_val = timebuf;
807                 timestamp.bv_len = strlen(timebuf);
808
809                 if( op->o_dn.bv_len == 0 ) {
810                         name.bv_val = SLAPD_ANONYMOUS;
811                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
812                         nname = name;
813                 } else {
814                         name = op->o_dn;
815                         nname = op->o_ndn;
816                 }
817         }
818
819         if( op->o_tag == LDAP_REQ_ADD ) {
820                 struct berval tmpval;
821
822                 if( global_schemacheck ) {
823                         int rc = mods_structural_class( mods, &tmpval,
824                                 text, textbuf, textlen );
825                         if( rc != LDAP_SUCCESS ) return rc;
826
827                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
828                         mod->sml_op = mop;
829                         mod->sml_type.bv_val = NULL;
830                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
831                         mod->sml_values =
832                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
833                         ber_dupbv( &mod->sml_values[0], &tmpval );
834                         mod->sml_values[1].bv_len = 0;
835                         mod->sml_values[1].bv_val = NULL;
836                         assert( mod->sml_values[0].bv_val );
837                         mod->sml_nvalues =
838                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
839                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
840                         mod->sml_nvalues[1].bv_len = 0;
841                         mod->sml_nvalues[1].bv_val = NULL;
842                         assert( mod->sml_nvalues[0].bv_val );
843                         *modtail = mod;
844                         modtail = &mod->sml_next;
845                 }
846
847                 if ( SLAP_LASTMOD( op->o_bd )) {
848                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
849
850                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
851                         tmpval.bv_val = uuidbuf;
852                 
853                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
854                         mod->sml_op = mop;
855                         mod->sml_type.bv_val = NULL;
856                         mod->sml_desc = slap_schema.si_ad_entryUUID;
857                         mod->sml_values =
858                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
859                         ber_dupbv( &mod->sml_values[0], &tmpval );
860                         mod->sml_values[1].bv_len = 0;
861                         mod->sml_values[1].bv_val = NULL;
862                         assert( mod->sml_values[0].bv_val );
863                         mod->sml_nvalues =
864                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
865                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
866                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
867                                         mod->sml_desc->ad_type->sat_syntax,
868                                         mod->sml_desc->ad_type->sat_equality,
869                                         mod->sml_values, mod->sml_nvalues, NULL );
870                         mod->sml_nvalues[1].bv_len = 0;
871                         mod->sml_nvalues[1].bv_val = NULL;
872                         *modtail = mod;
873                         modtail = &mod->sml_next;
874
875                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
876                         mod->sml_op = mop;
877                         mod->sml_type.bv_val = NULL;
878                         mod->sml_desc = slap_schema.si_ad_creatorsName;
879                         mod->sml_values =
880                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
881                         ber_dupbv( &mod->sml_values[0], &name );
882                         mod->sml_values[1].bv_len = 0;
883                         mod->sml_values[1].bv_val = NULL;
884                         assert( mod->sml_values[0].bv_val );
885                         mod->sml_nvalues =
886                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
887                         ber_dupbv( &mod->sml_nvalues[0], &nname );
888                         mod->sml_nvalues[1].bv_len = 0;
889                         mod->sml_nvalues[1].bv_val = NULL;
890                         assert( mod->sml_nvalues[0].bv_val );
891                         *modtail = mod;
892                         modtail = &mod->sml_next;
893
894                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
895                         mod->sml_op = mop;
896                         mod->sml_type.bv_val = NULL;
897                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
898                         mod->sml_values =
899                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
900                         ber_dupbv( &mod->sml_values[0], &timestamp );
901                         mod->sml_values[1].bv_len = 0;
902                         mod->sml_values[1].bv_val = NULL;
903                         assert( mod->sml_values[0].bv_val );
904                         mod->sml_nvalues = NULL;
905                         *modtail = mod;
906                         modtail = &mod->sml_next;
907                 }
908         }
909
910         if ( SLAP_LASTMOD( op->o_bd )) {
911                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
912                 mod->sml_op = mop;
913                 mod->sml_type.bv_val = NULL;
914                 mod->sml_desc = slap_schema.si_ad_entryCSN;
915                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
916                 ber_dupbv( &mod->sml_values[0], &csn );
917                 mod->sml_values[1].bv_len = 0;
918                 mod->sml_values[1].bv_val = NULL;
919                 assert( mod->sml_values[0].bv_val );
920                 mod->sml_nvalues = NULL;
921                 *modtail = mod;
922                 modtail = &mod->sml_next;
923
924                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
925                 mod->sml_op = mop;
926                 mod->sml_type.bv_val = NULL;
927                 mod->sml_desc = slap_schema.si_ad_modifiersName;
928                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
929                 ber_dupbv( &mod->sml_values[0], &name );
930                 mod->sml_values[1].bv_len = 0;
931                 mod->sml_values[1].bv_val = NULL;
932                 assert( mod->sml_values[0].bv_val );
933                 mod->sml_nvalues =
934                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
935                 ber_dupbv( &mod->sml_nvalues[0], &nname );
936                 mod->sml_nvalues[1].bv_len = 0;
937                 mod->sml_nvalues[1].bv_val = NULL;
938                 assert( mod->sml_nvalues[0].bv_val );
939                 *modtail = mod;
940                 modtail = &mod->sml_next;
941
942                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
943                 mod->sml_op = mop;
944                 mod->sml_type.bv_val = NULL;
945                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
946                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
947                 ber_dupbv( &mod->sml_values[0], &timestamp );
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 = NULL;
952                 *modtail = mod;
953                 modtail = &mod->sml_next;
954         }
955
956         *modtail = NULL;
957         return LDAP_SUCCESS;
958 }
959