]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/accesslog.c
Fix Bind/Unbind logging, align Search scopes with RFC2255
[openldap] / servers / slapd / overlays / accesslog.c
1 /* accesslog.c - log operations for audit/history purposes */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2005 The OpenLDAP Foundation.
6  * Portions copyright 2004-2005 Symas Corporation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Howard Chu for inclusion in
19  * OpenLDAP Software.
20  */
21
22 #include "portable.h"
23
24 #ifdef SLAPD_OVER_ACCESSLOG
25
26 #include <stdio.h>
27
28 #include <ac/string.h>
29 #include <ac/ctype.h>
30
31 #include "slap.h"
32 #include "config.h"
33 #include "lutil.h"
34 #include "ldap_rq.h"
35
36 #define LOG_OP_ADD      0x001
37 #define LOG_OP_DELETE   0x002
38 #define LOG_OP_MODIFY   0x004
39 #define LOG_OP_MODRDN   0x008
40 #define LOG_OP_COMPARE  0x010
41 #define LOG_OP_SEARCH   0x020
42 #define LOG_OP_BIND     0x040
43 #define LOG_OP_UNBIND   0x080
44 #define LOG_OP_ABANDON  0x100
45 #define LOG_OP_EXTENDED 0x200
46 #define LOG_OP_UNKNOWN  0x400
47
48 #define LOG_OP_WRITES   (LOG_OP_ADD|LOG_OP_DELETE|LOG_OP_MODIFY|LOG_OP_MODRDN)
49 #define LOG_OP_READS    (LOG_OP_COMPARE|LOG_OP_SEARCH)
50 #define LOG_OP_SESSION  (LOG_OP_BIND|LOG_OP_UNBIND|LOG_OP_ABANDON)
51 #define LOG_OP_ALL              (LOG_OP_READS|LOG_OP_WRITES|LOG_OP_SESSION| \
52         LOG_OP_EXTENDED|LOG_OP_UNKNOWN)
53
54 typedef struct log_info {
55         BackendDB *li_db;
56         slap_mask_t li_ops;
57         int li_age;
58         int li_cycle;
59         struct re_s *li_task;
60         int li_success;
61         ldap_pvt_thread_mutex_t li_op_mutex;
62         ldap_pvt_thread_mutex_t li_log_mutex;
63 } log_info;
64
65 static ConfigDriver log_cf_gen;
66
67 enum {
68         LOG_DB = 1,
69         LOG_OPS,
70         LOG_PURGE,
71         LOG_SUCCESS
72 };
73
74 static ConfigTable log_cfats[] = {
75         { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_MAGIC|LOG_DB,
76                 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
77                         "DESC 'Suffix of database for log content' "
78                         "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
79         { "logops", "op|writes|reads|session|all", 2, 0, 0,
80                 ARG_MAGIC|LOG_OPS,
81                 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
82                         "DESC 'Operation types to log' "
83                         "EQUALITY caseIgnoreMatch "
84                         "SYNTAX OMsDirectoryString )", NULL, NULL },
85         { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
86                 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
87                         "DESC 'Log cleanup parameters' "
88                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
89         { "logsuccess", NULL, 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|LOG_SUCCESS,
90                 log_cf_gen, "( OLcfgOvAt:4.4 NAME 'olcAccessLogSuccess' "
91                         "DESC 'Log successful ops only' "
92                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
93         { NULL }
94 };
95
96 static ConfigOCs log_cfocs[] = {
97         { "( OLcfgOvOc:4.1 "
98                 "NAME 'olcAccessLogConfig' "
99                 "DESC 'Access log configuration' "
100                 "SUP olcOverlayConfig "
101                 "MUST olcAccessLogDB "
102                 "MAY ( olcAccessLogOps $ olcAccessLogPurge $ olcAccessLogSuccess ) )",
103                         Cft_Overlay, log_cfats },
104         { NULL }
105 };
106
107 static slap_verbmasks logops[] = {
108         { BER_BVC("all"),               LOG_OP_ALL },
109         { BER_BVC("writes"),    LOG_OP_WRITES },
110         { BER_BVC("session"),   LOG_OP_SESSION },
111         { BER_BVC("reads"),             LOG_OP_READS },
112         { BER_BVC("add"),               LOG_OP_ADD },
113         { BER_BVC("delete"),    LOG_OP_DELETE },
114         { BER_BVC("modify"),    LOG_OP_MODIFY },
115         { BER_BVC("modrdn"),    LOG_OP_MODRDN },
116         { BER_BVC("compare"),   LOG_OP_COMPARE },
117         { BER_BVC("search"),    LOG_OP_SEARCH },
118         { BER_BVC("bind"),              LOG_OP_BIND },
119         { BER_BVC("unbind"),    LOG_OP_UNBIND },
120         { BER_BVC("abandon"),   LOG_OP_ABANDON },
121         { BER_BVC("extended"),  LOG_OP_EXTENDED },
122         { BER_BVC("unknown"),   LOG_OP_UNKNOWN },
123         { BER_BVNULL, 0 }
124 };
125
126 /* Start with "add" in logops */
127 #define EN_OFFSET       4
128
129 enum {
130         LOG_EN_ADD = 0,
131         LOG_EN_DELETE,
132         LOG_EN_MODIFY,
133         LOG_EN_MODRDN,
134         LOG_EN_COMPARE,
135         LOG_EN_SEARCH,
136         LOG_EN_BIND,
137         LOG_EN_UNBIND,
138         LOG_EN_ABANDON,
139         LOG_EN_EXTENDED,
140         LOG_EN_UNKNOWN,
141         LOG_EN__COUNT
142 };
143
144 static ObjectClass *log_ocs[LOG_EN__COUNT];
145
146 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
147
148 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
149 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
150
151 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
152         *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
153         *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
154         *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
155         *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
156         *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
157         *ad_reqId, *ad_reqMessage;
158 #if 0
159 static AttributeDescription *ad_oldest;
160 #endif
161
162 static struct {
163         char *at;
164         AttributeDescription **ad;
165 } lattrs[] = {
166         { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
167                 "DESC 'Target DN of request' "
168                 "EQUALITY distinguishedNameMatch "
169                 "SYNTAX OMsDN "
170                 "SINGLE-VALUE )", &ad_reqDN },
171         { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
172                 "DESC 'Start time of request' "
173                 "EQUALITY generalizedTimeMatch "
174                 "ORDERING generalizedTimeOrderingMatch "
175                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
176                 "SINGLE-VALUE )", &ad_reqStart },
177         { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
178                 "DESC 'End time of request' "
179                 "EQUALITY generalizedTimeMatch "
180                 "ORDERING generalizedTimeOrderingMatch "
181                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
182                 "SINGLE-VALUE )", &ad_reqEnd },
183         { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
184                 "DESC 'Type of request' "
185                 "EQUALITY caseIgnoreMatch "
186                 "SYNTAX OMsDirectoryString "
187                 "SINGLE-VALUE )", &ad_reqType },
188         { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
189                 "DESC 'Session ID of request' "
190                 "EQUALITY caseIgnoreMatch "
191                 "SYNTAX OMsDirectoryString "
192                 "SINGLE-VALUE )", &ad_reqSession },
193         { "( " LOG_SCHEMA_AT ".6 NAME 'reqResult' "
194                 "DESC 'Result code of request' "
195                 "EQUALITY integerMatch "
196                 "SYNTAX OMsInteger "
197                 "SINGLE-VALUE )", &ad_reqResult },
198         { "( " LOG_SCHEMA_AT ".7 NAME 'reqAuthzID' "
199                 "DESC 'Authorization ID of requestor' "
200                 "EQUALITY distinguishedNameMatch "
201                 "SYNTAX OMsDN "
202                 "SINGLE-VALUE )", &ad_reqAuthzID },
203         { "( " LOG_SCHEMA_AT ".8 NAME 'reqControls' "
204                 "DESC 'Request controls' "
205                 "SYNTAX OMsOctetString )", &ad_reqControls },
206         { "( " LOG_SCHEMA_AT ".9 NAME 'reqRespControls' "
207                 "DESC 'Response controls of request' "
208                 "SYNTAX OMsOctetString )", &ad_reqRespControls },
209         { "( " LOG_SCHEMA_AT ".10 NAME 'reqMethod' "
210                 "DESC 'Bind method of request' "
211                 "EQUALITY caseIgnoreMatch "
212                 "SYNTAX OMsDirectoryString "
213                 "SINGLE-VALUE )", &ad_reqMethod },
214         { "( " LOG_SCHEMA_AT ".11 NAME 'reqAssertion' "
215                 "DESC 'Compare Assertion of request' "
216                 "SYNTAX OMsDirectoryString "
217                 "SINGLE-VALUE )", &ad_reqAssertion },
218         { "( " LOG_SCHEMA_AT ".12 NAME 'reqNewRDN' "
219                 "DESC 'New RDN of request' "
220                 "EQUALITY distinguishedNameMatch "
221                 "SYNTAX OMsDN "
222                 "SINGLE-VALUE )", &ad_reqNewRDN },
223         { "( " LOG_SCHEMA_AT ".13 NAME 'reqNewSuperior' "
224                 "DESC 'New superior DN of request' "
225                 "EQUALITY distinguishedNameMatch "
226                 "SYNTAX OMsDN "
227                 "SINGLE-VALUE )", &ad_reqNewSuperior },
228         { "( " LOG_SCHEMA_AT ".14 NAME 'reqDeleteOldRDN' "
229                 "DESC 'Delete old RDN' "
230                 "SYNTAX OMsBoolean "
231                 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
232         { "( " LOG_SCHEMA_AT ".15 NAME 'reqMod' "
233                 "DESC 'Modifications of request' "
234                 "SYNTAX OMsDirectoryString "
235                 "EQUALITY caseIgnoreMatch "
236                 "SUBSTR caseIgnoreSubstringsMatch )", &ad_reqMod },
237         { "( " LOG_SCHEMA_AT ".16 NAME 'reqScope' "
238                 "DESC 'Scope of request' "
239                 "SYNTAX OMsDirectoryString "
240                 "SINGLE-VALUE )", &ad_reqScope },
241         { "( " LOG_SCHEMA_AT ".17 NAME 'reqFilter' "
242                 "DESC 'Filter of request' "
243                 "SYNTAX OMsDirectoryString "
244                 "SINGLE-VALUE )", &ad_reqFilter },
245         { "( " LOG_SCHEMA_AT ".18 NAME 'reqAttr' "
246                 "DESC 'Attributes of request' "
247                 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
248         { "( " LOG_SCHEMA_AT ".19 NAME 'reqEntries' "
249                 "DESC 'Number of entries returned' "
250                 "SYNTAX OMsInteger "
251                 "SINGLE-VALUE )", &ad_reqEntries },
252         { "( " LOG_SCHEMA_AT ".20 NAME 'reqSizeLimit' "
253                 "DESC 'Size limit of request' "
254                 "SYNTAX OMsInteger "
255                 "SINGLE-VALUE )", &ad_reqSizeLimit },
256         { "( " LOG_SCHEMA_AT ".21 NAME 'reqTimeLimit' "
257                 "DESC 'Time limit of request' "
258                 "SYNTAX OMsInteger "
259                 "SINGLE-VALUE )", &ad_reqTimeLimit },
260         { "( " LOG_SCHEMA_AT ".22 NAME 'reqAttrsOnly' "
261                 "DESC 'Attributes and values of request' "
262                 "SYNTAX OMsBoolean "
263                 "SINGLE-VALUE )", &ad_reqAttrsOnly },
264         { "( " LOG_SCHEMA_AT ".23 NAME 'reqData' "
265                 "DESC 'Data of extended request' "
266                 "SYNTAX OMsOctetString "
267                 "SINGLE-VALUE )", &ad_reqData },
268         { "( " LOG_SCHEMA_AT ".24 NAME 'reqId' "
269                 "DESC 'ID of Request to Abandon' "
270                 "SYNTAX OMsInteger "
271                 "SINGLE-VALUE )", &ad_reqId },
272         { "( " LOG_SCHEMA_AT ".25 NAME 'reqMessage' "
273                 "DESC 'Error text of request' "
274                 "SYNTAX OMsDirectoryString "
275                 "SINGLE-VALUE )", &ad_reqMessage },
276 #if 0
277         { "( " LOG_SCHEMA_AT ".26 NAME 'auditOldest' "
278                 "DESC 'Oldest record in this branch' "
279                 "EQUALITY generalizedTimeMatch "
280                 "ORDERING generalizedTimeOrderingMatch "
281                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
282                 "SINGLE-VALUE )", &ad_oldest },
283 #endif
284         { NULL, NULL }
285 };
286
287 static struct {
288         char *ot;
289         ObjectClass **oc;
290 } locs[] = {
291 #if 0
292         { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
293                 "SUP top STRUCTURAL "
294                 "MUST auditOldest "
295                 "MAY cn )", &oc_container },
296 #endif
297         { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
298                 "DESC 'OpenLDAP request auditing' "
299                 "SUP top STRUCTURAL "
300                 "MUST ( reqStart $ reqType $ reqSession ) "
301                 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
302                         "reqResult $ reqMessage ) )", &log_ocs[LOG_EN_UNBIND] },
303         { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
304                 "DESC 'OpenLDAP read request record' "
305                 "SUP auditObject STRUCTURAL )", NULL },
306         { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
307                 "DESC 'OpenLDAP write request record' "
308                 "SUP auditObject STRUCTURAL )", &log_ocs[LOG_EN_DELETE] },
309         { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
310                 "DESC 'Abandon operation' "
311                 "SUP auditObject STRUCTURAL "
312                 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
313         { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
314                 "DESC 'Add operation' "
315                 "SUP auditWriteObject STRUCTURAL "
316                 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
317         { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
318                 "DESC 'Bind operation' "
319                 "SUP auditObject STRUCTURAL "
320                 "MUST reqMethod )", &log_ocs[LOG_EN_BIND] },
321         { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
322                 "DESC 'Compare operation' "
323                 "SUP auditReadObject STRUCTURAL "
324                 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
325         { "( " LOG_SCHEMA_OC ".8 NAME 'auditModify' "
326                 "DESC 'Modify operation' "
327                 "SUP auditWriteObject STRUCTURAL "
328                 "MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
329         { "( " LOG_SCHEMA_OC ".9 NAME 'auditModRDN' "
330                 "DESC 'ModRDN operation' "
331                 "SUP auditWriteObject STRUCTURAL "
332                 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
333                 "MAY reqNewSuperior )", &log_ocs[LOG_EN_MODRDN] },
334         { "( " LOG_SCHEMA_OC ".10 NAME 'auditSearch' "
335                 "DESC 'Search operation' "
336                 "SUP auditReadObject STRUCTURAL "
337                 "MUST ( reqScope $ reqAttrsonly ) "
338                 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
339                         "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
340         { "( " LOG_SCHEMA_OC ".11 NAME 'auditExtended' "
341                 "DESC 'Extended operation' "
342                 "SUP auditObject STRUCTURAL "
343                 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
344         { NULL, NULL }
345 };
346
347 #define RDNEQ   "reqStart="
348
349 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
350  * If a field is present, it must be two digits. (Except for
351  * days, which can be arbitrary width.)
352  */
353 static int
354 log_age_parse(char *agestr)
355 {
356         int t1, t2;
357         int gotdays = 0;
358         char *endptr;
359
360         t1 = strtol( agestr, &endptr, 10 );
361         /* Is there a days delimiter? */
362         if ( *endptr == '+' ) {
363                 /* 32 bit time only covers about 68 years */
364                 if ( t1 < 0 || t1 > 25000 )
365                         return -1;
366                 t1 *= 24;
367                 gotdays = 1;
368         } else if ( *endptr != ':' ) {
369         /* No valid delimiter found, fail */
370                 return -1;
371         }
372
373         agestr += 3;
374         t2 = atoi( agestr );
375
376         /* if there's a delimiter, it can only be a colon */
377         if ( agestr[2] && agestr[2] != ':' )
378                 return -1;
379
380         /* If we're at the end of the string, and we started with days,
381          * fail because we expected to find minutes too.
382          */
383         if ( gotdays && !agestr[2] )
384                 return -1;
385
386         t1 *= 60;
387         t1 += t2;
388
389         if ( !agestr[2] )
390                 return t1 * 60;
391
392         agestr += 3;
393         t2 = atoi( agestr );
394
395         /* last field can only be seconds */
396         if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
397                 return -1;
398         t1 *= 60;
399         t1 += t2;
400
401         t1 *= 60;
402         if ( agestr[2] ) {
403                 agestr += 3;
404                 if ( agestr[2] )
405                         return -1;
406                 t1 += atoi( agestr );
407         }
408         return t1;
409 }
410
411 static void
412 log_age_unparse( int age, struct berval *agebv )
413 {
414         int dd, hh, mm, ss;
415         char *ptr;
416
417         ss = age % 60;
418         age /= 60;
419         mm = age % 60;
420         age /= 60;
421         hh = age % 60;
422         age /= 24;
423         dd = age;
424
425         ptr = agebv->bv_val;
426
427         if ( dd ) 
428                 ptr += sprintf( ptr, "%d+", dd );
429         ptr += sprintf( ptr, "%02d:%02d", hh, mm );
430         if ( ss )
431                 ptr += sprintf( ptr, ":%02d", ss );
432
433         agebv->bv_len = ptr - agebv->bv_val;
434 }
435
436 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
437
438 #define PURGE_INCREMENT 100
439
440 typedef struct purge_data {
441         int slots;
442         int used;
443         BerVarray dn;
444         BerVarray ndn;
445 } purge_data;
446
447 static int
448 log_old_lookup( Operation *op, SlapReply *rs )
449 {
450         purge_data *pd = op->o_callback->sc_private;
451
452         if ( rs->sr_type != REP_SEARCH) return 0;
453
454         if ( pd->used >= pd->slots ) {
455                 pd->slots += PURGE_INCREMENT;
456                 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
457                 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
458         }
459         ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
460         ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
461         pd->used++;
462         return 0;
463 }
464
465 /* Periodically search for old entries in the log database and delete them */
466 static void *
467 accesslog_purge( void *ctx, void *arg )
468 {
469         struct re_s *rtask = arg;
470         struct log_info *li = rtask->arg;
471
472         Connection conn = {0};
473         OperationBuffer opbuf;
474         Operation *op = (Operation *) &opbuf;
475         SlapReply rs = {REP_RESULT};
476         slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
477         Filter f;
478         AttributeAssertion ava = {0};
479         purge_data pd = {0};
480         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
481         time_t old = slap_get_time();
482
483         connection_fake_init( &conn, op, ctx );
484
485         f.f_choice = LDAP_FILTER_LE;
486         f.f_ava = &ava;
487         f.f_next = NULL;
488
489         ava.aa_desc = ad_reqStart;
490         ava.aa_value.bv_val = timebuf;
491         ava.aa_value.bv_len = sizeof(timebuf);
492
493         old -= li->li_age;
494         slap_timestamp( &old, &ava.aa_value );
495
496         op->o_tag = LDAP_REQ_SEARCH;
497         op->o_bd = li->li_db;
498         op->o_dn = li->li_db->be_rootdn;
499         op->o_ndn = li->li_db->be_rootndn;
500         op->o_req_dn = li->li_db->be_suffix[0];
501         op->o_req_ndn = li->li_db->be_nsuffix[0];
502         op->o_callback = &cb;
503         op->ors_scope = LDAP_SCOPE_ONELEVEL;
504         op->ors_deref = LDAP_DEREF_NEVER;
505         op->ors_tlimit = SLAP_NO_LIMIT;
506         op->ors_slimit = SLAP_NO_LIMIT;
507         op->ors_filter = &f;
508         filter2bv_x( op, &f, &op->ors_filterstr );
509         op->ors_attrs = slap_anlist_no_attrs;
510         op->ors_attrsonly = 1;
511         
512         cb.sc_private = &pd;
513
514         op->o_bd->be_search( op, &rs );
515         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
516
517         if ( pd.used ) {
518                 int i;
519
520                 op->o_tag = LDAP_REQ_DELETE;
521                 op->o_callback = &nullsc;
522
523                 for (i=0; i<pd.used; i++) {
524                         op->o_req_dn = pd.dn[i];
525                         op->o_req_ndn = pd.ndn[i];
526                         op->o_bd->be_delete( op, &rs );
527                         ch_free( pd.ndn[i].bv_val );
528                         ch_free( pd.dn[i].bv_val );
529                 }
530                 ch_free( pd.ndn );
531                 ch_free( pd.dn );
532         }
533
534         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
535         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
536         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
537
538         return NULL;
539 }
540
541 static int
542 log_cf_gen(ConfigArgs *c)
543 {
544         slap_overinst *on = (slap_overinst *)c->bi;
545         struct log_info *li = on->on_bi.bi_private;
546         int rc = 0;
547         slap_mask_t tmask = 0;
548         char agebuf[2*STRLENOF("ddddd+hh:mm:ss  ")];
549         struct berval agebv, cyclebv;
550
551         switch( c->op ) {
552         case SLAP_CONFIG_EMIT:
553                 switch( c->type ) {
554                 case LOG_DB:
555                         value_add( &c->rvalue_vals, li->li_db->be_suffix );
556                         value_add( &c->rvalue_nvals, li->li_db->be_nsuffix );
557                         break;
558                 case LOG_OPS:
559                         rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
560                         break;
561                 case LOG_PURGE:
562                         agebv.bv_val = agebuf;
563                         log_age_unparse( li->li_age, &agebv );
564                         agebv.bv_val[agebv.bv_len] = ' ';
565                         agebv.bv_len++;
566                         cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
567                         log_age_unparse( li->li_cycle, &cyclebv );
568                         agebv.bv_len += cyclebv.bv_len;
569                         value_add_one( &c->rvalue_vals, &agebv );
570                         break;
571                 case LOG_SUCCESS:
572                         if ( li->li_success )
573                                 c->value_int = li->li_success;
574                         else
575                                 rc = 1;
576                         break;
577                 }
578                 break;
579         case LDAP_MOD_DELETE:
580                 switch( c->type ) {
581                 case LOG_DB:
582                         /* noop. this should always be a valid backend. */
583                         break;
584                 case LOG_OPS:
585                         if ( c->valx < 0 ) {
586                                 li->li_ops = 0;
587                         } else {
588                                 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
589                                 if ( rc == 0 )
590                                         li->li_ops &= ~tmask;
591                         }
592                         break;
593                 case LOG_PURGE:
594                         if ( li->li_task ) {
595                                 struct re_s *re = li->li_task;
596                                 li->li_task = NULL;
597                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
598                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
599                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
600                         }
601                         li->li_age = 0;
602                         li->li_cycle = 0;
603                         break;
604                 case LOG_SUCCESS:
605                         li->li_success = 0;
606                         break;
607                 }
608                 break;
609         default:
610                 switch( c->type ) {
611                 case LOG_DB:
612                         li->li_db = select_backend( &c->value_ndn, 0, 0 );
613                         if ( !li->li_db ) {
614                                 sprintf( c->msg, "<%s> no matching backend found for suffix",
615                                         c->argv[0] );
616                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
617                                         c->log, c->msg, c->value_dn.bv_val );
618                                 rc = 1;
619                         }
620                         ch_free( c->value_dn.bv_val );
621                         ch_free( c->value_ndn.bv_val );
622                         break;
623                 case LOG_OPS:
624                         rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
625                         if ( rc == 0 )
626                                 li->li_ops |= tmask;
627                         break;
628                 case LOG_PURGE:
629                         li->li_age = log_age_parse( c->argv[1] );
630                         if ( li->li_age == -1 ) {
631                                 rc = 1;
632                         } else {
633                                 li->li_cycle = log_age_parse( c->argv[2] );
634                                 if ( li->li_cycle == -1 ) {
635                                         rc = 1;
636                                 } else if ( slapMode & SLAP_SERVER_MODE ) {
637                                         struct re_s *re = li->li_task;
638                                         if ( re )
639                                                 re->interval.tv_sec = li->li_cycle;
640                                         else
641                                                 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
642                                                         li->li_cycle, accesslog_purge, li,
643                                                         "accesslog_purge", li->li_db ?
644                                                                 li->li_db->be_suffix[0].bv_val :
645                                                                 c->be->be_suffix[0].bv_val );
646                                 }
647                         }
648                         break;
649                 case LOG_SUCCESS:
650                         li->li_success = c->value_int;
651                         break;
652                 }
653                 break;
654         }
655         return rc;
656 }
657
658 static Entry *accesslog_entry( Operation *op, int logop ) {
659         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
660         log_info *li = on->on_bi.bi_private;
661
662         char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
663         char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
664
665         struct berval rdn, nrdn, timestamp, ntimestamp, bv;
666         slap_verbmasks *lo = logops+logop+EN_OFFSET;
667
668         Entry *e = ch_calloc( 1, sizeof(Entry) );
669
670         strcpy( rdnbuf, RDNEQ );
671         rdn.bv_val = rdnbuf;
672         strcpy( nrdnbuf, RDNEQ );
673         nrdn.bv_val = nrdnbuf;
674
675         timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
676         timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
677         slap_timestamp( &op->o_time, &timestamp );
678         sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
679         timestamp.bv_len += 7;
680
681         rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
682         ad_reqStart->ad_type->sat_equality->smr_normalize(
683                 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
684                 ad_reqStart->ad_type->sat_equality, &timestamp, &ntimestamp,
685                 op->o_tmpmemctx );
686
687         strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
688         nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
689         build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
690         build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
691
692         attr_merge_one( e, slap_schema.si_ad_objectClass,
693                 &log_ocs[logop]->soc_cname, NULL );
694         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
695                 &log_ocs[logop]->soc_cname, NULL );
696         attr_merge_one( e, ad_reqStart, &timestamp, &ntimestamp );
697         op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
698
699         /* Exops have OID appended */
700         if ( logop == LOG_EN_EXTENDED ) {
701                 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
702                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
703                 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
704                 bv.bv_val[lo->word.bv_len] = '{';
705                 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
706                         op->ore_reqoid.bv_len );
707                 bv.bv_val[bv.bv_len-1] = '}';
708                 bv.bv_val[bv.bv_len] = '\0';
709                 attr_merge_one( e, ad_reqType, &bv, NULL );
710         } else {
711                 attr_merge_one( e, ad_reqType, &lo->word, NULL );
712         }
713
714         rdn.bv_len = sprintf( rdn.bv_val, "%lu", op->o_connid );
715         attr_merge_one( e, ad_reqSession, &rdn, NULL );
716
717         if ( BER_BVISNULL( &op->o_dn )) 
718                 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
719                         (struct berval *)&slap_empty_bv );
720         else
721                 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
722
723         /* FIXME: need to add reqControls and reqRespControls */
724
725         return e;
726 }
727
728 static struct berval scopes[] = {
729         BER_BVC("base"),
730         BER_BVC("one"),
731         BER_BVC("sub"),
732         BER_BVC("subord")
733 };
734
735 static struct berval simple = BER_BVC("SIMPLE");
736
737 static int accesslog_response(Operation *op, SlapReply *rs) {
738         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
739         log_info *li = on->on_bi.bi_private;
740         Attribute *a, *last_attr;
741         Modifications *m;
742         struct berval *b;
743         time_t endtime;
744         int i, nop;
745         int logop;
746         slap_verbmasks *lo;
747         Entry *e;
748         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
749         struct berval bv;
750         char *ptr;
751         BerVarray vals;
752         Operation op2 = {0};
753         SlapReply rs2 = {REP_RESULT};
754
755         if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
756                 return SLAP_CB_CONTINUE;
757
758         switch ( op->o_tag ) {
759         case LDAP_REQ_ADD:              logop = LOG_EN_ADD; break;
760         case LDAP_REQ_DELETE:   logop = LOG_EN_DELETE; break;
761         case LDAP_REQ_MODIFY:   logop = LOG_EN_MODIFY; break;
762         case LDAP_REQ_MODRDN:   logop = LOG_EN_MODRDN; break;
763         case LDAP_REQ_COMPARE:  logop = LOG_EN_COMPARE; break;
764         case LDAP_REQ_SEARCH:   logop = LOG_EN_SEARCH; break;
765         case LDAP_REQ_BIND:             logop = LOG_EN_BIND; break;
766         case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
767         default:        /* unknown operation type */
768                 logop = LOG_EN_UNKNOWN; break;
769         }       /* Unbind and Abandon never reach here */
770
771         lo = logops+logop+EN_OFFSET;
772         if ( !( li->li_ops & lo->mask ))
773                 return SLAP_CB_CONTINUE;
774
775         if ( lo->mask & LOG_OP_WRITES ) {
776                 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
777                 ldap_pvt_thread_mutex_unlock( &li->li_op_mutex );
778         }
779
780         if ( li->li_success && rs->sr_err != LDAP_SUCCESS )
781                 goto done;
782
783         slap_op_time( &endtime, &nop );
784
785         e = accesslog_entry( op, logop );
786
787         bv.bv_val = timebuf;
788         bv.bv_len = sizeof(timebuf);
789         slap_timestamp( &endtime, &bv );
790         sprintf( bv.bv_val + bv.bv_len-1, ".%06dZ", nop );
791         bv.bv_len += 7;
792
793         attr_merge_normalize_one( e, ad_reqEnd, &bv, op->o_tmpmemctx );
794
795         attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
796
797         if ( rs->sr_text ) {
798                 ber_str2bv( rs->sr_text, 0, 0, &bv );
799                 attr_merge_one( e, ad_reqMessage, &bv, NULL );
800         }
801         bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
802         bv.bv_val = timebuf;
803
804         attr_merge_one( e, ad_reqResult, &bv, NULL );
805
806         last_attr = attr_find( e->e_attrs, ad_reqResult );
807
808         switch( logop ) {
809         case LOG_EN_ADD:
810                 /* count all the vals */
811                 i = 0;
812                 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
813                         if ( a->a_vals ) {
814                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
815                                         i++;
816                                 }
817                         }
818                 }
819                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
820                 i = 0;
821                 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
822                         if ( a->a_vals ) {
823                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
824                                         vals[i].bv_len = a->a_desc->ad_cname.bv_len + b->bv_len +3;
825                                         vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
826                                         ptr = lutil_strcopy( vals[i].bv_val,
827                                                 a->a_desc->ad_cname.bv_val );
828                                         *ptr++ = ':';
829                                         *ptr++ = '+';
830                                         *ptr++ = ' ';
831                                         AC_MEMCPY( ptr, b->bv_val, b->bv_len );
832                                         vals[i].bv_val[vals[i].bv_len] = '\0';
833                                 }
834                         }
835                 }
836                 vals[i].bv_val = NULL;
837                 vals[i].bv_len = 0;
838                 a = attr_alloc( ad_reqMod );
839                 a->a_vals = vals;
840                 a->a_nvals = vals;
841                 last_attr->a_next = a;
842                 break;
843
844         case LOG_EN_DELETE:
845                 /* needs nothing else */
846                 break;
847
848         case LOG_EN_MODIFY:
849                 /* count all the mods */
850                 i = 0;
851                 for ( m=op->orm_modlist; m; m=m->sml_next ) {
852                         if ( m->sml_values ) {
853                                 for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
854                                         i++;
855                                 }
856                         } else if ( m->sml_op == LDAP_MOD_DELETE ) {
857                                 i++;
858                         }
859                 }
860                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
861                 i = 0;
862                 for ( m=op->orm_modlist; m; m=m->sml_next ) {
863                         if ( m->sml_values ) {
864                                 for (b=m->sml_values; !BER_BVISNULL( b ); b++,i++) {
865                                         char c_op;
866                                         vals[i].bv_len = m->sml_desc->ad_cname.bv_len + b->bv_len +3;
867                                         vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
868                                         ptr = lutil_strcopy( vals[i].bv_val,
869                                                 m->sml_desc->ad_cname.bv_val );
870                                         *ptr++ = ':';
871                                         switch( m->sml_op ) {
872                                         case LDAP_MOD_ADD: c_op = '+'; break;
873                                         case LDAP_MOD_DELETE:   c_op = '-'; break;
874                                         case LDAP_MOD_REPLACE:  c_op = '='; break;
875                                         case LDAP_MOD_INCREMENT:        c_op = '#'; break;
876
877                                         /* unknown op. there shouldn't be any of these. we
878                                          * don't know what to do with it, but we shouldn't just
879                                          * ignore it.
880                                          */
881                                         default: c_op = '?'; break;
882                                         }
883                                         *ptr++ = c_op;
884                                         *ptr++ = ' ';
885                                         AC_MEMCPY( ptr, b->bv_val, b->bv_len );
886                                         vals[i].bv_val[vals[i].bv_len] = '\0';
887                                 }
888                         } else if ( m->sml_op == LDAP_MOD_DELETE ) {
889                                 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
890                                 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
891                                 ptr = lutil_strcopy( vals[i].bv_val,
892                                         m->sml_desc->ad_cname.bv_val );
893                                 *ptr++ = ':';
894                                 *ptr++ = '-';
895                                 *ptr = '\0';
896                                 i++;
897                         }
898                 }
899                 vals[i].bv_val = NULL;
900                 vals[i].bv_len = 0;
901                 a = attr_alloc( ad_reqMod );
902                 a->a_vals = vals;
903                 a->a_nvals = vals;
904                 last_attr->a_next = a;
905                 break;
906
907         case LOG_EN_MODRDN:
908                 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
909                 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
910                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
911                         NULL );
912                 if ( op->orr_newSup ) {
913                         attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
914                 }
915                 break;
916
917         case LOG_EN_COMPARE:
918                 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
919                         op->orc_ava->aa_value.bv_len;
920                 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
921                 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
922                 *ptr++ = '=';
923                 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
924                 bv.bv_val[bv.bv_len] = '\0';
925                 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
926                 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
927                 break;
928
929         case LOG_EN_SEARCH:
930                 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
931                 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
932                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
933                         NULL );
934                 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
935                         attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
936                 if ( op->ors_attrs ) {
937                         /* count them */
938                         for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
939                                 ;
940                         vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
941                                 op->o_tmpmemctx );
942                         for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
943                                 vals[i] = op->ors_attrs[i].an_name;
944                         vals[i].bv_val = NULL;
945                         vals[i].bv_len = 0;
946                         attr_merge( e, ad_reqAttr, vals, NULL );
947                         op->o_tmpfree( vals, op->o_tmpmemctx );
948                 }
949                 bv.bv_val = timebuf;
950                 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
951                 attr_merge_one( e, ad_reqEntries, &bv, NULL );
952
953                 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
954                 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
955                 /* FIXME: slimit was zeroed by the backends */
956                 break;
957
958         case LOG_EN_BIND:
959                 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
960                         attr_merge_one( e, ad_reqMethod, &simple, NULL );
961                 } else {
962                         bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
963                         bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
964                         ptr = lutil_strcopy( bv.bv_val, "SASL(" );
965                         ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
966                         *ptr++ = ')';
967                         *ptr = '\0';
968                         attr_merge_one( e, ad_reqMethod, &bv, NULL );
969                         op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
970                 }
971                 break;
972
973         case LOG_EN_EXTENDED:
974                 if ( op->ore_reqdata ) {
975                         attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
976                 }
977                 break;
978
979         case LOG_EN_UNKNOWN:
980                 /* we don't know its parameters, don't add any */
981                 break;
982         }
983
984         op2.o_hdr = op->o_hdr;
985         op2.o_tag = LDAP_REQ_ADD;
986         op2.o_time = endtime;
987         op2.o_tincr = 0;
988         op2.o_bd = li->li_db;
989         op2.o_dn = li->li_db->be_rootdn;
990         op2.o_ndn = li->li_db->be_rootndn;
991         op2.o_req_dn = e->e_name;
992         op2.o_req_ndn = e->e_nname;
993         op2.ora_e = e;
994         op2.o_callback = &nullsc;
995
996         if (( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn )) {
997                 slap_queue_csn( &op2, &op->o_csn );
998         }
999
1000         op2.o_bd->be_add( &op2, &rs2 );
1001         entry_free( e );
1002
1003 done:
1004         ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
1005         return SLAP_CB_CONTINUE;
1006 }
1007
1008 /* Since Bind success is sent by the frontend, it won't normally enter
1009  * the overlay response callback. Add another callback to make sure it
1010  * gets here.
1011  */
1012 static int
1013 accesslog_bind_resp( Operation *op, SlapReply *rs )
1014 {
1015         BackendDB *be, db;
1016         int rc;
1017         slap_callback *sc;
1018
1019         be = op->o_bd;
1020         db = *be;
1021         op->o_bd = &db;
1022         db.bd_info = op->o_callback->sc_private;
1023         rc = accesslog_response( op, rs );
1024         op->o_bd = be;
1025         sc = op->o_callback;
1026         op->o_callback = sc->sc_next;
1027         op->o_tmpfree( sc, op->o_tmpmemctx );
1028         return rc;
1029 }
1030
1031 static int
1032 accesslog_op_bind( Operation *op, SlapReply *rs )
1033 {
1034         slap_callback *sc;
1035
1036         sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1037         sc->sc_response = accesslog_bind_resp;
1038         sc->sc_private = op->o_bd->bd_info;
1039
1040         if ( op->o_callback ) {
1041                 sc->sc_next = op->o_callback->sc_next;
1042                 op->o_callback->sc_next = sc;
1043         } else {
1044                 op->o_callback = sc;
1045         }
1046         return SLAP_CB_CONTINUE;
1047 }
1048
1049 static int
1050 accesslog_op_mod( Operation *op, SlapReply *rs )
1051 {
1052         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1053         log_info *li = on->on_bi.bi_private;
1054
1055         if ( li->li_ops & LOG_OP_WRITES ) {
1056                 /* FIXME: this needs to be a recursive mutex to allow
1057                  * overlays like refint to keep working.
1058                  */
1059                 ldap_pvt_thread_mutex_lock( &li->li_op_mutex );
1060         }
1061         return SLAP_CB_CONTINUE;
1062 }
1063
1064 /* unbinds are broadcast to all backends; we only log it if this
1065  * backend was used for the original bind.
1066  */
1067 static int
1068 accesslog_unbind( Operation *op, SlapReply *rs )
1069 {
1070         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1071         if ( op->o_conn->c_authz_backend == on->on_info->oi_origdb ) {
1072                 log_info *li = on->on_bi.bi_private;
1073                 Operation op2;
1074                 void *cids[SLAP_MAX_CIDS];
1075                 SlapReply rs2 = {REP_RESULT};
1076                 Entry *e;
1077
1078                 if ( !( li->li_ops & LOG_OP_UNBIND ))
1079                         return SLAP_CB_CONTINUE;
1080
1081                 e = accesslog_entry( op, LOG_EN_UNBIND );
1082                 op2.o_hdr = op->o_hdr;
1083                 op2.o_tag = LDAP_REQ_ADD;
1084                 op2.o_time = op->o_time;
1085                 op2.o_tincr = 0;
1086                 op2.o_bd = li->li_db;
1087                 op2.o_dn = li->li_db->be_rootdn;
1088                 op2.o_ndn = li->li_db->be_rootndn;
1089                 op2.o_req_dn = e->e_name;
1090                 op2.o_req_ndn = e->e_nname;
1091                 op2.ora_e = e;
1092                 op2.o_callback = &nullsc;
1093                 op2.o_controls = cids;
1094                 memset(cids, 0, sizeof( cids ));
1095                 memset(op2.o_ctrlflag, 0, sizeof( op2.o_ctrlflag ));
1096
1097                 op2.o_bd->be_add( &op2, &rs2 );
1098                 entry_free( e );
1099         }
1100         return SLAP_CB_CONTINUE;
1101 }
1102
1103 static int
1104 accesslog_abandon( Operation *op, SlapReply *rs )
1105 {
1106         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1107         log_info *li = on->on_bi.bi_private;
1108         Operation op2;
1109         void *cids[SLAP_MAX_CIDS];
1110         SlapReply rs2 = {REP_RESULT};
1111         Entry *e;
1112         char buf[64];
1113         struct berval bv;
1114
1115         if ( !op->o_time || !( li->li_ops & LOG_OP_ABANDON ))
1116                 return SLAP_CB_CONTINUE;
1117
1118         e = accesslog_entry( op, LOG_EN_ABANDON );
1119         bv.bv_val = buf;
1120         bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
1121         attr_merge_one( e, ad_reqId, &bv, NULL );
1122
1123         op2.o_hdr = op->o_hdr;
1124         op2.o_tag = LDAP_REQ_ADD;
1125         op2.o_time = op->o_time;
1126         op2.o_tincr = 0;
1127         op2.o_bd = li->li_db;
1128         op2.o_dn = li->li_db->be_rootdn;
1129         op2.o_ndn = li->li_db->be_rootndn;
1130         op2.o_req_dn = e->e_name;
1131         op2.o_req_ndn = e->e_nname;
1132         op2.ora_e = e;
1133         op2.o_callback = &nullsc;
1134         op2.o_controls = cids;
1135         memset(cids, 0, sizeof( cids ));
1136         memset(op2.o_ctrlflag, 0, sizeof( op2.o_ctrlflag ));
1137
1138         op2.o_bd->be_add( &op2, &rs2 );
1139         entry_free( e );
1140
1141         return SLAP_CB_CONTINUE;
1142 }
1143
1144 static slap_overinst accesslog;
1145
1146 static int
1147 accesslog_db_init(
1148         BackendDB *be
1149 )
1150 {
1151         slap_overinst *on = (slap_overinst *)be->bd_info;
1152         log_info *li = ch_calloc(1, sizeof(log_info));
1153
1154         on->on_bi.bi_private = li;
1155         ldap_pvt_thread_mutex_init( &li->li_op_mutex );
1156         ldap_pvt_thread_mutex_init( &li->li_log_mutex );
1157         return 0;
1158 }
1159
1160 static int
1161 accesslog_db_destroy(
1162         BackendDB *be
1163 )
1164 {
1165         slap_overinst *on = (slap_overinst *)be->bd_info;
1166         log_info *li = on->on_bi.bi_private;
1167         
1168         ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
1169         ldap_pvt_thread_mutex_destroy( &li->li_op_mutex );
1170         free( li );
1171         return LDAP_SUCCESS;
1172 }
1173
1174 int accesslog_init()
1175 {
1176         int i, rc;
1177
1178         accesslog.on_bi.bi_type = "accesslog";
1179         accesslog.on_bi.bi_db_init = accesslog_db_init;
1180         accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1181
1182         accesslog.on_bi.bi_op_add = accesslog_op_mod;
1183         accesslog.on_bi.bi_op_bind = accesslog_op_bind;
1184         accesslog.on_bi.bi_op_delete = accesslog_op_mod;
1185         accesslog.on_bi.bi_op_modify = accesslog_op_mod;
1186         accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
1187         accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1188         accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1189         accesslog.on_response = accesslog_response;
1190
1191         accesslog.on_bi.bi_cf_ocs = log_cfocs;
1192
1193         nullsc.sc_response = slap_null_cb;
1194
1195         rc = config_register_schema( log_cfats, log_cfocs );
1196         if ( rc ) return rc;
1197
1198         /* log schema integration */
1199         for ( i=0; lattrs[i].at; i++ ) {
1200                 LDAPAttributeType *lat;
1201                 AttributeType *at;
1202                 int code;
1203                 const char *err;
1204
1205                 lat = ldap_str2attributetype( lattrs[i].at, &code, &err,
1206                         LDAP_SCHEMA_ALLOW_ALL );
1207                 if ( !lat ) {
1208                         Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1209                                 "ldap_str2attributetype failed on %d: %s, %s\n",
1210                                 i, ldap_scherr2str(code), err );
1211                         return -1;
1212                 }
1213                 code = at_add( lat, 0, &at, &err );
1214                 ldap_memfree( lat );
1215                 if ( code ) {
1216                         Debug( LDAP_DEBUG_ANY, "log_back_initialize: "
1217                                 "at_add failed on %d: %s\n",
1218                                 i, scherr2str(code), 0 );
1219                         return -1;
1220                 }
1221                 if ( slap_bv2ad( &at->sat_cname, lattrs[i].ad, &err )) {
1222                         Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1223                                 "slap_bv2ad failed on %d: %s\n",
1224                                 i, err, 0 );
1225                         return -1;
1226                 }
1227         }
1228         for ( i=0; locs[i].ot; i++ ) {
1229                 LDAPObjectClass *loc;
1230                 ObjectClass *oc;
1231                 int code;
1232                 const char *err;
1233
1234                 loc = ldap_str2objectclass( locs[i].ot, &code, &err,
1235                         LDAP_SCHEMA_ALLOW_ALL );
1236                 if ( !loc ) {
1237                         Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1238                                 "ldap_str2objectclass failed on %d: %s, %s\n",
1239                                 i, ldap_scherr2str(code), err );
1240                         return -1;
1241                 }
1242                 
1243                 code = oc_add( loc, 0, &oc, &err );
1244                 ldap_memfree( loc );
1245                 if ( code ) {
1246                         Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1247                                 "oc_add failed on %d: %s\n",
1248                                 i, scherr2str(code), 0 );
1249                         return -1;
1250                 }
1251                 if ( locs[i].oc )
1252                         *locs[i].oc = oc;
1253         }
1254
1255         return overlay_register(&accesslog);
1256 }
1257
1258 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1259 int init_module( int argc, char *argv[]) {
1260         return accesslog_init();
1261 }
1262 #endif
1263
1264 #endif /* SLAPD_OVER_ACCESSLOG */