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