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