]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/accesslog.c
ITS#6656 add reqEntryUUID to logs of write ops
[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-2012 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_base {
60         struct log_base *lb_next;
61         slap_mask_t lb_ops;
62         struct berval lb_base;
63         struct berval lb_line;
64 } log_base;
65
66 typedef struct log_info {
67         BackendDB *li_db;
68         struct berval li_db_suffix;
69         slap_mask_t li_ops;
70         int li_age;
71         int li_cycle;
72         struct re_s *li_task;
73         Filter *li_oldf;
74         Entry *li_old;
75         log_attr *li_oldattrs;
76         struct berval li_uuid;
77         int li_success;
78         log_base *li_bases;
79         ldap_pvt_thread_rmutex_t li_op_rmutex;
80         ldap_pvt_thread_mutex_t li_log_mutex;
81 } log_info;
82
83 static ConfigDriver log_cf_gen;
84
85 enum {
86         LOG_DB = 1,
87         LOG_OPS,
88         LOG_PURGE,
89         LOG_SUCCESS,
90         LOG_OLD,
91         LOG_OLDATTR,
92         LOG_BASE
93 };
94
95 static ConfigTable log_cfats[] = {
96         { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_MAGIC|LOG_DB,
97                 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
98                         "DESC 'Suffix of database for log content' "
99                         "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
100         { "logops", "op|writes|reads|session|all", 2, 0, 0,
101                 ARG_MAGIC|LOG_OPS,
102                 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
103                         "DESC 'Operation types to log' "
104                         "EQUALITY caseIgnoreMatch "
105                         "SYNTAX OMsDirectoryString )", NULL, NULL },
106         { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
107                 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
108                         "DESC 'Log cleanup parameters' "
109                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
110         { "logsuccess", NULL, 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|LOG_SUCCESS,
111                 log_cf_gen, "( OLcfgOvAt:4.4 NAME 'olcAccessLogSuccess' "
112                         "DESC 'Log successful ops only' "
113                         "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
114         { "logold", "filter", 2, 2, 0, ARG_MAGIC|LOG_OLD,
115                 log_cf_gen, "( OLcfgOvAt:4.5 NAME 'olcAccessLogOld' "
116                         "DESC 'Log old values when modifying entries matching the filter' "
117                         "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
118         { "logoldattr", "attrs", 2, 0, 0, ARG_MAGIC|LOG_OLDATTR,
119                 log_cf_gen, "( OLcfgOvAt:4.6 NAME 'olcAccessLogOldAttr' "
120                         "DESC 'Log old values of these attributes even if unmodified' "
121                         "EQUALITY caseIgnoreMatch "
122                         "SYNTAX OMsDirectoryString )", NULL, NULL },
123         { "logbase", "op|writes|reads|session|all< <baseDN", 3, 3, 0,
124                 ARG_MAGIC|LOG_BASE,
125                 log_cf_gen, "( OLcfgOvAt:4.7 NAME 'olcAccessLogBase' "
126                         "DESC 'Operation types to log under a specific branch' "
127                         "EQUALITY caseIgnoreMatch "
128                         "SYNTAX OMsDirectoryString )", NULL, NULL },
129         { NULL }
130 };
131
132 static ConfigOCs log_cfocs[] = {
133         { "( OLcfgOvOc:4.1 "
134                 "NAME 'olcAccessLogConfig' "
135                 "DESC 'Access log configuration' "
136                 "SUP olcOverlayConfig "
137                 "MUST olcAccessLogDB "
138                 "MAY ( olcAccessLogOps $ olcAccessLogPurge $ olcAccessLogSuccess $ "
139                         "olcAccessLogOld $ olcAccessLogOldAttr $ olcAccessLogBase ) )",
140                         Cft_Overlay, log_cfats },
141         { NULL }
142 };
143
144 static slap_verbmasks logops[] = {
145         { BER_BVC("all"),               LOG_OP_ALL },
146         { BER_BVC("writes"),    LOG_OP_WRITES },
147         { BER_BVC("session"),   LOG_OP_SESSION },
148         { BER_BVC("reads"),             LOG_OP_READS },
149         { BER_BVC("add"),               LOG_OP_ADD },
150         { BER_BVC("delete"),    LOG_OP_DELETE },
151         { BER_BVC("modify"),    LOG_OP_MODIFY },
152         { BER_BVC("modrdn"),    LOG_OP_MODRDN },
153         { BER_BVC("compare"),   LOG_OP_COMPARE },
154         { BER_BVC("search"),    LOG_OP_SEARCH },
155         { BER_BVC("bind"),              LOG_OP_BIND },
156         { BER_BVC("unbind"),    LOG_OP_UNBIND },
157         { BER_BVC("abandon"),   LOG_OP_ABANDON },
158         { BER_BVC("extended"),  LOG_OP_EXTENDED },
159         { BER_BVC("unknown"),   LOG_OP_UNKNOWN },
160         { BER_BVNULL, 0 }
161 };
162
163 /* Start with "add" in logops */
164 #define EN_OFFSET       4
165
166 enum {
167         LOG_EN_ADD = 0,
168         LOG_EN_DELETE,
169         LOG_EN_MODIFY,
170         LOG_EN_MODRDN,
171         LOG_EN_COMPARE,
172         LOG_EN_SEARCH,
173         LOG_EN_BIND,
174         LOG_EN_UNBIND,
175         LOG_EN_ABANDON,
176         LOG_EN_EXTENDED,
177         LOG_EN_UNKNOWN,
178         LOG_EN__COUNT
179 };
180
181 static ObjectClass *log_ocs[LOG_EN__COUNT], *log_container,
182         *log_oc_read, *log_oc_write;
183
184 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
185
186 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
187 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
188 #define LOG_SCHEMA_SYN LOG_SCHEMA_ROOT ".3"
189
190 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
191         *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
192         *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
193         *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
194         *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
195         *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
196         *ad_reqId, *ad_reqMessage, *ad_reqVersion, *ad_reqDerefAliases,
197         *ad_reqReferral, *ad_reqOld, *ad_auditContext, *ad_reqEntryUUID;
198
199 static int
200 logSchemaControlValidate(
201         Syntax          *syntax,
202         struct berval   *val );
203
204 char    *mrControl[] = {
205         "objectIdentifierFirstComponentMatch",
206         NULL
207 };
208
209 static struct {
210         char                    *oid;
211         slap_syntax_defs_rec    syn;
212         char                    **mrs;
213 } lsyntaxes[] = {
214         { LOG_SCHEMA_SYN ".1" ,
215                 { "( " LOG_SCHEMA_SYN ".1 DESC 'Control' )",
216                         SLAP_SYNTAX_HIDE,
217                         NULL,
218                         logSchemaControlValidate,
219                         NULL },
220                 mrControl },
221         { NULL }
222 };
223
224 static struct {
225         char *at;
226         AttributeDescription **ad;
227 } lattrs[] = {
228         { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
229                 "DESC 'Target DN of request' "
230                 "EQUALITY distinguishedNameMatch "
231                 "SYNTAX OMsDN "
232                 "SINGLE-VALUE )", &ad_reqDN },
233         { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
234                 "DESC 'Start time of request' "
235                 "EQUALITY generalizedTimeMatch "
236                 "ORDERING generalizedTimeOrderingMatch "
237                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
238                 "SINGLE-VALUE )", &ad_reqStart },
239         { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
240                 "DESC 'End time of request' "
241                 "EQUALITY generalizedTimeMatch "
242                 "ORDERING generalizedTimeOrderingMatch "
243                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
244                 "SINGLE-VALUE )", &ad_reqEnd },
245         { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
246                 "DESC 'Type of request' "
247                 "EQUALITY caseIgnoreMatch "
248                 "SYNTAX OMsDirectoryString "
249                 "SINGLE-VALUE )", &ad_reqType },
250         { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
251                 "DESC 'Session ID of request' "
252                 "EQUALITY caseIgnoreMatch "
253                 "SYNTAX OMsDirectoryString "
254                 "SINGLE-VALUE )", &ad_reqSession },
255         { "( " LOG_SCHEMA_AT ".6 NAME 'reqAuthzID' "
256                 "DESC 'Authorization ID of requestor' "
257                 "EQUALITY distinguishedNameMatch "
258                 "SYNTAX OMsDN "
259                 "SINGLE-VALUE )", &ad_reqAuthzID },
260         { "( " LOG_SCHEMA_AT ".7 NAME 'reqResult' "
261                 "DESC 'Result code of request' "
262                 "EQUALITY integerMatch "
263                 "ORDERING integerOrderingMatch "
264                 "SYNTAX OMsInteger "
265                 "SINGLE-VALUE )", &ad_reqResult },
266         { "( " LOG_SCHEMA_AT ".8 NAME 'reqMessage' "
267                 "DESC 'Error text of request' "
268                 "EQUALITY caseIgnoreMatch "
269                 "SUBSTR caseIgnoreSubstringsMatch "
270                 "SYNTAX OMsDirectoryString "
271                 "SINGLE-VALUE )", &ad_reqMessage },
272         { "( " LOG_SCHEMA_AT ".9 NAME 'reqReferral' "
273                 "DESC 'Referrals returned for request' "
274                 "SUP labeledURI )", &ad_reqReferral },
275         { "( " LOG_SCHEMA_AT ".10 NAME 'reqControls' "
276                 "DESC 'Request controls' "
277                 "EQUALITY objectIdentifierFirstComponentMatch "
278                 "SYNTAX " LOG_SCHEMA_SYN ".1 "
279                 "X-ORDERED 'VALUES' )", &ad_reqControls },
280         { "( " LOG_SCHEMA_AT ".11 NAME 'reqRespControls' "
281                 "DESC 'Response controls of request' "
282                 "EQUALITY objectIdentifierFirstComponentMatch "
283                 "SYNTAX " LOG_SCHEMA_SYN ".1 "
284                 "X-ORDERED 'VALUES' )", &ad_reqRespControls },
285         { "( " LOG_SCHEMA_AT ".12 NAME 'reqId' "
286                 "DESC 'ID of Request to Abandon' "
287                 "EQUALITY integerMatch "
288                 "ORDERING integerOrderingMatch "
289                 "SYNTAX OMsInteger "
290                 "SINGLE-VALUE )", &ad_reqId },
291         { "( " LOG_SCHEMA_AT ".13 NAME 'reqVersion' "
292                 "DESC 'Protocol version of Bind request' "
293                 "EQUALITY integerMatch "
294                 "ORDERING integerOrderingMatch "
295                 "SYNTAX OMsInteger "
296                 "SINGLE-VALUE )", &ad_reqVersion },
297         { "( " LOG_SCHEMA_AT ".14 NAME 'reqMethod' "
298                 "DESC 'Bind method of request' "
299                 "EQUALITY caseIgnoreMatch "
300                 "SYNTAX OMsDirectoryString "
301                 "SINGLE-VALUE )", &ad_reqMethod },
302         { "( " LOG_SCHEMA_AT ".15 NAME 'reqAssertion' "
303                 "DESC 'Compare Assertion of request' "
304                 "SYNTAX OMsDirectoryString "
305                 "SINGLE-VALUE )", &ad_reqAssertion },
306         { "( " LOG_SCHEMA_AT ".16 NAME 'reqMod' "
307                 "DESC 'Modifications of request' "
308                 "EQUALITY octetStringMatch "
309                 "SUBSTR octetStringSubstringsMatch "
310                 "SYNTAX OMsOctetString )", &ad_reqMod },
311         { "( " LOG_SCHEMA_AT ".17 NAME 'reqOld' "
312                 "DESC 'Old values of entry before request completed' "
313                 "EQUALITY octetStringMatch "
314                 "SUBSTR octetStringSubstringsMatch "
315                 "SYNTAX OMsOctetString )", &ad_reqOld },
316         { "( " LOG_SCHEMA_AT ".18 NAME 'reqNewRDN' "
317                 "DESC 'New RDN of request' "
318                 "EQUALITY distinguishedNameMatch "
319                 "SYNTAX OMsDN "
320                 "SINGLE-VALUE )", &ad_reqNewRDN },
321         { "( " LOG_SCHEMA_AT ".19 NAME 'reqDeleteOldRDN' "
322                 "DESC 'Delete old RDN' "
323                 "EQUALITY booleanMatch "
324                 "SYNTAX OMsBoolean "
325                 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
326         { "( " LOG_SCHEMA_AT ".20 NAME 'reqNewSuperior' "
327                 "DESC 'New superior DN of request' "
328                 "EQUALITY distinguishedNameMatch "
329                 "SYNTAX OMsDN "
330                 "SINGLE-VALUE )", &ad_reqNewSuperior },
331         { "( " LOG_SCHEMA_AT ".21 NAME 'reqScope' "
332                 "DESC 'Scope of request' "
333                 "EQUALITY caseIgnoreMatch "
334                 "SYNTAX OMsDirectoryString "
335                 "SINGLE-VALUE )", &ad_reqScope },
336         { "( " LOG_SCHEMA_AT ".22 NAME 'reqDerefAliases' "
337                 "DESC 'Disposition of Aliases in request' "
338                 "EQUALITY caseIgnoreMatch "
339                 "SYNTAX OMsDirectoryString "
340                 "SINGLE-VALUE )", &ad_reqDerefAliases },
341         { "( " LOG_SCHEMA_AT ".23 NAME 'reqAttrsOnly' "
342                 "DESC 'Attributes and values of request' "
343                 "EQUALITY booleanMatch "
344                 "SYNTAX OMsBoolean "
345                 "SINGLE-VALUE )", &ad_reqAttrsOnly },
346         { "( " LOG_SCHEMA_AT ".24 NAME 'reqFilter' "
347                 "DESC 'Filter of request' "
348                 "EQUALITY caseIgnoreMatch "
349                 "SUBSTR caseIgnoreSubstringsMatch "
350                 "SYNTAX OMsDirectoryString "
351                 "SINGLE-VALUE )", &ad_reqFilter },
352         { "( " LOG_SCHEMA_AT ".25 NAME 'reqAttr' "
353                 "DESC 'Attributes of request' "
354                 "EQUALITY caseIgnoreMatch "
355                 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
356         { "( " LOG_SCHEMA_AT ".26 NAME 'reqSizeLimit' "
357                 "DESC 'Size limit of request' "
358                 "EQUALITY integerMatch "
359                 "ORDERING integerOrderingMatch "
360                 "SYNTAX OMsInteger "
361                 "SINGLE-VALUE )", &ad_reqSizeLimit },
362         { "( " LOG_SCHEMA_AT ".27 NAME 'reqTimeLimit' "
363                 "DESC 'Time limit of request' "
364                 "EQUALITY integerMatch "
365                 "ORDERING integerOrderingMatch "
366                 "SYNTAX OMsInteger "
367                 "SINGLE-VALUE )", &ad_reqTimeLimit },
368         { "( " LOG_SCHEMA_AT ".28 NAME 'reqEntries' "
369                 "DESC 'Number of entries returned' "
370                 "EQUALITY integerMatch "
371                 "ORDERING integerOrderingMatch "
372                 "SYNTAX OMsInteger "
373                 "SINGLE-VALUE )", &ad_reqEntries },
374         { "( " LOG_SCHEMA_AT ".29 NAME 'reqData' "
375                 "DESC 'Data of extended request' "
376                 "EQUALITY octetStringMatch "
377                 "SUBSTR octetStringSubstringsMatch "
378                 "SYNTAX OMsOctetString "
379                 "SINGLE-VALUE )", &ad_reqData },
380
381         /*
382          * from <draft-chu-ldap-logschema-01.txt>:
383          *
384
385    ( LOG_SCHEMA_AT .30 NAME 'auditContext'
386    DESC 'DN of auditContainer'
387    EQUALITY distinguishedNameMatch
388    SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
389    SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )
390
391          * - removed EQUALITY matchingRule
392          * - changed directoryOperation in dSAOperation
393          */
394         { "( " LOG_SCHEMA_AT ".30 NAME 'auditContext' "
395                 "DESC 'DN of auditContainer' "
396                 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
397                 "SINGLE-VALUE "
398                 "NO-USER-MODIFICATION "
399                 "USAGE dSAOperation )", &ad_auditContext },
400
401         /*
402          * ITS#6656
403          */
404         { "( " LOG_SCHEMA_AT ".31 NAME 'reqEntryUUID' "
405                 "DESC 'UUID of entry' "
406                 "EQUALITY UUIDMatch "
407                 "SYNTAX 1.3.6.1.1.16.1 "
408                 "SINGLE-VALUE )", &ad_reqEntryUUID },
409         { NULL, NULL }
410 };
411
412 static struct {
413         char *ot;
414         ObjectClass **oc;
415 } locs[] = {
416         { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
417                 "DESC 'AuditLog container' "
418                 "SUP top STRUCTURAL "
419                 "MAY ( cn $ reqStart $ reqEnd ) )", &log_container },
420         { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
421                 "DESC 'OpenLDAP request auditing' "
422                 "SUP top STRUCTURAL "
423                 "MUST ( reqStart $ reqType $ reqSession ) "
424                 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
425                         "reqResult $ reqMessage $ reqReferral $ reqEntryUUID ) )",
426                                 &log_ocs[LOG_EN_UNBIND] },
427         { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
428                 "DESC 'OpenLDAP read request record' "
429                 "SUP auditObject STRUCTURAL )", &log_oc_read },
430         { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
431                 "DESC 'OpenLDAP write request record' "
432                 "SUP auditObject STRUCTURAL )", &log_oc_write },
433         { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
434                 "DESC 'Abandon operation' "
435                 "SUP auditObject STRUCTURAL "
436                 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
437         { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
438                 "DESC 'Add operation' "
439                 "SUP auditWriteObject STRUCTURAL "
440                 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
441         { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
442                 "DESC 'Bind operation' "
443                 "SUP auditObject STRUCTURAL "
444                 "MUST ( reqVersion $ reqMethod ) )", &log_ocs[LOG_EN_BIND] },
445         { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
446                 "DESC 'Compare operation' "
447                 "SUP auditReadObject STRUCTURAL "
448                 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
449         { "( " LOG_SCHEMA_OC ".8 NAME 'auditDelete' "
450                 "DESC 'Delete operation' "
451                 "SUP auditWriteObject STRUCTURAL "
452                 "MAY reqOld )", &log_ocs[LOG_EN_DELETE] },
453         { "( " LOG_SCHEMA_OC ".9 NAME 'auditModify' "
454                 "DESC 'Modify operation' "
455                 "SUP auditWriteObject STRUCTURAL "
456                 "MAY reqOld MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
457         { "( " LOG_SCHEMA_OC ".10 NAME 'auditModRDN' "
458                 "DESC 'ModRDN operation' "
459                 "SUP auditWriteObject STRUCTURAL "
460                 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
461                 "MAY ( reqNewSuperior $ reqMod $ reqOld ) )", &log_ocs[LOG_EN_MODRDN] },
462         { "( " LOG_SCHEMA_OC ".11 NAME 'auditSearch' "
463                 "DESC 'Search operation' "
464                 "SUP auditReadObject STRUCTURAL "
465                 "MUST ( reqScope $ reqDerefAliases $ reqAttrsonly ) "
466                 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
467                         "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
468         { "( " LOG_SCHEMA_OC ".12 NAME 'auditExtended' "
469                 "DESC 'Extended operation' "
470                 "SUP auditObject STRUCTURAL "
471                 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
472         { NULL, NULL }
473 };
474
475 #define RDNEQ   "reqStart="
476
477 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
478  * If a field is present, it must be two digits. (Except for
479  * days, which can be arbitrary width.)
480  */
481 static int
482 log_age_parse(char *agestr)
483 {
484         int t1, t2;
485         int gotdays = 0;
486         char *endptr;
487
488         t1 = strtol( agestr, &endptr, 10 );
489         /* Is there a days delimiter? */
490         if ( *endptr == '+' ) {
491                 /* 32 bit time only covers about 68 years */
492                 if ( t1 < 0 || t1 > 25000 )
493                         return -1;
494                 t1 *= 24;
495                 gotdays = 1;
496                 agestr = endptr + 1;
497         } else {
498                 if ( agestr[2] != ':' ) {
499                         /* No valid delimiter found, fail */
500                         return -1;
501                 }
502                 t1 *= 60;
503                 agestr += 3;
504         }
505
506         t2 = atoi( agestr );
507         t1 += t2;
508
509         if ( agestr[2] ) {
510                 /* if there's a delimiter, it can only be a colon */
511                 if ( agestr[2] != ':' )
512                         return -1;
513         } else {
514                 /* If we're at the end of the string, and we started with days,
515                  * fail because we expected to find minutes too.
516                  */
517                 return gotdays ? -1 : t1 * 60;
518         }
519
520         agestr += 3;
521         t2 = atoi( agestr );
522
523         /* last field can only be seconds */
524         if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
525                 return -1;
526         t1 *= 60;
527         t1 += t2;
528
529         if ( agestr[2] ) {
530                 agestr += 3;
531                 if ( agestr[2] )
532                         return -1;
533                 t1 *= 60;
534                 t1 += atoi( agestr );
535         } else if ( gotdays ) {
536                 /* only got days+hh:mm */
537                 t1 *= 60;
538         }
539         return t1;
540 }
541
542 static void
543 log_age_unparse( int age, struct berval *agebv, size_t size )
544 {
545         int dd, hh, mm, ss, len;
546         char *ptr;
547
548         assert( size > 0 );
549
550         ss = age % 60;
551         age /= 60;
552         mm = age % 60;
553         age /= 60;
554         hh = age % 24;
555         age /= 24;
556         dd = age;
557
558         ptr = agebv->bv_val;
559
560         if ( dd ) {
561                 len = snprintf( ptr, size, "%d+", dd );
562                 assert( len >= 0 && (unsigned) len < size );
563                 size -= len;
564                 ptr += len;
565         }
566         len = snprintf( ptr, size, "%02d:%02d", hh, mm );
567         assert( len >= 0 && (unsigned) len < size );
568         size -= len;
569         ptr += len;
570         if ( ss ) {
571                 len = snprintf( ptr, size, ":%02d", ss );
572                 assert( len >= 0 && (unsigned) len < size );
573                 size -= len;
574                 ptr += len;
575         }
576
577         agebv->bv_len = ptr - agebv->bv_val;
578 }
579
580 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
581
582 #define PURGE_INCREMENT 100
583
584 typedef struct purge_data {
585         int slots;
586         int used;
587         BerVarray dn;
588         BerVarray ndn;
589         struct berval csn;      /* an arbitrary old CSN */
590 } purge_data;
591
592 static int
593 log_old_lookup( Operation *op, SlapReply *rs )
594 {
595         purge_data *pd = op->o_callback->sc_private;
596         Attribute *a;
597
598         if ( rs->sr_type != REP_SEARCH) return 0;
599
600         if ( slapd_shutdown ) return 0;
601
602         /* Remember max CSN: should always be the last entry
603          * seen, since log entries are ordered chronologically...
604          */
605         a = attr_find( rs->sr_entry->e_attrs,
606                 slap_schema.si_ad_entryCSN );
607         if ( a ) {
608                 ber_len_t len = a->a_nvals[0].bv_len;
609                 /* Paranoid len check, normalized CSNs are always the same length */
610                 if ( len > LDAP_PVT_CSNSTR_BUFSIZE )
611                         len = LDAP_PVT_CSNSTR_BUFSIZE;
612                 if ( memcmp( a->a_nvals[0].bv_val, pd->csn.bv_val, len ) > 0 ) {
613                         AC_MEMCPY( pd->csn.bv_val, a->a_nvals[0].bv_val, len );
614                         pd->csn.bv_len = len;
615                 }
616         }
617         if ( pd->used >= pd->slots ) {
618                 pd->slots += PURGE_INCREMENT;
619                 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
620                 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
621         }
622         ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
623         ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
624         pd->used++;
625         return 0;
626 }
627
628 /* Periodically search for old entries in the log database and delete them */
629 static void *
630 accesslog_purge( void *ctx, void *arg )
631 {
632         struct re_s *rtask = arg;
633         struct log_info *li = rtask->arg;
634
635         Connection conn = {0};
636         OperationBuffer opbuf;
637         Operation *op;
638         SlapReply rs = {REP_RESULT};
639         slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
640         Filter f;
641         AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
642         purge_data pd = {0};
643         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
644         char csnbuf[LDAP_PVT_CSNSTR_BUFSIZE];
645         time_t old = slap_get_time();
646
647         connection_fake_init( &conn, &opbuf, ctx );
648         op = &opbuf.ob_op;
649
650         f.f_choice = LDAP_FILTER_LE;
651         f.f_ava = &ava;
652         f.f_next = NULL;
653
654         ava.aa_desc = ad_reqStart;
655         ava.aa_value.bv_val = timebuf;
656         ava.aa_value.bv_len = sizeof(timebuf);
657
658         old -= li->li_age;
659         slap_timestamp( &old, &ava.aa_value );
660
661         op->o_tag = LDAP_REQ_SEARCH;
662         op->o_bd = li->li_db;
663         op->o_dn = li->li_db->be_rootdn;
664         op->o_ndn = li->li_db->be_rootndn;
665         op->o_req_dn = li->li_db->be_suffix[0];
666         op->o_req_ndn = li->li_db->be_nsuffix[0];
667         op->o_callback = &cb;
668         op->ors_scope = LDAP_SCOPE_ONELEVEL;
669         op->ors_deref = LDAP_DEREF_NEVER;
670         op->ors_tlimit = SLAP_NO_LIMIT;
671         op->ors_slimit = SLAP_NO_LIMIT;
672         op->ors_filter = &f;
673         filter2bv_x( op, &f, &op->ors_filterstr );
674         op->ors_attrs = slap_anlist_no_attrs;
675         op->ors_attrsonly = 1;
676         
677         pd.csn.bv_len = sizeof( csnbuf );
678         pd.csn.bv_val = csnbuf;
679         csnbuf[0] = '\0';
680         cb.sc_private = &pd;
681
682         op->o_bd->be_search( op, &rs );
683         op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
684
685         if ( pd.used ) {
686                 int i;
687
688                 /* delete the expired entries */
689                 op->o_tag = LDAP_REQ_DELETE;
690                 op->o_callback = &nullsc;
691                 op->o_csn = pd.csn;
692                 op->o_dont_replicate = 1;
693
694                 for (i=0; i<pd.used; i++) {
695                         op->o_req_dn = pd.dn[i];
696                         op->o_req_ndn = pd.ndn[i];
697                         if ( !slapd_shutdown ) {
698                                 rs_reinit( &rs, REP_RESULT );
699                                 op->o_bd->be_delete( op, &rs );
700                         }
701                         ch_free( pd.ndn[i].bv_val );
702                         ch_free( pd.dn[i].bv_val );
703                 }
704                 ch_free( pd.ndn );
705                 ch_free( pd.dn );
706
707                 {
708                         Modifications mod;
709                         struct berval bv[2];
710                         rs_reinit( &rs, REP_RESULT );
711                         /* update context's entryCSN to reflect oldest CSN */
712                         mod.sml_numvals = 1;
713                         mod.sml_values = bv;
714                         bv[0] = pd.csn;
715                         BER_BVZERO(&bv[1]);
716                         mod.sml_nvalues = NULL;
717                         mod.sml_desc = slap_schema.si_ad_entryCSN;
718                         mod.sml_op = LDAP_MOD_REPLACE;
719                         mod.sml_flags = SLAP_MOD_INTERNAL;
720                         mod.sml_next = NULL;
721
722                         op->o_tag = LDAP_REQ_MODIFY;
723                         op->orm_modlist = &mod;
724                         op->orm_no_opattrs = 1;
725                         op->o_req_dn = li->li_db->be_suffix[0];
726                         op->o_req_ndn = li->li_db->be_nsuffix[0];
727                         op->o_no_schema_check = 1;
728                         op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
729                         op->o_bd->be_modify( op, &rs );
730                         if ( mod.sml_next ) {
731                                 slap_mods_free( mod.sml_next, 1 );
732                         }
733                 }
734         }
735
736         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
737         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
738         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
739
740         return NULL;
741 }
742
743 static int
744 log_cf_gen(ConfigArgs *c)
745 {
746         slap_overinst *on = (slap_overinst *)c->bi;
747         struct log_info *li = on->on_bi.bi_private;
748         int rc = 0;
749         slap_mask_t tmask = 0;
750         char agebuf[2*STRLENOF("ddddd+hh:mm:ss  ")];
751         struct berval agebv, cyclebv;
752
753         switch( c->op ) {
754         case SLAP_CONFIG_EMIT:
755                 switch( c->type ) {
756                 case LOG_DB:
757                         if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
758                                 value_add_one( &c->rvalue_vals, &li->li_db_suffix );
759                                 value_add_one( &c->rvalue_nvals, &li->li_db_suffix );
760                         } else if ( li->li_db ) {
761                                 value_add_one( &c->rvalue_vals, li->li_db->be_suffix );
762                                 value_add_one( &c->rvalue_nvals, li->li_db->be_nsuffix );
763                         } else {
764                                 snprintf( c->cr_msg, sizeof( c->cr_msg ),
765                                         "accesslog: \"logdb <suffix>\" must be specified" );
766                                 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
767                                         c->log, c->cr_msg, c->value_dn.bv_val );
768                                 rc = 1;
769                                 break;
770                         }
771                         break;
772                 case LOG_OPS:
773                         rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
774                         break;
775                 case LOG_PURGE:
776                         if ( !li->li_age ) {
777                                 rc = 1;
778                                 break;
779                         }
780                         agebv.bv_val = agebuf;
781                         log_age_unparse( li->li_age, &agebv, sizeof( agebuf ) );
782                         agebv.bv_val[agebv.bv_len] = ' ';
783                         agebv.bv_len++;
784                         cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
785                         log_age_unparse( li->li_cycle, &cyclebv, sizeof( agebuf ) - agebv.bv_len );
786                         agebv.bv_len += cyclebv.bv_len;
787                         value_add_one( &c->rvalue_vals, &agebv );
788                         break;
789                 case LOG_SUCCESS:
790                         if ( li->li_success )
791                                 c->value_int = li->li_success;
792                         else
793                                 rc = 1;
794                         break;
795                 case LOG_OLD:
796                         if ( li->li_oldf ) {
797                                 filter2bv( li->li_oldf, &agebv );
798                                 ber_bvarray_add( &c->rvalue_vals, &agebv );
799                         }
800                         else
801                                 rc = 1;
802                         break;
803                 case LOG_OLDATTR:
804                         if ( li->li_oldattrs ) {
805                                 log_attr *la;
806
807                                 for ( la = li->li_oldattrs; la; la=la->next )
808                                         value_add_one( &c->rvalue_vals, &la->attr->ad_cname );
809                         }
810                         else
811                                 rc = 1;
812                         break;
813                 case LOG_BASE:
814                         if ( li->li_bases ) {
815                                 log_base *lb;
816
817                                 for ( lb = li->li_bases; lb; lb=lb->lb_next )
818                                         value_add_one( &c->rvalue_vals, &lb->lb_line );
819                         }
820                         else
821                                 rc = 1;
822                         break;
823                 }
824                 break;
825         case LDAP_MOD_DELETE:
826                 switch( c->type ) {
827                 case LOG_DB:
828                         /* noop. this should always be a valid backend. */
829                         break;
830                 case LOG_OPS:
831                         if ( c->valx < 0 ) {
832                                 li->li_ops = 0;
833                         } else {
834                                 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
835                                 if ( rc == 0 )
836                                         li->li_ops &= ~tmask;
837                         }
838                         break;
839                 case LOG_PURGE:
840                         if ( li->li_task ) {
841                                 struct re_s *re = li->li_task;
842                                 li->li_task = NULL;
843                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
844                                 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
845                                         ldap_pvt_runqueue_stoptask( &slapd_rq, re );
846                                 ldap_pvt_runqueue_remove( &slapd_rq, re );
847                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
848                         }
849                         li->li_age = 0;
850                         li->li_cycle = 0;
851                         break;
852                 case LOG_SUCCESS:
853                         li->li_success = 0;
854                         break;
855                 case LOG_OLD:
856                         if ( li->li_oldf ) {
857                                 filter_free( li->li_oldf );
858                                 li->li_oldf = NULL;
859                         }
860                         break;
861                 case LOG_OLDATTR:
862                         if ( c->valx < 0 ) {
863                                 log_attr *la, *ln;
864
865                                 for ( la = li->li_oldattrs; la; la = ln ) {
866                                         ln = la->next;
867                                         ch_free( la );
868                                 }
869                         } else {
870                                 log_attr *la = NULL, **lp;
871                                 int i;
872
873                                 for ( lp = &li->li_oldattrs, i=0; i < c->valx; i++ ) {
874                                         la = *lp;
875                                         lp = &la->next;
876                                 }
877                                 *lp = la->next;
878                                 ch_free( la );
879                         }
880                         break;
881                 case LOG_BASE:
882                         if ( c->valx < 0 ) {
883                                 log_base *lb, *ln;
884
885                                 for ( lb = li->li_bases; lb; lb = ln ) {
886                                         ln = lb->lb_next;
887                                         ch_free( lb );
888                                 }
889                         } else {
890                                 log_base *lb = NULL, **lp;
891                                 int i;
892
893                                 for ( lp = &li->li_bases, i=0; i < c->valx; i++ ) {
894                                         lb = *lp;
895                                         lp = &lb->lb_next;
896                                 }
897                                 *lp = lb->lb_next;
898                                 ch_free( lb );
899                         }
900                         break;
901                 }
902                 break;
903         default:
904                 switch( c->type ) {
905                 case LOG_DB:
906                         if ( CONFIG_ONLINE_ADD( c )) {
907                                 li->li_db = select_backend( &c->value_ndn, 0 );
908                                 if ( !li->li_db ) {
909                                         snprintf( c->cr_msg, sizeof( c->cr_msg ),
910                                                 "<%s> no matching backend found for suffix",
911                                                 c->argv[0] );
912                                         Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
913                                                 c->log, c->cr_msg, c->value_dn.bv_val );
914                                         rc = 1;
915                                 }
916                                 ch_free( c->value_ndn.bv_val );
917                         } else {
918                                 li->li_db_suffix = c->value_ndn;
919                         }
920                         ch_free( c->value_dn.bv_val );
921                         break;
922                 case LOG_OPS:
923                         rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
924                         if ( rc == 0 )
925                                 li->li_ops |= tmask;
926                         break;
927                 case LOG_PURGE:
928                         li->li_age = log_age_parse( c->argv[1] );
929                         if ( li->li_age < 1 ) {
930                                 rc = 1;
931                         } else {
932                                 li->li_cycle = log_age_parse( c->argv[2] );
933                                 if ( li->li_cycle < 1 ) {
934                                         rc = 1;
935                                 } else if ( slapMode & SLAP_SERVER_MODE ) {
936                                         struct re_s *re = li->li_task;
937                                         if ( re )
938                                                 re->interval.tv_sec = li->li_cycle;
939                                         else {
940                                                 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
941                                                 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
942                                                         li->li_cycle, accesslog_purge, li,
943                                                         "accesslog_purge", li->li_db ?
944                                                                 li->li_db->be_suffix[0].bv_val :
945                                                                 c->be->be_suffix[0].bv_val );
946                                                 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
947                                         }
948                                 }
949                         }
950                         break;
951                 case LOG_SUCCESS:
952                         li->li_success = c->value_int;
953                         break;
954                 case LOG_OLD:
955                         li->li_oldf = str2filter( c->argv[1] );
956                         if ( !li->li_oldf ) {
957                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "bad filter!" );
958                                 rc = 1;
959                         }
960                         break;
961                 case LOG_OLDATTR: {
962                         int i;
963                         AttributeDescription *ad;
964                         const char *text;
965
966                         for ( i=1; i< c->argc; i++ ) {
967                                 ad = NULL;
968                                 if ( slap_str2ad( c->argv[i], &ad, &text ) == LDAP_SUCCESS ) {
969                                         log_attr *la = ch_malloc( sizeof( log_attr ));
970                                         la->attr = ad;
971                                         la->next = li->li_oldattrs;
972                                         li->li_oldattrs = la;
973                                 } else {
974                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s <%s>: %s",
975                                                 c->argv[0], c->argv[i], text );
976                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
977                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
978                                         rc = ARG_BAD_CONF;
979                                         break;
980                                 }
981                         }
982                         }
983                         break;
984                 case LOG_BASE: {
985                         slap_mask_t m = 0;
986                         rc = verbstring_to_mask( logops, c->argv[1], '|', &m );
987                         if ( rc == 0 ) {
988                                 struct berval dn, ndn;
989                                 ber_str2bv( c->argv[2], 0, 0, &dn );
990                                 rc = dnNormalize( 0, NULL, NULL, &dn, &ndn, NULL );
991                                 if ( rc == 0 ) {
992                                         log_base *lb;
993                                         struct berval mbv;
994                                         char *ptr;
995                                         mask_to_verbstring( logops, m, '|', &mbv );
996                                         lb = ch_malloc( sizeof( log_base ) + mbv.bv_len + ndn.bv_len + 3 + 1 );
997                                         lb->lb_line.bv_val = (char *)(lb + 1);
998                                         lb->lb_line.bv_len = mbv.bv_len + ndn.bv_len + 3;
999                                         ptr = lutil_strcopy( lb->lb_line.bv_val, mbv.bv_val );
1000                                         *ptr++ = ' ';
1001                                         *ptr++ = '"';
1002                                         lb->lb_base.bv_val = ptr;
1003                                         lb->lb_base.bv_len = ndn.bv_len;
1004                                         ptr = lutil_strcopy( ptr, ndn.bv_val );
1005                                         *ptr++ = '"';
1006                                         lb->lb_ops = m;
1007                                         lb->lb_next = li->li_bases;
1008                                         li->li_bases = lb;
1009                                 } else {
1010                                         snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid DN: %s",
1011                                                 c->argv[0], c->argv[2] );
1012                                         Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
1013                                                 "%s: %s\n", c->log, c->cr_msg, 0 );
1014                                         rc = ARG_BAD_CONF;
1015                                 }
1016                         } else {
1017                                 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s invalid ops: %s",
1018                                         c->argv[0], c->argv[1] );
1019                                 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
1020                                         "%s: %s\n", c->log, c->cr_msg, 0 );
1021                                 rc = ARG_BAD_CONF;
1022                         }
1023                         }
1024                         break;
1025                 }
1026                 break;
1027         }
1028         return rc;
1029 }
1030
1031 static int
1032 logSchemaControlValidate(
1033         Syntax          *syntax,
1034         struct berval   *valp )
1035 {
1036         struct berval   val, bv;
1037         ber_len_t               i;
1038         int             rc = LDAP_SUCCESS;
1039
1040         assert( valp != NULL );
1041
1042         val = *valp;
1043
1044         /* check minimal size */
1045         if ( val.bv_len < STRLENOF( "{*}" ) ) {
1046                 return LDAP_INVALID_SYNTAX;
1047         }
1048
1049         val.bv_len--;
1050
1051         /* check SEQUENCE boundaries */
1052         if ( val.bv_val[ 0 ] != '{' /*}*/ ||
1053                 val.bv_val[ val.bv_len ] != /*{*/ '}' )
1054         {
1055                 return LDAP_INVALID_SYNTAX;
1056         }
1057
1058         /* extract and check OID */
1059         for ( i = 1; i < val.bv_len; i++ ) {
1060                 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1061                         break;
1062                 }
1063         }
1064
1065         bv.bv_val = &val.bv_val[ i ];
1066
1067         for ( i++; i < val.bv_len; i++ ) {
1068                 if ( ASCII_SPACE( val.bv_val[ i ] ) )
1069                 {
1070                         break;
1071                 }
1072         }
1073
1074         bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
1075
1076         rc = numericoidValidate( NULL, &bv );
1077         if ( rc != LDAP_SUCCESS ) {
1078                 return rc;
1079         }
1080
1081         if ( i == val.bv_len ) {
1082                 return LDAP_SUCCESS;
1083         }
1084
1085         if ( val.bv_val[ i ] != ' ' ) {
1086                 return LDAP_INVALID_SYNTAX;
1087         }
1088
1089         for ( i++; i < val.bv_len; i++ ) {
1090                 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1091                         break;
1092                 }
1093         }
1094
1095         if ( i == val.bv_len ) {
1096                 return LDAP_SUCCESS;
1097         }
1098
1099         /* extract and check criticality */
1100         if ( strncasecmp( &val.bv_val[ i ], "criticality ", STRLENOF( "criticality " ) ) == 0 )
1101         {
1102                 i += STRLENOF( "criticality " );
1103                 for ( ; i < val.bv_len; i++ ) {
1104                         if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1105                                 break;
1106                         }
1107                 }
1108
1109                 if ( i == val.bv_len ) {
1110                         return LDAP_INVALID_SYNTAX;
1111                 }
1112
1113                 bv.bv_val = &val.bv_val[ i ];
1114
1115                 for ( ; i < val.bv_len; i++ ) {
1116                         if ( ASCII_SPACE( val.bv_val[ i ] ) ) {
1117                                 break;
1118                         }
1119                 }
1120
1121                 bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
1122
1123                 if ( !bvmatch( &bv, &slap_true_bv ) && !bvmatch( &bv, &slap_false_bv ) ) 
1124                 {
1125                         return LDAP_INVALID_SYNTAX;
1126                 }
1127
1128                 if ( i == val.bv_len ) {
1129                         return LDAP_SUCCESS;
1130                 }
1131
1132                 if ( val.bv_val[ i ] != ' ' ) {
1133                         return LDAP_INVALID_SYNTAX;
1134                 }
1135
1136                 for ( i++; i < val.bv_len; i++ ) {
1137                         if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1138                                 break;
1139                         }
1140                 }
1141
1142                 if ( i == val.bv_len ) {
1143                         return LDAP_SUCCESS;
1144                 }
1145         }
1146
1147         /* extract and check controlValue */
1148         if ( strncasecmp( &val.bv_val[ i ], "controlValue ", STRLENOF( "controlValue " ) ) == 0 )
1149         {
1150                 ber_len_t valueStart, valueLen;
1151
1152                 i += STRLENOF( "controlValue " );
1153                 for ( ; i < val.bv_len; i++ ) {
1154                         if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1155                                 break;
1156                         }
1157                 }
1158
1159                 if ( i == val.bv_len ) {
1160                         return LDAP_INVALID_SYNTAX;
1161                 }
1162
1163                 if ( val.bv_val[ i ] != '"' ) {
1164                         return LDAP_INVALID_SYNTAX;
1165                 }
1166
1167                 i++;
1168                 valueStart = i;
1169
1170                 for ( ; i < val.bv_len; i++ ) {
1171                         if ( val.bv_val[ i ] == '"' ) {
1172                                 break;
1173                         }
1174
1175                         if ( !ASCII_HEX( val.bv_val[ i ] ) ) {
1176                                 return LDAP_INVALID_SYNTAX;
1177                         }
1178                 }
1179
1180                 if ( val.bv_val[ i ] != '"' ) {
1181                         return LDAP_INVALID_SYNTAX;
1182                 }
1183
1184                 valueLen = i - valueStart;
1185                 if ( (valueLen/2)*2 != valueLen ) {
1186                         return LDAP_INVALID_SYNTAX;
1187                 }
1188
1189                 for ( i++; i < val.bv_len; i++ ) {
1190                         if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1191                                 break;
1192                         }
1193                 }
1194
1195                 if ( i == val.bv_len ) {
1196                         return LDAP_SUCCESS;
1197                 }
1198         }
1199
1200         return LDAP_INVALID_SYNTAX;
1201 }
1202
1203 static int
1204 accesslog_ctrls(
1205         LDAPControl **ctrls,
1206         BerVarray *valsp,
1207         BerVarray *nvalsp,
1208         void *memctx )
1209 {
1210         long            i, rc = 0;
1211
1212         assert( valsp != NULL );
1213         assert( ctrls != NULL );
1214
1215         *valsp = NULL;
1216         *nvalsp = NULL;
1217
1218         for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1219                 struct berval   idx,
1220                                 oid,
1221                                 noid,
1222                                 bv;
1223                 char            *ptr,
1224                                 buf[ 32 ];
1225
1226                 if ( ctrls[ i ]->ldctl_oid == NULL ) {
1227                         return LDAP_PROTOCOL_ERROR;
1228                 }
1229
1230                 idx.bv_len = snprintf( buf, sizeof( buf ), "{%ld}", i );
1231                 idx.bv_val = buf;
1232
1233                 ber_str2bv( ctrls[ i ]->ldctl_oid, 0, 0, &oid );
1234                 noid.bv_len = idx.bv_len + oid.bv_len;
1235                 ptr = noid.bv_val = ber_memalloc_x( noid.bv_len + 1, memctx );
1236                 ptr = lutil_strcopy( ptr, idx.bv_val );
1237                 ptr = lutil_strcopy( ptr, oid.bv_val );
1238
1239                 bv.bv_len = idx.bv_len + STRLENOF( "{}" ) + oid.bv_len;
1240
1241                 if ( ctrls[ i ]->ldctl_iscritical ) {
1242                         bv.bv_len += STRLENOF( " criticality TRUE" );
1243                 }
1244
1245                 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1246                         bv.bv_len += STRLENOF( " controlValue \"\"" )
1247                                 + 2 * ctrls[ i ]->ldctl_value.bv_len;
1248                 }
1249
1250                 ptr = bv.bv_val = ber_memalloc_x( bv.bv_len + 1, memctx );
1251                 if ( ptr == NULL ) {
1252                         ber_bvarray_free( *valsp );
1253                         *valsp = NULL;
1254                         ber_bvarray_free( *nvalsp );
1255                         *nvalsp = NULL;
1256                         return LDAP_OTHER;
1257                 }
1258
1259                 ptr = lutil_strcopy( ptr, idx.bv_val );
1260
1261                 *ptr++ = '{' /*}*/ ;
1262                 ptr = lutil_strcopy( ptr, oid.bv_val );
1263
1264                 if ( ctrls[ i ]->ldctl_iscritical ) {
1265                         ptr = lutil_strcopy( ptr, " criticality TRUE" );
1266                 }
1267                 
1268                 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1269                         ber_len_t       j;
1270
1271                         ptr = lutil_strcopy( ptr, " controlValue \"" );
1272                         for ( j = 0; j < ctrls[ i ]->ldctl_value.bv_len; j++ ) {
1273                                 *ptr++ = SLAP_ESCAPE_HI(ctrls[ i ]->ldctl_value.bv_val[ j ]);
1274                                 *ptr++ = SLAP_ESCAPE_LO(ctrls[ i ]->ldctl_value.bv_val[ j ]);
1275                         }
1276
1277                         *ptr++ = '"';
1278                 }
1279
1280                 *ptr++ = '}';
1281                 *ptr = '\0';
1282
1283                 ber_bvarray_add_x( valsp, &bv, memctx );
1284                 ber_bvarray_add_x( nvalsp, &noid, memctx );
1285         }
1286
1287         return rc;
1288         
1289 }
1290
1291 static Entry *accesslog_entry( Operation *op, SlapReply *rs, int logop,
1292         Operation *op2 ) {
1293         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1294         log_info *li = on->on_bi.bi_private;
1295
1296         char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1297         char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1298
1299         struct berval rdn, nrdn, timestamp, ntimestamp, bv;
1300         slap_verbmasks *lo = logops+logop+EN_OFFSET;
1301
1302         Entry *e = entry_alloc();
1303
1304         strcpy( rdnbuf, RDNEQ );
1305         rdn.bv_val = rdnbuf;
1306         strcpy( nrdnbuf, RDNEQ );
1307         nrdn.bv_val = nrdnbuf;
1308
1309         timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
1310         timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1311         slap_timestamp( &op->o_time, &timestamp );
1312         snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op->o_tincr );
1313         timestamp.bv_len += STRLENOF(".123456");
1314
1315         rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
1316         ad_reqStart->ad_type->sat_equality->smr_normalize(
1317                 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
1318                 ad_reqStart->ad_type->sat_equality, &timestamp, &ntimestamp,
1319                 op->o_tmpmemctx );
1320
1321         strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
1322         nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
1323         build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
1324         build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
1325
1326         attr_merge_one( e, slap_schema.si_ad_objectClass,
1327                 &log_ocs[logop]->soc_cname, NULL );
1328         attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1329                 &log_ocs[logop]->soc_cname, NULL );
1330         attr_merge_one( e, ad_reqStart, &timestamp, &ntimestamp );
1331         op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
1332
1333         slap_op_time( &op2->o_time, &op2->o_tincr );
1334
1335         timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1336         slap_timestamp( &op2->o_time, &timestamp );
1337         snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op2->o_tincr );
1338         timestamp.bv_len += STRLENOF(".123456");
1339
1340         attr_merge_normalize_one( e, ad_reqEnd, &timestamp, op->o_tmpmemctx );
1341
1342         /* Exops have OID appended */
1343         if ( logop == LOG_EN_EXTENDED ) {
1344                 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
1345                 bv.bv_val = ch_malloc( bv.bv_len + 1 );
1346                 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
1347                 bv.bv_val[lo->word.bv_len] = '{';
1348                 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
1349                         op->ore_reqoid.bv_len );
1350                 bv.bv_val[bv.bv_len-1] = '}';
1351                 bv.bv_val[bv.bv_len] = '\0';
1352                 attr_merge_one( e, ad_reqType, &bv, NULL );
1353         } else {
1354                 attr_merge_one( e, ad_reqType, &lo->word, NULL );
1355         }
1356
1357         rdn.bv_len = snprintf( rdn.bv_val, sizeof( rdnbuf ), "%lu", op->o_connid );
1358         if ( rdn.bv_len < sizeof( rdnbuf ) ) {
1359                 attr_merge_one( e, ad_reqSession, &rdn, NULL );
1360         } /* else? */
1361
1362         if ( BER_BVISNULL( &op->o_dn ) ) {
1363                 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
1364                         (struct berval *)&slap_empty_bv );
1365         } else {
1366                 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
1367         }
1368
1369         /* FIXME: need to add reqControls and reqRespControls */
1370         if ( op->o_ctrls ) {
1371                 BerVarray       vals = NULL,
1372                                 nvals = NULL;
1373
1374                 if ( accesslog_ctrls( op->o_ctrls, &vals, &nvals,
1375                         op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1376                 {
1377                         attr_merge( e, ad_reqControls, vals, nvals );
1378                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
1379                         ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1380                 }
1381         }
1382
1383         if ( rs->sr_ctrls ) {
1384                 BerVarray       vals = NULL,
1385                                 nvals = NULL;
1386
1387                 if ( accesslog_ctrls( rs->sr_ctrls, &vals, &nvals,
1388                         op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1389                 {
1390                         attr_merge( e, ad_reqRespControls, vals, nvals );
1391                         ber_bvarray_free_x( vals, op->o_tmpmemctx );
1392                         ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1393                 }
1394
1395         }
1396
1397         return e;
1398 }
1399
1400 static struct berval scopes[] = {
1401         BER_BVC("base"),
1402         BER_BVC("one"),
1403         BER_BVC("sub"),
1404         BER_BVC("subord")
1405 };
1406
1407 static struct berval derefs[] = {
1408         BER_BVC("never"),
1409         BER_BVC("searching"),
1410         BER_BVC("finding"),
1411         BER_BVC("always")
1412 };
1413
1414 static struct berval simple = BER_BVC("SIMPLE");
1415
1416 static void accesslog_val2val(AttributeDescription *ad, struct berval *val,
1417         char c_op, struct berval *dst) {
1418         char *ptr;
1419
1420         dst->bv_len = ad->ad_cname.bv_len + val->bv_len + 2;
1421         if ( c_op ) dst->bv_len++;
1422
1423         dst->bv_val = ch_malloc( dst->bv_len+1 );
1424
1425         ptr = lutil_strcopy( dst->bv_val, ad->ad_cname.bv_val );
1426         *ptr++ = ':';
1427         if ( c_op )
1428                 *ptr++ = c_op;
1429         *ptr++ = ' ';
1430         AC_MEMCPY( ptr, val->bv_val, val->bv_len );
1431         dst->bv_val[dst->bv_len] = '\0';
1432 }
1433
1434 static int accesslog_response(Operation *op, SlapReply *rs) {
1435         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1436         log_info *li = on->on_bi.bi_private;
1437         Attribute *a, *last_attr;
1438         Modifications *m;
1439         struct berval *b;
1440         int i;
1441         int logop;
1442         slap_verbmasks *lo;
1443         Entry *e = NULL, *old = NULL, *e_uuid = NULL;
1444         char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
1445         struct berval bv;
1446         char *ptr;
1447         BerVarray vals;
1448         Operation op2 = {0};
1449         SlapReply rs2 = {REP_RESULT};
1450
1451         if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
1452                 return SLAP_CB_CONTINUE;
1453
1454         switch ( op->o_tag ) {
1455         case LDAP_REQ_ADD:              logop = LOG_EN_ADD; break;
1456         case LDAP_REQ_DELETE:   logop = LOG_EN_DELETE; break;
1457         case LDAP_REQ_MODIFY:   logop = LOG_EN_MODIFY; break;
1458         case LDAP_REQ_MODRDN:   logop = LOG_EN_MODRDN; break;
1459         case LDAP_REQ_COMPARE:  logop = LOG_EN_COMPARE; break;
1460         case LDAP_REQ_SEARCH:   logop = LOG_EN_SEARCH; break;
1461         case LDAP_REQ_BIND:             logop = LOG_EN_BIND; break;
1462         case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
1463         default:        /* unknown operation type */
1464                 logop = LOG_EN_UNKNOWN; break;
1465         }       /* Unbind and Abandon never reach here */
1466
1467         lo = logops+logop+EN_OFFSET;
1468         if ( !( li->li_ops & lo->mask )) {
1469                 log_base *lb;
1470
1471                 i = 0;
1472                 for ( lb = li->li_bases; lb; lb=lb->lb_next )
1473                         if (( lb->lb_ops & lo->mask ) && dnIsSuffix( &op->o_req_ndn, &lb->lb_base )) {
1474                                 i = 1;
1475                                 break;
1476                         }
1477                 if ( !i )
1478                         return SLAP_CB_CONTINUE;
1479         }
1480
1481         if ( lo->mask & LOG_OP_WRITES ) {
1482                 slap_callback *cb;
1483
1484                 /* These internal ops are not logged */
1485                 if ( op->o_dont_replicate && op->orm_no_opattrs )
1486                         return SLAP_CB_CONTINUE;
1487
1488                 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
1489                 old = li->li_old;
1490                 li->li_old = NULL;
1491                 /* Disarm mod_cleanup */
1492                 for ( cb = op->o_callback; cb; cb = cb->sc_next ) {
1493                         if ( cb->sc_private == (void *)on ) {
1494                                 cb->sc_private = NULL;
1495                                 break;
1496                         }
1497                 }
1498                 ldap_pvt_thread_rmutex_unlock( &li->li_op_rmutex, op->o_tid );
1499         }
1500
1501         if ( li->li_success && rs->sr_err != LDAP_SUCCESS )
1502                 goto done;
1503
1504         e = accesslog_entry( op, rs, logop, &op2 );
1505
1506         attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
1507
1508         if ( rs->sr_text ) {
1509                 ber_str2bv( rs->sr_text, 0, 0, &bv );
1510                 attr_merge_one( e, ad_reqMessage, &bv, NULL );
1511         }
1512         bv.bv_len = snprintf( timebuf, sizeof( timebuf ), "%d", rs->sr_err );
1513         if ( bv.bv_len < sizeof( timebuf ) ) {
1514                 bv.bv_val = timebuf;
1515                 attr_merge_one( e, ad_reqResult, &bv, NULL );
1516         }
1517
1518         last_attr = attr_find( e->e_attrs, ad_reqResult );
1519
1520         e_uuid = old;
1521         switch( logop ) {
1522         case LOG_EN_ADD:
1523         case LOG_EN_DELETE: {
1524                 char c_op;
1525                 Entry *e2;
1526
1527                 if ( logop == LOG_EN_ADD ) {
1528                         e2 = op->ora_e;
1529                         e_uuid = op->ora_e;
1530                         c_op = '+';
1531
1532                 } else {
1533                         if ( !old )
1534                                 break;
1535                         e2 = old;
1536                         c_op = 0;
1537                 }
1538                 /* count all the vals */
1539                 i = 0;
1540                 for ( a=e2->e_attrs; a; a=a->a_next ) {
1541                         i += a->a_numvals;
1542                 }
1543                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1544                 i = 0;
1545                 for ( a=e2->e_attrs; a; a=a->a_next ) {
1546                         if ( a->a_vals ) {
1547                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1548                                         accesslog_val2val( a->a_desc, b, c_op, &vals[i] );
1549                                 }
1550                         }
1551                 }
1552                 vals[i].bv_val = NULL;
1553                 vals[i].bv_len = 0;
1554                 a = attr_alloc( logop == LOG_EN_ADD ? ad_reqMod : ad_reqOld );
1555                 a->a_numvals = i;
1556                 a->a_vals = vals;
1557                 a->a_nvals = vals;
1558                 last_attr->a_next = a;
1559                 break;
1560         }
1561
1562         case LOG_EN_MODRDN:
1563         case LOG_EN_MODIFY:
1564                 /* count all the mods */
1565                 i = 0;
1566                 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1567                         if ( m->sml_values ) {
1568                                 i += m->sml_numvals;
1569                         } else if ( m->sml_op == LDAP_MOD_DELETE ||
1570                                 m->sml_op == LDAP_MOD_REPLACE )
1571                         {
1572                                 i++;
1573                         }
1574                 }
1575                 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1576                 i = 0;
1577
1578                 /* init flags on old entry */
1579                 if ( old ) {
1580                         for ( a = old->e_attrs; a; a = a->a_next ) {
1581                                 log_attr *la;
1582                                 a->a_flags = 0;
1583
1584                                 /* look for attrs that are always logged */
1585                                 for ( la = li->li_oldattrs; la; la = la->next ) {
1586                                         if ( a->a_desc == la->attr ) {
1587                                                 a->a_flags = 1;
1588                                         }
1589                                 }
1590                         }
1591                 }
1592
1593                 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1594                         /* Mark this attribute as modified */
1595                         if ( old ) {
1596                                 a = attr_find( old->e_attrs, m->sml_desc );
1597                                 if ( a ) {
1598                                         a->a_flags = 1;
1599                                 }
1600                         }
1601
1602                         /* don't log the RDN mods; they're explicitly logged later */
1603                         if ( logop == LOG_EN_MODRDN &&
1604                                 ( m->sml_op == SLAP_MOD_SOFTADD ||
1605                                   m->sml_op == LDAP_MOD_DELETE ) )
1606                         {
1607                                 continue;
1608                         }
1609
1610                         if ( m->sml_values ) {
1611                                 for ( b = m->sml_values; !BER_BVISNULL( b ); b++, i++ ) {
1612                                         char c_op;
1613
1614                                         switch ( m->sml_op ) {
1615                                         case LDAP_MOD_ADD: c_op = '+'; break;
1616                                         case LDAP_MOD_DELETE:   c_op = '-'; break;
1617                                         case LDAP_MOD_REPLACE:  c_op = '='; break;
1618                                         case LDAP_MOD_INCREMENT:        c_op = '#'; break;
1619
1620                                         /* unknown op. there shouldn't be any of these. we
1621                                          * don't know what to do with it, but we shouldn't just
1622                                          * ignore it.
1623                                          */
1624                                         default: c_op = '?'; break;
1625                                         }
1626                                         accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
1627                                 }
1628                         } else if ( m->sml_op == LDAP_MOD_DELETE ||
1629                                 m->sml_op == LDAP_MOD_REPLACE )
1630                         {
1631                                 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
1632                                 vals[i].bv_val = ch_malloc( vals[i].bv_len + 1 );
1633                                 ptr = lutil_strcopy( vals[i].bv_val,
1634                                         m->sml_desc->ad_cname.bv_val );
1635                                 *ptr++ = ':';
1636                                 if ( m->sml_op == LDAP_MOD_DELETE ) {
1637                                         *ptr++ = '-';
1638                                 } else {
1639                                         *ptr++ = '=';
1640                                 }
1641                                 *ptr = '\0';
1642                                 i++;
1643                         }
1644                 }
1645
1646                 if ( i > 0 ) {
1647                         BER_BVZERO( &vals[i] );
1648                         a = attr_alloc( ad_reqMod );
1649                         a->a_numvals = i;
1650                         a->a_vals = vals;
1651                         a->a_nvals = vals;
1652                         last_attr->a_next = a;
1653                         last_attr = a;
1654
1655                 } else {
1656                         ch_free( vals );
1657                 }
1658
1659                 if ( old ) {
1660                         /* count all the vals */
1661                         i = 0;
1662                         for ( a = old->e_attrs; a != NULL; a = a->a_next ) {
1663                                 if ( a->a_vals && a->a_flags ) {
1664                                         i += a->a_numvals;
1665                                 }
1666                         }
1667                         if ( i ) {
1668                                 vals = ch_malloc( (i + 1) * sizeof( struct berval ) );
1669                                 i = 0;
1670                                 for ( a=old->e_attrs; a; a=a->a_next ) {
1671                                         if ( a->a_vals && a->a_flags ) {
1672                                                 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1673                                                         accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1674                                                 }
1675                                         }
1676                                 }
1677                                 vals[i].bv_val = NULL;
1678                                 vals[i].bv_len = 0;
1679                                 a = attr_alloc( ad_reqOld );
1680                                 a->a_numvals = i;
1681                                 a->a_vals = vals;
1682                                 a->a_nvals = vals;
1683                                 last_attr->a_next = a;
1684                         }
1685                 }
1686                 if ( logop == LOG_EN_MODIFY ) {
1687                         break;
1688                 }
1689
1690                 /* Now log the actual modRDN info */
1691                 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
1692                 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
1693                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1694                         NULL );
1695                 if ( op->orr_newSup ) {
1696                         attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
1697                 }
1698                 break;
1699
1700         case LOG_EN_COMPARE:
1701                 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
1702                         op->orc_ava->aa_value.bv_len;
1703                 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
1704                 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
1705                 *ptr++ = '=';
1706                 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
1707                 bv.bv_val[bv.bv_len] = '\0';
1708                 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
1709                 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1710                 break;
1711
1712         case LOG_EN_SEARCH:
1713                 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
1714                 attr_merge_one( e, ad_reqDerefAliases, &derefs[op->ors_deref], NULL );
1715                 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
1716                         (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1717                         NULL );
1718                 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
1719                         attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
1720                 if ( op->ors_attrs ) {
1721                         int j;
1722                         /* count them */
1723                         for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1724                                 ;
1725                         vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
1726                                 op->o_tmpmemctx );
1727                         for (i=0, j=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++) {
1728                                 if (!BER_BVISEMPTY(&op->ors_attrs[i].an_name)) {
1729                                         vals[j] = op->ors_attrs[i].an_name;
1730                                         j++;
1731                                 }
1732                         }
1733                         BER_BVZERO(&vals[j]);
1734                         attr_merge( e, ad_reqAttr, vals, NULL );
1735                         op->o_tmpfree( vals, op->o_tmpmemctx );
1736                 }
1737                 bv.bv_val = timebuf;
1738                 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", rs->sr_nentries );
1739                 if ( bv.bv_len < sizeof( timebuf ) ) {
1740                         attr_merge_one( e, ad_reqEntries, &bv, NULL );
1741                 } /* else? */
1742
1743                 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_tlimit );
1744                 if ( bv.bv_len < sizeof( timebuf ) ) {
1745                         attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
1746                 } /* else? */
1747
1748                 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_slimit );
1749                 if ( bv.bv_len < sizeof( timebuf ) ) {
1750                         attr_merge_one( e, ad_reqSizeLimit, &bv, NULL );
1751                 } /* else? */
1752                 break;
1753
1754         case LOG_EN_BIND:
1755                 bv.bv_val = timebuf;
1756                 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->o_protocol );
1757                 if ( bv.bv_len < sizeof( timebuf ) ) {
1758                         attr_merge_one( e, ad_reqVersion, &bv, NULL );
1759                 } /* else? */
1760                 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
1761                         attr_merge_one( e, ad_reqMethod, &simple, NULL );
1762                 } else {
1763                         bv.bv_len = STRLENOF("SASL()") + op->orb_mech.bv_len;
1764                         bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
1765                         ptr = lutil_strcopy( bv.bv_val, "SASL(" );
1766                         ptr = lutil_strcopy( ptr, op->orb_mech.bv_val );
1767                         *ptr++ = ')';
1768                         *ptr = '\0';
1769                         attr_merge_one( e, ad_reqMethod, &bv, NULL );
1770                         op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1771                 }
1772
1773                 break;
1774
1775         case LOG_EN_EXTENDED:
1776                 if ( op->ore_reqdata ) {
1777                         attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
1778                 }
1779                 break;
1780
1781         case LOG_EN_UNKNOWN:
1782                 /* we don't know its parameters, don't add any */
1783                 break;
1784         }
1785
1786         if ( e_uuid || !BER_BVISNULL( &li->li_uuid ) ) {
1787                 struct berval *pbv;
1788
1789                 if ( !BER_BVISNULL( &li->li_uuid ) ) {
1790                         pbv = &li->li_uuid;
1791
1792                 } else {
1793                         a = attr_find( e_uuid->e_attrs, slap_schema.si_ad_entryUUID );
1794                         if ( a ) {
1795                                 pbv = &a->a_vals[0];
1796                         }
1797                 } 
1798
1799                 if ( pbv ) {
1800                         attr_merge_one( e, ad_reqEntryUUID, pbv, NULL );
1801                 }
1802
1803                 if ( !BER_BVISNULL( &li->li_uuid ) ) {
1804                         ber_memfree( li->li_uuid.bv_val );
1805                         BER_BVZERO( &li->li_uuid );
1806                 }
1807         }
1808
1809         op2.o_hdr = op->o_hdr;
1810         op2.o_tag = LDAP_REQ_ADD;
1811         op2.o_bd = li->li_db;
1812         op2.o_dn = li->li_db->be_rootdn;
1813         op2.o_ndn = li->li_db->be_rootndn;
1814         op2.o_req_dn = e->e_name;
1815         op2.o_req_ndn = e->e_nname;
1816         op2.ora_e = e;
1817         op2.o_callback = &nullsc;
1818
1819         if (( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn )) {
1820                 slap_queue_csn( &op2, &op->o_csn );
1821         }
1822
1823         op2.o_bd->be_add( &op2, &rs2 );
1824         if ( e == op2.ora_e ) entry_free( e );
1825         e = NULL;
1826
1827 done:
1828         if ( lo->mask & LOG_OP_WRITES )
1829                 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
1830         if ( old ) entry_free( old );
1831         return SLAP_CB_CONTINUE;
1832 }
1833
1834 /* Since Bind success is sent by the frontend, it won't normally enter
1835  * the overlay response callback. Add another callback to make sure it
1836  * gets here.
1837  */
1838 static int
1839 accesslog_bind_resp( Operation *op, SlapReply *rs )
1840 {
1841         BackendDB *be, db;
1842         int rc;
1843         slap_callback *sc;
1844
1845         be = op->o_bd;
1846         db = *be;
1847         op->o_bd = &db;
1848         db.bd_info = op->o_callback->sc_private;
1849         rc = accesslog_response( op, rs );
1850         op->o_bd = be;
1851         sc = op->o_callback;
1852         op->o_callback = sc->sc_next;
1853         op->o_tmpfree( sc, op->o_tmpmemctx );
1854         return rc;
1855 }
1856
1857 static int
1858 accesslog_op_bind( Operation *op, SlapReply *rs )
1859 {
1860         slap_callback *sc;
1861
1862         sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1863         sc->sc_response = accesslog_bind_resp;
1864         sc->sc_private = op->o_bd->bd_info;
1865
1866         if ( op->o_callback ) {
1867                 sc->sc_next = op->o_callback->sc_next;
1868                 op->o_callback->sc_next = sc;
1869         } else {
1870                 op->o_callback = sc;
1871         }
1872         return SLAP_CB_CONTINUE;
1873 }
1874
1875 static int
1876 accesslog_mod_cleanup( Operation *op, SlapReply *rs )
1877 {
1878         slap_callback *sc = op->o_callback;
1879         slap_overinst *on = sc->sc_private;
1880         op->o_callback = sc->sc_next;
1881
1882         op->o_tmpfree( sc, op->o_tmpmemctx );
1883
1884         if ( on ) {
1885                 BackendInfo *bi = op->o_bd->bd_info;
1886                 op->o_bd->bd_info = (BackendInfo *)on;
1887                 accesslog_response( op, rs );
1888                 op->o_bd->bd_info = bi;
1889         }
1890         return 0;
1891 }
1892
1893 static int
1894 accesslog_op_mod( Operation *op, SlapReply *rs )
1895 {
1896         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1897         log_info *li = on->on_bi.bi_private;
1898         int doit = 0;
1899
1900         /* These internal ops are not logged */
1901         if ( op->o_dont_replicate && op->orm_no_opattrs )
1902                 return SLAP_CB_CONTINUE;
1903
1904
1905         if ( li->li_ops & LOG_OP_WRITES ) {
1906                 doit = 1;
1907         } else {
1908                 log_base *lb;
1909                 for ( lb = li->li_bases; lb; lb = lb->lb_next )
1910                         if (( lb->lb_ops & LOG_OP_WRITES ) && dnIsSuffix( &op->o_req_ndn, &lb->lb_base )) {
1911                                 doit = 1;
1912                                 break;
1913                         }
1914         }
1915                         
1916         if ( doit ) {
1917                 slap_callback *cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx ), *cb2;
1918                 cb->sc_cleanup = accesslog_mod_cleanup;
1919                 cb->sc_response = NULL;
1920                 cb->sc_private = on;
1921                 cb->sc_next = NULL;
1922                 for ( cb2 = op->o_callback; cb2->sc_next; cb2 = cb2->sc_next );
1923                 cb2->sc_next = cb;
1924
1925                 ldap_pvt_thread_rmutex_lock( &li->li_op_rmutex, op->o_tid );
1926                 if ( li->li_oldf && ( op->o_tag == LDAP_REQ_DELETE ||
1927                         op->o_tag == LDAP_REQ_MODIFY ||
1928                         ( op->o_tag == LDAP_REQ_MODRDN && li->li_oldattrs )))
1929                 {
1930                         int rc;
1931                         Entry *e;
1932
1933                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1934                         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1935                         if ( e ) {
1936                                 if ( test_filter( op, e, li->li_oldf ) == LDAP_COMPARE_TRUE )
1937                                         li->li_old = entry_dup( e );
1938                                 be_entry_release_rw( op, e, 0 );
1939                         }
1940                         op->o_bd->bd_info = (BackendInfo *)on;
1941
1942                 } else {
1943                         int rc;
1944                         Entry *e;
1945
1946                         op->o_bd->bd_info = (BackendInfo *)on->on_info;
1947                         rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1948                         if ( e ) {
1949                                 Attribute *a = attr_find( e->e_attrs, slap_schema.si_ad_entryUUID );
1950                                 if ( a ) {
1951                                         ber_dupbv( &li->li_uuid, &a->a_vals[0] );
1952                                 }
1953                                 be_entry_release_rw( op, e, 0 );
1954                         }
1955                         op->o_bd->bd_info = (BackendInfo *)on;
1956                 }
1957         }
1958         return SLAP_CB_CONTINUE;
1959 }
1960
1961 /* unbinds are broadcast to all backends; we only log it if this
1962  * backend was used for the original bind.
1963  */
1964 static int
1965 accesslog_unbind( Operation *op, SlapReply *rs )
1966 {
1967         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1968         if ( op->o_conn->c_authz_backend == on->on_info->oi_origdb ) {
1969                 log_info *li = on->on_bi.bi_private;
1970                 Operation op2 = {0};
1971                 void *cids[SLAP_MAX_CIDS];
1972                 SlapReply rs2 = {REP_RESULT};
1973                 Entry *e;
1974
1975                 if ( !( li->li_ops & LOG_OP_UNBIND )) {
1976                         log_base *lb;
1977                         int i = 0;
1978
1979                         for ( lb = li->li_bases; lb; lb=lb->lb_next )
1980                                 if (( lb->lb_ops & LOG_OP_UNBIND ) && dnIsSuffix( &op->o_ndn, &lb->lb_base )) {
1981                                         i = 1;
1982                                         break;
1983                                 }
1984                         if ( !i )
1985                                 return SLAP_CB_CONTINUE;
1986                 }
1987
1988                 e = accesslog_entry( op, rs, LOG_EN_UNBIND, &op2 );
1989                 op2.o_hdr = op->o_hdr;
1990                 op2.o_tag = LDAP_REQ_ADD;
1991                 op2.o_bd = li->li_db;
1992                 op2.o_dn = li->li_db->be_rootdn;
1993                 op2.o_ndn = li->li_db->be_rootndn;
1994                 op2.o_req_dn = e->e_name;
1995                 op2.o_req_ndn = e->e_nname;
1996                 op2.ora_e = e;
1997                 op2.o_callback = &nullsc;
1998                 op2.o_controls = cids;
1999                 memset(cids, 0, sizeof( cids ));
2000
2001                 op2.o_bd->be_add( &op2, &rs2 );
2002                 if ( e == op2.ora_e )
2003                         entry_free( e );
2004         }
2005         return SLAP_CB_CONTINUE;
2006 }
2007
2008 static int
2009 accesslog_abandon( Operation *op, SlapReply *rs )
2010 {
2011         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2012         log_info *li = on->on_bi.bi_private;
2013         Operation op2 = {0};
2014         void *cids[SLAP_MAX_CIDS];
2015         SlapReply rs2 = {REP_RESULT};
2016         Entry *e;
2017         char buf[64];
2018         struct berval bv;
2019
2020         if ( !op->o_time )
2021                 return SLAP_CB_CONTINUE;
2022
2023         if ( !( li->li_ops & LOG_OP_ABANDON )) {
2024                 log_base *lb;
2025                 int i = 0;
2026
2027                 for ( lb = li->li_bases; lb; lb=lb->lb_next )
2028                         if (( lb->lb_ops & LOG_OP_ABANDON ) && dnIsSuffix( &op->o_ndn, &lb->lb_base )) {
2029                                 i = 1;
2030                                 break;
2031                         }
2032                 if ( !i )
2033                         return SLAP_CB_CONTINUE;
2034         }
2035
2036         e = accesslog_entry( op, rs, LOG_EN_ABANDON, &op2 );
2037         bv.bv_val = buf;
2038         bv.bv_len = snprintf( buf, sizeof( buf ), "%d", op->orn_msgid );
2039         if ( bv.bv_len < sizeof( buf ) ) {
2040                 attr_merge_one( e, ad_reqId, &bv, NULL );
2041         } /* else? */
2042
2043         op2.o_hdr = op->o_hdr;
2044         op2.o_tag = LDAP_REQ_ADD;
2045         op2.o_bd = li->li_db;
2046         op2.o_dn = li->li_db->be_rootdn;
2047         op2.o_ndn = li->li_db->be_rootndn;
2048         op2.o_req_dn = e->e_name;
2049         op2.o_req_ndn = e->e_nname;
2050         op2.ora_e = e;
2051         op2.o_callback = &nullsc;
2052         op2.o_controls = cids;
2053         memset(cids, 0, sizeof( cids ));
2054
2055         op2.o_bd->be_add( &op2, &rs2 );
2056         if ( e == op2.ora_e )
2057                 entry_free( e );
2058
2059         return SLAP_CB_CONTINUE;
2060 }
2061
2062 static int
2063 accesslog_operational( Operation *op, SlapReply *rs )
2064 {
2065         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
2066         log_info *li = on->on_bi.bi_private;
2067
2068         if ( op->o_sync != SLAP_CONTROL_NONE )
2069                 return SLAP_CB_CONTINUE;
2070
2071         if ( rs->sr_entry != NULL
2072                 && dn_match( &op->o_bd->be_nsuffix[0], &rs->sr_entry->e_nname ) )
2073         {
2074                 Attribute       **ap;
2075
2076                 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
2077                         /* just count */ ;
2078
2079                 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
2080                                 ad_inlist( ad_auditContext, rs->sr_attrs ) )
2081                 {
2082                         *ap = attr_alloc( ad_auditContext );
2083                         attr_valadd( *ap,
2084                                 &li->li_db->be_suffix[0],
2085                                 &li->li_db->be_nsuffix[0], 1 );
2086                 }
2087         }
2088
2089         return SLAP_CB_CONTINUE;
2090 }
2091
2092 static slap_overinst accesslog;
2093
2094 static int
2095 accesslog_db_init(
2096         BackendDB *be,
2097         ConfigReply *cr
2098 )
2099 {
2100         slap_overinst *on = (slap_overinst *)be->bd_info;
2101         log_info *li = ch_calloc(1, sizeof(log_info));
2102
2103         on->on_bi.bi_private = li;
2104         ldap_pvt_thread_rmutex_init( &li->li_op_rmutex );
2105         ldap_pvt_thread_mutex_init( &li->li_log_mutex );
2106         return 0;
2107 }
2108
2109 static int
2110 accesslog_db_destroy(
2111         BackendDB *be,
2112         ConfigReply *cr
2113 )
2114 {
2115         slap_overinst *on = (slap_overinst *)be->bd_info;
2116         log_info *li = on->on_bi.bi_private;
2117         log_attr *la;
2118
2119         if ( li->li_oldf )
2120                 filter_free( li->li_oldf );
2121         for ( la=li->li_oldattrs; la; la=li->li_oldattrs ) {
2122                 li->li_oldattrs = la->next;
2123                 ch_free( la );
2124         }
2125         ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
2126         ldap_pvt_thread_rmutex_destroy( &li->li_op_rmutex );
2127         free( li );
2128         return LDAP_SUCCESS;
2129 }
2130
2131 /* Create the logdb's root entry if it's missing */
2132 static void *
2133 accesslog_db_root(
2134         void *ctx,
2135         void *arg )
2136 {
2137         struct re_s *rtask = arg;
2138         slap_overinst *on = rtask->arg;
2139         log_info *li = on->on_bi.bi_private;
2140
2141         Connection conn = {0};
2142         OperationBuffer opbuf;
2143         Operation *op;
2144
2145         Entry *e;
2146         int rc;
2147
2148         connection_fake_init( &conn, &opbuf, ctx );
2149         op = &opbuf.ob_op;
2150         op->o_bd = li->li_db;
2151         op->o_dn = li->li_db->be_rootdn;
2152         op->o_ndn = li->li_db->be_rootndn;
2153         rc = be_entry_get_rw( op, li->li_db->be_nsuffix, NULL, NULL, 0, &e );
2154
2155         if ( e ) {
2156                 be_entry_release_rw( op, e, 0 );
2157
2158         } else {
2159                 SlapReply rs = {REP_RESULT};
2160                 struct berval rdn, nrdn, attr;
2161                 char *ptr;
2162                 AttributeDescription *ad = NULL;
2163                 const char *text = NULL;
2164                 Entry *e_ctx;
2165                 BackendDB db;
2166
2167                 e = entry_alloc();
2168                 ber_dupbv( &e->e_name, li->li_db->be_suffix );
2169                 ber_dupbv( &e->e_nname, li->li_db->be_nsuffix );
2170
2171                 attr_merge_one( e, slap_schema.si_ad_objectClass,
2172                         &log_container->soc_cname, NULL );
2173
2174                 dnRdn( &e->e_name, &rdn );
2175                 dnRdn( &e->e_nname, &nrdn );
2176                 ptr = ber_bvchr( &rdn, '=' );
2177
2178                 assert( ptr != NULL );
2179
2180                 attr.bv_val = rdn.bv_val;
2181                 attr.bv_len = ptr - rdn.bv_val;
2182
2183                 slap_bv2ad( &attr, &ad, &text );
2184
2185                 rdn.bv_val = ptr+1;
2186                 rdn.bv_len -= attr.bv_len + 1;
2187                 ptr = ber_bvchr( &nrdn, '=' );
2188                 nrdn.bv_len -= ptr - nrdn.bv_val + 1;
2189                 nrdn.bv_val = ptr+1;
2190                 attr_merge_one( e, ad, &rdn, &nrdn );
2191
2192                 /* Get contextCSN from main DB */
2193                 op->o_bd = on->on_info->oi_origdb;
2194                 rc = be_entry_get_rw( op, op->o_bd->be_nsuffix, NULL,
2195                         slap_schema.si_ad_contextCSN, 0, &e_ctx );
2196
2197                 if ( e_ctx ) {
2198                         Attribute *a;
2199
2200                         a = attr_find( e_ctx->e_attrs, slap_schema.si_ad_contextCSN );
2201                         if ( a ) {
2202                                 /* FIXME: contextCSN could have multiple values!
2203                                  * should select the one with the server's SID */
2204                                 attr_merge_one( e, slap_schema.si_ad_entryCSN,
2205                                         &a->a_vals[0], &a->a_nvals[0] );
2206                                 attr_merge( e, a->a_desc, a->a_vals, a->a_nvals );
2207                         }
2208                         be_entry_release_rw( op, e_ctx, 0 );
2209                 }
2210                 db = *li->li_db;
2211                 op->o_bd = &db;
2212
2213                 op->ora_e = e;
2214                 op->o_req_dn = e->e_name;
2215                 op->o_req_ndn = e->e_nname;
2216                 op->o_callback = &nullsc;
2217                 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
2218                 rc = op->o_bd->be_add( op, &rs );
2219                 if ( e == op->ora_e )
2220                         entry_free( e );
2221         }
2222         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2223         ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2224         ldap_pvt_runqueue_remove( &slapd_rq, rtask );
2225         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2226
2227         return NULL;
2228 }
2229
2230 static int
2231 accesslog_db_open(
2232         BackendDB *be,
2233         ConfigReply *cr
2234 )
2235 {
2236         slap_overinst *on = (slap_overinst *)be->bd_info;
2237         log_info *li = on->on_bi.bi_private;
2238
2239
2240         if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
2241                 li->li_db = select_backend( &li->li_db_suffix, 0 );
2242                 ch_free( li->li_db_suffix.bv_val );
2243                 BER_BVZERO( &li->li_db_suffix );
2244         }
2245         if ( li->li_db == NULL ) {
2246                 Debug( LDAP_DEBUG_ANY,
2247                         "accesslog: \"logdb <suffix>\" missing or invalid.\n",
2248                         0, 0, 0 );
2249                 return 1;
2250         }
2251
2252         if ( slapMode & SLAP_TOOL_MODE )
2253                 return 0;
2254
2255         if ( BER_BVISEMPTY( &li->li_db->be_rootndn )) {
2256                 ber_dupbv( &li->li_db->be_rootdn, li->li_db->be_suffix );
2257                 ber_dupbv( &li->li_db->be_rootndn, li->li_db->be_nsuffix );
2258         }
2259
2260         ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2261         ldap_pvt_runqueue_insert( &slapd_rq, 3600, accesslog_db_root, on,
2262                 "accesslog_db_root", li->li_db->be_suffix[0].bv_val );
2263         ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2264
2265         return 0;
2266 }
2267
2268 int accesslog_initialize()
2269 {
2270         int i, rc;
2271
2272         accesslog.on_bi.bi_type = "accesslog";
2273         accesslog.on_bi.bi_db_init = accesslog_db_init;
2274         accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
2275         accesslog.on_bi.bi_db_open = accesslog_db_open;
2276
2277         accesslog.on_bi.bi_op_add = accesslog_op_mod;
2278         accesslog.on_bi.bi_op_bind = accesslog_op_bind;
2279         accesslog.on_bi.bi_op_delete = accesslog_op_mod;
2280         accesslog.on_bi.bi_op_modify = accesslog_op_mod;
2281         accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
2282         accesslog.on_bi.bi_op_unbind = accesslog_unbind;
2283         accesslog.on_bi.bi_op_abandon = accesslog_abandon;
2284         accesslog.on_bi.bi_operational = accesslog_operational;
2285         accesslog.on_response = accesslog_response;
2286
2287         accesslog.on_bi.bi_cf_ocs = log_cfocs;
2288
2289         nullsc.sc_response = slap_null_cb;
2290
2291         rc = config_register_schema( log_cfats, log_cfocs );
2292         if ( rc ) return rc;
2293
2294         /* log schema integration */
2295         for ( i=0; lsyntaxes[i].oid; i++ ) {
2296                 int code;
2297
2298                 code = register_syntax( &lsyntaxes[ i ].syn );
2299                 if ( code != 0 ) {
2300                         Debug( LDAP_DEBUG_ANY,
2301                                 "accesslog_init: register_syntax failed\n",
2302                                 0, 0, 0 );
2303                         return code;
2304                 }
2305
2306                 if ( lsyntaxes[i].mrs != NULL ) {
2307                         code = mr_make_syntax_compat_with_mrs(
2308                                 lsyntaxes[i].oid, lsyntaxes[i].mrs );
2309                         if ( code < 0 ) {
2310                                 Debug( LDAP_DEBUG_ANY,
2311                                         "accesslog_init: "
2312                                         "mr_make_syntax_compat_with_mrs "
2313                                         "failed\n",
2314                                         0, 0, 0 );
2315                                 return code;
2316                         }
2317                 }
2318         }
2319
2320         for ( i=0; lattrs[i].at; i++ ) {
2321                 int code;
2322
2323                 code = register_at( lattrs[i].at, lattrs[i].ad, 0 );
2324                 if ( code ) {
2325                         Debug( LDAP_DEBUG_ANY,
2326                                 "accesslog_init: register_at failed\n",
2327                                 0, 0, 0 );
2328                         return -1;
2329                 }
2330 #ifndef LDAP_DEVEL
2331                 (*lattrs[i].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
2332 #endif
2333         }
2334
2335         for ( i=0; locs[i].ot; i++ ) {
2336                 int code;
2337
2338                 code = register_oc( locs[i].ot, locs[i].oc, 0 );
2339                 if ( code ) {
2340                         Debug( LDAP_DEBUG_ANY,
2341                                 "accesslog_init: register_oc failed\n",
2342                                 0, 0, 0 );
2343                         return -1;
2344                 }
2345 #ifndef LDAP_DEVEL
2346                 (*locs[i].oc)->soc_flags |= SLAP_OC_HIDE;
2347 #endif
2348         }
2349
2350         return overlay_register(&accesslog);
2351 }
2352
2353 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
2354 int
2355 init_module( int argc, char *argv[] )
2356 {
2357         return accesslog_initialize();
2358 }
2359 #endif
2360
2361 #endif /* SLAPD_OVER_ACCESSLOG */