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;
151 static AttributeDescription *ad_oldest;
156 AttributeDescription **ad;
158 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
159 "DESC 'Target DN of request' "
160 "EQUALITY distinguishedNameMatch "
162 "SINGLE-VALUE )", &ad_reqDN },
163 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
164 "DESC 'Start time of request' "
165 "EQUALITY generalizedTimeMatch "
166 "ORDERING generalizedTimeOrderingMatch "
167 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
168 "SINGLE-VALUE )", &ad_reqStart },
169 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
170 "DESC 'End time of request' "
171 "EQUALITY generalizedTimeMatch "
172 "ORDERING generalizedTimeOrderingMatch "
173 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
174 "SINGLE-VALUE )", &ad_reqEnd },
175 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
176 "DESC 'Type of request' "
177 "EQUALITY caseIgnoreMatch "
178 "SYNTAX OMsDirectoryString "
179 "SINGLE-VALUE )", &ad_reqType },
180 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
181 "DESC 'Session ID of request' "
182 "EQUALITY caseIgnoreMatch "
183 "SYNTAX OMsDirectoryString "
184 "SINGLE-VALUE )", &ad_reqSession },
185 { "( " LOG_SCHEMA_AT ".6 NAME 'reqResult' "
186 "DESC 'Result code of request' "
187 "EQUALITY integerMatch "
189 "SINGLE-VALUE )", &ad_reqResult },
190 { "( " LOG_SCHEMA_AT ".7 NAME 'reqAuthzID' "
191 "DESC 'AUthorization ID of requestor' "
192 "EQUALITY distinguishedNameMatch "
194 "SINGLE-VALUE )", &ad_reqAuthzID },
195 { "( " LOG_SCHEMA_AT ".8 NAME 'reqControls' "
196 "DESC 'Request controls' "
197 "SYNTAX OMsOctetString )", &ad_reqControls },
198 { "( " LOG_SCHEMA_AT ".9 NAME 'reqRespControls' "
199 "DESC 'Response controls of request' "
200 "SYNTAX OMsOctetString )", &ad_reqRespControls },
201 { "( " LOG_SCHEMA_AT ".10 NAME 'reqMethod' "
202 "DESC 'Bind method of request' "
203 "EQUALITY caseIgnoreMatch "
204 "SYNTAX OMsDirectoryString "
205 "SINGLE-VALUE )", &ad_reqMethod },
206 { "( " LOG_SCHEMA_AT ".11 NAME 'reqAssertion' "
207 "DESC 'Compare Assertion of request' "
208 "SYNTAX OMsDirectoryString "
209 "SINGLE-VALUE )", &ad_reqAssertion },
210 { "( " LOG_SCHEMA_AT ".12 NAME 'reqNewRDN' "
211 "DESC 'New RDN of request' "
212 "EQUALITY distinguishedNameMatch "
214 "SINGLE-VALUE )", &ad_reqNewRDN },
215 { "( " LOG_SCHEMA_AT ".13 NAME 'reqNewSuperior' "
216 "DESC 'New superior DN of request' "
217 "EQUALITY distinguishedNameMatch "
219 "SINGLE-VALUE )", &ad_reqNewSuperior },
220 { "( " LOG_SCHEMA_AT ".14 NAME 'reqDeleteOldRDN' "
221 "DESC 'Delete old RDN' "
223 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
224 { "( " LOG_SCHEMA_AT ".15 NAME 'reqMod' "
225 "DESC 'Modifications of request' "
226 "SYNTAX OMsDirectoryString "
227 "EQUALITY caseIgnoreMatch "
228 "SUBSTR caseIgnoreSubstringsMatch )", &ad_reqMod },
229 { "( " LOG_SCHEMA_AT ".16 NAME 'reqScope' "
230 "DESC 'Scope of request' "
231 "SYNTAX OMsDirectoryString "
232 "SINGLE-VALUE )", &ad_reqScope },
233 { "( " LOG_SCHEMA_AT ".17 NAME 'reqFilter' "
234 "DESC 'Filter of request' "
235 "SYNTAX OMsDirectoryString "
236 "SINGLE-VALUE )", &ad_reqFilter },
237 { "( " LOG_SCHEMA_AT ".18 NAME 'reqAttr' "
238 "DESC 'Attributes of request' "
239 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
240 { "( " LOG_SCHEMA_AT ".19 NAME 'reqEntries' "
241 "DESC 'Number of entries returned' "
243 "SINGLE-VALUE )", &ad_reqEntries },
244 { "( " LOG_SCHEMA_AT ".20 NAME 'reqSizeLimit' "
245 "DESC 'Size limit of request' "
247 "SINGLE-VALUE )", &ad_reqSizeLimit },
248 { "( " LOG_SCHEMA_AT ".21 NAME 'reqTimeLimit' "
249 "DESC 'Time limit of request' "
251 "SINGLE-VALUE )", &ad_reqTimeLimit },
252 { "( " LOG_SCHEMA_AT ".22 NAME 'reqAttrsOnly' "
253 "DESC 'Attributes and values of request' "
255 "SINGLE-VALUE )", &ad_reqAttrsOnly },
256 { "( " LOG_SCHEMA_AT ".23 NAME 'reqData' "
257 "DESC 'Data of extended request' "
258 "SYNTAX OMsOctetString "
259 "SINGLE-VALUE )", &ad_reqData },
260 { "( " LOG_SCHEMA_AT ".24 NAME 'reqId' "
261 "DESC 'ID of Request to Abandon' "
263 "SINGLE-VALUE )", &ad_reqId },
264 { "( " LOG_SCHEMA_AT ".25 NAME 'reqMessage' "
265 "DESC 'Error text of request' "
266 "SYNTAX OMsDirectoryString "
267 "SINGLE-VALUE )", &ad_reqMessage },
269 { "( " LOG_SCHEMA_AT ".26 NAME 'auditOldest' "
270 "DESC 'Oldest record in this branch' "
271 "EQUALITY generalizedTimeMatch "
272 "ORDERING generalizedTimeOrderingMatch "
273 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
274 "SINGLE-VALUE )", &ad_oldest },
284 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
285 "SUP top STRUCTURAL "
287 "MAY cn )", &oc_container },
289 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
290 "DESC 'OpenLDAP request auditing' "
291 "SUP top STRUCTURAL "
292 "MUST ( reqStart $ reqType $ reqSession ) "
293 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
294 "reqResult $ reqMessage ) )", &log_ocs[LOG_EN_UNBIND] },
295 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
296 "DESC 'OpenLDAP read request record' "
297 "SUP auditObject STRUCTURAL )", NULL },
298 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
299 "DESC 'OpenLDAP write request record' "
300 "SUP auditObject STRUCTURAL )", &log_ocs[LOG_EN_DELETE] },
301 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
302 "DESC 'Abandon operation' "
303 "SUP auditObject STRUCTURAL "
304 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
305 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
306 "DESC 'Add operation' "
307 "SUP auditWriteObject STRUCTURAL "
308 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
309 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
310 "DESC 'Bind operation' "
311 "SUP auditObject STRUCTURAL "
312 "MUST reqMethod )", &log_ocs[LOG_EN_BIND] },
313 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
314 "DESC 'Compare operation' "
315 "SUP auditReadObject STRUCTURAL "
316 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
317 { "( " LOG_SCHEMA_OC ".8 NAME 'auditModify' "
318 "DESC 'Modify operation' "
319 "SUP auditWriteObject STRUCTURAL "
320 "MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
321 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModRDN' "
322 "DESC 'ModRDN operation' "
323 "SUP auditWriteObject STRUCTURAL "
324 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
325 "MAY reqNewSuperior )", &log_ocs[LOG_EN_MODRDN] },
326 { "( " LOG_SCHEMA_OC ".10 NAME 'auditSearch' "
327 "DESC 'Search operation' "
328 "SUP auditReadObject STRUCTURAL "
329 "MUST ( reqScope $ reqAttrsonly ) "
330 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
331 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
332 { "( " LOG_SCHEMA_OC ".11 NAME 'auditExtended' "
333 "DESC 'Extended operation' "
334 "SUP auditObject STRUCTURAL "
335 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
339 #define RDNEQ "reqStart="
341 /* Our time intervals are of the form [dd+]hh:mm[:ss]
342 * If a field is present, it must be two digits. We assume no one
343 * will want to keep log records for longer than 99 days.
346 log_age_parse(char *agestr)
352 /* Is there a days delimiter? */
353 if ( agestr[2] == '+' ) {
356 } else if ( agestr[2] != ':' ) {
357 /* No valid delimiter found, fail */
364 /* if there's a delimiter, it can only be a colon */
365 if ( agestr[2] && agestr[2] != ':' )
368 /* If we're at the end of the string, and we started with days,
369 * fail because we expected to find minutes too.
371 if ( gotdays && !agestr[2] )
383 /* last field can only be seconds */
384 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
394 t1 += atoi( agestr );
400 log_age_unparse( int age, struct berval *agebv )
416 ptr += sprintf( ptr, "%02d+", dd );
417 ptr += sprintf( ptr, "%02d:%02d", hh, mm );
419 ptr += sprintf( ptr, ":%02d", ss );
421 agebv->bv_len = ptr - agebv->bv_val;
424 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
426 #define PURGE_INCREMENT 100
428 typedef struct purge_data {
436 log_old_lookup( Operation *op, SlapReply *rs )
438 purge_data *pd = op->o_callback->sc_private;
440 if ( rs->sr_type != REP_SEARCH) return 0;
442 if ( pd->used >= pd->slots ) {
443 pd->slots += PURGE_INCREMENT;
444 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
445 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
447 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
448 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
453 /* Periodically search for old entries in the log database and delete them */
455 accesslog_purge( void *ctx, void *arg )
457 struct re_s *rtask = arg;
458 struct log_info *li = rtask->arg;
460 Connection conn = {0};
461 char opbuf[OPERATION_BUFFER_SIZE];
462 Operation *op = (Operation *)opbuf;
463 SlapReply rs = {REP_RESULT};
464 slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
466 AttributeAssertion ava = {0};
468 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
469 time_t old = slap_get_time();
471 connection_fake_init( &conn, op, ctx );
473 f.f_choice = LDAP_FILTER_LE;
477 ava.aa_desc = ad_reqStart;
478 ava.aa_value.bv_val = timebuf;
479 ava.aa_value.bv_len = sizeof(timebuf);
482 slap_timestamp( &old, &ava.aa_value );
484 op->o_tag = LDAP_REQ_SEARCH;
485 op->o_bd = li->li_db;
486 op->o_dn = li->li_db->be_rootdn;
487 op->o_ndn = li->li_db->be_rootndn;
488 op->o_req_dn = li->li_db->be_suffix[0];
489 op->o_req_ndn = li->li_db->be_nsuffix[0];
490 op->o_callback = &cb;
491 op->ors_scope = LDAP_SCOPE_ONELEVEL;
492 op->ors_deref = LDAP_DEREF_NEVER;
493 op->ors_tlimit = SLAP_NO_LIMIT;
494 op->ors_slimit = SLAP_NO_LIMIT;
496 filter2bv_x( op, &f, &op->ors_filterstr );
497 op->ors_attrs = slap_anlist_no_attrs;
498 op->ors_attrsonly = 1;
502 op->o_bd->be_search( op, &rs );
503 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
508 op->o_tag = LDAP_REQ_DELETE;
509 op->o_callback = &nullsc;
511 for (i=0; i<pd.used; i++) {
512 op->o_req_dn = pd.dn[i];
513 op->o_req_ndn = pd.ndn[i];
514 op->o_bd->be_delete( op, &rs );
515 ch_free( pd.ndn[i].bv_val );
516 ch_free( pd.dn[i].bv_val );
522 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
523 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
524 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
530 log_cf_gen(ConfigArgs *c)
532 slap_overinst *on = (slap_overinst *)c->bi;
533 struct log_info *li = on->on_bi.bi_private;
535 slap_mask_t tmask = 0;
536 char agebuf[2*STRLENOF("dd+hh:mm:ss ")];
537 struct berval agebv, cyclebv;
540 case SLAP_CONFIG_EMIT:
543 value_add( &c->rvalue_vals, li->li_db->be_suffix );
544 value_add( &c->rvalue_nvals, li->li_db->be_nsuffix );
547 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
550 agebv.bv_val = agebuf;
551 log_age_unparse( li->li_age, &agebv );
552 agebv.bv_val[agebv.bv_len] = ' ';
554 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
555 log_age_unparse( li->li_cycle, &cyclebv );
556 agebv.bv_len += cyclebv.bv_len;
557 value_add_one( &c->rvalue_vals, &agebv );
561 case LDAP_MOD_DELETE:
564 /* noop. this should always be a valid backend. */
570 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
572 li->li_ops &= ~tmask;
577 struct re_s *re = li->li_task;
579 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
580 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
581 ldap_pvt_runqueue_remove( &slapd_rq, re );
591 li->li_db = select_backend( &c->value_ndn, 0, 0 );
593 sprintf( c->msg, "<%s> no matching backend found for suffix",
595 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
596 c->log, c->msg, c->value_dn.bv_val );
599 ch_free( c->value_dn.bv_val );
600 ch_free( c->value_ndn.bv_val );
603 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
608 li->li_age = log_age_parse( c->argv[1] );
609 if ( li->li_age == -1 ) {
612 li->li_cycle = log_age_parse( c->argv[2] );
613 if ( li->li_cycle == -1 ) {
615 } else if ( slapMode & SLAP_SERVER_MODE ) {
616 struct re_s *re = li->li_task;
618 re->interval.tv_sec = li->li_cycle;
620 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
621 li->li_cycle, accesslog_purge, li,
622 "accesslog_purge", li->li_db ?
623 li->li_db->be_suffix[0].bv_val :
624 c->be->be_suffix[0].bv_val );
634 static Entry *accesslog_entry( Operation *op, int logop ) {
635 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
636 log_info *li = on->on_bi.bi_private;
638 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
639 struct berval rdn, timestamp, bv;
640 slap_verbmasks *lo = logops+logop+EN_OFFSET;
642 Entry *e = ch_calloc( 1, sizeof(Entry) );
644 strcpy( rdnbuf, RDNEQ );
647 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
648 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
649 slap_timestamp( &op->o_time, ×tamp );
651 sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
652 timestamp.bv_len += 7;
654 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
655 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
656 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &rdn, NULL );
658 attr_merge_one( e, slap_schema.si_ad_objectClass,
659 &log_ocs[logop]->soc_cname, NULL );
660 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
661 &log_ocs[logop]->soc_cname, NULL );
662 attr_merge_one( e, ad_reqStart, ×tamp, NULL );
664 /* Exops have OID appended */
665 if ( logop == LOG_EN_EXTENDED ) {
666 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
667 bv.bv_val = ch_malloc( bv.bv_len + 1 );
668 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
669 bv.bv_val[lo->word.bv_len] = '{';
670 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
671 op->ore_reqoid.bv_len );
672 bv.bv_val[bv.bv_len-1] = '}';
673 bv.bv_val[bv.bv_len] = '\0';
674 attr_merge_one( e, ad_reqType, &bv, NULL );
676 attr_merge_one( e, ad_reqType, &lo->word, NULL );
679 rdn.bv_len = sprintf( rdn.bv_val, "%lu", op->o_connid );
680 attr_merge_one( e, ad_reqSession, &rdn, NULL );
682 if ( BER_BVISNULL( &op->o_dn ))
683 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
684 (struct berval *)&slap_empty_bv );
686 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
688 /* FIXME: need to add reqControls and reqRespControls */
693 static struct berval scopes[] = {
697 BER_BVC("subordinate")
700 static struct berval simple = BER_BVC("SIMPLE");
702 static int accesslog_response(Operation *op, SlapReply *rs) {
703 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
704 log_info *li = on->on_bi.bi_private;
705 Attribute *a, *last_attr;
713 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
718 SlapReply rs2 = {REP_RESULT};
720 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
721 return SLAP_CB_CONTINUE;
723 switch ( op->o_tag ) {
724 case LDAP_REQ_ADD: logop = LOG_EN_ADD; break;
725 case LDAP_REQ_DELETE: logop = LOG_EN_DELETE; break;
726 case LDAP_REQ_MODIFY: logop = LOG_EN_MODIFY; break;
727 case LDAP_REQ_MODRDN: logop = LOG_EN_MODRDN; break;
728 case LDAP_REQ_COMPARE: logop = LOG_EN_COMPARE; break;
729 case LDAP_REQ_SEARCH: logop = LOG_EN_SEARCH; break;
730 case LDAP_REQ_BIND: logop = LOG_EN_BIND; break;
731 case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
732 default: /* unknown operation type */
733 logop = LOG_EN_UNKNOWN; break;
734 } /* Unbind and Abandon never reach here */
736 lo = logops+logop+EN_OFFSET;
737 if ( !( li->li_ops & lo->mask ))
738 return SLAP_CB_CONTINUE;
740 endtime = slap_get_time();
742 e = accesslog_entry( op, logop );
745 bv.bv_len = sizeof(timebuf);
746 slap_timestamp( &endtime, &bv );
748 attr_merge_one( e, ad_reqEnd, &bv, NULL );
750 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
753 ber_str2bv( rs->sr_text, 0, 0, &bv );
754 attr_merge_one( e, ad_reqMessage, &bv, NULL );
756 bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
759 attr_merge_one( e, ad_reqResult, &bv, NULL );
761 last_attr = attr_find( e->e_attrs, ad_reqResult );
765 /* count all the vals */
767 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
769 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
774 vals = ch_malloc( (i+1) * sizeof( struct berval ));
776 for ( a=op->ora_e->e_attrs; a; a=a->a_next ) {
778 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
779 vals[i].bv_len = a->a_desc->ad_cname.bv_len + b->bv_len +3;
780 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
781 ptr = lutil_strcopy( vals[i].bv_val,
782 a->a_desc->ad_cname.bv_val );
786 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
787 vals[i].bv_val[vals[i].bv_len] = '\0';
791 vals[i].bv_val = NULL;
793 a = attr_alloc( ad_reqMod );
796 last_attr->a_next = a;
800 /* needs nothing else */
804 /* count all the mods */
806 for ( m=op->orm_modlist; m; m=m->sml_next ) {
807 if ( m->sml_values ) {
808 for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
811 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
815 vals = ch_malloc( (i+1) * sizeof( struct berval ));
817 for ( m=op->orm_modlist; m; m=m->sml_next ) {
818 if ( m->sml_values ) {
819 for (b=m->sml_values; !BER_BVISNULL( b ); b++,i++) {
821 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + b->bv_len +3;
822 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
823 ptr = lutil_strcopy( vals[i].bv_val,
824 m->sml_desc->ad_cname.bv_val );
826 switch( m->sml_op ) {
827 case LDAP_MOD_ADD: c_op = '+'; break;
828 case LDAP_MOD_DELETE: c_op = '-'; break;
829 case LDAP_MOD_REPLACE: c_op = '='; break;
830 case LDAP_MOD_INCREMENT: c_op = '#'; break;
832 /* unknown op. there shouldn't be any of these. we
833 * don't know what to do with it, but we shouldn't just
836 default: c_op = '?'; break;
840 AC_MEMCPY( ptr, b->bv_val, b->bv_len );
841 vals[i].bv_val[vals[i].bv_len] = '\0';
843 } else if ( m->sml_op == LDAP_MOD_DELETE ) {
844 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
845 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
846 ptr = lutil_strcopy( vals[i].bv_val,
847 m->sml_desc->ad_cname.bv_val );
854 vals[i].bv_val = NULL;
856 a = attr_alloc( ad_reqMod );
859 last_attr->a_next = a;
863 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
864 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
865 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
867 if ( op->orr_newSup ) {
868 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
873 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
874 op->orc_ava->aa_value.bv_len;
875 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
876 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
878 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
879 bv.bv_val[bv.bv_len] = '\0';
880 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
881 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
885 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
886 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
887 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
889 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
890 attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
891 if ( op->ors_attrs ) {
893 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
895 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
897 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
898 vals[i] = op->ors_attrs[i].an_name;
899 vals[i].bv_val = NULL;
901 attr_merge( e, ad_reqAttr, vals, NULL );
902 op->o_tmpfree( vals, op->o_tmpmemctx );
905 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
906 attr_merge_one( e, ad_reqEntries, &bv, NULL );
908 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
909 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
910 /* FIXME: slimit was zeroed by the backends */
914 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
915 attr_merge_one( e, ad_reqMethod, &simple, NULL );
917 bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
918 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
919 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
920 ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
923 attr_merge_one( e, ad_reqMethod, &bv, NULL );
924 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
928 case LOG_EN_EXTENDED:
929 if ( op->ore_reqdata ) {
930 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
935 /* we don't know its parameters, don't add any */
939 op2.o_hdr = op->o_hdr;
940 op2.o_tag = LDAP_REQ_ADD;
941 op2.o_time = endtime;
943 op2.o_bd = li->li_db;
944 op2.o_dn = li->li_db->be_rootdn;
945 op2.o_ndn = li->li_db->be_rootndn;
946 op2.o_req_dn = e->e_name;
947 op2.o_req_ndn = e->e_nname;
949 op2.o_callback = &nullsc;
951 op2.o_bd->be_add( &op2, &rs2 );
954 return SLAP_CB_CONTINUE;
957 /* unbinds are broadcast to all backends; we only log it if this
958 * backend was used for the original bind.
961 accesslog_unbind( Operation *op, SlapReply *rs )
963 if ( op->o_conn->c_authz_backend == op->o_bd ) {
964 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
965 log_info *li = on->on_bi.bi_private;
967 SlapReply rs2 = {REP_RESULT};
970 e = accesslog_entry( op, LOG_EN_UNBIND );
971 op2.o_hdr = op->o_hdr;
972 op2.o_tag = LDAP_REQ_ADD;
973 op2.o_time = op->o_time;
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;
981 op2.o_callback = &nullsc;
983 op2.o_bd->be_add( &op2, &rs2 );
986 return SLAP_CB_CONTINUE;
990 accesslog_abandon( Operation *op, SlapReply *rs )
992 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
993 log_info *li = on->on_bi.bi_private;
995 SlapReply rs2 = {REP_RESULT};
1001 return SLAP_CB_CONTINUE;
1003 e = accesslog_entry( op, LOG_EN_ABANDON );
1005 bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
1006 attr_merge_one( e, ad_reqId, &bv, NULL );
1008 op2.o_hdr = op->o_hdr;
1009 op2.o_tag = LDAP_REQ_ADD;
1010 op2.o_time = op->o_time;
1012 op2.o_bd = li->li_db;
1013 op2.o_dn = li->li_db->be_rootdn;
1014 op2.o_ndn = li->li_db->be_rootndn;
1015 op2.o_req_dn = e->e_name;
1016 op2.o_req_ndn = e->e_nname;
1018 op2.o_callback = &nullsc;
1020 op2.o_bd->be_add( &op2, &rs2 );
1023 return SLAP_CB_CONTINUE;
1026 static slap_overinst accesslog;
1033 slap_overinst *on = (slap_overinst *)be->bd_info;
1034 log_info *li = ch_calloc(1, sizeof(log_info));
1036 on->on_bi.bi_private = li;
1041 accesslog_db_destroy(
1045 slap_overinst *on = (slap_overinst *)be->bd_info;
1046 log_info *li = on->on_bi.bi_private;
1049 return LDAP_SUCCESS;
1052 int accesslog_init()
1056 accesslog.on_bi.bi_type = "accesslog";
1057 accesslog.on_bi.bi_db_init = accesslog_db_init;
1058 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1060 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1061 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1062 accesslog.on_response = accesslog_response;
1064 accesslog.on_bi.bi_cf_ocs = log_cfocs;
1066 nullsc.sc_response = slap_null_cb;
1068 rc = config_register_schema( log_cfats, log_cfocs );
1069 if ( rc ) return rc;
1071 /* log schema integration */
1072 for ( i=0; lattrs[i].at; i++ ) {
1073 LDAPAttributeType *lat;
1078 lat = ldap_str2attributetype( lattrs[i].at, &code, &err,
1079 LDAP_SCHEMA_ALLOW_ALL );
1081 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1082 "ldap_str2attributetype failed on %d: %s, %s\n",
1083 i, ldap_scherr2str(code), err );
1086 code = at_add( lat, 0, &at, &err );
1087 ldap_memfree( lat );
1089 Debug( LDAP_DEBUG_ANY, "log_back_initialize: "
1090 "at_add failed on %d: %s\n",
1091 i, scherr2str(code), 0 );
1094 if ( slap_bv2ad( &at->sat_cname, lattrs[i].ad, &err )) {
1095 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1096 "slap_bv2ad failed on %d: %s\n",
1101 for ( i=0; locs[i].ot; i++ ) {
1102 LDAPObjectClass *loc;
1107 loc = ldap_str2objectclass( locs[i].ot, &code, &err,
1108 LDAP_SCHEMA_ALLOW_ALL );
1110 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1111 "ldap_str2objectclass failed on %d: %s, %s\n",
1112 i, ldap_scherr2str(code), err );
1116 code = oc_add( loc, 0, &oc, &err );
1117 ldap_memfree( loc );
1119 Debug( LDAP_DEBUG_ANY, "accesslog_init: "
1120 "oc_add failed on %d: %s\n",
1121 i, scherr2str(code), 0 );
1128 return overlay_register(&accesslog);
1131 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1132 int init_module( int argc, char *argv[]) {
1133 return accesslog_init();
1137 #endif /* SLAPD_OVER_ACCESSLOG */