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