11 #include "ldapconfig.h"
13 #include "../back-ldbm/back-ldbm.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 )
32 int i, cargc, indb, stop, status;
34 char *defargv[MAXARGS];
41 struct dbcache *db, *db2;
45 struct berval *vals[2];
46 Avlnode *avltypes = NULL;
48 tailorfile = SLAPD_DEFAULT_CONFIGFILE;
50 while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
52 case 'd': /* turn on debugging */
53 ldap_debug = atoi( optarg );
56 case 'f': /* specify a tailor file */
57 tailorfile = strdup( optarg );
60 case 'i': /* input file */
61 inputfile = strdup( optarg );
64 case 'n': /* which config file db to index */
65 dbnum = atoi( optarg ) - 1;
73 if ( inputfile == NULL ) {
76 if ( freopen( inputfile, "r", stdin ) == NULL ) {
83 * initialize stuff and figure out which backend we're dealing with
87 read_config( tailorfile, &be, NULL );
90 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
91 if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
96 if ( dbnum == nbackends ) {
97 fprintf( stderr, "No ldbm database found in config file\n" );
100 } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
101 fprintf( stderr, "Database number selected via -n is out of range\n" );
102 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
104 } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
105 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
108 be = &backends[dbnum];
110 /* disable write sync'ing */
111 li = (struct ldbminfo *) be->be_private;
112 li->li_dbcachewsync = 0;
115 * first, make the dn2id index
118 if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_NEWDB ))
120 perror( "dn2id file" );
132 char *type, *val, *s;
136 ldbm_datum_init( key );
137 ldbm_datum_init( data );
139 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
143 len = strlen( line );
144 while ( lcur + len + 1 > lmax ) {
146 buf = (char *) ch_realloc( buf, lmax );
148 strcpy( buf + lcur, line );
153 if ( line[0] == '\n' || stop && buf && *buf ) {
154 if ( *buf != '\n' ) {
162 while ( (linep = str_getline( &s )) != NULL ) {
164 if ( str_parse_line( linep, &type, &val,
166 Debug( LDAP_DEBUG_PARSE,
167 "bad line %d in entry ending at line %d ignored\n",
168 elineno, lineno, 0 );
172 if ( strcmp( type, "dn" ) == 0 )
176 if ( linep == NULL ) {
177 fprintf( stderr, "entry %lu has no dn\n",
180 key.dptr = dn_normalize_case( val );
181 key.dsize = strlen( val ) + 1;
182 data.dptr = (char *) &id;
183 data.dsize = sizeof(ID);
184 if ( ldbm_store( db->dbc_db, key, data,
185 LDBM_REPLACE ) != 0 ) {
186 perror( "dn2id ldbm_store" );
200 * next, make the id2children index
203 if ( (db2 = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
204 LDBM_NEWDB )) == NULL ) {
205 perror( "id2children file" );
218 char *type, *val, *s, *dn;
224 ldbm_datum_init( key );
225 ldbm_datum_init( data );
227 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
230 len = strlen( line );
231 while ( lcur + len + 1 > lmax ) {
233 buf = (char *) ch_realloc( buf, lmax );
235 strcpy( buf + lcur, line );
240 if ( line[0] == '\n' || stop && buf && *buf ) {
241 if ( * buf != '\n' ) {
244 while ( (linep = str_getline( &s )) != NULL ) {
245 if ( str_parse_line( linep, &type, &val,
247 Debug( LDAP_DEBUG_PARSE,
248 "bad line %d ignored\n",
253 if ( strcmp( type, "dn" ) == 0 )
257 if ( linep == NULL ) {
258 fprintf( stderr, "entry %lu has no dn\n",
261 if ( (dn = dn_parent( be, val ))
266 dn_normalize_case( dn );
267 key.dsize = strlen( dn ) + 1;
269 data = ldbm_fetch( db->dbc_db,
272 if ( data.dptr == NULL ) {
274 if ( ! be_issuffix( be,
276 Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
283 (void) memcpy( (char *) &pid,
284 data.dptr, sizeof(ID) );
287 sprintf( buf2, "%c%ld", EQ_PREFIX, pid );
289 key.dsize = strlen( buf2 ) + 1;
290 if ( idl_insert_key( be, db2, key, id )
292 perror( "idl_insert_key" );
302 (*be->be_close)( be );