trace.c revision 1.1.1.6
1/*	$NetBSD: trace.c,v 1.1.1.6 2018/02/06 01:53:06 christos Exp $	*/
2
3/* trace.c - traces overlay invocation */
4/* $OpenLDAP$ */
5/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 2006-2017 The OpenLDAP Foundation.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
12 * Public License.
13 *
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
17 */
18/* ACKNOWLEDGEMENTS:
19 * This work was initially developed by Pierangelo Masarati for inclusion in
20 * OpenLDAP Software.
21 */
22
23#include <sys/cdefs.h>
24__RCSID("$NetBSD: trace.c,v 1.1.1.6 2018/02/06 01:53:06 christos Exp $");
25
26#include "portable.h"
27
28#ifdef SLAPD_OVER_TRACE
29
30#include <stdio.h>
31
32#include <ac/string.h>
33#include <ac/socket.h>
34
35#include "slap.h"
36#include "lutil.h"
37
38static int
39trace_op2str( Operation *op, char **op_strp )
40{
41	switch ( op->o_tag ) {
42	case LDAP_REQ_BIND:
43		*op_strp = "BIND";
44		break;
45
46	case LDAP_REQ_UNBIND:
47		*op_strp = "UNBIND";
48		break;
49
50	case LDAP_REQ_SEARCH:
51		*op_strp = "SEARCH";
52		break;
53
54	case LDAP_REQ_MODIFY:
55		*op_strp = "MODIFY";
56		break;
57
58	case LDAP_REQ_ADD:
59		*op_strp = "ADD";
60		break;
61
62	case LDAP_REQ_DELETE:
63		*op_strp = "DELETE";
64		break;
65
66	case LDAP_REQ_MODRDN:
67		*op_strp = "MODRDN";
68		break;
69
70	case LDAP_REQ_COMPARE:
71		*op_strp = "COMPARE";
72		break;
73
74	case LDAP_REQ_ABANDON:
75		*op_strp = "ABANDON";
76		break;
77
78	case LDAP_REQ_EXTENDED:
79		*op_strp = "EXTENDED";
80		break;
81
82	default:
83		assert( 0 );
84	}
85
86	return 0;
87}
88
89static int
90trace_op_func( Operation *op, SlapReply *rs )
91{
92	char	*op_str = NULL;
93
94	(void)trace_op2str( op, &op_str );
95
96	switch ( op->o_tag ) {
97	case LDAP_REQ_EXTENDED:
98		Log3( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
99			"%s trace op=EXTENDED dn=\"%s\" reqoid=%s\n",
100			op->o_log_prefix,
101			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
102			BER_BVISNULL( &op->ore_reqoid ) ? "" : op->ore_reqoid.bv_val );
103		break;
104
105	default:
106		Log3( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
107			"%s trace op=%s dn=\"%s\"\n",
108			op->o_log_prefix, op_str,
109			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val );
110		break;
111	}
112
113	return SLAP_CB_CONTINUE;
114}
115
116static int
117trace_response( Operation *op, SlapReply *rs )
118{
119	char	*op_str = NULL;
120
121	(void)trace_op2str( op, &op_str );
122
123	switch ( op->o_tag ) {
124	case LDAP_REQ_EXTENDED:
125		Log5( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
126			"%s trace op=EXTENDED RESPONSE dn=\"%s\" reqoid=%s rspoid=%s err=%d\n",
127			op->o_log_prefix,
128			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
129			BER_BVISNULL( &op->ore_reqoid ) ? "" : op->ore_reqoid.bv_val,
130			rs->sr_rspoid == NULL ? "" : rs->sr_rspoid,
131			rs->sr_err );
132		break;
133
134	case LDAP_REQ_SEARCH:
135		switch ( rs->sr_type ) {
136		case REP_SEARCH:
137			Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
138				"%s trace op=SEARCH ENTRY dn=\"%s\"\n",
139				op->o_log_prefix,
140				rs->sr_entry->e_name.bv_val );
141			goto done;
142
143		case REP_SEARCHREF:
144			Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
145				"%s trace op=SEARCH REFERENCE ref=\"%s\"\n",
146				op->o_log_prefix,
147				rs->sr_ref[ 0 ].bv_val );
148			goto done;
149
150		case REP_RESULT:
151			break;
152
153		default:
154			assert( 0 );
155		}
156		/* fallthru */
157
158	default:
159		Log4( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
160			"%s trace op=%s RESPONSE dn=\"%s\" err=%d\n",
161			op->o_log_prefix,
162			op_str,
163			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
164			rs->sr_err );
165		break;
166	}
167
168done:;
169	return SLAP_CB_CONTINUE;
170}
171
172static int
173trace_db_init( BackendDB *be, ConfigReply *cr )
174{
175	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
176		"trace DB_INIT\n" );
177
178	return 0;
179}
180
181static int
182trace_db_config(
183	BackendDB	*be,
184	const char	*fname,
185	int		lineno,
186	int		argc,
187	char		**argv )
188{
189	Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
190		"trace DB_CONFIG argc=%d argv[0]=\"%s\"\n",
191		argc, argv[ 0 ] );
192
193	return 0;
194}
195
196static int
197trace_db_open( BackendDB *be, ConfigReply *cr )
198{
199	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
200		"trace DB_OPEN\n" );
201
202	return 0;
203}
204
205static int
206trace_db_close( BackendDB *be, ConfigReply *cr )
207{
208	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
209		"trace DB_CLOSE\n" );
210
211	return 0;
212}
213
214static int
215trace_db_destroy( BackendDB *be, ConfigReply *cr )
216{
217	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
218		"trace DB_DESTROY\n" );
219
220	return 0;
221}
222
223static slap_overinst 		trace;
224
225int
226trace_initialize()
227{
228	trace.on_bi.bi_type = "trace";
229
230	trace.on_bi.bi_db_init = trace_db_init;
231	trace.on_bi.bi_db_open = trace_db_open;
232	trace.on_bi.bi_db_config = trace_db_config;
233	trace.on_bi.bi_db_close = trace_db_close;
234	trace.on_bi.bi_db_destroy = trace_db_destroy;
235
236	trace.on_bi.bi_op_add = trace_op_func;
237	trace.on_bi.bi_op_bind = trace_op_func;
238	trace.on_bi.bi_op_unbind = trace_op_func;
239	trace.on_bi.bi_op_compare = trace_op_func;
240	trace.on_bi.bi_op_delete = trace_op_func;
241	trace.on_bi.bi_op_modify = trace_op_func;
242	trace.on_bi.bi_op_modrdn = trace_op_func;
243	trace.on_bi.bi_op_search = trace_op_func;
244	trace.on_bi.bi_op_abandon = trace_op_func;
245	trace.on_bi.bi_extended = trace_op_func;
246
247	trace.on_response = trace_response;
248
249	return overlay_register( &trace );
250}
251
252#if SLAPD_OVER_TRACE == SLAPD_MOD_DYNAMIC
253int
254init_module( int argc, char *argv[] )
255{
256	return trace_initialize();
257}
258#endif /* SLAPD_OVER_TRACE == SLAPD_MOD_DYNAMIC */
259
260#endif /* defined(SLAPD_OVER_TRACE) */
261