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