1 /* auditlog.c - log modifications for audit/history purposes */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2005-2006 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 Symas Corp. for inclusion in
19 * OpenLDAP Software. This work was sponsored by Hewlett-Packard.
24 #ifdef SLAPD_OVER_AUDITLOG
28 #include <ac/string.h>
34 typedef struct auditlog_data {
35 ldap_pvt_thread_mutex_t ad_mutex;
39 static int fprint_ldif(FILE *f, char *name, char *val, ber_len_t len) {
41 if((s = ldif_put(LDIF_PUT_VALUE, name, val, len)) == NULL)
48 static int auditlog_response(Operation *op, SlapReply *rs) {
49 slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
50 auditlog_data *ad = on->on_bi.bi_private;
54 struct berval *b, *who = NULL;
56 long stamp = slap_get_time();
59 if ( rs->sr_err != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
61 if ( !ad->ad_logfile ) return SLAP_CB_CONTINUE;
64 ** add or modify: use modifiersName if present
68 case LDAP_REQ_MODRDN: what = "modrdn"; break;
69 case LDAP_REQ_DELETE: what = "delete"; break;
72 for(a = op->ora_e->e_attrs; a; a = a->a_next)
73 if( a->a_desc == slap_schema.si_ad_modifiersName ) {
80 for(m = op->orm_modlist; m; m = m->sml_next)
81 if( m->sml_desc == slap_schema.si_ad_modifiersName &&
82 ( m->sml_op == LDAP_MOD_ADD ||
83 m->sml_op == LDAP_MOD_REPLACE )) {
84 who = &m->sml_values[0];
89 return SLAP_CB_CONTINUE;
92 suffix = op->o_bd->be_suffix[0].bv_len ? op->o_bd->be_suffix[0].bv_val :
96 ** note: this means requestor's dn when modifiersName is null
101 ldap_pvt_thread_mutex_lock(&ad->ad_mutex);
102 if((f = fopen(ad->ad_logfile, "a")) == NULL) {
103 ldap_pvt_thread_mutex_unlock(&ad->ad_mutex);
104 return SLAP_CB_CONTINUE;
107 fprintf(f, "# %s %ld %s%s%s\n",
108 what, stamp, suffix, who ? " " : "", who ? who->bv_val : "");
110 if ( !who || !dn_match( who, &op->o_conn->c_dn ))
111 fprintf(f, "# realdn: %s\n", op->o_conn->c_dn.bv_val ? op->o_conn->c_dn.bv_val :
114 fprintf(f, "dn: %s\nchangetype: %s\n",
115 op->o_req_dn.bv_val, what);
119 for(a = op->ora_e->e_attrs; a; a = a->a_next)
120 if((b = a->a_vals) != NULL)
121 for(i = 0; b[i].bv_val; i++)
122 fprint_ldif(f, a->a_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
125 case LDAP_REQ_MODIFY:
126 for(m = op->orm_modlist; m; m = m->sml_next) {
127 switch(m->sml_op & LDAP_MOD_OP) {
128 case LDAP_MOD_ADD: what = "add"; break;
129 case LDAP_MOD_REPLACE: what = "replace"; break;
130 case LDAP_MOD_DELETE: what = "delete"; break;
131 case LDAP_MOD_INCREMENT: what = "increment"; break;
133 fprintf(f, "# MOD_TYPE_UNKNOWN:%02x\n", m->sml_op & LDAP_MOD_OP);
136 fprintf(f, "%s: %s\n", what, m->sml_desc->ad_cname.bv_val);
137 if((b = m->sml_values) != NULL)
138 for(i = 0; b[i].bv_val; i++)
139 fprint_ldif(f, m->sml_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
144 case LDAP_REQ_MODRDN:
145 fprintf(f, "newrdn: %s\ndeleteoldrdn: %s\n",
146 op->orr_newrdn.bv_val, op->orr_deleteoldrdn ? "1" : "0");
147 if(op->orr_newSup) fprintf(f, "newsuperior: %s\n", op->orr_newSup->bv_val);
150 case LDAP_REQ_DELETE:
151 /* nothing else needed */
155 fprintf(f, "# end %s %ld\n\n", what, stamp);
158 ldap_pvt_thread_mutex_unlock(&ad->ad_mutex);
159 return SLAP_CB_CONTINUE;
162 static slap_overinst auditlog;
169 slap_overinst *on = (slap_overinst *)be->bd_info;
170 auditlog_data *ad = ch_malloc(sizeof(auditlog_data));
172 on->on_bi.bi_private = ad;
173 ldap_pvt_thread_mutex_init( &ad->ad_mutex );
182 slap_overinst *on = (slap_overinst *)be->bd_info;
183 auditlog_data *ad = on->on_bi.bi_private;
185 free( ad->ad_logfile );
186 ad->ad_logfile = NULL;
195 slap_overinst *on = (slap_overinst *)be->bd_info;
196 auditlog_data *ad = on->on_bi.bi_private;
198 ldap_pvt_thread_mutex_destroy( &ad->ad_mutex );
212 slap_overinst *on = (slap_overinst *) be->bd_info;
213 auditlog_data *ad = on->on_bi.bi_private;
215 /* history log file */
216 if ( strcasecmp( argv[0], "auditlog" ) == 0 ) {
218 Debug( LDAP_DEBUG_ANY,
219 "%s: line %d: missing filename in \"auditlog <filename>\" line\n",
223 ad->ad_logfile = ch_strdup( argv[1] );
226 return SLAP_CONF_UNKNOWN;
229 int auditlog_initialize() {
231 auditlog.on_bi.bi_type = "auditlog";
232 auditlog.on_bi.bi_db_init = auditlog_db_init;
233 auditlog.on_bi.bi_db_config = auditlog_config;
234 auditlog.on_bi.bi_db_close = auditlog_db_close;
235 auditlog.on_bi.bi_db_destroy = auditlog_db_destroy;
236 auditlog.on_response = auditlog_response;
238 return overlay_register(&auditlog);
241 #if SLAPD_OVER_AUDITLOG == SLAPD_MOD_DYNAMIC && defined(PIC)
243 init_module( int argc, char *argv[] )
245 return auditlog_initialize();
249 #endif /* SLAPD_OVER_AUDITLOG */