]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/accesslog.c
Reworked recent backend API changes, now using a separate struct,
[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-2007 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_attr {
55         struct log_attr *next;
56         AttributeDescription *attr;
57 } log_attr;
58
59 typedef struct log_info {
60         BackendDB *li_db;
61         struct berval li_db_suffix;
62         slap_mask_t li_ops;
63         int li_age;
64         int li_cycle;
65         struct re_s *li_task;
66         Filter *li_oldf;
67         Entry *li_old;
68         log_attr *li_oldattrs;
69         int li_success;
70         ldap_pvt_thread_rmutex_t li_op_rmutex;
71         ldap_pvt_thread_mutex_t li_log_mutex;
72 } log_info;
73
74 static ConfigDriver log_cf_gen;
75
76 enum {
77         LOG_DB = 1,
78         LOG_OPS,
79         LOG_PURGE,
80         LOG_SUCCESS,
81         LOG_OLD,
82         LOG_OLDATTR
83 };
84
85 static ConfigTable log_cfats[] = {
86         { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_MAGIC|LOG_DB,
87                 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
88                         "DESC 'Suffix of database for log content' "
89                         "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
90         { "logops", "op|writes|reads|session|all", 2, 0, 0,
91                 ARG_MAGIC|LOG_OPS,
92                 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
93                         "DESC 'Operation types to log' "
94                         "EQUALITY caseIgnoreMatch "
95                         "SYNTAX OMsDirectoryString )", NULL, NULL },
96         { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
97                 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
98                         "DESC 'Log cleanup parameters' "
99                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
100         { "logsuccess", NULL, 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|LOG_SUCCESS,
101                 log_cf_gen, "( OLcfgOvAt:4.4 NAME 'olcAccessLogSuccess' "
102                         "DESC 'Log successful ops only' "
103                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
104         { "logold", "filter", 2, 2, 0, ARG_MAGIC|LOG_OLD,
105                 log_cf_gen, "( OLcfgOvAt:4.5 NAME 'olcAccessLogOld' "
106                         "DESC 'Log old values when modifying entries matching the filter' "
107                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
108         { "logoldattr", "attrs", 2, 0, 0, ARG_MAGIC|LOG_OLDATTR,
109                 log_cf_gen, "( OLcfgOvAt:4.6 NAME 'olcAccessLogOldAttr' "
110                         "DESC 'Log old values of these attributes even if unmodified' "
111                         "EQUALITY caseIgnoreMatch "
112                         "SYNTAX OMsDirectoryString )", NULL, NULL },
113         { NULL }
114 };
115
116 static ConfigOCs log_cfocs[] = {
117         { "( OLcfgOvOc:4.1 "
118                 "NAME 'olcAccessLogConfig' "
119                 "DESC 'Access log configuration' "
120                 "SUP olcOverlayConfig "
121                 "MUST olcAccessLogDB "
122                 "MAY ( olcAccessLogOps $ olcAccessLogPurge $ olcAccessLogSuccess $ "
123                         "olcAccessLogOld $ olcAccessLogOldAttr ) )",
124                         Cft_Overlay, log_cfats },
125         { NULL }
126 };
127
128 static slap_verbmasks logops[] = {
129         { BER_BVC("all"),               LOG_OP_ALL },
130         { BER_BVC("writes"),    LOG_OP_WRITES },
131         { BER_BVC("session"),   LOG_OP_SESSION },
132         { BER_BVC("reads"),             LOG_OP_READS },
133         { BER_BVC("add"),               LOG_OP_ADD },
134         { BER_BVC("delete"),    LOG_OP_DELETE },
135         { BER_BVC("modify"),    LOG_OP_MODIFY },
136         { BER_BVC("modrdn"),    LOG_OP_MODRDN },
137         { BER_BVC("compare"),   LOG_OP_COMPARE },
138         { BER_BVC("search"),    LOG_OP_SEARCH },
139         { BER_BVC("bind"),              LOG_OP_BIND },
140         { BER_BVC("unbind"),    LOG_OP_UNBIND },
141         { BER_BVC("abandon"),   LOG_OP_ABANDON },
142         { BER_BVC("extended"),  LOG_OP_EXTENDED },
143         { BER_BVC("unknown"),   LOG_OP_UNKNOWN },
144         { BER_BVNULL, 0 }
145 };
146
147 /* Start with "add" in logops */
148 #define EN_OFFSET       4
149
150 enum {
151         LOG_EN_ADD = 0,
152         LOG_EN_DELETE,
153         LOG_EN_MODIFY,
154         LOG_EN_MODRDN,
155         LOG_EN_COMPARE,
156         LOG_EN_SEARCH,
157         LOG_EN_BIND,
158         LOG_EN_UNBIND,
159         LOG_EN_ABANDON,
160         LOG_EN_EXTENDED,
161         LOG_EN_UNKNOWN,
162         LOG_EN__COUNT
163 };
164
165 static ObjectClass *log_ocs[LOG_EN__COUNT], *log_container;
166
167 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
168
169 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
170 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
171
172 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
173         *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
174         *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
175         *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
176         *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
177         *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
178         *ad_reqId, *ad_reqMessage, *ad_reqVersion, *ad_reqDerefAliases,
179         *ad_reqReferral, *ad_reqOld, *ad_auditContext;
180
181 static struct {
182         char *at;
183         AttributeDescription **ad;
184 } lattrs[] = {
185         { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
186                 "DESC 'Target DN of request' "
187                 "EQUALITY distinguishedNameMatch "
188                 "SYNTAX OMsDN "
189                 "SINGLE-VALUE )", &ad_reqDN },
190         { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
191                 "DESC 'Start time of request' "
192                 "EQUALITY generalizedTimeMatch "
193                 "ORDERING generalizedTimeOrderingMatch "
194                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
195                 "SINGLE-VALUE )", &ad_reqStart },
196         { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
197                 "DESC 'End time of request' "
198                 "EQUALITY generalizedTimeMatch "
199                 "ORDERING generalizedTimeOrderingMatch "
200                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
201                 "SINGLE-VALUE )", &ad_reqEnd },
202         { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
203                 "DESC 'Type of request' "
204                 "EQUALITY caseIgnoreMatch "
205                 "SYNTAX OMsDirectoryString "
206                 "SINGLE-VALUE )", &ad_reqType },
207         { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
208                 "DESC 'Session ID of request' "
209                 "EQUALITY caseIgnoreMatch "
210                 "SYNTAX OMsDirectoryString "
211                 "SINGLE-VALUE )", &ad_reqSession },
212         { "( " LOG_SCHEMA_AT ".6 NAME 'reqAuthzID' "
213                 "DESC 'Authorization ID of requestor' "
214                 "EQUALITY distinguishedNameMatch "
215                 "SYNTAX OMsDN "
216                 "SINGLE-VALUE )", &ad_reqAuthzID },
217         { "( " LOG_SCHEMA_AT ".7 NAME 'reqResult' "
218                 "DESC 'Result code of request' "
219                 "EQUALITY integerMatch "
220                 "ORDERING integerOrderingMatch "
221                 "SYNTAX OMsInteger "
222                 "SINGLE-VALUE )", &ad_reqResult },
223         { "( " LOG_SCHEMA_AT ".8 NAME 'reqMessage' "
224                 "DESC 'Error text of request' "
225                 "EQUALITY caseIgnoreMatch "
226                 "SUBSTR caseIgnoreSubstringsMatch "
227                 "SYNTAX OMsDirectoryString "
228                 "SINGLE-VALUE )", &ad_reqMessage },
229         { "( " LOG_SCHEMA_AT ".9 NAME 'reqReferral' "
230                 "DESC 'Referrals returned for request' "
231                 "SUP labeledURI )", &ad_reqReferral },
232         { "( " LOG_SCHEMA_AT ".10 NAME 'reqControls' "
233                 "DESC 'Request controls' "
234                 "SYNTAX OMsOctetString )", &ad_reqControls },
235         { "( " LOG_SCHEMA_AT ".11 NAME 'reqRespControls' "
236                 "DESC 'Response controls of request' "
237                 "SYNTAX OMsOctetString )", &ad_reqRespControls },
238         { "( " LOG_SCHEMA_AT ".12 NAME 'reqId' "
239                 "DESC 'ID of Request to Abandon' "
240                 "EQUALITY integerMatch "
241                 "ORDERING integerOrderingMatch "
242                 "SYNTAX OMsInteger "
243                 "SINGLE-VALUE )", &ad_reqId },
244         { "( " LOG_SCHEMA_AT ".13 NAME 'reqVersion' "
245                 "DESC 'Protocol version of Bind request' "
246                 "EQUALITY integerMatch "
247                 "ORDERING integerOrderingMatch "
248                 "SYNTAX OMsInteger "
249                 "SINGLE-VALUE )", &ad_reqVersion },
250         { "( " LOG_SCHEMA_AT ".14 NAME 'reqMethod' "
251                 "DESC 'Bind method of request' "
252                 "EQUALITY caseIgnoreMatch "
253                 "SYNTAX OMsDirectoryString "
254                 "SINGLE-VALUE )", &ad_reqMethod },
255         { "( " LOG_SCHEMA_AT ".15 NAME 'reqAssertion' "
256                 "DESC 'Compare Assertion of request' "
257                 "SYNTAX OMsDirectoryString "
258                 "SINGLE-VALUE )", &ad_reqAssertion },
259         { "( " LOG_SCHEMA_AT ".16 NAME 'reqMod' "
260                 "DESC 'Modifications of request' "
261                 "EQUALITY octetStringMatch "
262                 "SUBSTR octetStringSubstringsMatch "
263                 "SYNTAX OMsOctetString )", &ad_reqMod },
264         { "( " LOG_SCHEMA_AT ".17 NAME 'reqOld' "
265                 "DESC 'Old values of entry before request completed' "
266                 "EQUALITY octetStringMatch "
267                 "SUBSTR octetStringSubstringsMatch "
268                 "SYNTAX OMsOctetString )", &ad_reqOld },
269         { "( " LOG_SCHEMA_AT ".18 NAME 'reqNewRDN' "
270                 "DESC 'New RDN of request' "
271                 "EQUALITY distinguishedNameMatch "
272                 "SYNTAX OMsDN "
273                 "SINGLE-VALUE )", &ad_reqNewRDN },
274         { "( " LOG_SCHEMA_AT ".19 NAME 'reqDeleteOldRDN' "
275                 "DESC 'Delete old RDN' "
276                 "EQUALITY booleanMatch "
277                 "SYNTAX OMsBoolean "
278                 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
279         { "( " LOG_SCHEMA_AT ".20 NAME 'reqNewSuperior' "
280                 "DESC 'New superior DN of request' "
281                 "EQUALITY distinguishedNameMatch "
282                 "SYNTAX OMsDN "
283                 "SINGLE-VALUE )", &ad_reqNewSuperior },
284         { "( " LOG_SCHEMA_AT ".21 NAME 'reqScope' "
285                 "DESC 'Scope of request' "
286                 "EQUALITY caseIgnoreMatch "
287                 "SYNTAX OMsDirectoryString "
288                 "SINGLE-VALUE )", &ad_reqScope },
289         { "( " LOG_SCHEMA_AT ".22 NAME 'reqDerefAliases' "
290                 "DESC 'Disposition of Aliases in request' "
291                 "EQUALITY caseIgnoreMatch "
292                 "SYNTAX OMsDirectoryString "
293                 "SINGLE-VALUE )", &ad_reqDerefAliases },
294         { "( " LOG_SCHEMA_AT ".23 NAME 'reqAttrsOnly' "
295                 "DESC 'Attributes and values of request' "
296                 "EQUALITY booleanMatch "
297                 "SYNTAX OMsBoolean "
298                 "SINGLE-VALUE )", &ad_reqAttrsOnly },
299         { "( " LOG_SCHEMA_AT ".24 NAME 'reqFilter' "
300                 "DESC 'Filter of request' "
301                 "EQUALITY caseIgnoreMatch "
302                 "SUBSTR caseIgnoreSubstringsMatch "
303                 "SYNTAX OMsDirectoryString "
304                 "SINGLE-VALUE )", &ad_reqFilter },
305         { "( " LOG_SCHEMA_AT ".25 NAME 'reqAttr' "
306                 "DESC 'Attributes of request' "
307                 "EQUALITY caseIgnoreMatch "
308                 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
309         { "( " LOG_SCHEMA_AT ".26 NAME 'reqSizeLimit' "
310                 "DESC 'Size limit of request' "
311                 "EQUALITY integerMatch "
312                 "ORDERING integerOrderingMatch "
313                 "SYNTAX OMsInteger "
314                 "SINGLE-VALUE )", &ad_reqSizeLimit },
315         { "( " LOG_SCHEMA_AT ".27 NAME 'reqTimeLimit' "
316                 "DESC 'Time limit of request' "
317                 "EQUALITY integerMatch "
318                 "ORDERING integerOrderingMatch "
319                 "SYNTAX OMsInteger "
320                 "SINGLE-VALUE )", &ad_reqTimeLimit },
321         { "( " LOG_SCHEMA_AT ".28 NAME 'reqEntries' "
322                 "DESC 'Number of entries returned' "
323                 "EQUALITY integerMatch "
324                 "ORDERING integerOrderingMatch "
325                 "SYNTAX OMsInteger "
326                 "SINGLE-VALUE )", &ad_reqEntries },
327         { "( " LOG_SCHEMA_AT ".29 NAME 'reqData' "
328                 "DESC 'Data of extended request' "
329                 "EQUALITY octetStringMatch "
330                 "SUBSTR octetStringSubstringsMatch "
331                 "SYNTAX OMsOctetString "
332                 "SINGLE-VALUE )", &ad_reqData },
333
334         /*
335          * from <draft-chu-ldap-logschema-01.txt>:
336          *
337
338    ( LOG_SCHEMA_AT .30 NAME 'auditContext'
339    DESC 'DN of auditContainer'
340    EQUALITY distinguishedNameMatch
341    SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
342    SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )
343
344          * - removed EQUALITY matchingRule
345          * - changed directoryOperation in dSAOperation
346          */
347         { "( " LOG_SCHEMA_AT ".30 NAME 'auditContext' "
348                 "DESC 'DN of auditContainer' "
349                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
350                 "SINGLE-VALUE "
351                 "NO-USER-MODIFICATION "
352                 "USAGE dSAOperation )", &ad_auditContext },
353         { NULL, NULL }
354 };
355
356 static struct {
357         char *ot;
358         ObjectClass **oc;
359 } locs[] = {
360         { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
361                 "DESC 'AuditLog container' "
362                 "SUP top STRUCTURAL "
363                 "MAY ( cn $ reqStart $ reqEnd ) )", &log_container },
364         { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
365                 "DESC 'OpenLDAP request auditing' "
366                 "SUP top STRUCTURAL "
367                 "MUST ( reqStart $ reqType $ reqSession ) "
368                 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
369                         "reqResult $ reqMessage $ reqReferral ) )",
370                                 &log_ocs[LOG_EN_UNBIND] },
371         { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
372                 "DESC 'OpenLDAP read request record' "
373                 "SUP auditObject STRUCTURAL )", NULL },
374         { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
375                 "DESC 'OpenLDAP write request record' "
376                 "SUP auditObject STRUCTURAL )", NULL },
377         { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
378                 "DESC 'Abandon operation' "
379                 "SUP auditObject STRUCTURAL "
380                 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
381         { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
382                 "DESC 'Add operation' "
383                 "SUP auditWriteObject STRUCTURAL "
384                 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
385         { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
386                 "DESC 'Bind operation' "
387                 "SUP auditObject STRUCTURAL "
388                 "MUST ( reqVersion $ reqMethod ) )", &log_ocs[LOG_EN_BIND] },
389         { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
390                 "DESC 'Compare operation' "
391                 "SUP auditReadObject STRUCTURAL "
392                 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
393         { "( " LOG_SCHEMA_OC ".8 NAME 'auditDelete' "
394                 "DESC 'Delete operation' "
395                 "SUP auditWriteObject STRUCTURAL "
396                 "MAY reqOld )", &log_ocs[LOG_EN_DELETE] },
397         { "( " LOG_SCHEMA_OC ".9 NAME 'auditModify' "
398                 "DESC 'Modify operation' "
399                 "SUP auditWriteObject STRUCTURAL "
400                 "MAY reqOld MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
401         { "( " LOG_SCHEMA_OC ".10 NAME 'auditModRDN' "
402                 "DESC 'ModRDN operation' "
403                 "SUP auditWriteObject STRUCTURAL "
404                 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
405                 "MAY ( reqNewSuperior $ reqMod $ reqOld ) )", &log_ocs[LOG_EN_MODRDN] },
406         { "( " LOG_SCHEMA_OC ".11 NAME 'auditSearch' "
407                 "DESC 'Search operation' "
408                 "SUP auditReadObject STRUCTURAL "
409                 "MUST ( reqScope $ reqDerefAliases $ reqAttrsonly ) "
410                 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
411                         "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
412         { "( " LOG_SCHEMA_OC ".12 NAME 'auditExtended' "
413                 "DESC 'Extended operation' "
414                 "SUP auditObject STRUCTURAL "
415                 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
416         { NULL, NULL }
417 };
418
419 #define RDNEQ   "reqStart="
420
421 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
422  * If a field is present, it must be two digits. (Except for
423  * days, which can be arbitrary width.)
424  */
425 static int
426 log_age_parse(char *agestr)
427 {
428         int t1, t2;
429         int gotdays = 0;
430         char *endptr;
431
432         t1 = strtol( agestr, &endptr, 10 );
433         /* Is there a days delimiter? */
434         if ( *endptr == '+' ) {
435                 /* 32 bit time only covers about 68 years */
436                 if ( t1 < 0 || t1 > 25000 )
437                         return -1;
438                 t1 *= 24;
439                 gotdays = 1;
440                 agestr = endptr + 1;
441         } else {
442                 if ( agestr[2] != ':' ) {
443                         /* No valid delimiter found, fail */
444                         return -1;
445                 }
446                 t1 *= 60;
447                 agestr += 3;
448         }
449
450         t2 = atoi( agestr );
451         t1 += t2;
452
453         if ( agestr[2] ) {
454                 /* if there's a delimiter, it can only be a colon */
455                 if ( agestr[2] != ':' )
456                         return -1;
457         } else {
458                 /* If we're at the end of the string, and we started with days,
459                  * fail because we expected to find minutes too.
460                  */
461                 return gotdays ? -1 : t1 * 60;
462         }
463
464         agestr += 3;
465         t2 = atoi( agestr );
466
467         /* last field can only be seconds */
468         if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
469                 return -1;
470         t1 *= 60;
471         t1 += t2;
472
473         if ( agestr[2] ) {
474                 agestr += 3;
475                 if ( agestr[2] )
476                         return -1;
477                 t1 *= 60;
478                 t1 += atoi( agestr );
479         } else if ( gotdays ) {
480                 /* only got days+hh:mm */
481                 t1 *= 60;
482         }
483         return t1;
484 }
485
486 static void
487 log_age_unparse( int age, struct berval *agebv )
488 {
489         int dd, hh, mm, ss;
490         char *ptr;
491
492         ss = age % 60;
493         age /= 60;
494         mm = age % 60;
495         age /= 60;
496         hh = age % 24;
497         age /= 24;
498         dd = age;
499
500         ptr = agebv->bv_val;
501
502         if ( dd ) 
503                 ptr += sprintf( ptr, "%d+", dd );
504         ptr += sprintf( ptr, "%02d:%02d", hh, mm );
505         if ( ss )
506                 ptr += sprintf( ptr, ":%02d", ss );
507
508         agebv->bv_len = ptr - agebv->bv_val;
509 }
510
511 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
512
513 #define PURGE_INCREMENT 100
514
515 typedef struct purge_data {
516         int slots;
517         int used;
518         BerVarray dn;
519         BerVarray ndn;
520         struct berval csn;      /* an arbitrary old CSN */
521 } purge_data;
522
523 static int
524 log_old_lookup( Operation *op, SlapReply *rs )
525 {
526         purge_data *pd = op->o_callback->sc_private;
527
528         if ( rs->sr_type != REP_SEARCH) return 0;
529
530         if ( slapd_shutdown ) return 0;
531
532         /* Remember old CSN */
533         if ( pd->csn.bv_val[0] == '\0' ) {
534                 Attribute *a = attr_find( rs->sr_entry->e_attrs,
535                         slap_schema.si_ad_entryCSN );
536                 if ( a ) {
537                         int len = a->a_vals[0].bv_len;
538                         if ( len > pd->csn.bv_len )
539                                 len = pd->csn.bv_len;
540                         AC_MEMCPY( pd->csn.bv_val, a->a_vals[0].bv_val, len );
541                         pd->csn.bv_len = len;
542                 }
543         }
544         if ( pd->used >= pd->slots ) {
545                 pd->slots += PURGE_INCREMENT;
546                 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
547                 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
548         }
549         ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
550         ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
551         pd->used++;
552         return 0;
553 }
554
555 /* Periodically search for old entries in the log database and delete them */
556 static void *
557 accesslog_purge( void *ctx, void *arg )
558 {
559         struct re_s *rtask = arg;
560         struct log_info *li = rtask->arg;
561
562         Connection conn = {0};
563         OperationBuffer opbuf;
564         Operation *op;
565         SlapReply rs = {REP_RESULT};
566         slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
567         Filter f;
568         AttributeAssertion ava = {0};
569         purge_data pd = {0};
570         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
571         char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
572         time_t old = slap_get_time();
573
574         connection_fake_init( &conn, &opbuf, ctx );
575         op = &opbuf.ob_op;
576
577         f.f_choice = LDAP_FILTER_LE;
578         f.f_ava = &ava;
579         f.f_next = NULL;
580
581         ava.aa_desc = ad_reqStart;
582         ava.aa_value.bv_val = timebuf;
583         ava.aa_value.bv_len = sizeof(timebuf);
584
585         old -= li->li_age;
586         slap_timestamp( &old, &ava.aa_value );
587
588         op->o_tag = LDAP_REQ_SEARCH;
589         op->o_bd = li->li_db;
590         op->o_dn = li->li_db->be_rootdn;
591         op->o_ndn = li->li_db->be_rootndn;
592         op->o_req_dn = li->li_db->be_suffix[0];
593         op->o_req_ndn = li->li_db->be_nsuffix[0];
594         op->o_callback = &cb;
595         op->ors_scope = LDAP_SCOPE_ONELEVEL;
596         op->ors_deref = LDAP_DEREF_NEVER;
597         op->ors_tlimit = SLAP_NO_LIMIT;
598         op->ors_slimit = SLAP_NO_LIMIT;
599         op->ors_filter = &f;
600         filter2bv_x( op, &f, &op->ors_filterstr );
601         op->ors_attrs = slap_anlist_no_attrs;
602         op->ors_attrsonly = 1;
603         
604         pd.csn.bv_len = sizeof( csnbuf );
605         pd.csn.bv_val = csnbuf;
606         csnbuf[0] = '\0';
607         cb.sc_private = &pd;
608
609         op->o_bd->be_search( op, &rs );
610         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
611
612         if ( pd.used ) {
613                 int i;
614
615                 op->o_tag = LDAP_REQ_DELETE;
616                 op->o_callback = &nullsc;
617                 op->o_csn = pd.csn;
618
619                 for (i=0; i<pd.used; i++) {
620                         op->o_req_dn = pd.dn[i];
621                         op->o_req_ndn = pd.ndn[i];
622                         if ( !slapd_shutdown )
623                                 op->o_bd->be_delete( op, &rs );
624                         ch_free( pd.ndn[i].bv_val );
625                         ch_free( pd.dn[i].bv_val );
626                 }
627                 ch_free( pd.ndn );
628                 ch_free( pd.dn );
629         }
630
631         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
632         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
633         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
634
635         return NULL;
636 }
637
638 static int
639 log_cf_gen(ConfigArgs *c)
640 {
641         slap_overinst *on = (slap_overinst *)c->bi;
642         struct log_info *li = on->on_bi.bi_private;
643         int rc = 0;
644         slap_mask_t tmask = 0;
645         char agebuf[2*STRLENOF("ddddd+hh:mm:ss  ")];
646         struct berval agebv, cyclebv;
647
648         switch( c->op ) {
649         case SLAP_CONFIG_EMIT:
650                 switch( c->type ) {
651                 case LOG_DB:
652                         if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
653                                 value_add_one( &c->rvalue_vals, &li->li_db_suffix );
654                                 value_add_one( &c->rvalue_nvals, &li->li_db_suffix );
655                         } else if ( li->li_db ) {
656                                 value_add_one( &c->rvalue_vals, li->li_db->be_suffix );
657                                 value_add_one( &c->rvalue_nvals, li->li_db->be_nsuffix );
658                         } else {
659                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
660                                         "accesslog: \"logdb <suffix>\" must be specified" );
661                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
662                                         c->log, c->cr_msg, c->value_dn.bv_val );
663                                 rc = 1;
664                                 break;
665                         }
666                         break;
667                 case LOG_OPS:
668                         rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
669                         break;
670                 case LOG_PURGE:
671                         if ( !li->li_age ) {
672                                 rc = 1;
673                                 break;
674                         }
675                         agebv.bv_val = agebuf;
676                         log_age_unparse( li->li_age, &agebv );
677                         agebv.bv_val[agebv.bv_len] = ' ';
678                         agebv.bv_len++;
679                         cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
680                         log_age_unparse( li->li_cycle, &cyclebv );
681                         agebv.bv_len += cyclebv.bv_len;
682                         value_add_one( &c->rvalue_vals, &agebv );
683                         break;
684                 case LOG_SUCCESS:
685                         if ( li->li_success )
686                                 c->value_int = li->li_success;
687                         else
688                                 rc = 1;
689                         break;
690                 case LOG_OLD:
691                         if ( li->li_oldf ) {
692                                 filter2bv( li->li_oldf, &agebv );
693                                 ber_bvarray_add( &c->rvalue_vals, &agebv );
694                         }
695                         else
696                                 rc = 1;
697                         break;
698                 case LOG_OLDATTR:
699                         if ( li->li_oldattrs ) {
700                                 log_attr *la;
701
702                                 for ( la = li->li_oldattrs; la; la=la->next )
703                                         value_add_one( &c->rvalue_vals, &la->attr->ad_cname );
704                         }
705                         else
706                                 rc = 1;
707                         break;
708                 }
709                 break;
710         case LDAP_MOD_DELETE:
711                 switch( c->type ) {
712                 case LOG_DB:
713                         /* noop. this should always be a valid backend. */
714                         break;
715                 case LOG_OPS:
716                         if ( c->valx < 0 ) {
717                                 li->li_ops = 0;
718                         } else {
719                                 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
720                                 if ( rc == 0 )
721                                         li->li_ops &= ~tmask;
722                         }
723                         break;
724                 case LOG_PURGE:
725                         if ( li->li_task ) {
726                                 struct re_s *re = li->li_task;
727                                 li->li_task = NULL;
728                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
729                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
730                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
731                         }
732                         li->li_age = 0;
733                         li->li_cycle = 0;
734                         break;
735                 case LOG_SUCCESS:
736                         li->li_success = 0;
737                         break;
738                 case LOG_OLD:
739                         if ( li->li_oldf ) {
740                                 filter_free( li->li_oldf );
741                                 li->li_oldf = NULL;
742                         }
743                         break;
744                 case LOG_OLDATTR:
745                         if ( c->valx < 0 ) {
746                                 log_attr *la, *ln;
747
748                                 for ( la = li->li_oldattrs; la; la = ln ) {
749                                         ln = la->next;
750                                         ch_free( la );
751                                 }
752                         } else {
753                                 log_attr *la = NULL, **lp;
754                                 int i;
755
756                                 for ( lp = &li->li_oldattrs, i=0; i < c->valx; i++ ) {
757                                         la = *lp;
758                                         lp = &la->next;
759                                 }
760                                 *lp = la->next;
761                                 ch_free( la );
762                         }
763                         break;
764                 }
765                 break;
766         default:
767                 switch( c->type ) {
768                 case LOG_DB:
769                         if ( CONFIG_ONLINE_ADD( c )) {
770                                 li->li_db = select_backend( &c->value_ndn, 0 );
771                                 if ( !li->li_db ) {
772                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
773                                                 "<%s> no matching backend found for suffix",
774                                                 c->argv[0] );
775                                         Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
776                                                 c->log, c->cr_msg, c->value_dn.bv_val );
777                                         rc = 1;
778                                 }
779                                 ch_free( c->value_ndn.bv_val );
780                         } else {
781                                 li->li_db_suffix = c->value_ndn;
782                         }
783                         ch_free( c->value_dn.bv_val );
784                         break;
785                 case LOG_OPS:
786                         rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
787                         if ( rc == 0 )
788                                 li->li_ops |= tmask;
789                         break;
790                 case LOG_PURGE:
791                         li->li_age = log_age_parse( c->argv[1] );
792                         if ( li->li_age < 1 ) {
793                                 rc = 1;
794                         } else {
795                                 li->li_cycle = log_age_parse( c->argv[2] );
796                                 if ( li->li_cycle < 1 ) {
797                                         rc = 1;
798                                 } else if ( slapMode & SLAP_SERVER_MODE ) {
799                                         struct re_s *re = li->li_task;
800                                         if ( re )
801                                                 re->interval.tv_sec = li->li_cycle;
802                                         else
803                                                 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
804                                                         li->li_cycle, accesslog_purge, li,
805                                                         "accesslog_purge", li->li_db ?
806                                                                 li->li_db->be_suffix[0].bv_val :
807                                                                 c->be->be_suffix[0].bv_val );
808                                 }
809                         }
810                         break;
811                 case LOG_SUCCESS:
812                         li->li_success = c->value_int;
813                         break;
814                 case LOG_OLD:
815                         li->li_oldf = str2filter( c->argv[1] );
816                         if ( !li->li_oldf ) {
817                                 sprintf( c->cr_msg, "bad filter!" );
818                                 rc = 1;
819                         }
820                         break;
821                 case LOG_OLDATTR: {
822                         int i;
823                         AttributeDescription *ad;
824                         const char *text;
825
826                         for ( i=1; i< c->argc; i++ ) {
827                                 ad = NULL;
828                                 if ( slap_str2ad( c->argv[i], &ad, &text ) == LDAP_SUCCESS ) {
829                                         log_attr *la = ch_malloc( sizeof( log_attr ));
830                                         la->attr = ad;
831                                         la->next = li->li_oldattrs;
832                                         li->li_oldattrs = la;
833                                 } else {
834                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s <%s>: %s",
835                                                 c->argv[0], c->argv[i], text );
836                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
837                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
838                                         rc = ARG_BAD_CONF;
839                                         break;
840                                 }
841                         }
842                         }
843                         break;
844                 }
845                 break;
846         }
847         return rc;
848 }
849
850 static Entry *accesslog_entry( Operation *op, int logop,
851         Operation *op2 ) {
852         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
853         log_info *li = on->on_bi.bi_private;
854
855         char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
856         char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
857
858         struct berval rdn, nrdn, timestamp, ntimestamp, bv;
859         slap_verbmasks *lo = logops+logop+EN_OFFSET;
860
861         Entry *e = entry_alloc();
862
863         strcpy( rdnbuf, RDNEQ );
864         rdn.bv_val = rdnbuf;
865         strcpy( nrdnbuf, RDNEQ );
866         nrdn.bv_val = nrdnbuf;
867
868         timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
869         timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
870         slap_timestamp( &op->o_time, &timestamp );
871         sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
872         timestamp.bv_len += 7;
873
874         rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
875         ad_reqStart->ad_type->sat_equality->smr_normalize(
876                 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
877                 ad_reqStart->ad_type->sat_equality, &timestamp, &ntimestamp,
878                 op->o_tmpmemctx );
879
880         strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
881         nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
882         build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
883         build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
884
885         attr_merge_one( e, slap_schema.si_ad_objectClass,
886                 &log_ocs[logop]->soc_cname, NULL );
887         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
888                 &log_ocs[logop]->soc_cname, NULL );
889         attr_merge_one( e, ad_reqStart, &timestamp, &ntimestamp );
890         op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
891
892         slap_op_time( &op2->o_time, &op2->o_tincr );
893
894         timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
895         slap_timestamp( &op2->o_time, &timestamp );
896         sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op2->o_tincr );
897         timestamp.bv_len += 7;
898
899         attr_merge_normalize_one( e, ad_reqEnd, &timestamp, op->o_tmpmemctx );
900
901         /* Exops have OID appended */
902         if ( logop == LOG_EN_EXTENDED ) {
903                 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
904                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
905                 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
906                 bv.bv_val[lo->word.bv_len] = '{';
907                 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
908                         op->ore_reqoid.bv_len );
909                 bv.bv_val[bv.bv_len-1] = '}';
910                 bv.bv_val[bv.bv_len] = '\0';
911                 attr_merge_one( e, ad_reqType, &bv, NULL );
912         } else {
913                 attr_merge_one( e, ad_reqType, &lo->word, NULL );
914         }
915
916         rdn.bv_len = sprintf( rdn.bv_val, "%lu", op->o_connid );
917         attr_merge_one( e, ad_reqSession, &rdn, NULL );
918
919         if ( BER_BVISNULL( &op->o_dn )) 
920                 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
921                         (struct berval *)&slap_empty_bv );
922         else
923                 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
924
925         /* FIXME: need to add reqControls and reqRespControls */
926
927         return e;
928 }
929
930 static struct berval scopes[] = {
931         BER_BVC("base"),
932         BER_BVC("one"),
933         BER_BVC("sub"),
934         BER_BVC("subord")
935 };
936
937 static struct berval derefs[] = {
938         BER_BVC("never"),
939         BER_BVC("searching"),
940         BER_BVC("finding"),
941         BER_BVC("always")
942 };
943
944 static struct berval simple = BER_BVC("SIMPLE");
945
946 static void accesslog_val2val(AttributeDescription *ad, struct berval *val,
947         char c_op, struct berval *dst) {
948         char *ptr;
949
950         dst->bv_len = ad->ad_cname.bv_len + val->bv_len + 2;
951         if ( c_op ) dst->bv_len++;
952
953         dst->bv_val = ch_malloc( dst->bv_len+1 );
954
955         ptr = lutil_strcopy( dst->bv_val, ad->ad_cname.bv_val );
956         *ptr++ = ':';
957         if ( c_op )
958                 *ptr++ = c_op;
959         *ptr++ = ' ';
960         AC_MEMCPY( ptr, val->bv_val, val->bv_len );
961         dst->bv_val[dst->bv_len] = '\0';
962 }
963
964 static int accesslog_response(Operation *op, SlapReply *rs) {
965         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
966         log_info *li = on->on_bi.bi_private;
967         Attribute *a, *last_attr;
968         Modifications *m;
969         struct berval *b;
970         int i;
971         int logop;
972         slap_verbmasks *lo;
973         Entry *e = NULL, *old = NULL;
974         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
975         struct berval bv;
976         char *ptr;
977         BerVarray vals;
978         Operation op2 = {0};
979         SlapReply rs2 = {REP_RESULT};
980
981         if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
982                 return SLAP_CB_CONTINUE;
983
984         switch ( op->o_tag ) {
985         case LDAP_REQ_ADD:              logop = LOG_EN_ADD; break;
986         case LDAP_REQ_DELETE:   logop = LOG_EN_DELETE; break;
987         case LDAP_REQ_MODIFY:   logop = LOG_EN_MODIFY; break;
988         case LDAP_REQ_MODRDN:   logop = LOG_EN_MODRDN; break;
989         case LDAP_REQ_COMPARE:  logop = LOG_EN_COMPARE; break;
990         case LDAP_REQ_SEARCH:   logop = LOG_EN_SEARCH; break;
991         case LDAP_REQ_BIND:             logop = LOG_EN_BIND; break;
992         case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
993         default:        /* unknown operation type */
994                 logop = LOG_EN_UNKNOWN; break;
995         }       /* Unbind and Abandon never reach here */
996
997         lo = logops+logop+EN_OFFSET;
998         if ( !( li->li_ops & lo->mask ))
999                 return SLAP_CB_CONTINUE;
1000
1001         if ( lo->mask & LOG_OP_WRITES ) {
1002                 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
1003                 old = li->li_old;
1004                 li->li_old = NULL;
1005                 ldap_pvt_thread_rmutex_unlock( &li->li_op_rmutex, op->o_tid );
1006         }
1007
1008         if ( li->li_success && rs->sr_err != LDAP_SUCCESS )
1009                 goto done;
1010
1011         e = accesslog_entry( op, logop, &op2 );
1012
1013         attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
1014
1015         if ( rs->sr_text ) {
1016                 ber_str2bv( rs->sr_text, 0, 0, &bv );
1017                 attr_merge_one( e, ad_reqMessage, &bv, NULL );
1018         }
1019         bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
1020         bv.bv_val = timebuf;
1021
1022         attr_merge_one( e, ad_reqResult, &bv, NULL );
1023
1024         last_attr = attr_find( e->e_attrs, ad_reqResult );
1025
1026         switch( logop ) {
1027         case LOG_EN_ADD:
1028         case LOG_EN_DELETE: {
1029                 char c_op;
1030                 Entry *e2;
1031
1032                 if ( logop == LOG_EN_ADD ) {
1033                         e2 = op->ora_e;
1034                         c_op = '+';
1035                 } else {
1036                         if ( !old )
1037                                 break;
1038                         e2 = old;
1039                         c_op = 0;
1040                 }
1041                 /* count all the vals */
1042                 i = 0;
1043                 for ( a=e2->e_attrs; a; a=a->a_next ) {
1044                         if ( a->a_vals ) {
1045                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
1046                                         i++;
1047                                 }
1048                         }
1049                 }
1050                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1051                 i = 0;
1052                 for ( a=e2->e_attrs; a; a=a->a_next ) {
1053                         if ( a->a_vals ) {
1054                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1055                                         accesslog_val2val( a->a_desc, b, c_op, &vals[i] );
1056                                 }
1057                         }
1058                 }
1059                 vals[i].bv_val = NULL;
1060                 vals[i].bv_len = 0;
1061                 a = attr_alloc( logop == LOG_EN_ADD ? ad_reqMod : ad_reqOld );
1062                 a->a_vals = vals;
1063                 a->a_nvals = vals;
1064                 last_attr->a_next = a;
1065                 break;
1066         }
1067
1068         case LOG_EN_MODRDN:
1069         case LOG_EN_MODIFY:
1070                 /* count all the mods */
1071                 i = 0;
1072                 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1073                         if ( m->sml_values ) {
1074                                 for ( b = m->sml_values; !BER_BVISNULL( b ); b++ ) {
1075                                         i++;
1076                                 }
1077                         } else if ( m->sml_op == LDAP_MOD_DELETE ||
1078                                 m->sml_op == LDAP_MOD_REPLACE )
1079                         {
1080                                 i++;
1081                         }
1082                 }
1083                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1084                 i = 0;
1085
1086                 /* init flags on old entry */
1087                 if ( old ) {
1088                         for ( a = old->e_attrs; a; a = a->a_next ) {
1089                                 log_attr *la;
1090                                 a->a_flags = 0;
1091
1092                                 /* look for attrs that are always logged */
1093                                 for ( la = li->li_oldattrs; la; la = la->next ) {
1094                                         if ( a->a_desc == la->attr ) {
1095                                                 a->a_flags = 1;
1096                                         }
1097                                 }
1098                         }
1099                 }
1100
1101                 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1102                         /* Mark this attribute as modified */
1103                         if ( old ) {
1104                                 a = attr_find( old->e_attrs, m->sml_desc );
1105                                 if ( a ) {
1106                                         a->a_flags = 1;
1107                                 }
1108                         }
1109
1110                         /* don't log the RDN mods; they're explicitly logged later */
1111                         if ( logop == LOG_EN_MODRDN &&
1112                                 ( m->sml_op == SLAP_MOD_SOFTADD ||
1113                                   m->sml_op == LDAP_MOD_DELETE ) )
1114                         {
1115                                 continue;
1116                         }
1117
1118                         if ( m->sml_values ) {
1119                                 for ( b = m->sml_values; !BER_BVISNULL( b ); b++, i++ ) {
1120                                         char c_op;
1121
1122                                         switch ( m->sml_op ) {
1123                                         case LDAP_MOD_ADD: c_op = '+'; break;
1124                                         case LDAP_MOD_DELETE:   c_op = '-'; break;
1125                                         case LDAP_MOD_REPLACE:  c_op = '='; break;
1126                                         case LDAP_MOD_INCREMENT:        c_op = '#'; break;
1127
1128                                         /* unknown op. there shouldn't be any of these. we
1129                                          * don't know what to do with it, but we shouldn't just
1130                                          * ignore it.
1131                                          */
1132                                         default: c_op = '?'; break;
1133                                         }
1134                                         accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
1135                                 }
1136                         } else if ( m->sml_op == LDAP_MOD_DELETE ||
1137                                 m->sml_op == LDAP_MOD_REPLACE )
1138                         {
1139                                 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
1140                                 vals[i].bv_val = ch_malloc( vals[i].bv_len + 1 );
1141                                 ptr = lutil_strcopy( vals[i].bv_val,
1142                                         m->sml_desc->ad_cname.bv_val );
1143                                 *ptr++ = ':';
1144                                 if ( m->sml_op == LDAP_MOD_DELETE ) {
1145                                         *ptr++ = '-';
1146                                 } else {
1147                                         *ptr++ = '=';
1148                                 }
1149                                 *ptr = '\0';
1150                                 i++;
1151                         }
1152                 }
1153
1154                 if ( i > 0 ) {
1155                         BER_BVZERO( &vals[i] );
1156                         a = attr_alloc( ad_reqMod );
1157                         a->a_vals = vals;
1158                         a->a_nvals = vals;
1159                         last_attr->a_next = a;
1160                         last_attr = a;
1161
1162                 } else {
1163                         ch_free( vals );
1164                 }
1165
1166                 if ( old ) {
1167                         /* count all the vals */
1168                         i = 0;
1169                         for ( a = old->e_attrs; a != NULL; a = a->a_next ) {
1170                                 if ( a->a_vals && a->a_flags ) {
1171                                         for ( b = a->a_vals; !BER_BVISNULL( b ); b++ ) {
1172                                                 i++;
1173                                         }
1174                                 }
1175                         }
1176                         vals = ch_malloc( (i + 1) * sizeof( struct berval ) );
1177                         i = 0;
1178                         for ( a=old->e_attrs; a; a=a->a_next ) {
1179                                 if ( a->a_vals && a->a_flags ) {
1180                                         for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1181                                                 accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1182                                         }
1183                                 }
1184                         }
1185                         vals[i].bv_val = NULL;
1186                         vals[i].bv_len = 0;
1187                         a = attr_alloc( ad_reqOld );
1188                         a->a_vals = vals;
1189                         a->a_nvals = vals;
1190                         last_attr->a_next = a;
1191                 }
1192                 if ( logop == LOG_EN_MODIFY ) {
1193                         break;
1194                 }
1195
1196                 /* Now log the actual modRDN info */
1197                 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
1198                 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
1199                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1200                         NULL );
1201                 if ( op->orr_newSup ) {
1202                         attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
1203                 }
1204                 break;
1205
1206         case LOG_EN_COMPARE:
1207                 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
1208                         op->orc_ava->aa_value.bv_len;
1209                 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
1210                 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
1211                 *ptr++ = '=';
1212                 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
1213                 bv.bv_val[bv.bv_len] = '\0';
1214                 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
1215                 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1216                 break;
1217
1218         case LOG_EN_SEARCH:
1219                 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
1220                 attr_merge_one( e, ad_reqDerefAliases, &derefs[op->ors_deref], NULL );
1221                 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
1222                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1223                         NULL );
1224                 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
1225                         attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
1226                 if ( op->ors_attrs ) {
1227                         /* count them */
1228                         for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1229                                 ;
1230                         vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
1231                                 op->o_tmpmemctx );
1232                         for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1233                                 vals[i] = op->ors_attrs[i].an_name;
1234                         vals[i].bv_val = NULL;
1235                         vals[i].bv_len = 0;
1236                         attr_merge( e, ad_reqAttr, vals, NULL );
1237                         op->o_tmpfree( vals, op->o_tmpmemctx );
1238                 }
1239                 bv.bv_val = timebuf;
1240                 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
1241                 attr_merge_one( e, ad_reqEntries, &bv, NULL );
1242
1243                 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
1244                 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
1245
1246                 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_slimit );
1247                 attr_merge_one( e, ad_reqSizeLimit, &bv, NULL );
1248                 break;
1249
1250         case LOG_EN_BIND:
1251                 bv.bv_val = timebuf;
1252                 bv.bv_len = sprintf( bv.bv_val, "%d", op->o_protocol );
1253                 attr_merge_one( e, ad_reqVersion, &bv, NULL );
1254                 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
1255                         attr_merge_one( e, ad_reqMethod, &simple, NULL );
1256                 } else {
1257                         bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
1258                         bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
1259                         ptr = lutil_strcopy( bv.bv_val, "SASL(" );
1260                         ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
1261                         *ptr++ = ')';
1262                         *ptr = '\0';
1263                         attr_merge_one( e, ad_reqMethod, &bv, NULL );
1264                         op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1265                 }
1266
1267                 break;
1268
1269         case LOG_EN_EXTENDED:
1270                 if ( op->ore_reqdata ) {
1271                         attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
1272                 }
1273                 break;
1274
1275         case LOG_EN_UNKNOWN:
1276                 /* we don't know its parameters, don't add any */
1277                 break;
1278         }
1279
1280         op2.o_hdr = op->o_hdr;
1281         op2.o_tag = LDAP_REQ_ADD;
1282         op2.o_bd = li->li_db;
1283         op2.o_dn = li->li_db->be_rootdn;
1284         op2.o_ndn = li->li_db->be_rootndn;
1285         op2.o_req_dn = e->e_name;
1286         op2.o_req_ndn = e->e_nname;
1287         op2.ora_e = e;
1288         op2.o_callback = &nullsc;
1289
1290         if (( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn )) {
1291                 slap_queue_csn( &op2, &op->o_csn );
1292         }
1293
1294         op2.o_bd->be_add( &op2, &rs2 );
1295         if ( e == op2.ora_e ) entry_free( e );
1296         e = NULL;
1297
1298 done:
1299         if ( lo->mask & LOG_OP_WRITES )
1300                 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
1301         if ( old ) entry_free( old );
1302         return SLAP_CB_CONTINUE;
1303 }
1304
1305 /* Since Bind success is sent by the frontend, it won't normally enter
1306  * the overlay response callback. Add another callback to make sure it
1307  * gets here.
1308  */
1309 static int
1310 accesslog_bind_resp( Operation *op, SlapReply *rs )
1311 {
1312         BackendDB *be, db;
1313         int rc;
1314         slap_callback *sc;
1315
1316         be = op->o_bd;
1317         db = *be;
1318         op->o_bd = &db;
1319         db.bd_info = op->o_callback->sc_private;
1320         rc = accesslog_response( op, rs );
1321         op->o_bd = be;
1322         sc = op->o_callback;
1323         op->o_callback = sc->sc_next;
1324         op->o_tmpfree( sc, op->o_tmpmemctx );
1325         return rc;
1326 }
1327
1328 static int
1329 accesslog_op_bind( Operation *op, SlapReply *rs )
1330 {
1331         slap_callback *sc;
1332
1333         sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1334         sc->sc_response = accesslog_bind_resp;
1335         sc->sc_private = op->o_bd->bd_info;
1336
1337         if ( op->o_callback ) {
1338                 sc->sc_next = op->o_callback->sc_next;
1339                 op->o_callback->sc_next = sc;
1340         } else {
1341                 op->o_callback = sc;
1342         }
1343         return SLAP_CB_CONTINUE;
1344 }
1345
1346 static int
1347 accesslog_op_mod( Operation *op, SlapReply *rs )
1348 {
1349         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1350         log_info *li = on->on_bi.bi_private;
1351
1352         if ( li->li_ops & LOG_OP_WRITES ) {
1353                 ldap_pvt_thread_rmutex_lock( &li->li_op_rmutex, op->o_tid );
1354                 if ( li->li_oldf && ( op->o_tag == LDAP_REQ_DELETE ||
1355                         op->o_tag == LDAP_REQ_MODIFY ||
1356                         ( op->o_tag == LDAP_REQ_MODRDN && li->li_oldattrs ))) {
1357                         int rc;
1358                         Entry *e;
1359
1360                         op->o_bd->bd_info = on->on_info->oi_orig;
1361                         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1362                         if ( e ) {
1363                                 if ( test_filter( op, e, li->li_oldf ) == LDAP_COMPARE_TRUE )
1364                                         li->li_old = entry_dup( e );
1365                                 be_entry_release_rw( op, e, 0 );
1366                         }
1367                         op->o_bd->bd_info = (BackendInfo *)on;
1368                 }
1369         }
1370         return SLAP_CB_CONTINUE;
1371 }
1372
1373 /* unbinds are broadcast to all backends; we only log it if this
1374  * backend was used for the original bind.
1375  */
1376 static int
1377 accesslog_unbind( Operation *op, SlapReply *rs )
1378 {
1379         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1380         if ( op->o_conn->c_authz_backend == on->on_info->oi_origdb ) {
1381                 log_info *li = on->on_bi.bi_private;
1382                 Operation op2 = {0};
1383                 void *cids[SLAP_MAX_CIDS];
1384                 SlapReply rs2 = {REP_RESULT};
1385                 Entry *e;
1386
1387                 if ( !( li->li_ops & LOG_OP_UNBIND ))
1388                         return SLAP_CB_CONTINUE;
1389
1390                 e = accesslog_entry( op, LOG_EN_UNBIND, &op2 );
1391                 op2.o_hdr = op->o_hdr;
1392                 op2.o_tag = LDAP_REQ_ADD;
1393                 op2.o_bd = li->li_db;
1394                 op2.o_dn = li->li_db->be_rootdn;
1395                 op2.o_ndn = li->li_db->be_rootndn;
1396                 op2.o_req_dn = e->e_name;
1397                 op2.o_req_ndn = e->e_nname;
1398                 op2.ora_e = e;
1399                 op2.o_callback = &nullsc;
1400                 op2.o_controls = cids;
1401                 memset(cids, 0, sizeof( cids ));
1402
1403                 op2.o_bd->be_add( &op2, &rs2 );
1404                 if ( e == op2.ora_e )
1405                         entry_free( e );
1406         }
1407         return SLAP_CB_CONTINUE;
1408 }
1409
1410 static int
1411 accesslog_abandon( Operation *op, SlapReply *rs )
1412 {
1413         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1414         log_info *li = on->on_bi.bi_private;
1415         Operation op2 = {0};
1416         void *cids[SLAP_MAX_CIDS];
1417         SlapReply rs2 = {REP_RESULT};
1418         Entry *e;
1419         char buf[64];
1420         struct berval bv;
1421
1422         if ( !op->o_time || !( li->li_ops & LOG_OP_ABANDON ))
1423                 return SLAP_CB_CONTINUE;
1424
1425         e = accesslog_entry( op, LOG_EN_ABANDON, &op2 );
1426         bv.bv_val = buf;
1427         bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
1428         attr_merge_one( e, ad_reqId, &bv, NULL );
1429
1430         op2.o_hdr = op->o_hdr;
1431         op2.o_tag = LDAP_REQ_ADD;
1432         op2.o_bd = li->li_db;
1433         op2.o_dn = li->li_db->be_rootdn;
1434         op2.o_ndn = li->li_db->be_rootndn;
1435         op2.o_req_dn = e->e_name;
1436         op2.o_req_ndn = e->e_nname;
1437         op2.ora_e = e;
1438         op2.o_callback = &nullsc;
1439         op2.o_controls = cids;
1440         memset(cids, 0, sizeof( cids ));
1441
1442         op2.o_bd->be_add( &op2, &rs2 );
1443         if ( e == op2.ora_e )
1444                 entry_free( e );
1445
1446         return SLAP_CB_CONTINUE;
1447 }
1448
1449 static int
1450 accesslog_operational( Operation *op, SlapReply *rs )
1451 {
1452         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1453         log_info *li = on->on_bi.bi_private;
1454
1455         if ( rs->sr_entry != NULL
1456                 && dn_match( &op->o_bd->be_nsuffix[0], &rs->sr_entry->e_nname ) )
1457         {
1458                 Attribute       **ap;
1459
1460                 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1461                         /* just count */ ;
1462
1463                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1464                                 ad_inlist( ad_auditContext, rs->sr_attrs ) )
1465                 {
1466                         *ap = attr_alloc( ad_auditContext );
1467                         value_add_one( &(*ap)->a_vals,
1468                                 &li->li_db->be_suffix[0] );
1469                         value_add_one( &(*ap)->a_nvals,
1470                                 &li->li_db->be_nsuffix[0] );
1471                 }
1472         }
1473
1474         return SLAP_CB_CONTINUE;
1475 }
1476
1477 static slap_overinst accesslog;
1478
1479 static int
1480 accesslog_db_init(
1481         BackendDB *be,
1482         ConfigReply *cr
1483 )
1484 {
1485         slap_overinst *on = (slap_overinst *)be->bd_info;
1486         log_info *li = ch_calloc(1, sizeof(log_info));
1487
1488         on->on_bi.bi_private = li;
1489         ldap_pvt_thread_rmutex_init( &li->li_op_rmutex );
1490         ldap_pvt_thread_mutex_init( &li->li_log_mutex );
1491         return 0;
1492 }
1493
1494 static int
1495 accesslog_db_destroy(
1496         BackendDB *be,
1497         ConfigReply *cr
1498 )
1499 {
1500         slap_overinst *on = (slap_overinst *)be->bd_info;
1501         log_info *li = on->on_bi.bi_private;
1502         log_attr *la;
1503
1504         if ( li->li_oldf )
1505                 filter_free( li->li_oldf );
1506         for ( la=li->li_oldattrs; la; la=li->li_oldattrs ) {
1507                 li->li_oldattrs = la->next;
1508                 ch_free( la );
1509         }
1510         ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
1511         ldap_pvt_thread_rmutex_destroy( &li->li_op_rmutex );
1512         free( li );
1513         return LDAP_SUCCESS;
1514 }
1515
1516 /* Create the logdb's root entry if it's missing */
1517 static void *
1518 accesslog_db_root(
1519         void *ctx,
1520         void *arg )
1521 {
1522         struct re_s *rtask = arg;
1523         slap_overinst *on = rtask->arg;
1524         log_info *li = on->on_bi.bi_private;
1525
1526         Connection conn = {0};
1527         OperationBuffer opbuf;
1528         Operation *op;
1529
1530         Entry *e;
1531         int rc;
1532
1533         connection_fake_init( &conn, &opbuf, ctx );
1534         op = &opbuf.ob_op;
1535         op->o_bd = li->li_db;
1536         op->o_dn = li->li_db->be_rootdn;
1537         op->o_ndn = li->li_db->be_rootndn;
1538         rc = be_entry_get_rw( op, li->li_db->be_nsuffix, NULL, NULL, 0, &e );
1539
1540         if ( e ) {
1541                 be_entry_release_rw( op, e, 0 );
1542
1543         } else {
1544                 SlapReply rs = {REP_RESULT};
1545                 struct berval rdn, nrdn, attr;
1546                 char *ptr;
1547                 AttributeDescription *ad = NULL;
1548                 const char *text = NULL;
1549                 Entry *e_ctx;
1550
1551                 e = entry_alloc();
1552                 ber_dupbv( &e->e_name, li->li_db->be_suffix );
1553                 ber_dupbv( &e->e_nname, li->li_db->be_nsuffix );
1554
1555                 attr_merge_one( e, slap_schema.si_ad_objectClass,
1556                         &log_container->soc_cname, NULL );
1557
1558                 dnRdn( &e->e_name, &rdn );
1559                 dnRdn( &e->e_nname, &nrdn );
1560                 ptr = ber_bvchr( &rdn, '=' );
1561
1562                 assert( ptr != NULL );
1563
1564                 attr.bv_val = rdn.bv_val;
1565                 attr.bv_len = ptr - rdn.bv_val;
1566
1567                 slap_bv2ad( &attr, &ad, &text );
1568
1569                 rdn.bv_val = ptr+1;
1570                 rdn.bv_len -= attr.bv_len + 1;
1571                 ptr = ber_bvchr( &nrdn, '=' );
1572                 nrdn.bv_len -= ptr - nrdn.bv_val + 1;
1573                 nrdn.bv_val = ptr+1;
1574                 attr_merge_one( e, ad, &rdn, &nrdn );
1575
1576                 /* Get contextCSN from main DB */
1577                 op->o_bd = on->on_info->oi_origdb;
1578                 rc = be_entry_get_rw( op, op->o_bd->be_nsuffix, NULL,
1579                         slap_schema.si_ad_contextCSN, 0, &e_ctx );
1580
1581                 if ( e_ctx ) {
1582                         Attribute *a;
1583
1584                         a = attr_find( e_ctx->e_attrs, slap_schema.si_ad_contextCSN );
1585                         if ( a ) {
1586                                 attr_merge( e, slap_schema.si_ad_entryCSN, a->a_vals, NULL );
1587                                 attr_merge( e, a->a_desc, a->a_vals, NULL );
1588                         }
1589                         be_entry_release_rw( op, e_ctx, 0 );
1590                 }
1591                 op->o_bd = li->li_db;
1592
1593                 op->ora_e = e;
1594                 op->o_req_dn = e->e_name;
1595                 op->o_req_ndn = e->e_nname;
1596                 op->o_callback = &nullsc;
1597                 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
1598                 rc = op->o_bd->be_add( op, &rs );
1599                 SLAP_DBFLAGS( op->o_bd ) ^= SLAP_DBFLAG_NOLASTMOD;
1600                 if ( e == op->ora_e )
1601                         entry_free( e );
1602         }
1603         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1604         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1605         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1606         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1607
1608         return NULL;
1609 }
1610
1611 static int
1612 accesslog_db_open(
1613         BackendDB *be,
1614         ConfigReply *cr
1615 )
1616 {
1617         slap_overinst *on = (slap_overinst *)be->bd_info;
1618         log_info *li = on->on_bi.bi_private;
1619
1620
1621         if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
1622                 li->li_db = select_backend( &li->li_db_suffix, 0 );
1623                 ch_free( li->li_db_suffix.bv_val );
1624                 BER_BVZERO( &li->li_db_suffix );
1625         }
1626         if ( li->li_db == NULL ) {
1627                 Debug( LDAP_DEBUG_ANY,
1628                         "accesslog: \"logdb <suffix>\" missing or invalid.\n",
1629                         0, 0, 0 );
1630                 return 1;
1631         }
1632
1633         if ( slapMode & SLAP_TOOL_MODE )
1634                 return 0;
1635
1636         if ( BER_BVISEMPTY( &li->li_db->be_rootndn )) {
1637                 ber_dupbv( &li->li_db->be_rootdn, li->li_db->be_suffix );
1638                 ber_dupbv( &li->li_db->be_rootndn, li->li_db->be_nsuffix );
1639         }
1640
1641         ldap_pvt_runqueue_insert( &slapd_rq, 3600, accesslog_db_root, on,
1642                 "accesslog_db_root", li->li_db->be_suffix[0].bv_val );
1643
1644         return 0;
1645 }
1646
1647 int accesslog_initialize()
1648 {
1649         int i, rc;
1650
1651         accesslog.on_bi.bi_type = "accesslog";
1652         accesslog.on_bi.bi_db_init = accesslog_db_init;
1653         accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1654         accesslog.on_bi.bi_db_open = accesslog_db_open;
1655
1656         accesslog.on_bi.bi_op_add = accesslog_op_mod;
1657         accesslog.on_bi.bi_op_bind = accesslog_op_bind;
1658         accesslog.on_bi.bi_op_delete = accesslog_op_mod;
1659         accesslog.on_bi.bi_op_modify = accesslog_op_mod;
1660         accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
1661         accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1662         accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1663         accesslog.on_bi.bi_operational = accesslog_operational;
1664         accesslog.on_response = accesslog_response;
1665
1666         accesslog.on_bi.bi_cf_ocs = log_cfocs;
1667
1668         nullsc.sc_response = slap_null_cb;
1669
1670         rc = config_register_schema( log_cfats, log_cfocs );
1671         if ( rc ) return rc;
1672
1673         /* log schema integration */
1674         for ( i=0; lattrs[i].at; i++ ) {
1675                 int code;
1676
1677                 code = register_at( lattrs[i].at, lattrs[i].ad, 0 );
1678                 if ( code ) {
1679                         Debug( LDAP_DEBUG_ANY,
1680                                 "accesslog_init: register_at failed\n", 0, 0, 0 );
1681                         return -1;
1682                 }
1683         }
1684         for ( i=0; locs[i].ot; i++ ) {
1685                 int code;
1686
1687                 code = register_oc( locs[i].ot, locs[i].oc, 0 );
1688                 if ( code ) {
1689                         Debug( LDAP_DEBUG_ANY,
1690                                 "accesslog_init: register_oc failed\n", 0, 0, 0 );
1691                         return -1;
1692                 }
1693         }
1694
1695         return overlay_register(&accesslog);
1696 }
1697
1698 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1699 int
1700 init_module( int argc, char *argv[] )
1701 {
1702         return accesslog_initialize();
1703 }
1704 #endif
1705
1706 #endif /* SLAPD_OVER_ACCESSLOG */