xref: /netbsd-src/external/bsd/openldap/dist/servers/slapd/slapindex.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: slapindex.c,v 1.1.1.4 2014/05/28 09:58:48 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2014 The OpenLDAP Foundation.
7  * Portions Copyright 1998-2003 Kurt D. Zeilenga.
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 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 Kurt Zeilenga for inclusion
20  * in OpenLDAP Software.
21  */
22 
23 #include "portable.h"
24 
25 #include <stdio.h>
26 
27 #include <ac/stdlib.h>
28 
29 #include <ac/ctype.h>
30 #include <ac/string.h>
31 #include <ac/socket.h>
32 #include <ac/unistd.h>
33 
34 #include "slapcommon.h"
35 
36 int
37 slapindex( int argc, char **argv )
38 {
39 	ID id;
40 	int rc = EXIT_SUCCESS;
41 	const char *progname = "slapindex";
42 	AttributeDescription *ad, **adv = NULL;
43 
44 	slap_tool_init( progname, SLAPINDEX, argc, argv );
45 
46 	if( !be->be_entry_open ||
47 		!be->be_entry_close ||
48 		!( be->be_entry_first || be->be_entry_first_x ) ||
49 		!be->be_entry_next  ||
50 		!be->be_entry_reindex )
51 	{
52 		fprintf( stderr, "%s: database doesn't support necessary operations.\n",
53 			progname );
54 		exit( EXIT_FAILURE );
55 	}
56 
57 	argc -= optind;
58 	if ( argc > 0 ) {
59 		const char *text;
60 		int i;
61 
62 		argv = &argv[optind];
63 		adv = (AttributeDescription **)argv;
64 
65 		for (i = 0; i < argc; i++ ) {
66 			ad = NULL;
67 			rc = slap_str2ad( argv[i], &ad, &text );
68 			if ( rc != LDAP_SUCCESS ) {
69 				fprintf( stderr, "slap_str2ad(%s) failed %d (%s)\n",
70 					argv[i], rc, ldap_err2string( rc ));
71 				exit( EXIT_FAILURE );
72 			}
73 			adv[i] = ad;
74 		}
75 	}
76 
77 	if( be->be_entry_open( be, 0 ) != 0 ) {
78 		fprintf( stderr, "%s: could not open database.\n",
79 			progname );
80 		exit( EXIT_FAILURE );
81 	}
82 
83 	if ( be->be_entry_first ) {
84 		id = be->be_entry_first( be );
85 
86 	} else {
87 		assert( be->be_entry_first_x != NULL );
88 		id = be->be_entry_first_x( be, NULL, LDAP_SCOPE_DEFAULT, NULL );
89 	}
90 
91 	for ( ; id != NOID; id = be->be_entry_next( be ) ) {
92 		int rtn;
93 
94 		if( verbose ) {
95 			printf("indexing id=%08lx\n", (long) id );
96 		}
97 
98 		rtn =  be->be_entry_reindex( be, id, adv );
99 
100 		if( rtn != LDAP_SUCCESS ) {
101 			rc = EXIT_FAILURE;
102 			if( continuemode ) continue;
103 			break;
104 		}
105 	}
106 
107 	(void) be->be_entry_close( be );
108 
109 	if ( slap_tool_destroy())
110 		rc = EXIT_FAILURE;
111 	return( rc );
112 }
113