GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/ldapctl/../ldapd/logmsg.c Lines: 0 163 0.0 %
Date: 2017-11-13 Branches: 0 98 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: logmsg.c,v 1.1 2017/01/20 11:55:08 benno Exp $	*/
2
3
/*
4
 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15
 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
#include <sys/queue.h>
21
#include <sys/socket.h>
22
#include <sys/time.h>
23
24
#include <errno.h>
25
#include <netdb.h>
26
#include <stdarg.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <syslog.h>
31
#include <time.h>
32
#include <unistd.h>
33
#include <vis.h>
34
35
#include "ldapd.h"
36
#include "log.h"
37
38
extern int	debug;
39
extern int	verbose;
40
41
const char *
42
print_host(struct sockaddr_storage *ss, char *buf, size_t len)
43
{
44
	if (getnameinfo((struct sockaddr *)ss, ss->ss_len,
45
	    buf, len, NULL, 0, NI_NUMERICHOST) != 0) {
46
		buf[0] = '\0';
47
		return (NULL);
48
	}
49
	return (buf);
50
}
51
52
void
53
hexdump(void *data, size_t len, const char *fmt, ...)
54
{
55
	uint8_t *p = data;
56
	va_list ap;
57
58
	if (verbose < 2 || !debug)
59
		return;
60
61
	va_start(ap, fmt);
62
	vlog(LOG_DEBUG, fmt, ap);
63
	va_end(ap);
64
65
	while (len--) {
66
		size_t ofs = p - (uint8_t *)data;
67
		if (ofs % 16 == 0)
68
			fprintf(stderr, "%s%04lx:", ofs == 0 ? "" : "\n", ofs);
69
		else if (ofs % 8 == 0)
70
			fprintf(stderr, " ");
71
		fprintf(stderr, " %02x", *p++);
72
	}
73
	fprintf(stderr, "\n");
74
}
75
76
/*
77
 * Display a list of ber elements.
78
 *
79
 */
