]> git.sur5r.net Git - openldap/blob - servers/slapd/modify.c
418110180063d945d282f9009856a37ec3a9d511
[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 && rc != LDAP_OTHER ) {
352                 /*
353                  * either there is no preOp (modify) plugins
354                  * or a plugin failed. Just log it
355                  *
356                  * FIXME: is this correct?
357                  */
358 #ifdef NEW_LOGGING
359                 LDAP_LOG( OPERATION, INFO, "do_modify: modify preOps "
360                                 "failed\n", 0, 0, 0 );
361 #else
362                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify preOps failed.\n",
363                                 0, 0, 0);
364 #endif
365         }
366 #endif /* defined( LDAP_SLAPI ) */
367
368         /*
369          * do the modify if 1 && (2 || 3)
370          * 1) there is a modify function implemented in this backend;
371          * 2) this backend is master for what it holds;
372          * 3) it's a replica and the dn supplied is the update_ndn.
373          */
374         if ( be->be_modify ) {
375                 /* do the update here */
376                 int repl_user = be_isupdate( be, &op->o_ndn );
377 #ifndef SLAPD_MULTIMASTER
378                 /* Multimaster slapd does not have to check for replicator dn
379                  * because it accepts each modify request
380                  */
381                 if ( !be->be_update_ndn.bv_len || repl_user )
382 #endif
383                 {
384                         int update = be->be_update_ndn.bv_len;
385                         const char *text;
386                         char textbuf[SLAP_TEXT_BUFLEN];
387                         size_t textlen = sizeof textbuf;
388
389                         rc = slap_mods_check( modlist, update, &text,
390                                 textbuf, textlen );
391
392                         if( rc != LDAP_SUCCESS ) {
393                                 send_ldap_result( conn, op, rc,
394                                         NULL, text, NULL, NULL );
395                                 goto cleanup;
396                         }
397
398                         if ( !repl_user ) {
399                                 for( modtail = &modlist;
400                                         *modtail != NULL;
401                                         modtail = &(*modtail)->sml_next )
402                                 {
403                                         /* empty */
404                                 }
405
406                                 rc = slap_mods_opattrs( be, op, modlist, modtail, &text,
407                                         textbuf, textlen );
408                                 if( rc != LDAP_SUCCESS ) {
409                                         send_ldap_result( conn, op, rc,
410                                                 NULL, text,
411                                                 NULL, NULL );
412                                         goto cleanup;
413                                 }
414                         }
415
416                         if ( (*be->be_modify)( be, conn, op, &pdn, &ndn, modlist ) == 0
417 #ifdef SLAPD_MULTIMASTER
418                                 && !repl_user
419 #endif
420                         ) {
421                                 /* but we log only the ones not from a replicator user */
422                                 replog( be, op, &pdn, &ndn, modlist );
423                         }
424
425 #ifndef SLAPD_MULTIMASTER
426                 /* send a referral */
427                 } else {
428                         BerVarray defref = be->be_update_refs
429                                 ? be->be_update_refs : default_referral;
430                         BerVarray ref = referral_rewrite( defref,
431                                 NULL, &pdn, LDAP_SCOPE_DEFAULT );
432
433                         send_ldap_result( conn, op, rc = LDAP_REFERRAL, NULL, NULL,
434                                 ref ? ref : defref, NULL );
435
436                         ber_bvarray_free( ref );
437 #endif
438                 }
439         } else {
440                 send_ldap_result( conn, op, rc = LDAP_UNWILLING_TO_PERFORM,
441                     NULL, "operation not supported within namingContext",
442                         NULL, NULL );
443         }
444
445 #if defined( LDAP_SLAPI )
446         rc = doPluginFNs( be, SLAPI_PLUGIN_POST_MODIFY_FN, pb );
447         if ( rc != 0 && rc != LDAP_OTHER ) {
448                 /*
449                  * either there is no postOp (modify) plugins
450                  * or a plugin failed. Just log it
451                  *
452                  * FIXME: is this correct?
453                  */
454 #ifdef NEW_LOGGING
455                 LDAP_LOG( OPERATION, INFO, "do_modify: modify postOps "
456                                 "failed\n", 0, 0, 0 );
457 #else
458                 Debug(LDAP_DEBUG_TRACE, "do_modify: modify postOps "
459                                 "failed.\n", 0, 0, 0);
460 #endif
461         }
462 #endif /* defined( LDAP_SLAPI ) */
463
464 cleanup:
465         free( pdn.bv_val );
466         free( ndn.bv_val );
467         if ( modlist != NULL )
468                 slap_mods_free( modlist );
469         if ( modv != NULL )
470                 FreeLDAPMods( modv );
471         return rc;
472 }
473
474 /*
475  * Do basic attribute type checking and syntax validation.
476  */
477 int slap_mods_check(
478         Modifications *ml,
479         int update,
480         const char **text,
481         char *textbuf,
482         size_t textlen )
483 {
484         int rc;
485
486         for( ; ml != NULL; ml = ml->sml_next ) {
487                 AttributeDescription *ad = NULL;
488
489                 /* convert to attribute description */
490                 rc = slap_bv2ad( &ml->sml_type, &ml->sml_desc, text );
491
492                 if( rc != LDAP_SUCCESS ) {
493                         snprintf( textbuf, textlen, "%s: %s",
494                                 ml->sml_type.bv_val, *text );
495                         *text = textbuf;
496                         return rc;
497                 }
498
499                 ad = ml->sml_desc;
500
501                 if( slap_syntax_is_binary( ad->ad_type->sat_syntax )
502                         && !slap_ad_is_binary( ad ))
503                 {
504                         /* attribute requires binary transfer */
505                         snprintf( textbuf, textlen,
506                                 "%s: requires ;binary transfer",
507                                 ml->sml_type.bv_val );
508                         *text = textbuf;
509                         return LDAP_UNDEFINED_TYPE;
510                 }
511
512                 if( !slap_syntax_is_binary( ad->ad_type->sat_syntax )
513                         && slap_ad_is_binary( ad ))
514                 {
515                         /* attribute requires binary transfer */
516                         snprintf( textbuf, textlen,
517                                 "%s: disallows ;binary transfer",
518                                 ml->sml_type.bv_val );
519                         *text = textbuf;
520                         return LDAP_UNDEFINED_TYPE;
521                 }
522
523                 if( slap_ad_is_tag_range( ad )) {
524                         /* attribute requires binary transfer */
525                         snprintf( textbuf, textlen,
526                                 "%s: inappropriate use of tag range option",
527                                 ml->sml_type.bv_val );
528                         *text = textbuf;
529                         return LDAP_UNDEFINED_TYPE;
530                 }
531
532                 if (!update && is_at_no_user_mod( ad->ad_type )) {
533                         /* user modification disallowed */
534                         snprintf( textbuf, textlen,
535                                 "%s: no user modification allowed",
536                                 ml->sml_type.bv_val );
537                         *text = textbuf;
538                         return LDAP_CONSTRAINT_VIOLATION;
539                 }
540
541                 if ( is_at_obsolete( ad->ad_type ) &&
542                         ( ml->sml_op == LDAP_MOD_ADD || ml->sml_bvalues != NULL ) )
543                 {
544                         /*
545                          * attribute is obsolete,
546                          * only allow replace/delete with no values
547                          */
548                         snprintf( textbuf, textlen,
549                                 "%s: attribute is obsolete",
550                                 ml->sml_type.bv_val );
551                         *text = textbuf;
552                         return LDAP_CONSTRAINT_VIOLATION;
553                 }
554
555                 /*
556                  * check values
557                  */
558                 if( ml->sml_bvalues != NULL ) {
559                         ber_len_t nvals;
560                         slap_syntax_validate_func *validate =
561                                 ad->ad_type->sat_syntax->ssyn_validate;
562                         slap_syntax_transform_func *pretty =
563                                 ad->ad_type->sat_syntax->ssyn_pretty;
564  
565                         if( !pretty && !validate ) {
566                                 *text = "no validator for syntax";
567                                 snprintf( textbuf, textlen,
568                                         "%s: no validator for syntax %s",
569                                         ml->sml_type.bv_val,
570                                         ad->ad_type->sat_syntax->ssyn_oid );
571                                 *text = textbuf;
572                                 return LDAP_INVALID_SYNTAX;
573                         }
574
575                         /*
576                          * check that each value is valid per syntax
577                          *      and pretty if appropriate
578                          */
579                         for( nvals = 0; ml->sml_bvalues[nvals].bv_val; nvals++ ) {
580                                 struct berval pval;
581                                 if( pretty ) {
582                                         rc = pretty( ad->ad_type->sat_syntax,
583                                                 &ml->sml_bvalues[nvals], &pval );
584                                 } else {
585                                         rc = validate( ad->ad_type->sat_syntax,
586                                                 &ml->sml_bvalues[nvals] );
587                                 }
588
589                                 if( rc != 0 ) {
590                                         snprintf( textbuf, textlen,
591                                                 "%s: value #%ld invalid per syntax",
592                                                 ml->sml_type.bv_val, (long) nvals );
593                                         *text = textbuf;
594                                         return LDAP_INVALID_SYNTAX;
595                                 }
596
597                                 if( pretty ) {
598                                         ber_memfree( ml->sml_bvalues[nvals].bv_val );
599                                         ml->sml_bvalues[nvals] = pval;
600                                 }
601                         }
602
603                         /*
604                          * a rough single value check... an additional check is needed
605                          * to catch add of single value to existing single valued attribute
606                          */
607                         if( ( ml->sml_op == LDAP_MOD_ADD || ml->sml_op == LDAP_MOD_REPLACE )
608                                 && nvals > 1 && is_at_single_value( ad->ad_type ))
609                         {
610                                 snprintf( textbuf, textlen,
611                                         "%s: multiple value provided",
612                                         ml->sml_type.bv_val );
613                                 *text = textbuf;
614                                 return LDAP_CONSTRAINT_VIOLATION;
615                         }
616                 }
617         }
618
619         return LDAP_SUCCESS;
620 }
621
622 int slap_mods_opattrs(
623         Backend *be,
624         Operation *op,
625         Modifications *mods,
626         Modifications **modtail,
627         const char **text,
628         char *textbuf, size_t textlen )
629 {
630         struct berval name, timestamp, csn;
631         char timebuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
632         char csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
633         Modifications *mod;
634
635         int mop = op->o_tag == LDAP_REQ_ADD
636                 ? LDAP_MOD_ADD : LDAP_MOD_REPLACE;
637
638         assert( modtail != NULL );
639         assert( *modtail == NULL );
640
641         if( SLAP_LASTMOD(be) ) {
642                 struct tm *ltm;
643                 time_t now = slap_get_time();
644
645                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
646                 ltm = gmtime( &now );
647                 lutil_gentime( timebuf, sizeof(timebuf), ltm );
648
649                 csn.bv_len = lutil_csnstr( csnbuf, sizeof( csnbuf ), 0, 0 );
650                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
651                 csn.bv_val = csnbuf;
652
653                 timestamp.bv_val = timebuf;
654                 timestamp.bv_len = strlen(timebuf);
655
656                 if( op->o_dn.bv_len == 0 ) {
657                         name.bv_val = SLAPD_ANONYMOUS;
658                         name.bv_len = sizeof(SLAPD_ANONYMOUS)-1;
659                 } else {
660                         name = op->o_dn;
661                 }
662         }
663
664         if( op->o_tag == LDAP_REQ_ADD ) {
665                 struct berval tmpval;
666
667                 if( global_schemacheck ) {
668                         int rc = mods_structural_class( mods, &tmpval,
669                                 text, textbuf, textlen );
670                         if( rc != LDAP_SUCCESS ) {
671                                 return rc;
672                         }
673
674                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
675                         mod->sml_op = mop;
676                         mod->sml_type.bv_val = NULL;
677                         mod->sml_desc = slap_schema.si_ad_structuralObjectClass;
678                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
679                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
680                         mod->sml_bvalues[1].bv_val = NULL;
681                         assert( mod->sml_bvalues[0].bv_val );
682                         *modtail = mod;
683                         modtail = &mod->sml_next;
684                 }
685
686                 if( SLAP_LASTMOD(be) ) {
687                         char uuidbuf[ LDAP_LUTIL_UUIDSTR_BUFSIZE ];
688
689                         tmpval.bv_len = lutil_uuidstr( uuidbuf, sizeof( uuidbuf ) );
690                         tmpval.bv_val = uuidbuf;
691                 
692                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
693                         mod->sml_op = mop;
694                         mod->sml_type.bv_val = NULL;
695                         mod->sml_desc = slap_schema.si_ad_entryUUID;
696                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
697                         ber_dupbv( &mod->sml_bvalues[0], &tmpval );
698                         mod->sml_bvalues[1].bv_val = NULL;
699                         assert( mod->sml_bvalues[0].bv_val );
700                         *modtail = mod;
701                         modtail = &mod->sml_next;
702
703                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
704                         mod->sml_op = mop;
705                         mod->sml_type.bv_val = NULL;
706                         mod->sml_desc = slap_schema.si_ad_creatorsName;
707                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
708                         ber_dupbv( &mod->sml_bvalues[0], &name );
709                         mod->sml_bvalues[1].bv_val = NULL;
710                         assert( mod->sml_bvalues[0].bv_val );
711                         *modtail = mod;
712                         modtail = &mod->sml_next;
713
714                         mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
715                         mod->sml_op = mop;
716                         mod->sml_type.bv_val = NULL;
717                         mod->sml_desc = slap_schema.si_ad_createTimestamp;
718                         mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
719                         ber_dupbv( &mod->sml_bvalues[0], &timestamp );
720                         mod->sml_bvalues[1].bv_val = NULL;
721                         assert( mod->sml_bvalues[0].bv_val );
722                         *modtail = mod;
723                         modtail = &mod->sml_next;
724                 }
725         }
726
727         if( SLAP_LASTMOD(be) ) {
728                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
729                 mod->sml_op = mop;
730                 mod->sml_type.bv_val = NULL;
731                 mod->sml_desc = slap_schema.si_ad_entryCSN;
732                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
733                 ber_dupbv( &mod->sml_bvalues[0], &csn );
734                 mod->sml_bvalues[1].bv_val = NULL;
735                 assert( mod->sml_bvalues[0].bv_val );
736                 *modtail = mod;
737                 modtail = &mod->sml_next;
738
739                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
740                 mod->sml_op = mop;
741                 mod->sml_type.bv_val = NULL;
742                 mod->sml_desc = slap_schema.si_ad_modifiersName;
743                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
744                 ber_dupbv( &mod->sml_bvalues[0], &name );
745                 mod->sml_bvalues[1].bv_val = NULL;
746                 assert( mod->sml_bvalues[0].bv_val );
747                 *modtail = mod;
748                 modtail = &mod->sml_next;
749
750                 mod = (Modifications *) ch_malloc( sizeof( Modifications ) );
751                 mod->sml_op = mop;
752                 mod->sml_type.bv_val = NULL;
753                 mod->sml_desc = slap_schema.si_ad_modifyTimestamp;
754                 mod->sml_bvalues = (BerVarray) ch_malloc( 2 * sizeof( struct berval ) );
755                 ber_dupbv( &mod->sml_bvalues[0], &timestamp );
756                 mod->sml_bvalues[1].bv_val = NULL;
757                 assert( mod->sml_bvalues[0].bv_val );
758                 *modtail = mod;
759                 modtail = &mod->sml_next;
760         }
761
762         *modtail = NULL;
763         return LDAP_SUCCESS;
764 }
765
766 #ifdef LDAP_SLAPI
767 /*
768  * Synthesise an LDAPMod array from a Modifications list to pass
769  * to SLAPI.
770  */
771 static LDAPMod **Modifications2LDAPMods(Modifications *modlist)
772 {
773         LDAPMod *mods, **modv;
774         int i, j;
775         Modifications *ml;
776
777         /* based on back-ldap/modify.c */
778         for (i = 0, ml = modlist; ml != NULL; i++, ml = ml->sml_next)
779                 ;
780
781         mods = (LDAPMod *)ch_malloc(i * sizeof(LDAPMod));
782         if (mods == NULL) {
783                 return NULL;
784         }
785
786         modv = (LDAPMod **)ch_malloc((i + 1) * sizeof(LDAPMod *));
787         if (modv == NULL) {
788                 ch_free(mods);
789                 return NULL;
790         }
791
792         for (i = 0, ml = modlist; ml != NULL; ml = ml->sml_next) {
793                 if (ml->sml_desc->ad_type->sat_no_user_mod) {
794                         continue;
795                 }
796                 modv[i] = &mods[i];
797                 mods[i].mod_op = ml->sml_op | LDAP_MOD_BVALUES;
798                 mods[i].mod_type = ml->sml_desc->ad_cname.bv_val;
799                 if (ml->sml_bvalues != NULL) {
800                         for (j = 0; ml->sml_bvalues[j].bv_val != NULL; j++)
801                                 ;
802                         mods[i].mod_bvalues = (struct berval **)ch_malloc((j + 1) *
803                                 sizeof(struct berval *));
804                         for (j = 0; ml->sml_bvalues[j].bv_val != NULL; j++)
805                                 mods[i].mod_bvalues[j] = &ml->sml_bvalues[j];
806                 } else {
807                         mods[i].mod_bvalues = NULL;
808                 }
809                 i++;
810         }
811         modv[i] = NULL;
812
813         return modv;
814 }
815
816 /*
817  * Free a contiguous block of LDAP modifications.
818  */
819 void FreeLDAPMods(LDAPMod **modv)
820 {
821         int i;
822         LDAPMod *mods;
823
824         mods = modv[0];
825
826         for (i = 0; modv[i] != NULL; i++) {
827                 ch_free(modv[i]->mod_bvalues);
828         }
829         ch_free(mods);
830         ch_free(modv);
831 }
832 #endif /* LDAP_SLAPI */
833