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