]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
Apply ksp's LDBM DB2 patch (after tab cleanup).
[openldap] / servers / slapd / tools / ldif2ldbm.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/ctype.h>
7 #include <ac/socket.h>
8 #include <ac/unistd.h>
9 #include <ac/wait.h>
10
11 #include <sys/param.h>
12
13 #include "ldapconfig.h"
14 #include "../slap.h"
15 #include "../back-ldbm/back-ldbm.h"
16 #include "ldif.h"
17
18 #define INDEXCMD                "ldif2index"
19 #define ID2ENTRYCMD             "ldif2id2entry"
20 #define ID2CHILDRENCMD          "ldif2id2children"
21 #define MAXARGS                 100
22
23 int             ldap_debug;
24 int             ldap_syslog;
25 int             ldap_syslog_level;
26 int             global_schemacheck;
27 long            num_entries_sent;
28 long            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 void fork_child( char *prog, char *args[] );
44 static void     wait4kids( int nkidval );
45
46 static char     *indexcmd;
47 static char     *tailorfile;
48 static char     *inputfile;
49 static int      maxkids = 1;
50 static int      nkids;
51
52 static void
53 usage( char *name )
54 {
55         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
56         exit( 1 );
57 }
58
59 int
60 main( int argc, char **argv )
61 {
62         int             i, stop, status;
63         char            *linep, *buf, *sbindir;
64         char            *args[10];
65         char            buf2[20], buf3[20];
66         char            line[BUFSIZ];
67         char            cmd[MAXPATHLEN];
68         int             lineno, elineno;
69         int             lmax, lcur;
70         int             dbnum;
71         ID              id;
72         Backend         *be = NULL;
73         struct berval   bv;
74         struct berval   *vals[2];
75         Avlnode         *avltypes = NULL;
76
77         sbindir = DEFAULT_SBINDIR;
78         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
79         dbnum = -1;
80         while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
81                 switch ( i ) {
82                 case 'd':       /* turn on debugging */
83                         ldap_debug = atoi( optarg );
84                         break;
85
86                 case 's':       /* alternate sbindir (index cmd location) */
87                 case 'e':       /* accept -e for backwards compatibility */
88                         sbindir = strdup( optarg );
89                         break;
90
91                 case 'f':       /* specify a tailor file */
92                         tailorfile = strdup( optarg );
93                         break;
94
95                 case 'i':       /* input file */
96                         inputfile = strdup( optarg );
97                         break;
98
99                 case 'j':       /* number of parallel index procs */
100                         maxkids = atoi( optarg );
101                         break;
102
103                 case 'n':       /* which config file db to index */
104                         dbnum = atoi( optarg ) - 1;
105                         break;
106
107                 default:
108                         usage( argv[0] );
109                         break;
110                 }
111         }
112         if ( inputfile == NULL ) {
113                 usage( argv[0] );
114         } else {
115                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
116                         perror( inputfile );
117                         exit( 1 );
118                 }
119         }
120
121         /*
122          * initialize stuff and figure out which backend we're dealing with
123          */
124
125         init();
126         read_config( tailorfile, &be, NULL );
127
128         if ( dbnum == -1 ) {
129                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
130                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
131                             == 0 ) {
132                                 break;
133                         }
134                 }
135                 if ( dbnum == nbackends ) {
136                         fprintf( stderr, "No ldbm database found in config file\n" );
137                         exit( 1 );
138                 }
139         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
140                 fprintf( stderr, "Database number selected via -n is out of range\n" );
141                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
142                 exit( 1 );
143         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
144                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
145                 exit( 1 );
146         }
147         be = &backends[dbnum];
148
149         /*
150          * generate the id2entry index
151          */
152
153         i = 0;
154         sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
155         args[i++] = cmd;
156         args[i++] = "-i";
157         args[i++] = inputfile;
158         args[i++] = "-f";
159         args[i++] = tailorfile;
160         args[i++] = "-n";
161         sprintf( buf2, "%d", dbnum+1 );
162         args[i++] = buf2;
163         if ( ldap_debug ) {
164                 sprintf( buf3, "%d", ldap_debug );
165                 args[i++] = "-d";
166                 args[i++] = buf3;
167         }
168         args[i++] = NULL;
169         fork_child( cmd, args );
170
171         /*
172          * generate the dn2id and id2children indexes
173          */
174
175         i = 0;
176         sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
177         args[i++] = cmd;
178         args[i++] = "-i";
179         args[i++] = inputfile;
180         args[i++] = "-f";
181         args[i++] = tailorfile;
182         args[i++] = "-n";
183         sprintf( buf2, "%d", dbnum+1 );
184         args[i++] = buf2;
185         if ( ldap_debug ) {
186                 sprintf( buf3, "%d", ldap_debug );
187                 args[i++] = "-d";
188                 args[i++] = buf3;
189         }
190         args[i++] = NULL;
191         fork_child( cmd, args );
192
193         /*
194          * generate the attribute indexes
195          */
196
197         i = 0;
198         sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
199         args[i++] = cmd;
200         args[i++] = "-i";
201         args[i++] = inputfile;
202         args[i++] = "-f";
203         args[i++] = tailorfile;
204         args[i++] = "-n";
205         sprintf( buf2, "%d", dbnum+1 );
206         args[i++] = buf2;
207         if ( ldap_debug ) {
208                 sprintf( buf3, "%d", ldap_debug );
209                 args[i++] = "-d";
210                 args[i++] = buf3;
211         }
212         args[i++] = NULL;               /* will hold the attribute name */
213         args[i++] = NULL;
214
215         id = 0;
216         stop = 0;
217         buf = NULL;
218         lineno = 0;
219         lcur = lmax = 0;
220         vals[0] = &bv;
221         vals[1] = NULL;
222         while ( ! stop ) {
223                 char            *type, *val, *s;
224                 int             vlen, indexmask, syntaxmask;
225                 Datum           key, data;
226
227                 memset( &key, 0, sizeof( key ));
228                 memset( &data, 0, sizeof( data ));
229
230                 if ( fgets( line, sizeof(line), stdin ) != NULL ) {
231                         int     len;
232
233                         lineno++;
234                         len = strlen( line );
235                         while ( lcur + len + 1 > lmax ) {
236                                 lmax += BUFSIZ;
237                                 buf = (char *) ch_realloc( buf, lmax );
238                         }
239                         strcpy( buf + lcur, line );
240                         lcur += len;
241                 } else {
242                         stop = 1;
243                 }
244                 if ( line[0] == '\n' || stop && buf && *buf ) {
245                         id++;
246                         s = buf;
247                         elineno = 0;
248                         while ( (linep = str_getline( &s )) != NULL ) {
249                                 elineno++;
250                                 if ( str_parse_line( linep, &type, &val, &vlen )
251                                     != 0 ) {
252                                         Debug( LDAP_DEBUG_PARSE,
253                             "bad line %d in entry ending at line %d ignored\n",
254                                             elineno, lineno, 0 );
255                                         continue;
256                                 }
257
258                                 if ( !isascii( *type ) || isdigit( *type ) )
259                                         continue;
260
261                                 type = strdup( type );
262                                 if ( avl_insert( &avltypes, type, strcasecmp,
263                                     avl_dup_error ) != 0 ) {
264                                         free( type );
265                                 } else {
266                                         attr_masks( be->be_private, type,
267                                             &indexmask, &syntaxmask );
268                                         if ( indexmask ) {
269                                                 args[i - 2] = type;
270                                                 fork_child( cmd, args );
271                                         }
272                                 }
273                         }
274                         *buf = '\0';
275                         lcur = 0;
276                 }
277         }
278         (*be->be_close)( be );
279
280         wait4kids( -1 );
281
282         exit( 0 );
283 }
284
285 static void
286 fork_child( char *prog, char *args[] )
287 {
288         int     status, pid;
289
290         wait4kids( maxkids );
291
292         switch ( pid = fork() ) {
293         case 0:         /* child */
294                 execvp( prog, args );
295                 fprintf( stderr, "%s: ", prog );
296                 perror( "execv" );
297                 exit( -1 );
298                 break;
299
300         case -1:        /* trouble */
301                 fprintf( stderr, "Could not fork to run %s\n", prog );
302                 perror( "fork" );
303                 break;
304
305         default:        /* parent */
306                 nkids++;
307                 break;
308         }
309 }
310
311 static void
312 wait4kids( int nkidval )
313 {
314         int             status;
315         unsigned char   *p;
316
317         while ( nkids >= nkidval ) {
318                 wait( &status );
319                 p = (unsigned char *) &status;
320                 if ( p[sizeof(int) - 1] == 0177 ) {
321                         fprintf( stderr,
322                             "stopping: child stopped with signal %d\n",
323                             p[sizeof(int) - 2] );
324                 } else if ( p[sizeof(int) - 1] != 0 ) {
325                         fprintf( stderr, 
326                             "stopping: child terminated with signal %d\n",
327                             p[sizeof(int) - 1] );
328                         exit( p[sizeof(int) - 1] );
329                 } else if ( p[sizeof(int) - 2] != 0 ) {
330                         fprintf( stderr, 
331                             "stopping: child exited with status %d\n",
332                             p[sizeof(int) - 2] );
333                         exit( p[sizeof(int) - 2] );
334                 } else {
335                         nkids--;
336                 }
337         }
338 }