xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/trace/trace.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: trace.c,v 1.1.1.4 2014/05/28 09:58:28 tron 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-2014 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 "portable.h"
24 
25 #ifdef SLAPD_OVER_TRACE
26 
27 #include <stdio.h>
28 
29 #include <ac/string.h>
30 #include <ac/socket.h>
31 
32 #include "slap.h"
33 #include "lutil.h"
34 
35 static int
36 trace_op2str( Operation *op, char **op_strp )
37 {
38 	switch ( op->o_tag ) {
39 	case LDAP_REQ_BIND:
40 		*op_strp = "BIND";
41 		break;
42 
43 	case LDAP_REQ_UNBIND:
44 		*op_strp = "UNBIND";
45 		break;
46 
47 	case LDAP_REQ_SEARCH:
48 		*op_strp = "SEARCH";
49 		break;
50 
51 	case LDAP_REQ_MODIFY:
52 		*op_strp = "MODIFY";
53 		break;
54 
55 	case LDAP_REQ_ADD:
56 		*op_strp = "ADD";
57 		break;
58 
59 	case LDAP_REQ_DELETE:
60 		*op_strp = "DELETE";
61 		break;
62 
63 	case LDAP_REQ_MODRDN:
64 		*op_strp = "MODRDN";
65 		break;
66 
67 	case LDAP_REQ_COMPARE:
68 		*op_strp = "COMPARE";
69 		break;
70 
71 	case LDAP_REQ_ABANDON:
72 		*op_strp = "ABANDON";
73 		break;
74 
75 	case LDAP_REQ_EXTENDED:
76 		*op_strp = "EXTENDED";
77 		break;
78 
79 	default:
80 		assert( 0 );
81 	}
82 
83 	return 0;
84 }
85 
86 static int
87 trace_op_func( Operation *op, SlapReply *rs )
88 {
89 	char	*op_str = NULL;
90 
91 	(void)trace_op2str( op, &op_str );
92 
93 	switch ( op->o_tag ) {
94 	case LDAP_REQ_EXTENDED:
95 		Log3( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
96 			"%s trace op=EXTENDED dn=\"%s\" reqoid=%s\n",
97 			op->o_log_prefix,
98 			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
99 			BER_BVISNULL( &op->ore_reqoid ) ? "" : op->ore_reqoid.bv_val );
100 		break;
101 
102 	default:
103 		Log3( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
104 			"%s trace op=%s dn=\"%s\"\n",
105 			op->o_log_prefix, op_str,
106 			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val );
107 		break;
108 	}
109 
110 	return SLAP_CB_CONTINUE;
111 }
112 
113 static int
114 trace_response( Operation *op, SlapReply *rs )
115 {
116 	char	*op_str = NULL;
117 
118 	(void)trace_op2str( op, &op_str );
119 
120 	switch ( op->o_tag ) {
121 	case LDAP_REQ_EXTENDED:
122 		Log5( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
123 			"%s trace op=EXTENDED RESPONSE dn=\"%s\" reqoid=%s rspoid=%s err=%d\n",
124 			op->o_log_prefix,
125 			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
126 			BER_BVISNULL( &op->ore_reqoid ) ? "" : op->ore_reqoid.bv_val,
127 			rs->sr_rspoid == NULL ? "" : rs->sr_rspoid,
128 			rs->sr_err );
129 		break;
130 
131 	case LDAP_REQ_SEARCH:
132 		switch ( rs->sr_type ) {
133 		case REP_SEARCH:
134 			Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
135 				"%s trace op=SEARCH ENTRY dn=\"%s\"\n",
136 				op->o_log_prefix,
137 				rs->sr_entry->e_name.bv_val );
138 			goto done;
139 
140 		case REP_SEARCHREF:
141 			Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
142 				"%s trace op=SEARCH REFERENCE ref=\"%s\"\n",
143 				op->o_log_prefix,
144 				rs->sr_ref[ 0 ].bv_val );
145 			goto done;
146 
147 		case REP_RESULT:
148 			break;
149 
150 		default:
151 			assert( 0 );
152 		}
153 		/* fallthru */
154 
155 	default:
156 		Log4( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
157 			"%s trace op=%s RESPONSE dn=\"%s\" err=%d\n",
158 			op->o_log_prefix,
159 			op_str,
160 			BER_BVISNULL( &op->o_req_ndn ) ? "(null)" : op->o_req_ndn.bv_val,
161 			rs->sr_err );
162 		break;
163 	}
164 
165 done:;
166 	return SLAP_CB_CONTINUE;
167 }
168 
169 static int
170 trace_db_init(
171 	BackendDB *be )
172 {
173 	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
174 		"trace DB_INIT\n" );
175 
176 	return 0;
177 }
178 
179 static int
180 trace_db_config(
181 	BackendDB	*be,
182 	const char	*fname,
183 	int		lineno,
184 	int		argc,
185 	char		**argv )
186 {
187 	Log2( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
188 		"trace DB_CONFIG argc=%d argv[0]=\"%s\"\n",
189 		argc, argv[ 0 ] );
190 
191 	return 0;
192 }
193 
194 static int
195 trace_db_open(
196 	BackendDB *be )
197 {
198 	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
199 		"trace DB_OPEN\n" );
200 
201 	return 0;
202 }
203 
204 static int
205 trace_db_close(
206 	BackendDB *be )
207 {
208 	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
209 		"trace DB_CLOSE\n" );
210 
211 	return 0;
212 }
213 
214 static int
215 trace_db_destroy(
216 	BackendDB *be )
217 {
218 	Log0( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
219 		"trace DB_DESTROY\n" );
220 
221 	return 0;
222 }
223 
224 static slap_overinst 		trace;
225 
226 int
227 trace_initialize()
228 {
229 	trace.on_bi.bi_type = "trace";
230 
231 	trace.on_bi.bi_db_init = trace_db_init;
232 	trace.on_bi.bi_db_open = trace_db_open;
233 	trace.on_bi.bi_db_config = trace_db_config;
234 	trace.on_bi.bi_db_close = trace_db_close;
235 	trace.on_bi.bi_db_destroy = trace_db_destroy;
236 
237 	trace.on_bi.bi_op_add = trace_op_func;
238 	trace.on_bi.bi_op_bind = trace_op_func;
239 	trace.on_bi.bi_op_unbind = trace_op_func;
240 	trace.on_bi.bi_op_compare = trace_op_func;
241 	trace.on_bi.bi_op_delete = trace_op_func;
242 	trace.on_bi.bi_op_modify = trace_op_func;
243 	trace.on_bi.bi_op_modrdn = trace_op_func;
244 	trace.on_bi.bi_op_search = trace_op_func;
245 	trace.on_bi.bi_op_abandon = trace_op_func;
246 	trace.on_bi.bi_extended = trace_op_func;
247 
248 	trace.on_response = trace_response;
249 
250 	return overlay_register( &trace );
251 }
252 
253 #if SLAPD_OVER_TRACE == SLAPD_MOD_DYNAMIC
254 int
255 init_module( int argc, char *argv[] )
256 {
257 	return trace_initialize();
258 }
259 #endif /* SLAPD_OVER_TRACE == SLAPD_MOD_DYNAMIC */
260 
261 #endif /* defined(SLAPD_OVER_TRACE) */
262