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