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