]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/lastmod.c
ITS#4720 release csn mutex before checkpointing.
[openldap] / servers / slapd / overlays / lastmod.c
1 /* lastmod.c - returns last modification info */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 2004-2006 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by Pierangelo Masarati for inclusion in
17  * OpenLDAP Software.
18  */
19
20 #include "portable.h"
21
22 #ifdef SLAPD_OVER_LASTMOD
23
24 #include <stdio.h>
25
26 #include <ac/string.h>
27 #include <ac/socket.h>
28
29 #include "slap.h"
30 #include "lutil.h"
31
32 typedef struct lastmod_info_t {
33         struct berval           lmi_rdnvalue;
34         Entry                   *lmi_e;
35         ldap_pvt_thread_mutex_t lmi_entry_mutex;
36         int                     lmi_enabled;
37 } lastmod_info_t;
38
39 struct lastmod_schema_t {
40         ObjectClass             *lms_oc_lastmod;
41         AttributeDescription    *lms_ad_lastmodDN;
42         AttributeDescription    *lms_ad_lastmodType;
43         AttributeDescription    *lms_ad_lastmodEnabled;
44 } lastmod_schema;
45
46 enum lastmodType_e {
47         LASTMOD_ADD = 0,
48         LASTMOD_DELETE,
49         LASTMOD_EXOP,
50         LASTMOD_MODIFY,
51         LASTMOD_MODRDN,
52         LASTMOD_UNKNOWN
53 };
54
55 struct berval lastmodType[] = {
56         BER_BVC( "add" ),
57         BER_BVC( "delete" ),
58         BER_BVC( "exop" ),
59         BER_BVC( "modify" ),
60         BER_BVC( "modrdn" ),
61         BER_BVC( "unknown" ),
62         BER_BVNULL
63 };
64
65 static struct m_s {
66         char                    *schema;
67         slap_mask_t             flags;
68         int                     offset;
69 } moc[] = {
70         { "( 1.3.6.1.4.1.4203.666.3.13"
71                 "NAME 'lastmod' "
72                 "DESC 'OpenLDAP per-database last modification monitoring' "
73                 "STRUCTURAL "
74                 "SUP top "
75                 "MUST cn "
76                 "MAY ( "
77                         "lastmodDN "
78                         "$ lastmodType "
79                         "$ description "
80                         "$ seeAlso "
81                 ") )", SLAP_OC_OPERATIONAL|SLAP_OC_HIDE,
82                 offsetof( struct lastmod_schema_t, lms_oc_lastmod ) },
83         { NULL }
84 }, mat[] = {
85         { "( 1.3.6.1.4.1.4203.666.1.28"
86                 "NAME 'lastmodDN' "
87                 "DESC 'DN of last modification' "
88                 "EQUALITY distinguishedNameMatch "
89                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
90                 "NO-USER-MODIFICATION "
91                 "USAGE directoryOperation )", SLAP_AT_HIDE,
92                 offsetof( struct lastmod_schema_t, lms_ad_lastmodDN ) },
93         { "( 1.3.6.1.4.1.4203.666.1.29"
94                 "NAME 'lastmodType' "
95                 "DESC 'Type of last modification' "
96                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 "
97                 "EQUALITY caseIgnoreMatch "
98                 "SINGLE-VALUE "
99                 "NO-USER-MODIFICATION "
100                 "USAGE directoryOperation )", SLAP_AT_HIDE,
101                 offsetof( struct lastmod_schema_t, lms_ad_lastmodType ) },
102         { "( 1.3.6.1.4.1.4203.666.1.30"
103                 "NAME 'lastmodEnabled' "
104                 "DESC 'Lastmod overlay state' "
105                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.7 "
106                 "EQUALITY booleanMatch "
107                 "SINGLE-VALUE )", 0,
108                 offsetof( struct lastmod_schema_t, lms_ad_lastmodEnabled ) },
109         { NULL }
110
111         /* FIXME: what about UUID of last modified entry? */
112 };
113
114 static int
115 lastmod_search( Operation *op, SlapReply *rs )
116 {
117         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
118         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
119         int                     rc;
120
121         /* if we get here, it must be a success */
122         rs->sr_err = LDAP_SUCCESS;
123
124         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
125
126         rc = test_filter( op, lmi->lmi_e, op->oq_search.rs_filter );
127         if ( rc == LDAP_COMPARE_TRUE ) {
128                 rs->sr_attrs = op->ors_attrs;
129                 rs->sr_flags = 0;
130                 rs->sr_entry = lmi->lmi_e;
131                 rs->sr_err = send_search_entry( op, rs );
132                 rs->sr_entry = NULL;
133                 rs->sr_flags = 0;
134                 rs->sr_attrs = NULL;
135         }
136
137         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
138
139         send_ldap_result( op, rs );
140
141         return 0;
142 }
143
144 static int
145 lastmod_compare( Operation *op, SlapReply *rs )
146 {
147         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
148         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
149         Attribute               *a;
150
151         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
152
153         if ( get_assert( op ) &&
154                 ( test_filter( op, lmi->lmi_e, get_assertion( op ) ) != LDAP_COMPARE_TRUE ) )
155         {
156                 rs->sr_err = LDAP_ASSERTION_FAILED;
157                 goto return_results;
158         }
159
160         rs->sr_err = access_allowed( op, lmi->lmi_e, op->oq_compare.rs_ava->aa_desc,
161                 &op->oq_compare.rs_ava->aa_value, ACL_COMPARE, NULL );
162         if ( ! rs->sr_err ) {
163                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
164                 goto return_results;
165         }
166
167         rs->sr_err = LDAP_NO_SUCH_ATTRIBUTE;
168
169         for ( a = attr_find( lmi->lmi_e->e_attrs, op->oq_compare.rs_ava->aa_desc );
170                 a != NULL;
171                 a = attr_find( a->a_next, op->oq_compare.rs_ava->aa_desc ) )
172         {
173                 rs->sr_err = LDAP_COMPARE_FALSE;
174
175                 if ( value_find_ex( op->oq_compare.rs_ava->aa_desc,
176                         SLAP_MR_ATTRIBUTE_VALUE_NORMALIZED_MATCH |
177                                 SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
178                         a->a_nvals, &op->oq_compare.rs_ava->aa_value, op->o_tmpmemctx ) == 0 )
179                 {
180                         rs->sr_err = LDAP_COMPARE_TRUE;
181                         break;
182                 }
183         }
184
185 return_results:;
186
187         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
188
189         send_ldap_result( op, rs );
190
191         if( rs->sr_err == LDAP_COMPARE_FALSE || rs->sr_err == LDAP_COMPARE_TRUE ) {
192                 rs->sr_err = LDAP_SUCCESS;
193         }
194
195         return rs->sr_err;
196 }
197
198 static int
199 lastmod_exop( Operation *op, SlapReply *rs )
200 {
201         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
202
203         /* Temporary */
204
205         op->o_bd->bd_info = (BackendInfo *)on->on_info;
206         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
207         rs->sr_text = "not allowed within namingContext";
208         send_ldap_result( op, rs );
209         rs->sr_text = NULL;
210         
211         return -1;
212 }
213
214 static int
215 lastmod_modify( Operation *op, SlapReply *rs )
216 {
217         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
218         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
219         Modifications           *ml;
220
221         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
222
223         if ( !acl_check_modlist( op, lmi->lmi_e, op->orm_modlist ) ) {
224                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
225                 goto cleanup;
226         }
227
228         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
229                 Attribute       *a;
230
231                 if ( ml->sml_desc != lastmod_schema.lms_ad_lastmodEnabled ) {
232                         continue;
233                 }
234
235                 if ( ml->sml_op != LDAP_MOD_REPLACE ) {
236                         rs->sr_text = "unsupported mod type";
237                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
238                         goto cleanup;
239                 }
240                 
241                 a = attr_find( lmi->lmi_e->e_attrs, ml->sml_desc );
242
243                 if ( a == NULL ) {
244                         rs->sr_text = "lastmod overlay internal error";
245                         rs->sr_err = LDAP_OTHER;
246                         goto cleanup;
247                 }
248
249                 ch_free( a->a_vals[ 0 ].bv_val );
250                 ber_dupbv( &a->a_vals[ 0 ], &ml->sml_values[ 0 ] );
251                 if ( a->a_nvals ) {
252                         ch_free( a->a_nvals[ 0 ].bv_val );
253                         if ( ml->sml_nvalues && !BER_BVISNULL( &ml->sml_nvalues[ 0 ] ) ) {
254                                 ber_dupbv( &a->a_nvals[ 0 ], &ml->sml_nvalues[ 0 ] );
255                         } else {
256                                 ber_dupbv( &a->a_nvals[ 0 ], &ml->sml_values[ 0 ] );
257                         }
258                 }
259
260                 if ( strcmp( ml->sml_values[ 0 ].bv_val, "TRUE" ) == 0 ) {
261                         lmi->lmi_enabled = 1;
262                 } else if ( strcmp( ml->sml_values[ 0 ].bv_val, "FALSE" ) == 0 ) {
263                         lmi->lmi_enabled = 0;
264                 } else {
265                         assert( 0 );
266                 }
267         }
268
269         rs->sr_err = LDAP_SUCCESS;
270
271 cleanup:;
272         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
273
274         send_ldap_result( op, rs );
275         rs->sr_text = NULL;
276
277         return rs->sr_err;
278 }
279
280 static int
281 lastmod_op_func( Operation *op, SlapReply *rs )
282 {
283         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
284         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
285         Modifications           *ml;
286
287         if ( dn_match( &op->o_req_ndn, &lmi->lmi_e->e_nname ) ) {
288                 switch ( op->o_tag ) {
289                 case LDAP_REQ_SEARCH:
290                         if ( op->ors_scope != LDAP_SCOPE_BASE ) {
291                                 goto return_referral;
292                         }
293                         /* process */
294                         return lastmod_search( op, rs );
295
296                 case LDAP_REQ_COMPARE:
297                         return lastmod_compare( op, rs );
298
299                 case LDAP_REQ_EXTENDED:
300                         /* if write, reject; otherwise process */
301                         if ( exop_is_write( op )) {
302                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
303                                 rs->sr_text = "not allowed within namingContext";
304                                 goto return_error;
305                         }
306                         return lastmod_exop( op, rs );
307
308                 case LDAP_REQ_MODIFY:
309                         /* allow only changes to overlay status */
310                         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
311                                 if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_modifiersName ) != 0
312                                                 && ad_cmp( ml->sml_desc, slap_schema.si_ad_modifyTimestamp ) != 0
313                                                 && ad_cmp( ml->sml_desc, slap_schema.si_ad_entryCSN ) != 0
314                                                 && ad_cmp( ml->sml_desc, lastmod_schema.lms_ad_lastmodEnabled ) != 0 )
315                                 {
316                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
317                                         rs->sr_text = "not allowed within namingContext";
318                                         goto return_error;
319                                 }
320                         }
321                         return lastmod_modify( op, rs );
322
323                 default:
324                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
325                         rs->sr_text = "not allowed within namingContext";
326                         goto return_error;
327                 }
328         }
329
330         if ( dnIsSuffix( &op->o_req_ndn, &lmi->lmi_e->e_nname ) ) {
331                 goto return_referral;
332         }
333
334         return SLAP_CB_CONTINUE;
335
336 return_referral:;
337         op->o_bd->bd_info = (BackendInfo *)on->on_info;
338         rs->sr_ref = referral_rewrite( default_referral,
339                         NULL, &op->o_req_dn, op->ors_scope );
340
341         if ( !rs->sr_ref ) {
342                 rs->sr_ref = default_referral;
343         }
344         rs->sr_err = LDAP_REFERRAL;
345         send_ldap_result( op, rs );
346
347         if ( rs->sr_ref != default_referral ) {
348                 ber_bvarray_free( rs->sr_ref );
349         }
350         rs->sr_ref = NULL;
351
352         return -1;
353
354 return_error:;
355         op->o_bd->bd_info = (BackendInfo *)on->on_info;
356         send_ldap_result( op, rs );
357         rs->sr_text = NULL;
358
359         return -1;
360 }
361
362 static int
363 best_guess( Operation *op,
364                 struct berval *bv_entryCSN, struct berval *bv_nentryCSN,
365                 struct berval *bv_modifyTimestamp, struct berval *bv_nmodifyTimestamp,
366                 struct berval *bv_modifiersName, struct berval *bv_nmodifiersName )
367 {
368         if ( bv_entryCSN ) {
369                 char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
370                 struct berval   entryCSN;
371         
372                 entryCSN.bv_val = csnbuf;
373                 entryCSN.bv_len = sizeof( csnbuf );
374                 slap_get_csn( NULL, &entryCSN, 0 );
375
376                 ber_dupbv( bv_entryCSN, &entryCSN );
377                 ber_dupbv( bv_nentryCSN, &entryCSN );
378         }
379
380         if ( bv_modifyTimestamp ) {
381                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
382                 struct berval timestamp;
383                 time_t          currtime;
384
385                 /* best guess */
386 #if 0
387                 currtime = slap_get_time();
388 #endif
389                 /* maybe we better use the time the operation was initiated */
390                 currtime = op->o_time;
391
392                 timestamp.bv_val = tmbuf;
393                 timestamp.bv_len = sizeof(tmbuf);
394                 slap_timestamp( &currtime, &timestamp );
395
396                 ber_dupbv( bv_modifyTimestamp, &timestamp );
397                 ber_dupbv( bv_nmodifyTimestamp, bv_modifyTimestamp );
398         }
399
400         if ( bv_modifiersName ) {
401                 /* best guess */
402                 ber_dupbv( bv_modifiersName, &op->o_dn );
403                 ber_dupbv( bv_nmodifiersName, &op->o_ndn );
404         }
405
406         return 0;
407 }
408
409 static int
410 lastmod_update( Operation *op, SlapReply *rs )
411 {
412         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
413         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
414         Attribute               *a;
415         Modifications           *ml = NULL;
416         struct berval           bv_entryCSN = BER_BVNULL,
417                                 bv_nentryCSN = BER_BVNULL,
418                                 bv_modifyTimestamp = BER_BVNULL,
419                                 bv_nmodifyTimestamp = BER_BVNULL,
420                                 bv_modifiersName = BER_BVNULL,
421                                 bv_nmodifiersName = BER_BVNULL,
422                                 bv_name = BER_BVNULL,
423                                 bv_nname = BER_BVNULL;
424         enum lastmodType_e      lmt = LASTMOD_UNKNOWN;
425         Entry                   *e = NULL;
426         int                     rc = -1;
427
428         /* FIXME: timestamp? modifier? */
429         switch ( op->o_tag ) {
430         case LDAP_REQ_ADD:
431                 lmt = LASTMOD_ADD;
432                 e = op->ora_e;
433                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
434                 if ( a != NULL ) {
435                         ber_dupbv( &bv_entryCSN, &a->a_vals[0] );
436                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
437                                 ber_dupbv( &bv_nentryCSN, &a->a_nvals[0] );
438                         } else {
439                                 ber_dupbv( &bv_nentryCSN, &a->a_vals[0] );
440                         }
441                 }
442                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
443                 if ( a != NULL ) {
444                         ber_dupbv( &bv_modifyTimestamp, &a->a_vals[0] );
445                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
446                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_nvals[0] );
447                         } else {
448                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_vals[0] );
449                         }
450                 }
451                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
452                 if ( a != NULL ) {
453                         ber_dupbv( &bv_modifiersName, &a->a_vals[0] );
454                         ber_dupbv( &bv_nmodifiersName, &a->a_nvals[0] );
455                 }
456                 ber_dupbv( &bv_name, &e->e_name );
457                 ber_dupbv( &bv_nname, &e->e_nname );
458                 break;
459
460         case LDAP_REQ_DELETE:
461                 lmt = LASTMOD_DELETE;
462
463                 best_guess( op, &bv_entryCSN, &bv_nentryCSN,
464                                 &bv_modifyTimestamp, &bv_nmodifyTimestamp,
465                                 &bv_modifiersName, &bv_nmodifiersName );
466
467                 ber_dupbv( &bv_name, &op->o_req_dn );
468                 ber_dupbv( &bv_nname, &op->o_req_ndn );
469                 break;
470
471         case LDAP_REQ_EXTENDED:
472                 lmt = LASTMOD_EXOP;
473
474                 /* actually, password change is wrapped around a backend 
475                  * call to modify, so it never shows up as an exop... */
476                 best_guess( op, &bv_entryCSN, &bv_nentryCSN,
477                                 &bv_modifyTimestamp, &bv_nmodifyTimestamp,
478                                 &bv_modifiersName, &bv_nmodifiersName );
479
480                 ber_dupbv( &bv_name, &op->o_req_dn );
481                 ber_dupbv( &bv_nname, &op->o_req_ndn );
482                 break;
483
484         case LDAP_REQ_MODIFY:
485                 lmt = LASTMOD_MODIFY;
486                 rc = 3;
487
488                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
489                         if ( ad_cmp( ml->sml_desc , slap_schema.si_ad_modifiersName ) == 0 ) {
490                                 ber_dupbv( &bv_modifiersName, &ml->sml_values[0] );
491                                 ber_dupbv( &bv_nmodifiersName, &ml->sml_nvalues[0] );
492
493                                 rc--;
494                                 if ( !rc ) {
495                                         break;
496                                 }
497
498                         } else if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_entryCSN ) == 0 ) {
499                                 ber_dupbv( &bv_entryCSN, &ml->sml_values[0] );
500                                 if ( ml->sml_nvalues && !BER_BVISNULL( &ml->sml_nvalues[0] ) ) {
501                                         ber_dupbv( &bv_nentryCSN, &ml->sml_nvalues[0] );
502                                 } else {
503                                         ber_dupbv( &bv_nentryCSN, &ml->sml_values[0] );
504                                 }
505
506                                 rc --;
507                                 if ( !rc ) {
508                                         break;
509                                 }
510
511                         } else if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_modifyTimestamp ) == 0 ) {
512                                 ber_dupbv( &bv_modifyTimestamp, &ml->sml_values[0] );
513                                 if ( ml->sml_nvalues && !BER_BVISNULL( &ml->sml_nvalues[0] ) ) {
514                                         ber_dupbv( &bv_nmodifyTimestamp, &ml->sml_nvalues[0] );
515                                 } else {
516                                         ber_dupbv( &bv_nmodifyTimestamp, &ml->sml_values[0] );
517                                 }
518
519                                 rc --;
520                                 if ( !rc ) {
521                                         break;
522                                 }
523                         }
524                 }
525
526                 /* if rooted at global overlay, opattrs are not yet in place */
527                 if ( BER_BVISNULL( &bv_modifiersName ) ) {
528                         best_guess( op, NULL, NULL, NULL, NULL, &bv_modifiersName, &bv_nmodifiersName );
529                 }
530
531                 if ( BER_BVISNULL( &bv_entryCSN ) ) {
532                         best_guess( op, &bv_entryCSN, &bv_nentryCSN, NULL, NULL, NULL, NULL );
533                 }
534
535                 if ( BER_BVISNULL( &bv_modifyTimestamp ) ) {
536                         best_guess( op, NULL, NULL, &bv_modifyTimestamp, &bv_nmodifyTimestamp, NULL, NULL );
537                 }
538
539                 ber_dupbv( &bv_name, &op->o_req_dn );
540                 ber_dupbv( &bv_nname, &op->o_req_ndn );
541                 break;
542
543         case LDAP_REQ_MODRDN:
544                 lmt = LASTMOD_MODRDN;
545                 e = NULL;
546
547                 if ( op->orr_newSup && !BER_BVISNULL( op->orr_newSup ) ) {
548                         build_new_dn( &bv_name, op->orr_newSup, &op->orr_newrdn, NULL );
549                         build_new_dn( &bv_nname, op->orr_nnewSup, &op->orr_nnewrdn, NULL );
550
551                 } else {
552                         struct berval   pdn;
553
554                         dnParent( &op->o_req_dn, &pdn );
555                         build_new_dn( &bv_name, &pdn, &op->orr_newrdn, NULL );
556
557                         dnParent( &op->o_req_ndn, &pdn );
558                         build_new_dn( &bv_nname, &pdn, &op->orr_nnewrdn, NULL );
559                 }
560
561                 if ( on->on_info->oi_orig->bi_entry_get_rw ) {
562                         BackendInfo     *bi = op->o_bd->bd_info;
563                         int             rc;
564
565                         op->o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
566                         rc = (*op->o_bd->bd_info->bi_entry_get_rw)( op, &bv_name, NULL, NULL, 0, &e );
567                         if ( rc == LDAP_SUCCESS ) {
568                                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
569                                 if ( a != NULL ) {
570                                         ber_dupbv( &bv_modifiersName, &a->a_vals[0] );
571                                         ber_dupbv( &bv_nmodifiersName, &a->a_nvals[0] );
572                                 }
573                                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
574                                 if ( a != NULL ) {
575                                         ber_dupbv( &bv_entryCSN, &a->a_vals[0] );
576                                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
577                                                 ber_dupbv( &bv_nentryCSN, &a->a_nvals[0] );
578                                         } else {
579                                                 ber_dupbv( &bv_nentryCSN, &a->a_vals[0] );
580                                         }
581                                 }
582                                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
583                                 if ( a != NULL ) {
584                                         ber_dupbv( &bv_modifyTimestamp, &a->a_vals[0] );
585                                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
586                                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_nvals[0] );
587                                         } else {
588                                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_vals[0] );
589                                         }
590                                 }
591
592                                 assert( dn_match( &bv_name, &e->e_name ) );
593                                 assert( dn_match( &bv_nname, &e->e_nname ) );
594
595                                 (*op->o_bd->bd_info->bi_entry_release_rw)( op, e, 0 );
596                         }
597
598                         op->o_bd->bd_info = bi;
599
600                 }
601
602                 /* if !bi_entry_get_rw || bi_entry_get_rw failed for any reason... */
603                 if ( e == NULL ) {
604                         best_guess( op, &bv_entryCSN, &bv_nentryCSN,
605                                         &bv_modifyTimestamp, &bv_nmodifyTimestamp,
606                                         &bv_modifiersName, &bv_nmodifiersName );
607                 }
608
609                 break;
610
611         default:
612                 return -1;
613         }
614         
615         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
616
617 #if 0
618         fprintf( stderr, "### lastmodDN: %s %s\n", bv_name.bv_val, bv_nname.bv_val );
619 #endif
620
621         a = attr_find( lmi->lmi_e->e_attrs, lastmod_schema.lms_ad_lastmodDN );
622         if ( a == NULL ) {
623                 goto error_return;
624         }
625         ch_free( a->a_vals[0].bv_val );
626         a->a_vals[0] = bv_name;
627         ch_free( a->a_nvals[0].bv_val );
628         a->a_nvals[0] = bv_nname;
629
630 #if 0
631         fprintf( stderr, "### lastmodType: %s %s\n", lastmodType[ lmt ].bv_val, lastmodType[ lmt ].bv_val );
632 #endif
633
634         a = attr_find( lmi->lmi_e->e_attrs, lastmod_schema.lms_ad_lastmodType );
635         if ( a == NULL ) {
636                 goto error_return;
637         } 
638         ch_free( a->a_vals[0].bv_val );
639         ber_dupbv( &a->a_vals[0], &lastmodType[ lmt ] );
640         ch_free( a->a_nvals[0].bv_val );
641         ber_dupbv( &a->a_nvals[0], &lastmodType[ lmt ] );
642
643 #if 0
644         fprintf( stderr, "### modifiersName: %s %s\n", bv_modifiersName.bv_val, bv_nmodifiersName.bv_val );
645 #endif
646
647         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_modifiersName );
648         if ( a == NULL ) {
649                 goto error_return;
650         } 
651         ch_free( a->a_vals[0].bv_val );
652         a->a_vals[0] = bv_modifiersName;
653         ch_free( a->a_nvals[0].bv_val );
654         a->a_nvals[0] = bv_nmodifiersName;
655
656 #if 0
657         fprintf( stderr, "### modifyTimestamp: %s %s\n", bv_nmodifyTimestamp.bv_val, bv_modifyTimestamp.bv_val );
658 #endif
659
660         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_modifyTimestamp );
661         if ( a == NULL ) {
662                 goto error_return;
663         } 
664         ch_free( a->a_vals[0].bv_val );
665         a->a_vals[0] = bv_modifyTimestamp;
666         ch_free( a->a_nvals[0].bv_val );
667         a->a_nvals[0] = bv_nmodifyTimestamp;
668
669 #if 0
670         fprintf( stderr, "### entryCSN: %s %s\n", bv_nentryCSN.bv_val, bv_entryCSN.bv_val );
671 #endif
672
673         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_entryCSN );
674         if ( a == NULL ) {
675                 goto error_return;
676         } 
677         ch_free( a->a_vals[0].bv_val );
678         a->a_vals[0] = bv_entryCSN;
679         ch_free( a->a_nvals[0].bv_val );
680         a->a_nvals[0] = bv_nentryCSN;
681
682         rc = 0;
683
684 error_return:;
685         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
686         
687         return rc;
688 }
689
690 static int
691 lastmod_response( Operation *op, SlapReply *rs )
692 {
693         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
694         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
695
696         /* don't record failed operations */
697         switch ( rs->sr_err ) {
698         case LDAP_SUCCESS:
699                 /* FIXME: other cases? */
700                 break;
701
702         default:
703                 return SLAP_CB_CONTINUE;
704         }
705
706         /* record only write operations */
707         switch ( op->o_tag ) {
708         case LDAP_REQ_ADD:
709         case LDAP_REQ_MODIFY:
710         case LDAP_REQ_MODRDN:
711         case LDAP_REQ_DELETE:
712                 break;
713
714         case LDAP_REQ_EXTENDED:
715                 /* if write, process */
716                 if ( exop_is_write( op ))
717                         break;
718
719                 /* fall thru */
720         default:
721                 return SLAP_CB_CONTINUE;
722         }
723
724         /* skip if disabled */
725         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
726         if ( !lmi->lmi_enabled ) {
727                 ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
728                 return SLAP_CB_CONTINUE;
729         }
730         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
731
732         (void)lastmod_update( op, rs );
733
734         return SLAP_CB_CONTINUE;
735 }
736
737 static int
738 lastmod_db_init(
739         BackendDB *be
740 )
741 {
742         slap_overinst           *on = (slap_overinst *)be->bd_info;
743         lastmod_info_t          *lmi;
744
745         if ( lastmod_schema.lms_oc_lastmod == NULL ) {
746                 int             i;
747                 const char      *text;
748
749                 /* schema integration */
750                 for ( i = 0; mat[i].schema; i++ ) {
751                         int                     code;
752                         AttributeDescription    **ad =
753                                 ((AttributeDescription **)&(((char *)&lastmod_schema)[mat[i].offset]));
754                         ad[0] = NULL;
755
756                         code = register_at( mat[i].schema, ad, 0 );
757                         if ( code ) {
758                                 Debug( LDAP_DEBUG_ANY,
759                                         "lastmod_init: register_at failed\n", 0, 0, 0 );
760                                 return -1;
761                         }
762                         (*ad)->ad_type->sat_flags |= mat[i].flags;
763                 }
764
765                 for ( i = 0; moc[i].schema; i++ ) {
766                         int                     code;
767                         ObjectClass             **Oc =
768                                 ((ObjectClass **)&(((char *)&lastmod_schema)[moc[i].offset]));
769         
770                         code = register_oc( moc[i].schema, Oc, 0 );
771                         if ( code ) {
772                                 Debug( LDAP_DEBUG_ANY,
773                                         "lastmod_init: register_oc failed\n", 0, 0, 0 );
774                                 return -1;
775                         }
776                         (*Oc)->soc_flags |= moc[i].flags;
777                 }
778         }
779
780         lmi = (lastmod_info_t *)ch_malloc( sizeof( lastmod_info_t ) );
781
782         memset( lmi, 0, sizeof( lastmod_info_t ) );
783         lmi->lmi_enabled = 1;
784         
785         on->on_bi.bi_private = lmi;
786
787         return 0;
788 }
789
790 static int
791 lastmod_db_config(
792         BackendDB       *be,
793         const char      *fname,
794         int             lineno,
795         int             argc,
796         char    **argv
797 )
798 {
799         slap_overinst           *on = (slap_overinst *)be->bd_info;
800         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
801
802         if ( strcasecmp( argv[ 0 ], "lastmod-rdnvalue" ) == 0 ) {
803                 if ( lmi->lmi_rdnvalue.bv_val ) {
804                         /* already defined! */
805                         ch_free( lmi->lmi_rdnvalue.bv_val );
806                 }
807
808                 ber_str2bv( argv[ 1 ], 0, 1, &lmi->lmi_rdnvalue );
809
810         } else if ( strcasecmp( argv[ 0 ], "lastmod-enabled" ) == 0 ) {
811                 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
812                         lmi->lmi_enabled = 1;
813
814                 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
815                         lmi->lmi_enabled = 0;
816
817                 } else {
818                         return -1;
819                 }
820
821         } else {
822                 return SLAP_CONF_UNKNOWN;
823         }
824
825         return 0;
826 }
827
828 static int
829 lastmod_db_open(
830         BackendDB *be
831 )
832 {
833         slap_overinst   *on = (slap_overinst *) be->bd_info;
834         lastmod_info_t  *lmi = (lastmod_info_t *)on->on_bi.bi_private;
835         char            buf[ 8192 ];
836         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
837
838         char                    csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
839         struct berval           entryCSN;
840         struct berval timestamp;
841
842         if ( !SLAP_LASTMOD( be ) ) {
843                 fprintf( stderr, "set \"lastmod on\" to make this overlay effective\n" );
844                 return -1;
845         }
846
847         /*
848          * Start
849          */
850         timestamp.bv_val = tmbuf;
851         timestamp.bv_len = sizeof(tmbuf);
852         slap_timestamp( &starttime, &timestamp );
853
854         entryCSN.bv_val = csnbuf;
855         entryCSN.bv_len = sizeof( csnbuf );
856         slap_get_csn( NULL, &entryCSN, 0 );
857
858         if ( BER_BVISNULL( &lmi->lmi_rdnvalue ) ) {
859                 ber_str2bv( "Lastmod", 0, 1, &lmi->lmi_rdnvalue );
860         }
861
862         snprintf( buf, sizeof( buf ),
863                         "dn: cn=%s%s%s\n"
864                         "objectClass: %s\n"
865                         "structuralObjectClass: %s\n"
866                         "cn: %s\n"
867                         "description: This object contains the last modification to this database\n"
868                         "%s: cn=%s%s%s\n"
869                         "%s: %s\n"
870                         "%s: %s\n"
871                         "createTimestamp: %s\n"
872                         "creatorsName: %s\n"
873                         "entryCSN: %s\n"
874                         "modifyTimestamp: %s\n"
875                         "modifiersName: %s\n"
876                         "hasSubordinates: FALSE\n",
877                         lmi->lmi_rdnvalue.bv_val, BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ? "" : ",", be->be_suffix[ 0 ].bv_val,
878                         lastmod_schema.lms_oc_lastmod->soc_cname.bv_val,
879                         lastmod_schema.lms_oc_lastmod->soc_cname.bv_val,
880                         lmi->lmi_rdnvalue.bv_val,
881                         lastmod_schema.lms_ad_lastmodDN->ad_cname.bv_val,
882                                 lmi->lmi_rdnvalue.bv_val, BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ? "" : ",", be->be_suffix[ 0 ].bv_val,
883                         lastmod_schema.lms_ad_lastmodType->ad_cname.bv_val, lastmodType[ LASTMOD_ADD ].bv_val,
884                         lastmod_schema.lms_ad_lastmodEnabled->ad_cname.bv_val, lmi->lmi_enabled ? "TRUE" : "FALSE",
885                         tmbuf,
886                         BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val,
887                         entryCSN.bv_val,
888                         tmbuf,
889                         BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val );
890
891 #if 0
892         fprintf( stderr, "# entry:\n%s\n", buf );
893 #endif
894
895         lmi->lmi_e = str2entry( buf );
896         if ( lmi->lmi_e == NULL ) {
897                 return -1;
898         }
899
900         ldap_pvt_thread_mutex_init( &lmi->lmi_entry_mutex );
901
902         return 0;
903 }
904
905 static int
906 lastmod_db_destroy(
907         BackendDB *be
908 )
909 {
910         slap_overinst   *on = (slap_overinst *)be->bd_info;
911         lastmod_info_t  *lmi = (lastmod_info_t *)on->on_bi.bi_private;
912
913         if ( lmi ) {
914                 if ( !BER_BVISNULL( &lmi->lmi_rdnvalue ) ) {
915                         ch_free( lmi->lmi_rdnvalue.bv_val );
916                 }
917
918                 if ( lmi->lmi_e ) {
919                         entry_free( lmi->lmi_e );
920
921                         ldap_pvt_thread_mutex_destroy( &lmi->lmi_entry_mutex );
922                 }
923
924                 ch_free( lmi );
925         }
926
927         return 0;
928 }
929
930 /* This overlay is set up for dynamic loading via moduleload. For static
931  * configuration, you'll need to arrange for the slap_overinst to be
932  * initialized and registered by some other function inside slapd.
933  */
934
935 static slap_overinst            lastmod;
936
937 int
938 lastmod_initialize()
939 {
940         lastmod.on_bi.bi_type = "lastmod";
941         lastmod.on_bi.bi_db_init = lastmod_db_init;
942         lastmod.on_bi.bi_db_config = lastmod_db_config;
943         lastmod.on_bi.bi_db_destroy = lastmod_db_destroy;
944         lastmod.on_bi.bi_db_open = lastmod_db_open;
945
946         lastmod.on_bi.bi_op_add = lastmod_op_func;
947         lastmod.on_bi.bi_op_compare = lastmod_op_func;
948         lastmod.on_bi.bi_op_delete = lastmod_op_func;
949         lastmod.on_bi.bi_op_modify = lastmod_op_func;
950         lastmod.on_bi.bi_op_modrdn = lastmod_op_func;
951         lastmod.on_bi.bi_op_search = lastmod_op_func;
952         lastmod.on_bi.bi_extended = lastmod_op_func;
953
954         lastmod.on_response = lastmod_response;
955
956         return overlay_register( &lastmod );
957 }
958
959 #if SLAPD_OVER_LASTMOD == SLAPD_MOD_DYNAMIC
960 int
961 init_module( int argc, char *argv[] )
962 {
963         return lastmod_initialize();
964 }
965 #endif /* SLAPD_OVER_LASTMOD == SLAPD_MOD_DYNAMIC */
966
967 #endif /* defined(SLAPD_OVER_LASTMOD) */