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