1 /* accesslog.c - log operations for audit/history purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2007 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_attr {
55 struct log_attr *next;
56 AttributeDescription *attr;
59 typedef struct log_info {
61 struct berval li_db_suffix;
68 log_attr *li_oldattrs;
70 ldap_pvt_thread_rmutex_t li_op_rmutex;
71 ldap_pvt_thread_mutex_t li_log_mutex;
74 static ConfigDriver log_cf_gen;
85 static ConfigTable log_cfats[] = {
86 { "logdb", "suffix", 2, 2, 0, ARG_DN|ARG_MAGIC|LOG_DB,
87 log_cf_gen, "( OLcfgOvAt:4.1 NAME 'olcAccessLogDB' "
88 "DESC 'Suffix of database for log content' "
89 "SUP distinguishedName SINGLE-VALUE )", NULL, NULL },
90 { "logops", "op|writes|reads|session|all", 2, 0, 0,
92 log_cf_gen, "( OLcfgOvAt:4.2 NAME 'olcAccessLogOps' "
93 "DESC 'Operation types to log' "
94 "EQUALITY caseIgnoreMatch "
95 "SYNTAX OMsDirectoryString )", NULL, NULL },
96 { "logpurge", "age> <interval", 3, 3, 0, ARG_MAGIC|LOG_PURGE,
97 log_cf_gen, "( OLcfgOvAt:4.3 NAME 'olcAccessLogPurge' "
98 "DESC 'Log cleanup parameters' "
99 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
100 { "logsuccess", NULL, 2, 2, 0, ARG_MAGIC|ARG_ON_OFF|LOG_SUCCESS,
101 log_cf_gen, "( OLcfgOvAt:4.4 NAME 'olcAccessLogSuccess' "
102 "DESC 'Log successful ops only' "
103 "SYNTAX OMsBoolean SINGLE-VALUE )", NULL, NULL },
104 { "logold", "filter", 2, 2, 0, ARG_MAGIC|LOG_OLD,
105 log_cf_gen, "( OLcfgOvAt:4.5 NAME 'olcAccessLogOld' "
106 "DESC 'Log old values when modifying entries matching the filter' "
107 "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
108 { "logoldattr", "attrs", 2, 0, 0, ARG_MAGIC|LOG_OLDATTR,
109 log_cf_gen, "( OLcfgOvAt:4.6 NAME 'olcAccessLogOldAttr' "
110 "DESC 'Log old values of these attributes even if unmodified' "
111 "EQUALITY caseIgnoreMatch "
112 "SYNTAX OMsDirectoryString )", NULL, NULL },
116 static ConfigOCs log_cfocs[] = {
118 "NAME 'olcAccessLogConfig' "
119 "DESC 'Access log configuration' "
120 "SUP olcOverlayConfig "
121 "MUST olcAccessLogDB "
122 "MAY ( olcAccessLogOps $ olcAccessLogPurge $ olcAccessLogSuccess $ "
123 "olcAccessLogOld $ olcAccessLogOldAttr ) )",
124 Cft_Overlay, log_cfats },
128 static slap_verbmasks logops[] = {
129 { BER_BVC("all"), LOG_OP_ALL },
130 { BER_BVC("writes"), LOG_OP_WRITES },
131 { BER_BVC("session"), LOG_OP_SESSION },
132 { BER_BVC("reads"), LOG_OP_READS },
133 { BER_BVC("add"), LOG_OP_ADD },
134 { BER_BVC("delete"), LOG_OP_DELETE },
135 { BER_BVC("modify"), LOG_OP_MODIFY },
136 { BER_BVC("modrdn"), LOG_OP_MODRDN },
137 { BER_BVC("compare"), LOG_OP_COMPARE },
138 { BER_BVC("search"), LOG_OP_SEARCH },
139 { BER_BVC("bind"), LOG_OP_BIND },
140 { BER_BVC("unbind"), LOG_OP_UNBIND },
141 { BER_BVC("abandon"), LOG_OP_ABANDON },
142 { BER_BVC("extended"), LOG_OP_EXTENDED },
143 { BER_BVC("unknown"), LOG_OP_UNKNOWN },
147 /* Start with "add" in logops */
165 static ObjectClass *log_ocs[LOG_EN__COUNT], *log_container;
167 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
169 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
170 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
172 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
173 *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
174 *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
175 *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
176 *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
177 *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
178 *ad_reqId, *ad_reqMessage, *ad_reqVersion, *ad_reqDerefAliases,
179 *ad_reqReferral, *ad_reqOld, *ad_auditContext;
183 AttributeDescription **ad;
185 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
186 "DESC 'Target DN of request' "
187 "EQUALITY distinguishedNameMatch "
189 "SINGLE-VALUE )", &ad_reqDN },
190 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
191 "DESC 'Start time of request' "
192 "EQUALITY generalizedTimeMatch "
193 "ORDERING generalizedTimeOrderingMatch "
194 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
195 "SINGLE-VALUE )", &ad_reqStart },
196 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
197 "DESC 'End time of request' "
198 "EQUALITY generalizedTimeMatch "
199 "ORDERING generalizedTimeOrderingMatch "
200 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
201 "SINGLE-VALUE )", &ad_reqEnd },
202 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
203 "DESC 'Type of request' "
204 "EQUALITY caseIgnoreMatch "
205 "SYNTAX OMsDirectoryString "
206 "SINGLE-VALUE )", &ad_reqType },
207 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
208 "DESC 'Session ID of request' "
209 "EQUALITY caseIgnoreMatch "
210 "SYNTAX OMsDirectoryString "
211 "SINGLE-VALUE )", &ad_reqSession },
212 { "( " LOG_SCHEMA_AT ".6 NAME 'reqAuthzID' "
213 "DESC 'Authorization ID of requestor' "
214 "EQUALITY distinguishedNameMatch "
216 "SINGLE-VALUE )", &ad_reqAuthzID },
217 { "( " LOG_SCHEMA_AT ".7 NAME 'reqResult' "
218 "DESC 'Result code of request' "
219 "EQUALITY integerMatch "
220 "ORDERING integerOrderingMatch "
222 "SINGLE-VALUE )", &ad_reqResult },
223 { "( " LOG_SCHEMA_AT ".8 NAME 'reqMessage' "
224 "DESC 'Error text of request' "
225 "EQUALITY caseIgnoreMatch "
226 "SUBSTR caseIgnoreSubstringsMatch "
227 "SYNTAX OMsDirectoryString "
228 "SINGLE-VALUE )", &ad_reqMessage },
229 { "( " LOG_SCHEMA_AT ".9 NAME 'reqReferral' "
230 "DESC 'Referrals returned for request' "
231 "SUP labeledURI )", &ad_reqReferral },
232 { "( " LOG_SCHEMA_AT ".10 NAME 'reqControls' "
233 "DESC 'Request controls' "
234 "SYNTAX OMsOctetString )", &ad_reqControls },
235 { "( " LOG_SCHEMA_AT ".11 NAME 'reqRespControls' "
236 "DESC 'Response controls of request' "
237 "SYNTAX OMsOctetString )", &ad_reqRespControls },
238 { "( " LOG_SCHEMA_AT ".12 NAME 'reqId' "
239 "DESC 'ID of Request to Abandon' "
240 "EQUALITY integerMatch "
241 "ORDERING integerOrderingMatch "
243 "SINGLE-VALUE )", &ad_reqId },
244 { "( " LOG_SCHEMA_AT ".13 NAME 'reqVersion' "
245 "DESC 'Protocol version of Bind request' "
246 "EQUALITY integerMatch "
247 "ORDERING integerOrderingMatch "
249 "SINGLE-VALUE )", &ad_reqVersion },
250 { "( " LOG_SCHEMA_AT ".14 NAME 'reqMethod' "
251 "DESC 'Bind method of request' "
252 "EQUALITY caseIgnoreMatch "
253 "SYNTAX OMsDirectoryString "
254 "SINGLE-VALUE )", &ad_reqMethod },
255 { "( " LOG_SCHEMA_AT ".15 NAME 'reqAssertion' "
256 "DESC 'Compare Assertion of request' "
257 "SYNTAX OMsDirectoryString "
258 "SINGLE-VALUE )", &ad_reqAssertion },
259 { "( " LOG_SCHEMA_AT ".16 NAME 'reqMod' "
260 "DESC 'Modifications of request' "
261 "EQUALITY octetStringMatch "
262 "SUBSTR octetStringSubstringsMatch "
263 "SYNTAX OMsOctetString )", &ad_reqMod },
264 { "( " LOG_SCHEMA_AT ".17 NAME 'reqOld' "
265 "DESC 'Old values of entry before request completed' "
266 "EQUALITY octetStringMatch "
267 "SUBSTR octetStringSubstringsMatch "
268 "SYNTAX OMsOctetString )", &ad_reqOld },
269 { "( " LOG_SCHEMA_AT ".18 NAME 'reqNewRDN' "
270 "DESC 'New RDN of request' "
271 "EQUALITY distinguishedNameMatch "
273 "SINGLE-VALUE )", &ad_reqNewRDN },
274 { "( " LOG_SCHEMA_AT ".19 NAME 'reqDeleteOldRDN' "
275 "DESC 'Delete old RDN' "
276 "EQUALITY booleanMatch "
278 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
279 { "( " LOG_SCHEMA_AT ".20 NAME 'reqNewSuperior' "
280 "DESC 'New superior DN of request' "
281 "EQUALITY distinguishedNameMatch "
283 "SINGLE-VALUE )", &ad_reqNewSuperior },
284 { "( " LOG_SCHEMA_AT ".21 NAME 'reqScope' "
285 "DESC 'Scope of request' "
286 "EQUALITY caseIgnoreMatch "
287 "SYNTAX OMsDirectoryString "
288 "SINGLE-VALUE )", &ad_reqScope },
289 { "( " LOG_SCHEMA_AT ".22 NAME 'reqDerefAliases' "
290 "DESC 'Disposition of Aliases in request' "
291 "EQUALITY caseIgnoreMatch "
292 "SYNTAX OMsDirectoryString "
293 "SINGLE-VALUE )", &ad_reqDerefAliases },
294 { "( " LOG_SCHEMA_AT ".23 NAME 'reqAttrsOnly' "
295 "DESC 'Attributes and values of request' "
296 "EQUALITY booleanMatch "
298 "SINGLE-VALUE )", &ad_reqAttrsOnly },
299 { "( " LOG_SCHEMA_AT ".24 NAME 'reqFilter' "
300 "DESC 'Filter of request' "
301 "EQUALITY caseIgnoreMatch "
302 "SUBSTR caseIgnoreSubstringsMatch "
303 "SYNTAX OMsDirectoryString "
304 "SINGLE-VALUE )", &ad_reqFilter },
305 { "( " LOG_SCHEMA_AT ".25 NAME 'reqAttr' "
306 "DESC 'Attributes of request' "
307 "EQUALITY caseIgnoreMatch "
308 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
309 { "( " LOG_SCHEMA_AT ".26 NAME 'reqSizeLimit' "
310 "DESC 'Size limit of request' "
311 "EQUALITY integerMatch "
312 "ORDERING integerOrderingMatch "
314 "SINGLE-VALUE )", &ad_reqSizeLimit },
315 { "( " LOG_SCHEMA_AT ".27 NAME 'reqTimeLimit' "
316 "DESC 'Time limit of request' "
317 "EQUALITY integerMatch "
318 "ORDERING integerOrderingMatch "
320 "SINGLE-VALUE )", &ad_reqTimeLimit },
321 { "( " LOG_SCHEMA_AT ".28 NAME 'reqEntries' "
322 "DESC 'Number of entries returned' "
323 "EQUALITY integerMatch "
324 "ORDERING integerOrderingMatch "
326 "SINGLE-VALUE )", &ad_reqEntries },
327 { "( " LOG_SCHEMA_AT ".29 NAME 'reqData' "
328 "DESC 'Data of extended request' "
329 "EQUALITY octetStringMatch "
330 "SUBSTR octetStringSubstringsMatch "
331 "SYNTAX OMsOctetString "
332 "SINGLE-VALUE )", &ad_reqData },
335 * from <draft-chu-ldap-logschema-01.txt>:
338 ( LOG_SCHEMA_AT .30 NAME 'auditContext'
339 DESC 'DN of auditContainer'
340 EQUALITY distinguishedNameMatch
341 SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
342 SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )
344 * - removed EQUALITY matchingRule
345 * - changed directoryOperation in dSAOperation
347 { "( " LOG_SCHEMA_AT ".30 NAME 'auditContext' "
348 "DESC 'DN of auditContainer' "
349 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
351 "NO-USER-MODIFICATION "
352 "USAGE dSAOperation )", &ad_auditContext },
360 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
361 "DESC 'AuditLog container' "
362 "SUP top STRUCTURAL "
363 "MAY ( cn $ reqStart $ reqEnd ) )", &log_container },
364 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
365 "DESC 'OpenLDAP request auditing' "
366 "SUP top STRUCTURAL "
367 "MUST ( reqStart $ reqType $ reqSession ) "
368 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
369 "reqResult $ reqMessage $ reqReferral ) )",
370 &log_ocs[LOG_EN_UNBIND] },
371 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
372 "DESC 'OpenLDAP read request record' "
373 "SUP auditObject STRUCTURAL )", NULL },
374 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
375 "DESC 'OpenLDAP write request record' "
376 "SUP auditObject STRUCTURAL )", NULL },
377 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
378 "DESC 'Abandon operation' "
379 "SUP auditObject STRUCTURAL "
380 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
381 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
382 "DESC 'Add operation' "
383 "SUP auditWriteObject STRUCTURAL "
384 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
385 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
386 "DESC 'Bind operation' "
387 "SUP auditObject STRUCTURAL "
388 "MUST ( reqVersion $ reqMethod ) )", &log_ocs[LOG_EN_BIND] },
389 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
390 "DESC 'Compare operation' "
391 "SUP auditReadObject STRUCTURAL "
392 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
393 { "( " LOG_SCHEMA_OC ".8 NAME 'auditDelete' "
394 "DESC 'Delete operation' "
395 "SUP auditWriteObject STRUCTURAL "
396 "MAY reqOld )", &log_ocs[LOG_EN_DELETE] },
397 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModify' "
398 "DESC 'Modify operation' "
399 "SUP auditWriteObject STRUCTURAL "
400 "MAY reqOld MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
401 { "( " LOG_SCHEMA_OC ".10 NAME 'auditModRDN' "
402 "DESC 'ModRDN operation' "
403 "SUP auditWriteObject STRUCTURAL "
404 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
405 "MAY ( reqNewSuperior $ reqOld ) )", &log_ocs[LOG_EN_MODRDN] },
406 { "( " LOG_SCHEMA_OC ".11 NAME 'auditSearch' "
407 "DESC 'Search operation' "
408 "SUP auditReadObject STRUCTURAL "
409 "MUST ( reqScope $ reqDerefAliases $ reqAttrsonly ) "
410 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
411 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
412 { "( " LOG_SCHEMA_OC ".12 NAME 'auditExtended' "
413 "DESC 'Extended operation' "
414 "SUP auditObject STRUCTURAL "
415 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
419 #define RDNEQ "reqStart="
421 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
422 * If a field is present, it must be two digits. (Except for
423 * days, which can be arbitrary width.)
426 log_age_parse(char *agestr)
432 t1 = strtol( agestr, &endptr, 10 );
433 /* Is there a days delimiter? */
434 if ( *endptr == '+' ) {
435 /* 32 bit time only covers about 68 years */
436 if ( t1 < 0 || t1 > 25000 )
442 if ( agestr[2] != ':' ) {
443 /* No valid delimiter found, fail */
454 /* if there's a delimiter, it can only be a colon */
455 if ( agestr[2] != ':' )
458 /* If we're at the end of the string, and we started with days,
459 * fail because we expected to find minutes too.
461 return gotdays ? -1 : t1 * 60;
467 /* last field can only be seconds */
468 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
478 t1 += atoi( agestr );
479 } else if ( gotdays ) {
480 /* only got days+hh:mm */
487 log_age_unparse( int age, struct berval *agebv )
503 ptr += sprintf( ptr, "%d+", dd );
504 ptr += sprintf( ptr, "%02d:%02d", hh, mm );
506 ptr += sprintf( ptr, ":%02d", ss );
508 agebv->bv_len = ptr - agebv->bv_val;
511 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
513 #define PURGE_INCREMENT 100
515 typedef struct purge_data {
520 struct berval csn; /* an arbitrary old CSN */
524 log_old_lookup( Operation *op, SlapReply *rs )
526 purge_data *pd = op->o_callback->sc_private;
528 if ( rs->sr_type != REP_SEARCH) return 0;
530 if ( slapd_shutdown ) return 0;
532 /* Remember old CSN */
533 if ( pd->csn.bv_val[0] == '\0' ) {
534 Attribute *a = attr_find( rs->sr_entry->e_attrs,
535 slap_schema.si_ad_entryCSN );
537 int len = a->a_vals[0].bv_len;
538 if ( len > pd->csn.bv_len )
539 len = pd->csn.bv_len;
540 AC_MEMCPY( pd->csn.bv_val, a->a_vals[0].bv_val, len );
541 pd->csn.bv_len = len;
544 if ( pd->used >= pd->slots ) {
545 pd->slots += PURGE_INCREMENT;
546 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
547 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
549 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
550 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
555 /* Periodically search for old entries in the log database and delete them */
557 accesslog_purge( void *ctx, void *arg )
559 struct re_s *rtask = arg;
560 struct log_info *li = rtask->arg;
562 Connection conn = {0};
563 OperationBuffer opbuf;
564 Operation *op = (Operation *) &opbuf;
565 SlapReply rs = {REP_RESULT};
566 slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
568 AttributeAssertion ava = {0};
570 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
571 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
572 time_t old = slap_get_time();
574 connection_fake_init( &conn, op, ctx );
576 f.f_choice = LDAP_FILTER_LE;
580 ava.aa_desc = ad_reqStart;
581 ava.aa_value.bv_val = timebuf;
582 ava.aa_value.bv_len = sizeof(timebuf);
585 slap_timestamp( &old, &ava.aa_value );
587 op->o_tag = LDAP_REQ_SEARCH;
588 op->o_bd = li->li_db;
589 op->o_dn = li->li_db->be_rootdn;
590 op->o_ndn = li->li_db->be_rootndn;
591 op->o_req_dn = li->li_db->be_suffix[0];
592 op->o_req_ndn = li->li_db->be_nsuffix[0];
593 op->o_callback = &cb;
594 op->ors_scope = LDAP_SCOPE_ONELEVEL;
595 op->ors_deref = LDAP_DEREF_NEVER;
596 op->ors_tlimit = SLAP_NO_LIMIT;
597 op->ors_slimit = SLAP_NO_LIMIT;
599 filter2bv_x( op, &f, &op->ors_filterstr );
600 op->ors_attrs = slap_anlist_no_attrs;
601 op->ors_attrsonly = 1;
603 pd.csn.bv_len = sizeof( csnbuf );
604 pd.csn.bv_val = csnbuf;
608 op->o_bd->be_search( op, &rs );
609 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
614 op->o_tag = LDAP_REQ_DELETE;
615 op->o_callback = &nullsc;
618 for (i=0; i<pd.used; i++) {
619 op->o_req_dn = pd.dn[i];
620 op->o_req_ndn = pd.ndn[i];
621 if ( !slapd_shutdown )
622 op->o_bd->be_delete( op, &rs );
623 ch_free( pd.ndn[i].bv_val );
624 ch_free( pd.dn[i].bv_val );
630 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
631 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
632 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
638 log_cf_gen(ConfigArgs *c)
640 slap_overinst *on = (slap_overinst *)c->bi;
641 struct log_info *li = on->on_bi.bi_private;
643 slap_mask_t tmask = 0;
644 char agebuf[2*STRLENOF("ddddd+hh:mm:ss ")];
645 struct berval agebv, cyclebv;
648 case SLAP_CONFIG_EMIT:
651 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
652 value_add_one( &c->rvalue_vals, &li->li_db_suffix );
653 value_add_one( &c->rvalue_nvals, &li->li_db_suffix );
654 } else if ( li->li_db ) {
655 value_add_one( &c->rvalue_vals, li->li_db->be_suffix );
656 value_add_one( &c->rvalue_nvals, li->li_db->be_nsuffix );
658 snprintf( c->msg, sizeof( c->msg ),
659 "accesslog: \"logdb <suffix>\" must be specified" );
660 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
661 c->log, c->msg, c->value_dn.bv_val );
667 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
674 agebv.bv_val = agebuf;
675 log_age_unparse( li->li_age, &agebv );
676 agebv.bv_val[agebv.bv_len] = ' ';
678 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
679 log_age_unparse( li->li_cycle, &cyclebv );
680 agebv.bv_len += cyclebv.bv_len;
681 value_add_one( &c->rvalue_vals, &agebv );
684 if ( li->li_success )
685 c->value_int = li->li_success;
691 filter2bv( li->li_oldf, &agebv );
692 ber_bvarray_add( &c->rvalue_vals, &agebv );
698 if ( li->li_oldattrs ) {
701 for ( la = li->li_oldattrs; la; la=la->next )
702 value_add_one( &c->rvalue_vals, &la->attr->ad_cname );
709 case LDAP_MOD_DELETE:
712 /* noop. this should always be a valid backend. */
718 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
720 li->li_ops &= ~tmask;
725 struct re_s *re = li->li_task;
727 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
728 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
729 ldap_pvt_runqueue_remove( &slapd_rq, re );
739 filter_free( li->li_oldf );
747 for ( la = li->li_oldattrs; la; la = ln ) {
752 log_attr *la = NULL, **lp;
755 for ( lp = &li->li_oldattrs, i=0; i < c->valx; i++ ) {
768 if ( CONFIG_ONLINE_ADD( c )) {
769 li->li_db = select_backend( &c->value_ndn, 0, 0 );
771 snprintf( c->msg, sizeof( c->msg ),
772 "<%s> no matching backend found for suffix",
774 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
775 c->log, c->msg, c->value_dn.bv_val );
778 ch_free( c->value_ndn.bv_val );
780 li->li_db_suffix = c->value_ndn;
782 ch_free( c->value_dn.bv_val );
785 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
790 li->li_age = log_age_parse( c->argv[1] );
791 if ( li->li_age < 1 ) {
794 li->li_cycle = log_age_parse( c->argv[2] );
795 if ( li->li_cycle < 1 ) {
797 } else if ( slapMode & SLAP_SERVER_MODE ) {
798 struct re_s *re = li->li_task;
800 re->interval.tv_sec = li->li_cycle;
802 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
803 li->li_cycle, accesslog_purge, li,
804 "accesslog_purge", li->li_db ?
805 li->li_db->be_suffix[0].bv_val :
806 c->be->be_suffix[0].bv_val );
811 li->li_success = c->value_int;
814 li->li_oldf = str2filter( c->argv[1] );
815 if ( !li->li_oldf ) {
816 sprintf( c->msg, "bad filter!" );
822 AttributeDescription *ad;
825 for ( i=1; i< c->argc; i++ ) {
827 if ( slap_str2ad( c->argv[i], &ad, &text ) == LDAP_SUCCESS ) {
828 log_attr *la = ch_malloc( sizeof( log_attr ));
830 la->next = li->li_oldattrs;
831 li->li_oldattrs = la;
833 snprintf( c->msg, sizeof( c->msg ), "%s <%s>: %s",
834 c->argv[0], c->argv[i], text );
835 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
836 "%s: %s\n", c->log, c->msg, 0 );
849 static Entry *accesslog_entry( Operation *op, int logop,
851 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
852 log_info *li = on->on_bi.bi_private;
854 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
855 char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
857 struct berval rdn, nrdn, timestamp, ntimestamp, bv;
858 slap_verbmasks *lo = logops+logop+EN_OFFSET;
860 Entry *e = entry_alloc();
862 strcpy( rdnbuf, RDNEQ );
864 strcpy( nrdnbuf, RDNEQ );
865 nrdn.bv_val = nrdnbuf;
867 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
868 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
869 slap_timestamp( &op->o_time, ×tamp );
870 sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op->o_tincr );
871 timestamp.bv_len += 7;
873 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
874 ad_reqStart->ad_type->sat_equality->smr_normalize(
875 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
876 ad_reqStart->ad_type->sat_equality, ×tamp, &ntimestamp,
879 strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
880 nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
881 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
882 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
884 attr_merge_one( e, slap_schema.si_ad_objectClass,
885 &log_ocs[logop]->soc_cname, NULL );
886 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
887 &log_ocs[logop]->soc_cname, NULL );
888 attr_merge_one( e, ad_reqStart, ×tamp, &ntimestamp );
889 op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
891 slap_op_time( &op2->o_time, &op2->o_tincr );
893 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
894 slap_timestamp( &op2->o_time, ×tamp );
895 sprintf( timestamp.bv_val + timestamp.bv_len-1, ".%06dZ", op2->o_tincr );
896 timestamp.bv_len += 7;
898 attr_merge_normalize_one( e, ad_reqEnd, ×tamp, op->o_tmpmemctx );
900 /* Exops have OID appended */
901 if ( logop == LOG_EN_EXTENDED ) {
902 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
903 bv.bv_val = ch_malloc( bv.bv_len + 1 );
904 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
905 bv.bv_val[lo->word.bv_len] = '{';
906 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
907 op->ore_reqoid.bv_len );
908 bv.bv_val[bv.bv_len-1] = '}';
909 bv.bv_val[bv.bv_len] = '\0';
910 attr_merge_one( e, ad_reqType, &bv, NULL );
912 attr_merge_one( e, ad_reqType, &lo->word, NULL );
915 rdn.bv_len = sprintf( rdn.bv_val, "%lu", op->o_connid );
916 attr_merge_one( e, ad_reqSession, &rdn, NULL );
918 if ( BER_BVISNULL( &op->o_dn ))
919 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
920 (struct berval *)&slap_empty_bv );
922 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
924 /* FIXME: need to add reqControls and reqRespControls */
929 static struct berval scopes[] = {
936 static struct berval derefs[] = {
938 BER_BVC("searching"),
943 static struct berval simple = BER_BVC("SIMPLE");
945 static void accesslog_val2val(AttributeDescription *ad, struct berval *val,
946 char c_op, struct berval *dst) {
949 dst->bv_len = ad->ad_cname.bv_len + val->bv_len + 2;
950 if ( c_op ) dst->bv_len++;
952 dst->bv_val = ch_malloc( dst->bv_len+1 );
954 ptr = lutil_strcopy( dst->bv_val, ad->ad_cname.bv_val );
959 AC_MEMCPY( ptr, val->bv_val, val->bv_len );
960 dst->bv_val[dst->bv_len] = '\0';
963 static int accesslog_response(Operation *op, SlapReply *rs) {
964 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
965 log_info *li = on->on_bi.bi_private;
966 Attribute *a, *last_attr;
972 Entry *e = NULL, *old = NULL;
973 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
978 SlapReply rs2 = {REP_RESULT};
980 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
981 return SLAP_CB_CONTINUE;
983 switch ( op->o_tag ) {
984 case LDAP_REQ_ADD: logop = LOG_EN_ADD; break;
985 case LDAP_REQ_DELETE: logop = LOG_EN_DELETE; break;
986 case LDAP_REQ_MODIFY: logop = LOG_EN_MODIFY; break;
987 case LDAP_REQ_MODRDN: logop = LOG_EN_MODRDN; break;
988 case LDAP_REQ_COMPARE: logop = LOG_EN_COMPARE; break;
989 case LDAP_REQ_SEARCH: logop = LOG_EN_SEARCH; break;
990 case LDAP_REQ_BIND: logop = LOG_EN_BIND; break;
991 case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
992 default: /* unknown operation type */
993 logop = LOG_EN_UNKNOWN; break;
994 } /* Unbind and Abandon never reach here */
996 lo = logops+logop+EN_OFFSET;
997 if ( !( li->li_ops & lo->mask ))
998 return SLAP_CB_CONTINUE;
1000 if ( lo->mask & LOG_OP_WRITES ) {
1001 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
1004 ldap_pvt_thread_rmutex_unlock( &li->li_op_rmutex, op->o_tid );
1007 if ( li->li_success && rs->sr_err != LDAP_SUCCESS )
1010 e = accesslog_entry( op, logop, &op2 );
1012 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
1014 if ( rs->sr_text ) {
1015 ber_str2bv( rs->sr_text, 0, 0, &bv );
1016 attr_merge_one( e, ad_reqMessage, &bv, NULL );
1018 bv.bv_len = sprintf( timebuf, "%d", rs->sr_err );
1019 bv.bv_val = timebuf;
1021 attr_merge_one( e, ad_reqResult, &bv, NULL );
1023 last_attr = attr_find( e->e_attrs, ad_reqResult );
1027 case LOG_EN_DELETE: {
1031 if ( logop == LOG_EN_ADD ) {
1040 /* count all the vals */
1042 for ( a=e2->e_attrs; a; a=a->a_next ) {
1044 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
1049 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1051 for ( a=e2->e_attrs; a; a=a->a_next ) {
1053 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1054 accesslog_val2val( a->a_desc, b, c_op, &vals[i] );
1058 vals[i].bv_val = NULL;
1060 a = attr_alloc( logop == LOG_EN_ADD ? ad_reqMod : ad_reqOld );
1063 last_attr->a_next = a;
1068 /* count all the mods */
1070 for ( m=op->orm_modlist; m; m=m->sml_next ) {
1071 if ( m->sml_values ) {
1072 for (b=m->sml_values; !BER_BVISNULL( b ); b++) {
1075 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1076 m->sml_op == LDAP_MOD_REPLACE ) {
1080 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1083 /* init flags on old entry */
1085 for ( a=old->e_attrs; a; a=a->a_next ) {
1089 /* look for attrs that are always logged */
1090 for ( la=li->li_oldattrs; la; la=la->next )
1091 if ( a->a_desc == la->attr )
1096 for ( m=op->orm_modlist; m; m=m->sml_next ) {
1097 /* Mark this attribute as modified */
1099 a = attr_find( old->e_attrs, m->sml_desc );
1103 if ( m->sml_values ) {
1104 for (b=m->sml_values; !BER_BVISNULL( b ); b++,i++) {
1107 switch( m->sml_op ) {
1108 case LDAP_MOD_ADD: c_op = '+'; break;
1109 case LDAP_MOD_DELETE: c_op = '-'; break;
1110 case LDAP_MOD_REPLACE: c_op = '='; break;
1111 case LDAP_MOD_INCREMENT: c_op = '#'; break;
1113 /* unknown op. there shouldn't be any of these. we
1114 * don't know what to do with it, but we shouldn't just
1117 default: c_op = '?'; break;
1119 accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
1121 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1122 m->sml_op == LDAP_MOD_REPLACE ) {
1123 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
1124 vals[i].bv_val = ch_malloc( vals[i].bv_len+1 );
1125 ptr = lutil_strcopy( vals[i].bv_val,
1126 m->sml_desc->ad_cname.bv_val );
1128 if ( m->sml_op == LDAP_MOD_DELETE )
1136 vals[i].bv_val = NULL;
1138 a = attr_alloc( ad_reqMod );
1141 last_attr->a_next = a;
1145 /* count all the vals */
1147 for ( a=old->e_attrs; a; a=a->a_next ) {
1148 if ( a->a_vals && a->a_flags ) {
1149 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
1154 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1156 for ( a=old->e_attrs; a; a=a->a_next ) {
1157 if ( a->a_vals && a->a_flags ) {
1158 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1159 accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1163 vals[i].bv_val = NULL;
1165 a = attr_alloc( ad_reqOld );
1168 last_attr->a_next = a;
1174 /* count all the vals */
1176 for ( a=old->e_attrs; a; a=a->a_next ) {
1179 /* look for attrs that are always logged */
1180 for ( la=li->li_oldattrs; la; la=la->next ) {
1181 if ( a->a_desc == la->attr ) {
1182 for (b=a->a_vals; !BER_BVISNULL( b ); b++) {
1188 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1190 for ( a=old->e_attrs; a; a=a->a_next ) {
1192 for ( la=li->li_oldattrs; la; la=la->next ) {
1193 if ( a->a_desc == la->attr ) {
1194 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1195 accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1200 vals[i].bv_val = NULL;
1202 a = attr_alloc( ad_reqOld );
1205 last_attr->a_next = a;
1207 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
1208 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
1209 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1211 if ( op->orr_newSup ) {
1212 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
1216 case LOG_EN_COMPARE:
1217 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
1218 op->orc_ava->aa_value.bv_len;
1219 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
1220 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
1222 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
1223 bv.bv_val[bv.bv_len] = '\0';
1224 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
1225 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1229 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
1230 attr_merge_one( e, ad_reqDerefAliases, &derefs[op->ors_deref], NULL );
1231 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
1232 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1234 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
1235 attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
1236 if ( op->ors_attrs ) {
1238 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1240 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
1242 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1243 vals[i] = op->ors_attrs[i].an_name;
1244 vals[i].bv_val = NULL;
1246 attr_merge( e, ad_reqAttr, vals, NULL );
1247 op->o_tmpfree( vals, op->o_tmpmemctx );
1249 bv.bv_val = timebuf;
1250 bv.bv_len = sprintf( bv.bv_val, "%d", rs->sr_nentries );
1251 attr_merge_one( e, ad_reqEntries, &bv, NULL );
1253 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_tlimit );
1254 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
1256 bv.bv_len = sprintf( bv.bv_val, "%d", op->ors_slimit );
1257 attr_merge_one( e, ad_reqSizeLimit, &bv, NULL );
1261 bv.bv_val = timebuf;
1262 bv.bv_len = sprintf( bv.bv_val, "%d", op->o_protocol );
1263 attr_merge_one( e, ad_reqVersion, &bv, NULL );
1264 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
1265 attr_merge_one( e, ad_reqMethod, &simple, NULL );
1267 bv.bv_len = STRLENOF("SASL()") + op->orb_tmp_mech.bv_len;
1268 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
1269 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
1270 ptr = lutil_strcopy( ptr, op->orb_tmp_mech.bv_val );
1273 attr_merge_one( e, ad_reqMethod, &bv, NULL );
1274 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1279 case LOG_EN_EXTENDED:
1280 if ( op->ore_reqdata ) {
1281 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
1285 case LOG_EN_UNKNOWN:
1286 /* we don't know its parameters, don't add any */
1290 op2.o_hdr = op->o_hdr;
1291 op2.o_tag = LDAP_REQ_ADD;
1292 op2.o_bd = li->li_db;
1293 op2.o_dn = li->li_db->be_rootdn;
1294 op2.o_ndn = li->li_db->be_rootndn;
1295 op2.o_req_dn = e->e_name;
1296 op2.o_req_ndn = e->e_nname;
1298 op2.o_callback = &nullsc;
1300 if (( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn )) {
1301 slap_queue_csn( &op2, &op->o_csn );
1304 op2.o_bd->be_add( &op2, &rs2 );
1305 if ( e == op2.ora_e ) entry_free( e );
1309 if ( lo->mask & LOG_OP_WRITES )
1310 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
1311 if ( e ) entry_free( e );
1312 if ( old ) entry_free( old );
1313 return SLAP_CB_CONTINUE;
1316 /* Since Bind success is sent by the frontend, it won't normally enter
1317 * the overlay response callback. Add another callback to make sure it
1321 accesslog_bind_resp( Operation *op, SlapReply *rs )
1330 db.bd_info = op->o_callback->sc_private;
1331 rc = accesslog_response( op, rs );
1333 sc = op->o_callback;
1334 op->o_callback = sc->sc_next;
1335 op->o_tmpfree( sc, op->o_tmpmemctx );
1340 accesslog_op_bind( Operation *op, SlapReply *rs )
1344 sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1345 sc->sc_response = accesslog_bind_resp;
1346 sc->sc_private = op->o_bd->bd_info;
1348 if ( op->o_callback ) {
1349 sc->sc_next = op->o_callback->sc_next;
1350 op->o_callback->sc_next = sc;
1352 op->o_callback = sc;
1354 return SLAP_CB_CONTINUE;
1358 accesslog_op_mod( Operation *op, SlapReply *rs )
1360 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1361 log_info *li = on->on_bi.bi_private;
1363 if ( li->li_ops & LOG_OP_WRITES ) {
1364 ldap_pvt_thread_rmutex_lock( &li->li_op_rmutex, op->o_tid );
1365 if ( li->li_oldf && ( op->o_tag == LDAP_REQ_DELETE ||
1366 op->o_tag == LDAP_REQ_MODIFY ||
1367 ( op->o_tag == LDAP_REQ_MODRDN && li->li_oldattrs ))) {
1371 op->o_bd->bd_info = on->on_info->oi_orig;
1372 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1374 if ( test_filter( op, e, li->li_oldf ) == LDAP_COMPARE_TRUE )
1375 li->li_old = entry_dup( e );
1376 be_entry_release_rw( op, e, 0 );
1378 op->o_bd->bd_info = (BackendInfo *)on;
1381 return SLAP_CB_CONTINUE;
1384 /* unbinds are broadcast to all backends; we only log it if this
1385 * backend was used for the original bind.
1388 accesslog_unbind( Operation *op, SlapReply *rs )
1390 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1391 if ( op->o_conn->c_authz_backend == on->on_info->oi_origdb ) {
1392 log_info *li = on->on_bi.bi_private;
1393 Operation op2 = {0};
1394 void *cids[SLAP_MAX_CIDS];
1395 SlapReply rs2 = {REP_RESULT};
1398 if ( !( li->li_ops & LOG_OP_UNBIND ))
1399 return SLAP_CB_CONTINUE;
1401 e = accesslog_entry( op, LOG_EN_UNBIND, &op2 );
1402 op2.o_hdr = op->o_hdr;
1403 op2.o_tag = LDAP_REQ_ADD;
1404 op2.o_bd = li->li_db;
1405 op2.o_dn = li->li_db->be_rootdn;
1406 op2.o_ndn = li->li_db->be_rootndn;
1407 op2.o_req_dn = e->e_name;
1408 op2.o_req_ndn = e->e_nname;
1410 op2.o_callback = &nullsc;
1411 op2.o_controls = cids;
1412 memset(cids, 0, sizeof( cids ));
1414 op2.o_bd->be_add( &op2, &rs2 );
1415 if ( e == op2.ora_e )
1418 return SLAP_CB_CONTINUE;
1422 accesslog_abandon( Operation *op, SlapReply *rs )
1424 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1425 log_info *li = on->on_bi.bi_private;
1426 Operation op2 = {0};
1427 void *cids[SLAP_MAX_CIDS];
1428 SlapReply rs2 = {REP_RESULT};
1433 if ( !op->o_time || !( li->li_ops & LOG_OP_ABANDON ))
1434 return SLAP_CB_CONTINUE;
1436 e = accesslog_entry( op, LOG_EN_ABANDON, &op2 );
1438 bv.bv_len = sprintf( buf, "%d", op->orn_msgid );
1439 attr_merge_one( e, ad_reqId, &bv, NULL );
1441 op2.o_hdr = op->o_hdr;
1442 op2.o_tag = LDAP_REQ_ADD;
1443 op2.o_bd = li->li_db;
1444 op2.o_dn = li->li_db->be_rootdn;
1445 op2.o_ndn = li->li_db->be_rootndn;
1446 op2.o_req_dn = e->e_name;
1447 op2.o_req_ndn = e->e_nname;
1449 op2.o_callback = &nullsc;
1450 op2.o_controls = cids;
1451 memset(cids, 0, sizeof( cids ));
1453 op2.o_bd->be_add( &op2, &rs2 );
1454 if ( e == op2.ora_e )
1457 return SLAP_CB_CONTINUE;
1461 accesslog_operational( Operation *op, SlapReply *rs )
1463 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1464 log_info *li = on->on_bi.bi_private;
1466 if ( rs->sr_entry != NULL
1467 && dn_match( &op->o_bd->be_nsuffix[0], &rs->sr_entry->e_nname ) )
1471 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1474 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1475 ad_inlist( ad_auditContext, rs->sr_attrs ) )
1477 *ap = attr_alloc( ad_auditContext );
1478 value_add_one( &(*ap)->a_vals,
1479 &li->li_db->be_suffix[0] );
1480 value_add_one( &(*ap)->a_nvals,
1481 &li->li_db->be_nsuffix[0] );
1485 return SLAP_CB_CONTINUE;
1488 static slap_overinst accesslog;
1495 slap_overinst *on = (slap_overinst *)be->bd_info;
1496 log_info *li = ch_calloc(1, sizeof(log_info));
1498 on->on_bi.bi_private = li;
1499 ldap_pvt_thread_rmutex_init( &li->li_op_rmutex );
1500 ldap_pvt_thread_mutex_init( &li->li_log_mutex );
1505 accesslog_db_destroy(
1509 slap_overinst *on = (slap_overinst *)be->bd_info;
1510 log_info *li = on->on_bi.bi_private;
1514 filter_free( li->li_oldf );
1515 for ( la=li->li_oldattrs; la; la=li->li_oldattrs ) {
1516 li->li_oldattrs = la->next;
1519 ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
1520 ldap_pvt_thread_rmutex_destroy( &li->li_op_rmutex );
1522 return LDAP_SUCCESS;
1525 /* Create the logdb's root entry if it's missing */
1531 struct re_s *rtask = arg;
1532 slap_overinst *on = rtask->arg;
1533 log_info *li = on->on_bi.bi_private;
1535 Connection conn = {0};
1536 OperationBuffer opbuf;
1537 Operation *op = (Operation *) &opbuf;
1542 connection_fake_init( &conn, op, ctx );
1543 op->o_bd = li->li_db;
1544 op->o_dn = li->li_db->be_rootdn;
1545 op->o_ndn = li->li_db->be_rootndn;
1546 rc = be_entry_get_rw( op, li->li_db->be_nsuffix, NULL, NULL, 0, &e );
1549 be_entry_release_rw( op, e, 0 );
1552 SlapReply rs = {REP_RESULT};
1553 struct berval rdn, nrdn, attr;
1555 AttributeDescription *ad = NULL;
1556 const char *text = NULL;
1560 ber_dupbv( &e->e_name, li->li_db->be_suffix );
1561 ber_dupbv( &e->e_nname, li->li_db->be_nsuffix );
1563 attr_merge_one( e, slap_schema.si_ad_objectClass,
1564 &log_container->soc_cname, NULL );
1566 dnRdn( &e->e_name, &rdn );
1567 dnRdn( &e->e_nname, &nrdn );
1568 ptr = ber_bvchr( &rdn, '=' );
1570 assert( ptr != NULL );
1572 attr.bv_val = rdn.bv_val;
1573 attr.bv_len = ptr - rdn.bv_val;
1575 slap_bv2ad( &attr, &ad, &text );
1578 rdn.bv_len -= attr.bv_len + 1;
1579 ptr = ber_bvchr( &nrdn, '=' );
1580 nrdn.bv_len -= ptr - nrdn.bv_val + 1;
1581 nrdn.bv_val = ptr+1;
1582 attr_merge_one( e, ad, &rdn, &nrdn );
1584 /* Get contextCSN from main DB */
1585 op->o_bd = on->on_info->oi_origdb;
1586 rc = be_entry_get_rw( op, op->o_bd->be_nsuffix, NULL,
1587 slap_schema.si_ad_contextCSN, 0, &e_ctx );
1592 a = attr_find( e_ctx->e_attrs, slap_schema.si_ad_contextCSN );
1594 attr_merge( e, slap_schema.si_ad_entryCSN, a->a_vals, NULL );
1595 attr_merge( e, a->a_desc, a->a_vals, NULL );
1597 be_entry_release_rw( op, e_ctx, 0 );
1599 op->o_bd = li->li_db;
1602 op->o_req_dn = e->e_name;
1603 op->o_req_ndn = e->e_nname;
1604 op->o_callback = &nullsc;
1605 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
1606 rc = op->o_bd->be_add( op, &rs );
1607 SLAP_DBFLAGS( op->o_bd ) ^= SLAP_DBFLAG_NOLASTMOD;
1608 if ( e == op->ora_e )
1611 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
1612 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
1613 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
1614 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
1624 slap_overinst *on = (slap_overinst *)be->bd_info;
1625 log_info *li = on->on_bi.bi_private;
1628 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
1629 li->li_db = select_backend( &li->li_db_suffix, 0, 0 );
1630 ch_free( li->li_db_suffix.bv_val );
1631 BER_BVZERO( &li->li_db_suffix );
1633 if ( li->li_db == NULL ) {
1634 Debug( LDAP_DEBUG_ANY,
1635 "accesslog: \"logdb <suffix>\" missing or invalid.\n",
1640 if ( slapMode & SLAP_TOOL_MODE )
1643 if ( BER_BVISEMPTY( &li->li_db->be_rootndn )) {
1644 ber_dupbv( &li->li_db->be_rootdn, li->li_db->be_suffix );
1645 ber_dupbv( &li->li_db->be_rootndn, li->li_db->be_nsuffix );
1648 ldap_pvt_runqueue_insert( &slapd_rq, 3600, accesslog_db_root, on,
1649 "accesslog_db_root", li->li_db->be_suffix[0].bv_val );
1654 int accesslog_initialize()
1658 accesslog.on_bi.bi_type = "accesslog";
1659 accesslog.on_bi.bi_db_init = accesslog_db_init;
1660 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
1661 accesslog.on_bi.bi_db_open = accesslog_db_open;
1663 accesslog.on_bi.bi_op_add = accesslog_op_mod;
1664 accesslog.on_bi.bi_op_bind = accesslog_op_bind;
1665 accesslog.on_bi.bi_op_delete = accesslog_op_mod;
1666 accesslog.on_bi.bi_op_modify = accesslog_op_mod;
1667 accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
1668 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
1669 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
1670 accesslog.on_bi.bi_operational = accesslog_operational;
1671 accesslog.on_response = accesslog_response;
1673 accesslog.on_bi.bi_cf_ocs = log_cfocs;
1675 nullsc.sc_response = slap_null_cb;
1677 rc = config_register_schema( log_cfats, log_cfocs );
1678 if ( rc ) return rc;
1680 /* log schema integration */
1681 for ( i=0; lattrs[i].at; i++ ) {
1684 code = register_at( lattrs[i].at, lattrs[i].ad, 0 );
1686 Debug( LDAP_DEBUG_ANY,
1687 "accesslog_init: register_at failed\n", 0, 0, 0 );
1691 for ( i=0; locs[i].ot; i++ ) {
1694 code = register_oc( locs[i].ot, locs[i].oc, 0 );
1696 Debug( LDAP_DEBUG_ANY,
1697 "accesslog_init: register_oc failed\n", 0, 0, 0 );
1702 return overlay_register(&accesslog);
1705 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
1707 init_module( int argc, char *argv[] )
1709 return accesslog_initialize();
1713 #endif /* SLAPD_OVER_ACCESSLOG */