]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/auditlog.c
cleanup
[openldap] / servers / slapd / overlays / auditlog.c
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-2007 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 "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           "SYNTAX OMsDirectoryString )", NULL, NULL },
47         { NULL, NULL, 0, 0, 0, ARG_IGNORED }
48 };
49
50 static ConfigOCs auditlogocs[] = {
51         { "( OLcfgOvOc:15.1 "
52           "NAME 'olcAuditlogConfig' "
53           "DESC 'Auditlog configuration' "
54           "SUP olcOverlayConfig "
55           "MAY ( olcAuditlogFile ) )",
56           Cft_Overlay, auditlogcfg },
57         { NULL, 0, NULL }
58 };
59
60 static int fprint_ldif(FILE *f, char *name, char *val, ber_len_t len) {
61         char *s;
62         if((s = ldif_put(LDIF_PUT_VALUE, name, val, len)) == NULL)
63                 return(-1);
64         fputs(s, f);
65         ber_memfree(s);
66         return(0);
67 }
68
69 static int auditlog_response(Operation *op, SlapReply *rs) {
70         slap_overinst *on = (slap_overinst *)op->o_bd->bd_info;
71         auditlog_data *ad = on->on_bi.bi_private;
72         FILE *f;
73         Attribute *a;
74         Modifications *m;
75         struct berval *b, *who = NULL;
76         char *what, *suffix;
77         long stamp = slap_get_time();
78         int i;
79
80         if ( rs->sr_err != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
81
82         if ( !ad->ad_logfile ) return SLAP_CB_CONTINUE;
83
84 /*
85 ** add or modify: use modifiersName if present
86 **
87 */
88         switch(op->o_tag) {
89                 case LDAP_REQ_MODRDN:   what = "modrdn";        break;
90                 case LDAP_REQ_DELETE:   what = "delete";        break;
91                 case LDAP_REQ_ADD:
92                         what = "add";
93                         for(a = op->ora_e->e_attrs; a; a = a->a_next)
94                                 if( a->a_desc == slap_schema.si_ad_modifiersName ) {
95                                         who = &a->a_vals[0];
96                                         break;
97                                 }
98                         break;
99                 case LDAP_REQ_MODIFY:
100                         what = "modify";
101                         for(m = op->orm_modlist; m; m = m->sml_next)
102                                 if( m->sml_desc == slap_schema.si_ad_modifiersName &&
103                                         ( m->sml_op == LDAP_MOD_ADD ||
104                                         m->sml_op == LDAP_MOD_REPLACE )) {
105                                         who = &m->sml_values[0];
106                                         break;
107                                 }
108                         break;
109                 default:
110                         return SLAP_CB_CONTINUE;
111         }
112
113         suffix = op->o_bd->be_suffix[0].bv_len ? op->o_bd->be_suffix[0].bv_val :
114                 "global";
115
116 /*
117 ** note: this means requestor's dn when modifiersName is null
118 */
119         if ( !who )
120                 who = &op->o_dn;
121
122         ldap_pvt_thread_mutex_lock(&ad->ad_mutex);
123         if((f = fopen(ad->ad_logfile, "a")) == NULL) {
124                 ldap_pvt_thread_mutex_unlock(&ad->ad_mutex);
125                 return SLAP_CB_CONTINUE;
126         }
127
128         fprintf(f, "# %s %ld %s%s%s\n",
129                 what, stamp, suffix, who ? " " : "", who ? who->bv_val : "");
130
131         if ( !BER_BVISEMPTY( &op->o_conn->c_dn ) &&
132                 (!who || !dn_match( who, &op->o_conn->c_dn )))
133                 fprintf(f, "# realdn: %s\n", op->o_conn->c_dn.bv_val );
134
135         fprintf(f, "dn: %s\nchangetype: %s\n",
136                 op->o_req_dn.bv_val, what);
137
138         switch(op->o_tag) {
139           case LDAP_REQ_ADD:
140                 for(a = op->ora_e->e_attrs; a; a = a->a_next)
141                   if((b = a->a_vals) != NULL)
142                         for(i = 0; b[i].bv_val; i++)
143                                 fprint_ldif(f, a->a_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
144                 break;
145
146           case LDAP_REQ_MODIFY:
147                 for(m = op->orm_modlist; m; m = m->sml_next) {
148                         switch(m->sml_op & LDAP_MOD_OP) {
149                                 case LDAP_MOD_ADD:       what = "add";          break;
150                                 case LDAP_MOD_REPLACE:   what = "replace";      break;
151                                 case LDAP_MOD_DELETE:    what = "delete";       break;
152                                 case LDAP_MOD_INCREMENT: what = "increment";    break;
153                                 default:
154                                         fprintf(f, "# MOD_TYPE_UNKNOWN:%02x\n", m->sml_op & LDAP_MOD_OP);
155                                         continue;
156                         }
157                         fprintf(f, "%s: %s\n", what, m->sml_desc->ad_cname.bv_val);
158                         if((b = m->sml_values) != NULL)
159                           for(i = 0; b[i].bv_val; i++)
160                                 fprint_ldif(f, m->sml_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
161                         fprintf(f, "-\n");
162                 }
163                 break;
164
165           case LDAP_REQ_MODRDN:
166                 fprintf(f, "newrdn: %s\ndeleteoldrdn: %s\n",
167                         op->orr_newrdn.bv_val, op->orr_deleteoldrdn ? "1" : "0");
168                 if(op->orr_newSup) fprintf(f, "newsuperior: %s\n", op->orr_newSup->bv_val);
169                 break;
170
171           case LDAP_REQ_DELETE:
172                 /* nothing else needed */
173                 break;
174         }
175
176         fprintf(f, "# end %s %ld\n\n", what, stamp);
177
178         fclose(f);
179         ldap_pvt_thread_mutex_unlock(&ad->ad_mutex);
180         return SLAP_CB_CONTINUE;
181 }
182
183 static slap_overinst auditlog;
184
185 static int
186 auditlog_db_init(
187         BackendDB *be
188 )
189 {
190         slap_overinst *on = (slap_overinst *)be->bd_info;
191         auditlog_data *ad = ch_calloc(1, sizeof(auditlog_data));
192
193         on->on_bi.bi_private = ad;
194         ldap_pvt_thread_mutex_init( &ad->ad_mutex );
195         return 0;
196 }
197
198 static int
199 auditlog_db_close(
200         BackendDB *be
201 )
202 {
203         slap_overinst *on = (slap_overinst *)be->bd_info;
204         auditlog_data *ad = on->on_bi.bi_private;
205
206         free( ad->ad_logfile );
207         ad->ad_logfile = NULL;
208         return 0;
209 }
210
211 static int
212 auditlog_db_destroy(
213         BackendDB *be
214 )
215 {
216         slap_overinst *on = (slap_overinst *)be->bd_info;
217         auditlog_data *ad = on->on_bi.bi_private;
218
219         ldap_pvt_thread_mutex_destroy( &ad->ad_mutex );
220         free( ad );
221         return 0;
222 }
223
224 static int
225 auditlog_config(
226         BackendDB       *be,
227         const char      *fname,
228         int             lineno,
229         int             argc,
230         char    **argv
231 )
232 {
233         slap_overinst *on = (slap_overinst *) be->bd_info;
234         auditlog_data *ad = on->on_bi.bi_private;
235
236         /* history log file */
237         if ( strcasecmp( argv[0], "auditlog" ) == 0 ) {
238                 if ( argc < 2 ) {
239                         Debug( LDAP_DEBUG_ANY,
240             "%s: line %d: missing filename in \"auditlog <filename>\" line\n",
241                             fname, lineno, 0 );
242                                 return( 1 );
243                 }
244                 ad->ad_logfile = ch_strdup( argv[1] );
245                 return 0;
246         }
247         return SLAP_CONF_UNKNOWN;
248 }
249
250 int auditlog_initialize() {
251         int rc;
252
253         auditlog.on_bi.bi_type = "auditlog";
254         auditlog.on_bi.bi_db_init = auditlog_db_init;
255         auditlog.on_bi.bi_db_close = auditlog_db_close;
256         auditlog.on_bi.bi_db_destroy = auditlog_db_destroy;
257         auditlog.on_response = auditlog_response;
258
259         auditlog.on_bi.bi_cf_ocs = auditlogocs;
260         rc = config_register_schema( auditlogcfg, auditlogocs );
261         if ( rc ) return rc;
262
263         return overlay_register(&auditlog);
264 }
265
266 #if SLAPD_OVER_AUDITLOG == SLAPD_MOD_DYNAMIC && defined(PIC)
267 int
268 init_module( int argc, char *argv[] )
269 {
270         return auditlog_initialize();
271 }
272 #endif
273
274 #endif /* SLAPD_OVER_AUDITLOG */