]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2id2children.c
07e2b616382818d52729f3c634db5d167e455fcf
[openldap] / servers / slapd / tools / ldif2id2children.c
1 #include "portable.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include <ac/ctype.h>
7 #include <ac/string.h>
8 #include <ac/socket.h>
9 #include <ac/unistd.h>
10
11 #include "ldapconfig.h"
12 #include "../slap.h"
13 #include "../back-ldbm/back-ldbm.h"
14
15 #include "ldif.h"
16
17 #define MAXARGS                 100
18
19 int             lineno;
20 int             ldap_debug;
21 int             ldap_syslog;
22 int             ldap_syslog_level;
23 long            num_entries_sent;
24 long            num_bytes_sent;
25 int             active_threads;
26 char            *default_referral;
27 time_t          currenttime;
28 pthread_t       listener_tid;
29 pthread_mutex_t num_sent_mutex;
30 pthread_mutex_t entry2str_mutex;
31 pthread_mutex_t active_threads_mutex;
32 pthread_mutex_t new_conn_mutex;
33 pthread_mutex_t currenttime_mutex;
34 pthread_mutex_t replog_mutex;
35 pthread_mutex_t ops_mutex;
36 pthread_mutex_t regex_mutex;
37 #ifdef SLAPD_CRYPT
38 pthread_mutex_t crypt_mutex;
39 #endif
40
41 static char     *tailorfile;
42 static char     *inputfile;
43  
44 static void
45 usage( char *name )
46 {
47         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-n databasenumber]\n", name );
48         exit( 1 );
49 }
50
51 int
52 main( int argc, char **argv )
53 {
54         int             i, cargc, indb, stop, status;
55         char            *cargv[MAXARGS];
56         char            *defargv[MAXARGS];
57         char            *linep, *buf;
58         char            line[BUFSIZ];
59         int             lineno, elineno;
60         int             lmax, lcur;
61         int             dbnum;
62         ID              id;
63         struct dbcache  *db, *db2;
64         Backend         *be = NULL;
65         struct ldbminfo *li;
66         struct berval   bv;
67         struct berval   *vals[2];
68         Avlnode         *avltypes = NULL;
69
70         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
71         dbnum = -1;
72         while ( (i = getopt( argc, argv, "d:f:i:n:" )) != EOF ) {
73                 switch ( i ) {
74                 case 'd':       /* turn on debugging */
75                         ldap_debug = atoi( optarg );
76                         break;
77
78                 case 'f':       /* specify a tailor file */
79                         tailorfile = strdup( optarg );
80                         break;
81
82                 case 'i':       /* input file */
83                         inputfile = strdup( optarg );
84                         break;
85
86                 case 'n':       /* which config file db to index */
87                         dbnum = atoi( optarg ) - 1;
88                         break;
89
90                 default:
91                         usage( argv[0] );
92                         break;
93                 }
94         }
95         if ( inputfile == NULL ) {
96                 usage( argv[0] );
97         } else {
98                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
99                         perror( inputfile );
100                         exit( 1 );
101                 }
102         }
103
104         /*
105          * initialize stuff and figure out which backend we're dealing with
106          */
107
108         init();
109         read_config( tailorfile, &be, NULL );
110
111         if ( dbnum == -1 ) {
112                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
113                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
114                             == 0 ) {
115                                 break;
116                         }
117                 }
118                 if ( dbnum == nbackends ) {
119                         fprintf( stderr, "No ldbm database found in config file\n" );
120                         exit( 1 );
121                 }
122         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
123                 fprintf( stderr, "Database number selected via -n is out of range\n" );
124                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
125                 exit( 1 );
126         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
127                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
128                 exit( 1 );
129         }
130         be = &backends[dbnum];
131
132         /* disable write sync'ing */
133         li = (struct ldbminfo *) be->be_private;
134         li->li_dbcachewsync = 0;
135
136         /*
137          * first, make the dn2id index
138          */
139
140         if ( (db = ldbm_cache_open( be, "dn2id", LDBM_SUFFIX, LDBM_NEWDB ))
141             == NULL ) {
142                 perror( "dn2id file" );
143                 exit( 1 );
144         }
145
146         id = 0;
147         stop = 0;
148         lineno = 0;
149         buf = NULL;
150         lcur = lmax = 0;
151         vals[0] = &bv;
152         vals[1] = NULL;
153         while ( ! stop ) {
154                 char            *type, *val, *s;
155                 int             vlen;
156                 Datum           key, data;
157
158                 ldbm_datum_init( key );
159                 ldbm_datum_init( data );
160
161                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
162                         int     len;
163
164                         lineno++;
165                         len = strlen( line );
166                         while ( lcur + len + 1 > lmax ) {
167                                 lmax += BUFSIZ;
168                                 buf = (char *) ch_realloc( buf, lmax );
169                         }
170                         strcpy( buf + lcur, line );
171                         lcur += len;
172                 } else {
173                         stop = 1;
174                 }
175                 if ( line[0] == '\n' || stop && buf && *buf ) {
176                         if ( *buf != '\n' ) {
177                                 if (isdigit(*buf)) {
178                                         id = atol(buf);
179                                 } else {
180                                         id++;
181                                 }
182                                 s = buf;
183                                 elineno = 0;
184                                 while ( (linep = str_getline( &s )) != NULL ) {
185                                         elineno++;
186                                         if ( str_parse_line( linep, &type, &val,
187                                             &vlen ) != 0 ) {
188                                                 Debug( LDAP_DEBUG_PARSE,
189                             "bad line %d in entry ending at line %d ignored\n",
190                                                     elineno, lineno, 0 );
191                                                 continue;
192                                         }
193
194                                         if ( strcmp( type, "dn" ) == 0 )
195                                                 break;
196                                 }
197
198                                 if ( linep == NULL ) {
199                                         fprintf( stderr, "entry %lu has no dn\n",
200                                             id );
201                                 } else {
202                                         key.dptr = dn_normalize_case( val );
203                                         key.dsize = strlen( val ) + 1;
204                                         data.dptr = (char *) &id;
205                                         data.dsize = sizeof(ID);
206                                         if ( ldbm_store( db->dbc_db, key, data,
207                                             LDBM_REPLACE ) != 0 ) {
208                                                 perror( "dn2id ldbm_store" );
209                                                 exit( 1 );
210                                         }
211                                 }
212                         }
213                         *buf = '\0';
214                         lcur = 0;
215                         line[0] = '\0';
216                 }
217         }
218         if ( buf )
219                 free( buf );
220
221         /*
222          * next, make the id2children index
223          */
224
225         if ( (db2 = ldbm_cache_open( be, "id2children", LDBM_SUFFIX,
226             LDBM_NEWDB )) == NULL ) {
227                 perror( "id2children file" );
228                 exit( 1 );
229         }
230
231         rewind( stdin );
232         id = 0;
233         stop = 0;
234         buf = NULL;
235         lineno = 0;
236         lcur = lmax = 0;
237         vals[0] = &bv;
238         vals[1] = NULL;
239         while ( ! stop ) {
240                 char    *type, *val, *s, *dn;
241                 int     vlen;
242                 ID      pid;
243                 char    buf2[20];
244                 Datum   key, data;
245
246                 ldbm_datum_init( key );
247                 ldbm_datum_init( data );
248
249                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
250                         int     len;
251
252                         len = strlen( line );
253                         while ( lcur + len + 1 > lmax ) {
254                                 lmax += BUFSIZ;
255                                 buf = (char *) ch_realloc( buf, lmax );
256                         }
257                         strcpy( buf + lcur, line );
258                         lcur += len;
259                 } else {
260                         stop = 1;
261                 }
262                 if ( line[0] == '\n' || stop && buf && *buf ) {
263                         if ( * buf != '\n' ) {
264                                 id++;
265                                 s = buf;
266                                 while ( (linep = str_getline( &s )) != NULL ) {
267                                         if ( str_parse_line( linep, &type, &val,
268                                             &vlen ) != 0 ) {
269                                                 Debug( LDAP_DEBUG_PARSE,
270                                                     "bad line %d ignored\n",
271                                                     lineno, 0, 0 );
272                                                 continue;
273                                         }
274
275                                         if ( strcmp( type, "dn" ) == 0 )
276                                                 break;
277                                 }
278
279                                 if ( linep == NULL ) {
280                                         fprintf( stderr, "entry %lu has no dn\n",
281                                             id );
282                                 } else {
283                                         if ( (dn = dn_parent( be, val ))
284                                             == NULL ) {
285                                                 pid = 0;
286                                         } else {
287                                                 key.dptr =
288                                                     dn_normalize_case( dn );
289                                                 key.dsize = strlen( dn ) + 1;
290
291                                                 data = ldbm_fetch( db->dbc_db,
292                                                     key );
293                                                 free( dn );
294                                                 if ( data.dptr == NULL ) {
295                                                         dn_normalize( val );
296                                                         if ( ! be_issuffix( be,
297                                                             val ) ) {
298         Debug( LDAP_DEBUG_PARSE, "no parent \"%s\" of \"%s\"\n", dn, val, 0 );
299                                                         }
300                                                         *buf = '\0';
301                                                         lcur = 0;
302                                                         line[0] = '\0';
303                                                         continue;
304                                                 }
305                                                 (void) memcpy( (char *) &pid,
306                                                     data.dptr, sizeof(ID) );
307                                         }
308
309                                         sprintf( buf2, "%c%ld", EQ_PREFIX, pid );
310                                         key.dptr = buf2;
311                                         key.dsize = strlen( buf2 ) + 1;
312                                         if ( idl_insert_key( be, db2, key, id )
313                                             != 0 ) {
314                                                 perror( "idl_insert_key" );
315                                                 exit( 1 );
316                                         }
317                                 }
318                         }
319                         *buf = '\0';
320                         lcur = 0;
321                         line[0] = '\0';
322                 }
323         }
324         (*be->be_close)( be );
325
326         exit( 0 );
327 }