]> git.sur5r.net Git - openldap/blob - servers/slapd/overlays/auditlog.c
cc67c2b950b55dd2afedddc64cda5eaac52ec5ac
[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-2006 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 "ldif.h"
33
34 typedef struct auditlog_data {
35         ldap_pvt_thread_mutex_t ad_mutex;
36         char *ad_logfile;
37 } auditlog_data;
38
39 static int fprint_ldif(FILE *f, char *name, char *val, ber_len_t len) {
40         char *s;
41         if((s = ldif_put(LDIF_PUT_VALUE, name, val, len)) == NULL)
42                 return(-1);
43         fputs(s, f);
44         ber_memfree(s);
45         return(0);
46 }
47
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;
51         FILE *f;
52         Attribute *a;
53         Modifications *m;
54         struct berval *b;
55         char *what, *suffix, *who = NULL;
56         long stamp = slap_get_time();
57         int i;
58
59         if ( rs->sr_err != LDAP_SUCCESS ) return SLAP_CB_CONTINUE;
60
61         if ( !ad->ad_logfile ) return SLAP_CB_CONTINUE;
62
63 /*
64 ** add or modify: use modifiersName if present
65 **
66 */
67         switch(op->o_tag) {
68                 case LDAP_REQ_MODRDN:   what = "modrdn";        break;
69                 case LDAP_REQ_DELETE:   what = "delete";        break;
70                 case LDAP_REQ_ADD:
71                         what = "add";
72                         for(a = op->ora_e->e_attrs; a; a = a->a_next)
73                                 if( a->a_desc == slap_schema.si_ad_modifiersName ) {
74                                         who = a->a_vals[0].bv_val;
75                                         break;
76                                 }
77                         break;
78                 case LDAP_REQ_MODIFY:
79                         what = "modify";
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].bv_val;
85                                         break;
86                                 }
87                         break;
88                 default:
89                         return SLAP_CB_CONTINUE;
90         }
91
92         suffix = op->o_bd->be_suffix[0].bv_len ? op->o_bd->be_suffix[0].bv_val :
93                 "global";
94
95 /*
96 ** note: this means requestor's dn when modifiersName is null
97 */
98         if ( !who )
99                 who = op->o_dn.bv_val;
100
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;
105         }
106
107         fprintf(f, "# %s %ld %s%s%s\ndn: %s\nchangetype: %s\n",
108                 what, stamp, suffix, who ? " " : "", who ? who : "",
109                 op->o_req_dn.bv_val, what);
110
111         switch(op->o_tag) {
112           case LDAP_REQ_ADD:
113                 for(a = op->ora_e->e_attrs; a; a = a->a_next)
114                   if((b = a->a_vals) != NULL)
115                         for(i = 0; b[i].bv_val; i++)
116                                 fprint_ldif(f, a->a_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
117                 break;
118
119           case LDAP_REQ_MODIFY:
120                 for(m = op->orm_modlist; m; m = m->sml_next) {
121                         switch(m->sml_op & LDAP_MOD_OP) {
122                                 case LDAP_MOD_ADD:       what = "add";          break;
123                                 case LDAP_MOD_REPLACE:   what = "replace";      break;
124                                 case LDAP_MOD_DELETE:    what = "delete";       break;
125                                 case LDAP_MOD_INCREMENT: what = "increment";    break;
126                                 default:
127                                         fprintf(f, "# MOD_TYPE_UNKNOWN:%02x\n", m->sml_op & LDAP_MOD_OP);
128                                         continue;
129                         }
130                         fprintf(f, "%s: %s\n", what, m->sml_desc->ad_cname.bv_val);
131                         if((b = m->sml_values) != NULL)
132                           for(i = 0; b[i].bv_val; i++)
133                                 fprint_ldif(f, m->sml_desc->ad_cname.bv_val, b[i].bv_val, b[i].bv_len);
134                         fprintf(f, "-\n");
135                 }
136                 break;
137
138           case LDAP_REQ_MODRDN:
139                 fprintf(f, "newrdn: %s\ndeleteoldrdn: %s\n",
140                         op->orr_newrdn.bv_val, op->orr_deleteoldrdn ? "1" : "0");
141                 if(op->orr_newSup) fprintf(f, "newsuperior: %s\n", op->orr_newSup->bv_val);
142                 break;
143
144           case LDAP_REQ_DELETE:
145                 /* nothing else needed */
146                 break;
147         }
148
149         fprintf(f, "# end %s %ld\n\n", what, stamp);
150
151         fclose(f);
152         ldap_pvt_thread_mutex_unlock(&ad->ad_mutex);
153         return SLAP_CB_CONTINUE;
154 }
155
156 static slap_overinst auditlog;
157
158 static int
159 auditlog_db_init(
160         BackendDB *be
161 )
162 {
163         slap_overinst *on = (slap_overinst *)be->bd_info;
164         auditlog_data *ad = ch_malloc(sizeof(auditlog_data));
165
166         on->on_bi.bi_private = ad;
167         ldap_pvt_thread_mutex_init( &ad->ad_mutex );
168         return 0;
169 }
170
171 static int
172 auditlog_db_close(
173         BackendDB *be
174 )
175 {
176         slap_overinst *on = (slap_overinst *)be->bd_info;
177         auditlog_data *ad = on->on_bi.bi_private;
178
179         free( ad->ad_logfile );
180         ad->ad_logfile = NULL;
181         return 0;
182 }
183
184 static int
185 auditlog_db_destroy(
186         BackendDB *be
187 )
188 {
189         slap_overinst *on = (slap_overinst *)be->bd_info;
190         auditlog_data *ad = on->on_bi.bi_private;
191
192         ldap_pvt_thread_mutex_destroy( &ad->ad_mutex );
193         free( ad );
194         return 0;
195 }
196
197 static int
198 auditlog_config(
199         BackendDB       *be,
200         const char      *fname,
201         int             lineno,
202         int             argc,
203         char    **argv
204 )
205 {
206         slap_overinst *on = (slap_overinst *) be->bd_info;
207         auditlog_data *ad = on->on_bi.bi_private;
208
209         /* history log file */
210         if ( strcasecmp( argv[0], "auditlog" ) == 0 ) {
211                 if ( argc < 2 ) {
212                         Debug( LDAP_DEBUG_ANY,
213             "%s: line %d: missing filename in \"auditlog <filename>\" line\n",
214                             fname, lineno, 0 );
215                                 return( 1 );
216                 }
217                 ad->ad_logfile = ch_strdup( argv[1] );
218                 return 0;
219         }
220         return SLAP_CONF_UNKNOWN;
221 }
222
223 int auditlog_initialize() {
224
225         auditlog.on_bi.bi_type = "auditlog";
226         auditlog.on_bi.bi_db_init = auditlog_db_init;
227         auditlog.on_bi.bi_db_config = auditlog_config;
228         auditlog.on_bi.bi_db_close = auditlog_db_close;
229         auditlog.on_bi.bi_db_destroy = auditlog_db_destroy;
230         auditlog.on_response = auditlog_response;
231
232         return overlay_register(&auditlog);
233 }
234
235 #if SLAPD_OVER_AUDITLOG == SLAPD_MOD_DYNAMIC && defined(PIC)
236 int
237 init_module( int argc, char *argv[] )
238 {
239         return auditlog_initialize();
240 }
241 #endif
242
243 #endif /* SLAPD_OVER_AUDITLOG */