]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
9e866be5815aac1caaf298d553f61104dec18cc8
[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 )
116                     == LBER_ERROR )
117                 {
118                         send_ldap_discon( op, rs, LDAP_PROTOCOL_ERROR,
119                                 "decoding modlist error" );
120                         rs->sr_err = SLAPD_DISCONNECT;
121                         goto cleanup;
122                 }
123
124                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
125                 mod->sml_op = mop;
126                 mod->sml_type = tmp.sml_type;
127                 mod->sml_values = tmp.sml_values;
128                 mod->sml_nvalues = NULL;
129                 mod->sml_desc = NULL;
130                 mod->sml_next = NULL;
131                 *modtail = mod;
132
133                 switch( mop ) {
134                 case LDAP_MOD_ADD:
135                         if ( mod->sml_values == NULL ) {
136 #ifdef NEW_LOGGING
137                                 LDAP_LOG( OPERATION, ERR, 
138                                         "do_modify: modify/add operation (%ld) requires values\n",
139                                         (long)mop, 0, 0 );
140 #else
141                                 Debug( LDAP_DEBUG_ANY,
142                                         "do_modify: modify/add operation (%ld) requires values\n",
143                                         (long) mop, 0, 0 );
144 #endif
145
146                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
147                                         "modify/add operation requires values" );
148                                 goto cleanup;
149                         }
150
151                         /* fall through */
152
153                 case LDAP_MOD_DELETE:
154                 case LDAP_MOD_REPLACE:
155                         break;
156
157                 case LDAP_MOD_INCREMENT:
158                         if( op->o_protocol >= LDAP_VERSION3 ) {
159                                 increment++;
160                                 if ( mod->sml_values == NULL ) {
161 #ifdef NEW_LOGGING
162                                         LDAP_LOG( OPERATION, ERR, "do_modify: "
163                                                 "modify/increment operation (%ld) requires value\n",
164                                                 (long)mop, 0, 0 );
165 #else
166                                         Debug( LDAP_DEBUG_ANY, "do_modify: "
167                                                 "modify/increment operation (%ld) requires value\n",
168                                                 (long) mop, 0, 0 );
169 #endif
170
171                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
172                                                 "modify/increment operation requires value" );
173                                         goto cleanup;
174                                 }
175
176                                 if( mod->sml_values[1].bv_val ) {
177 #ifdef NEW_LOGGING
178                                         LDAP_LOG( OPERATION, ERR, "do_modify: modify/increment "
179                                                 "operation (%ld) requires single value\n",
180                                                 (long)mop, 0, 0 );
181 #else
182                                         Debug( LDAP_DEBUG_ANY, "do_modify: modify/increment "
183                                                 "operation (%ld) requires single value\n",
184                                                 (long) mop, 0, 0 );
185 #endif
186
187                                         send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
188                                                 "modify/increment operation requires single value" );
189                                         goto cleanup;
190                                 }
191
192                                 break;
193                         }
194                         /* fall thru */
195
196                 default: {
197 #ifdef NEW_LOGGING
198                                 LDAP_LOG( OPERATION, ERR, 
199                                         "do_modify: unrecognized modify operation (%ld)\n",
200                                         (long)mop, 0, 0 );
201 #else
202                                 Debug( LDAP_DEBUG_ANY,
203                                         "do_modify: unrecognized modify operation (%ld)\n",
204                                         (long) mop, 0, 0 );
205 #endif
206
207                                 send_ldap_error( op, rs, LDAP_PROTOCOL_ERROR,
208                                         "unrecognized modify operation" );
209                                 goto cleanup;
210                         }
211                 }
212
213                 modtail = &mod->sml_next;
214         }
215         *modtail = NULL;
216
217         if( get_ctrls( op, rs, 1 ) != LDAP_SUCCESS ) {
218 #ifdef NEW_LOGGING
219                 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
220 #else
221                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
222 #endif
223
224                 goto cleanup;
225         }
226
227         rs->sr_err = dnPrettyNormal( NULL, &dn, &op->o_req_dn, &op->o_req_ndn,
228                 op->o_tmpmemctx );
229         if( rs->sr_err != LDAP_SUCCESS ) {
230 #ifdef NEW_LOGGING
231                 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d  invalid dn (%s)\n",
232                         op->o_connid, dn.bv_val, 0 );
233 #else
234                 Debug( LDAP_DEBUG_ANY,
235                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
236 #endif
237                 send_ldap_error( op, rs, LDAP_INVALID_DN_SYNTAX, "invalid DN" );
238                 goto cleanup;
239         }
240
241         if( op->o_req_ndn.bv_len == 0 ) {
242 #ifdef NEW_LOGGING
243                 LDAP_LOG( OPERATION, ERR, 
244                         "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
245 #else
246                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
247 #endif
248
249                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
250                         "modify upon the root DSE not supported" );
251                 goto cleanup;
252
253         } else if ( bvmatch( &op->o_req_ndn, &global_schemandn ) ) {
254 #ifdef NEW_LOGGING
255                 LDAP_LOG( OPERATION, ERR,
256                         "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0  );
257 #else
258                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
259 #endif
260
261                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
262                         "modification of subschema subentry not supported" );
263                 goto cleanup;
264         }
265
266 #ifdef LDAP_DEBUG
267 #ifdef NEW_LOGGING
268         LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0  );
269 #else
270         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
271 #endif
272
273         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
274 #ifdef NEW_LOGGING
275                 LDAP_LOG( OPERATION, DETAIL1, "\t%s:  %s\n", 
276                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
277                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
278                                         (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
279                                                 "replace")), tmp->sml_type.bv_val, 0 );
280
281                 if ( tmp->sml_values == NULL ) {
282                         LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
283                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
284                         LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
285                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
286                         LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
287                 } else {
288                         LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
289                 }
290
291 #else
292                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
293                         tmp->sml_op == LDAP_MOD_ADD ? "add" :
294                                 (tmp->sml_op == LDAP_MOD_INCREMENT ? "increment" :
295                                 (tmp->sml_op == LDAP_MOD_DELETE ? "delete" :
296                                         "replace")), tmp->sml_type.bv_val, 0 );
297
298                 if ( tmp->sml_values == NULL ) {
299                         Debug( LDAP_DEBUG_ARGS, "%s\n",
300                            "\t\tno values", NULL, NULL );
301                 } else if ( tmp->sml_values[0].bv_val == NULL ) {
302                         Debug( LDAP_DEBUG_ARGS, "%s\n",
303                            "\t\tzero values", NULL, NULL );
304                 } else if ( tmp->sml_values[1].bv_val == NULL ) {
305                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
306                            "\t\tone value", (long) tmp->sml_values[0].bv_len, NULL );
307                 } else {
308                         Debug( LDAP_DEBUG_ARGS, "%s\n",
309                            "\t\tmultiple values", NULL, NULL );
310                 }
311 #endif
312         }
313
314         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
315                 char abuf[BUFSIZ/2], *ptr = abuf;
316                 int len = 0;
317
318                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
319                         op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
320
321                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
322                         if (len + 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, abuf, 0, 0 );
325
326                         len = 0;
327                                 ptr = abuf;
328
329                                 if( 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
330                                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
331                                                 op->o_connid, op->o_opid, tmp->sml_type.bv_val, 0, 0 );
332                                         continue;
333                                 }
334                         }
335                         if (len) {
336                                 *ptr++ = ' ';
337                                 len++;
338                         }
339                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
340                         len += tmp->sml_type.bv_len;
341                 }
342                 if (len) {
343                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
344                                 op->o_connid, op->o_opid, abuf, 0, 0 );
345                 }
346         }
347 #endif  /* LDAP_DEBUG */
348
349         manageDSAit = get_manageDSAit( op );
350
351         /*
352          * We could be serving multiple database backends.  Select the
353          * appropriate one, or send a referral to our "referral server"
354          * if we don't hold it.
355          */
356         op->o_bd = select_backend( &op->o_req_ndn, manageDSAit, 0 );
357         if ( op->o_bd == NULL ) {
358                 rs->sr_ref = referral_rewrite( default_referral,
359                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
360                 if (!rs->sr_ref) rs->sr_ref = default_referral;
361
362                 if (rs->sr_ref != NULL ) {
363                         rs->sr_err = LDAP_REFERRAL;
364                         send_ldap_result( op, rs );
365
366                         if (rs->sr_ref != default_referral) ber_bvarray_free( rs->sr_ref );
367                 } else {
368                         send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
369                                 "no global superior knowledge" );
370                 }
371                 goto cleanup;
372         }
373
374         /* check restrictions */
375         if( backend_check_restrictions( op, rs, NULL ) != LDAP_SUCCESS ) {
376                 send_ldap_result( op, rs );
377                 goto cleanup;
378         }
379
380         /* check for referrals */
381         if( backend_check_referrals( op, rs ) != LDAP_SUCCESS ) {
382                 goto cleanup;
383         }
384
385         /* check for modify/increment support */
386         if( increment && !SLAP_INCREMENT( op->o_bd ) ) {
387                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
388                         "modify/increment not supported in context" );
389         }
390
391 #if defined( LDAP_SLAPI )
392 #define pb      op->o_pb
393         if ( pb ) {
394                 slapi_int_pblock_set_operation( pb, op );
395                 slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
396                 slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
397                 modv = slapi_int_modifications2ldapmods( &modlist );
398                 slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
399
400                 rs->sr_err = slapi_int_call_plugins( op->o_bd,
401                         SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
402                 if ( rs->sr_err < 0 ) {
403                         /*
404                          * A preoperation plugin failure will abort the
405                          * entire operation.
406                          */
407 #ifdef NEW_LOGGING
408                         LDAP_LOG( OPERATION, INFO,
409                                 "do_modify: modify preoperation plugin failed\n",
410                                 0, 0, 0 );
411 #else
412                         Debug(LDAP_DEBUG_TRACE,
413                                 "do_modify: modify preoperation plugin failed.\n",
414                                 0, 0, 0);
415 #endif
416                         if ( ( slapi_pblock_get( op->o_pb, SLAPI_RESULT_CODE,
417                                 (void *)&rs->sr_err ) != 0 ) || rs->sr_err == LDAP_SUCCESS )
418                         {
419                                 rs->sr_err = LDAP_OTHER;
420                         }
421                         slapi_int_free_ldapmods( modv );
422                         modv = NULL;
423                         goto cleanup;
424                 }
425
426                 /*
427                  * It's possible that the preoperation plugin changed the
428                  * modification array, so we need to convert it back to
429                  * a Modification list.
430                  *
431                  * Calling slapi_int_modifications2ldapmods() destroyed modlist so
432                  * we don't need to free it.
433                  */
434                 slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
435                 modlist = slapi_int_ldapmods2modifications( modv );
436         }
437
438         /*
439          * NB: it is valid for the plugin to return no modifications
440          * (for example, a plugin might store some attributes elsewhere
441          * and remove them from the modification list; if only those
442          * attribute types were included in the modification request,
443          * then slapi_int_ldapmods2modifications() above will return
444          * NULL).
445          *
446          * However, the post-operation plugin should still be 
447          * called.
448          */
449         if ( modlist == NULL ) {
450                 rs->sr_err = LDAP_SUCCESS;
451                 send_ldap_result( op, rs );
452         } else {
453 #endif /* defined( LDAP_SLAPI ) */
454
455         /*
456          * do the modify if 1 && (2 || 3)
457          * 1) there is a modify function implemented in this backend;
458          * 2) this backend is master for what it holds;
459          * 3) it's a replica and the dn supplied is the update_ndn.
460          */
461         if ( op->o_bd->be_modify ) {
462                 /* do the update here */
463                 int repl_user = be_isupdate( op );
464
465                 /* Multimaster slapd does not have to check for replicator dn
466                  * because it accepts each modify request
467                  */
468 #ifndef SLAPD_MULTIMASTER
469                 if ( !SLAP_SHADOW(op->o_bd) || repl_user )
470 #endif
471                 {
472                         int update = op->o_bd->be_update_ndn.bv_len;
473                         char textbuf[SLAP_TEXT_BUFLEN];
474                         size_t textlen = sizeof textbuf;
475                         slap_callback cb = { NULL, slap_replog_cb, NULL, NULL };
476
477                         rs->sr_err = slap_mods_check( modlist, update, &rs->sr_text,
478                                 textbuf, textlen, NULL );
479
480                         if( rs->sr_err != LDAP_SUCCESS ) {
481                                 send_ldap_result( op, rs );
482                                 goto cleanup;
483                         }
484
485                         if ( !repl_user ) {
486                                 for( modtail = &modlist;
487                                         *modtail != NULL;
488                                         modtail = &(*modtail)->sml_next )
489                                 {
490                                         /* empty */
491                                 }
492
493                                 rs->sr_err = slap_mods_opattrs( op, modlist, modtail,
494                                         &rs->sr_text, textbuf, textlen );
495                                 if( rs->sr_err != LDAP_SUCCESS ) {
496                                         send_ldap_result( op, rs );
497                                         goto cleanup;
498                                 }
499                         }
500
501                         op->orm_modlist = modlist;
502 #ifdef SLAPD_MULTIMASTER
503                         if ( !repl_user )
504 #endif
505                         {
506                                 /* but we log only the ones not from a replicator user */
507                                 cb.sc_next = op->o_callback;
508                                 op->o_callback = &cb;
509                         }
510                         op->o_bd->be_modify( op, rs );
511
512 #ifndef SLAPD_MULTIMASTER
513                 /* send a referral */
514                 } else {
515                         BerVarray defref = op->o_bd->be_update_refs
516                                 ? op->o_bd->be_update_refs : default_referral;
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, LDAP_UNWILLING_TO_PERFORM,
529                                         "shadow context; no update referral" );
530                         }
531 #endif
532                 }
533         } else {
534                 send_ldap_error( op, rs, LDAP_UNWILLING_TO_PERFORM,
535                     "operation not supported within namingContext" );
536         }
537
538 #if defined( LDAP_SLAPI )
539         } /* modlist != NULL */
540
541         if ( pb != NULL && slapi_int_call_plugins( op->o_bd,
542                 SLAPI_PLUGIN_POST_MODIFY_FN, pb ) < 0 )
543         {
544 #ifdef NEW_LOGGING
545                 LDAP_LOG( OPERATION, INFO,
546                         "do_modify: modify postoperation plugins failed\n", 0, 0, 0 );
547 #else
548                 Debug(LDAP_DEBUG_TRACE,
549                         "do_modify: modify postoperation plugins failed.\n", 0, 0, 0);
550 #endif
551         }
552 #endif /* defined( LDAP_SLAPI ) */
553
554 cleanup:
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_int_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 #ifdef HAVE_GMTIME_R
797                 struct tm ltm_buf;
798 #endif
799                 time_t now = slap_get_time();
800
801 #ifdef HAVE_GMTIME_R
802                 ltm = gmtime_r( &now, &ltm_buf );
803 #else
804                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
805                 ltm = gmtime( &now );
806 #endif /* HAVE_GMTIME_R */
807                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
808
809                 slap_get_csn( op, csnbuf, sizeof(csnbuf), &csn, 1 );
810
811 #ifndef HAVE_GMTIME_R
812                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
813 #endif
814
815                 timestamp.bv_val = timebuf;
816                 timestamp.bv_len = strlen(timebuf);
817
818                 if( op->o_dn.bv_len == 0 ) {
819                         name.bv_val = SLAPD_ANONYMOUS;
820                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
821                         nname = name;
822                 } else {
823                         name = op->o_dn;
824                         nname = op->o_ndn;
825                 }
826         }
827
828         if( op->o_tag == LDAP_REQ_ADD ) {
829                 struct berval tmpval;
830
831                 if( global_schemacheck ) {
832                         int rc = mods_structural_class( mods, &tmpval,
833                                 text, textbuf, textlen );
834                         if( rc != LDAP_SUCCESS ) return rc;
835
836                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
837                         mod->sml_op = mop;
838                         mod->sml_type.bv_val = NULL;
839                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
840                         mod->sml_values =
841                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
842                         ber_dupbv( &mod->sml_values[0], &tmpval );
843                         mod->sml_values[1].bv_len = 0;
844                         mod->sml_values[1].bv_val = NULL;
845                         assert( mod->sml_values[0].bv_val );
846                         mod->sml_nvalues =
847                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
848                         ber_dupbv( &mod->sml_nvalues[0], &tmpval );
849                         mod->sml_nvalues[1].bv_len = 0;
850                         mod->sml_nvalues[1].bv_val = NULL;
851                         assert( mod->sml_nvalues[0].bv_val );
852                         *modtail = mod;
853                         modtail = &mod->sml_next;
854                 }
855
856                 if ( SLAP_LASTMOD( op->o_bd )) {
857                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
858
859                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
860                         tmpval.bv_val = uuidbuf;
861                 
862                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
863                         mod->sml_op = mop;
864                         mod->sml_type.bv_val = NULL;
865                         mod->sml_desc = slap_schema.si_ad_entryUUID;
866                         mod->sml_values =
867                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
868                         ber_dupbv( &mod->sml_values[0], &tmpval );
869                         mod->sml_values[1].bv_len = 0;
870                         mod->sml_values[1].bv_val = NULL;
871                         assert( mod->sml_values[0].bv_val );
872                         mod->sml_nvalues =
873                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
874                         (*mod->sml_desc->ad_type->sat_equality->smr_normalize)(
875                                         SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
876                                         mod->sml_desc->ad_type->sat_syntax,
877                                         mod->sml_desc->ad_type->sat_equality,
878                                         mod->sml_values, mod->sml_nvalues, NULL );
879                         mod->sml_nvalues[1].bv_len = 0;
880                         mod->sml_nvalues[1].bv_val = NULL;
881                         *modtail = mod;
882                         modtail = &mod->sml_next;
883
884                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
885                         mod->sml_op = mop;
886                         mod->sml_type.bv_val = NULL;
887                         mod->sml_desc = slap_schema.si_ad_creatorsName;
888                         mod->sml_values =
889                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
890                         ber_dupbv( &mod->sml_values[0], &name );
891                         mod->sml_values[1].bv_len = 0;
892                         mod->sml_values[1].bv_val = NULL;
893                         assert( mod->sml_values[0].bv_val );
894                         mod->sml_nvalues =
895                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
896                         ber_dupbv( &mod->sml_nvalues[0], &nname );
897                         mod->sml_nvalues[1].bv_len = 0;
898                         mod->sml_nvalues[1].bv_val = NULL;
899                         assert( mod->sml_nvalues[0].bv_val );
900                         *modtail = mod;
901                         modtail = &mod->sml_next;
902
903                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
904                         mod->sml_op = mop;
905                         mod->sml_type.bv_val = NULL;
906                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
907                         mod->sml_values =
908                                 (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
909                         ber_dupbv( &mod->sml_values[0], &timestamp );
910                         mod->sml_values[1].bv_len = 0;
911                         mod->sml_values[1].bv_val = NULL;
912                         assert( mod->sml_values[0].bv_val );
913                         mod->sml_nvalues = NULL;
914                         *modtail = mod;
915                         modtail = &mod->sml_next;
916                 }
917         }
918
919         if ( SLAP_LASTMOD( op->o_bd )) {
920                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
921                 mod->sml_op = mop;
922                 mod->sml_type.bv_val = NULL;
923                 mod->sml_desc = slap_schema.si_ad_entryCSN;
924                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
925                 ber_dupbv( &mod->sml_values[0], &csn );
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 = NULL;
930                 *modtail = mod;
931                 modtail = &mod->sml_next;
932
933                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
934                 mod->sml_op = mop;
935                 mod->sml_type.bv_val = NULL;
936                 mod->sml_desc = slap_schema.si_ad_modifiersName;
937                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
938                 ber_dupbv( &mod->sml_values[0], &name );
939                 mod->sml_values[1].bv_len = 0;
940                 mod->sml_values[1].bv_val = NULL;
941                 assert( mod->sml_values[0].bv_val );
942                 mod->sml_nvalues =
943                         (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
944                 ber_dupbv( &mod->sml_nvalues[0], &nname );
945                 mod->sml_nvalues[1].bv_len = 0;
946                 mod->sml_nvalues[1].bv_val = NULL;
947                 assert( mod->sml_nvalues[0].bv_val );
948                 *modtail = mod;
949                 modtail = &mod->sml_next;
950
951                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
952                 mod->sml_op = mop;
953                 mod->sml_type.bv_val = NULL;
954                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
955                 mod->sml_values = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
956                 ber_dupbv( &mod->sml_values[0], &timestamp );
957                 mod->sml_values[1].bv_len = 0;
958                 mod->sml_values[1].bv_val = NULL;
959                 assert( mod->sml_values[0].bv_val );
960                 mod->sml_nvalues = NULL;
961                 *modtail = mod;
962                 modtail = &mod->sml_next;
963         }
964
965         *modtail = NULL;
966         return LDAP_SUCCESS;
967 }
968