80
void
81
ldap_debug_elements(struct ber_element *root, int context, const char *fmt, ...)
82
{
83
	va_list		 ap;
84
	static int	 indent = 0;
85
	long long	 v;
86
	int		 d;
87
	char		*buf, *visbuf;
88
	size_t		 len;
89
	u_int		 i;
90
	int		 constructed;
91
	struct ber_oid	 o;
92
93
	if (verbose < 2 || !debug)
94
		return;
95
96
	if (fmt != NULL) {
97
		va_start(ap, fmt);
98
		vlog(LOG_DEBUG, fmt, ap);
99
		va_end(ap);
100
	}
101
102
	/* calculate lengths */
103
	ber_calc_len(root);
104
105
	switch (root->be_encoding) {
106
	case BER_TYPE_SEQUENCE:
107
	case BER_TYPE_SET:
108
		constructed = root->be_encoding;
109
		break;
110
	default:
111
		constructed = 0;
112
		break;
113
	}
114
115
	fprintf(stderr, "%*slen %lu ", indent, "", root->be_len);
116
	switch (root->be_class) {
117
	case BER_CLASS_UNIVERSAL:
118
		fprintf(stderr, "class: universal(%u) type: ", root->be_class);
119
		switch (root->be_type) {
120
		case BER_TYPE_EOC:
121
			fprintf(stderr, "end-of-content");
122
			break;
123
		case BER_TYPE_BOOLEAN:
124
			fprintf(stderr, "boolean");
125
			break;
126
		case BER_TYPE_INTEGER:
127
			fprintf(stderr, "integer");
128
			break;
129
		case BER_TYPE_BITSTRING:
130
			fprintf(stderr, "bit-string");
131
			break;
132
		case BER_TYPE_OCTETSTRING:
133
			fprintf(stderr, "octet-string");
134
			break;
135
		case BER_TYPE_NULL:
136
			fprintf(stderr, "null");
137
			break;
138
		case BER_TYPE_OBJECT:
139
			fprintf(stderr, "object");
140
			break;
141
		case BER_TYPE_ENUMERATED:
142
			fprintf(stderr, "enumerated");
143
			break;
144
		case BER_TYPE_SEQUENCE:
145
			fprintf(stderr, "sequence");
146
			break;
147
		case BER_TYPE_SET:
148
			fprintf(stderr, "set");
149
			break;
150
		}
151
		break;
152
	case BER_CLASS_APPLICATION:
153
		fprintf(stderr, "class: application(%u) type: ",
154
		    root->be_class);
155
		switch (root->be_type) {
156
		case LDAP_REQ_BIND:
157
		case LDAP_RES_BIND:
158
			fprintf(stderr, "bind");
159
			break;
160
		case LDAP_REQ_UNBIND_30:
161
			fprintf(stderr, "unbind");
162
			break;
163
		case LDAP_REQ_SEARCH:
164
			fprintf(stderr, "search");
165
			break;
166
		case LDAP_RES_SEARCH_ENTRY:
167
			fprintf(stderr, "search entry");
168
			break;
169
		case LDAP_RES_SEARCH_RESULT:
170
			fprintf(stderr, "search result");
171
			break;
172
		case LDAP_REQ_MODIFY:
173
		case LDAP_RES_MODIFY:
174
			fprintf(stderr, "modify");
175
			break;
176
		case LDAP_REQ_ADD:
177
		case LDAP_RES_ADD:
178
			fprintf(stderr, "add");
179
			break;
180
		case LDAP_REQ_DELETE_30:
181
		case LDAP_RES_DELETE:
182
			fprintf(stderr, "delete");
183
			break;
184
		case LDAP_REQ_MODRDN:
185
		case LDAP_RES_MODRDN:
186
			fprintf(stderr, "modrdn");
187
			break;
188
		case LDAP_REQ_COMPARE:
189
		case LDAP_RES_COMPARE:
190
			fprintf(stderr, "compare");
191
			break;
192
		case LDAP_REQ_ABANDON_30:
193
			fprintf(stderr, "abandon");
194
			break;
195
		case LDAP_REQ_EXTENDED:
196
		case LDAP_RES_EXTENDED:
197
			fprintf(stderr, "extended");
198
			break;
199
		}
200
		break;
201
	case BER_CLASS_PRIVATE:
202
		fprintf(stderr, "class: private(%u) type: ", root->be_class);
203
		fprintf(stderr, "encoding (%lu) type: ", root->be_encoding);
204
		break;
205
	case BER_CLASS_CONTEXT:
206
		fprintf(stderr, "class: context(%u) type: ", root->be_class);
207
		switch (context) {
208
		case LDAP_REQ_BIND:
209
			switch(root->be_type) {
210
			case LDAP_AUTH_SIMPLE:
211
				fprintf(stderr, "auth simple");
212
				break;
213
			}
214
			break;
215
		case LDAP_REQ_SEARCH:
216
			switch(root->be_type) {
217
			case LDAP_FILT_AND:
218
				fprintf(stderr, "and");
219
				break;
220
			case LDAP_FILT_OR:
221
				fprintf(stderr, "or");
222
				break;
223
			case LDAP_FILT_NOT:
224
				fprintf(stderr, "not");
225
				break;
226
			case LDAP_FILT_EQ:
227
				fprintf(stderr, "equal");
228
				break;
229
			case LDAP_FILT_SUBS:
230
				fprintf(stderr, "substring");
231
				break;
232
			case LDAP_FILT_GE:
233
				fprintf(stderr, "greater-or-equal");
234
				break;
235
			case LDAP_FILT_LE:
236
				fprintf(stderr, "less-or-equal");
237
				break;
238
			case LDAP_FILT_PRES:
239
				fprintf(stderr, "presence");
240
				break;
241
			case LDAP_FILT_APPR:
242
				fprintf(stderr, "approximate");
243
				break;
244
			}
245
			break;
246
		}
247
		break;
248
	default:
249
		fprintf(stderr, "class: <INVALID>(%u) type: ", root->be_class);
250
		break;
251
	}
252
	fprintf(stderr, "(%lu) encoding %lu ",
253
	    root->be_type, root->be_encoding);
254
255
	if (constructed)
256
		root->be_encoding = constructed;
257
258
	switch (root->be_encoding) {
259
	case BER_TYPE_BOOLEAN:
260
		if (ber_get_boolean(root, &d) == -1) {
261
			fprintf(stderr, "<INVALID>\n");
262
			break;
263
		}
264
		fprintf(stderr, "%s(%d)\n", d ? "true" : "false", d);
265
		break;
266
	case BER_TYPE_INTEGER:
267
		if (ber_get_integer(root, &v) == -1) {
268
			fprintf(stderr, "<INVALID>\n");
269
			break;
270
		}
271
		fprintf(stderr, "value %lld\n", v);
272
		break;
273
	case BER_TYPE_ENUMERATED:
274
		if (ber_get_enumerated(root, &v) == -1) {
275
			fprintf(stderr, "<INVALID>\n");
276
			break;
277
		}
278
		fprintf(stderr, "value %lld\n", v);
279
		break;
280
	case BER_TYPE_BITSTRING:
281
		if (ber_get_bitstring(root, (void *)&buf, &len) == -1) {
282
			fprintf(stderr, "<INVALID>\n");
283
			break;
284
		}
285
		fprintf(stderr, "hexdump ");
286
		for (i = 0; i < len; i++)
287
			fprintf(stderr, "%02x", buf[i]);
288
		fprintf(stderr, "\n");
289
		break;
290
	case BER_TYPE_OBJECT:
291
		if (ber_get_oid(root, &o) == -1) {
292
			fprintf(stderr, "<INVALID>\n");
293
			break;
294
		}
295
		fprintf(stderr, "\n");
296
		break;
297
	case BER_TYPE_OCTETSTRING:
298
		if (ber_get_nstring(root, (void *)&buf, &len) == -1) {
299
			fprintf(stderr, "<INVALID>\n");
300
			break;
301
		}
302
		if ((visbuf = malloc(len * 4 + 1)) != NULL) {
303
			strvisx(visbuf, buf, len, 0);
304
			fprintf(stderr, "string \"%s\"\n",  visbuf);
305
			free(visbuf);
306
		}
307
		break;
308
	case BER_TYPE_NULL:	/* no payload */
309
	case BER_TYPE_EOC:
310
	case BER_TYPE_SEQUENCE:
311
	case BER_TYPE_SET:
312
	default:
313
		fprintf(stderr, "\n");
314
		break;
315
	}
316
317
	if (constructed && root->be_sub) {
318
		indent += 2;
319
		ldap_debug_elements(root->be_sub, context, NULL);
320
		indent -= 2;
321
	}
322
	if (root->be_next)
323
		ldap_debug_elements(root->be_next, context, NULL);
324
}
325