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