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