| 1 |
/* auditlog.c - log modifications for audit/history purposes */ |
| 2 |
/* $OpenLDAP$ */ |
| 3 |
/* This work is part of OpenLDAP Software <http://www.openldap.org/>. |
| 4 |
* |
| 5 |
* Copyright 2005-2024 The OpenLDAP Foundation. |
| 6 |
* Portions copyright 2004-2005 Symas Corporation. |
| 7 |
* All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted only as authorized by the OpenLDAP |
| 11 |
* Public License. |
| 12 |
* |
| 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>. |
| 16 |
*/ |
| 17 |
/* ACKNOWLEDGEMENTS: |
| 18 |
* This work was initially developed by Symas Corp. for inclusion in |
| 19 |
* OpenLDAP Software. This work was sponsored by Hewlett-Packard. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "portable.h" |
| 23 |
|
| 24 |
#ifdef SLAPD_OVER_AUDITLOG |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
|
| 28 |
#include <ac/string.h> |
| 29 |
#include <ac/ctype.h> |
| 30 |
|
| 31 |
#include "slap.h" |
| 32 |
#include "slap-config.h" |
| 33 |
#include "ldif.h" |
| 34 |
|
| 35 |
typedef struct auditlog_data { |
| 36 |
ldap_pvt_thread_mutex_t ad_mutex; |
| 37 |
char *ad_logfile; |
| 38 |
} auditlog_data; |
| 39 |
|
| 40 |
static ConfigTable auditlogcfg[] = { |
| 41 |
{ "auditlog", "filename", 2, 2, 0, |
| 42 |
ARG_STRING|ARG_OFFSET, |
| 43 |
(void *)offsetof(auditlog_data, ad_logfile), |
| 44 |
"( OLcfgOvAt:15.1 NAME 'olcAuditlogFile' " |
| 45 |
"DESC 'Filename for auditlogging' " |
| 46 |
"EQUALITY caseExactMatch " |
| 47 |
"SYNTAX OMsDirectoryString )", NULL, NULL }, |
| 48 |
{ NULL, NULL, 0, 0, 0, ARG_IGNORED } |
| 49 |
}; |
| 50 |
|
| 51 |
static ConfigOCs auditlogocs[] = { |
| 52 |
{ "( OLcfgOvOc:15.1 " |
| 53 |
"NAME 'olcAuditlogConfig' " |
| 54 |
"DESC 'Auditlog configuration' " |
| 55 |
"SUP olcOverlayConfig " |
| 56 |
"MAY ( olcAuditlogFile ) )", |
| 57 |
Cft_Overlay, auditlogcfg }, |
| 58 |
{ NULL, 0, NULL } |
| 59 |
}; |
| 60 |
|
| 61 |
static int fprint_ldif(FILE *f, char *name, char *val, ber_len_t len) { |
| 62 |
char *s; |
| 63 |
if((s = ldif_put(LDIF_PUT_VALUE, name, val, len)) == NULL) |
| 64 |
return(-1); |
| 65 |
fputs(s, f); |
| 66 |
ber_memfree(s); |
| 67 |
return(0); |
| 68 |
} |
| 69 |
|
| 70 |
static int auditlog_response(Operation *op, SlapReply *rs) { |
| 71 |
slap_overinst *on = (slap_overinst *)op->o_bd->bd_info; |
| 72 |
auditlog_data *ad = on->on_bi.bi_private; |
| 73 |
FILE *f; |
| 74 |
Attribute *a; |
| 75 |
Modifications *m; |
| 76 |
struct berval *b, *who = NULL, peername; |
| 77 |
char *what, *whatm, *suffix; |
| 78 |
time_t stamp; |
| 79 |
int i; |
| 80 |
|
| 81 |
if ( rs->sr_err != LDAP_SUCCESS ) return SLAP_CB_CONTINUE; |
| 82 |
|
| 83 |
if ( !ad->ad_logfile ) return SLAP_CB_CONTINUE; |
| 84 |
|
| 85 |
/* |
| 86 |
** add or modify: use modifiersName if present |
| 87 |
** |
| 88 |
*/ |
| 89 |
switch(op->o_tag) { |
| 90 |
case LDAP_REQ_MODRDN: what = "modrdn"; break; |
| 91 |
case LDAP_REQ_DELETE: what = "delete"; break; |
| 92 |
case LDAP_REQ_ADD: |
| 93 |
what = "add"; |
| 94 |
for(a = op->ora_e->e_attrs; a; a = a->a_next) |
| 95 |
if( a->a_desc == slap_schema.si_ad_modifiersName ) { |
| 96 |
who = &a->a_vals[0]; |
| 97 |
break; |
| 98 |
} |
| 99 |
break; |
| 100 |
case LDAP_REQ_MODIFY: |
| 101 |
what = "modify"; |
| 102 |
for(m = op->orm_modlist; m; m = m->sml_next) |
| 103 |
if( m->sml_desc == slap_schema.si_ad_modifiersName && |
| 104 |
( m->sml_op == LDAP_MOD_ADD || |
| 105 |
m->sml_op == LDAP_MOD_REPLACE )) { |
| 106 |
who = &m->sml_values[0]; |
| 107 |
break; |
| 108 |
} |
| 109 |
break; |
| 110 |
default: |
| 111 |
return SLAP_CB_CONTINUE; |
| 112 |
} |
| 113 |
|
| 114 |
suffix = op->o_bd->be_suffix[0].bv_len ? op->o_bd->be_suffix[0].bv_val : |
| 115 |
"global"; |
| 116 |
|
| 117 |
/* |
| 118 |
** note: this means requestor's dn when modifiersName is null |
| 119 |
*/ |
| 120 |
if ( !who ) |
| 121 |
who = &op->o_dn; |
| 122 |
|
| 123 |
peername = op->o_conn->c_peer_name; |
| 124 |
ldap_pvt_thread_mutex_lock(&ad->ad_mutex); |
| 125 |
if((f = fopen(ad->ad_logfile, "a")) == NULL) { |
| 126 |
ldap_pvt_thread_mutex_unlock(&ad->ad_mutex); |
| 127 |
return SLAP_CB_CONTINUE; |
| 128 |
} |
| 129 |
|
| 130 |
stamp = slap_get_time(); |
| 131 |
fprintf(f, "# %s %ld %s%s%s %s conn=%ld\n", |
| 132 |
what, (long)stamp, suffix, who ? " " : "", who ? who->bv_val : "", |
| 133 |
peername.bv_val ? peername.bv_val: "", op->o_conn->c_connid); |
| 134 |
|
| 135 |
if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) && |
| 136 |
(!who || !dn_match( who, &op->o_conn->c_dn ))) |
| 137 |
fprintf(f, "# realdn: %s\n", op->o_conn->c_dn.bv_val ); |
| 138 |
|
| 139 |
fprintf(f, "dn: %s\nchangetype: %s\n", |
| 140 |
op->o_req_dn.bv_val, what); |
| 141 |
|
| 142 |
switch(op->o_tag) { |
| 143 |
case LDAP_REQ_ADD: |
| 144 |
for(a = op->ora_e->e_attrs; a; a = a->a_next) |
| 145 |
if((b = a->a_vals) != NULL) |
| 146 |
for(i = 0; b[i].bv_val; i++) |
| 147 |
fprint_ldif(f, a->a_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len); |
| 148 |
break; |
| 149 |
|
| 150 |
case LDAP_REQ_MODIFY: |
| 151 |
for(m = op->orm_modlist; m; m = m->sml_next) { |
| 152 |
switch(m->sml_op & LDAP_MOD_OP) { |
| 153 |
case LDAP_MOD_ADD: whatm = "add"; break; |
| 154 |
case LDAP_MOD_REPLACE: whatm = "replace"; break; |
| 155 |
case LDAP_MOD_DELETE: whatm = "delete"; break; |
| 156 |
case LDAP_MOD_INCREMENT: whatm = "increment"; break; |
| 157 |
default: |
| 158 |
fprintf(f, "# MOD_TYPE_UNKNOWN:%02x\n", m->sml_op & LDAP_MOD_OP); |
| 159 |
continue; |
| 160 |
} |
| 161 |
fprintf(f, "%s: %s\n", whatm, m->sml_desc->ad_cname.bv_val); |
| 162 |
if((b = m->sml_values) != NULL) |
| 163 |
for(i = 0; b[i].bv_val; i++) |
| 164 |
fprint_ldif(f, m->sml_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len); |
| 165 |
fprintf(f, "-\n"); |
| 166 |
} |
| 167 |
break; |
| 168 |
|
| 169 |
case LDAP_REQ_MODRDN: |
| 170 |
fprintf(f, "newrdn: %s\ndeleteoldrdn: %s\n", |
| 171 |
op->orr_newrdn.bv_val, op->orr_deleteoldrdn ? "1" : "0"); |
| 172 |
if(op->orr_newSup) fprintf(f, "newsuperior: %s\n", op->orr_newSup->bv_val); |
| 173 |
break; |
| 174 |
|
| 175 |
case LDAP_REQ_DELETE: |
| 176 |
/* nothing else needed */ |
| 177 |
break; |
| 178 |
} |
| 179 |
|
| 180 |
fprintf(f, "# end %s %ld\n\n", what, (long)stamp); |
| 181 |
|
| 182 |
fclose(f); |
| 183 |
ldap_pvt_thread_mutex_unlock(&ad->ad_mutex); |
| 184 |
return SLAP_CB_CONTINUE; |
| 185 |
} |
| 186 |
|
| 187 |
static slap_overinst auditlog; |
| 188 |
|
| 189 |
static int |
| 190 |
auditlog_db_init( |
| 191 |
BackendDB *be, |
| 192 |
ConfigReply *cr |
| 193 |
) |
| 194 |
{ |
| 195 |
slap_overinst *on = (slap_overinst *)be->bd_info; |
| 196 |
auditlog_data *ad = ch_calloc(1, sizeof(auditlog_data)); |
| 197 |
|
| 198 |
on->on_bi.bi_private = ad; |
| 199 |
ldap_pvt_thread_mutex_init( &ad->ad_mutex ); |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int |
| 204 |
auditlog_db_destroy( |
| 205 |
BackendDB *be, |
| 206 |
ConfigReply *cr |
| 207 |
) |
| 208 |
{ |
| 209 |
slap_overinst *on = (slap_overinst *)be->bd_info; |
| 210 |
auditlog_data *ad = on->on_bi.bi_private; |
| 211 |
|
| 212 |
ldap_pvt_thread_mutex_destroy( &ad->ad_mutex ); |
| 213 |
free( ad->ad_logfile ); |
| 214 |
free( ad ); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
int auditlog_initialize() { |
| 219 |
int rc; |
| 220 |
|
| 221 |
auditlog.on_bi.bi_type = "auditlog"; |
| 222 |
auditlog.on_bi.bi_flags = SLAPO_BFLAG_SINGLE; |
| 223 |
auditlog.on_bi.bi_db_init = auditlog_db_init; |
| 224 |
auditlog.on_bi.bi_db_destroy = auditlog_db_destroy; |
| 225 |
auditlog.on_response = auditlog_response; |
| 226 |
|
| 227 |
auditlog.on_bi.bi_cf_ocs = auditlogocs; |
| 228 |
rc = config_register_schema( auditlogcfg, auditlogocs ); |
| 229 |
if ( rc ) return rc; |
| 230 |
|
| 231 |
return overlay_register(&auditlog); |
| 232 |
} |
| 233 |
|
| 234 |
#if SLAPD_OVER_AUDITLOG == SLAPD_MOD_DYNAMIC && defined(PIC) |
| 235 |
int |
| 236 |
init_module( int argc, char *argv[] ) |
| 237 |
{ |
| 238 |
return auditlog_initialize(); |
| 239 |
} |
| 240 |
#endif |
| 241 |
|
| 242 |
#endif /* SLAPD_OVER_AUDITLOG */ |