]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/lastmod.c
a645f7d8b49eb425bf22cdc6d7e69a6e21d5b120
[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         unsigned                i;
287         Modifications           *ml;
288
289         if ( dn_match( &op->o_req_ndn, &lmi->lmi_e->e_nname ) ) {
290                 switch ( op->o_tag ) {
291                 case LDAP_REQ_SEARCH:
292                         if ( op->ors_scope != LDAP_SCOPE_BASE ) {
293                                 goto return_referral;
294                         }
295                         /* process */
296                         return lastmod_search( op, rs );
297
298                 case LDAP_REQ_COMPARE:
299                         return lastmod_compare( op, rs );
300
301                 case LDAP_REQ_EXTENDED:
302                         /* if write, reject; otherwise process */
303                         if ( exop_is_write( op )) {
304                                 rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
305                                 rs->sr_text = "not allowed within namingContext";
306                                 goto return_error;
307                         }
308                         return lastmod_exop( op, rs );
309
310                 case LDAP_REQ_MODIFY:
311                         /* allow only changes to overlay status */
312                         for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
313                                 if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_modifiersName ) != 0
314                                                 && ad_cmp( ml->sml_desc, slap_schema.si_ad_modifyTimestamp ) != 0
315                                                 && ad_cmp( ml->sml_desc, slap_schema.si_ad_entryCSN ) != 0
316                                                 && ad_cmp( ml->sml_desc, lastmod_schema.lms_ad_lastmodEnabled ) != 0 )
317                                 {
318                                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
319                                         rs->sr_text = "not allowed within namingContext";
320                                         goto return_error;
321                                 }
322                         }
323                         return lastmod_modify( op, rs );
324
325                 default:
326                         rs->sr_err = LDAP_UNWILLING_TO_PERFORM;
327                         rs->sr_text = "not allowed within namingContext";
328                         goto return_error;
329                 }
330         }
331
332         if ( dnIsSuffix( &op->o_req_ndn, &lmi->lmi_e->e_nname ) ) {
333                 goto return_referral;
334         }
335
336         return SLAP_CB_CONTINUE;
337
338 return_referral:;
339         op->o_bd->bd_info = (BackendInfo *)on->on_info;
340         rs->sr_ref = referral_rewrite( default_referral,
341                         NULL, &op->o_req_dn, op->ors_scope );
342
343         if ( !rs->sr_ref ) {
344                 rs->sr_ref = default_referral;
345         }
346         rs->sr_err = LDAP_REFERRAL;
347         send_ldap_result( op, rs );
348
349         if ( rs->sr_ref != default_referral ) {
350                 ber_bvarray_free( rs->sr_ref );
351         }
352         rs->sr_ref = NULL;
353
354         return -1;
355
356 return_error:;
357         op->o_bd->bd_info = (BackendInfo *)on->on_info;
358         send_ldap_result( op, rs );
359         rs->sr_text = NULL;
360
361         return -1;
362 }
363
364 static int
365 best_guess( Operation *op,
366                 struct berval *bv_entryCSN, struct berval *bv_nentryCSN,
367                 struct berval *bv_modifyTimestamp, struct berval *bv_nmodifyTimestamp,
368                 struct berval *bv_modifiersName, struct berval *bv_nmodifiersName )
369 {
370         if ( bv_entryCSN ) {
371                 char            csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
372                 struct berval   entryCSN;
373         
374                 slap_get_csn( NULL, csnbuf, sizeof(csnbuf), &entryCSN, 0 );
375
376                 ber_dupbv( bv_entryCSN, &entryCSN );
377                 ber_dupbv( bv_nentryCSN, &entryCSN );
378         }
379
380         if ( bv_modifyTimestamp ) {
381                 struct tm       *tm;
382 #ifdef HAVE_GMTIME_R
383                 struct tm       tm_buf;
384 #endif
385                 char            tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
386                 time_t          currtime;
387
388                 /* best guess */
389 #if 0
390                 currtime = slap_get_time();
391 #endif
392                 /* maybe we better use the time the operation was initiated */
393                 currtime = op->o_time;
394
395 #ifndef HAVE_GMTIME_R
396                 ldap_pvt_thread_mutex_lock( &gmtime_mutex );
397                 tm = gmtime( &currtime );
398 #else /* HAVE_GMTIME_R */
399                 tm = gmtime_r( &currtime, &tm_buf );
400 #endif /* HAVE_GMTIME_R */
401                 lutil_gentime( tmbuf, sizeof( tmbuf ), tm );
402 #ifndef HAVE_GMTIME_R
403                 ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
404 #endif
405
406                 ber_str2bv( tmbuf, 0, 1, bv_modifyTimestamp );
407                 ber_dupbv( bv_nmodifyTimestamp, bv_modifyTimestamp );
408         }
409
410         if ( bv_modifiersName ) {
411                 /* best guess */
412                 ber_dupbv( bv_modifiersName, &op->o_dn );
413                 ber_dupbv( bv_nmodifiersName, &op->o_ndn );
414         }
415
416         return 0;
417 }
418
419 static int
420 lastmod_update( Operation *op, SlapReply *rs )
421 {
422         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
423         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
424         Attribute               *a;
425         Modifications           *ml = NULL;
426         struct berval           bv_entryCSN = BER_BVNULL,
427                                 bv_nentryCSN = BER_BVNULL,
428                                 bv_modifyTimestamp = BER_BVNULL,
429                                 bv_nmodifyTimestamp = BER_BVNULL,
430                                 bv_modifiersName = BER_BVNULL,
431                                 bv_nmodifiersName = BER_BVNULL,
432                                 bv_name = BER_BVNULL,
433                                 bv_nname = BER_BVNULL;
434         enum lastmodType_e      lmt = LASTMOD_UNKNOWN;
435         Entry                   *e = NULL;
436         int                     rc = -1;
437
438         /* FIXME: timestamp? modifier? */
439         switch ( op->o_tag ) {
440         case LDAP_REQ_ADD:
441                 lmt = LASTMOD_ADD;
442                 e = op->ora_e;
443                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
444                 if ( a != NULL ) {
445                         ber_dupbv( &bv_entryCSN, &a->a_vals[0] );
446                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
447                                 ber_dupbv( &bv_nentryCSN, &a->a_nvals[0] );
448                         } else {
449                                 ber_dupbv( &bv_nentryCSN, &a->a_vals[0] );
450                         }
451                 }
452                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
453                 if ( a != NULL ) {
454                         ber_dupbv( &bv_modifyTimestamp, &a->a_vals[0] );
455                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
456                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_nvals[0] );
457                         } else {
458                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_vals[0] );
459                         }
460                 }
461                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
462                 if ( a != NULL ) {
463                         ber_dupbv( &bv_modifiersName, &a->a_vals[0] );
464                         ber_dupbv( &bv_nmodifiersName, &a->a_nvals[0] );
465                 }
466                 ber_dupbv( &bv_name, &e->e_name );
467                 ber_dupbv( &bv_nname, &e->e_nname );
468                 break;
469
470         case LDAP_REQ_DELETE:
471                 lmt = LASTMOD_DELETE;
472
473                 best_guess( op, &bv_entryCSN, &bv_nentryCSN,
474                                 &bv_modifyTimestamp, &bv_nmodifyTimestamp,
475                                 &bv_modifiersName, &bv_nmodifiersName );
476
477                 ber_dupbv( &bv_name, &op->o_req_dn );
478                 ber_dupbv( &bv_nname, &op->o_req_ndn );
479                 break;
480
481         case LDAP_REQ_EXTENDED:
482                 lmt = LASTMOD_EXOP;
483
484                 /* actually, password change is wrapped around a backend 
485                  * call to modify, so it never shows up as an exop... */
486                 best_guess( op, &bv_entryCSN, &bv_nentryCSN,
487                                 &bv_modifyTimestamp, &bv_nmodifyTimestamp,
488                                 &bv_modifiersName, &bv_nmodifiersName );
489
490                 ber_dupbv( &bv_name, &op->o_req_dn );
491                 ber_dupbv( &bv_nname, &op->o_req_ndn );
492                 break;
493
494         case LDAP_REQ_MODIFY:
495                 lmt = LASTMOD_MODIFY;
496                 rc = 3;
497
498                 for ( ml = op->orm_modlist; ml; ml = ml->sml_next ) {
499                         if ( ad_cmp( ml->sml_desc , slap_schema.si_ad_modifiersName ) == 0 ) {
500                                 ber_dupbv( &bv_modifiersName, &ml->sml_values[0] );
501                                 ber_dupbv( &bv_nmodifiersName, &ml->sml_nvalues[0] );
502
503                                 rc--;
504                                 if ( !rc ) {
505                                         break;
506                                 }
507
508                         } else if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_entryCSN ) == 0 ) {
509                                 ber_dupbv( &bv_entryCSN, &ml->sml_values[0] );
510                                 if ( ml->sml_nvalues && !BER_BVISNULL( &ml->sml_nvalues[0] ) ) {
511                                         ber_dupbv( &bv_nentryCSN, &ml->sml_nvalues[0] );
512                                 } else {
513                                         ber_dupbv( &bv_nentryCSN, &ml->sml_values[0] );
514                                 }
515
516                                 rc --;
517                                 if ( !rc ) {
518                                         break;
519                                 }
520
521                         } else if ( ad_cmp( ml->sml_desc, slap_schema.si_ad_modifyTimestamp ) == 0 ) {
522                                 ber_dupbv( &bv_modifyTimestamp, &ml->sml_values[0] );
523                                 if ( ml->sml_nvalues && !BER_BVISNULL( &ml->sml_nvalues[0] ) ) {
524                                         ber_dupbv( &bv_nmodifyTimestamp, &ml->sml_nvalues[0] );
525                                 } else {
526                                         ber_dupbv( &bv_nmodifyTimestamp, &ml->sml_values[0] );
527                                 }
528
529                                 rc --;
530                                 if ( !rc ) {
531                                         break;
532                                 }
533                         }
534                 }
535
536                 /* if rooted at global overlay, opattrs are not yet in place */
537                 if ( BER_BVISNULL( &bv_modifiersName ) ) {
538                         best_guess( op, NULL, NULL, NULL, NULL, &bv_modifiersName, &bv_nmodifiersName );
539                 }
540
541                 if ( BER_BVISNULL( &bv_entryCSN ) ) {
542                         best_guess( op, &bv_entryCSN, &bv_nentryCSN, NULL, NULL, NULL, NULL );
543                 }
544
545                 if ( BER_BVISNULL( &bv_modifyTimestamp ) ) {
546                         best_guess( op, NULL, NULL, &bv_modifyTimestamp, &bv_nmodifyTimestamp, NULL, NULL );
547                 }
548
549                 ber_dupbv( &bv_name, &op->o_req_dn );
550                 ber_dupbv( &bv_nname, &op->o_req_ndn );
551                 break;
552
553         case LDAP_REQ_MODRDN:
554                 lmt = LASTMOD_MODRDN;
555                 e = NULL;
556
557                 if ( op->orr_newSup && !BER_BVISNULL( op->orr_newSup ) ) {
558                         build_new_dn( &bv_name, op->orr_newSup, &op->orr_newrdn, NULL );
559                         build_new_dn( &bv_nname, op->orr_nnewSup, &op->orr_nnewrdn, NULL );
560
561                 } else {
562                         struct berval   pdn;
563
564                         dnParent( &op->o_req_dn, &pdn );
565                         build_new_dn( &bv_name, &pdn, &op->orr_newrdn, NULL );
566
567                         dnParent( &op->o_req_ndn, &pdn );
568                         build_new_dn( &bv_nname, &pdn, &op->orr_nnewrdn, NULL );
569                 }
570
571                 if ( on->on_info->oi_orig->bi_entry_get_rw ) {
572                         BackendInfo     *bi = op->o_bd->bd_info;
573                         int             rc;
574
575                         op->o_bd->bd_info = (BackendInfo *)on->on_info->oi_orig;
576                         rc = (*op->o_bd->bd_info->bi_entry_get_rw)( op, &bv_name, NULL, NULL, 0, &e );
577                         if ( rc == LDAP_SUCCESS ) {
578                                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifiersName );
579                                 if ( a != NULL ) {
580                                         ber_dupbv( &bv_modifiersName, &a->a_vals[0] );
581                                         ber_dupbv( &bv_nmodifiersName, &a->a_nvals[0] );
582                                 }
583                                 a = attr_find( e->e_attrs, slap_schema.si_ad_entryCSN );
584                                 if ( a != NULL ) {
585                                         ber_dupbv( &bv_entryCSN, &a->a_vals[0] );
586                                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
587                                                 ber_dupbv( &bv_nentryCSN, &a->a_nvals[0] );
588                                         } else {
589                                                 ber_dupbv( &bv_nentryCSN, &a->a_vals[0] );
590                                         }
591                                 }
592                                 a = attr_find( e->e_attrs, slap_schema.si_ad_modifyTimestamp );
593                                 if ( a != NULL ) {
594                                         ber_dupbv( &bv_modifyTimestamp, &a->a_vals[0] );
595                                         if ( a->a_nvals && !BER_BVISNULL( &a->a_nvals[0] ) ) {
596                                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_nvals[0] );
597                                         } else {
598                                                 ber_dupbv( &bv_nmodifyTimestamp, &a->a_vals[0] );
599                                         }
600                                 }
601
602                                 assert( dn_match( &bv_name, &e->e_name ) );
603                                 assert( dn_match( &bv_nname, &e->e_nname ) );
604
605                                 (*op->o_bd->bd_info->bi_entry_release_rw)( op, e, 0 );
606                         }
607
608                         op->o_bd->bd_info = bi;
609
610                 }
611
612                 /* if !bi_entry_get_rw || bi_entry_get_rw failed for any reason... */
613                 if ( e == NULL ) {
614                         best_guess( op, &bv_entryCSN, &bv_nentryCSN,
615                                         &bv_modifyTimestamp, &bv_nmodifyTimestamp,
616                                         &bv_modifiersName, &bv_nmodifiersName );
617                 }
618
619                 break;
620
621         default:
622                 return -1;
623         }
624         
625         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
626
627 #if 0
628         fprintf( stderr, "### lastmodDN: %s %s\n", bv_name.bv_val, bv_nname.bv_val );
629 #endif
630
631         a = attr_find( lmi->lmi_e->e_attrs, lastmod_schema.lms_ad_lastmodDN );
632         if ( a == NULL ) {
633                 goto error_return;
634         }
635         ch_free( a->a_vals[0].bv_val );
636         a->a_vals[0] = bv_name;
637         ch_free( a->a_nvals[0].bv_val );
638         a->a_nvals[0] = bv_nname;
639
640 #if 0
641         fprintf( stderr, "### lastmodType: %s %s\n", lastmodType[ lmt ].bv_val, lastmodType[ lmt ].bv_val );
642 #endif
643
644         a = attr_find( lmi->lmi_e->e_attrs, lastmod_schema.lms_ad_lastmodType );
645         if ( a == NULL ) {
646                 goto error_return;
647         } 
648         ch_free( a->a_vals[0].bv_val );
649         ber_dupbv( &a->a_vals[0], &lastmodType[ lmt ] );
650         ch_free( a->a_nvals[0].bv_val );
651         ber_dupbv( &a->a_nvals[0], &lastmodType[ lmt ] );
652
653 #if 0
654         fprintf( stderr, "### modifiersName: %s %s\n", bv_modifiersName.bv_val, bv_nmodifiersName.bv_val );
655 #endif
656
657         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_modifiersName );
658         if ( a == NULL ) {
659                 goto error_return;
660         } 
661         ch_free( a->a_vals[0].bv_val );
662         a->a_vals[0] = bv_modifiersName;
663         ch_free( a->a_nvals[0].bv_val );
664         a->a_nvals[0] = bv_nmodifiersName;
665
666 #if 0
667         fprintf( stderr, "### modifyTimestamp: %s %s\n", bv_nmodifyTimestamp.bv_val, bv_modifyTimestamp.bv_val );
668 #endif
669
670         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_modifyTimestamp );
671         if ( a == NULL ) {
672                 goto error_return;
673         } 
674         ch_free( a->a_vals[0].bv_val );
675         a->a_vals[0] = bv_modifyTimestamp;
676         ch_free( a->a_nvals[0].bv_val );
677         a->a_nvals[0] = bv_nmodifyTimestamp;
678
679 #if 0
680         fprintf( stderr, "### entryCSN: %s %s\n", bv_nentryCSN.bv_val, bv_entryCSN.bv_val );
681 #endif
682
683         a = attr_find( lmi->lmi_e->e_attrs, slap_schema.si_ad_entryCSN );
684         if ( a == NULL ) {
685                 goto error_return;
686         } 
687         ch_free( a->a_vals[0].bv_val );
688         a->a_vals[0] = bv_entryCSN;
689         ch_free( a->a_nvals[0].bv_val );
690         a->a_nvals[0] = bv_nentryCSN;
691
692         rc = 0;
693
694 error_return:;
695         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
696         
697         return rc;
698 }
699
700 static int
701 lastmod_response( Operation *op, SlapReply *rs )
702 {
703         slap_overinst           *on = (slap_overinst *)op->o_bd->bd_info;
704         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
705         unsigned int            i;
706
707         /* don't record failed operations */
708         switch ( rs->sr_err ) {
709         case LDAP_SUCCESS:
710                 /* FIXME: other cases? */
711                 break;
712
713         default:
714                 return SLAP_CB_CONTINUE;
715         }
716
717         /* record only write operations */
718         switch ( op->o_tag ) {
719         case LDAP_REQ_ADD:
720         case LDAP_REQ_MODIFY:
721         case LDAP_REQ_MODRDN:
722         case LDAP_REQ_DELETE:
723                 break;
724
725         case LDAP_REQ_EXTENDED:
726                 /* if write, process */
727                 if ( exop_is_write( op ))
728                         break;
729
730                 /* fall thru */
731         default:
732                 return SLAP_CB_CONTINUE;
733         }
734
735         /* skip if disabled */
736         ldap_pvt_thread_mutex_lock( &lmi->lmi_entry_mutex );
737         if ( !lmi->lmi_enabled ) {
738                 ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
739                 return SLAP_CB_CONTINUE;
740         }
741         ldap_pvt_thread_mutex_unlock( &lmi->lmi_entry_mutex );
742
743 process:;
744         (void)lastmod_update( op, rs );
745
746         return SLAP_CB_CONTINUE;
747 }
748
749 static int
750 lastmod_db_init(
751         BackendDB *be
752 )
753 {
754         slap_overinst           *on = (slap_overinst *)be->bd_info;
755         lastmod_info_t          *lmi;
756
757         if ( lastmod_schema.lms_oc_lastmod == NULL ) {
758                 int             i;
759                 const char      *text;
760
761                 /* schema integration */
762                 for ( i = 0; mat[i].name; i++ ) {
763                         LDAPAttributeType       *at;
764                         int                     code;
765                         const char              *err;
766                         AttributeDescription    **ad;
767         
768                         at = ldap_str2attributetype( mat[i].schema, &code,
769                                 &err, LDAP_SCHEMA_ALLOW_ALL );
770                         if ( !at ) {
771                                 Debug( LDAP_DEBUG_ANY, "lastmod_init: "
772                                         "in AttributeType '%s' %s before %s\n",
773                                         mat[i].name, ldap_scherr2str(code), err );
774                                 return -1;
775                         }
776         
777                         if ( at->at_oid == NULL ) {
778                                 Debug( LDAP_DEBUG_ANY, "lastmod_init: "
779                                         "null OID for attributeType '%s'\n",
780                                         mat[i].name, 0, 0 );
781                                 return -1;
782                         }
783         
784                         code = at_add(at, 0, NULL, &err);
785                         if ( code ) {
786                                 Debug( LDAP_DEBUG_ANY, "lastmod_init: "
787                                         "%s in attributeType '%s'\n",
788                                         scherr2str(code), mat[i].name, 0 );
789                                 return -1;
790                         }
791                         ldap_memfree(at);
792         
793                         ad = ((AttributeDescription **)&(((char *)&lastmod_schema)[mat[i].offset]));
794                         ad[0] = NULL;
795                         if ( slap_str2ad( mat[i].name, ad, &text ) ) {
796                                 Debug( LDAP_DEBUG_ANY,
797                                         "lastmod_init: %s\n", text, 0, 0 );
798                                 return -1;
799                         }
800         
801                         (*ad)->ad_type->sat_flags |= mat[i].flags;
802                 }
803
804                 for ( i = 0; moc[i].name; i++ ) {
805                         LDAPObjectClass         *oc;
806                         int                     code;
807                         const char              *err;
808                         ObjectClass             *Oc;
809         
810                         oc = ldap_str2objectclass(moc[i].schema, &code, &err,
811                                         LDAP_SCHEMA_ALLOW_ALL );
812                         if ( !oc ) {
813                                 Debug( LDAP_DEBUG_ANY,
814                                         "unable to parse lastmod objectClass '%s': "
815                                         "%s before %s\n" , moc[i].name,
816                                         ldap_scherr2str(code), err );
817                                 return -1;
818                         }
819
820                         if ( oc->oc_oid == NULL ) {
821                                 Debug( LDAP_DEBUG_ANY,
822                                         "objectClass '%s' has no OID\n" ,
823                                         moc[i].name, 0, 0 );
824                                 return -1;
825                         }
826
827                         code = oc_add(oc, 0, NULL, &err);
828                         if ( code ) {
829                                 Debug( LDAP_DEBUG_ANY,
830                                         "objectClass '%s': %s \"%s\"\n" ,
831                                         moc[i].name, scherr2str(code), err );
832                                 return -1;
833                         }
834         
835                         ldap_memfree(oc);
836         
837                         Oc = oc_find( moc[i].name );
838                         if ( Oc == NULL ) {
839                                 Debug( LDAP_DEBUG_ANY, "lastmod_init: "
840                                                 "unable to find objectClass %s "
841                                                 "(just added)\n", moc[i].name, 0, 0 );
842                                 return -1;
843                         }
844
845                         Oc->soc_flags |= moc[i].flags;
846
847                         ((ObjectClass **)&(((char *)&lastmod_schema)[moc[i].offset]))[0] = Oc;
848                 }
849         }
850
851         lmi = (lastmod_info_t *)ch_malloc( sizeof( lastmod_info_t ) );
852
853         memset( lmi, 0, sizeof( lastmod_info_t ) );
854         lmi->lmi_enabled = 1;
855         
856         on->on_bi.bi_private = lmi;
857
858         return 0;
859 }
860
861 static int
862 lastmod_db_config(
863         BackendDB       *be,
864         const char      *fname,
865         int             lineno,
866         int             argc,
867         char    **argv
868 )
869 {
870         slap_overinst           *on = (slap_overinst *)be->bd_info;
871         lastmod_info_t          *lmi = (lastmod_info_t *)on->on_bi.bi_private;
872
873         if ( strcasecmp( argv[ 0 ], "lastmod-rdnvalue" ) == 0 ) {
874                 if ( lmi->lmi_rdnvalue.bv_val ) {
875                         /* already defined! */
876                         ch_free( lmi->lmi_rdnvalue.bv_val );
877                 }
878
879                 ber_str2bv( argv[ 1 ], 0, 1, &lmi->lmi_rdnvalue );
880
881         } else if ( strcasecmp( argv[ 0 ], "lastmod-enabled" ) == 0 ) {
882                 if ( strcasecmp( argv[ 1 ], "yes" ) == 0 ) {
883                         lmi->lmi_enabled = 1;
884
885                 } else if ( strcasecmp( argv[ 1 ], "no" ) == 0 ) {
886                         lmi->lmi_enabled = 0;
887
888                 } else {
889                         return -1;
890                 }
891
892         } else {
893                 return SLAP_CONF_UNKNOWN;
894         }
895
896         return 0;
897 }
898
899 static int
900 lastmod_db_open(
901         BackendDB *be
902 )
903 {
904         slap_overinst   *on = (slap_overinst *) be->bd_info;
905         lastmod_info_t  *lmi = (lastmod_info_t *)on->on_bi.bi_private;
906         char            buf[ 8192 ];
907         struct tm               *tms;
908 #ifdef HAVE_GMTIME_R
909         struct tm               tm_buf;
910 #endif
911         static char             tmbuf[ LDAP_LUTIL_GENTIME_BUFSIZE ];
912
913         char                    csnbuf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
914         struct berval           entryCSN;
915
916         if ( !SLAP_LASTMOD( be ) ) {
917                 fprintf( stderr, "set \"lastmod on\" to make this overlay effective\n" );
918                 return -1;
919         }
920
921         /*
922          * Start
923          */
924 #ifndef HAVE_GMTIME_R
925         ldap_pvt_thread_mutex_lock( &gmtime_mutex );
926         tms = gmtime( &starttime );
927 #else /* HAVE_GMTIME_R */
928         tms = gmtime_r( &starttime, &tm_buf );
929 #endif /* HAVE_GMTIME_R */
930         lutil_gentime( tmbuf, sizeof(tmbuf), tms );
931 #ifndef HAVE_GMTIME_R
932         ldap_pvt_thread_mutex_unlock( &gmtime_mutex );
933 #endif
934
935         slap_get_csn( NULL, csnbuf, sizeof(csnbuf), &entryCSN, 0 );
936
937         if ( BER_BVISNULL( &lmi->lmi_rdnvalue ) ) {
938                 ber_str2bv( "Lastmod", 0, 1, &lmi->lmi_rdnvalue );
939         }
940
941         snprintf( buf, sizeof( buf ),
942                         "dn: cn=%s%s%s\n"
943                         "objectClass: %s\n"
944                         "structuralObjectClass: %s\n"
945                         "cn: %s\n"
946                         "description: This object contains the last modification to this database\n"
947                         "%s: cn=%s%s%s\n"
948                         "%s: %s\n"
949                         "%s: %s\n"
950                         "createTimestamp: %s\n"
951                         "creatorsName: %s\n"
952                         "entryCSN: %s\n"
953                         "modifyTimestamp: %s\n"
954                         "modifiersName: %s\n"
955                         "hasSubordinates: FALSE\n",
956                         lmi->lmi_rdnvalue.bv_val, BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ? "" : ",", be->be_suffix[ 0 ].bv_val,
957                         lastmod_schema.lms_oc_lastmod->soc_cname.bv_val,
958                         lastmod_schema.lms_oc_lastmod->soc_cname.bv_val,
959                         lmi->lmi_rdnvalue.bv_val,
960                         lastmod_schema.lms_ad_lastmodDN->ad_cname.bv_val,
961                                 lmi->lmi_rdnvalue.bv_val, BER_BVISEMPTY( &be->be_suffix[ 0 ] ) ? "" : ",", be->be_suffix[ 0 ].bv_val,
962                         lastmod_schema.lms_ad_lastmodType->ad_cname.bv_val, lastmodType[ LASTMOD_ADD ].bv_val,
963                         lastmod_schema.lms_ad_lastmodEnabled->ad_cname.bv_val, lmi->lmi_enabled ? "TRUE" : "FALSE",
964                         tmbuf,
965                         BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val,
966                         entryCSN.bv_val,
967                         tmbuf,
968                         BER_BVISNULL( &be->be_rootdn ) ? SLAPD_ANONYMOUS : be->be_rootdn.bv_val );
969
970 #if 0
971         fprintf( stderr, "# entry:\n%s\n", buf );
972 #endif
973
974         lmi->lmi_e = str2entry( buf );
975         if ( lmi->lmi_e == NULL ) {
976                 return -1;
977         }
978
979         ldap_pvt_thread_mutex_init( &lmi->lmi_entry_mutex );
980
981         return 0;
982 }
983
984 static int
985 lastmod_db_destroy(
986         BackendDB *be
987 )
988 {
989         slap_overinst   *on = (slap_overinst *)be->bd_info;
990         lastmod_info_t  *lmi = (lastmod_info_t *)on->on_bi.bi_private;
991
992         if ( lmi ) {
993                 if ( !BER_BVISNULL( &lmi->lmi_rdnvalue ) ) {
994                         ch_free( lmi->lmi_rdnvalue.bv_val );
995                 }
996
997                 if ( lmi->lmi_e ) {
998                         entry_free( lmi->lmi_e );
999
1000                         ldap_pvt_thread_mutex_destroy( &lmi->lmi_entry_mutex );
1001                 }
1002
1003                 ch_free( lmi );
1004         }
1005
1006         return 0;
1007 }
1008
1009 /* This overlay is set up for dynamic loading via moduleload. For static
1010  * configuration, you'll need to arrange for the slap_overinst to be
1011  * initialized and registered by some other function inside slapd.
1012  */
1013
1014 static slap_overinst            lastmod;
1015
1016 int
1017 lastmod_init()
1018 {
1019         lastmod.on_bi.bi_type = "lastmod";
1020         lastmod.on_bi.bi_db_init = lastmod_db_init;
1021         lastmod.on_bi.bi_db_config = lastmod_db_config;
1022         lastmod.on_bi.bi_db_destroy = lastmod_db_destroy;
1023         lastmod.on_bi.bi_db_open = lastmod_db_open;
1024
1025         lastmod.on_bi.bi_op_add = lastmod_op_func;
1026         lastmod.on_bi.bi_op_compare = lastmod_op_func;
1027         lastmod.on_bi.bi_op_delete = lastmod_op_func;
1028         lastmod.on_bi.bi_op_modify = lastmod_op_func;
1029         lastmod.on_bi.bi_op_modrdn = lastmod_op_func;
1030         lastmod.on_bi.bi_op_search = lastmod_op_func;
1031         lastmod.on_bi.bi_extended = lastmod_op_func;
1032
1033         lastmod.on_response = lastmod_response;
1034
1035         return overlay_register( &lastmod );
1036 }
1037
1038 #if SLAPD_OVER_LASTMOD == SLAPD_MOD_DYNAMIC
1039 int
1040 init_module( int argc, char *argv[] )
1041 {
1042         return lastmod_init();
1043 }
1044 #endif /* SLAPD_OVER_LASTMOD == SLAPD_MOD_DYNAMIC */
1045
1046 #endif /* defined(SLAPD_OVER_LASTMOD) */