1 /* accesslog.c - log operations for audit/history purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005 The OpenLDAP Foundation.
6 * Portions copyright 2004-2005 Symas Corporation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
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>.
18 * This work was initially developed by Howard Chu for inclusion in
24 #ifdef SLAPD_OVER_ACCESSLOG
28 #include <ac/string.h>
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
47 #define LOG_OP_WRITES (LOG_OP_ADD|LOG_OP_DELETE|LOG_OP_MODIFY|LOG_OP_MODRDN)
48 #define LOG_OP_READS (LOG_OP_COMPARE|LOG_OP_SEARCH)
49 #define LOG_OP_SESSION (LOG_OP_BIND|LOG_OP_UNBIND|LOG_OP_ABANDON)
50 #define LOG_OP_ALL (LOG_OP_READS|LOG_OP_WRITES|LOG_OP_SESSION|LOG_OP_EXTENDED)
52 typedef struct log_info {
60 static ConfigDriver log_cf_gen;
68 static ConfigTable log_cfats[] = {
69 { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_MAGIC|LOG_DB,
70 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
71 "DESC 'Suffix of database for log content' "
72 "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
73 { "logops", "op|writes|reads|session|all", 2, 0, 0,
75 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
76 "DESC 'Operation types to log' "
77 "EQUALITY caseIgnoreMatch "
78 "SYNTAX OMsDirectoryString )", NULL, NULL },
79 { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
80 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
81 "DESC 'Log cleanup parameters' "
82 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
86 static ConfigOCs log_cfocs[] = {
88 "NAME 'olcAccessLogConfig' "
89 "DESC 'Access log configuration' "
90 "SUP olcOverlayConfig "
91 "MUST olcAccessLogDB "
92 "MAY ( olcAccessLogOps $ olcAccessLogPurge ) )",
93 Cft_Overlay, log_cfats },
97 static slap_verbmasks logops[] = {
98 { BER_BVC("all"), LOG_OP_ALL },
99 { BER_BVC("writes"), LOG_OP_WRITES },
100 { BER_BVC("session"), LOG_OP_SESSION },
101 { BER_BVC("reads"), LOG_OP_READS },
102 { BER_BVC("add"), LOG_OP_ADD },
103 { BER_BVC("delete"), LOG_OP_DELETE },
104 { BER_BVC("modify"), LOG_OP_MODIFY },
105 { BER_BVC("modrdn"), LOG_OP_MODRDN },
106 { BER_BVC("compare"), LOG_OP_COMPARE },
107 { BER_BVC("search"), LOG_OP_SEARCH },
108 { BER_BVC("bind"), LOG_OP_BIND },
109 { BER_BVC("unbind"), LOG_OP_UNBIND },
110 { BER_BVC("abandon"), LOG_OP_ABANDON },
111 { BER_BVC("extended"), LOG_OP_EXTENDED },
115 /* Start with "add" in logops */
132 static ObjectClass *log_ocs[LOG_EN__COUNT];
134 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
136 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
137 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
139 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
140 *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
141 *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
142 *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
143 *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
144 *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
145 *ad_reqId, *ad_reqMessage, *ad_oldest;
149 AttributeDescription **ad;
151 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
152 "DESC 'Target DN of request' "
153 "EQUALITY distinguishedNameMatch "
155 "SINGLE-VALUE )", &ad_reqDN },
156 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
157 "DESC 'Start time of request' "
158 "EQUALITY generalizedTimeMatch "
159 "ORDERING generalizedTimeOrderingMatch "
160 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
161 "SINGLE-VALUE )", &ad_reqStart },
162 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
163 "DESC 'End time of request' "
164 "EQUALITY generalizedTimeMatch "
165 "ORDERING generalizedTimeOrderingMatch "
166 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
167 "SINGLE-VALUE )", &ad_reqEnd },
168 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
169 "DESC 'Type of request' "
170 "EQUALITY caseIgnoreMatch "
171 "SYNTAX OMsDirectoryString "
172 "SINGLE-VALUE )", &ad_reqType },
173 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
174 "DESC 'Session ID of request' "
175 "EQUALITY caseIgnoreMatch "
176 "SYNTAX OMsDirectoryString "
177 "SINGLE-VALUE )", &ad_reqSession },
178 { "( " LOG_SCHEMA_AT ".6 NAME 'reqResult' "
179 "DESC 'Result code of request' "
180 "EQUALITY integerMatch "
182 "SINGLE-VALUE )", &ad_reqResult },
183 { "( " LOG_SCHEMA_AT ".7 NAME 'reqAuthzID' "
184 "DESC 'AUthorization ID of requestor' "
185 "EQUALITY distinguishedNameMatch "
187 "SINGLE-VALUE )", &ad_reqAuthzID },
188 { "( " LOG_SCHEMA_AT ".8 NAME 'reqControls' "
189 "DESC 'Request controls' "
190 "SYNTAX OMsOctetString )", &ad_reqControls },
191 { "( " LOG_SCHEMA_AT ".9 NAME 'reqRespControls' "
192 "DESC 'Response controls of request' "
193 "SYNTAX OMsOctetString )", &ad_reqRespControls },
194 { "( " LOG_SCHEMA_AT ".10 NAME 'reqMethod' "
195 "DESC 'Bind method of request' "
196 "EQUALITY caseIgnoreMatch "
197 "SYNTAX OMsDirectoryString "
198 "SINGLE-VALUE )", &ad_reqMethod },
199 { "( " LOG_SCHEMA_AT ".11 NAME 'reqAssertion' "
200 "DESC 'Compare Assertion of request' "
201 "SYNTAX OMsDirectoryString "
202 "SINGLE-VALUE )", &ad_reqAssertion },
203 { "( " LOG_SCHEMA_AT ".12 NAME 'reqNewRDN' "
204 "DESC 'New RDN of request' "
205 "EQUALITY distinguishedNameMatch "
207 "SINGLE-VALUE )", &ad_reqNewRDN },
208 { "( " LOG_SCHEMA_AT ".13 NAME 'reqNewSuperior' "
209 "DESC 'New superior DN of request' "
210 "EQUALITY distinguishedNameMatch "
212 "SINGLE-VALUE )", &ad_reqNewSuperior },
213 { "( " LOG_SCHEMA_AT ".14 NAME 'reqDeleteOldRDN' "
214 "DESC 'Delete old RDN' "
216 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
217 { "( " LOG_SCHEMA_AT ".15 NAME 'reqMod' "
218 "DESC 'Modifications of request' "
219 "SYNTAX OMsDirectoryString "
220 "EQUALITY caseIgnoreMatch "
221 "SUBSTR caseIgnoreSubstringsMatch )", &ad_reqMod },
222 { "( " LOG_SCHEMA_AT ".16 NAME 'reqScope' "
223 "DESC 'Scope of request' "
224 "SYNTAX OMsDirectoryString "
225 "SINGLE-VALUE )", &ad_reqScope },
226 { "( " LOG_SCHEMA_AT ".17 NAME 'reqFilter' "
227 "DESC 'Filter of request' "
228 "SYNTAX OMsDirectoryString "
229 "SINGLE-VALUE )", &ad_reqFilter },
230 { "( " LOG_SCHEMA_AT ".18 NAME 'reqAttr' "
231 "DESC 'Attributes of request' "
232 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
233 { "( " LOG_SCHEMA_AT ".19 NAME 'reqEntries' "
234 "DESC 'Number of entries returned' "
236 "SINGLE-VALUE )", &ad_reqEntries },
237 { "( " LOG_SCHEMA_AT ".20 NAME 'reqSizeLimit' "
238 "DESC 'Size limit of request' "
240 "SINGLE-VALUE )", &ad_reqSizeLimit },
241 { "( " LOG_SCHEMA_AT ".21 NAME 'reqTimeLimit' "
242 "DESC 'Time limit of request' "
244 "SINGLE-VALUE )", &ad_reqTimeLimit },
245 { "( " LOG_SCHEMA_AT ".22 NAME 'reqAttrsOnly' "
246 "DESC 'Attributes and values of request' "
248 "SINGLE-VALUE )", &ad_reqAttrsOnly },
249 { "( " LOG_SCHEMA_AT ".23 NAME 'reqData' "
250 "DESC 'Data of extended request' "
251 "SYNTAX OMsOctetString "
252 "SINGLE-VALUE )", &ad_reqData },
253 { "( " LOG_SCHEMA_AT ".24 NAME 'reqId' "
254 "DESC 'ID of Request to Abandon' "
256 "SINGLE-VALUE )", &ad_reqId },
257 { "( " LOG_SCHEMA_AT ".25 NAME 'reqMessage' "
258 "DESC 'Error text of request' "
259 "SYNTAX OMsDirectoryString "
260 "SINGLE-VALUE )", &ad_reqMessage },
262 { "( " LOG_SCHEMA_AT ".26 NAME 'auditOldest' "
263 "DESC 'Oldest record in this branch' "
264 "EQUALITY generalizedTimeMatch "
265 "ORDERING generalizedTimeOrderingMatch "
266 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
267 "SINGLE-VALUE )", &ad_oldest },
277 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
278 "SUP top STRUCTURAL "
280 "MAY cn )", &oc_container },
282 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
283 "DESC 'OpenLDAP request auditing' "
284 "SUP top STRUCTURAL "
285 "MUST ( reqStart $ reqType $ reqSession ) "
286 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
287 "reqResult $ reqMessage ) )", &log_ocs[LOG_EN_UNBIND] },
288 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
289 "DESC 'OpenLDAP read request record' "
290 "SUP auditObject STRUCTURAL )", NULL },
291 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
292 "DESC 'OpenLDAP write request record' "
293 "SUP auditObject STRUCTURAL )", &log_ocs[LOG_EN_DELETE] },
294 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
295 "DESC 'Abandon operation' "
296 "SUP auditObject STRUCTURAL "
297 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
298 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
299 "DESC 'Add operation' "
300 "SUP auditWriteObject STRUCTURAL "
301 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
302 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
303 "DESC 'Bind operation' "
304 "SUP auditObject STRUCTURAL "
305 "MUST reqMethod )", &log_ocs[LOG_EN_BIND] },
306 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
307 "DESC 'Compare operation' "
308 "SUP auditReadObject STRUCTURAL "
309 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
310 { "( " LOG_SCHEMA_OC ".8 NAME 'auditModify' "
311 "DESC 'Modify operation' "
312 "SUP auditWriteObject STRUCTURAL "
313 "MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
314 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModRDN' "
315 "DESC 'ModRDN operation' "
316 "SUP auditWriteObject STRUCTURAL "
317 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
318 "MAY reqNewSuperior )", &log_ocs[LOG_EN_MODRDN] },
319 { "( " LOG_SCHEMA_OC ".10 NAME 'auditSearch' "
320 "DESC 'Search operation' "
321 "SUP auditReadObject STRUCTURAL "
322 "MUST ( reqScope $ reqAttrsonly ) "
323 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
324 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
325 { "( " LOG_SCHEMA_OC ".11 NAME 'auditExtended' "
326 "DESC 'Extended operation' "
327 "SUP auditObject STRUCTURAL "
328 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
332 #define RDNEQ "reqStart="
334 /* Our time intervals are of the form [dd+]hh:mm[:ss]
335 * If a field is present, it must be two digits. We assume no one
336 * will want to keep log records for longer than 99 days.
339 log_age_parse(char *agestr)
346 /* Is there a days delimiter? */
347 if ( agestr[2] == '+' ) {
350 } else if ( agestr[2] != ':' ) {
351 /* No valid delimiter found, fail */
358 /* if there's a delimiter, it can only be a colon */
359 if ( agestr[2] && agestr[2] != ':' )
362 /* If we're at the end of the string, and we started with days,
363 * fail because we expected to find minutes too.
365 if ( gotdays && !agestr[2] )
377 /* last field can only be seconds */
378 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
388 t1 += atoi( agestr );
394 log_age_unparse( int age, struct berval *agebv )
410 ptr += sprintf( ptr, "%02d+", dd );
411 ptr += sprintf( ptr, "%02d:%02d", hh, mm );
413 ptr += sprintf( ptr, ":%02d", ss );
415 agebv->bv_len = ptr - agebv->bv_val;
418 static slap_callback nullsc = { NULL, slap_null_cb, NULL, NULL };
420 #define PURGE_INCREMENT 100
422 typedef struct purge_data {
430 log_old_lookup( Operation *op, SlapReply *rs )
432 purge_data *pd = op->o_callback->sc_private;
434 if ( rs->sr_type != REP_SEARCH) return 0;
436 if ( pd->used >= pd->slots ) {
437 pd->slots += PURGE_INCREMENT;
438 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
439 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
441 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
442 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
447 /* Periodically search for old entries in the log database and delete them */
449 accesslog_purge( void *ctx, void *arg )
451 struct re_s *rtask = arg;
452 struct log_info *li = rtask->arg;
454 Connection conn = {0};
455 char opbuf[OPERATION_BUFFER_SIZE];
456 Operation *op = (Operation *)opbuf;
457 SlapReply rs = {REP_RESULT};
458 slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
460 AttributeAssertion ava = {0};
462 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
463 time_t old = slap_get_time();
465 connection_fake_init( &conn, op, ctx );
467 f.f_choice = LDAP_FILTER_LE;
471 ava.aa_desc = ad_reqStart;
472 ava.aa_value.bv_val = timebuf;
473 ava.aa_value.bv_len = sizeof(timebuf);
476 slap_timestamp( &old, &ava.aa_value );
478 op->o_tag = LDAP_REQ_SEARCH;
479 op->o_bd = li->li_db;
480 op->o_dn = li->li_db->be_rootdn;
481 op->o_ndn = li->li_db->be_rootndn;
482 op->o_req_dn = li->li_db->be_suffix[0];
483 op->o_req_ndn = li->li_db->be_nsuffix[0];
484 op->o_callback = &cb;
485 op->ors_scope = LDAP_SCOPE_ONELEVEL;
486 op->ors_deref = LDAP_DEREF_NEVER;
487 op->ors_tlimit = SLAP_NO_LIMIT;
488 op->ors_slimit = SLAP_NO_LIMIT;
490 filter2bv_x( op, &f, &op->ors_filterstr );
491 op->ors_attrs = slap_anlist_no_attrs;
492 op->ors_attrsonly = 1;
496 op->o_bd->be_search( op, &rs );
497 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
502 op->o_tag = LDAP_REQ_DELETE;
503 op->o_callback = &nullsc;
505 for (i=0; i<pd.used; i++) {
506 op->o_req_dn = pd.dn[i];
507 op->o_req_ndn = pd.ndn[i];
508 op->o_bd->be_delete( op, &rs );
509 ch_free( pd.ndn[i].bv_val );
510 ch_free( pd.dn[i].bv_val );
516 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
517 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
518 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
524 log_cf_gen(ConfigArgs *c)
526 slap_overinst *on = (slap_overinst *)c->bi;
527 struct log_info *li = on->on_bi.bi_private;
529 slap_mask_t tmask = 0;
530 char agebuf[2*STRLENOF("dd+hh:mm:ss ")];
531 struct berval agebv, cyclebv;
534 case SLAP_CONFIG_EMIT:
537 value_add( &c->rvalue_vals, li->li_db->be_suffix );
538 value_add( &c->rvalue_nvals, li->li_db->be_nsuffix );
541 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
544 agebv.bv_val = agebuf;
545 log_age_unparse( li->li_age, &agebv );
546 agebv.bv_val[agebv.bv_len] = ' ';
548 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
549 log_age_unparse( li->li_cycle, &cyclebv );
550 agebv.bv_len += cyclebv.bv_len;
551 value_add_one( &c->rvalue_vals, &agebv );
555 case LDAP_MOD_DELETE:
558 /* noop. this should always be a valid backend. */
564 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
566 li->li_ops &= ~tmask;
571 struct re_s *re = li->li_task;
573 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
574 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
575 ldap_pvt_runqueue_remove( &slapd_rq, re );
585 li->li_db = select_backend( &c->value_ndn, 0, 0 );
587 sprintf( c->msg, "<%s> no matching backend found for suffix",
589 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
590 c->log, c->msg, c->value_dn.bv_val );
593 ch_free( c->value_dn.bv_val );
594 ch_free( c->value_ndn.bv_val );
597 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
602 li->li_age = log_age_parse( c->argv[1] );
603 if ( li->li_age == -1 ) {
606 li->li_cycle = log_age_parse( c->argv[2] );
607 if ( li->li_cycle == -1 ) {
609 } else if ( slapMode & SLAP_SERVER_MODE ) {
610 struct re_s *re = li->li_task;
612 re->interval.tv_sec = li->li_cycle;
614 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
615 li->li_cycle, accesslog_purge, li,
616 "accesslog_purge", li->li_db ?
617 li->li_db->be_suffix[0].bv_val :
618 c->be->be_suffix[0].bv_val );
628 static Entry *accesslog_entry( Operation *op, int logop ) {
629 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
630 log_info *li = on->on_bi.bi_private;
632 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
633 struct berval rdn, timestamp, bv;
634 slap_verbmasks *lo = logops+logop+EN_OFFSET;
636 Entry *e = ch_calloc( 1, sizeof(Entry) );
638 strcpy( rdnbuf, RDNEQ );
641 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
642 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
643 slap_timestamp( &op->o_time, ×tamp );
645 sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
646 timestamp.bv_len += 7;
648 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
649 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
650 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &rdn, NULL );
652 attr_merge_one( e, slap_schema.si_ad_objectClass,
653 &log_ocs[logop]->soc_cname, NULL );
654 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
655 &log_ocs[logop]->soc_cname, NULL );
656 attr_merge_one( e, ad_reqStart, ×tamp, NULL );
658 /* Exops have OID appended */
659 if ( logop == LOG_EN_EXTENDED ) {
660 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
661 bv.bv_val = ch_malloc( bv.bv_len + 1 );
662 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
663 bv.bv_val[lo->word.bv_len] = '{';
664 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
665 op->ore_reqoid.bv_len );
666 bv.bv_val[bv.bv_len-1] = '}';
667 bv.bv_val[bv.bv_len] = '\0';
668 attr_merge_one( e, ad_reqType, &bv, NULL );
670 attr_merge_one( e, ad_reqType, &lo->word, NULL );
673 rdn.bv_len = sprintf( rdn.bv_val, "%d", op->o_connid );
674 attr_merge_one( e, ad_reqSession, &rdn, NULL );
676 if ( BER_BVISNULL( &op->o_dn ))
677 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
678 (struct berval *)&slap_empty_bv );
680 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
682 /* FIXME: need to add reqControls and reqRespControls */
687 static struct berval scopes[] = {
691 BER_BVC("subordinate")
694 static struct berval simple = BER_BVC("SIMPLE");
696 static int accesslog_response(Operation *op, SlapReply *rs) {
697 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
698 log_info *li = on->on_bi.bi_private;
699 Attribute *a, *last_attr;
707 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
712 SlapReply rs2 = {REP_RESULT};
714 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
715 return SLAP_CB_CONTINUE;
717 switch ( op->o_tag ) {
718 case LDAP_REQ_ADD: logop = LOG_EN_ADD; break;
719 case LDAP_REQ_DELETE: logop = LOG_EN_DELETE; break;
720 case LDAP_REQ_MODIFY: logop = LOG_EN_MODIFY; break;
721 case LDAP_REQ_MODRDN: logop = LOG_EN_MODRDN; break;
722 case LDAP_REQ_COMPARE: logop = LOG_EN_COMPARE; break;
723 case LDAP_REQ_SEARCH: logop = LOG_EN_SEARCH; break;
724 case LDAP_REQ_BIND: logop = LOG_EN_BIND; break;
725 case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
726 } /* Unbind and Abandon never reach here */
728 lo = logops+logop+EN_OFFSET;
729 if ( !( li->li_ops & lo->mask ))
730 return SLAP_CB_CONTINUE;
732 endtime = slap_get_time();
734 e = accesslog_entry( op, logop );
737 bv.bv_len = sizeof(timebuf);
738 slap_timestamp( &endtime, &bv );
740 attr_merge_one( e, ad_reqEnd, &bv, NULL );
742 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
745 ber_str2bv( rs->sr_text, 0, 0, &bv );
746 attr_merge_one( e, ad_reqMessage, &bv, NULL );
748 bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
751 attr_merge_one( e, ad_reqResult, &bv, NULL );
753 last_attr = attr_find( e->e_attrs, ad_reqResult );
757 /* count all the vals */
759 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
761 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
766 vals = ch_malloc( (i+1) * sizeof( struct berval ));
768 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
770 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
771 vals[i].bv_len = a->a_desc->ad_cname.bv_len + b->bv_len +3;
772 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
773 ptr = lutil_strcopy( vals[i].bv_val,
774 a->a_desc->ad_cname.bv_val );
778 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
779 vals[i].bv_val[vals[i].bv_len] = '\0';
783 vals[i].bv_val = NULL;
785 a = attr_alloc( ad_reqMod );
788 last_attr->a_next = a;
792 /* needs nothing else */
796 /* count all the mods */
798 for ( m=op->orm_modlist; m; m=m->sml_next ) {
799 if ( m->sml_values ) {
800 for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
803 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
807 vals = ch_malloc( (i+1) * sizeof( struct berval ));
809 for ( m=op->orm_modlist; m; m=m->sml_next ) {
810 if ( m->sml_values ) {
811 for (b=m->sml_values; !BER_BVISNULL( b ); b++,i++) {
813 vals[i].bv_len = a->a_desc->ad_cname.bv_len + b->bv_len +3;
814 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
815 ptr = lutil_strcopy( vals[i].bv_val,
816 a->a_desc->ad_cname.bv_val );
818 switch( m->sml_op ) {
819 case LDAP_MOD_ADD: c_op = '+'; break;
820 case LDAP_MOD_DELETE: c_op = '-'; break;
821 case LDAP_MOD_REPLACE: c_op = '='; break;
822 case LDAP_MOD_INCREMENT: c_op = '#'; break;
826 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
827 vals[i].bv_val[vals[i].bv_len] = '\0';
829 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
830 vals[i].bv_len = a->a_desc->ad_cname.bv_len + 2;
831 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
832 ptr = lutil_strcopy( vals[i].bv_val,
833 a->a_desc->ad_cname.bv_val );
840 vals[i].bv_val = NULL;
842 a = attr_alloc( ad_reqMod );
845 last_attr->a_next = a;
849 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
850 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
851 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
853 if ( op->orr_newSup ) {
854 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
859 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
860 op->orc_ava->aa_value.bv_len;
861 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
862 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
864 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
865 bv.bv_val[bv.bv_len] = '\0';
866 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
867 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
871 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
872 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
873 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
875 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
876 attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
877 if ( op->ors_attrs ) {
879 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
881 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
883 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
884 vals[i] = op->ors_attrs[i].an_name;
885 vals[i].bv_val = NULL;
887 attr_merge( e, ad_reqAttr, vals, NULL );
888 op->o_tmpfree( vals, op->o_tmpmemctx );
891 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
892 attr_merge_one( e, ad_reqEntries, &bv, NULL );
894 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
895 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
896 /* FIXME: slimit was zeroed by the backends */
900 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
901 attr_merge_one( e, ad_reqMethod, &simple, NULL );
903 bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
904 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
905 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
906 ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
909 attr_merge_one( e, ad_reqMethod, &bv, NULL );
910 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
914 case LOG_EN_EXTENDED:
915 if ( op->ore_reqdata ) {
916 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
921 op2.o_hdr = op->o_hdr;
922 op2.o_tag = LDAP_REQ_ADD;
923 op2.o_time = endtime;
925 op2.o_bd = li->li_db;
926 op2.o_dn = li->li_db->be_rootdn;
927 op2.o_ndn = li->li_db->be_rootndn;
928 op2.o_req_dn = e->e_name;
929 op2.o_req_ndn = e->e_nname;
931 op2.o_callback = &nullsc;
933 op2.o_bd->be_add( &op2, &rs2 );
936 return SLAP_CB_CONTINUE;
939 /* unbinds are broadcast to all backends; we only log it if this
940 * backend was used for the original bind.
943 accesslog_unbind( Operation *op, SlapReply *rs )
945 if ( op->o_conn->c_authz_backend == op->o_bd ) {
946 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
947 log_info *li = on->on_bi.bi_private;
949 SlapReply rs2 = {REP_RESULT};
952 e = accesslog_entry( op, LOG_EN_UNBIND );
953 op2.o_hdr = op->o_hdr;
954 op2.o_tag = LDAP_REQ_ADD;
955 op2.o_time = op->o_time;
957 op2.o_bd = li->li_db;
958 op2.o_dn = li->li_db->be_rootdn;
959 op2.o_ndn = li->li_db->be_rootndn;
960 op2.o_req_dn = e->e_name;
961 op2.o_req_ndn = e->e_nname;
963 op2.o_callback = &nullsc;
965 op2.o_bd->be_add( &op2, &rs2 );
968 return SLAP_CB_CONTINUE;
972 accesslog_abandon( Operation *op, SlapReply *rs )
974 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
975 log_info *li = on->on_bi.bi_private;
977 SlapReply rs2 = {REP_RESULT};
983 return SLAP_CB_CONTINUE;
985 e = accesslog_entry( op, LOG_EN_ABANDON );
987 bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
988 attr_merge_one( e, ad_reqId, &bv, NULL );
990 op2.o_hdr = op->o_hdr;
991 op2.o_tag = LDAP_REQ_ADD;
992 op2.o_time = op->o_time;
994 op2.o_bd = li->li_db;
995 op2.o_dn = li->li_db->be_rootdn;
996 op2.o_ndn = li->li_db->be_rootndn;
997 op2.o_req_dn = e->e_name;
998 op2.o_req_ndn = e->e_nname;
1000 op2.o_callback = &nullsc;
1002 op2.o_bd->be_add( &op2, &rs2 );
1005 return SLAP_CB_CONTINUE;
1008 static slap_overinst accesslog;
1015 slap_overinst *on = (slap_overinst *)be->bd_info;
1016 log_info *li = ch_calloc(1, sizeof(log_info));
1018 on->on_bi.bi_private = li;
1023 accesslog_db_destroy(
1027 slap_overinst *on = (slap_overinst *)be->bd_info;
1028 log_info *li = on->on_bi.bi_private;
1033 int accesslog_init()
1037 accesslog.on_bi.bi_type = "accesslog";
1038 accesslog.on_bi.bi_db_init = accesslog_db_init;
1039 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1041 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1042 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1043 accesslog.on_response = accesslog_response;
1045 accesslog.on_bi.bi_cf_ocs = log_cfocs;
1047 rc = config_register_schema( log_cfats, log_cfocs );
1048 if ( rc ) return rc;
1050 /* log schema integration */
1051 for ( i=0; lattrs[i].at; i++ ) {
1052 LDAPAttributeType *lat;
1057 lat = ldap_str2attributetype( lattrs[i].at, &code, &err,
1058 LDAP_SCHEMA_ALLOW_ALL );
1060 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1061 "ldap_str2attributetype failed on %d: %s, %s\n",
1062 i, ldap_scherr2str(code), err );
1065 code = at_add( lat, 0, &at, &err );
1066 ldap_memfree( lat );
1068 Debug( LDAP_DEBUG_ANY, "log_back_initialize: "
1069 "at_add failed on %d: %s\n",
1070 i, scherr2str(code), 0 );
1073 if ( slap_bv2ad( &at->sat_cname, lattrs[i].ad, &err )) {
1074 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1075 "slap_bv2ad failed on %d: %s\n",
1080 for ( i=0; locs[i].ot; i++ ) {
1081 LDAPObjectClass *loc;
1086 loc = ldap_str2objectclass( locs[i].ot, &code, &err,
1087 LDAP_SCHEMA_ALLOW_ALL );
1089 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1090 "ldap_str2objectclass failed on %d: %s, %s\n",
1091 i, ldap_scherr2str(code), err );
1095 code = oc_add( loc, 0, &oc, &err );
1096 ldap_memfree( loc );
1098 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1099 "oc_add failed on %d: %s\n",
1100 i, scherr2str(code), 0 );
1107 return overlay_register(&accesslog);
1110 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1111 int init_module( int argc, char *argv[]) {
1112 return accesslog_init();
1116 #endif /* SLAPD_OVER_ACCESSLOG */