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