]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
44c9da2be8d937290961a30fc67c8f11d4fdf852
[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 "lutil.h"
27
28 #include "ldap_pvt.h"
29 #include "slap.h"
30 #ifdef LDAP_SLAPI
31 #include "slapi.h"
32 #endif
33
34 int
35 do_modify(
36     Connection  *conn,
37     Operation   *op )
38 {
39         struct berval dn = { 0, NULL };
40         struct berval pdn = { 0, NULL };
41         struct berval ndn = { 0, NULL };
42         char            *last;
43         ber_tag_t       tag;
44         ber_len_t       len;
45         Modifications   *modlist = NULL;
46         Modifications   **modtail = &modlist;
47 #ifdef LDAP_DEBUG
48         Modifications *tmp;
49 #endif
50 #ifdef LDAP_SLAPI
51         LDAPMod         **modv = NULL;
52         Slapi_PBlock *pb = op->o_pb;
53 #endif
54         Backend         *be;
55         int rc;
56         const char      *text;
57         int manageDSAit;
58
59 #ifdef NEW_LOGGING
60         LDAP_LOG( OPERATION, ENTRY, "do_modify: enter\n", 0, 0, 0 );
61 #else
62         Debug( LDAP_DEBUG_TRACE, "do_modify\n", 0, 0, 0 );
63 #endif
64
65         /*
66          * Parse the modify request.  It looks like this:
67          *
68          *      ModifyRequest := [APPLICATION 6] SEQUENCE {
69          *              name    DistinguishedName,
70          *              mods    SEQUENCE OF SEQUENCE {
71          *                      operation       ENUMERATED {
72          *                              add     (0),
73          *                              delete  (1),
74          *                              replace (2)
75          *                      },
76          *                      modification    SEQUENCE {
77          *                              type    AttributeType,
78          *                              values  SET OF AttributeValue
79          *                      }
80          *              }
81          *      }
82          */
83
84         if ( ber_scanf( op->o_ber, "{m" /*}*/, &dn ) == LBER_ERROR ) {
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( OPERATION, ERR, "do_modify: ber_scanf failed\n", 0, 0, 0 );
87 #else
88                 Debug( LDAP_DEBUG_ANY, "do_modify: ber_scanf failed\n", 0, 0, 0 );
89 #endif
90
91                 send_ldap_disconnect( conn, op,
92                         LDAP_PROTOCOL_ERROR, "decoding error" );
93                 return SLAPD_DISCONNECT;
94         }
95
96 #ifdef NEW_LOGGING
97         LDAP_LOG( OPERATION, ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
98 #else
99         Debug( LDAP_DEBUG_ARGS, "do_modify: dn (%s)\n", dn.bv_val, 0, 0 );
100 #endif
101
102
103         /* collect modifications & save for later */
104
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
113                 if ( ber_scanf( op->o_ber, "{i{m[W]}}", &mop,
114                     &tmp.sml_type, &tmp.sml_bvalues )
115                     == LBER_ERROR )
116                 {
117                         send_ldap_disconnect( conn, op,
118                                 LDAP_PROTOCOL_ERROR, "decoding modlist error" );
119                         rc = SLAPD_DISCONNECT;
120                         goto cleanup;
121                 }
122
123                 mod = (Modifications *) ch_malloc( sizeof(Modifications) );
124                 mod->sml_op = mop;
125                 mod->sml_type = tmp.sml_type;
126                 mod->sml_bvalues = tmp.sml_bvalues;
127                 mod->sml_desc = NULL;
128                 mod->sml_next = NULL;
129                 *modtail = mod;
130
131                 switch( mop ) {
132                 case LDAP_MOD_ADD:
133                         if ( mod->sml_bvalues == NULL ) {
134 #ifdef NEW_LOGGING
135                                 LDAP_LOG( OPERATION, ERR, 
136                                         "do_modify: modify/add operation (%ld) requires values\n",
137                                         (long)mop, 0, 0 );
138 #else
139                                 Debug( LDAP_DEBUG_ANY,
140                                         "do_modify: modify/add operation (%ld) requires values\n",
141                                         (long) mop, 0, 0 );
142 #endif
143
144                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
145                                         NULL, "modify/add operation requires values",
146                                         NULL, NULL );
147                                 rc = LDAP_PROTOCOL_ERROR;
148                                 goto cleanup;
149                         }
150
151                         /* fall through */
152
153                 case LDAP_MOD_DELETE:
154                 case LDAP_MOD_REPLACE:
155                         break;
156
157                 default: {
158 #ifdef NEW_LOGGING
159                                 LDAP_LOG( OPERATION, ERR, 
160                                         "do_modify: invalid modify operation (%ld)\n", (long)mop, 0, 0 );
161 #else
162                                 Debug( LDAP_DEBUG_ANY,
163                                         "do_modify: invalid modify operation (%ld)\n",
164                                         (long) mop, 0, 0 );
165 #endif
166
167                                 send_ldap_result( conn, op, LDAP_PROTOCOL_ERROR,
168                                         NULL, "unrecognized modify operation", NULL, NULL );
169                                 rc = LDAP_PROTOCOL_ERROR;
170                                 goto cleanup;
171                         }
172                 }
173
174                 modtail = &mod->sml_next;
175         }
176         *modtail = NULL;
177
178         if( (rc = get_ctrls( conn, op, 1 )) != LDAP_SUCCESS ) {
179 #ifdef NEW_LOGGING
180                 LDAP_LOG( OPERATION, ERR, "do_modify: get_ctrls failed\n", 0, 0, 0 );
181 #else
182                 Debug( LDAP_DEBUG_ANY, "do_modify: get_ctrls failed\n", 0, 0, 0 );
183 #endif
184
185                 goto cleanup;
186         }
187
188         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn );
189         if( rc != LDAP_SUCCESS ) {
190 #ifdef NEW_LOGGING
191                 LDAP_LOG( OPERATION, INFO, "do_modify: conn %d  invalid dn (%s)\n",
192                         conn->c_connid, dn.bv_val, 0 );
193 #else
194                 Debug( LDAP_DEBUG_ANY,
195                         "do_modify: invalid dn (%s)\n", dn.bv_val, 0, 0 );
196 #endif
197                 send_ldap_result( conn, op, rc = LDAP_INVALID_DN_SYNTAX, NULL,
198                     "invalid DN", NULL, NULL );
199                 goto cleanup;
200         }
201
202         if( ndn.bv_len == 0 ) {
203 #ifdef NEW_LOGGING
204                 LDAP_LOG( OPERATION, ERR, 
205                         "do_modify: attempt to modify root DSE.\n",0, 0, 0 );
206 #else
207                 Debug( LDAP_DEBUG_ANY, "do_modify: root dse!\n", 0, 0, 0 );
208 #endif
209
210                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
211                         NULL, "modify upon the root DSE not supported", NULL, NULL );
212                 goto cleanup;
213
214         } else if ( bvmatch( &ndn, &global_schemandn ) ) {
215 #ifdef NEW_LOGGING
216                 LDAP_LOG( OPERATION, ERR,
217                         "do_modify: attempt to modify subschema subentry.\n" , 0, 0, 0  );
218 #else
219                 Debug( LDAP_DEBUG_ANY, "do_modify: subschema subentry!\n", 0, 0, 0 );
220 #endif
221
222                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
223                         NULL, "modification of subschema subentry not supported",
224                         NULL, NULL );
225                 goto cleanup;
226         }
227
228 #ifdef LDAP_DEBUG
229 #ifdef NEW_LOGGING
230         LDAP_LOG( OPERATION, DETAIL1, "do_modify: modifications:\n", 0, 0, 0  );
231 #else
232         Debug( LDAP_DEBUG_ARGS, "modifications:\n", 0, 0, 0 );
233 #endif
234
235         for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
236 #ifdef NEW_LOGGING
237                 LDAP_LOG( OPERATION, DETAIL1, "\t%s:  %s\n", 
238                         tmp->sml_op == LDAP_MOD_ADD ?
239                         "add" : (tmp->sml_op == LDAP_MOD_DELETE ?
240                         "delete" : "replace"), tmp->sml_type.bv_val, 0 );
241
242                 if ( tmp->sml_bvalues == NULL ) {
243                         LDAP_LOG( OPERATION, DETAIL1, "\t\tno values", 0, 0, 0 );
244                 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
245                         LDAP_LOG( OPERATION, DETAIL1, "\t\tzero values", 0, 0, 0 );
246                 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
247                         LDAP_LOG( OPERATION, DETAIL1, "\t\tone value", 0, 0, 0 );
248                 } else {
249                         LDAP_LOG( OPERATION, DETAIL1, "\t\tmultiple values", 0, 0, 0 );
250                 }
251
252 #else
253                 Debug( LDAP_DEBUG_ARGS, "\t%s: %s\n",
254                         tmp->sml_op == LDAP_MOD_ADD
255                                 ? "add" : (tmp->sml_op == LDAP_MOD_DELETE
256                                         ? "delete" : "replace"), tmp->sml_type.bv_val, 0 );
257
258                 if ( tmp->sml_bvalues == NULL ) {
259                         Debug( LDAP_DEBUG_ARGS, "%s\n",
260                            "\t\tno values", NULL, NULL );
261                 } else if ( tmp->sml_bvalues[0].bv_val == NULL ) {
262                         Debug( LDAP_DEBUG_ARGS, "%s\n",
263                            "\t\tzero values", NULL, NULL );
264                 } else if ( tmp->sml_bvalues[1].bv_val == NULL ) {
265                         Debug( LDAP_DEBUG_ARGS, "%s, length %ld\n",
266                            "\t\tone value", (long) tmp->sml_bvalues[0].bv_len, NULL );
267                 } else {
268                         Debug( LDAP_DEBUG_ARGS, "%s\n",
269                            "\t\tmultiple values", NULL, NULL );
270                 }
271 #endif
272         }
273 #endif
274
275         if ( StatslogTest( LDAP_DEBUG_STATS ) ) {
276                 char abuf[BUFSIZ/2], *ptr = abuf;
277                 int len = 0;
278
279                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD dn=\"%s\"\n",
280                         op->o_connid, op->o_opid, dn.bv_val, 0, 0 );
281
282                 for ( tmp = modlist; tmp != NULL; tmp = tmp->sml_next ) {
283                         if (len + 1 + tmp->sml_type.bv_len > sizeof(abuf)) {
284                                 Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
285                                     op->o_connid, op->o_opid, abuf, 0, 0 );
286                                 len = 0;
287                                 ptr = abuf;
288                         }
289                         if (len) {
290                                 *ptr++ = ' ';
291                                 len++;
292                         }
293                         ptr = lutil_strcopy(ptr, tmp->sml_type.bv_val);
294                         len += tmp->sml_type.bv_len;
295                 }
296                 if (len) {
297                         Statslog( LDAP_DEBUG_STATS, "conn=%lu op=%lu MOD attr=%s\n",
298                                 op->o_connid, op->o_opid, abuf, 0, 0 );
299                 }
300         }
301
302         manageDSAit = get_manageDSAit( op );
303
304         /*
305          * We could be serving multiple database backends.  Select the
306          * appropriate one, or send a referral to our "referral server"
307          * if we don't hold it.
308          */
309         if ( (be = select_backend( &ndn, manageDSAit, 0 )) == NULL ) {
310                 BerVarray ref = referral_rewrite( default_referral,
311                         NULL, &pdn, LDAP_SCOPE_DEFAULT );
312
313                 send_ldap_result( conn, op, rc = LDAP_REFERRAL,
314                         NULL, NULL, ref ? ref : default_referral, NULL );
315
316                 ber_bvarray_free( ref );
317                 goto cleanup;
318         }
319
320         /* check restrictions */
321         rc = backend_check_restrictions( be, conn, op, NULL, &text ) ;
322         if( rc != LDAP_SUCCESS ) {
323                 send_ldap_result( conn, op, rc,
324                         NULL, text, NULL, NULL );
325                 goto cleanup;
326         }
327
328         /* check for referrals */
329         rc = backend_check_referrals( be, conn, op, &pdn, &ndn );
330         if ( rc != LDAP_SUCCESS ) {
331                 goto cleanup;
332         }
333
334 #if defined( LDAP_SLAPI )
335         slapi_x_backend_set_pb( pb, be );
336         slapi_x_connection_set_pb( pb, conn );
337         slapi_x_operation_set_pb( pb, op );
338         slapi_pblock_set( pb, SLAPI_MODIFY_TARGET, (void *)dn.bv_val );
339         slapi_pblock_set( pb, SLAPI_MANAGEDSAIT, (void *)manageDSAit );
340         modv = slapi_x_modifications2ldapmods( &modlist );
341         slapi_pblock_set( pb, SLAPI_MODIFY_MODS, (void *)modv );
342
343         rc = doPluginFNs( be, SLAPI_PLUGIN_PRE_MODIFY_FN, pb );
344         if ( rc != 0 ) {
345                 /*
346                  * A preoperation plugin failure will abort the
347                  * entire operation.
348                  */
349 #ifdef NEW_LOGGING
350                 LDAP_LOG( OPERATION, INFO, "do_modify: modify preoperation plugin "
351                                 "failed\n", 0, 0, 0 );
352 #else
353                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preoperation plugin failed.\n",
354                                 0, 0, 0);
355 #endif
356                 if ( slapi_pblock_get( pb, SLAPI_RESULT_CODE, (void *)&rc ) != 0) {
357                         rc = LDAP_OTHER;
358                 }
359                 ldap_mods_free( modv, 1 );
360                 modv = NULL;
361                 goto cleanup;
362         }
363
364         /*
365          * It's possible that the preoperation plugin changed the
366          * modification array, so we need to convert it back to
367          * a Modification list.
368          *
369          * Calling slapi_x_modifications2ldapmods() destroyed modlist so
370          * we don't need to free it.
371          */
372         slapi_pblock_get( pb, SLAPI_MODIFY_MODS, (void **)&modv );
373         modlist = slapi_x_ldapmods2modifications( modv );
374 #endif /* defined( LDAP_SLAPI ) */
375
376         /*
377          * do the modify if 1 && (2 || 3)
378          * 1) there is a modify function implemented in this backend;
379          * 2) this backend is master for what it holds;
380          * 3) it's a replica and the dn supplied is the update_ndn.
381          */
382         if ( be->be_modify ) {
383                 /* do the update here */
384                 int repl_user = be_isupdate( be, &op->o_ndn );
385 #ifndef SLAPD_MULTIMASTER
386                 /* Multimaster slapd does not have to check for replicator dn
387                  * because it accepts each modify request
388                  */
389                 if ( !be->be_update_ndn.bv_len || repl_user )
390 #endif
391                 {
392                         int update = be->be_update_ndn.bv_len;
393                         const char *text;
394                         char textbuf[SLAP_TEXT_BUFLEN];
395                         size_t textlen = sizeof textbuf;
396
397                         rc = slap_mods_check( modlist, update, &text,
398                                 textbuf, textlen );
399
400                         if( rc != LDAP_SUCCESS ) {
401                                 send_ldap_result( conn, op, rc,
402                                         NULL, text, NULL, NULL );
403                                 goto cleanup;
404                         }
405
406                         if ( !repl_user ) {
407                                 for( modtail = &modlist;
408                                         *modtail != NULL;
409                                         modtail = &(*modtail)->sml_next )
410                                 {
411                                         /* empty */
412                                 }
413
414                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
415                                         textbuf, textlen );
416                                 if( rc != LDAP_SUCCESS ) {
417                                         send_ldap_result( conn, op, rc,
418                                                 NULL, text,
419                                                 NULL, NULL );
420                                         goto cleanup;
421                                 }
422                         }
423
424                         if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
425 #ifdef SLAPD_MULTIMASTER
426                                 && !repl_user
427 #endif
428                         ) {
429                                 /* but we log only the ones not from a replicator user */
430                                 replog( be, op, &pdn, &ndn, modlist );
431                         }
432
433 #ifndef SLAPD_MULTIMASTER
434                 /* send a referral */
435                 } else {
436                         BerVarray defref = be->be_update_refs
437                                 ? be->be_update_refs : default_referral;
438                         BerVarray ref = referral_rewrite( defref,
439                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
440
441                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
442                                 ref ? ref : defref, NULL );
443
444                         ber_bvarray_free( ref );
445 #endif
446                 }
447         } else {
448                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
449                     NULL, "operation not supported within namingContext",
450                         NULL, NULL );
451         }
452
453 #if defined( LDAP_SLAPI )
454         if ( doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb ) != 0 ) {
455 #ifdef NEW_LOGGING
456                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postoperation plugins "
457                                 "failed\n", 0, 0, 0 );
458 #else
459                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postoperation plugins "
460                                 "failed.\n", 0, 0, 0);
461 #endif
462         }
463 #endif /* defined( LDAP_SLAPI ) */
464
465 cleanup:
466         free( pdn.bv_val );
467         free( ndn.bv_val );
468         if ( modlist != NULL ) slap_mods_free( modlist );
469 #if defined( LDAP_SLAPI )
470         if ( modv != NULL ) slapi_x_free_ldapmods( modv );
471 #endif
472         return rc;
473 }
474
475 /*
476  * Do basic attribute type checking and syntax validation.
477  */
478 int slap_mods_check(
479         Modifications *ml,
480         int update,
481         const char **text,
482         char *textbuf,
483         size_t textlen )
484 {
485         int rc;
486
487         for( ; ml != NULL; ml = ml->sml_next ) {
488                 AttributeDescription *ad = NULL;
489
490                 /* convert to attribute description */
491                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
492
493                 if( rc != LDAP_SUCCESS ) {
494                         snprintf( textbuf, textlen, "%s: %s",
495                                 ml->sml_type.bv_val, *text );
496                         *text = textbuf;
497                         return rc;
498                 }
499
500                 ad = ml->sml_desc;
501
502                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
503                         && !slap_ad_is_binary( ad ))
504                 {
505                         /* attribute requires binary transfer */
506                         snprintf( textbuf, textlen,
507                                 "%s: requires ;binary transfer",
508                                 ml->sml_type.bv_val );
509                         *text = textbuf;
510                         return LDAP_UNDEFINED_TYPE;
511                 }
512
513                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
514                         && slap_ad_is_binary( ad ))
515                 {
516                         /* attribute requires binary transfer */
517                         snprintf( textbuf, textlen,
518                                 "%s: disallows ;binary transfer",
519                                 ml->sml_type.bv_val );
520                         *text = textbuf;
521                         return LDAP_UNDEFINED_TYPE;
522                 }
523
524                 if( slap_ad_is_tag_range( ad )) {
525                         /* attribute requires binary transfer */
526                         snprintf( textbuf, textlen,
527                                 "%s: inappropriate use of tag range option",
528                                 ml->sml_type.bv_val );
529                         *text = textbuf;
530                         return LDAP_UNDEFINED_TYPE;
531                 }
532
533                 if (!update && is_at_no_user_mod( ad->ad_type )) {
534                         /* user modification disallowed */
535                         snprintf( textbuf, textlen,
536                                 "%s: no user modification allowed",
537                                 ml->sml_type.bv_val );
538                         *text = textbuf;
539                         return LDAP_CONSTRAINT_VIOLATION;
540                 }
541
542                 if ( is_at_obsolete( ad->ad_type ) &&
543                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
544                 {
545                         /*
546                          * attribute is obsolete,
547                          * only allow replace/delete with no values
548                          */
549                         snprintf( textbuf, textlen,
550                                 "%s: attribute is obsolete",
551                                 ml->sml_type.bv_val );
552                         *text = textbuf;
553                         return LDAP_CONSTRAINT_VIOLATION;
554                 }
555
556                 /*
557                  * check values
558                  */
559                 if( ml->sml_bvalues != NULL ) {
560                         ber_len_t nvals;
561                         slap_syntax_validate_func *validate =
562                                 ad->ad_type->sat_syntax->ssyn_validate;
563                         slap_syntax_transform_func *pretty =
564                                 ad->ad_type->sat_syntax->ssyn_pretty;
565  
566                         if( !pretty && !validate ) {
567                                 *text = "no validator for syntax";
568                                 snprintf( textbuf, textlen,
569                                         "%s: no validator for syntax %s",
570                                         ml->sml_type.bv_val,
571                                         ad->ad_type->sat_syntax->ssyn_oid );
572                                 *text = textbuf;
573                                 return LDAP_INVALID_SYNTAX;
574                         }
575
576                         /*
577                          * check that each value is valid per syntax
578                          *      and pretty if appropriate
579                          */
580                         for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
581                                 struct berval pval;
582                                 if( pretty ) {
583                                         rc = pretty( ad->ad_type->sat_syntax,
584                                                 &ml->sml_bvalues[nvals], &pval );
585                                 } else {
586                                         rc = validate( ad->ad_type->sat_syntax,
587                                                 &ml->sml_bvalues[nvals] );
588                                 }
589
590                                 if( rc != 0 ) {
591                                         snprintf( textbuf, textlen,
592                                                 "%s: value #%ld invalid per syntax",
593                                                 ml->sml_type.bv_val, (long) nvals );
594                                         *text = textbuf;
595                                         return LDAP_INVALID_SYNTAX;
596                                 }
597
598                                 if( pretty ) {
599                                         ber_memfree( ml->sml_bvalues[nvals].bv_val );
600                                         ml->sml_bvalues[nvals] = pval;
601                                 }
602                         }
603
604                         /*
605                          * a rough single value check... an additional check is needed
606                          * to catch add of single value to existing single valued attribute
607                          */
608                         if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
609                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
610                         {
611                                 snprintf( textbuf, textlen,
612                                         "%s: multiple value provided",
613                                         ml->sml_type.bv_val );
614                                 *text = textbuf;
615                                 return LDAP_CONSTRAINT_VIOLATION;
616                         }
617                 }
618         }
619
620         return LDAP_SUCCESS;
621 }
622
623 int slap_mods_opattrs(
624         Backend *be,
625         Operation *op,
626         Modifications *mods,
627         Modifications **modtail,
628         const char **text,
629         char *textbuf, size_t textlen )
630 {
631         struct berval name, timestamp, csn;
632         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
633         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
634         Modifications *mod;
635
636         int mop = op->o_tag == LDAP_REQ_ADD
637                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
638
639         assert( modtail != NULL );
640         assert( *modtail == NULL );
641
642         if( SLAP_LASTMOD(be) ) {
643                 struct tm *ltm;
644                 time_t now = slap_get_time();
645
646                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
647                 ltm = gmtime( &now );
648                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
649
650                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
651                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
652                 csn.bv_val = csnbuf;
653
654                 timestamp.bv_val = timebuf;
655                 timestamp.bv_len = strlen(timebuf);
656
657                 if( op->o_dn.bv_len == 0 ) {
658                         name.bv_val = SLAPD_ANONYMOUS;
659                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
660                 } else {
661                         name = op->o_dn;
662                 }
663         }
664
665         if( op->o_tag == LDAP_REQ_ADD ) {
666                 struct berval tmpval;
667
668                 if( global_schemacheck ) {
669                         int rc = mods_structural_class( mods, &tmpval,
670                                 text, textbuf, textlen );
671                         if( rc != LDAP_SUCCESS ) {
672                                 return rc;
673                         }
674
675                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
676                         mod->sml_op = mop;
677                         mod->sml_type.bv_val = NULL;
678                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
679                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
680                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
681                         mod->sml_bvalues[1].bv_val = NULL;
682                         assert( mod->sml_bvalues[0].bv_val );
683                         *modtail = mod;
684                         modtail = &mod->sml_next;
685                 }
686
687                 if( SLAP_LASTMOD(be) ) {
688                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
689
690                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
691                         tmpval.bv_val = uuidbuf;
692                 
693                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
694                         mod->sml_op = mop;
695                         mod->sml_type.bv_val = NULL;
696                         mod->sml_desc = slap_schema.si_ad_entryUUID;
697                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
698                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
699                         mod->sml_bvalues[1].bv_val = NULL;
700                         assert( mod->sml_bvalues[0].bv_val );
701                         *modtail = mod;
702                         modtail = &mod->sml_next;
703
704                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
705                         mod->sml_op = mop;
706                         mod->sml_type.bv_val = NULL;
707                         mod->sml_desc = slap_schema.si_ad_creatorsName;
708                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
709                         ber_dupbv( &mod->sml_bvalues[0], &name );
710                         mod->sml_bvalues[1].bv_val = NULL;
711                         assert( mod->sml_bvalues[0].bv_val );
712                         *modtail = mod;
713                         modtail = &mod->sml_next;
714
715                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
716                         mod->sml_op = mop;
717                         mod->sml_type.bv_val = NULL;
718                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
719                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
720                         ber_dupbv( &mod->sml_bvalues[0], &timestamp );
721                         mod->sml_bvalues[1].bv_val = NULL;
722                         assert( mod->sml_bvalues[0].bv_val );
723                         *modtail = mod;
724                         modtail = &mod->sml_next;
725                 }
726         }
727
728         if( SLAP_LASTMOD(be) ) {
729                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
730                 mod->sml_op = mop;
731                 mod->sml_type.bv_val = NULL;
732                 mod->sml_desc = slap_schema.si_ad_entryCSN;
733                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
734                 ber_dupbv( &mod->sml_bvalues[0], &csn );
735                 mod->sml_bvalues[1].bv_val = NULL;
736                 assert( mod->sml_bvalues[0].bv_val );
737                 *modtail = mod;
738                 modtail = &mod->sml_next;
739
740                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
741                 mod->sml_op = mop;
742                 mod->sml_type.bv_val = NULL;
743                 mod->sml_desc = slap_schema.si_ad_modifiersName;
744                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
745                 ber_dupbv( &mod->sml_bvalues[0], &name );
746                 mod->sml_bvalues[1].bv_val = NULL;
747                 assert( mod->sml_bvalues[0].bv_val );
748                 *modtail = mod;
749                 modtail = &mod->sml_next;
750
751                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
752                 mod->sml_op = mop;
753                 mod->sml_type.bv_val = NULL;
754                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
755                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
756                 ber_dupbv( &mod->sml_bvalues[0], &timestamp );
757                 mod->sml_bvalues[1].bv_val = NULL;
758                 assert( mod->sml_bvalues[0].bv_val );
759                 *modtail = mod;
760                 modtail = &mod->sml_next;
761         }
762
763         *modtail = NULL;
764         return LDAP_SUCCESS;
765 }
766