xref: /openbsd-src/usr.sbin/ldapctl/ldapctl.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ldapctl.c,v 1.9 2016/02/02 18:18:04 jca Exp $	*/
2 
3 /*
4  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
5  * Copyright (c) 2007, 2008 Reyk Floeter <reyk@vantronix.net>
6  * Copyright (c) 2005 Claudio Jeker <claudio@openbsd.org>
7  * Copyright (c) 2004, 2005 Esben Norby <norby@openbsd.org>
8  * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/queue.h>
27 #include <sys/un.h>
28 #include <sys/tree.h>
29 
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <net/if.h>
33 #include <net/if_media.h>
34 #include <net/if_types.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <event.h>
43 
44 #include "ldapd.h"
45 
46 enum action {
47 	NONE,
48 	SHOW_STATS,
49 	LOG_VERBOSE,
50 	LOG_BRIEF,
51 	COMPACT_DB,
52 	INDEX_DB
53 };
54 
55 __dead void	 usage(void);
56 void		 show_stats(struct imsg *imsg);
57 void		 show_dbstats(const char *prefix, struct btree_stat *st);
58 void		 show_nsstats(struct imsg *imsg);
59 int		 compact_db(const char *path);
60 int		 compact_namespace(struct namespace *ns, const char *datadir);
61 int		 compact_namespaces(const char *datadir);
62 int		 index_namespace(struct namespace *ns, const char *datadir);
63 int		 index_namespaces(const char *datadir);
64 
65 __dead void
66 usage(void)
67 {
68 	extern char *__progname;
69 
70 	fprintf(stderr,
71 	    "usage: %s [-v] [-f file] [-r directory] [-s socket] "
72 	    "command [argument ...]\n",
73 	    __progname);
74 	exit(1);
75 }
76 
77 int
78 compact_db(const char *path)
79 {
80 	struct btree	*bt;
81 	int		 rc;
82 
83 	log_info("compacting database %s", path);
84 	bt = btree_open(path, BT_NOSYNC | BT_REVERSEKEY, 0644);
85 	if (bt == NULL)
86 		return -1;
87 
88 	do {
89 		if ((rc = btree_compact(bt)) == -1 && errno == EBUSY)
90 			usleep(100000);
91 	} while (rc == -1 && errno == EBUSY);
92 
93 	btree_close(bt);
94 	return rc;
95 }
96 
97 int
98 compact_namespace(struct namespace *ns, const char *datadir)
99 {
100 	char		*path;
101 
102 	if (asprintf(&path, "%s/%s_data.db", datadir, ns->suffix) < 0)
103 		return -1;
104 	if (compact_db(path) != 0) {
105 		log_warn("%s", path);
106 		free(path);
107 		return -1;
108 	}
109 	free(path);
110 
111 	if (asprintf(&path, "%s/%s_indx.db", datadir, ns->suffix) < 0)
112 		return -1;
113 	if (compact_db(path) != 0) {
114 		log_warn("%s", path);
115 		free(path);
116 		return -1;
117 	}
118 	free(path);
119 
120 	return 0;
121 }
122 
123 int
124 compact_namespaces(const char *datadir)
125 {
126 	struct namespace	*ns;
127 
128 	TAILQ_FOREACH(ns, &conf->namespaces, next) {
129 		if (SLIST_EMPTY(&ns->referrals))
130 		    continue;
131 		if (compact_namespace(ns, datadir) != 0)
132 			return -1;
133 	}
134 
135 	return 0;
136 }
137 
138 int
139 index_namespace(struct namespace *ns, const char *datadir)
140 {
141 	struct btval		 key, val;
142 	struct btree		*data_db, *indx_db;
143 	struct cursor		*cursor;
144 	struct ber_element	*elm;
145 	char			*path;
146 	long long int		 ncomplete = 0;
147 	int			 i, rc;
148 
149 	log_info("indexing namespace %s", ns->suffix);
150 
151 	if (asprintf(&path, "%s/%s_data.db", DATADIR, ns->suffix) < 0)
152 		return -1;
153 	data_db = btree_open(path, BT_NOSYNC | BT_REVERSEKEY, 0644);
154 	free(path);
155 	if (data_db == NULL)
156 		return -1;
157 
158 	if (asprintf(&path, "%s/%s_indx.db", datadir, ns->suffix) < 0)
159 		return -1;
160 	indx_db = btree_open(path, BT_NOSYNC, 0644);
161 	free(path);
162 	if (indx_db == NULL) {
163 		btree_close(data_db);
164 		return -1;
165 	}
166 
167 	if ((cursor = btree_cursor_open(data_db)) == NULL) {
168 		btree_close(data_db);
169 		btree_close(indx_db);
170 		return -1;
171 	}
172 
173 	bzero(&key, sizeof(key));
174 	bzero(&val, sizeof(val));
175 
176 	for (;;) {
177 		for (;;) {
178 			ns->indx_txn = btree_txn_begin(indx_db, 0);
179 			if (ns->indx_txn == NULL && errno == EBUSY)
180 				usleep(100000);
181 			else
182 				break;
183 		}
184 
185 		if (ns->indx_txn == NULL) {
186 			log_warn("failed to start transaction");
187 			break;
188 		}
189 
190 		for (i = 0; i < 100; i++) {
191 			rc = btree_cursor_get(cursor, &key, &val, BT_NEXT);
192 			if (rc != BT_SUCCESS)
193 				break;
194 			if ((elm = db2ber(&val, ns->compression_level)) == NULL)
195 				continue;
196 			rc = index_entry(ns, &key, elm);
197 			ber_free_elements(elm);
198 			btval_reset(&key);
199 			btval_reset(&val);
200 			if (rc != 0)
201 				break;
202 			++ncomplete;
203 		}
204 
205 		if (btree_txn_commit(ns->indx_txn) != BT_SUCCESS)
206 			break;
207 
208 		if (i != 100)
209 			break;
210 	}
211 
212 	btree_cursor_close(cursor);
213 	btree_close(data_db);
214 	btree_close(indx_db);
215 
216 	return 0;
217 }
218 
219 int
220 index_namespaces(const char *datadir)
221 {
222 	struct namespace	*ns;
223 
224 	TAILQ_FOREACH(ns, &conf->namespaces, next) {
225 		if (SLIST_EMPTY(&ns->referrals))
226 			continue;
227 		if (index_namespace(ns, datadir) != 0)
228 			return -1;
229 	}
230 
231 	return 0;
232 }
233 
234 int
235 ssl_load_certfile(struct ldapd_config *env, const char *name, u_int8_t flags)
236 {
237 	return 0;
238 }
239 
240 int
241 main(int argc, char *argv[])
242 {
243 	int			 ctl_sock;
244 	int			 done = 0, verbose = 0;
245 	ssize_t			 n;
246 	int			 ch;
247 	enum action		 action = NONE;
248 	const char		*datadir = DATADIR;
249 	struct stat		 sb;
250 	const char		*sock = LDAPD_SOCKET;
251 	char			*conffile = CONFFILE;
252 	struct sockaddr_un	 sun;
253 	struct imsg		 imsg;
254 	struct imsgbuf		 ibuf;
255 
256 	log_init(1);
257 
258 	while ((ch = getopt(argc, argv, "f:r:s:v")) != -1) {
259 		switch (ch) {
260 		case 'f':
261 			conffile = optarg;
262 			break;
263 		case 'r':
264 			datadir = optarg;
265 			break;
266 		case 's':
267 			sock = optarg;
268 			break;
269 		case 'v':
270 			verbose = 1;
271 			break;
272 		default:
273 			usage();
274 			/* NOTREACHED */
275 		}
276 	}
277 	argc -= optind;
278 	argv += optind;
279 
280 	if (argc == 0)
281 		usage();
282 
283 	if (stat(datadir, &sb) == -1)
284 		err(1, "%s", datadir);
285 	if (!S_ISDIR(sb.st_mode))
286 		errx(1, "%s is not a directory", datadir);
287 
288 	log_verbose(verbose);
289 
290 	if (strcmp(argv[0], "stats") == 0)
291 		action = SHOW_STATS;
292 	else if (strcmp(argv[0], "compact") == 0)
293 		action = COMPACT_DB;
294 	else if (strcmp(argv[0], "index") == 0)
295 		action = INDEX_DB;
296 	else if (strcmp(argv[0], "log") == 0) {
297 		if (argc != 2)
298 			usage();
299 		if (strcmp(argv[1], "verbose") == 0)
300 			action = LOG_VERBOSE;
301 		else if (strcmp(argv[1], "brief") == 0)
302 			action = LOG_BRIEF;
303 		else
304 			usage();
305 	} else
306 		usage();
307 
308 	if (action == COMPACT_DB || action == INDEX_DB) {
309 		if (parse_config(conffile) != 0)
310 			exit(2);
311 
312 		if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
313 			err(1, "pledge");
314 
315 		if (action == COMPACT_DB)
316 			return compact_namespaces(datadir);
317 		else
318 			return index_namespaces(datadir);
319 	}
320 
321 	/* connect to ldapd control socket */
322 	if ((ctl_sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
323 		err(1, "socket");
324 
325 	bzero(&sun, sizeof(sun));
326 	sun.sun_family = AF_UNIX;
327 	strlcpy(sun.sun_path, sock, sizeof(sun.sun_path));
328 	if (connect(ctl_sock, (struct sockaddr *)&sun, sizeof(sun)) == -1)
329 		err(1, "connect: %s", sock);
330 
331 	imsg_init(&ibuf, ctl_sock);
332 	done = 0;
333 
334 	if (pledge("stdio", NULL) == -1)
335 		err(1, "pledge");
336 
337 	/* process user request */
338 	switch (action) {
339 	case SHOW_STATS:
340 		imsg_compose(&ibuf, IMSG_CTL_STATS, 0, 0, -1, NULL, 0);
341 		break;
342 	case LOG_VERBOSE:
343 		verbose = 1;
344 		/* FALLTHROUGH */
345 	case LOG_BRIEF:
346 		imsg_compose(&ibuf, IMSG_CTL_LOG_VERBOSE, 0, 0, -1,
347 		    &verbose, sizeof(verbose));
348 		printf("logging request sent.\n");
349 		done = 1;
350 		break;
351 	case NONE:
352 		break;
353 	case COMPACT_DB:
354 	case INDEX_DB:
355 		fatal("internal error");
356 	}
357 
358 	while (ibuf.w.queued)
359 		if (msgbuf_write(&ibuf.w) <= 0 && errno != EAGAIN)
360 			err(1, "write error");
361 
362 	while (!done) {
363 		if ((n = imsg_read(&ibuf)) == -1 && errno != EAGAIN)
364 			errx(1, "imsg_read error");
365 		if (n == 0)
366 			errx(1, "pipe closed");
367 
368 		while (!done) {
369 			if ((n = imsg_get(&ibuf, &imsg)) == -1)
370 				errx(1, "imsg_get error");
371 			if (n == 0)
372 				break;
373 			switch (imsg.hdr.type) {
374 			case IMSG_CTL_STATS:
375 				show_stats(&imsg);
376 				break;
377 			case IMSG_CTL_NSSTATS:
378 				show_nsstats(&imsg);
379 				break;
380 			case IMSG_CTL_END:
381 				done = 1;
382 				break;
383 			case NONE:
384 				break;
385 			}
386 			imsg_free(&imsg);
387 		}
388 	}
389 	close(ctl_sock);
390 
391 	return (0);
392 }
393 
394 void
395 show_stats(struct imsg *imsg)
396 {
397 	struct ldapd_stats	*st;
398 
399 	st = imsg->data;
400 
401 	printf("start time: %s", ctime(&st->started_at));
402 	printf("requests: %llu\n", st->requests);
403 	printf("search requests: %llu\n", st->req_search);
404 	printf("bind requests: %llu\n", st->req_bind);
405 	printf("modify requests: %llu\n", st->req_mod);
406 	printf("timeouts: %llu\n", st->timeouts);
407 	printf("unindexed searches: %llu\n", st->unindexed);
408 	printf("active connections: %u\n", st->conns);
409 	printf("active searches: %u\n", st->searches);
410 }
411 
412 #define ZDIV(t,n)	((n) == 0 ? 0 : (float)(t) / (n))
413 
414 void
415 show_dbstats(const char *prefix, struct btree_stat *st)
416 {
417 	printf("%s timestamp: %s", prefix, ctime(&st->created_at));
418 	printf("%s page size: %u\n", prefix, st->psize);
419 	printf("%s depth: %u\n", prefix, st->depth);
420 	printf("%s revisions: %u\n", prefix, st->revisions);
421 	printf("%s entries: %llu\n", prefix, st->entries);
422 	printf("%s branch/leaf/overflow pages: %u/%u/%u\n",
423 	    prefix, st->branch_pages, st->leaf_pages, st->overflow_pages);
424 
425 	printf("%s cache size: %u of %u (%.1f%% full)\n", prefix,
426 	    st->cache_size, st->max_cache,
427 	    100 * ZDIV(st->cache_size, st->max_cache));
428 	printf("%s page reads: %llu\n", prefix, st->reads);
429 	printf("%s cache hits: %llu (%.1f%%)\n", prefix, st->hits,
430 	    100 * ZDIV(st->hits, (st->hits + st->reads)));
431 }
432 
433 void
434 show_nsstats(struct imsg *imsg)
435 {
436 	struct ns_stat		*nss;
437 
438 	nss = imsg->data;
439 
440 	printf("\nsuffix: %s\n", nss->suffix);
441 	show_dbstats("data", &nss->data_stat);
442 	show_dbstats("indx", &nss->indx_stat);
443 }
444 
445