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