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
46 #define LOG_OP_UNKNOWN 0x400
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)
54 typedef struct log_info {
62 static ConfigDriver log_cf_gen;
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,
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 },
88 static ConfigOCs log_cfocs[] = {
90 "NAME 'olcAccessLogConfig' "
91 "DESC 'Access log configuration' "
92 "SUP olcOverlayConfig "
93 "MUST olcAccessLogDB "
94 "MAY ( olcAccessLogOps $ olcAccessLogPurge ) )",
95 Cft_Overlay, log_cfats },
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 },
118 /* Start with "add" in logops */
136 static ObjectClass *log_ocs[LOG_EN__COUNT];
138 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
140 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
141 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
143 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
144 *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
145 *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
146 *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
147 *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
148 *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
149 *ad_reqId, *ad_reqMessage, *ad_oldest;
153 AttributeDescription **ad;
155 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
156 "DESC 'Target DN of request' "
157 "EQUALITY distinguishedNameMatch "
159 "SINGLE-VALUE )", &ad_reqDN },
160 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
161 "DESC 'Start time of request' "
162 "EQUALITY generalizedTimeMatch "
163 "ORDERING generalizedTimeOrderingMatch "
164 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
165 "SINGLE-VALUE )", &ad_reqStart },
166 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
167 "DESC 'End time of request' "
168 "EQUALITY generalizedTimeMatch "
169 "ORDERING generalizedTimeOrderingMatch "
170 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
171 "SINGLE-VALUE )", &ad_reqEnd },
172 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
173 "DESC 'Type of request' "
174 "EQUALITY caseIgnoreMatch "
175 "SYNTAX OMsDirectoryString "
176 "SINGLE-VALUE )", &ad_reqType },
177 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
178 "DESC 'Session ID of request' "
179 "EQUALITY caseIgnoreMatch "
180 "SYNTAX OMsDirectoryString "
181 "SINGLE-VALUE )", &ad_reqSession },
182 { "( " LOG_SCHEMA_AT ".6 NAME 'reqResult' "
183 "DESC 'Result code of request' "
184 "EQUALITY integerMatch "
186 "SINGLE-VALUE )", &ad_reqResult },
187 { "( " LOG_SCHEMA_AT ".7 NAME 'reqAuthzID' "
188 "DESC 'AUthorization ID of requestor' "
189 "EQUALITY distinguishedNameMatch "
191 "SINGLE-VALUE )", &ad_reqAuthzID },
192 { "( " LOG_SCHEMA_AT ".8 NAME 'reqControls' "
193 "DESC 'Request controls' "
194 "SYNTAX OMsOctetString )", &ad_reqControls },
195 { "( " LOG_SCHEMA_AT ".9 NAME 'reqRespControls' "
196 "DESC 'Response controls of request' "
197 "SYNTAX OMsOctetString )", &ad_reqRespControls },
198 { "( " LOG_SCHEMA_AT ".10 NAME 'reqMethod' "
199 "DESC 'Bind method of request' "
200 "EQUALITY caseIgnoreMatch "
201 "SYNTAX OMsDirectoryString "
202 "SINGLE-VALUE )", &ad_reqMethod },
203 { "( " LOG_SCHEMA_AT ".11 NAME 'reqAssertion' "
204 "DESC 'Compare Assertion of request' "
205 "SYNTAX OMsDirectoryString "
206 "SINGLE-VALUE )", &ad_reqAssertion },
207 { "( " LOG_SCHEMA_AT ".12 NAME 'reqNewRDN' "
208 "DESC 'New RDN of request' "
209 "EQUALITY distinguishedNameMatch "
211 "SINGLE-VALUE )", &ad_reqNewRDN },
212 { "( " LOG_SCHEMA_AT ".13 NAME 'reqNewSuperior' "
213 "DESC 'New superior DN of request' "
214 "EQUALITY distinguishedNameMatch "
216 "SINGLE-VALUE )", &ad_reqNewSuperior },
217 { "( " LOG_SCHEMA_AT ".14 NAME 'reqDeleteOldRDN' "
218 "DESC 'Delete old RDN' "
220 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
221 { "( " LOG_SCHEMA_AT ".15 NAME 'reqMod' "
222 "DESC 'Modifications of request' "
223 "SYNTAX OMsDirectoryString "
224 "EQUALITY caseIgnoreMatch "
225 "SUBSTR caseIgnoreSubstringsMatch )", &ad_reqMod },
226 { "( " LOG_SCHEMA_AT ".16 NAME 'reqScope' "
227 "DESC 'Scope of request' "
228 "SYNTAX OMsDirectoryString "
229 "SINGLE-VALUE )", &ad_reqScope },
230 { "( " LOG_SCHEMA_AT ".17 NAME 'reqFilter' "
231 "DESC 'Filter of request' "
232 "SYNTAX OMsDirectoryString "
233 "SINGLE-VALUE )", &ad_reqFilter },
234 { "( " LOG_SCHEMA_AT ".18 NAME 'reqAttr' "
235 "DESC 'Attributes of request' "
236 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
237 { "( " LOG_SCHEMA_AT ".19 NAME 'reqEntries' "
238 "DESC 'Number of entries returned' "
240 "SINGLE-VALUE )", &ad_reqEntries },
241 { "( " LOG_SCHEMA_AT ".20 NAME 'reqSizeLimit' "
242 "DESC 'Size limit of request' "
244 "SINGLE-VALUE )", &ad_reqSizeLimit },
245 { "( " LOG_SCHEMA_AT ".21 NAME 'reqTimeLimit' "
246 "DESC 'Time limit of request' "
248 "SINGLE-VALUE )", &ad_reqTimeLimit },
249 { "( " LOG_SCHEMA_AT ".22 NAME 'reqAttrsOnly' "
250 "DESC 'Attributes and values of request' "
252 "SINGLE-VALUE )", &ad_reqAttrsOnly },
253 { "( " LOG_SCHEMA_AT ".23 NAME 'reqData' "
254 "DESC 'Data of extended request' "
255 "SYNTAX OMsOctetString "
256 "SINGLE-VALUE )", &ad_reqData },
257 { "( " LOG_SCHEMA_AT ".24 NAME 'reqId' "
258 "DESC 'ID of Request to Abandon' "
260 "SINGLE-VALUE )", &ad_reqId },
261 { "( " LOG_SCHEMA_AT ".25 NAME 'reqMessage' "
262 "DESC 'Error text of request' "
263 "SYNTAX OMsDirectoryString "
264 "SINGLE-VALUE )", &ad_reqMessage },
266 { "( " LOG_SCHEMA_AT ".26 NAME 'auditOldest' "
267 "DESC 'Oldest record in this branch' "
268 "EQUALITY generalizedTimeMatch "
269 "ORDERING generalizedTimeOrderingMatch "
270 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
271 "SINGLE-VALUE )", &ad_oldest },
281 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
282 "SUP top STRUCTURAL "
284 "MAY cn )", &oc_container },
286 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
287 "DESC 'OpenLDAP request auditing' "
288 "SUP top STRUCTURAL "
289 "MUST ( reqStart $ reqType $ reqSession ) "
290 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
291 "reqResult $ reqMessage ) )", &log_ocs[LOG_EN_UNBIND] },
292 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
293 "DESC 'OpenLDAP read request record' "
294 "SUP auditObject STRUCTURAL )", NULL },
295 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
296 "DESC 'OpenLDAP write request record' "
297 "SUP auditObject STRUCTURAL )", &log_ocs[LOG_EN_DELETE] },
298 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
299 "DESC 'Abandon operation' "
300 "SUP auditObject STRUCTURAL "
301 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
302 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
303 "DESC 'Add operation' "
304 "SUP auditWriteObject STRUCTURAL "
305 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
306 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
307 "DESC 'Bind operation' "
308 "SUP auditObject STRUCTURAL "
309 "MUST reqMethod )", &log_ocs[LOG_EN_BIND] },
310 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
311 "DESC 'Compare operation' "
312 "SUP auditReadObject STRUCTURAL "
313 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
314 { "( " LOG_SCHEMA_OC ".8 NAME 'auditModify' "
315 "DESC 'Modify operation' "
316 "SUP auditWriteObject STRUCTURAL "
317 "MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
318 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModRDN' "
319 "DESC 'ModRDN operation' "
320 "SUP auditWriteObject STRUCTURAL "
321 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
322 "MAY reqNewSuperior )", &log_ocs[LOG_EN_MODRDN] },
323 { "( " LOG_SCHEMA_OC ".10 NAME 'auditSearch' "
324 "DESC 'Search operation' "
325 "SUP auditReadObject STRUCTURAL "
326 "MUST ( reqScope $ reqAttrsonly ) "
327 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
328 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
329 { "( " LOG_SCHEMA_OC ".11 NAME 'auditExtended' "
330 "DESC 'Extended operation' "
331 "SUP auditObject STRUCTURAL "
332 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
336 #define RDNEQ "reqStart="
338 /* Our time intervals are of the form [dd+]hh:mm[:ss]
339 * If a field is present, it must be two digits. We assume no one
340 * will want to keep log records for longer than 99 days.
343 log_age_parse(char *agestr)
349 /* Is there a days delimiter? */
350 if ( agestr[2] == '+' ) {
353 } else if ( agestr[2] != ':' ) {
354 /* No valid delimiter found, fail */
361 /* if there's a delimiter, it can only be a colon */
362 if ( agestr[2] && agestr[2] != ':' )
365 /* If we're at the end of the string, and we started with days,
366 * fail because we expected to find minutes too.
368 if ( gotdays && !agestr[2] )
380 /* last field can only be seconds */
381 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
391 t1 += atoi( agestr );
397 log_age_unparse( int age, struct berval *agebv )
413 ptr += sprintf( ptr, "%02d+", dd );
414 ptr += sprintf( ptr, "%02d:%02d", hh, mm );
416 ptr += sprintf( ptr, ":%02d", ss );
418 agebv->bv_len = ptr - agebv->bv_val;
421 static slap_callback nullsc = { NULL, slap_null_cb, NULL, NULL };
423 #define PURGE_INCREMENT 100
425 typedef struct purge_data {
433 log_old_lookup( Operation *op, SlapReply *rs )
435 purge_data *pd = op->o_callback->sc_private;
437 if ( rs->sr_type != REP_SEARCH) return 0;
439 if ( pd->used >= pd->slots ) {
440 pd->slots += PURGE_INCREMENT;
441 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
442 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
444 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
445 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
450 /* Periodically search for old entries in the log database and delete them */
452 accesslog_purge( void *ctx, void *arg )
454 struct re_s *rtask = arg;
455 struct log_info *li = rtask->arg;
457 Connection conn = {0};
458 char opbuf[OPERATION_BUFFER_SIZE];
459 Operation *op = (Operation *)opbuf;
460 SlapReply rs = {REP_RESULT};
461 slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
463 AttributeAssertion ava = {0};
465 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
466 time_t old = slap_get_time();
468 connection_fake_init( &conn, op, ctx );
470 f.f_choice = LDAP_FILTER_LE;
474 ava.aa_desc = ad_reqStart;
475 ava.aa_value.bv_val = timebuf;
476 ava.aa_value.bv_len = sizeof(timebuf);
479 slap_timestamp( &old, &ava.aa_value );
481 op->o_tag = LDAP_REQ_SEARCH;
482 op->o_bd = li->li_db;
483 op->o_dn = li->li_db->be_rootdn;
484 op->o_ndn = li->li_db->be_rootndn;
485 op->o_req_dn = li->li_db->be_suffix[0];
486 op->o_req_ndn = li->li_db->be_nsuffix[0];
487 op->o_callback = &cb;
488 op->ors_scope = LDAP_SCOPE_ONELEVEL;
489 op->ors_deref = LDAP_DEREF_NEVER;
490 op->ors_tlimit = SLAP_NO_LIMIT;
491 op->ors_slimit = SLAP_NO_LIMIT;
493 filter2bv_x( op, &f, &op->ors_filterstr );
494 op->ors_attrs = slap_anlist_no_attrs;
495 op->ors_attrsonly = 1;
499 op->o_bd->be_search( op, &rs );
500 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
505 op->o_tag = LDAP_REQ_DELETE;
506 op->o_callback = &nullsc;
508 for (i=0; i<pd.used; i++) {
509 op->o_req_dn = pd.dn[i];
510 op->o_req_ndn = pd.ndn[i];
511 op->o_bd->be_delete( op, &rs );
512 ch_free( pd.ndn[i].bv_val );
513 ch_free( pd.dn[i].bv_val );
519 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
520 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
521 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
527 log_cf_gen(ConfigArgs *c)
529 slap_overinst *on = (slap_overinst *)c->bi;
530 struct log_info *li = on->on_bi.bi_private;
532 slap_mask_t tmask = 0;
533 char agebuf[2*STRLENOF("dd+hh:mm:ss ")];
534 struct berval agebv, cyclebv;
537 case SLAP_CONFIG_EMIT:
540 value_add( &c->rvalue_vals, li->li_db->be_suffix );
541 value_add( &c->rvalue_nvals, li->li_db->be_nsuffix );
544 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
547 agebv.bv_val = agebuf;
548 log_age_unparse( li->li_age, &agebv );
549 agebv.bv_val[agebv.bv_len] = ' ';
551 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
552 log_age_unparse( li->li_cycle, &cyclebv );
553 agebv.bv_len += cyclebv.bv_len;
554 value_add_one( &c->rvalue_vals, &agebv );
558 case LDAP_MOD_DELETE:
561 /* noop. this should always be a valid backend. */
567 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
569 li->li_ops &= ~tmask;
574 struct re_s *re = li->li_task;
576 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
577 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
578 ldap_pvt_runqueue_remove( &slapd_rq, re );
588 li->li_db = select_backend( &c->value_ndn, 0, 0 );
590 sprintf( c->msg, "<%s> no matching backend found for suffix",
592 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
593 c->log, c->msg, c->value_dn.bv_val );
596 ch_free( c->value_dn.bv_val );
597 ch_free( c->value_ndn.bv_val );
600 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
605 li->li_age = log_age_parse( c->argv[1] );
606 if ( li->li_age == -1 ) {
609 li->li_cycle = log_age_parse( c->argv[2] );
610 if ( li->li_cycle == -1 ) {
612 } else if ( slapMode & SLAP_SERVER_MODE ) {
613 struct re_s *re = li->li_task;
615 re->interval.tv_sec = li->li_cycle;
617 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
618 li->li_cycle, accesslog_purge, li,
619 "accesslog_purge", li->li_db ?
620 li->li_db->be_suffix[0].bv_val :
621 c->be->be_suffix[0].bv_val );
631 static Entry *accesslog_entry( Operation *op, int logop ) {
632 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
633 log_info *li = on->on_bi.bi_private;
635 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
636 struct berval rdn, timestamp, bv;
637 slap_verbmasks *lo = logops+logop+EN_OFFSET;
639 Entry *e = ch_calloc( 1, sizeof(Entry) );
641 strcpy( rdnbuf, RDNEQ );
644 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
645 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
646 slap_timestamp( &op->o_time, ×tamp );
648 sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
649 timestamp.bv_len += 7;
651 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
652 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
653 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &rdn, NULL );
655 attr_merge_one( e, slap_schema.si_ad_objectClass,
656 &log_ocs[logop]->soc_cname, NULL );
657 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
658 &log_ocs[logop]->soc_cname, NULL );
659 attr_merge_one( e, ad_reqStart, ×tamp, NULL );
661 /* Exops have OID appended */
662 if ( logop == LOG_EN_EXTENDED ) {
663 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
664 bv.bv_val = ch_malloc( bv.bv_len + 1 );
665 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
666 bv.bv_val[lo->word.bv_len] = '{';
667 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
668 op->ore_reqoid.bv_len );
669 bv.bv_val[bv.bv_len-1] = '}';
670 bv.bv_val[bv.bv_len] = '\0';
671 attr_merge_one( e, ad_reqType, &bv, NULL );
673 attr_merge_one( e, ad_reqType, &lo->word, NULL );
676 rdn.bv_len = sprintf( rdn.bv_val, "%lu", op->o_connid );
677 attr_merge_one( e, ad_reqSession, &rdn, NULL );
679 if ( BER_BVISNULL( &op->o_dn ))
680 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
681 (struct berval *)&slap_empty_bv );
683 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
685 /* FIXME: need to add reqControls and reqRespControls */
690 static struct berval scopes[] = {
694 BER_BVC("subordinate")
697 static struct berval simple = BER_BVC("SIMPLE");
699 static int accesslog_response(Operation *op, SlapReply *rs) {
700 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
701 log_info *li = on->on_bi.bi_private;
702 Attribute *a, *last_attr;
710 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
715 SlapReply rs2 = {REP_RESULT};
717 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
718 return SLAP_CB_CONTINUE;
720 switch ( op->o_tag ) {
721 case LDAP_REQ_ADD: logop = LOG_EN_ADD; break;
722 case LDAP_REQ_DELETE: logop = LOG_EN_DELETE; break;
723 case LDAP_REQ_MODIFY: logop = LOG_EN_MODIFY; break;
724 case LDAP_REQ_MODRDN: logop = LOG_EN_MODRDN; break;
725 case LDAP_REQ_COMPARE: logop = LOG_EN_COMPARE; break;
726 case LDAP_REQ_SEARCH: logop = LOG_EN_SEARCH; break;
727 case LDAP_REQ_BIND: logop = LOG_EN_BIND; break;
728 case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
729 default: /* unknown operation type */
730 logop = LOG_EN_UNKNOWN; break;
731 } /* Unbind and Abandon never reach here */
733 lo = logops+logop+EN_OFFSET;
734 if ( !( li->li_ops & lo->mask ))
735 return SLAP_CB_CONTINUE;
737 endtime = slap_get_time();
739 e = accesslog_entry( op, logop );
742 bv.bv_len = sizeof(timebuf);
743 slap_timestamp( &endtime, &bv );
745 attr_merge_one( e, ad_reqEnd, &bv, NULL );
747 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
750 ber_str2bv( rs->sr_text, 0, 0, &bv );
751 attr_merge_one( e, ad_reqMessage, &bv, NULL );
753 bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
756 attr_merge_one( e, ad_reqResult, &bv, NULL );
758 last_attr = attr_find( e->e_attrs, ad_reqResult );
762 /* count all the vals */
764 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
766 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
771 vals = ch_malloc( (i+1) * sizeof( struct berval ));
773 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
775 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
776 vals[i].bv_len = a->a_desc->ad_cname.bv_len + b->bv_len +3;
777 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
778 ptr = lutil_strcopy( vals[i].bv_val,
779 a->a_desc->ad_cname.bv_val );
783 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
784 vals[i].bv_val[vals[i].bv_len] = '\0';
788 vals[i].bv_val = NULL;
790 a = attr_alloc( ad_reqMod );
793 last_attr->a_next = a;
797 /* needs nothing else */
801 /* count all the mods */
803 for ( m=op->orm_modlist; m; m=m->sml_next ) {
804 if ( m->sml_values ) {
805 for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
808 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
812 vals = ch_malloc( (i+1) * sizeof( struct berval ));
814 for ( m=op->orm_modlist; m; m=m->sml_next ) {
815 if ( m->sml_values ) {
816 for (b=m->sml_values; !BER_BVISNULL( b ); b++,i++) {
818 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + b->bv_len +3;
819 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
820 ptr = lutil_strcopy( vals[i].bv_val,
821 m->sml_desc->ad_cname.bv_val );
823 switch( m->sml_op ) {
824 case LDAP_MOD_ADD: c_op = '+'; break;
825 case LDAP_MOD_DELETE: c_op = '-'; break;
826 case LDAP_MOD_REPLACE: c_op = '='; break;
827 case LDAP_MOD_INCREMENT: c_op = '#'; break;
829 /* unknown op. there shouldn't be any of these. we
830 * don't know what to do with it, but we shouldn't just
833 default: c_op = '?'; break;
837 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
838 vals[i].bv_val[vals[i].bv_len] = '\0';
840 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
841 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
842 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
843 ptr = lutil_strcopy( vals[i].bv_val,
844 a->a_desc->ad_cname.bv_val );
851 vals[i].bv_val = NULL;
853 a = attr_alloc( ad_reqMod );
856 last_attr->a_next = a;
860 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
861 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
862 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
864 if ( op->orr_newSup ) {
865 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
870 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
871 op->orc_ava->aa_value.bv_len;
872 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
873 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
875 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
876 bv.bv_val[bv.bv_len] = '\0';
877 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
878 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
882 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
883 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
884 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
886 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
887 attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
888 if ( op->ors_attrs ) {
890 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
892 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
894 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
895 vals[i] = op->ors_attrs[i].an_name;
896 vals[i].bv_val = NULL;
898 attr_merge( e, ad_reqAttr, vals, NULL );
899 op->o_tmpfree( vals, op->o_tmpmemctx );
902 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
903 attr_merge_one( e, ad_reqEntries, &bv, NULL );
905 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
906 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
907 /* FIXME: slimit was zeroed by the backends */
911 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
912 attr_merge_one( e, ad_reqMethod, &simple, NULL );
914 bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
915 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
916 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
917 ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
920 attr_merge_one( e, ad_reqMethod, &bv, NULL );
921 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
925 case LOG_EN_EXTENDED:
926 if ( op->ore_reqdata ) {
927 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
932 /* we don't know its parameters, don't add any */
936 op2.o_hdr = op->o_hdr;
937 op2.o_tag = LDAP_REQ_ADD;
938 op2.o_time = endtime;
940 op2.o_bd = li->li_db;
941 op2.o_dn = li->li_db->be_rootdn;
942 op2.o_ndn = li->li_db->be_rootndn;
943 op2.o_req_dn = e->e_name;
944 op2.o_req_ndn = e->e_nname;
946 op2.o_callback = &nullsc;
948 op2.o_bd->be_add( &op2, &rs2 );
951 return SLAP_CB_CONTINUE;
954 /* unbinds are broadcast to all backends; we only log it if this
955 * backend was used for the original bind.
958 accesslog_unbind( Operation *op, SlapReply *rs )
960 if ( op->o_conn->c_authz_backend == op->o_bd ) {
961 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
962 log_info *li = on->on_bi.bi_private;
964 SlapReply rs2 = {REP_RESULT};
967 e = accesslog_entry( op, LOG_EN_UNBIND );
968 op2.o_hdr = op->o_hdr;
969 op2.o_tag = LDAP_REQ_ADD;
970 op2.o_time = op->o_time;
972 op2.o_bd = li->li_db;
973 op2.o_dn = li->li_db->be_rootdn;
974 op2.o_ndn = li->li_db->be_rootndn;
975 op2.o_req_dn = e->e_name;
976 op2.o_req_ndn = e->e_nname;
978 op2.o_callback = &nullsc;
980 op2.o_bd->be_add( &op2, &rs2 );
983 return SLAP_CB_CONTINUE;
987 accesslog_abandon( Operation *op, SlapReply *rs )
989 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
990 log_info *li = on->on_bi.bi_private;
992 SlapReply rs2 = {REP_RESULT};
998 return SLAP_CB_CONTINUE;
1000 e = accesslog_entry( op, LOG_EN_ABANDON );
1002 bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
1003 attr_merge_one( e, ad_reqId, &bv, NULL );
1005 op2.o_hdr = op->o_hdr;
1006 op2.o_tag = LDAP_REQ_ADD;
1007 op2.o_time = op->o_time;
1009 op2.o_bd = li->li_db;
1010 op2.o_dn = li->li_db->be_rootdn;
1011 op2.o_ndn = li->li_db->be_rootndn;
1012 op2.o_req_dn = e->e_name;
1013 op2.o_req_ndn = e->e_nname;
1015 op2.o_callback = &nullsc;
1017 op2.o_bd->be_add( &op2, &rs2 );
1020 return SLAP_CB_CONTINUE;
1023 static slap_overinst accesslog;
1030 slap_overinst *on = (slap_overinst *)be->bd_info;
1031 log_info *li = ch_calloc(1, sizeof(log_info));
1033 on->on_bi.bi_private = li;
1038 accesslog_db_destroy(
1042 slap_overinst *on = (slap_overinst *)be->bd_info;
1043 log_info *li = on->on_bi.bi_private;
1046 return LDAP_SUCCESS;
1049 int accesslog_init()
1053 accesslog.on_bi.bi_type = "accesslog";
1054 accesslog.on_bi.bi_db_init = accesslog_db_init;
1055 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1057 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1058 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1059 accesslog.on_response = accesslog_response;
1061 accesslog.on_bi.bi_cf_ocs = log_cfocs;
1063 rc = config_register_schema( log_cfats, log_cfocs );
1064 if ( rc ) return rc;
1066 /* log schema integration */
1067 for ( i=0; lattrs[i].at; i++ ) {
1068 LDAPAttributeType *lat;
1073 lat = ldap_str2attributetype( lattrs[i].at, &code, &err,
1074 LDAP_SCHEMA_ALLOW_ALL );
1076 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1077 "ldap_str2attributetype failed on %d: %s, %s\n",
1078 i, ldap_scherr2str(code), err );
1081 code = at_add( lat, 0, &at, &err );
1082 ldap_memfree( lat );
1084 Debug( LDAP_DEBUG_ANY, "log_back_initialize: "
1085 "at_add failed on %d: %s\n",
1086 i, scherr2str(code), 0 );
1089 if ( slap_bv2ad( &at->sat_cname, lattrs[i].ad, &err )) {
1090 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1091 "slap_bv2ad failed on %d: %s\n",
1096 for ( i=0; locs[i].ot; i++ ) {
1097 LDAPObjectClass *loc;
1102 loc = ldap_str2objectclass( locs[i].ot, &code, &err,
1103 LDAP_SCHEMA_ALLOW_ALL );
1105 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1106 "ldap_str2objectclass failed on %d: %s, %s\n",
1107 i, ldap_scherr2str(code), err );
1111 code = oc_add( loc, 0, &oc, &err );
1112 ldap_memfree( loc );
1114 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1115 "oc_add failed on %d: %s\n",
1116 i, scherr2str(code), 0 );
1123 return overlay_register(&accesslog);
1126 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1127 int init_module( int argc, char *argv[]) {
1128 return accesslog_init();
1132 #endif /* SLAPD_OVER_ACCESSLOG */