]> git.sur5r.net Git - openldap/blob - servers/slapd/tools/ldif2ldbm.c
merged with autoconf branch
[openldap] / servers / slapd / tools / ldif2ldbm.c
1 #include "portable.h"
2
3 #include <stdio.h>
4
5 #include <ac/string.h>
6 #include <ac/socket.h>
7
8 #include <sys/param.h>
9
10 #include "../slap.h"
11 #include "../back-ldbm/back-ldbm.h"
12 #include "ldapconfig.h"
13
14 #define INDEXCMD                "ldif2index"
15 #define ID2ENTRYCMD             "ldif2id2entry"
16 #define ID2CHILDRENCMD          "ldif2id2children"
17 #define MAXARGS                 100
18
19 extern void             attr_index_config();
20 extern char             *str_getline();
21 extern int              strcasecmp();
22 extern int              nbackends;
23 extern Backend          *backends;
24 extern int              ldap_debug;
25
26 int             ldap_debug;
27 int             ldap_syslog;
28 int             ldap_syslog_level;
29 int             global_schemacheck;
30 int             num_entries_sent;
31 int             num_bytes_sent;
32 int             active_threads;
33 char            *default_referral;
34 struct objclass *global_oc;
35 time_t          currenttime;
36 pthread_t       listener_tid;
37 pthread_mutex_t num_sent_mutex;
38 pthread_mutex_t entry2str_mutex;
39 pthread_mutex_t active_threads_mutex;
40 pthread_mutex_t new_conn_mutex;
41 pthread_mutex_t currenttime_mutex;
42 pthread_mutex_t replog_mutex;
43 pthread_mutex_t ops_mutex;
44 pthread_mutex_t regex_mutex;
45
46 static void     fork_child();
47 static void     wait4kids();
48
49 static char     *indexcmd;
50 static char     *tailorfile;
51 static char     *inputfile;
52 static int      maxkids = 1;
53 static int      nkids;
54  
55 static void
56 usage( char *name )
57 {
58         fprintf( stderr, "usage: %s -i inputfile [-d debuglevel] [-f configfile] [-j #jobs] [-n databasenumber] [-s sbindir]\n", name );
59         exit( 1 );
60 }
61
62 main( int argc, char **argv )
63 {
64         int             i, stop, status;
65         char            *linep, *buf, *sbindir;
66         char            *args[10];
67         char            buf2[20], buf3[20];
68         char            line[BUFSIZ];
69         char            cmd[MAXPATHLEN];
70         int             lineno, elineno;
71         int             lmax, lcur;
72         int             dbnum;
73         ID              id;
74         Backend         *be = NULL;
75         struct berval   bv;
76         struct berval   *vals[2];
77         Avlnode         *avltypes = NULL;
78         extern char     *optarg;
79
80         sbindir = DEFAULT_SBINDIR;
81         tailorfile = SLAPD_DEFAULT_CONFIGFILE;
82         dbnum = -1;
83         while ( (i = getopt( argc, argv, "d:e:s:f:i:j:n:" )) != EOF ) {
84                 switch ( i ) {
85                 case 'd':       /* turn on debugging */
86                         ldap_debug = atoi( optarg );
87                         break;
88
89                 case 's':       /* alternate sbindir (index cmd location) */
90                 case 'e':       /* accept -e for backwards compatibility */
91                         sbindir = strdup( optarg );
92                         break;
93
94                 case 'f':       /* specify a tailor file */
95                         tailorfile = strdup( optarg );
96                         break;
97
98                 case 'i':       /* input file */
99                         inputfile = strdup( optarg );
100                         break;
101
102                 case 'j':       /* number of parallel index procs */
103                         maxkids = atoi( optarg );
104                         break;
105
106                 case 'n':       /* which config file db to index */
107                         dbnum = atoi( optarg ) - 1;
108                         break;
109
110                 default:
111                         usage( argv[0] );
112                         break;
113                 }
114         }
115         if ( inputfile == NULL ) {
116                 usage( argv[0] );
117         } else {
118                 if ( freopen( inputfile, "r", stdin ) == NULL ) {
119                         perror( inputfile );
120                         exit( 1 );
121                 }
122         }
123
124         /*
125          * initialize stuff and figure out which backend we're dealing with
126          */
127
128         init();
129         read_config( tailorfile, &be, NULL );
130
131         if ( dbnum == -1 ) {
132                 for ( dbnum = 0; dbnum < nbackends; dbnum++ ) {
133                         if ( strcasecmp( backends[dbnum].be_type, "ldbm" )
134                             == 0 ) {
135                                 break;
136                         }
137                 }
138                 if ( dbnum == nbackends ) {
139                         fprintf( stderr, "No ldbm database found in config file\n" );
140                         exit( 1 );
141                 }
142         } else if ( dbnum < 0 || dbnum > (nbackends-1) ) {
143                 fprintf( stderr, "Database number selected via -n is out of range\n" );
144                 fprintf( stderr, "Must be in the range 1 to %d (number of databases in the config file)\n", nbackends );
145                 exit( 1 );
146         } else if ( strcasecmp( backends[dbnum].be_type, "ldbm" ) != 0 ) {
147                 fprintf( stderr, "Database number %d selected via -n is not an ldbm database\n", dbnum );
148                 exit( 1 );
149         }
150         be = &backends[dbnum];
151
152         /*
153          * generate the id2entry index
154          */
155
156         i = 0;
157         sprintf( cmd, "%s/%s", sbindir, ID2ENTRYCMD );
158         args[i++] = cmd;
159         args[i++] = "-i";
160         args[i++] = inputfile;
161         args[i++] = "-f";
162         args[i++] = tailorfile;
163         args[i++] = "-n";
164         sprintf( buf2, "%d", dbnum+1 );
165         args[i++] = buf2;
166         if ( ldap_debug ) {
167                 sprintf( buf3, "%d", ldap_debug );
168                 args[i++] = "-d";
169                 args[i++] = buf3;
170         }
171         args[i++] = NULL;
172         fork_child( cmd, args );
173
174         /*
175          * generate the dn2id and id2children indexes
176          */
177
178         i = 0;
179         sprintf( cmd, "%s/%s", sbindir, ID2CHILDRENCMD );
180         args[i++] = cmd;
181         args[i++] = "-i";
182         args[i++] = inputfile;
183         args[i++] = "-f";
184         args[i++] = tailorfile;
185         args[i++] = "-n";
186         sprintf( buf2, "%d", dbnum+1 );
187         args[i++] = buf2;
188         if ( ldap_debug ) {
189                 sprintf( buf3, "%d", ldap_debug );
190                 args[i++] = "-d";
191                 args[i++] = buf3;
192         }
193         args[i++] = NULL;
194         fork_child( cmd, args );
195
196         /*
197          * generate the attribute indexes
198          */
199
200         i = 0;
201         sprintf( cmd, "%s/%s", sbindir, INDEXCMD );
202         args[i++] = cmd;
203         args[i++] = "-i";
204         args[i++] = inputfile;
205         args[i++] = "-f";
206         args[i++] = tailorfile;
207         args[i++] = "-n";
208         sprintf( buf2, "%d", dbnum+1 );
209         args[i++] = buf2;
210         if ( ldap_debug ) {
211                 sprintf( buf3, "%d", ldap_debug );
212                 args[i++] = "-d";
213                 args[i++] = buf3;
214         }
215         args[i++] = NULL;               /* will hold the attribute name */
216         args[i++] = NULL;
217
218         id = 0;
219         stop = 0;
220         buf = NULL;
221         lineno = 0;
222         lcur = lmax = 0;
223         vals[0] = &bv;
224         vals[1] = NULL;
225         while ( ! stop ) {
226                 char            *type, *val, *s;
227                 int             vlen, indexmask, syntaxmask;
228                 Datum           key, 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 }