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