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