1 /* accesslog.c - log operations for audit/history purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2008 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,
166 *log_oc_read, *log_oc_write;
168 #define LOG_SCHEMA_ROOT "1.3.6.1.4.1.4203.666.11.5"
170 #define LOG_SCHEMA_AT LOG_SCHEMA_ROOT ".1"
171 #define LOG_SCHEMA_OC LOG_SCHEMA_ROOT ".2"
172 #define LOG_SCHEMA_SYN LOG_SCHEMA_ROOT ".3"
174 static AttributeDescription *ad_reqDN, *ad_reqStart, *ad_reqEnd, *ad_reqType,
175 *ad_reqSession, *ad_reqResult, *ad_reqAuthzID, *ad_reqControls,
176 *ad_reqRespControls, *ad_reqMethod, *ad_reqAssertion, *ad_reqNewRDN,
177 *ad_reqNewSuperior, *ad_reqDeleteOldRDN, *ad_reqMod,
178 *ad_reqScope, *ad_reqFilter, *ad_reqAttr, *ad_reqEntries,
179 *ad_reqSizeLimit, *ad_reqTimeLimit, *ad_reqAttrsOnly, *ad_reqData,
180 *ad_reqId, *ad_reqMessage, *ad_reqVersion, *ad_reqDerefAliases,
181 *ad_reqReferral, *ad_reqOld, *ad_auditContext;
184 logSchemaControlValidate(
186 struct berval *val );
188 char *mrControl[] = {
189 "objectIdentifierFirstComponentMatch",
195 slap_syntax_defs_rec syn;
198 { LOG_SCHEMA_SYN ".1" ,
199 { "( " LOG_SCHEMA_SYN ".1 DESC 'Control' )",
202 logSchemaControlValidate,
210 AttributeDescription **ad;
212 { "( " LOG_SCHEMA_AT ".1 NAME 'reqDN' "
213 "DESC 'Target DN of request' "
214 "EQUALITY distinguishedNameMatch "
216 "SINGLE-VALUE )", &ad_reqDN },
217 { "( " LOG_SCHEMA_AT ".2 NAME 'reqStart' "
218 "DESC 'Start time of request' "
219 "EQUALITY generalizedTimeMatch "
220 "ORDERING generalizedTimeOrderingMatch "
221 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
222 "SINGLE-VALUE )", &ad_reqStart },
223 { "( " LOG_SCHEMA_AT ".3 NAME 'reqEnd' "
224 "DESC 'End time of request' "
225 "EQUALITY generalizedTimeMatch "
226 "ORDERING generalizedTimeOrderingMatch "
227 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.24 "
228 "SINGLE-VALUE )", &ad_reqEnd },
229 { "( " LOG_SCHEMA_AT ".4 NAME 'reqType' "
230 "DESC 'Type of request' "
231 "EQUALITY caseIgnoreMatch "
232 "SYNTAX OMsDirectoryString "
233 "SINGLE-VALUE )", &ad_reqType },
234 { "( " LOG_SCHEMA_AT ".5 NAME 'reqSession' "
235 "DESC 'Session ID of request' "
236 "EQUALITY caseIgnoreMatch "
237 "SYNTAX OMsDirectoryString "
238 "SINGLE-VALUE )", &ad_reqSession },
239 { "( " LOG_SCHEMA_AT ".6 NAME 'reqAuthzID' "
240 "DESC 'Authorization ID of requestor' "
241 "EQUALITY distinguishedNameMatch "
243 "SINGLE-VALUE )", &ad_reqAuthzID },
244 { "( " LOG_SCHEMA_AT ".7 NAME 'reqResult' "
245 "DESC 'Result code of request' "
246 "EQUALITY integerMatch "
247 "ORDERING integerOrderingMatch "
249 "SINGLE-VALUE )", &ad_reqResult },
250 { "( " LOG_SCHEMA_AT ".8 NAME 'reqMessage' "
251 "DESC 'Error text of request' "
252 "EQUALITY caseIgnoreMatch "
253 "SUBSTR caseIgnoreSubstringsMatch "
254 "SYNTAX OMsDirectoryString "
255 "SINGLE-VALUE )", &ad_reqMessage },
256 { "( " LOG_SCHEMA_AT ".9 NAME 'reqReferral' "
257 "DESC 'Referrals returned for request' "
258 "SUP labeledURI )", &ad_reqReferral },
259 { "( " LOG_SCHEMA_AT ".10 NAME 'reqControls' "
260 "DESC 'Request controls' "
261 "EQUALITY objectIdentifierFirstComponentMatch "
262 "SYNTAX " LOG_SCHEMA_SYN ".1 "
263 "X-ORDERED 'VALUES' )", &ad_reqControls },
264 { "( " LOG_SCHEMA_AT ".11 NAME 'reqRespControls' "
265 "DESC 'Response controls of request' "
266 "EQUALITY objectIdentifierFirstComponentMatch "
267 "SYNTAX " LOG_SCHEMA_SYN ".1 "
268 "X-ORDERED 'VALUES' )", &ad_reqRespControls },
269 { "( " LOG_SCHEMA_AT ".12 NAME 'reqId' "
270 "DESC 'ID of Request to Abandon' "
271 "EQUALITY integerMatch "
272 "ORDERING integerOrderingMatch "
274 "SINGLE-VALUE )", &ad_reqId },
275 { "( " LOG_SCHEMA_AT ".13 NAME 'reqVersion' "
276 "DESC 'Protocol version of Bind request' "
277 "EQUALITY integerMatch "
278 "ORDERING integerOrderingMatch "
280 "SINGLE-VALUE )", &ad_reqVersion },
281 { "( " LOG_SCHEMA_AT ".14 NAME 'reqMethod' "
282 "DESC 'Bind method of request' "
283 "EQUALITY caseIgnoreMatch "
284 "SYNTAX OMsDirectoryString "
285 "SINGLE-VALUE )", &ad_reqMethod },
286 { "( " LOG_SCHEMA_AT ".15 NAME 'reqAssertion' "
287 "DESC 'Compare Assertion of request' "
288 "SYNTAX OMsDirectoryString "
289 "SINGLE-VALUE )", &ad_reqAssertion },
290 { "( " LOG_SCHEMA_AT ".16 NAME 'reqMod' "
291 "DESC 'Modifications of request' "
292 "EQUALITY octetStringMatch "
293 "SUBSTR octetStringSubstringsMatch "
294 "SYNTAX OMsOctetString )", &ad_reqMod },
295 { "( " LOG_SCHEMA_AT ".17 NAME 'reqOld' "
296 "DESC 'Old values of entry before request completed' "
297 "EQUALITY octetStringMatch "
298 "SUBSTR octetStringSubstringsMatch "
299 "SYNTAX OMsOctetString )", &ad_reqOld },
300 { "( " LOG_SCHEMA_AT ".18 NAME 'reqNewRDN' "
301 "DESC 'New RDN of request' "
302 "EQUALITY distinguishedNameMatch "
304 "SINGLE-VALUE )", &ad_reqNewRDN },
305 { "( " LOG_SCHEMA_AT ".19 NAME 'reqDeleteOldRDN' "
306 "DESC 'Delete old RDN' "
307 "EQUALITY booleanMatch "
309 "SINGLE-VALUE )", &ad_reqDeleteOldRDN },
310 { "( " LOG_SCHEMA_AT ".20 NAME 'reqNewSuperior' "
311 "DESC 'New superior DN of request' "
312 "EQUALITY distinguishedNameMatch "
314 "SINGLE-VALUE )", &ad_reqNewSuperior },
315 { "( " LOG_SCHEMA_AT ".21 NAME 'reqScope' "
316 "DESC 'Scope of request' "
317 "EQUALITY caseIgnoreMatch "
318 "SYNTAX OMsDirectoryString "
319 "SINGLE-VALUE )", &ad_reqScope },
320 { "( " LOG_SCHEMA_AT ".22 NAME 'reqDerefAliases' "
321 "DESC 'Disposition of Aliases in request' "
322 "EQUALITY caseIgnoreMatch "
323 "SYNTAX OMsDirectoryString "
324 "SINGLE-VALUE )", &ad_reqDerefAliases },
325 { "( " LOG_SCHEMA_AT ".23 NAME 'reqAttrsOnly' "
326 "DESC 'Attributes and values of request' "
327 "EQUALITY booleanMatch "
329 "SINGLE-VALUE )", &ad_reqAttrsOnly },
330 { "( " LOG_SCHEMA_AT ".24 NAME 'reqFilter' "
331 "DESC 'Filter of request' "
332 "EQUALITY caseIgnoreMatch "
333 "SUBSTR caseIgnoreSubstringsMatch "
334 "SYNTAX OMsDirectoryString "
335 "SINGLE-VALUE )", &ad_reqFilter },
336 { "( " LOG_SCHEMA_AT ".25 NAME 'reqAttr' "
337 "DESC 'Attributes of request' "
338 "EQUALITY caseIgnoreMatch "
339 "SYNTAX OMsDirectoryString )", &ad_reqAttr },
340 { "( " LOG_SCHEMA_AT ".26 NAME 'reqSizeLimit' "
341 "DESC 'Size limit of request' "
342 "EQUALITY integerMatch "
343 "ORDERING integerOrderingMatch "
345 "SINGLE-VALUE )", &ad_reqSizeLimit },
346 { "( " LOG_SCHEMA_AT ".27 NAME 'reqTimeLimit' "
347 "DESC 'Time limit of request' "
348 "EQUALITY integerMatch "
349 "ORDERING integerOrderingMatch "
351 "SINGLE-VALUE )", &ad_reqTimeLimit },
352 { "( " LOG_SCHEMA_AT ".28 NAME 'reqEntries' "
353 "DESC 'Number of entries returned' "
354 "EQUALITY integerMatch "
355 "ORDERING integerOrderingMatch "
357 "SINGLE-VALUE )", &ad_reqEntries },
358 { "( " LOG_SCHEMA_AT ".29 NAME 'reqData' "
359 "DESC 'Data of extended request' "
360 "EQUALITY octetStringMatch "
361 "SUBSTR octetStringSubstringsMatch "
362 "SYNTAX OMsOctetString "
363 "SINGLE-VALUE )", &ad_reqData },
366 * from <draft-chu-ldap-logschema-01.txt>:
369 ( LOG_SCHEMA_AT .30 NAME 'auditContext'
370 DESC 'DN of auditContainer'
371 EQUALITY distinguishedNameMatch
372 SYNTAX 1.3.6.1.4.1.1466.115.121.1.12
373 SINGLE-VALUE NO-USER-MODIFICATION USAGE directoryOperation )
375 * - removed EQUALITY matchingRule
376 * - changed directoryOperation in dSAOperation
378 { "( " LOG_SCHEMA_AT ".30 NAME 'auditContext' "
379 "DESC 'DN of auditContainer' "
380 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.12 "
382 "NO-USER-MODIFICATION "
383 "USAGE dSAOperation )", &ad_auditContext },
391 { "( " LOG_SCHEMA_OC ".0 NAME 'auditContainer' "
392 "DESC 'AuditLog container' "
393 "SUP top STRUCTURAL "
394 "MAY ( cn $ reqStart $ reqEnd ) )", &log_container },
395 { "( " LOG_SCHEMA_OC ".1 NAME 'auditObject' "
396 "DESC 'OpenLDAP request auditing' "
397 "SUP top STRUCTURAL "
398 "MUST ( reqStart $ reqType $ reqSession ) "
399 "MAY ( reqDN $ reqAuthzID $ reqControls $ reqRespControls $ reqEnd $ "
400 "reqResult $ reqMessage $ reqReferral ) )",
401 &log_ocs[LOG_EN_UNBIND] },
402 { "( " LOG_SCHEMA_OC ".2 NAME 'auditReadObject' "
403 "DESC 'OpenLDAP read request record' "
404 "SUP auditObject STRUCTURAL )", &log_oc_read },
405 { "( " LOG_SCHEMA_OC ".3 NAME 'auditWriteObject' "
406 "DESC 'OpenLDAP write request record' "
407 "SUP auditObject STRUCTURAL )", &log_oc_write },
408 { "( " LOG_SCHEMA_OC ".4 NAME 'auditAbandon' "
409 "DESC 'Abandon operation' "
410 "SUP auditObject STRUCTURAL "
411 "MUST reqId )", &log_ocs[LOG_EN_ABANDON] },
412 { "( " LOG_SCHEMA_OC ".5 NAME 'auditAdd' "
413 "DESC 'Add operation' "
414 "SUP auditWriteObject STRUCTURAL "
415 "MUST reqMod )", &log_ocs[LOG_EN_ADD] },
416 { "( " LOG_SCHEMA_OC ".6 NAME 'auditBind' "
417 "DESC 'Bind operation' "
418 "SUP auditObject STRUCTURAL "
419 "MUST ( reqVersion $ reqMethod ) )", &log_ocs[LOG_EN_BIND] },
420 { "( " LOG_SCHEMA_OC ".7 NAME 'auditCompare' "
421 "DESC 'Compare operation' "
422 "SUP auditReadObject STRUCTURAL "
423 "MUST reqAssertion )", &log_ocs[LOG_EN_COMPARE] },
424 { "( " LOG_SCHEMA_OC ".8 NAME 'auditDelete' "
425 "DESC 'Delete operation' "
426 "SUP auditWriteObject STRUCTURAL "
427 "MAY reqOld )", &log_ocs[LOG_EN_DELETE] },
428 { "( " LOG_SCHEMA_OC ".9 NAME 'auditModify' "
429 "DESC 'Modify operation' "
430 "SUP auditWriteObject STRUCTURAL "
431 "MAY reqOld MUST reqMod )", &log_ocs[LOG_EN_MODIFY] },
432 { "( " LOG_SCHEMA_OC ".10 NAME 'auditModRDN' "
433 "DESC 'ModRDN operation' "
434 "SUP auditWriteObject STRUCTURAL "
435 "MUST ( reqNewRDN $ reqDeleteOldRDN ) "
436 "MAY ( reqNewSuperior $ reqMod $ reqOld ) )", &log_ocs[LOG_EN_MODRDN] },
437 { "( " LOG_SCHEMA_OC ".11 NAME 'auditSearch' "
438 "DESC 'Search operation' "
439 "SUP auditReadObject STRUCTURAL "
440 "MUST ( reqScope $ reqDerefAliases $ reqAttrsonly ) "
441 "MAY ( reqFilter $ reqAttr $ reqEntries $ reqSizeLimit $ "
442 "reqTimeLimit ) )", &log_ocs[LOG_EN_SEARCH] },
443 { "( " LOG_SCHEMA_OC ".12 NAME 'auditExtended' "
444 "DESC 'Extended operation' "
445 "SUP auditObject STRUCTURAL "
446 "MAY reqData )", &log_ocs[LOG_EN_EXTENDED] },
450 #define RDNEQ "reqStart="
452 /* Our time intervals are of the form [ddd+]hh:mm[:ss]
453 * If a field is present, it must be two digits. (Except for
454 * days, which can be arbitrary width.)
457 log_age_parse(char *agestr)
463 t1 = strtol( agestr, &endptr, 10 );
464 /* Is there a days delimiter? */
465 if ( *endptr == '+' ) {
466 /* 32 bit time only covers about 68 years */
467 if ( t1 < 0 || t1 > 25000 )
473 if ( agestr[2] != ':' ) {
474 /* No valid delimiter found, fail */
485 /* if there's a delimiter, it can only be a colon */
486 if ( agestr[2] != ':' )
489 /* If we're at the end of the string, and we started with days,
490 * fail because we expected to find minutes too.
492 return gotdays ? -1 : t1 * 60;
498 /* last field can only be seconds */
499 if ( agestr[2] && ( agestr[2] != ':' || !gotdays ))
509 t1 += atoi( agestr );
510 } else if ( gotdays ) {
511 /* only got days+hh:mm */
518 log_age_unparse( int age, struct berval *agebv, size_t size )
520 int dd, hh, mm, ss, len;
536 len = snprintf( ptr, size, "%d+", dd );
537 assert( len >= 0 && len < size );
541 len = snprintf( ptr, size, "%02d:%02d", hh, mm );
542 assert( len >= 0 && len < size );
546 len = snprintf( ptr, size, ":%02d", ss );
547 assert( len >= 0 && len < size );
552 agebv->bv_len = ptr - agebv->bv_val;
555 static slap_callback nullsc = { NULL, NULL, NULL, NULL };
557 #define PURGE_INCREMENT 100
559 typedef struct purge_data {
564 struct berval csn; /* an arbitrary old CSN */
568 log_old_lookup( Operation *op, SlapReply *rs )
570 purge_data *pd = op->o_callback->sc_private;
573 if ( rs->sr_type != REP_SEARCH) return 0;
575 if ( slapd_shutdown ) return 0;
577 /* Remember max CSN: should always be the last entry
578 * seen, since log entries are ordered chronologically...
580 a = attr_find( rs->sr_entry->e_attrs,
581 slap_schema.si_ad_entryCSN );
582 if ( a && ber_bvcmp( &a->a_vals[0], &pd->csn ) > 0 ) {
583 AC_MEMCPY( pd->csn.bv_val, a->a_vals[0].bv_val,
584 a->a_vals[0].bv_len );
585 pd->csn.bv_len = a->a_vals[0].bv_len;
587 if ( pd->used >= pd->slots ) {
588 pd->slots += PURGE_INCREMENT;
589 pd->dn = ch_realloc( pd->dn, pd->slots * sizeof( struct berval ));
590 pd->ndn = ch_realloc( pd->ndn, pd->slots * sizeof( struct berval ));
592 ber_dupbv( &pd->dn[pd->used], &rs->sr_entry->e_name );
593 ber_dupbv( &pd->ndn[pd->used], &rs->sr_entry->e_nname );
598 /* Periodically search for old entries in the log database and delete them */
600 accesslog_purge( void *ctx, void *arg )
602 struct re_s *rtask = arg;
603 struct log_info *li = rtask->arg;
605 Connection conn = {0};
606 OperationBuffer opbuf;
608 SlapReply rs = {REP_RESULT};
609 slap_callback cb = { NULL, log_old_lookup, NULL, NULL };
611 AttributeAssertion ava = ATTRIBUTEASSERTION_INIT;
613 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE];
614 char csnbuf[LDAP_LUTIL_CSNSTR_BUFSIZE];
615 time_t old = slap_get_time();
617 connection_fake_init( &conn, &opbuf, ctx );
620 f.f_choice = LDAP_FILTER_LE;
624 ava.aa_desc = ad_reqStart;
625 ava.aa_value.bv_val = timebuf;
626 ava.aa_value.bv_len = sizeof(timebuf);
629 slap_timestamp( &old, &ava.aa_value );
631 op->o_tag = LDAP_REQ_SEARCH;
632 op->o_bd = li->li_db;
633 op->o_dn = li->li_db->be_rootdn;
634 op->o_ndn = li->li_db->be_rootndn;
635 op->o_req_dn = li->li_db->be_suffix[0];
636 op->o_req_ndn = li->li_db->be_nsuffix[0];
637 op->o_callback = &cb;
638 op->ors_scope = LDAP_SCOPE_ONELEVEL;
639 op->ors_deref = LDAP_DEREF_NEVER;
640 op->ors_tlimit = SLAP_NO_LIMIT;
641 op->ors_slimit = SLAP_NO_LIMIT;
643 filter2bv_x( op, &f, &op->ors_filterstr );
644 op->ors_attrs = slap_anlist_no_attrs;
645 op->ors_attrsonly = 1;
647 pd.csn.bv_len = sizeof( csnbuf );
648 pd.csn.bv_val = csnbuf;
652 op->o_bd->be_search( op, &rs );
653 op->o_tmpfree( op->ors_filterstr.bv_val, op->o_tmpmemctx );
658 /* delete the expired entries */
659 op->o_tag = LDAP_REQ_DELETE;
660 op->o_callback = &nullsc;
663 for (i=0; i<pd.used; i++) {
664 op->o_req_dn = pd.dn[i];
665 op->o_req_ndn = pd.ndn[i];
666 if ( !slapd_shutdown )
667 op->o_bd->be_delete( op, &rs );
668 ch_free( pd.ndn[i].bv_val );
669 ch_free( pd.dn[i].bv_val );
677 /* update context's entryCSN to reflect oldest CSN */
682 mod.sml_nvalues = NULL;
683 mod.sml_desc = slap_schema.si_ad_entryCSN;
684 mod.sml_op = LDAP_MOD_REPLACE;
685 mod.sml_flags = SLAP_MOD_INTERNAL;
688 op->o_tag = LDAP_REQ_MODIFY;
689 op->orm_modlist = &mod;
690 op->orm_no_opattrs = 1;
691 op->o_req_dn = li->li_db->be_suffix[0];
692 op->o_req_ndn = li->li_db->be_nsuffix[0];
693 op->o_no_schema_check = 1;
694 op->o_managedsait = SLAP_CONTROL_NONCRITICAL;
695 op->o_bd->be_modify( op, &rs );
696 if ( mod.sml_next ) {
697 slap_mods_free( mod.sml_next, 1 );
702 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
703 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
704 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
710 log_cf_gen(ConfigArgs *c)
712 slap_overinst *on = (slap_overinst *)c->bi;
713 struct log_info *li = on->on_bi.bi_private;
715 slap_mask_t tmask = 0;
716 char agebuf[2*STRLENOF("ddddd+hh:mm:ss ")];
717 struct berval agebv, cyclebv;
720 case SLAP_CONFIG_EMIT:
723 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
724 value_add_one( &c->rvalue_vals, &li->li_db_suffix );
725 value_add_one( &c->rvalue_nvals, &li->li_db_suffix );
726 } else if ( li->li_db ) {
727 value_add_one( &c->rvalue_vals, li->li_db->be_suffix );
728 value_add_one( &c->rvalue_nvals, li->li_db->be_nsuffix );
730 snprintf( c->cr_msg, sizeof( c->cr_msg ),
731 "accesslog: \"logdb <suffix>\" must be specified" );
732 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
733 c->log, c->cr_msg, c->value_dn.bv_val );
739 rc = mask_to_verbs( logops, li->li_ops, &c->rvalue_vals );
746 agebv.bv_val = agebuf;
747 log_age_unparse( li->li_age, &agebv, sizeof( agebuf ) );
748 agebv.bv_val[agebv.bv_len] = ' ';
750 cyclebv.bv_val = agebv.bv_val + agebv.bv_len;
751 log_age_unparse( li->li_cycle, &cyclebv, sizeof( agebuf ) - agebv.bv_len );
752 agebv.bv_len += cyclebv.bv_len;
753 value_add_one( &c->rvalue_vals, &agebv );
756 if ( li->li_success )
757 c->value_int = li->li_success;
763 filter2bv( li->li_oldf, &agebv );
764 ber_bvarray_add( &c->rvalue_vals, &agebv );
770 if ( li->li_oldattrs ) {
773 for ( la = li->li_oldattrs; la; la=la->next )
774 value_add_one( &c->rvalue_vals, &la->attr->ad_cname );
781 case LDAP_MOD_DELETE:
784 /* noop. this should always be a valid backend. */
790 rc = verbs_to_mask( 1, &c->line, logops, &tmask );
792 li->li_ops &= ~tmask;
797 struct re_s *re = li->li_task;
799 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
800 if ( ldap_pvt_runqueue_isrunning( &slapd_rq, re ))
801 ldap_pvt_runqueue_stoptask( &slapd_rq, re );
802 ldap_pvt_runqueue_remove( &slapd_rq, re );
803 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
813 filter_free( li->li_oldf );
821 for ( la = li->li_oldattrs; la; la = ln ) {
826 log_attr *la = NULL, **lp;
829 for ( lp = &li->li_oldattrs, i=0; i < c->valx; i++ ) {
842 if ( CONFIG_ONLINE_ADD( c )) {
843 li->li_db = select_backend( &c->value_ndn, 0 );
845 snprintf( c->cr_msg, sizeof( c->cr_msg ),
846 "<%s> no matching backend found for suffix",
848 Debug( LDAP_DEBUG_ANY, "%s: %s \"%s\"\n",
849 c->log, c->cr_msg, c->value_dn.bv_val );
852 ch_free( c->value_ndn.bv_val );
854 li->li_db_suffix = c->value_ndn;
856 ch_free( c->value_dn.bv_val );
859 rc = verbs_to_mask( c->argc, c->argv, logops, &tmask );
864 li->li_age = log_age_parse( c->argv[1] );
865 if ( li->li_age < 1 ) {
868 li->li_cycle = log_age_parse( c->argv[2] );
869 if ( li->li_cycle < 1 ) {
871 } else if ( slapMode & SLAP_SERVER_MODE ) {
872 struct re_s *re = li->li_task;
874 re->interval.tv_sec = li->li_cycle;
876 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
877 li->li_task = ldap_pvt_runqueue_insert( &slapd_rq,
878 li->li_cycle, accesslog_purge, li,
879 "accesslog_purge", li->li_db ?
880 li->li_db->be_suffix[0].bv_val :
881 c->be->be_suffix[0].bv_val );
882 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
888 li->li_success = c->value_int;
891 li->li_oldf = str2filter( c->argv[1] );
892 if ( !li->li_oldf ) {
893 snprintf( c->cr_msg, sizeof( c->cr_msg ), "bad filter!" );
899 AttributeDescription *ad;
902 for ( i=1; i< c->argc; i++ ) {
904 if ( slap_str2ad( c->argv[i], &ad, &text ) == LDAP_SUCCESS ) {
905 log_attr *la = ch_malloc( sizeof( log_attr ));
907 la->next = li->li_oldattrs;
908 li->li_oldattrs = la;
910 snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s <%s>: %s",
911 c->argv[0], c->argv[i], text );
912 Debug( LDAP_DEBUG_CONFIG|LDAP_DEBUG_NONE,
913 "%s: %s\n", c->log, c->cr_msg, 0 );
927 logSchemaControlValidate(
929 struct berval *valp )
931 struct berval val, bv;
933 int rc = LDAP_SUCCESS;
935 assert( valp != NULL );
939 /* check minimal size */
940 if ( val.bv_len < STRLENOF( "{*}" ) ) {
941 return LDAP_INVALID_SYNTAX;
946 /* check SEQUENCE boundaries */
947 if ( val.bv_val[ 0 ] != '{' /*}*/ ||
948 val.bv_val[ val.bv_len ] != /*{*/ '}' )
950 return LDAP_INVALID_SYNTAX;
953 /* extract and check OID */
954 for ( i = 1; i < val.bv_len; i++ ) {
955 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
960 bv.bv_val = &val.bv_val[ i ];
962 for ( i++; i < val.bv_len; i++ ) {
963 if ( ASCII_SPACE( val.bv_val[ i ] ) )
969 bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
971 rc = numericoidValidate( NULL, &bv );
972 if ( rc != LDAP_SUCCESS ) {
976 if ( i == val.bv_len ) {
980 if ( val.bv_val[ i ] != ' ' ) {
981 return LDAP_INVALID_SYNTAX;
984 for ( i++; i < val.bv_len; i++ ) {
985 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
990 if ( i == val.bv_len ) {
994 /* extract and check criticality */
995 if ( strncasecmp( &val.bv_val[ i ], "criticality ", STRLENOF( "criticality " ) ) == 0 )
997 i += STRLENOF( "criticality " );
998 for ( ; i < val.bv_len; i++ ) {
999 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1004 if ( i == val.bv_len ) {
1005 return LDAP_INVALID_SYNTAX;
1008 bv.bv_val = &val.bv_val[ i ];
1010 for ( ; i < val.bv_len; i++ ) {
1011 if ( ASCII_SPACE( val.bv_val[ i ] ) ) {
1016 bv.bv_len = &val.bv_val[ i ] - bv.bv_val;
1018 if ( !bvmatch( &bv, &slap_true_bv ) && !bvmatch( &bv, &slap_false_bv ) )
1020 return LDAP_INVALID_SYNTAX;
1023 if ( i == val.bv_len ) {
1024 return LDAP_SUCCESS;
1027 if ( val.bv_val[ i ] != ' ' ) {
1028 return LDAP_INVALID_SYNTAX;
1031 for ( i++; i < val.bv_len; i++ ) {
1032 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1037 if ( i == val.bv_len ) {
1038 return LDAP_SUCCESS;
1042 /* extract and check controlValue */
1043 if ( strncasecmp( &val.bv_val[ i ], "controlValue ", STRLENOF( "controlValue " ) ) == 0 )
1045 i += STRLENOF( "controlValue " );
1046 for ( ; i < val.bv_len; i++ ) {
1047 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1052 if ( i == val.bv_len ) {
1053 return LDAP_INVALID_SYNTAX;
1056 if ( val.bv_val[ i ] != '"' ) {
1057 return LDAP_INVALID_SYNTAX;
1060 for ( ; i < val.bv_len; i++ ) {
1061 if ( val.bv_val[ i ] == '"' ) {
1065 if ( !ASCII_HEX( val.bv_val[ i ] ) ) {
1066 return LDAP_INVALID_SYNTAX;
1070 if ( val.bv_val[ i ] != '"' ) {
1071 return LDAP_INVALID_SYNTAX;
1074 for ( ; i < val.bv_len; i++ ) {
1075 if ( !ASCII_SPACE( val.bv_val[ i ] ) ) {
1080 if ( i == val.bv_len ) {
1081 return LDAP_SUCCESS;
1085 return LDAP_INVALID_SYNTAX;
1090 LDAPControl **ctrls,
1097 assert( valsp != NULL );
1098 assert( ctrls != NULL );
1103 for ( i = 0; ctrls[ i ] != NULL; i++ ) {
1111 if ( ctrls[ i ]->ldctl_oid == NULL ) {
1112 return LDAP_PROTOCOL_ERROR;
1115 idx.bv_len = snprintf( buf, sizeof( buf ), "{%ld}", i );
1118 ber_str2bv( ctrls[ i ]->ldctl_oid, 0, 0, &oid );
1119 noid.bv_len = idx.bv_len + oid.bv_len;
1120 ptr = noid.bv_val = ber_memalloc_x( noid.bv_len + 1, memctx );
1121 ptr = lutil_strcopy( ptr, idx.bv_val );
1122 ptr = lutil_strcopy( ptr, oid.bv_val );
1124 bv.bv_len = idx.bv_len + STRLENOF( "{}" ) + oid.bv_len;
1126 if ( ctrls[ i ]->ldctl_iscritical ) {
1127 bv.bv_len += STRLENOF( " criticality TRUE" );
1130 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1131 bv.bv_len += STRLENOF( " controlValue \"\"" )
1132 + 2 * ctrls[ i ]->ldctl_value.bv_len;
1135 ptr = bv.bv_val = ber_memalloc_x( bv.bv_len + 1, memctx );
1136 if ( ptr == NULL ) {
1137 ber_bvarray_free( *valsp );
1139 ber_bvarray_free( *nvalsp );
1144 ptr = lutil_strcopy( ptr, idx.bv_val );
1146 *ptr++ = '{' /*}*/ ;
1147 ptr = lutil_strcopy( ptr, oid.bv_val );
1149 if ( ctrls[ i ]->ldctl_iscritical ) {
1150 ptr = lutil_strcopy( ptr, " criticality TRUE" );
1153 if ( !BER_BVISNULL( &ctrls[ i ]->ldctl_value ) ) {
1156 ptr = lutil_strcopy( ptr, " controlValue \"" );
1157 for ( j = 0; j < ctrls[ i ]->ldctl_value.bv_len; j++ )
1161 o = ( ( ctrls[ i ]->ldctl_value.bv_val[ j ] >> 4 ) & 0xF );
1169 o = ( ctrls[ i ]->ldctl_value.bv_val[ j ] & 0xF );
1184 ber_bvarray_add_x( valsp, &bv, memctx );
1185 ber_bvarray_add_x( nvalsp, &noid, memctx );
1192 static Entry *accesslog_entry( Operation *op, SlapReply *rs, int logop,
1194 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1195 log_info *li = on->on_bi.bi_private;
1197 char rdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1198 char nrdnbuf[STRLENOF(RDNEQ)+LDAP_LUTIL_GENTIME_BUFSIZE+8];
1200 struct berval rdn, nrdn, timestamp, ntimestamp, bv;
1201 slap_verbmasks *lo = logops+logop+EN_OFFSET;
1203 Entry *e = entry_alloc();
1205 strcpy( rdnbuf, RDNEQ );
1206 rdn.bv_val = rdnbuf;
1207 strcpy( nrdnbuf, RDNEQ );
1208 nrdn.bv_val = nrdnbuf;
1210 timestamp.bv_val = rdnbuf+STRLENOF(RDNEQ);
1211 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1212 slap_timestamp( &op->o_time, ×tamp );
1213 snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op->o_tincr );
1214 timestamp.bv_len += STRLENOF(".123456");
1216 rdn.bv_len = STRLENOF(RDNEQ)+timestamp.bv_len;
1217 ad_reqStart->ad_type->sat_equality->smr_normalize(
1218 SLAP_MR_VALUE_OF_ASSERTION_SYNTAX, ad_reqStart->ad_type->sat_syntax,
1219 ad_reqStart->ad_type->sat_equality, ×tamp, &ntimestamp,
1222 strcpy( nrdn.bv_val + STRLENOF(RDNEQ), ntimestamp.bv_val );
1223 nrdn.bv_len = STRLENOF(RDNEQ)+ntimestamp.bv_len;
1224 build_new_dn( &e->e_name, li->li_db->be_suffix, &rdn, NULL );
1225 build_new_dn( &e->e_nname, li->li_db->be_nsuffix, &nrdn, NULL );
1227 attr_merge_one( e, slap_schema.si_ad_objectClass,
1228 &log_ocs[logop]->soc_cname, NULL );
1229 attr_merge_one( e, slap_schema.si_ad_structuralObjectClass,
1230 &log_ocs[logop]->soc_cname, NULL );
1231 attr_merge_one( e, ad_reqStart, ×tamp, &ntimestamp );
1232 op->o_tmpfree( ntimestamp.bv_val, op->o_tmpmemctx );
1234 slap_op_time( &op2->o_time, &op2->o_tincr );
1236 timestamp.bv_len = sizeof(rdnbuf) - STRLENOF(RDNEQ);
1237 slap_timestamp( &op2->o_time, ×tamp );
1238 snprintf( timestamp.bv_val + timestamp.bv_len-1, sizeof(".123456Z"), ".%06dZ", op2->o_tincr );
1239 timestamp.bv_len += STRLENOF(".123456");
1241 attr_merge_normalize_one( e, ad_reqEnd, ×tamp, op->o_tmpmemctx );
1243 /* Exops have OID appended */
1244 if ( logop == LOG_EN_EXTENDED ) {
1245 bv.bv_len = lo->word.bv_len + op->ore_reqoid.bv_len + 2;
1246 bv.bv_val = ch_malloc( bv.bv_len + 1 );
1247 AC_MEMCPY( bv.bv_val, lo->word.bv_val, lo->word.bv_len );
1248 bv.bv_val[lo->word.bv_len] = '{';
1249 AC_MEMCPY( bv.bv_val+lo->word.bv_len+1, op->ore_reqoid.bv_val,
1250 op->ore_reqoid.bv_len );
1251 bv.bv_val[bv.bv_len-1] = '}';
1252 bv.bv_val[bv.bv_len] = '\0';
1253 attr_merge_one( e, ad_reqType, &bv, NULL );
1255 attr_merge_one( e, ad_reqType, &lo->word, NULL );
1258 rdn.bv_len = snprintf( rdn.bv_val, sizeof( rdnbuf ), "%lu", op->o_connid );
1259 if ( rdn.bv_len >= 0 || rdn.bv_len < sizeof( rdnbuf ) ) {
1260 attr_merge_one( e, ad_reqSession, &rdn, NULL );
1263 if ( BER_BVISNULL( &op->o_dn ) ) {
1264 attr_merge_one( e, ad_reqAuthzID, (struct berval *)&slap_empty_bv,
1265 (struct berval *)&slap_empty_bv );
1267 attr_merge_one( e, ad_reqAuthzID, &op->o_dn, &op->o_ndn );
1270 /* FIXME: need to add reqControls and reqRespControls */
1271 if ( op->o_ctrls ) {
1272 BerVarray vals = NULL,
1275 if ( accesslog_ctrls( op->o_ctrls, &vals, &nvals,
1276 op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1278 attr_merge( e, ad_reqControls, vals, nvals );
1279 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1280 ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1284 if ( rs->sr_ctrls ) {
1285 BerVarray vals = NULL,
1288 if ( accesslog_ctrls( rs->sr_ctrls, &vals, &nvals,
1289 op->o_tmpmemctx ) == LDAP_SUCCESS && vals )
1291 attr_merge( e, ad_reqRespControls, vals, nvals );
1292 ber_bvarray_free_x( vals, op->o_tmpmemctx );
1293 ber_bvarray_free_x( nvals, op->o_tmpmemctx );
1301 static struct berval scopes[] = {
1308 static struct berval derefs[] = {
1310 BER_BVC("searching"),
1315 static struct berval simple = BER_BVC("SIMPLE");
1317 static void accesslog_val2val(AttributeDescription *ad, struct berval *val,
1318 char c_op, struct berval *dst) {
1321 dst->bv_len = ad->ad_cname.bv_len + val->bv_len + 2;
1322 if ( c_op ) dst->bv_len++;
1324 dst->bv_val = ch_malloc( dst->bv_len+1 );
1326 ptr = lutil_strcopy( dst->bv_val, ad->ad_cname.bv_val );
1331 AC_MEMCPY( ptr, val->bv_val, val->bv_len );
1332 dst->bv_val[dst->bv_len] = '\0';
1335 static int accesslog_response(Operation *op, SlapReply *rs) {
1336 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1337 log_info *li = on->on_bi.bi_private;
1338 Attribute *a, *last_attr;
1344 Entry *e = NULL, *old = NULL;
1345 char timebuf[LDAP_LUTIL_GENTIME_BUFSIZE+8];
1349 Operation op2 = {0};
1350 SlapReply rs2 = {REP_RESULT};
1352 if ( rs->sr_type != REP_RESULT && rs->sr_type != REP_EXTENDED )
1353 return SLAP_CB_CONTINUE;
1355 switch ( op->o_tag ) {
1356 case LDAP_REQ_ADD: logop = LOG_EN_ADD; break;
1357 case LDAP_REQ_DELETE: logop = LOG_EN_DELETE; break;
1358 case LDAP_REQ_MODIFY: logop = LOG_EN_MODIFY; break;
1359 case LDAP_REQ_MODRDN: logop = LOG_EN_MODRDN; break;
1360 case LDAP_REQ_COMPARE: logop = LOG_EN_COMPARE; break;
1361 case LDAP_REQ_SEARCH: logop = LOG_EN_SEARCH; break;
1362 case LDAP_REQ_BIND: logop = LOG_EN_BIND; break;
1363 case LDAP_REQ_EXTENDED: logop = LOG_EN_EXTENDED; break;
1364 default: /* unknown operation type */
1365 logop = LOG_EN_UNKNOWN; break;
1366 } /* Unbind and Abandon never reach here */
1368 lo = logops+logop+EN_OFFSET;
1369 if ( !( li->li_ops & lo->mask ))
1370 return SLAP_CB_CONTINUE;
1372 if ( lo->mask & LOG_OP_WRITES ) {
1374 ldap_pvt_thread_mutex_lock( &li->li_log_mutex );
1377 /* Disarm mod_cleanup */
1378 for ( cb = op->o_callback; cb; cb = cb->sc_next ) {
1379 if ( cb->sc_private == (void *)on ) {
1380 cb->sc_private = NULL;
1384 ldap_pvt_thread_rmutex_unlock( &li->li_op_rmutex, op->o_tid );
1387 if ( li->li_success && rs->sr_err != LDAP_SUCCESS )
1390 e = accesslog_entry( op, rs, logop, &op2 );
1392 attr_merge_one( e, ad_reqDN, &op->o_req_dn, &op->o_req_ndn );
1394 if ( rs->sr_text ) {
1395 ber_str2bv( rs->sr_text, 0, 0, &bv );
1396 attr_merge_one( e, ad_reqMessage, &bv, NULL );
1398 bv.bv_len = snprintf( timebuf, sizeof( timebuf ), "%d", rs->sr_err );
1399 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( timebuf ) ) {
1400 bv.bv_val = timebuf;
1401 attr_merge_one( e, ad_reqResult, &bv, NULL );
1404 last_attr = attr_find( e->e_attrs, ad_reqResult );
1408 case LOG_EN_DELETE: {
1412 if ( logop == LOG_EN_ADD ) {
1421 /* count all the vals */
1423 for ( a=e2->e_attrs; a; a=a->a_next ) {
1426 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1428 for ( a=e2->e_attrs; a; a=a->a_next ) {
1430 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1431 accesslog_val2val( a->a_desc, b, c_op, &vals[i] );
1435 vals[i].bv_val = NULL;
1437 a = attr_alloc( logop == LOG_EN_ADD ? ad_reqMod : ad_reqOld );
1441 last_attr->a_next = a;
1447 /* count all the mods */
1449 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1450 if ( m->sml_values ) {
1451 i += m->sml_numvals;
1452 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1453 m->sml_op == LDAP_MOD_REPLACE )
1458 vals = ch_malloc( (i+1) * sizeof( struct berval ));
1461 /* init flags on old entry */
1463 for ( a = old->e_attrs; a; a = a->a_next ) {
1467 /* look for attrs that are always logged */
1468 for ( la = li->li_oldattrs; la; la = la->next ) {
1469 if ( a->a_desc == la->attr ) {
1476 for ( m = op->orm_modlist; m; m = m->sml_next ) {
1477 /* Mark this attribute as modified */
1479 a = attr_find( old->e_attrs, m->sml_desc );
1485 /* don't log the RDN mods; they're explicitly logged later */
1486 if ( logop == LOG_EN_MODRDN &&
1487 ( m->sml_op == SLAP_MOD_SOFTADD ||
1488 m->sml_op == LDAP_MOD_DELETE ) )
1493 if ( m->sml_values ) {
1494 for ( b = m->sml_values; !BER_BVISNULL( b ); b++, i++ ) {
1497 switch ( m->sml_op ) {
1498 case LDAP_MOD_ADD: c_op = '+'; break;
1499 case LDAP_MOD_DELETE: c_op = '-'; break;
1500 case LDAP_MOD_REPLACE: c_op = '='; break;
1501 case LDAP_MOD_INCREMENT: c_op = '#'; break;
1503 /* unknown op. there shouldn't be any of these. we
1504 * don't know what to do with it, but we shouldn't just
1507 default: c_op = '?'; break;
1509 accesslog_val2val( m->sml_desc, b, c_op, &vals[i] );
1511 } else if ( m->sml_op == LDAP_MOD_DELETE ||
1512 m->sml_op == LDAP_MOD_REPLACE )
1514 vals[i].bv_len = m->sml_desc->ad_cname.bv_len + 2;
1515 vals[i].bv_val = ch_malloc( vals[i].bv_len + 1 );
1516 ptr = lutil_strcopy( vals[i].bv_val,
1517 m->sml_desc->ad_cname.bv_val );
1519 if ( m->sml_op == LDAP_MOD_DELETE ) {
1530 BER_BVZERO( &vals[i] );
1531 a = attr_alloc( ad_reqMod );
1535 last_attr->a_next = a;
1543 /* count all the vals */
1545 for ( a = old->e_attrs; a != NULL; a = a->a_next ) {
1546 if ( a->a_vals && a->a_flags ) {
1550 vals = ch_malloc( (i + 1) * sizeof( struct berval ) );
1552 for ( a=old->e_attrs; a; a=a->a_next ) {
1553 if ( a->a_vals && a->a_flags ) {
1554 for (b=a->a_vals; !BER_BVISNULL( b ); b++,i++) {
1555 accesslog_val2val( a->a_desc, b, 0, &vals[i] );
1559 vals[i].bv_val = NULL;
1561 a = attr_alloc( ad_reqOld );
1565 last_attr->a_next = a;
1567 if ( logop == LOG_EN_MODIFY ) {
1571 /* Now log the actual modRDN info */
1572 attr_merge_one( e, ad_reqNewRDN, &op->orr_newrdn, &op->orr_nnewrdn );
1573 attr_merge_one( e, ad_reqDeleteOldRDN, op->orr_deleteoldrdn ?
1574 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1576 if ( op->orr_newSup ) {
1577 attr_merge_one( e, ad_reqNewSuperior, op->orr_newSup, op->orr_nnewSup );
1581 case LOG_EN_COMPARE:
1582 bv.bv_len = op->orc_ava->aa_desc->ad_cname.bv_len + 1 +
1583 op->orc_ava->aa_value.bv_len;
1584 bv.bv_val = op->o_tmpalloc( bv.bv_len+1, op->o_tmpmemctx );
1585 ptr = lutil_strcopy( bv.bv_val, op->orc_ava->aa_desc->ad_cname.bv_val );
1587 AC_MEMCPY( ptr, op->orc_ava->aa_value.bv_val, op->orc_ava->aa_value.bv_len );
1588 bv.bv_val[bv.bv_len] = '\0';
1589 attr_merge_one( e, ad_reqAssertion, &bv, NULL );
1590 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1594 attr_merge_one( e, ad_reqScope, &scopes[op->ors_scope], NULL );
1595 attr_merge_one( e, ad_reqDerefAliases, &derefs[op->ors_deref], NULL );
1596 attr_merge_one( e, ad_reqAttrsOnly, op->ors_attrsonly ?
1597 (struct berval *)&slap_true_bv : (struct berval *)&slap_false_bv,
1599 if ( !BER_BVISEMPTY( &op->ors_filterstr ))
1600 attr_merge_one( e, ad_reqFilter, &op->ors_filterstr, NULL );
1601 if ( op->ors_attrs ) {
1603 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1605 vals = op->o_tmpalloc( (i+1) * sizeof(struct berval),
1607 for (i=0; !BER_BVISNULL(&op->ors_attrs[i].an_name );i++)
1608 vals[i] = op->ors_attrs[i].an_name;
1609 vals[i].bv_val = NULL;
1611 attr_merge( e, ad_reqAttr, vals, NULL );
1612 op->o_tmpfree( vals, op->o_tmpmemctx );
1614 bv.bv_val = timebuf;
1615 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", rs->sr_nentries );
1616 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( timebuf ) ) {
1617 attr_merge_one( e, ad_reqEntries, &bv, NULL );
1620 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_tlimit );
1621 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( timebuf ) ) {
1622 attr_merge_one( e, ad_reqTimeLimit, &bv, NULL );
1625 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->ors_slimit );
1626 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( timebuf ) ) {
1627 attr_merge_one( e, ad_reqSizeLimit, &bv, NULL );
1632 bv.bv_val = timebuf;
1633 bv.bv_len = snprintf( bv.bv_val, sizeof( timebuf ), "%d", op->o_protocol );
1634 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( timebuf ) ) {
1635 attr_merge_one( e, ad_reqVersion, &bv, NULL );
1637 if ( op->orb_method == LDAP_AUTH_SIMPLE ) {
1638 attr_merge_one( e, ad_reqMethod, &simple, NULL );
1640 bv.bv_len = STRLENOF("SASL()") + op->orb_mech.bv_len;
1641 bv.bv_val = op->o_tmpalloc( bv.bv_len + 1, op->o_tmpmemctx );
1642 ptr = lutil_strcopy( bv.bv_val, "SASL(" );
1643 ptr = lutil_strcopy( ptr, op->orb_mech.bv_val );
1646 attr_merge_one( e, ad_reqMethod, &bv, NULL );
1647 op->o_tmpfree( bv.bv_val, op->o_tmpmemctx );
1652 case LOG_EN_EXTENDED:
1653 if ( op->ore_reqdata ) {
1654 attr_merge_one( e, ad_reqData, op->ore_reqdata, NULL );
1658 case LOG_EN_UNKNOWN:
1659 /* we don't know its parameters, don't add any */
1663 op2.o_hdr = op->o_hdr;
1664 op2.o_tag = LDAP_REQ_ADD;
1665 op2.o_bd = li->li_db;
1666 op2.o_dn = li->li_db->be_rootdn;
1667 op2.o_ndn = li->li_db->be_rootndn;
1668 op2.o_req_dn = e->e_name;
1669 op2.o_req_ndn = e->e_nname;
1671 op2.o_callback = &nullsc;
1673 if (( lo->mask & LOG_OP_WRITES ) && !BER_BVISEMPTY( &op->o_csn )) {
1674 slap_queue_csn( &op2, &op->o_csn );
1677 op2.o_bd->be_add( &op2, &rs2 );
1678 if ( e == op2.ora_e ) entry_free( e );
1682 if ( lo->mask & LOG_OP_WRITES )
1683 ldap_pvt_thread_mutex_unlock( &li->li_log_mutex );
1684 if ( old ) entry_free( old );
1685 return SLAP_CB_CONTINUE;
1688 /* Since Bind success is sent by the frontend, it won't normally enter
1689 * the overlay response callback. Add another callback to make sure it
1693 accesslog_bind_resp( Operation *op, SlapReply *rs )
1702 db.bd_info = op->o_callback->sc_private;
1703 rc = accesslog_response( op, rs );
1705 sc = op->o_callback;
1706 op->o_callback = sc->sc_next;
1707 op->o_tmpfree( sc, op->o_tmpmemctx );
1712 accesslog_op_bind( Operation *op, SlapReply *rs )
1716 sc = op->o_tmpcalloc( 1, sizeof(slap_callback), op->o_tmpmemctx );
1717 sc->sc_response = accesslog_bind_resp;
1718 sc->sc_private = op->o_bd->bd_info;
1720 if ( op->o_callback ) {
1721 sc->sc_next = op->o_callback->sc_next;
1722 op->o_callback->sc_next = sc;
1724 op->o_callback = sc;
1726 return SLAP_CB_CONTINUE;
1730 accesslog_mod_cleanup( Operation *op, SlapReply *rs )
1732 slap_callback *sc = op->o_callback;
1733 slap_overinst *on = sc->sc_private;
1734 op->o_callback = sc->sc_next;
1736 op->o_tmpfree( sc, op->o_tmpmemctx );
1739 BackendInfo *bi = op->o_bd->bd_info;
1740 op->o_bd->bd_info = (BackendInfo *)on;
1741 accesslog_response( op, rs );
1742 op->o_bd->bd_info = bi;
1748 accesslog_op_mod( Operation *op, SlapReply *rs )
1750 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1751 log_info *li = on->on_bi.bi_private;
1753 if ( li->li_ops & LOG_OP_WRITES ) {
1754 slap_callback *cb = op->o_tmpalloc( sizeof( slap_callback ), op->o_tmpmemctx ), *cb2;
1755 cb->sc_cleanup = accesslog_mod_cleanup;
1756 cb->sc_response = NULL;
1757 cb->sc_private = on;
1759 for ( cb2 = op->o_callback; cb2->sc_next; cb2 = cb2->sc_next );
1762 ldap_pvt_thread_rmutex_lock( &li->li_op_rmutex, op->o_tid );
1763 if ( li->li_oldf && ( op->o_tag == LDAP_REQ_DELETE ||
1764 op->o_tag == LDAP_REQ_MODIFY ||
1765 ( op->o_tag == LDAP_REQ_MODRDN && li->li_oldattrs ))) {
1769 op->o_bd->bd_info = on->on_info->oi_orig;
1770 rc = be_entry_get_rw( op, &op->o_req_ndn, NULL, NULL, 0, &e );
1772 if ( test_filter( op, e, li->li_oldf ) == LDAP_COMPARE_TRUE )
1773 li->li_old = entry_dup( e );
1774 be_entry_release_rw( op, e, 0 );
1776 op->o_bd->bd_info = (BackendInfo *)on;
1779 return SLAP_CB_CONTINUE;
1782 /* unbinds are broadcast to all backends; we only log it if this
1783 * backend was used for the original bind.
1786 accesslog_unbind( Operation *op, SlapReply *rs )
1788 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1789 if ( op->o_conn->c_authz_backend == on->on_info->oi_origdb ) {
1790 log_info *li = on->on_bi.bi_private;
1791 Operation op2 = {0};
1792 void *cids[SLAP_MAX_CIDS];
1793 SlapReply rs2 = {REP_RESULT};
1796 if ( !( li->li_ops & LOG_OP_UNBIND ))
1797 return SLAP_CB_CONTINUE;
1799 e = accesslog_entry( op, rs, LOG_EN_UNBIND, &op2 );
1800 op2.o_hdr = op->o_hdr;
1801 op2.o_tag = LDAP_REQ_ADD;
1802 op2.o_bd = li->li_db;
1803 op2.o_dn = li->li_db->be_rootdn;
1804 op2.o_ndn = li->li_db->be_rootndn;
1805 op2.o_req_dn = e->e_name;
1806 op2.o_req_ndn = e->e_nname;
1808 op2.o_callback = &nullsc;
1809 op2.o_controls = cids;
1810 memset(cids, 0, sizeof( cids ));
1812 op2.o_bd->be_add( &op2, &rs2 );
1813 if ( e == op2.ora_e )
1816 return SLAP_CB_CONTINUE;
1820 accesslog_abandon( Operation *op, SlapReply *rs )
1822 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1823 log_info *li = on->on_bi.bi_private;
1824 Operation op2 = {0};
1825 void *cids[SLAP_MAX_CIDS];
1826 SlapReply rs2 = {REP_RESULT};
1831 if ( !op->o_time || !( li->li_ops & LOG_OP_ABANDON ))
1832 return SLAP_CB_CONTINUE;
1834 e = accesslog_entry( op, rs, LOG_EN_ABANDON, &op2 );
1836 bv.bv_len = snprintf( buf, sizeof( buf ), "%d", op->orn_msgid );
1837 if ( bv.bv_len >= 0 && bv.bv_len < sizeof( buf ) ) {
1838 attr_merge_one( e, ad_reqId, &bv, NULL );
1841 op2.o_hdr = op->o_hdr;
1842 op2.o_tag = LDAP_REQ_ADD;
1843 op2.o_bd = li->li_db;
1844 op2.o_dn = li->li_db->be_rootdn;
1845 op2.o_ndn = li->li_db->be_rootndn;
1846 op2.o_req_dn = e->e_name;
1847 op2.o_req_ndn = e->e_nname;
1849 op2.o_callback = &nullsc;
1850 op2.o_controls = cids;
1851 memset(cids, 0, sizeof( cids ));
1853 op2.o_bd->be_add( &op2, &rs2 );
1854 if ( e == op2.ora_e )
1857 return SLAP_CB_CONTINUE;
1861 accesslog_operational( Operation *op, SlapReply *rs )
1863 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
1864 log_info *li = on->on_bi.bi_private;
1866 if ( op->o_sync != SLAP_CONTROL_NONE )
1867 return SLAP_CB_CONTINUE;
1869 if ( rs->sr_entry != NULL
1870 && dn_match( &op->o_bd->be_nsuffix[0], &rs->sr_entry->e_nname ) )
1874 for ( ap = &rs->sr_operational_attrs; *ap; ap = &(*ap)->a_next )
1877 if ( SLAP_OPATTRS( rs->sr_attr_flags ) ||
1878 ad_inlist( ad_auditContext, rs->sr_attrs ) )
1880 *ap = attr_alloc( ad_auditContext );
1882 &li->li_db->be_suffix[0],
1883 &li->li_db->be_nsuffix[0], 1 );
1887 return SLAP_CB_CONTINUE;
1890 static slap_overinst accesslog;
1898 slap_overinst *on = (slap_overinst *)be->bd_info;
1899 log_info *li = ch_calloc(1, sizeof(log_info));
1901 on->on_bi.bi_private = li;
1902 ldap_pvt_thread_rmutex_init( &li->li_op_rmutex );
1903 ldap_pvt_thread_mutex_init( &li->li_log_mutex );
1908 accesslog_db_destroy(
1913 slap_overinst *on = (slap_overinst *)be->bd_info;
1914 log_info *li = on->on_bi.bi_private;
1918 filter_free( li->li_oldf );
1919 for ( la=li->li_oldattrs; la; la=li->li_oldattrs ) {
1920 li->li_oldattrs = la->next;
1923 ldap_pvt_thread_mutex_destroy( &li->li_log_mutex );
1924 ldap_pvt_thread_rmutex_destroy( &li->li_op_rmutex );
1926 return LDAP_SUCCESS;
1929 /* Create the logdb's root entry if it's missing */
1935 struct re_s *rtask = arg;
1936 slap_overinst *on = rtask->arg;
1937 log_info *li = on->on_bi.bi_private;
1939 Connection conn = {0};
1940 OperationBuffer opbuf;
1946 connection_fake_init( &conn, &opbuf, ctx );
1948 op->o_bd = li->li_db;
1949 op->o_dn = li->li_db->be_rootdn;
1950 op->o_ndn = li->li_db->be_rootndn;
1951 rc = be_entry_get_rw( op, li->li_db->be_nsuffix, NULL, NULL, 0, &e );
1954 be_entry_release_rw( op, e, 0 );
1957 SlapReply rs = {REP_RESULT};
1958 struct berval rdn, nrdn, attr;
1960 AttributeDescription *ad = NULL;
1961 const char *text = NULL;
1965 ber_dupbv( &e->e_name, li->li_db->be_suffix );
1966 ber_dupbv( &e->e_nname, li->li_db->be_nsuffix );
1968 attr_merge_one( e, slap_schema.si_ad_objectClass,
1969 &log_container->soc_cname, NULL );
1971 dnRdn( &e->e_name, &rdn );
1972 dnRdn( &e->e_nname, &nrdn );
1973 ptr = ber_bvchr( &rdn, '=' );
1975 assert( ptr != NULL );
1977 attr.bv_val = rdn.bv_val;
1978 attr.bv_len = ptr - rdn.bv_val;
1980 slap_bv2ad( &attr, &ad, &text );
1983 rdn.bv_len -= attr.bv_len + 1;
1984 ptr = ber_bvchr( &nrdn, '=' );
1985 nrdn.bv_len -= ptr - nrdn.bv_val + 1;
1986 nrdn.bv_val = ptr+1;
1987 attr_merge_one( e, ad, &rdn, &nrdn );
1989 /* Get contextCSN from main DB */
1990 op->o_bd = on->on_info->oi_origdb;
1991 rc = be_entry_get_rw( op, op->o_bd->be_nsuffix, NULL,
1992 slap_schema.si_ad_contextCSN, 0, &e_ctx );
1997 a = attr_find( e_ctx->e_attrs, slap_schema.si_ad_contextCSN );
1999 /* FIXME: contextCSN could have multiple values!
2000 * should select the one with the server's SID */
2001 attr_merge_one( e, slap_schema.si_ad_entryCSN,
2002 &a->a_vals[0], &a->a_nvals[0] );
2003 attr_merge( e, a->a_desc, a->a_vals, a->a_nvals );
2005 be_entry_release_rw( op, e_ctx, 0 );
2007 op->o_bd = li->li_db;
2010 op->o_req_dn = e->e_name;
2011 op->o_req_ndn = e->e_nname;
2012 op->o_callback = &nullsc;
2013 SLAP_DBFLAGS( op->o_bd ) |= SLAP_DBFLAG_NOLASTMOD;
2014 rc = op->o_bd->be_add( op, &rs );
2015 SLAP_DBFLAGS( op->o_bd ) ^= SLAP_DBFLAG_NOLASTMOD;
2016 if ( e == op->ora_e )
2019 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2020 ldap_pvt_runqueue_stoptask( &slapd_rq, rtask );
2021 ldap_pvt_runqueue_remove( &slapd_rq, rtask );
2022 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2033 slap_overinst *on = (slap_overinst *)be->bd_info;
2034 log_info *li = on->on_bi.bi_private;
2037 if ( !BER_BVISEMPTY( &li->li_db_suffix )) {
2038 li->li_db = select_backend( &li->li_db_suffix, 0 );
2039 ch_free( li->li_db_suffix.bv_val );
2040 BER_BVZERO( &li->li_db_suffix );
2042 if ( li->li_db == NULL ) {
2043 Debug( LDAP_DEBUG_ANY,
2044 "accesslog: \"logdb <suffix>\" missing or invalid.\n",
2049 if ( slapMode & SLAP_TOOL_MODE )
2052 if ( BER_BVISEMPTY( &li->li_db->be_rootndn )) {
2053 ber_dupbv( &li->li_db->be_rootdn, li->li_db->be_suffix );
2054 ber_dupbv( &li->li_db->be_rootndn, li->li_db->be_nsuffix );
2057 ldap_pvt_thread_mutex_lock( &slapd_rq.rq_mutex );
2058 ldap_pvt_runqueue_insert( &slapd_rq, 3600, accesslog_db_root, on,
2059 "accesslog_db_root", li->li_db->be_suffix[0].bv_val );
2060 ldap_pvt_thread_mutex_unlock( &slapd_rq.rq_mutex );
2065 int accesslog_initialize()
2069 accesslog.on_bi.bi_type = "accesslog";
2070 accesslog.on_bi.bi_db_init = accesslog_db_init;
2071 accesslog.on_bi.bi_db_destroy = accesslog_db_destroy;
2072 accesslog.on_bi.bi_db_open = accesslog_db_open;
2074 accesslog.on_bi.bi_op_add = accesslog_op_mod;
2075 accesslog.on_bi.bi_op_bind = accesslog_op_bind;
2076 accesslog.on_bi.bi_op_delete = accesslog_op_mod;
2077 accesslog.on_bi.bi_op_modify = accesslog_op_mod;
2078 accesslog.on_bi.bi_op_modrdn = accesslog_op_mod;
2079 accesslog.on_bi.bi_op_unbind = accesslog_unbind;
2080 accesslog.on_bi.bi_op_abandon = accesslog_abandon;
2081 accesslog.on_bi.bi_operational = accesslog_operational;
2082 accesslog.on_response = accesslog_response;
2084 accesslog.on_bi.bi_cf_ocs = log_cfocs;
2086 nullsc.sc_response = slap_null_cb;
2088 rc = config_register_schema( log_cfats, log_cfocs );
2089 if ( rc ) return rc;
2091 /* log schema integration */
2092 for ( i=0; lsyntaxes[i].oid; i++ ) {
2095 code = register_syntax( &lsyntaxes[ i ].syn );
2097 Debug( LDAP_DEBUG_ANY,
2098 "accesslog_init: register_syntax failed\n",
2103 if ( lsyntaxes[i].mrs != NULL ) {
2104 code = mr_make_syntax_compat_with_mrs(
2105 lsyntaxes[i].oid, lsyntaxes[i].mrs );
2107 Debug( LDAP_DEBUG_ANY,
2109 "mr_make_syntax_compat_with_mrs "
2117 for ( i=0; lattrs[i].at; i++ ) {
2120 code = register_at( lattrs[i].at, lattrs[i].ad, 0 );
2122 Debug( LDAP_DEBUG_ANY,
2123 "accesslog_init: register_at failed\n",
2128 (*lattrs[i].ad)->ad_type->sat_flags |= SLAP_AT_HIDE;
2132 for ( i=0; locs[i].ot; i++ ) {
2135 code = register_oc( locs[i].ot, locs[i].oc, 0 );
2137 Debug( LDAP_DEBUG_ANY,
2138 "accesslog_init: register_oc failed\n",
2143 (*locs[i].oc)->soc_flags |= SLAP_OC_HIDE;
2147 return overlay_register(&accesslog);
2150 #if SLAPD_OVER_ACCESSLOG == SLAPD_MOD_DYNAMIC
2152 init_module( int argc, char *argv[] )
2154 return accesslog_initialize();
2158 #endif /* SLAPD_OVER_ACCESSLOG */