11 #include "ldapconfig.h"
13 #include "../back-bdb2/back-bdb2.h"
19 static char *tailorfile;
20 static char *inputfile;
25 fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
30 main( int argc, char **argv )
39 struct dbcache *db, *db2;
43 struct berval *vals[2];
45 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
47 while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
49 case 'd': /* turn on debugging */
50 ldap_debug = atoi( optarg );
53 case 'f': /* specify a tailor file */
54 tailorfile = strdup( optarg );
57 case 'i': /* input file */
58 inputfile = strdup( optarg );
61 case 'n': /* which config file db to index */
62 dbnum = atoi( optarg ) - 1;
70 if ( inputfile == NULL ) {
73 if ( freopen( inputfile, "r", stdin ) == NULL ) {
80 * initialize stuff and figure out which backend we're dealing with
83 slap_init(SLAP_TOOL_MODE, "ldif2id2children");
84 read_config( tailorfile );
87 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
88 if ( strcasecmp( backends[dbnum].be_type, "bdb2" )
93 if ( dbnum == nbackends ) {
94 fprintf( stderr, "No bdb2 database found in config file\n" );
97 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
98 fprintf( stderr, "Database number selected via -n is out of range\n" );
99 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
101 } else if ( strcasecmp( backends[dbnum].be_type, "bdb2" ) != 0 ) {
102 fprintf( stderr, "Database number %d selected via -n is not an bdb2 database\n", dbnum );
107 be = &backends[dbnum];
109 /* disable write sync'ing */
110 li = (struct ldbminfo *) be->be_private;
111 li->li_dbcachewsync = 0;
114 * first, make the dn2id index
117 if ( (db = bdb2i_cache_open( be, "dn2id", BDB2_SUFFIX, LDBM_NEWDB ))
119 perror( "dn2id file" );
131 char *type, *val, *s;
135 ldbm_datum_init( key );
136 ldbm_datum_init( data );
138 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
142 len = strlen( line );
143 while ( lcur + len + 1 > lmax ) {
145 buf = (char *) ch_realloc( buf, lmax );
147 strcpy( buf + lcur, line );
152 if ( line[0] == '\n' || stop && buf && *buf ) {
153 if ( *buf != '\n' ) {
154 if (isdigit((unsigned char) *buf)) {
161 while ( (linep = ldif_getline( &s )) != NULL ) {
163 if ( ldif_parse_line( linep, &type, &val,
165 Debug( LDAP_DEBUG_PARSE,
166 "bad line %d in entry ending at line %d ignored\n",
167 elineno, lineno, 0 );
171 if ( strcmp( type, "dn" ) == 0 )
175 if ( linep == NULL ) {
176 fprintf( stderr, "entry %ld has no dn\n",
179 key.dptr = dn_normalize_case( val );
180 key.dsize = strlen( val ) + 1;
181 data.dptr = (char *) &id;
182 data.dsize = sizeof(ID);
183 if ( ldbm_store( db->dbc_db, key, data,
184 LDBM_REPLACE ) != 0 ) {
185 perror( "dn2id ldbm_store..." );
199 * next, make the id2children index
202 if ( (db2 = bdb2i_cache_open( be, "id2children", BDB2_SUFFIX,
203 LDBM_NEWDB )) == NULL ) {
204 perror( "id2children file" );
217 char *type, *val, *s, *dn;
223 ldbm_datum_init( key );
224 ldbm_datum_init( data );
226 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
229 len = strlen( line );
230 while ( lcur + len + 1 > lmax ) {
232 buf = (char *) ch_realloc( buf, lmax );
234 strcpy( buf + lcur, line );
239 if ( line[0] == '\n' || stop && buf && *buf ) {
240 if ( * buf != '\n' ) {
243 while ( (linep = ldif_getline( &s )) != NULL ) {
244 if ( ldif_parse_line( linep, &type, &val,
246 Debug( LDAP_DEBUG_PARSE,
247 "bad line %d ignored\n",
252 if ( strcmp( type, "dn" ) == 0 )
256 if ( linep == NULL ) {
257 fprintf( stderr, "entry %ld has no dn\n",
260 if ( (dn = dn_parent( be, val ))
265 dn_normalize_case( dn );
266 key.dsize = strlen( dn ) + 1;
268 data = ldbm_fetch( db->dbc_db,
271 if ( data.dptr == NULL ) {
272 dn_normalize_case( val );
273 if ( ! be_issuffix( be,
275 Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
282 (void) memcpy( (char *) &pid,
283 data.dptr, sizeof(ID) );
285 ldbm_datum_free( db->dbc_db, data);
288 sprintf( buf2, "%c%ld", EQ_PREFIX, pid );
290 key.dsize = strlen( buf2 ) + 1;
291 if ( bdb2i_idl_insert_key( be, db2, key, id )
293 perror( "bdb2i_idl_insert_key" );
304 slap_shutdown(dbnum);