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