xref: /netbsd-src/external/bsd/openldap/dist/contrib/slapd-modules/trace/trace.c (revision 549b59ed3ccf0d36d3097190a0db27b770f3a839)
1 /*	$NetBSD: trace.c,v 1.3 2021/08/14 16:14:54 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-2021 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.3 2021/08/14 16:14:54 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 
38 static int
trace_op2str(Operation * op,char ** op_strp)39 trace_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 
89 static int
trace_op_func(Operation * op,SlapReply * rs)90 trace_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 		Log( 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 		Log( 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 
116 static int
trace_response(Operation * op,SlapReply * rs)117 trace_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 		Log( 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 			Log( 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 			Log( 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 		Log( 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 
168 done:;
169 	return SLAP_CB_CONTINUE;
170 }
171 
172 static int
trace_db_init(BackendDB * be,ConfigReply * cr)173 trace_db_init( BackendDB *be, ConfigReply *cr )
174 {
175 	Log( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
176 		"trace DB_INIT\n" );
177 
178 	return 0;
179 }
180 
181 static int
trace_db_config(BackendDB * be,const char * fname,int lineno,int argc,char ** argv)182 trace_db_config(
183 	BackendDB	*be,
184 	const char	*fname,
185 	int		lineno,
186 	int		argc,
187 	char		**argv )
188 {
189 	Log( 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 
196 static int
trace_db_open(BackendDB * be,ConfigReply * cr)197 trace_db_open( BackendDB *be, ConfigReply *cr )
198 {
199 	Log( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
200 		"trace DB_OPEN\n" );
201 
202 	return 0;
203 }
204 
205 static int
trace_db_close(BackendDB * be,ConfigReply * cr)206 trace_db_close( BackendDB *be, ConfigReply *cr )
207 {
208 	Log( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
209 		"trace DB_CLOSE\n" );
210 
211 	return 0;
212 }
213 
214 static int
trace_db_destroy(BackendDB * be,ConfigReply * cr)215 trace_db_destroy( BackendDB *be, ConfigReply *cr )
216 {
217 	Log( LDAP_DEBUG_ANY, LDAP_LEVEL_INFO,
218 		"trace DB_DESTROY\n" );
219 
220 	return 0;
221 }
222 
223 static slap_overinst 		trace;
224 
225 int
trace_initialize()226 trace_initialize()
227 {
228 	trace.on_bi.bi_type = "trace";
229 
230 	trace.on_bi.bi_flags = SLAPO_BFLAG_SINGLE;
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
init_module(int argc,char * argv[])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